xref: /netbsd-src/sys/ddb/db_access.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: db_access.c,v 1.16 2001/11/12 22:54:03 lukem Exp $	*/
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  *
28  *	Author: David B. Golub, Carnegie Mellon University
29  *	Date:	7/90
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: db_access.c,v 1.16 2001/11/12 22:54:03 lukem Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/proc.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 
48 const int db_extend[] = {	/* table for sign-extending */
49 	0,
50 	0xFFFFFF80,
51 	0xFFFF8000,
52 	0xFF800000
53 };
54 
55 db_expr_t
56 db_get_value(addr, size, is_signed)
57 	db_addr_t addr;
58 	size_t size;
59 	boolean_t is_signed;
60 {
61 	char data[sizeof(db_expr_t)];
62 	db_expr_t value;
63 	size_t i;
64 
65 	db_read_bytes(addr, size, data);
66 
67 	value = 0;
68 #if BYTE_ORDER == LITTLE_ENDIAN
69 	for (i = size; i-- > 0;)
70 #else /* BYTE_ORDER == BIG_ENDIAN */
71 	for (i = 0; i < size; i++)
72 #endif /* BYTE_ORDER */
73 		value = (value << 8) + (data[i] & 0xFF);
74 
75 	if (size < 4 && is_signed && (value & db_extend[size]) != 0)
76 		value |= db_extend[size];
77 	return (value);
78 }
79 
80 void
81 db_put_value(addr, size, value)
82 	db_addr_t addr;
83 	size_t size;
84 	db_expr_t value;
85 {
86 	char data[sizeof(db_expr_t)];
87 	size_t i;
88 
89 #if BYTE_ORDER == LITTLE_ENDIAN
90 	for (i = 0; i < size; i++)
91 #else /* BYTE_ORDER == BIG_ENDIAN */
92 	for (i = size; i-- > 0;)
93 #endif /* BYTE_ORDER */
94 	{
95 		data[i] = value & 0xFF;
96 		value >>= 8;
97 	}
98 
99 	db_write_bytes(addr, size, data);
100 }
101