xref: /openbsd-src/sys/ddb/db_access.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: db_access.c,v 1.10 2007/03/15 17:10:22 miod Exp $	*/
2 /*	$NetBSD: db_access.c,v 1.8 1994/10/09 08:37:35 mycroft 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  * the rights to redistribute these changes.
28  *
29  *	Author: David B. Golub, Carnegie Mellon University
30  *	Date:	7/90
31  */
32 
33 #include <sys/param.h>
34 #include <sys/proc.h>
35 
36 #include <uvm/uvm_extern.h>
37 
38 #include <machine/db_machdep.h>		/* type definitions */
39 #include <machine/endian.h>
40 
41 #include <ddb/db_access.h>
42 
43 /*
44  * Access unaligned data items on aligned (longword)
45  * boundaries.
46  */
47 db_expr_t
48 db_get_value(db_addr_t addr, size_t size, boolean_t is_signed)
49 {
50 	char data[sizeof(db_expr_t)];
51 	db_expr_t value, extend;
52 	int i;
53 
54 #ifdef DIAGNOSTIC
55 	if (size > sizeof data)
56 		size = sizeof data;
57 #endif
58 
59 	db_read_bytes(addr, size, data);
60 
61 	value = 0;
62 	extend = (~(db_expr_t)0) << (size * 8 - 1);
63 #if BYTE_ORDER == LITTLE_ENDIAN
64 	for (i = size - 1; i >= 0; i--)
65 #else /* BYTE_ORDER == BIG_ENDIAN */
66 	for (i = 0; i < size; i++)
67 #endif /* BYTE_ORDER */
68 		value = (value << 8) + (data[i] & 0xFF);
69 
70 	if (size < sizeof(db_expr_t) && is_signed && (value & extend))
71 		value |= extend;
72 	return (value);
73 }
74 
75 void
76 db_put_value(db_addr_t addr, size_t size, db_expr_t value)
77 {
78 	char data[sizeof(db_expr_t)];
79 	int i;
80 
81 #ifdef DIAGNOSTIC
82 	if (size > sizeof data)
83 		size = sizeof data;
84 #endif
85 
86 #if BYTE_ORDER == LITTLE_ENDIAN
87 	for (i = 0; i < size; i++)
88 #else /* BYTE_ORDER == BIG_ENDIAN */
89 	for (i = size - 1; i >= 0; i--)
90 #endif /* BYTE_ORDER */
91 	{
92 		data[i] = value & 0xff;
93 		value >>= 8;
94 	}
95 
96 	db_write_bytes(addr, size, data);
97 }
98