db_memrw.c revision 1.23 1 /* $NetBSD: db_memrw.c,v 1.23 2003/07/15 03:36:17 lukem 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/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.23 2003/07/15 03:36:17 lukem Exp $");
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61
62 #include <uvm/uvm_extern.h>
63
64 #include <machine/db_machdep.h>
65 #include <machine/pte.h>
66 #include <m68k/cacheops.h>
67
68 #include <sun3/sun3/machdep.h>
69
70 #include <ddb/db_access.h>
71
72 extern char etext[]; /* defined by the linker */
73 extern char kernel_text[]; /* locore.s */
74
75 static void db_write_text __P((char *, size_t size, char *));
76
77
78 /*
79 * Read bytes from kernel address space for debugger.
80 * This used to check for valid PTEs, but now that
81 * traps in DDB work correctly, "Just Do It!"
82 */
83 void
84 db_read_bytes(addr, size, data)
85 db_addr_t addr;
86 size_t size;
87 char *data;
88 {
89 char *src = (char*)addr;
90
91 if (size == 4) {
92 *((int*)data) = *((int*)src);
93 return;
94 }
95
96 if (size == 2) {
97 *((short*)data) = *((short*)src);
98 return;
99 }
100
101 while (size > 0) {
102 --size;
103 *data++ = *src++;
104 }
105 }
106
107 /*
108 * Write bytes somewhere in kernel text.
109 * Makes text page writable temporarily.
110 */
111 static void
112 db_write_text(dst, size, data)
113 char *dst;
114 size_t size;
115 char *data;
116 {
117 int oldpte, tmppte;
118 vaddr_t pgva, prevpg;
119
120 /* Prevent restoring a garbage PTE. */
121 if (size <= 0)
122 return;
123
124 pgva = m68k_trunc_page((long)dst);
125
126 goto firstpage;
127 do {
128
129 /*
130 * If we are on a new page, restore the PTE
131 * for the previous page, and make the new
132 * page writable.
133 */
134 pgva = m68k_trunc_page((long)dst);
135 if (pgva != prevpg) {
136 /*
137 * Restore old PTE. No cache flush,
138 * because the tmp PTE has no-cache.
139 */
140 set_pte(prevpg, oldpte);
141
142 firstpage:
143 /*
144 * Flush the VAC to prevent a cache hit
145 * on the old, read-only PTE.
146 */
147 #ifdef HAVECACHE
148 if (cache_size)
149 cache_flush_page(pgva);
150 #endif
151 oldpte = get_pte(pgva);
152 if ((oldpte & PG_VALID) == 0) {
153 printf(" address %p not a valid page\n", dst);
154 return;
155 }
156
157 /*
158 * Make the pte writable and non-cached.
159 */
160 tmppte = oldpte;
161 #ifdef _SUN3_
162 tmppte |= (PG_WRITE | PG_NC);
163 #endif
164 #ifdef _SUN3X_
165 tmppte &= ~MMU_SHORT_PTE_WP;
166 tmppte |= MMU_SHORT_PTE_CI;
167 #endif
168
169 set_pte(pgva, tmppte);
170 prevpg = pgva;
171 }
172
173 /* Now we can write in this page of kernel text... */
174 *dst++ = *data++;
175
176 } while (--size > 0);
177
178 /* Restore old PTE for the last page touched. */
179 set_pte(prevpg, oldpte);
180
181 /* Finally, clear the instruction cache. */
182 ICIA();
183 }
184
185 /*
186 * Write bytes to kernel address space for debugger.
187 */
188 void
189 db_write_bytes(addr, size, data)
190 db_addr_t addr;
191 size_t size;
192 char *data;
193 {
194 char *dst = (char *)addr;
195
196 /* If any part is in kernel text, use db_write_text() */
197 if ((dst < etext) && ((dst + size) > kernel_text)) {
198 db_write_text(dst, size, data);
199 return;
200 }
201
202 if (size == 4) {
203 *((int*)dst) = *((int*)data);
204 return;
205 }
206
207 if (size == 2) {
208 *((short*)dst) = *((short*)data);
209 return;
210 }
211
212 while (size > 0) {
213 --size;
214 *dst++ = *data++;
215 }
216 }
217
218