xref: /freebsd/sys/ddb/db_access.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1dd3cb568SWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: MIT-CMU
3796df753SPedro F. Giffuni  *
45b81b6b3SRodney W. Grimes  * Mach Operating System
55b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
65b81b6b3SRodney W. Grimes  * All Rights Reserved.
75b81b6b3SRodney W. Grimes  *
85b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
95b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
105b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
115b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
125b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
135b81b6b3SRodney W. Grimes  *
145b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
155b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
165b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
195b81b6b3SRodney W. Grimes  *
205b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
215b81b6b3SRodney W. Grimes  *  School of Computer Science
225b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
235b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
245b81b6b3SRodney W. Grimes  *
255b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
265b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
275b81b6b3SRodney W. Grimes  */
285b81b6b3SRodney W. Grimes /*
295b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
305b81b6b3SRodney W. Grimes  *	Date:	7/90
315b81b6b3SRodney W. Grimes  */
32753960f7SDavid E. O'Brien 
33f540b106SGarrett Wollman #include <sys/param.h>
3437224cd3SMarcel Moolenaar #include <sys/kdb.h>
359d89a9f3SWarner Losh #include <sys/endian.h>
365ccbc3ccSBruce Evans 
37f540b106SGarrett Wollman #include <ddb/ddb.h>
38058284fcSBruce Evans #include <ddb/db_access.h>
395b81b6b3SRodney W. Grimes 
405b81b6b3SRodney W. Grimes /*
415b81b6b3SRodney W. Grimes  * Access unaligned data items on aligned (longword)
425b81b6b3SRodney W. Grimes  * boundaries.
435b81b6b3SRodney W. Grimes  */
445b81b6b3SRodney W. Grimes 
45f73a856dSPoul-Henning Kamp static unsigned db_extend[] = {	/* table for sign-extending */
465b81b6b3SRodney W. Grimes 	0,
47381fe1aaSGarrett Wollman 	0xFFFFFF80U,
48381fe1aaSGarrett Wollman 	0xFFFF8000U,
49381fe1aaSGarrett Wollman 	0xFF800000U
505b81b6b3SRodney W. Grimes };
515b81b6b3SRodney W. Grimes 
525b81b6b3SRodney W. Grimes db_expr_t
db_get_value(db_addr_t addr,int size,bool is_signed)53cd508278SPedro F. Giffuni db_get_value(db_addr_t addr, int size, bool is_signed)
545b81b6b3SRodney W. Grimes {
55*db269074SJohn Baldwin 	char		data[sizeof(uint64_t)];
563e85b721SEd Maste 	db_expr_t	value;
573e85b721SEd Maste 	int		i;
585b81b6b3SRodney W. Grimes 
5937224cd3SMarcel Moolenaar 	if (db_read_bytes(addr, size, data) != 0) {
6037224cd3SMarcel Moolenaar 		db_printf("*** error reading from address %llx ***\n",
6137224cd3SMarcel Moolenaar 		    (long long)addr);
6237224cd3SMarcel Moolenaar 		kdb_reenter();
6337224cd3SMarcel Moolenaar 	}
645b81b6b3SRodney W. Grimes 
655b81b6b3SRodney W. Grimes 	value = 0;
669d89a9f3SWarner Losh #if _BYTE_ORDER == _BIG_ENDIAN
675b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
689d89a9f3SWarner Losh #else	/* _LITTLE_ENDIAN */
695b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
705b81b6b3SRodney W. Grimes #endif
715b81b6b3SRodney W. Grimes 	{
725b81b6b3SRodney W. Grimes 	    value = (value << 8) + (data[i] & 0xFF);
735b81b6b3SRodney W. Grimes 	}
745b81b6b3SRodney W. Grimes 
755b81b6b3SRodney W. Grimes 	if (size < 4) {
765b81b6b3SRodney W. Grimes 	    if (is_signed && (value & db_extend[size]) != 0)
775b81b6b3SRodney W. Grimes 		value |= db_extend[size];
785b81b6b3SRodney W. Grimes 	}
795b81b6b3SRodney W. Grimes 	return (value);
805b81b6b3SRodney W. Grimes }
815b81b6b3SRodney W. Grimes 
825b81b6b3SRodney W. Grimes void
db_put_value(db_addr_t addr,int size,db_expr_t value)83a41dd031SPedro F. Giffuni db_put_value(db_addr_t addr, int size, db_expr_t value)
845b81b6b3SRodney W. Grimes {
855b81b6b3SRodney W. Grimes 	char		data[sizeof(int)];
863e85b721SEd Maste 	int		i;
875b81b6b3SRodney W. Grimes 
889d89a9f3SWarner Losh #if _BYTE_ORDER == _BIG_ENDIAN
895b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
909d89a9f3SWarner Losh #else	/* _LITTLE_ENDIAN */
915b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
925b81b6b3SRodney W. Grimes #endif
935b81b6b3SRodney W. Grimes 	{
945b81b6b3SRodney W. Grimes 	    data[i] = value & 0xFF;
955b81b6b3SRodney W. Grimes 	    value >>= 8;
965b81b6b3SRodney W. Grimes 	}
975b81b6b3SRodney W. Grimes 
9837224cd3SMarcel Moolenaar 	if (db_write_bytes(addr, size, data) != 0) {
9937224cd3SMarcel Moolenaar 		db_printf("*** error writing to address %llx ***\n",
10037224cd3SMarcel Moolenaar 		    (long long)addr);
10137224cd3SMarcel Moolenaar 		kdb_reenter();
1025b81b6b3SRodney W. Grimes 	}
10337224cd3SMarcel Moolenaar }
104