xref: /freebsd/sys/ddb/db_access.c (revision 3e85b721d653d65a5b6ff655ed1551113f0e0d5b)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
30753960f7SDavid E. O'Brien 
31753960f7SDavid E. O'Brien #include <sys/cdefs.h>
32753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
33753960f7SDavid E. O'Brien 
34f540b106SGarrett Wollman #include <sys/param.h>
3537224cd3SMarcel Moolenaar #include <sys/kdb.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 
52f4e98881SRuslan Ermilov #ifndef BYTE_MSF
53f4e98881SRuslan Ermilov #define	BYTE_MSF	0
54f4e98881SRuslan Ermilov #endif
55f4e98881SRuslan Ermilov 
565b81b6b3SRodney W. Grimes db_expr_t
57cd508278SPedro F. Giffuni db_get_value(db_addr_t addr, int size, bool is_signed)
585b81b6b3SRodney W. Grimes {
590ddc915cSHartmut Brandt 	char		data[sizeof(u_int64_t)];
60*3e85b721SEd Maste 	db_expr_t	value;
61*3e85b721SEd Maste 	int		i;
625b81b6b3SRodney W. Grimes 
6337224cd3SMarcel Moolenaar 	if (db_read_bytes(addr, size, data) != 0) {
6437224cd3SMarcel Moolenaar 		db_printf("*** error reading from address %llx ***\n",
6537224cd3SMarcel Moolenaar 		    (long long)addr);
6637224cd3SMarcel Moolenaar 		kdb_reenter();
6737224cd3SMarcel Moolenaar 	}
685b81b6b3SRodney W. Grimes 
695b81b6b3SRodney W. Grimes 	value = 0;
705b81b6b3SRodney W. Grimes #if	BYTE_MSF
715b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
725b81b6b3SRodney W. Grimes #else	/* BYTE_LSF */
735b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
745b81b6b3SRodney W. Grimes #endif
755b81b6b3SRodney W. Grimes 	{
765b81b6b3SRodney W. Grimes 	    value = (value << 8) + (data[i] & 0xFF);
775b81b6b3SRodney W. Grimes 	}
785b81b6b3SRodney W. Grimes 
795b81b6b3SRodney W. Grimes 	if (size < 4) {
805b81b6b3SRodney W. Grimes 	    if (is_signed && (value & db_extend[size]) != 0)
815b81b6b3SRodney W. Grimes 		value |= db_extend[size];
825b81b6b3SRodney W. Grimes 	}
835b81b6b3SRodney W. Grimes 	return (value);
845b81b6b3SRodney W. Grimes }
855b81b6b3SRodney W. Grimes 
865b81b6b3SRodney W. Grimes void
87a41dd031SPedro F. Giffuni db_put_value(db_addr_t addr, int size, db_expr_t value)
885b81b6b3SRodney W. Grimes {
895b81b6b3SRodney W. Grimes 	char		data[sizeof(int)];
90*3e85b721SEd Maste 	int		i;
915b81b6b3SRodney W. Grimes 
925b81b6b3SRodney W. Grimes #if	BYTE_MSF
935b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
945b81b6b3SRodney W. Grimes #else	/* BYTE_LSF */
955b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
965b81b6b3SRodney W. Grimes #endif
975b81b6b3SRodney W. Grimes 	{
985b81b6b3SRodney W. Grimes 	    data[i] = value & 0xFF;
995b81b6b3SRodney W. Grimes 	    value >>= 8;
1005b81b6b3SRodney W. Grimes 	}
1015b81b6b3SRodney W. Grimes 
10237224cd3SMarcel Moolenaar 	if (db_write_bytes(addr, size, data) != 0) {
10337224cd3SMarcel Moolenaar 		db_printf("*** error writing to address %llx ***\n",
10437224cd3SMarcel Moolenaar 		    (long long)addr);
10537224cd3SMarcel Moolenaar 		kdb_reenter();
1065b81b6b3SRodney W. Grimes 	}
10737224cd3SMarcel Moolenaar }
108