db_memrw.c revision 1.24 1 /* $NetBSD: db_memrw.c,v 1.24 2005/01/22 15:36:10 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/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.24 2005/01/22 15:36:10 chs 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(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(db_addr_t addr, size_t size, 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(char *dst, size_t size, char *data)
110 {
111 int oldpte, tmppte;
112 vaddr_t pgva, prevpg;
113
114 /* Prevent restoring a garbage PTE. */
115 if (size <= 0)
116 return;
117
118 pgva = m68k_trunc_page((long)dst);
119
120 goto firstpage;
121 do {
122
123 /*
124 * If we are on a new page, restore the PTE
125 * for the previous page, and make the new
126 * page writable.
127 */
128 pgva = m68k_trunc_page((long)dst);
129 if (pgva != prevpg) {
130 /*
131 * Restore old PTE. No cache flush,
132 * because the tmp PTE has no-cache.
133 */
134 set_pte(prevpg, oldpte);
135
136 firstpage:
137 /*
138 * Flush the VAC to prevent a cache hit
139 * on the old, read-only PTE.
140 */
141 #ifdef HAVECACHE
142 if (cache_size)
143 cache_flush_page(pgva);
144 #endif
145 oldpte = get_pte(pgva);
146 if ((oldpte & PG_VALID) == 0) {
147 printf(" address %p not a valid page\n", dst);
148 return;
149 }
150
151 /*
152 * Make the pte writable and non-cached.
153 */
154 tmppte = oldpte;
155 #ifdef _SUN3_
156 tmppte |= (PG_WRITE | PG_NC);
157 #endif
158 #ifdef _SUN3X_
159 tmppte &= ~MMU_SHORT_PTE_WP;
160 tmppte |= MMU_SHORT_PTE_CI;
161 #endif
162
163 set_pte(pgva, tmppte);
164 prevpg = pgva;
165 }
166
167 /* Now we can write in this page of kernel text... */
168 *dst++ = *data++;
169
170 } while (--size > 0);
171
172 /* Restore old PTE for the last page touched. */
173 set_pte(prevpg, oldpte);
174
175 /* Finally, clear the instruction cache. */
176 ICIA();
177 }
178
179 /*
180 * Write bytes to kernel address space for debugger.
181 */
182 void
183 db_write_bytes(db_addr_t addr, size_t size, char *data)
184 {
185 char *dst = (char *)addr;
186
187 /* If any part is in kernel text, use db_write_text() */
188 if ((dst < etext) && ((dst + size) > kernel_text)) {
189 db_write_text(dst, size, data);
190 return;
191 }
192
193 if (size == 4) {
194 *((int *)dst) = *((int *)data);
195 return;
196 }
197
198 if (size == 2) {
199 *((short *)dst) = *((short *)data);
200 return;
201 }
202
203 while (size > 0) {
204 --size;
205 *dst++ = *data++;
206 }
207 }
208
209