xref: /freebsd/sys/ddb/db_access.c (revision 5ccbc3cc5aa70fee35bc061ab0e4fadcf67ad335)
15b81b6b3SRodney W. Grimes /*
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.
250edf66ecSRodney W. Grimes  *
265ccbc3ccSBruce Evans  *	$Id: db_access.c,v 1.10 1995/12/07 12:44:44 davidg Exp $
275b81b6b3SRodney W. Grimes  */
280edf66ecSRodney W. Grimes 
295b81b6b3SRodney W. Grimes /*
305b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
315b81b6b3SRodney W. Grimes  *	Date:	7/90
325b81b6b3SRodney W. Grimes  */
33f540b106SGarrett Wollman #include <sys/param.h>
34f540b106SGarrett Wollman #include <sys/systm.h>
355ccbc3ccSBruce Evans 
36f540b106SGarrett Wollman #include <ddb/ddb.h>
37058284fcSBruce Evans #include <ddb/db_access.h>
385b81b6b3SRodney W. Grimes 
395b81b6b3SRodney W. Grimes /*
405b81b6b3SRodney W. Grimes  * Access unaligned data items on aligned (longword)
415b81b6b3SRodney W. Grimes  * boundaries.
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
44f73a856dSPoul-Henning Kamp static unsigned db_extend[] = {	/* table for sign-extending */
455b81b6b3SRodney W. Grimes 	0,
46381fe1aaSGarrett Wollman 	0xFFFFFF80U,
47381fe1aaSGarrett Wollman 	0xFFFF8000U,
48381fe1aaSGarrett Wollman 	0xFF800000U
495b81b6b3SRodney W. Grimes };
505b81b6b3SRodney W. Grimes 
515b81b6b3SRodney W. Grimes db_expr_t
525b81b6b3SRodney W. Grimes db_get_value(addr, size, is_signed)
535b81b6b3SRodney W. Grimes 	db_addr_t	addr;
545b81b6b3SRodney W. Grimes 	register int	size;
555b81b6b3SRodney W. Grimes 	boolean_t	is_signed;
565b81b6b3SRodney W. Grimes {
575b81b6b3SRodney W. Grimes 	char		data[sizeof(int)];
585b81b6b3SRodney W. Grimes 	register db_expr_t value;
595b81b6b3SRodney W. Grimes 	register int	i;
605b81b6b3SRodney W. Grimes 
615b81b6b3SRodney W. Grimes 	db_read_bytes(addr, size, data);
625b81b6b3SRodney W. Grimes 
635b81b6b3SRodney W. Grimes 	value = 0;
645b81b6b3SRodney W. Grimes #if	BYTE_MSF
655b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
665b81b6b3SRodney W. Grimes #else	/* BYTE_LSF */
675b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
685b81b6b3SRodney W. Grimes #endif
695b81b6b3SRodney W. Grimes 	{
705b81b6b3SRodney W. Grimes 	    value = (value << 8) + (data[i] & 0xFF);
715b81b6b3SRodney W. Grimes 	}
725b81b6b3SRodney W. Grimes 
735b81b6b3SRodney W. Grimes 	if (size < 4) {
745b81b6b3SRodney W. Grimes 	    if (is_signed && (value & db_extend[size]) != 0)
755b81b6b3SRodney W. Grimes 		value |= db_extend[size];
765b81b6b3SRodney W. Grimes 	}
775b81b6b3SRodney W. Grimes 	return (value);
785b81b6b3SRodney W. Grimes }
795b81b6b3SRodney W. Grimes 
805b81b6b3SRodney W. Grimes void
815b81b6b3SRodney W. Grimes db_put_value(addr, size, value)
825b81b6b3SRodney W. Grimes 	db_addr_t	addr;
835b81b6b3SRodney W. Grimes 	register int	size;
845b81b6b3SRodney W. Grimes 	register db_expr_t value;
855b81b6b3SRodney W. Grimes {
865b81b6b3SRodney W. Grimes 	char		data[sizeof(int)];
875b81b6b3SRodney W. Grimes 	register int	i;
885b81b6b3SRodney W. Grimes 
895b81b6b3SRodney W. Grimes #if	BYTE_MSF
905b81b6b3SRodney W. Grimes 	for (i = size - 1; i >= 0; i--)
915b81b6b3SRodney W. Grimes #else	/* BYTE_LSF */
925b81b6b3SRodney W. Grimes 	for (i = 0; i < size; i++)
935b81b6b3SRodney W. Grimes #endif
945b81b6b3SRodney W. Grimes 	{
955b81b6b3SRodney W. Grimes 	    data[i] = value & 0xFF;
965b81b6b3SRodney W. Grimes 	    value >>= 8;
975b81b6b3SRodney W. Grimes 	}
985b81b6b3SRodney W. Grimes 
995b81b6b3SRodney W. Grimes 	db_write_bytes(addr, size, data);
1005b81b6b3SRodney W. Grimes }
1015b81b6b3SRodney W. Grimes 
102