1139747Simp/*-
24Srgrimes * Mach Operating System
34Srgrimes * Copyright (c) 1991,1990 Carnegie Mellon University
44Srgrimes * All Rights Reserved.
58876Srgrimes *
64Srgrimes * Permission to use, copy, modify and distribute this software and its
74Srgrimes * documentation is hereby granted, provided that both the copyright
84Srgrimes * notice and this permission notice appear in all copies of the
94Srgrimes * software, derivative works or modified versions, and any portions
104Srgrimes * thereof, and that both notices appear in supporting documentation.
118876Srgrimes *
128876Srgrimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
134Srgrimes * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
144Srgrimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
158876Srgrimes *
164Srgrimes * Carnegie Mellon requests users of this software to return to
178876Srgrimes *
184Srgrimes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
194Srgrimes *  School of Computer Science
204Srgrimes *  Carnegie Mellon University
214Srgrimes *  Pittsburgh PA 15213-3890
228876Srgrimes *
234Srgrimes * any improvements or extensions that they make and grant Carnegie the
244Srgrimes * rights to redistribute these changes.
254Srgrimes */
264Srgrimes/*
274Srgrimes *	Author: David B. Golub, Carnegie Mellon University
284Srgrimes *	Date:	7/90
294Srgrimes */
30116176Sobrien
31116176Sobrien#include <sys/cdefs.h>
32116176Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/ddb/db_access.c 283248 2015-05-21 15:16:18Z pfg $");
33116176Sobrien
342056Swollman#include <sys/param.h>
35131952Smarcel#include <sys/kdb.h>
3612734Sbde
372056Swollman#include <ddb/ddb.h>
3812473Sbde#include <ddb/db_access.h>
394Srgrimes
404Srgrimes/*
414Srgrimes * Access unaligned data items on aligned (longword)
424Srgrimes * boundaries.
434Srgrimes */
444Srgrimes
4512515Sphkstatic unsigned db_extend[] = {	/* table for sign-extending */
464Srgrimes	0,
47798Swollman	0xFFFFFF80U,
48798Swollman	0xFFFF8000U,
49798Swollman	0xFF800000U
504Srgrimes};
514Srgrimes
52153072Sru#ifndef BYTE_MSF
53153072Sru#define	BYTE_MSF	0
54153072Sru#endif
55153072Sru
564Srgrimesdb_expr_t
57283248Spfgdb_get_value(db_addr_t addr, int size, bool is_signed)
584Srgrimes{
59118822Sharti	char		data[sizeof(u_int64_t)];
604Srgrimes	register db_expr_t value;
614Srgrimes	register int	i;
624Srgrimes
63131952Smarcel	if (db_read_bytes(addr, size, data) != 0) {
64131952Smarcel		db_printf("*** error reading from address %llx ***\n",
65131952Smarcel		    (long long)addr);
66131952Smarcel		kdb_reenter();
67131952Smarcel	}
684Srgrimes
694Srgrimes	value = 0;
704Srgrimes#if	BYTE_MSF
714Srgrimes	for (i = 0; i < size; i++)
724Srgrimes#else	/* BYTE_LSF */
734Srgrimes	for (i = size - 1; i >= 0; i--)
744Srgrimes#endif
754Srgrimes	{
764Srgrimes	    value = (value << 8) + (data[i] & 0xFF);
774Srgrimes	}
788876Srgrimes
794Srgrimes	if (size < 4) {
804Srgrimes	    if (is_signed && (value & db_extend[size]) != 0)
814Srgrimes		value |= db_extend[size];
824Srgrimes	}
834Srgrimes	return (value);
844Srgrimes}
854Srgrimes
864Srgrimesvoid
87273006Spfgdb_put_value(db_addr_t addr, int size, db_expr_t value)
884Srgrimes{
894Srgrimes	char		data[sizeof(int)];
904Srgrimes	register int	i;
914Srgrimes
924Srgrimes#if	BYTE_MSF
934Srgrimes	for (i = size - 1; i >= 0; i--)
944Srgrimes#else	/* BYTE_LSF */
954Srgrimes	for (i = 0; i < size; i++)
964Srgrimes#endif
974Srgrimes	{
984Srgrimes	    data[i] = value & 0xFF;
994Srgrimes	    value >>= 8;
1004Srgrimes	}
1014Srgrimes
102131952Smarcel	if (db_write_bytes(addr, size, data) != 0) {
103131952Smarcel		db_printf("*** error writing to address %llx ***\n",
104131952Smarcel		    (long long)addr);
105131952Smarcel		kdb_reenter();
106131952Smarcel	}
1074Srgrimes}
108