Module Name:    src
Committed By:   reinoud
Date:           Wed Aug  1 10:22:20 UTC 2018

Added Files:
        src/sys/arch/usermode/usermode: cpufunc.S db_memrw.c kgdb_machdep.c

Log Message:
Add the kgdb meat


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/usermode/usermode/cpufunc.S \
    src/sys/arch/usermode/usermode/db_memrw.c \
    src/sys/arch/usermode/usermode/kgdb_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/sys/arch/usermode/usermode/cpufunc.S
diff -u /dev/null src/sys/arch/usermode/usermode/cpufunc.S:1.1
--- /dev/null	Wed Aug  1 10:22:20 2018
+++ src/sys/arch/usermode/usermode/cpufunc.S	Wed Aug  1 10:22:20 2018
@@ -0,0 +1,75 @@
+
+#include <machine/asm.h>
+#include "assym.h"
+
+#if defined(__i386__)
+
+ENTRY(breakpoint)
+	.byte 0xcc	// BKPT_INST
+	ret
+
+#error implement setjmp/longjmp for i386
+
+#elif defined(__amd64__)
+ENTRY(breakpoint)
+	.byte 0xcc	// BKPT_INST, int3
+	ret
+
+/*
+ * int setjmp(label_t *)
+ *
+ * Used primarily by DDB.
+ */
+ENTRY(setjmp)
+	/*
+	 * Only save registers that must be preserved across function
+	 * calls according to the ABI (%rbx, %rsp, %rbp, %r12-%r15)
+	 * and %rip.
+	 */
+	movq	%rdi,%rax
+	movq	%rbx,(%rax)
+	movq	%rsp,8(%rax)
+	movq	%rbp,16(%rax)
+	movq	%r12,24(%rax)
+	movq	%r13,32(%rax)
+	movq	%r14,40(%rax)
+	movq	%r15,48(%rax)
+	movq	(%rsp),%rdx
+	movq	%rdx,56(%rax)
+	xorl	%eax,%eax
+	ret
+END(setjmp)
+
+/*
+ * int longjmp(label_t *)
+ *
+ * Used primarily by DDB.
+ */
+ENTRY(longjmp)
+	movq	%rdi,%rax
+	movq	(%rax),%rbx
+	movq	8(%rax),%rsp
+	movq	16(%rax),%rbp
+	movq	24(%rax),%r12
+	movq	32(%rax),%r13
+	movq	40(%rax),%r14
+	movq	48(%rax),%r15
+	movq	56(%rax),%rdx
+	movq	%rdx,(%rsp)
+	movl	$1,%eax
+	ret
+END(longjmp)
+#elif defined(__arm__)
+
+ENTRY(breakpoint)
+	BKPT_ASM
+	mov pc, lr
+
+#error implement setjmp/longjmp for arm32
+
+#else
+
+#error port me 
+
+#endif
+
Index: src/sys/arch/usermode/usermode/db_memrw.c
diff -u /dev/null src/sys/arch/usermode/usermode/db_memrw.c:1.1
--- /dev/null	Wed Aug  1 10:22:20 2018
+++ src/sys/arch/usermode/usermode/db_memrw.c	Wed Aug  1 10:22:20 2018
@@ -0,0 +1,257 @@
+/*	$NetBSD: db_memrw.c,v 1.1 2018/08/01 10:22:20 reinoud Exp $	*/
+
+/*-
+ * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Gordon W. Ross and Jason R. Thorpe.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Interface to the debugger for virtual memory read/write.
+ * This file is shared by DDB and KGDB, and must work even
+ * when only KGDB is included (thus no db_printf calls).
+ *
+ * To write in the text segment, we have to first make
+ * the page writable, do the write, then restore the PTE.
+ * For writes outside the text segment, and all reads,
+ * just do the access -- if it causes a fault, the debugger
+ * will recover with a longjmp to an appropriate place.
+ *
+ * ALERT!  If you want to access device registers with a
+ * specific size, then the read/write functions have to
+ * make sure to do the correct sized pointer access.
+ *
+ * Modified for i386 from hp300 version by
+ * Jason R. Thorpe <[email protected]>.
+ *
+ * Basic copy to amd64 by fvdl.
+ * 
+ * i386 and amd64 merge by jym.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.1 2018/08/01 10:22:20 reinoud Exp $");
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+
+#include <machine/pmap.h>
+#include <machine/db_machdep.h>
+#include <uvm/uvm_extern.h>
+
+#include <ddb/db_access.h>
+#include <ddb/db_output.h>
+
+#include <machine/thunk.h>
+
+int
+db_validate_address(vaddr_t addr)
+{
+	struct proc *p = curproc;
+	struct pmap *pmap;
+
+	if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap ||
+	    addr >= VM_MIN_KERNEL_ADDRESS) {
+		/* XXX safe??? */
+		return false;
+	}
+
+	pmap = p->p_vmspace->vm_map.pmap;
+	return (pmap_extract(pmap, addr, NULL) == false);
+}
+
+/*
+ * Read bytes from kernel address space for debugger.
+ */
+void
+db_read_bytes(vaddr_t addr, size_t size, char *data)
+{
+	char *src;
+
+	src = (char *)addr;
+
+	if (db_validate_address((vaddr_t)src)) {
+		printf("address %p is invalid\n", src);
+		return;
+	}
+
+	if (size == 8) {
+		*((long *)data) = *((long *)src);
+		return;
+	}
+
+	if (size == 4) {
+		*((int *)data) = *((int *)src);
+		return;
+	}
+
+	if (size == 2) {
+		*((short *)data) = *((short *)src);
+		return;
+	}
+
+	while (size-- > 0) {
+		if (db_validate_address((vaddr_t)src)) {
+			printf("address %p is invalid\n", src);
+			return;
+		}
+
+		*data++ = *src++;
+	}
+}
+
+/*
+ * Write bytes somewhere in the kernel text.  Make the text
+ * pages writable temporarily.
+ */
+#if 0
+static void
+db_write_text(vaddr_t addr, size_t size, const char *data)
+{
+	panic("%s: implement me\n", __func__);
+
+	pt_entry_t *ppte, pte;
+	size_t limit;
+	char *dst;
+
+	if (size == 0)
+		return;
+
+	dst = (char *)addr;
+
+	do {
+		addr = (vaddr_t)dst;
+		/*
+		 * Get the PTE for the page.
+		 */
+		ppte = kvtopte(addr);
+		pte = *ppte;
+
+		if ((pte & PG_V) == 0) {
+			printf(" address %p not a valid page\n", dst);
+			return;
+		}
+
+		/*
+		 * Compute number of bytes that can be written
+		 * with this mapping and subtract it from the
+		 * total size.
+		 */
+		if (pte & PG_PS)
+			limit = NBPD_L2 - (addr & (NBPD_L2 - 1));
+		else
+			limit = PAGE_SIZE - (addr & PGOFSET);
+		if (limit > size)
+			limit = size;
+		size -= limit;
+
+		/*
+		 * Make the kernel text page writable.
+		 */
+		pmap_pte_clearbits(ppte, PG_KR);
+		pmap_pte_setbits(ppte, PG_KW);
+		pmap_update_pg(addr);
+
+		/*
+		 * MULTIPROCESSOR: no shootdown required as the PTE continues to
+		 * map the same page and other CPUs do not need write access.
+		 */
+
+		/*
+		 * Page is now writable.  Do as much access as we
+		 * can in this page.
+		 */
+		for (; limit > 0; limit--)
+			*dst++ = *data++;
+
+		/*
+		 * Turn the page back to read-only.
+		 */
+		pmap_pte_clearbits(ppte, PG_KW);
+		pmap_pte_setbits(ppte, PG_KR);
+		pmap_update_pg(addr);
+
+		/*
+		 * MULTIPROCESSOR: no shootdown required as all other CPUs
+		 * should be in CPUF_PAUSE state and will not cache the PTE
+		 * with the write access set.
+		 */
+	} while (size != 0);
+}
+#endif
+
+#include <machine/thunk.h>
+/*
+ * Write bytes to kernel address space for debugger.
+ */
+void
+db_write_bytes(vaddr_t addr, size_t size, const char *data)
+{
+//	extern struct bootspace bootspace;
+	char *dst;
+//	size_t i;
+
+	dst = (char *)addr;
+thunk_printf("\n%s : %p + %d\n", __func__, dst, (int) size);
+#if 0
+	// TODO: check if we in kernel range and if so, do the mmap dance
+	// ourselves?
+
+	/* If any part is in kernel text or rodata, use db_write_text() */
+	for (i = 0; i < BTSPACE_NSEGS; i++) {
+		if (bootspace.segs[i].type != BTSEG_TEXT &&
+		    bootspace.segs[i].type != BTSEG_RODATA) {
+			continue;
+		}
+		if (addr >= bootspace.segs[i].va &&
+		    addr < (bootspace.segs[i].va + bootspace.segs[i].sz)) {
+			db_write_text(addr, size, data);
+			return;
+		}
+	}
+#endif
+
+	dst = (char *)addr;
+
+	if (size == 8) {
+		*((long *)dst) = *((const long *)data);
+		return;
+	}
+
+	if (size == 4) {
+		*((int *)dst) = *((const int *)data);
+		return;
+	}
+
+	if (size == 2) {
+		*((short *)dst) = *((const short *)data);
+		return;
+	}
+
+	while (size-- > 0)
+		*dst++ = *data++;
+}
Index: src/sys/arch/usermode/usermode/kgdb_machdep.c
diff -u /dev/null src/sys/arch/usermode/usermode/kgdb_machdep.c:1.1
--- /dev/null	Wed Aug  1 10:22:20 2018
+++ src/sys/arch/usermode/usermode/kgdb_machdep.c	Wed Aug  1 10:22:20 2018
@@ -0,0 +1,324 @@
+/*	$NetBSD: kgdb_machdep.c,v 1.1 2018/08/01 10:22:20 reinoud Exp $	*/
+
+/*
+ * Copyright (c) 1996 Matthias Pfaller.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: kgdb_machdep.c,v 1.1 2018/08/01 10:22:20 reinoud Exp $");
+
+#include "opt_ddb.h"
+#include "opt_kgdb.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kgdb.h>
+#include <sys/socket.h>
+
+#include <uvm/uvm_extern.h>
+
+//#include <machine/frame.h>
+#include <machine/reg.h>
+#include <machine/trap.h>
+#include <machine/db_machdep.h>
+#include <machine/thunk.h>
+#include <netinet/in.h>
+
+
+/*
+ * Determine if the memory at va..(va+len) is valid.
+ */
+int
+kgdb_acc(vaddr_t va, size_t len)
+{
+	vaddr_t last_va;
+
+	last_va = va + len;
+	va  &= ~PGOFSET;
+	last_va &= ~PGOFSET;
+
+thunk_printf("%s: [%p .. %p]\n", __func__, (void *) va, (void *) last_va);
+	do {
+		if (db_validate_address(va))
+			return (0);
+		va  += PAGE_SIZE;
+	} while (va < last_va);
+
+	return (1);
+}
+
+/*
+ * Translate a trap number into a unix compatible signal value.
+ * (gdb only understands unix signal numbers).
+ */
+int 
+kgdb_signal(int type)
+{
+	return type;
+
+//	panic("%s", __func__);
+#if 0
+	switch (type) {
+	case T_BREAKPOINT:
+		return(SIGTRAP);
+	case -1:
+		return(SIGSEGV);
+	default:
+		return(SIGINT);
+	}
+#endif
+}
+
+/*
+ * Definitions exported from gdb.
+ */
+
+/*
+ * Translate the values stored in the kernel regs struct to the format
+ * understood by gdb.
+ */
+void
+kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
+{
+#ifdef __x86_64__
+	kgdb_reg_t *gregs = regs->uc_mcontext.__gregs;
+
+	gdb_regs[ 0] = gregs[_REG_RAX];
+	gdb_regs[ 1] = gregs[_REG_RBX];
+	gdb_regs[ 2] = gregs[_REG_RCX];
+	gdb_regs[ 3] = gregs[_REG_RDX];
+	gdb_regs[ 4] = gregs[_REG_RSI];
+	gdb_regs[ 5] = gregs[_REG_RDI];
+	gdb_regs[ 6] = gregs[_REG_RBP];
+	gdb_regs[ 7] = gregs[_REG_RSP];
+	gdb_regs[ 8] = gregs[_REG_R8];
+	gdb_regs[ 9] = gregs[_REG_R9];
+	gdb_regs[10] = gregs[_REG_R10];
+	gdb_regs[11] = gregs[_REG_R11];
+	gdb_regs[12] = gregs[_REG_R12];
+	gdb_regs[13] = gregs[_REG_R13];
+	gdb_regs[14] = gregs[_REG_R14];
+	gdb_regs[15] = gregs[_REG_R15];
+	gdb_regs[16] = gregs[_REG_RIP];
+	gdb_regs[17] = gregs[_REG_RFLAGS];
+	gdb_regs[18] = gregs[_REG_CS];
+	gdb_regs[19] = gregs[_REG_SS];
+		
+#elif defined(__i386)
+	gdb_regs[ 0] = regs->tf_eax;
+	gdb_regs[ 1] = regs->tf_ecx;
+	gdb_regs[ 2] = regs->tf_edx;
+	gdb_regs[ 3] = regs->tf_ebx;
+	gdb_regs[ 4] = regs->tf_esp;
+	gdb_regs[ 5] = regs->tf_ebp;
+	gdb_regs[ 6] = regs->tf_esi;
+	gdb_regs[ 7] = regs->tf_edi;
+	gdb_regs[ 8] = regs->tf_eip;
+	gdb_regs[ 9] = regs->tf_eflags;
+	gdb_regs[10] = regs->tf_cs;
+	gdb_regs[11] = regs->tf_ss;
+	gdb_regs[12] = regs->tf_ds;
+	gdb_regs[13] = regs->tf_es;
+	gdb_regs[14] = regs->tf_fs;
+	gdb_regs[15] = regs->tf_gs;
+
+/*XXX OOPS XXX? */
+#if 0
+	if (KERNELMODE(regs->tf_cs)) {
+		/*
+		 * Kernel mode - esp and ss not saved.
+		 */
+		gdb_regs[ 4] = (kgdb_reg_t)&regs->tf_esp; /* kernel stack
+							     pointer */
+		gdb_regs[11] = x86_getss();
+	}
+#endif
+#else
+#error port kgdb_machdep.c kgdb_getregs
+#endif
+}
+
+ /*
+ * And the reverse.
+ */
+
+void
+kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
+{
+#ifdef __x86_64__
+	kgdb_reg_t *gregs = regs->uc_mcontext.__gregs;
+
+	gregs[_REG_RAX] = gdb_regs[ 0];
+	gregs[_REG_RBX] = gdb_regs[ 1];
+	gregs[_REG_RCX] = gdb_regs[ 2];
+	gregs[_REG_RDX] = gdb_regs[ 3];
+	gregs[_REG_RSI] = gdb_regs[ 4];
+	gregs[_REG_RDI] = gdb_regs[ 5];
+	gregs[_REG_RBP] = gdb_regs[ 6];
+	gregs[_REG_RSP] = gdb_regs[ 7];
+	gregs[_REG_R8 ] = gdb_regs[ 8];
+	gregs[_REG_R9 ] = gdb_regs[ 9];
+	gregs[_REG_R10] = gdb_regs[10];
+	gregs[_REG_R11] = gdb_regs[11];
+	gregs[_REG_R12] = gdb_regs[12];
+	gregs[_REG_R13] = gdb_regs[13];
+	gregs[_REG_R14] = gdb_regs[14];
+	gregs[_REG_R15] = gdb_regs[15];
+	gregs[_REG_RIP] = gdb_regs[16];
+	gregs[_REG_RFLAGS] = gdb_regs[17];
+	gregs[_REG_CS ] = gdb_regs[18];
+	gregs[_REG_SS ] = gdb_regs[19];
+#elif defined(__i386)
+panic("%s", __func__);
+	regs->tf_eax    = gdb_regs[ 0];
+	regs->tf_ecx    = gdb_regs[ 1];
+	regs->tf_edx    = gdb_regs[ 2];
+	regs->tf_ebx    = gdb_regs[ 3];
+	regs->tf_ebp    = gdb_regs[ 5];
+	regs->tf_esi    = gdb_regs[ 6];
+	regs->tf_edi    = gdb_regs[ 7];
+	regs->tf_eip    = gdb_regs[ 8];
+	regs->tf_eflags = gdb_regs[ 9];
+	regs->tf_cs     = gdb_regs[10];
+	regs->tf_ds     = gdb_regs[12];
+	regs->tf_es     = gdb_regs[13];
+
+	if (KERNELMODE(regs->tf_cs) == 0) {
+		/*
+		 * Trapped in user mode - restore esp and ss.
+		 */
+		regs->tf_esp = gdb_regs[ 4];
+		regs->tf_ss  = gdb_regs[11];
+	}
+#else
+panic("%s", __func__);
+#endif
+}
+
+/*
+ * Trap into kgdb to wait for debugger to connect,
+ * noting on the console why nothing else is going on.
+ */
+void
+kgdb_connect(int verbose)
+{
+	if (kgdb_dev == NODEV)
+		return;
+
+	if (verbose)
+		printf("kgdb waiting...");
+
+	breakpoint();
+
+	if (verbose)
+		printf("connected.\n");
+
+	kgdb_debug_panic = 1;
+}
+
+/*
+ * Decide what to do on panic.
+ * (This is called by panic, like Debugger())
+ */
+void
+kgdb_panic(void)
+{
+	if (kgdb_dev != NODEV && kgdb_debug_panic) {
+		printf("entering kgdb\n");
+		kgdb_connect(kgdb_active == 0);
+	}
+}
+
+static int kgdb_socket, kgdb_fd;
+static int kgdb_connected;
+
+
+static void
+kgdb_get_connection(void)
+{
+	while (!kgdb_connected) {
+		thunk_printf("...[kgdb connecting]...");
+		kgdb_fd = thunk_gdb_accept(kgdb_socket);
+		if (kgdb_fd)
+			kgdb_connected = 1;
+	}
+	kgdb_active = 1;
+}
+
+static int
+kgdb_getc(void *arg)
+{
+	char ch;
+
+	while (thunk_kgdb_getc(kgdb_fd, &ch) < 0) {
+		kgdb_connected = 0;
+		kgdb_get_connection();
+	}
+//thunk_printf("[<%c]", ch);
+	return (int) ch;
+}
+
+
+static void
+kgdb_putc(void *arg, int ch_in)
+{
+	char ch = (char) ch_in;
+	while (thunk_kgdb_putc(kgdb_fd, ch) < 0) {
+		kgdb_connected = 0;
+		kgdb_get_connection();
+	}
+//thunk_printf("[>%c]", ch);
+}
+
+void
+kgdb_port_init(void)
+{
+	kgdb_connected = 0;
+
+	/* open our socket */
+	kgdb_socket = thunk_gdb_open();
+	if (kgdb_socket == 0) {
+		kgdb_dev = 0;
+		printf("aborting kgdb\n");
+		return;
+	}
+
+	/* signal we have a connection `dev' */
+	kgdb_dev = 0x123;
+	kgdb_attach(kgdb_getc, kgdb_putc, 0);
+}
+
+/*
+ * handle an trap instruction encountered from KGDB
+ */
+void
+kgdb_kernel_trap(int signo, vaddr_t pc, vaddr_t va, ucontext_t *ucp)
+{
+	kgdb_get_connection();
+
+thunk_printf("entering trap\n");
+thunk_printf("  signo %d, pc %p, va %p\n", signo, (void *) pc, (void *) va);
+	kgdb_trap(signo, ucp);
+}
+

Reply via email to