xref: /freebsd/sys/ddb/db_access.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * SPDX-License-Identifier: MIT-CMU
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
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 /*
29  *	Author: David B. Golub, Carnegie Mellon University
30  *	Date:	7/90
31  */
32 
33 #include <sys/param.h>
34 #include <sys/kdb.h>
35 #include <sys/endian.h>
36 
37 #include <ddb/ddb.h>
38 #include <ddb/db_access.h>
39 
40 /*
41  * Access unaligned data items on aligned (longword)
42  * boundaries.
43  */
44 
45 static unsigned db_extend[] = {	/* table for sign-extending */
46 	0,
47 	0xFFFFFF80U,
48 	0xFFFF8000U,
49 	0xFF800000U
50 };
51 
52 db_expr_t
53 db_get_value(db_addr_t addr, int size, bool is_signed)
54 {
55 	char		data[sizeof(uint64_t)];
56 	db_expr_t	value;
57 	int		i;
58 
59 	if (db_read_bytes(addr, size, data) != 0) {
60 		db_printf("*** error reading from address %llx ***\n",
61 		    (long long)addr);
62 		kdb_reenter();
63 	}
64 
65 	value = 0;
66 #if _BYTE_ORDER == _BIG_ENDIAN
67 	for (i = 0; i < size; i++)
68 #else	/* _LITTLE_ENDIAN */
69 	for (i = size - 1; i >= 0; i--)
70 #endif
71 	{
72 	    value = (value << 8) + (data[i] & 0xFF);
73 	}
74 
75 	if (size < 4) {
76 	    if (is_signed && (value & db_extend[size]) != 0)
77 		value |= db_extend[size];
78 	}
79 	return (value);
80 }
81 
82 void
83 db_put_value(db_addr_t addr, int size, db_expr_t value)
84 {
85 	char		data[sizeof(int)];
86 	int		i;
87 
88 #if _BYTE_ORDER == _BIG_ENDIAN
89 	for (i = size - 1; i >= 0; i--)
90 #else	/* _LITTLE_ENDIAN */
91 	for (i = 0; i < size; i++)
92 #endif
93 	{
94 	    data[i] = value & 0xFF;
95 	    value >>= 8;
96 	}
97 
98 	if (db_write_bytes(addr, size, data) != 0) {
99 		db_printf("*** error writing to address %llx ***\n",
100 		    (long long)addr);
101 		kdb_reenter();
102 	}
103 }
104