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