Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/usermode Allow for setting kernel breakpoints in ou...



details:   https://anonhg.NetBSD.org/src/rev/92071bea6aa4
branches:  trunk
changeset: 991867:92071bea6aa4
user:      reinoud <reinoud%NetBSD.org@localhost>
date:      Fri Aug 03 11:18:22 2018 +0000

description:
Allow for setting kernel breakpoints in our remote kgdb

diffstat:

 sys/arch/usermode/include/vmparam.h   |   4 +-
 sys/arch/usermode/usermode/db_memrw.c |  40 ++++++++++++++++++----------------
 sys/arch/usermode/usermode/pmap.c     |  23 ++++++++++++++-----
 3 files changed, 40 insertions(+), 27 deletions(-)

diffs (155 lines):

diff -r dddfc2b84e88 -r 92071bea6aa4 sys/arch/usermode/include/vmparam.h
--- a/sys/arch/usermode/include/vmparam.h       Fri Aug 03 09:54:40 2018 +0000
+++ b/sys/arch/usermode/include/vmparam.h       Fri Aug 03 11:18:22 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: vmparam.h,v 1.19 2018/08/01 12:09:02 reinoud Exp $ */
+/* $NetBSD: vmparam.h,v 1.20 2018/08/03 11:18:22 reinoud Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -43,7 +43,7 @@
 #define VM_MAX_ADDRESS         kmem_user_end
 #define VM_MAXUSER_ADDRESS     kmem_user_end
 #define VM_MIN_KERNEL_ADDRESS  kmem_kvm_start
-#define VM_MAX_KERNEL_ADDRESS  kmem_kvm_end
+#define VM_MAX_KERNEL_ADDRESS  kmem_k_end
 
 #define VM_PHYSSEG_STRAT       VM_PSTRAT_BIGFIRST
 #define VM_PHYSSEG_MAX         1
diff -r dddfc2b84e88 -r 92071bea6aa4 sys/arch/usermode/usermode/db_memrw.c
--- a/sys/arch/usermode/usermode/db_memrw.c     Fri Aug 03 09:54:40 2018 +0000
+++ b/sys/arch/usermode/usermode/db_memrw.c     Fri Aug 03 11:18:22 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: db_memrw.c,v 1.2 2018/08/01 10:27:28 reinoud Exp $     */
+/*     $NetBSD: db_memrw.c,v 1.3 2018/08/03 11:18:22 reinoud Exp $     */
 
 /*-
  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
@@ -53,11 +53,12 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.2 2018/08/01 10:27:28 reinoud Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.3 2018/08/03 11:18:22 reinoud Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
 #include <sys/systm.h>
+#include <sys/mman.h>
 
 #include <machine/pmap.h>
 #include <machine/db_machdep.h>
@@ -211,29 +212,30 @@
 void
 db_write_bytes(vaddr_t addr, size_t size, const char *data)
 {
-//     extern struct bootspace bootspace;
        char *dst;
-//     size_t i;
+       int ret;
 
        dst = (char *)addr;
        thunk_printf_debug("\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 (db_validate_address((vaddr_t)addr)) {
+               printf("address %p is invalid\n", (void *) addr);
+               return;
+       }
 
-       /* 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;
-               }
+       /*
+        * if we are in the kernel range, just allow writing by using
+        * mprotect(); Note that this needs an unprotected binary, set with
+        * `paxctl -agm netbsd`
+        */
+       if (addr > kmem_k_start) {
+               ret = thunk_mprotect((void *) trunc_page(addr), PAGE_SIZE,
+                       PROT_READ | PROT_WRITE | PROT_EXEC);
+               if (ret != 0)
+                       panic("please unprotect kernel binary with "
+                             "`paxctl -agm netbsd`");
+               assert(ret == 0);
        }
-#endif
 
        dst = (char *)addr;
 
diff -r dddfc2b84e88 -r 92071bea6aa4 sys/arch/usermode/usermode/pmap.c
--- a/sys/arch/usermode/usermode/pmap.c Fri Aug 03 09:54:40 2018 +0000
+++ b/sys/arch/usermode/usermode/pmap.c Fri Aug 03 11:18:22 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pmap.c,v 1.111 2018/08/03 06:52:50 reinoud Exp $ */
+/* $NetBSD: pmap.c,v 1.112 2018/08/03 11:18:22 reinoud Exp $ */
 
 /*-
  * Copyright (c) 2011 Reinoud Zandijk <reinoud%NetBSD.org@localhost>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.111 2018/08/03 06:52:50 reinoud Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.112 2018/08/03 11:18:22 reinoud Exp $");
 
 #include "opt_memsize.h"
 #include "opt_kmempages.h"
@@ -139,7 +139,7 @@
        struct pmap *pmap;
        paddr_t DRAM_cfg;
        paddr_t fpos, file_len;
-       paddr_t pv_fpos, tlb_fpos, pm_l1_fpos, pm_fpos;
+       paddr_t kernel_fpos, pv_fpos, tlb_fpos, pm_l1_fpos, pm_fpos;
        paddr_t wlen;
        paddr_t barrier_len;
        paddr_t pv_table_size;
@@ -281,9 +281,11 @@
        assert(err == 0);
 
        /* map the kernel at the start of the 'memory' file */
-       written = thunk_pwrite(mem_fh, (void *) kmem_k_start, kmem_k_length, 0);
+       kernel_fpos = 0;
+       written = thunk_pwrite(mem_fh, (void *) kmem_k_start, kmem_k_length,
+                       kernel_fpos);
        assert(written == kmem_k_length);
-       fpos = kmem_k_length;
+       fpos = kernel_fpos + kmem_k_length;
 
        /* initialize counters */
        free_start = fpos;     /* in physical space ! */
@@ -298,7 +300,7 @@
                (uint64_t) pv_table_size/1024, (uintptr_t) phys_npages);
 
        /* calculate number of pmap entries needed for a complete map */
-       pm_nentries = (kmem_k_start - VM_MIN_ADDRESS) / PAGE_SIZE;
+       pm_nentries = (kmem_k_end - VM_MIN_ADDRESS) / PAGE_SIZE;
        pm_entries_size = round_page(pm_nentries * sizeof(struct pv_entry *));
        thunk_printf_debug("tlb va->pa lookup table is %"PRIu64" KB for "
                "%d logical pages\n", pm_entries_size/1024, pm_nentries);
@@ -417,6 +419,15 @@
                pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
        }
        thunk_printf_debug("kernel pmap entries mem added to the kernel pmap\n");
+#if 0
+       /* not yet, or not needed */
+       for (pg = 0; pg < kmem_k_length; pg += PAGE_SIZE) {
+               pa = kernel_fpos + pg;
+               va = (vaddr_t) kmem_k_start + pg;
+               pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 0);
+       }
+       thunk_printf_debug("kernel mem added to the kernel pmap\n");
+#endif
 
        /* add file space to uvm's FREELIST */
        uvm_page_physload(atop(0),



Home | Main Index | Thread Index | Old Index