db_memrw.c revision 1.4 1 1.4 gwr /* $NetBSD: db_memrw.c,v 1.4 1994/11/28 19:33:08 gwr Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1994 Gordon W. Ross
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.1 gwr * 3. The name of the author may not be used to endorse or promote products
16 1.1 gwr * derived from this software without specific prior written permission.
17 1.1 gwr *
18 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 gwr */
29 1.1 gwr
30 1.1 gwr /*
31 1.4 gwr * Interface to the debugger for virtual memory read/write.
32 1.1 gwr * To write in the text segment, we have to first make
33 1.1 gwr * the page writable, do the write, then restore the PTE.
34 1.4 gwr * For reads, validate address first to avoid MMU trap.
35 1.1 gwr */
36 1.1 gwr
37 1.1 gwr #include <sys/param.h>
38 1.1 gwr #include <sys/proc.h>
39 1.1 gwr
40 1.1 gwr #include <vm/vm.h>
41 1.1 gwr
42 1.1 gwr #include <machine/db_machdep.h>
43 1.1 gwr #include <machine/pte.h>
44 1.1 gwr
45 1.1 gwr /*
46 1.3 gwr * Read one byte somewhere in the kernel.
47 1.3 gwr * It does not matter if this is slow. -gwr
48 1.3 gwr */
49 1.3 gwr static char
50 1.3 gwr db_read_data(src)
51 1.3 gwr char *src;
52 1.3 gwr {
53 1.3 gwr int oldpte;
54 1.3 gwr vm_offset_t pgva;
55 1.3 gwr int ch;
56 1.3 gwr
57 1.3 gwr pgva = sun3_trunc_page((long)src);
58 1.3 gwr oldpte = get_pte(pgva);
59 1.3 gwr
60 1.3 gwr if ((oldpte & PG_VALID) == 0) {
61 1.3 gwr db_printf(" address 0x%x not a valid page\n", src);
62 1.3 gwr return 0;
63 1.3 gwr }
64 1.3 gwr return (*src);
65 1.3 gwr }
66 1.3 gwr
67 1.3 gwr /*
68 1.3 gwr * Read bytes from kernel address space for debugger.
69 1.3 gwr * It does not matter if this is slow. -gwr
70 1.3 gwr */
71 1.3 gwr void
72 1.3 gwr db_read_bytes(addr, size, data)
73 1.3 gwr vm_offset_t addr;
74 1.3 gwr register int size;
75 1.3 gwr register char *data;
76 1.3 gwr {
77 1.3 gwr char *src, *limit;
78 1.3 gwr
79 1.3 gwr src = (char *)addr;
80 1.3 gwr limit = src + size;
81 1.3 gwr
82 1.3 gwr while (src < limit) {
83 1.3 gwr *data = db_read_data(src);
84 1.3 gwr data++;
85 1.3 gwr src++;
86 1.3 gwr }
87 1.3 gwr }
88 1.3 gwr
89 1.3 gwr /*
90 1.1 gwr * Write one byte somewhere in kernel text.
91 1.1 gwr * It does not matter if this is slow. -gwr
92 1.1 gwr */
93 1.1 gwr static void
94 1.1 gwr db_write_text(dst, ch)
95 1.1 gwr char *dst;
96 1.1 gwr int ch;
97 1.1 gwr {
98 1.1 gwr int oldpte, tmppte;
99 1.1 gwr vm_offset_t pgva;
100 1.1 gwr
101 1.1 gwr pgva = sun3_trunc_page((long)dst);
102 1.1 gwr oldpte = get_pte(pgva);
103 1.1 gwr
104 1.1 gwr if ((oldpte & PG_VALID) == 0) {
105 1.1 gwr db_printf(" address 0x%x not a valid page\n", dst);
106 1.1 gwr return;
107 1.1 gwr }
108 1.1 gwr
109 1.1 gwr tmppte = oldpte | PG_WRITE;
110 1.1 gwr set_pte(pgva, tmppte);
111 1.1 gwr
112 1.1 gwr *dst = (char) ch;
113 1.1 gwr
114 1.1 gwr set_pte(pgva, oldpte);
115 1.1 gwr }
116 1.1 gwr
117 1.1 gwr /*
118 1.3 gwr * Write one byte somewhere outside kernel text.
119 1.3 gwr * It does not matter if this is slow. -gwr
120 1.3 gwr */
121 1.3 gwr static void
122 1.3 gwr db_write_data(dst, ch)
123 1.3 gwr char *dst;
124 1.3 gwr int ch;
125 1.3 gwr {
126 1.3 gwr int oldpte;
127 1.3 gwr vm_offset_t pgva;
128 1.3 gwr
129 1.3 gwr pgva = sun3_trunc_page((long)dst);
130 1.3 gwr oldpte = get_pte(pgva);
131 1.3 gwr
132 1.3 gwr if ((oldpte & (PG_VALID | PG_WRITE)) == 0) {
133 1.3 gwr db_printf(" address 0x%x not a valid page\n", dst);
134 1.3 gwr return;
135 1.3 gwr }
136 1.3 gwr *dst = (char) ch;
137 1.3 gwr }
138 1.3 gwr
139 1.3 gwr /*
140 1.1 gwr * Write bytes to kernel address space for debugger.
141 1.1 gwr */
142 1.1 gwr void
143 1.1 gwr db_write_bytes(addr, size, data)
144 1.1 gwr vm_offset_t addr;
145 1.1 gwr int size;
146 1.1 gwr char *data;
147 1.1 gwr {
148 1.3 gwr extern char start[], etext[] ;
149 1.1 gwr char *dst, *limit;
150 1.1 gwr
151 1.1 gwr dst = (char *)addr;
152 1.1 gwr limit = dst + size;
153 1.1 gwr
154 1.1 gwr while (dst < limit) {
155 1.1 gwr if ((dst >= start) && (dst < etext))
156 1.1 gwr db_write_text(dst, *data);
157 1.1 gwr else
158 1.3 gwr db_write_data(dst, *data);
159 1.1 gwr dst++;
160 1.1 gwr data++;
161 1.1 gwr }
162 1.1 gwr }
163