xref: /openbsd-src/sys/arch/powerpc/ddb/db_memrw.c (revision fe1fe620f8418f588086cadfe1b03bb5d224b548)
184c41355Smiod /*	$NetBSD: db_memrw.c,v 1.4 2001/05/18 20:38:27 matt Exp $	*/
2*fe1fe620Scheloha /*	$OpenBSD: db_memrw.c,v 1.7 2024/02/23 18:19:03 cheloha Exp $	*/
384c41355Smiod 
484c41355Smiod /*
584c41355Smiod  * Mach Operating System
684c41355Smiod  * Copyright (c) 1992 Carnegie Mellon University
784c41355Smiod  * All Rights Reserved.
884c41355Smiod  *
984c41355Smiod  * Permission to use, copy, modify and distribute this software and its
1084c41355Smiod  * documentation is hereby granted, provided that both the copyright
1184c41355Smiod  * notice and this permission notice appear in all copies of the
1284c41355Smiod  * software, derivative works or modified versions, and any portions
1384c41355Smiod  * thereof, and that both notices appear in supporting documentation.
1484c41355Smiod  *
1584c41355Smiod  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1684c41355Smiod  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1784c41355Smiod  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1884c41355Smiod  *
1984c41355Smiod  * Carnegie Mellon requests users of this software to return to
2084c41355Smiod  *
2184c41355Smiod  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
2284c41355Smiod  *  School of Computer Science
2384c41355Smiod  *  Carnegie Mellon University
2484c41355Smiod  *  Pittsburgh PA 15213-3890
2584c41355Smiod  *
2684c41355Smiod  * any improvements or extensions that they make and grant Carnegie Mellon
2784c41355Smiod  * the rights to redistribute these changes.
2884c41355Smiod  */
2984c41355Smiod 
3084c41355Smiod /*
3184c41355Smiod  * Interface to the debugger for virtual memory read/write.
3284c41355Smiod  * This is a simple version for kernels with writable text.
3384c41355Smiod  * For an example of read-only kernel text, see the file:
3484c41355Smiod  * sys/arch/sun3/sun3/db_memrw.c
3584c41355Smiod  *
3684c41355Smiod  * ALERT!  If you want to access device registers with a
3784c41355Smiod  * specific size, then the read/write functions have to
3884c41355Smiod  * make sure to do the correct sized pointer access.
3984c41355Smiod  */
4084c41355Smiod 
4184c41355Smiod #include <sys/param.h>
4284c41355Smiod #include <sys/proc.h>
4384c41355Smiod #include <sys/systm.h>
4484c41355Smiod 
4584c41355Smiod #include <uvm/uvm_extern.h>
4684c41355Smiod 
4784c41355Smiod #include <machine/db_machdep.h>
4884c41355Smiod #include <machine/pcb.h>
4984c41355Smiod 
5084c41355Smiod #include <ddb/db_access.h>
5184c41355Smiod 
5284c41355Smiod /*
5384c41355Smiod  * Read bytes from kernel address space for debugger.
5484c41355Smiod  */
5584c41355Smiod void
db_read_bytes(vaddr_t addr,size_t size,void * datap)56*fe1fe620Scheloha db_read_bytes(vaddr_t addr, size_t size, void *datap)
5784c41355Smiod {
58*fe1fe620Scheloha 	char *data = datap, *src = (char *)addr;
5984c41355Smiod 	faultbuf env;
6084c41355Smiod 	faultbuf *old_onfault = curpcb->pcb_onfault;
614140fb10Sdrahn 	if (setfault(&env)) {
6284c41355Smiod 		curpcb->pcb_onfault = old_onfault;
6384c41355Smiod 		return;
6484c41355Smiod 	}
6584c41355Smiod 
6684c41355Smiod 	if (size == 4) {
6784c41355Smiod 		*((int *)data) = *((int *)src);
6884c41355Smiod 	} else if (size == 2) {
6984c41355Smiod 		*((short *)data) = *((short *)src);
7084c41355Smiod 	} else {
7184c41355Smiod 		while (size > 0) {
7284c41355Smiod 			--size;
7384c41355Smiod 			*data++ = *src++;
7484c41355Smiod 		}
7584c41355Smiod 	}
7684c41355Smiod 	curpcb->pcb_onfault = old_onfault;
7784c41355Smiod }
7884c41355Smiod 
7984c41355Smiod /*
8084c41355Smiod  * Write bytes to kernel address space for debugger.
8184c41355Smiod  */
8284c41355Smiod void
db_write_bytes(vaddr_t addr,size_t size,void * datap)83*fe1fe620Scheloha db_write_bytes(vaddr_t addr, size_t size, void *datap)
8484c41355Smiod {
85*fe1fe620Scheloha 	char *data = datap, *dst = (char *)addr;
8684c41355Smiod 	faultbuf env;
8784c41355Smiod 	faultbuf *old_onfault = curpcb->pcb_onfault;
8884c41355Smiod 
894140fb10Sdrahn 	if (setfault(&env)) {
9084c41355Smiod 		curpcb->pcb_onfault = old_onfault;
9184c41355Smiod 		return;
9284c41355Smiod 	}
9384c41355Smiod 
9484c41355Smiod 	if (size == 4) {
9584c41355Smiod 		*((int *)dst) = *((int *)data);
9684c41355Smiod 	} else if (size == 2) {
9784c41355Smiod 		*((short *)dst) = *((short *)data);
9884c41355Smiod 	} else  {
9984c41355Smiod 		while (size > 0) {
10084c41355Smiod 			--size;
10184c41355Smiod 			*dst++ = *data++;
10284c41355Smiod 		}
10384c41355Smiod 	}
10484c41355Smiod 	syncicache((void *)addr, size);
10584c41355Smiod 	curpcb->pcb_onfault = old_onfault;
10684c41355Smiod }
10784c41355Smiod 
108