xref: /openbsd/sys/ddb/db_access.c (revision 74db0e49)
1 /*	$OpenBSD: db_access.c,v 1.6 1997/07/06 23:09:23 niklas 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(addr, size, is_signed)
47 	db_addr_t addr;
48 	size_t size;
49 	boolean_t is_signed;
50 {
51 	char data[sizeof(db_expr_t)];
52 	db_expr_t value, extend;
53 	int i;
54 
55 	db_read_bytes(addr, size, data);
56 
57 	value = 0;
58 	extend = (~(db_expr_t)0) << (size * 8 - 1);
59 #if BYTE_ORDER == LITTLE_ENDIAN
60 	for (i = size - 1; i >= 0; i--)
61 #else /* BYTE_ORDER == BIG_ENDIAN */
62 	for (i = 0; i < size; i++)
63 #endif /* BYTE_ORDER */
64 		value = (value << 8) + (data[i] & 0xFF);
65 
66 	if (size < sizeof(db_expr_t) && is_signed && (value & extend))
67 		value |= extend;
68 	return (value);
69 }
70 
71 void
72 db_put_value(addr, size, value)
73 	db_addr_t addr;
74 	register size_t size;
75 	register db_expr_t value;
76 {
77 	char data[sizeof(db_expr_t)];
78 	register int i;
79 
80 #if BYTE_ORDER == LITTLE_ENDIAN
81 	for (i = 0; i < size; i++)
82 #else /* BYTE_ORDER == BIG_ENDIAN */
83 	for (i = size - 1; i >= 0; i--)
84 #endif /* BYTE_ORDER */
85 	{
86 		data[i] = value & 0xFF;
87 		value >>= 8;
88 	}
89 
90 	db_write_bytes(addr, size, data);
91 }
92