db_access.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* $OpenBSD: db_access.c,v 1.13 2014/12/19 22:44:58 guenther Exp $ */
  2. /* $NetBSD: db_access.c,v 1.8 1994/10/09 08:37:35 mycroft Exp $ */
  3. /*
  4. * Mach Operating System
  5. * Copyright (c) 1991,1990 Carnegie Mellon University
  6. * All Rights Reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
  15. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  16. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  17. *
  18. * Carnegie Mellon requests users of this software to return to
  19. *
  20. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  21. * School of Computer Science
  22. * Carnegie Mellon University
  23. * Pittsburgh PA 15213-3890
  24. *
  25. * any improvements or extensions that they make and grant Carnegie the
  26. * the rights to redistribute these changes.
  27. *
  28. * Author: David B. Golub, Carnegie Mellon University
  29. * Date: 7/90
  30. */
  31. #include <sys/param.h>
  32. #include <sys/endian.h>
  33. #include <machine/db_machdep.h> /* type definitions */
  34. #include <ddb/db_access.h>
  35. /*
  36. * Access unaligned data items on aligned (longword)
  37. * boundaries.
  38. */
  39. db_expr_t
  40. db_get_value(db_addr_t addr, size_t size, boolean_t is_signed)
  41. {
  42. char data[sizeof(db_expr_t)];
  43. db_expr_t value, extend;
  44. int i;
  45. #ifdef DIAGNOSTIC
  46. if (size > sizeof data)
  47. size = sizeof data;
  48. #endif
  49. db_read_bytes(addr, size, data);
  50. value = 0;
  51. extend = (~(db_expr_t)0) << (size * 8 - 1);
  52. #if BYTE_ORDER == LITTLE_ENDIAN
  53. for (i = size - 1; i >= 0; i--)
  54. #else /* BYTE_ORDER == BIG_ENDIAN */
  55. for (i = 0; i < size; i++)
  56. #endif /* BYTE_ORDER */
  57. value = (value << 8) + (data[i] & 0xFF);
  58. if (size < sizeof(db_expr_t) && is_signed && (value & extend))
  59. value |= extend;
  60. return (value);
  61. }
  62. void
  63. db_put_value(db_addr_t addr, size_t size, db_expr_t value)
  64. {
  65. char data[sizeof(db_expr_t)];
  66. int i;
  67. #ifdef DIAGNOSTIC
  68. if (size > sizeof data)
  69. size = sizeof data;
  70. #endif
  71. #if BYTE_ORDER == LITTLE_ENDIAN
  72. for (i = 0; i < size; i++)
  73. #else /* BYTE_ORDER == BIG_ENDIAN */
  74. for (i = size - 1; i >= 0; i--)
  75. #endif /* BYTE_ORDER */
  76. {
  77. data[i] = value & 0xff;
  78. value >>= 8;
  79. }
  80. db_write_bytes(addr, size, data);
  81. }