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