1 /* $OpenBSD: db_memrw.c,v 1.2 2024/02/23 18:19:03 cheloha Exp $ */
2 /* $NetBSD: db_memrw.c,v 1.8 2006/02/24 00:57:19 uwe 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
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 the
27 * 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.
35 */
36
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 #include <sys/stdint.h>
41
42 #include <uvm/uvm_extern.h>
43
44 #include <machine/db_machdep.h>
45
46 #include <ddb/db_access.h>
47
48 /*
49 * Read bytes from kernel address space for debugger.
50 */
51 void
db_read_bytes(vaddr_t addr,size_t size,void * datap)52 db_read_bytes(vaddr_t addr, size_t size, void *datap)
53 {
54 char *data = datap, *src = (char *)addr;
55
56 /* properly aligned 4-byte */
57 if (size == 4 && ((addr & 3) == 0) && (((uintptr_t)data & 3) == 0)) {
58 *(uint32_t *)data = *(uint32_t *)src;
59 return;
60 }
61
62 /* properly aligned 2-byte */
63 if (size == 2 && ((addr & 1) == 0) && (((uintptr_t)data & 1) == 0)) {
64 *(uint16_t *)data = *(uint16_t *)src;
65 return;
66 }
67
68 while (size-- > 0)
69 *data++ = *src++;
70 }
71
72 /*
73 * Write bytes to kernel address space for debugger.
74 */
75 void
db_write_bytes(vaddr_t addr,size_t size,void * datap)76 db_write_bytes(vaddr_t addr, size_t size, void *datap)
77 {
78 char *data = datap, *dst = (char *)addr;
79
80 /* properly aligned 4-byte */
81 if (size == 4 && ((addr & 3) == 0) && (((uintptr_t)data & 3) == 0)) {
82 *(uint32_t *)dst = *(const uint32_t *)data;
83 return;
84 }
85
86 /* properly aligned 2-byte */
87 if (size == 2 && ((addr & 1) == 0) && (((uintptr_t)data & 1) == 0)) {
88 *(uint16_t *)dst = *(const uint16_t *)data;
89 return;
90 }
91
92 while (size-- > 0)
93 *dst++ = *data++;
94 }
95