xref: /openbsd-src/sys/ddb/db_access.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: db_access.c,v 1.11 2014/07/08 13:02:57 deraadt 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 <machine/db_machdep.h>		/* type definitions */
37 #include <machine/endian.h>
38 
39 #include <ddb/db_access.h>
40 
41 /*
42  * Access unaligned data items on aligned (longword)
43  * boundaries.
44  */
45 db_expr_t
46 db_get_value(db_addr_t addr, size_t size, boolean_t is_signed)
47 {
48 	char data[sizeof(db_expr_t)];
49 	db_expr_t value, extend;
50 	int i;
51 
52 #ifdef DIAGNOSTIC
53 	if (size > sizeof data)
54 		size = sizeof data;
55 #endif
56 
57 	db_read_bytes(addr, size, data);
58 
59 	value = 0;
60 	extend = (~(db_expr_t)0) << (size * 8 - 1);
61 #if BYTE_ORDER == LITTLE_ENDIAN
62 	for (i = size - 1; i >= 0; i--)
63 #else /* BYTE_ORDER == BIG_ENDIAN */
64 	for (i = 0; i < size; i++)
65 #endif /* BYTE_ORDER */
66 		value = (value << 8) + (data[i] & 0xFF);
67 
68 	if (size < sizeof(db_expr_t) && is_signed && (value & extend))
69 		value |= extend;
70 	return (value);
71 }
72 
73 void
74 db_put_value(db_addr_t addr, size_t size, db_expr_t value)
75 {
76 	char data[sizeof(db_expr_t)];
77 	int i;
78 
79 #ifdef DIAGNOSTIC
80 	if (size > sizeof data)
81 		size = sizeof data;
82 #endif
83 
84 #if BYTE_ORDER == LITTLE_ENDIAN
85 	for (i = 0; i < size; i++)
86 #else /* BYTE_ORDER == BIG_ENDIAN */
87 	for (i = size - 1; i >= 0; i--)
88 #endif /* BYTE_ORDER */
89 	{
90 		data[i] = value & 0xff;
91 		value >>= 8;
92 	}
93 
94 	db_write_bytes(addr, size, data);
95 }
96