1 /* $NetBSD: db_memrw.c,v 1.22 2002/10/20 02:37:35 chs 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Interface to the debugger for virtual memory read/write. 41 * This file is shared by DDB and KGDB, and must work even 42 * when only KGDB is included (thus no db_printf calls). 43 * 44 * To write in the text segment, we have to first make 45 * the page writable, do the write, then restore the PTE. 46 * For writes outside the text segment, and all reads, 47 * just do the access -- if it causes a fault, the debugger 48 * will recover with a longjmp to an appropriate place. 49 * 50 * ALERT! If you want to access device registers with a 51 * specific size, then the read/write functions have to 52 * make sure to do the correct sized pointer access. 53 */ 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/proc.h> 58 59 #include <uvm/uvm_extern.h> 60 61 #include <machine/db_machdep.h> 62 #include <machine/pte.h> 63 #include <m68k/cacheops.h> 64 65 #include <sun3/sun3/machdep.h> 66 67 #include <ddb/db_access.h> 68 69 extern char etext[]; /* defined by the linker */ 70 extern char kernel_text[]; /* locore.s */ 71 72 static void db_write_text __P((char *, size_t size, char *)); 73 74 75 /* 76 * Read bytes from kernel address space for debugger. 77 * This used to check for valid PTEs, but now that 78 * traps in DDB work correctly, "Just Do It!" 79 */ 80 void 81 db_read_bytes(addr, size, data) 82 db_addr_t addr; 83 size_t size; 84 char *data; 85 { 86 char *src = (char*)addr; 87 88 if (size == 4) { 89 *((int*)data) = *((int*)src); 90 return; 91 } 92 93 if (size == 2) { 94 *((short*)data) = *((short*)src); 95 return; 96 } 97 98 while (size > 0) { 99 --size; 100 *data++ = *src++; 101 } 102 } 103 104 /* 105 * Write bytes somewhere in kernel text. 106 * Makes text page writable temporarily. 107 */ 108 static void 109 db_write_text(dst, size, data) 110 char *dst; 111 size_t size; 112 char *data; 113 { 114 int oldpte, tmppte; 115 vaddr_t pgva, prevpg; 116 117 /* Prevent restoring a garbage PTE. */ 118 if (size <= 0) 119 return; 120 121 pgva = m68k_trunc_page((long)dst); 122 123 goto firstpage; 124 do { 125 126 /* 127 * If we are on a new page, restore the PTE 128 * for the previous page, and make the new 129 * page writable. 130 */ 131 pgva = m68k_trunc_page((long)dst); 132 if (pgva != prevpg) { 133 /* 134 * Restore old PTE. No cache flush, 135 * because the tmp PTE has no-cache. 136 */ 137 set_pte(prevpg, oldpte); 138 139 firstpage: 140 /* 141 * Flush the VAC to prevent a cache hit 142 * on the old, read-only PTE. 143 */ 144 #ifdef HAVECACHE 145 if (cache_size) 146 cache_flush_page(pgva); 147 #endif 148 oldpte = get_pte(pgva); 149 if ((oldpte & PG_VALID) == 0) { 150 printf(" address %p not a valid page\n", dst); 151 return; 152 } 153 154 /* 155 * Make the pte writable and non-cached. 156 */ 157 tmppte = oldpte; 158 #ifdef _SUN3_ 159 tmppte |= (PG_WRITE | PG_NC); 160 #endif 161 #ifdef _SUN3X_ 162 tmppte &= ~MMU_SHORT_PTE_WP; 163 tmppte |= MMU_SHORT_PTE_CI; 164 #endif 165 166 set_pte(pgva, tmppte); 167 prevpg = pgva; 168 } 169 170 /* Now we can write in this page of kernel text... */ 171 *dst++ = *data++; 172 173 } while (--size > 0); 174 175 /* Restore old PTE for the last page touched. */ 176 set_pte(prevpg, oldpte); 177 178 /* Finally, clear the instruction cache. */ 179 ICIA(); 180 } 181 182 /* 183 * Write bytes to kernel address space for debugger. 184 */ 185 void 186 db_write_bytes(addr, size, data) 187 db_addr_t addr; 188 size_t size; 189 char *data; 190 { 191 char *dst = (char *)addr; 192 193 /* If any part is in kernel text, use db_write_text() */ 194 if ((dst < etext) && ((dst + size) > kernel_text)) { 195 db_write_text(dst, size, data); 196 return; 197 } 198 199 if (size == 4) { 200 *((int*)dst) = *((int*)data); 201 return; 202 } 203 204 if (size == 2) { 205 *((short*)dst) = *((short*)data); 206 return; 207 } 208 209 while (size > 0) { 210 --size; 211 *dst++ = *data++; 212 } 213 } 214 215