1 /* $NetBSD: db_memrw.c,v 1.8 2008/04/28 20:23:37 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross and Jeremy Cooper. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Interface to the debugger for virtual memory read/write. 34 * This file is shared by DDB and KGDB, and must work even 35 * when only KGDB is included (thus no db_printf calls). 36 * 37 * To write in the text segment, we have to first make 38 * the page writable, do the write, then restore the PTE. 39 * For writes outside the text segment, and all reads, 40 * just do the access -- if it causes a fault, the debugger 41 * will recover with a longjmp to an appropriate place. 42 * 43 * ALERT! If you want to access device registers with a 44 * specific size, then the read/write functions have to 45 * make sure to do the correct sized pointer access. 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.8 2008/04/28 20:23:37 martin Exp $"); 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/proc.h> 54 55 #include <uvm/uvm_extern.h> 56 57 #include <machine/db_machdep.h> 58 #include <machine/pte.h> 59 60 #include <sun2/sun2/machdep.h> 61 #include <sun2/sun2/control.h> 62 63 #include <ddb/db_access.h> 64 65 extern char etext[]; /* defined by the linker */ 66 extern char kernel_text[]; /* locore.s */ 67 68 static void db_write_text(char *, size_t size, const char *); 69 70 71 /* 72 * Read bytes from kernel address space for debugger. 73 * This used to check for valid PTEs, but now that 74 * traps in DDB work correctly, "Just Do It!" 75 */ 76 void 77 db_read_bytes(db_addr_t addr, size_t size, char *data) 78 { 79 char *src = (char *)addr; 80 81 if (size == 4) { 82 *((int *)data) = *((int *)src); 83 return; 84 } 85 86 if (size == 2) { 87 *((short *)data) = *((short *)src); 88 return; 89 } 90 91 while (size > 0) { 92 --size; 93 *data++ = *src++; 94 } 95 } 96 97 /* 98 * Write bytes somewhere in kernel text. 99 * Makes text page writable temporarily. 100 */ 101 static void 102 db_write_text(char *dst, size_t size, const char *data) 103 { 104 int oldpte, tmppte; 105 vaddr_t pgva, prevpg; 106 int old_ctx; 107 108 /* Prevent restoring a garbage PTE. */ 109 if (size <= 0) 110 return; 111 112 pgva = m68k_trunc_page((long)dst); 113 114 old_ctx = get_context(); 115 set_context(0); 116 117 goto firstpage; 118 do { 119 120 /* 121 * If we are on a new page, restore the PTE 122 * for the previous page, and make the new 123 * page writable. 124 */ 125 pgva = m68k_trunc_page((long)dst); 126 if (pgva != prevpg) { 127 /* 128 * Restore old PTE. No cache flush, 129 * because the tmp PTE has no-cache. 130 */ 131 set_pte(prevpg, oldpte); 132 133 firstpage: 134 oldpte = get_pte(pgva); 135 if ((oldpte & PG_VALID) == 0) { 136 printf(" address %p not a valid page\n", dst); 137 set_context(old_ctx); 138 return; 139 } 140 141 /* 142 * Make the pte writable and non-cached. 143 */ 144 tmppte = oldpte; 145 tmppte |= (PG_WRITE | PG_NC); 146 147 set_pte(pgva, tmppte); 148 prevpg = pgva; 149 } 150 151 /* Now we can write in this page of kernel text... */ 152 *dst++ = *data++; 153 154 } while (--size > 0); 155 156 /* Restore old PTE for the last page touched. */ 157 set_pte(prevpg, oldpte); 158 set_context(old_ctx); 159 } 160 161 /* 162 * Write bytes to kernel address space for debugger. 163 */ 164 void 165 db_write_bytes(db_addr_t addr, size_t size, const char *data) 166 { 167 char *dst = (char *)addr; 168 169 /* If any part is in kernel text, use db_write_text() */ 170 if ((dst < etext) && ((dst + size) > kernel_text)) { 171 db_write_text(dst, size, data); 172 return; 173 } 174 175 if (size == 4) { 176 *((int *)dst) = *((const int *)data); 177 return; 178 } 179 180 if (size == 2) { 181 *((short *)dst) = *((const short *)data); 182 return; 183 } 184 185 while (size > 0) { 186 --size; 187 *dst++ = *data++; 188 } 189 } 190 191