xref: /freebsd/sys/ddb/db_access.c (revision db269074ff820c4ee1afcc20b4788da88ffe22ed)
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 
33753960f7SDavid E. O'Brien #include <sys/cdefs.h>
34753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
35753960f7SDavid E. O'Brien 
36f540b106SGarrett Wollman #include <sys/param.h>
3737224cd3SMarcel Moolenaar #include <sys/kdb.h>
389d89a9f3SWarner Losh #include <sys/endian.h>
395ccbc3ccSBruce Evans 
40f540b106SGarrett Wollman #include <ddb/ddb.h>
41058284fcSBruce Evans #include <ddb/db_access.h>
425b81b6b3SRodney W. Grimes 
435b81b6b3SRodney W. Grimes /*
445b81b6b3SRodney W. Grimes  * Access unaligned data items on aligned (longword)
455b81b6b3SRodney W. Grimes  * boundaries.
465b81b6b3SRodney W. Grimes  */
475b81b6b3SRodney W. Grimes 
48f73a856dSPoul-Henning Kamp static unsigned db_extend[] = {	/* table for sign-extending */
495b81b6b3SRodney W. Grimes 	0,
50381fe1aaSGarrett Wollman 	0xFFFFFF80U,
51381fe1aaSGarrett Wollman 	0xFFFF8000U,
52381fe1aaSGarrett Wollman 	0xFF800000U
535b81b6b3SRodney W. Grimes };
545b81b6b3SRodney W. Grimes 
555b81b6b3SRodney W. Grimes db_expr_t
56cd508278SPedro F. Giffuni db_get_value(db_addr_t addr, int size, bool is_signed)
575b81b6b3SRodney W. Grimes {
58*db269074SJohn Baldwin 	char		data[sizeof(uint64_t)];
593e85b721SEd Maste 	db_expr_t	value;
603e85b721SEd Maste 	int		i;
615b81b6b3SRodney W. Grimes 
6237224cd3SMarcel Moolenaar 	if (db_read_bytes(addr, size, data) != 0) {
6337224cd3SMarcel Moolenaar 		db_printf("*** error reading from address %llx ***\n",
6437224cd3SMarcel Moolenaar 		    (long long)addr);
6537224cd3SMarcel Moolenaar 		kdb_reenter();
6637224cd3SMarcel Moolenaar 	}
675b81b6b3SRodney W. Grimes 
685b81b6b3SRodney W. Grimes 	value = 0;
699d89a9f3SWarner Losh #if _BYTE_ORDER == _BIG_ENDIAN
705b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
719d89a9f3SWarner Losh #else	/* _LITTLE_ENDIAN */
725b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
735b81b6b3SRodney W. Grimes #endif
745b81b6b3SRodney W. Grimes 	{
755b81b6b3SRodney W. Grimes 	    value = (value << 8) + (data[i] & 0xFF);
765b81b6b3SRodney W. Grimes 	}
775b81b6b3SRodney W. Grimes 
785b81b6b3SRodney W. Grimes 	if (size < 4) {
795b81b6b3SRodney W. Grimes 	    if (is_signed && (value & db_extend[size]) != 0)
805b81b6b3SRodney W. Grimes 		value |= db_extend[size];
815b81b6b3SRodney W. Grimes 	}
825b81b6b3SRodney W. Grimes 	return (value);
835b81b6b3SRodney W. Grimes }
845b81b6b3SRodney W. Grimes 
855b81b6b3SRodney W. Grimes void
86a41dd031SPedro F. Giffuni db_put_value(db_addr_t addr, int size, db_expr_t value)
875b81b6b3SRodney W. Grimes {
885b81b6b3SRodney W. Grimes 	char		data[sizeof(int)];
893e85b721SEd Maste 	int		i;
905b81b6b3SRodney W. Grimes 
919d89a9f3SWarner Losh #if _BYTE_ORDER == _BIG_ENDIAN
925b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
939d89a9f3SWarner Losh #else	/* _LITTLE_ENDIAN */
945b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
955b81b6b3SRodney W. Grimes #endif
965b81b6b3SRodney W. Grimes 	{
975b81b6b3SRodney W. Grimes 	    data[i] = value & 0xFF;
985b81b6b3SRodney W. Grimes 	    value >>= 8;
995b81b6b3SRodney W. Grimes 	}
1005b81b6b3SRodney W. Grimes 
10137224cd3SMarcel Moolenaar 	if (db_write_bytes(addr, size, data) != 0) {
10237224cd3SMarcel Moolenaar 		db_printf("*** error writing to address %llx ***\n",
10337224cd3SMarcel Moolenaar 		    (long long)addr);
10437224cd3SMarcel Moolenaar 		kdb_reenter();
1055b81b6b3SRodney W. Grimes 	}
10637224cd3SMarcel Moolenaar }
107