db_memrw.c revision 1.14 1 /* $NetBSD: db_memrw.c,v 1.14 1996/12/17 21:11:20 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/systm.h>
57 #include <sys/proc.h>
58
59 #include <vm/vm.h>
60
61 #include <machine/control.h>
62 #include <machine/pte.h>
63 #include <machine/db_machdep.h>
64
65 #include <ddb/db_access.h>
66
67 #include "machdep.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 vm_offset_t addr;
83 register size_t size;
84 register char *data;
85 {
86 register 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 register char *dst;
111 register size_t size;
112 register char *data;
113 {
114 int oldpte, tmppte;
115 vm_offset_t pgva, prevpg;
116
117 /* Prevent restoring a garbage PTE. */
118 if (size <= 0)
119 return;
120
121 pgva = sun3_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 = sun3_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 tmppte = oldpte | PG_WRITE | PG_NC;
154 set_pte(pgva, tmppte);
155
156 prevpg = pgva;
157 }
158
159 /* Now we can write in this page of kernel text... */
160 *dst++ = *data++;
161
162 } while (--size > 0);
163
164 /* Restore old PTE for the last page touched. */
165 set_pte(prevpg, oldpte);
166
167 /* Finally, clear the instruction cache. */
168 ICIA();
169 }
170
171 /*
172 * Write bytes to kernel address space for debugger.
173 */
174 void
175 db_write_bytes(addr, size, data)
176 vm_offset_t addr;
177 register size_t size;
178 register char *data;
179 {
180 register char *dst = (char *)addr;
181
182 /* If any part is in kernel text, use db_write_text() */
183 if ((dst < etext) && ((dst + size) > kernel_text)) {
184 db_write_text(dst, size, data);
185 return;
186 }
187
188 if (size == 4) {
189 *((int*)dst) = *((int*)data);
190 return;
191 }
192
193 if (size == 2) {
194 *((short*)dst) = *((short*)data);
195 return;
196 }
197
198 while (size > 0) {
199 --size;
200 *dst++ = *data++;
201 }
202 }
203
204