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