1 /* $OpenBSD: db_memrw.c,v 1.12 2007/02/20 21:15:01 tom Exp $ */
2 /* $NetBSD: db_memrw.c,v 1.6 1999/04/12 20:38:19 pk Exp $ */
3
4 /*
5 * Mach Operating System
6 * Copyright (c) 1991,1990 Carnegie Mellon University
7 * All Rights Reserved.
8 *
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
17 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie Mellon
27 * the rights to redistribute these changes.
28 *
29 * db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
30 */
31
32 /*
33 * Routines to read and write memory on behalf of the debugger, used
34 * by DDB and KGDB.
35 */
36
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <machine/db_machdep.h>
44
45 #include <ddb/db_access.h>
46
47 /*
48 * Read bytes from kernel address space for debugger.
49 */
50 void
51 db_read_bytes(vaddr_t addr, size_t size, char *data)
52 {
53 char *src;
54
55 src = (char *)addr;
56 while (size-- > 0)
57 *data++ = *src++;
58 }
59
60 /*
61 * Write bytes to kernel address space for debugger.
62 */
63 void
64 db_write_bytes(vaddr_t addr, size_t size, char *data)
65 {
66 char *dst;
67
68 pt_entry_t *ptep0 = 0;
69 pt_entry_t oldmap0 = { 0 };
70 vaddr_t addr1;
71 pt_entry_t *ptep1 = 0;
72 pt_entry_t oldmap1 = { 0 };
73 extern char etext;
74
75 if (addr >= VM_MIN_KERNEL_ADDRESS &&
76 addr < (vaddr_t)&etext) {
77 ptep0 = kvtopte(addr);
78 oldmap0 = *ptep0;
79 *(int *)ptep0 |= /* INTEL_PTE_WRITE */ PG_RW;
80
81 addr1 = trunc_page(addr + size - 1);
82 if (trunc_page(addr) != addr1) {
83 /* data crosses a page boundary */
84 ptep1 = kvtopte(addr1);
85 oldmap1 = *ptep1;
86 *(int *)ptep1 |= /* INTEL_PTE_WRITE */ PG_RW;
87 }
88 tlbflush();
89 }
90
91 dst = (char *)addr;
92
93 while (size-- > 0)
94 *dst++ = *data++;
95
96 if (ptep0) {
97 *ptep0 = oldmap0;
98 if (ptep1)
99 *ptep1 = oldmap1;
100 tlbflush();
101 }
102 }