1/*	$NetBSD: db_memrw.c,v 1.4 2001/05/18 20:38:27 matt Exp $	*/
2/*	$OpenBSD: db_memrw.c,v 1.7 2024/02/23 18:19:03 cheloha Exp $	*/
3
4/*
5 * Mach Operating System
6 * Copyright (c) 1992 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
30/*
31 * Interface to the debugger for virtual memory read/write.
32 * This is a simple version for kernels with writable text.
33 * For an example of read-only kernel text, see the file:
34 * sys/arch/sun3/sun3/db_memrw.c
35 *
36 * ALERT!  If you want to access device registers with a
37 * specific size, then the read/write functions have to
38 * make sure to do the correct sized pointer access.
39 */
40
41#include <sys/param.h>
42#include <sys/proc.h>
43#include <sys/systm.h>
44
45#include <uvm/uvm_extern.h>
46
47#include <machine/db_machdep.h>
48#include <machine/pcb.h>
49
50#include <ddb/db_access.h>
51
52/*
53 * Read bytes from kernel address space for debugger.
54 */
55void
56db_read_bytes(vaddr_t addr, size_t size, void *datap)
57{
58	char *data = datap, *src = (char *)addr;
59	faultbuf env;
60	faultbuf *old_onfault = curpcb->pcb_onfault;
61	if (setfault(&env)) {
62		curpcb->pcb_onfault = old_onfault;
63		return;
64	}
65
66	if (size == 4) {
67		*((int *)data) = *((int *)src);
68	} else if (size == 2) {
69		*((short *)data) = *((short *)src);
70	} else {
71		while (size > 0) {
72			--size;
73			*data++ = *src++;
74		}
75	}
76	curpcb->pcb_onfault = old_onfault;
77}
78
79/*
80 * Write bytes to kernel address space for debugger.
81 */
82void
83db_write_bytes(vaddr_t addr, size_t size, void *datap)
84{
85	char *data = datap, *dst = (char *)addr;
86	faultbuf env;
87	faultbuf *old_onfault = curpcb->pcb_onfault;
88
89	if (setfault(&env)) {
90		curpcb->pcb_onfault = old_onfault;
91		return;
92	}
93
94	if (size == 4) {
95		*((int *)dst) = *((int *)data);
96	} else if (size == 2) {
97		*((short *)dst) = *((short *)data);
98	} else  {
99		while (size > 0) {
100			--size;
101			*dst++ = *data++;
102		}
103	}
104	syncicache((void *)addr, size);
105	curpcb->pcb_onfault = old_onfault;
106}
107
108