db_memrw.c revision 1.13 1 /* $NetBSD: db_memrw.c,v 1.13 1996/11/20 18:57:28 gwr 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.
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 REGENTS OR CONTRIBUTORS BE
30 * 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/proc.h>
57
58 #include <vm/vm.h>
59
60 #include <machine/pte.h>
61 #include <machine/db_machdep.h>
62
63 #include <ddb/db_access.h>
64
65 #include "cache.h"
66
67 /*
68 * Read bytes from kernel address space for debugger.
69 * This used to check for valid PTEs, but now that
70 * traps in DDB work correctly, "Just Do It!"
71 */
72 void
73 db_read_bytes(addr, size, data)
74 vm_offset_t addr;
75 register size_t size;
76 register char *data;
77 {
78 register char *src = (char*)addr;
79
80 if (size == 4) {
81 *((int*)data) = *((int*)src);
82 return;
83 }
84
85 if (size == 2) {
86 *((short*)data) = *((short*)src);
87 return;
88 }
89
90 while (size > 0) {
91 --size;
92 *data++ = *src++;
93 }
94 }
95
96 /*
97 * Write bytes somewhere in kernel text.
98 * Makes text page writable temporarily.
99 */
100 static void
101 db_write_text(addr, size, data)
102 vm_offset_t addr;
103 register size_t size;
104 register char *data;
105 {
106 register char *dst;
107 int ch, oldpte, tmppte;
108 vm_offset_t pgva, prevpg;
109
110 /* Prevent restoring a garbage PTE. */
111 if (size <= 0)
112 return;
113
114 dst = (char*)addr;
115 pgva = sun3_trunc_page((long)dst);
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 = sun3_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 /*
135 * Flush the VAC to prevent a cache hit
136 * on the old, read-only PTE.
137 */
138 #ifdef HAVECACHE
139 if (cache_size)
140 cache_flush_page(pgva);
141 #endif
142 oldpte = get_pte(pgva);
143 if ((oldpte & PG_VALID) == 0) {
144 printf(" address 0x%x not a valid page\n", dst);
145 return;
146 }
147 tmppte = oldpte | PG_WRITE | PG_NC;
148 set_pte(pgva, tmppte);
149
150 prevpg = pgva;
151 }
152
153 /* Now we can write in this page of kernel text... */
154 *dst++ = *data++;
155
156 } while (--size > 0);
157
158 /* Restore old PTE for the last page touched. */
159 set_pte(prevpg, oldpte);
160
161 /* Finally, clear the instruction cache. */
162 ICIA();
163 }
164
165 /*
166 * Write bytes to kernel address space for debugger.
167 */
168 extern char kernel_text[], etext[];
169 void
170 db_write_bytes(addr, size, data)
171 vm_offset_t addr;
172 register size_t size;
173 register char *data;
174 {
175 register char *dst = (char *)addr;
176
177 /* If any part is in kernel text, use db_write_text() */
178 if ((dst < etext) && ((dst + size) > kernel_text)) {
179 db_write_text(dst, size, data);
180 return;
181 }
182
183 if (size == 4) {
184 *((int*)dst) = *((int*)data);
185 return;
186 }
187
188 if (size == 2) {
189 *((short*)dst) = *((short*)data);
190 return;
191 }
192
193 while (size > 0) {
194 --size;
195 *dst++ = *data++;
196 }
197 }
198
199