1/*	$NetBSD: db_memrw.c,v 1.26 2005/12/11 12:19:27 christos 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Interface to the debugger for virtual memory read/write.
34 * This file is shared by DDB and KGDB, and must work even
35 * when only KGDB is included (thus no db_printf calls).
36 *
37 * To write in the text segment, we have to first make
38 * the page writable, do the write, then restore the PTE.
39 * For writes outside the text segment, and all reads,
40 * just do the access -- if it causes a fault, the debugger
41 * will recover with a longjmp to an appropriate place.
42 *
43 * ALERT!  If you want to access device registers with a
44 * specific size, then the read/write functions have to
45 * make sure to do the correct sized pointer access.
46 */
47
48#include <sys/cdefs.h>
49__KERNEL_RCSID(0, "$NetBSD: db_memrw.c,v 1.26 2005/12/11 12:19:27 christos Exp $");
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/proc.h>
54
55#include <uvm/uvm_extern.h>
56
57#include <machine/db_machdep.h>
58#include <machine/pte.h>
59#include <m68k/cacheops.h>
60
61#include <sun3/sun3/machdep.h>
62
63#include <ddb/db_access.h>
64
65extern char etext[];	/* defined by the linker */
66extern char	kernel_text[];	/* locore.s */
67
68static void db_write_text(char *, size_t size, const char *);
69
70
71/*
72 * Read bytes from kernel address space for debugger.
73 * This used to check for valid PTEs, but now that
74 * traps in DDB work correctly, "Just Do It!"
75 */
76void
77db_read_bytes(db_addr_t addr, size_t size, char *data)
78{
79	 char *src = (char *)addr;
80
81	if (size == 4) {
82		*((int *)data) = *((int *)src);
83		return;
84	}
85
86	if (size == 2) {
87		*((short *)data) = *((short *)src);
88		return;
89	}
90
91	while (size > 0) {
92		--size;
93		*data++ = *src++;
94	}
95}
96
97/*
98 * Write bytes somewhere in kernel text.
99 * Makes text page writable temporarily.
100 */
101static void
102db_write_text(char *dst, size_t size, const char *data)
103{
104	int oldpte, tmppte;
105	vaddr_t pgva, prevpg;
106
107	/* Prevent restoring a garbage PTE. */
108	if (size <= 0)
109		return;
110
111	pgva = m68k_trunc_page((long)dst);
112
113	goto firstpage;
114	do {
115
116		/*
117		 * If we are on a new page, restore the PTE
118		 * for the previous page, and make the new
119		 * page writable.
120		 */
121		pgva = m68k_trunc_page((long)dst);
122		if (pgva != prevpg) {
123			/*
124			 * Restore old PTE.  No cache flush,
125			 * because the tmp PTE has no-cache.
126			 */
127			set_pte(prevpg, oldpte);
128
129		firstpage:
130			/*
131			 * Flush the VAC to prevent a cache hit
132			 * on the old, read-only PTE.
133			 */
134#ifdef	HAVECACHE
135			if (cache_size)
136				cache_flush_page(pgva);
137#endif
138			oldpte = get_pte(pgva);
139			if ((oldpte & PG_VALID) == 0) {
140				printf(" address %p not a valid page\n", dst);
141				return;
142			}
143
144			/*
145			 * Make the pte writable and non-cached.
146			 */
147			tmppte = oldpte;
148#ifdef	_SUN3_
149			tmppte |= (PG_WRITE | PG_NC);
150#endif
151#ifdef	_SUN3X_
152			tmppte &= ~MMU_SHORT_PTE_WP;
153			tmppte |= MMU_SHORT_PTE_CI;
154#endif
155
156			set_pte(pgva, tmppte);
157			prevpg = pgva;
158		}
159
160		/* Now we can write in this page of kernel text... */
161		*dst++ = *data++;
162
163	} while (--size > 0);
164
165	/* Restore old PTE for the last page touched. */
166	set_pte(prevpg, oldpte);
167
168	/* Finally, clear the instruction cache. */
169	ICIA();
170}
171
172/*
173 * Write bytes to kernel address space for debugger.
174 */
175void
176db_write_bytes(db_addr_t addr, size_t size, const char *data)
177{
178	char *dst = (char *)addr;
179
180	/* If any part is in kernel text, use db_write_text() */
181	if ((dst < etext) && ((dst + size) > kernel_text)) {
182		db_write_text(dst, size, data);
183		return;
184	}
185
186	if (size == 4) {
187		*((int *)dst) = *((const int *)data);
188		return;
189	}
190
191	if (size == 2) {
192		*((short *)dst) = *((const short *)data);
193		return;
194	}
195
196	while (size > 0) {
197		--size;
198		*dst++ = *data++;
199	}
200}
201
202