You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gdbmach.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <assert.h>
  21. #include <ipxe/uaccess.h>
  22. #include <ipxe/gdbstub.h>
  23. #include <gdbmach.h>
  24. /** @file
  25. *
  26. * GDB architecture-specific bits for i386
  27. *
  28. */
  29. enum {
  30. DR7_CLEAR = 0x00000400, /* disable hardware breakpoints */
  31. DR6_CLEAR = 0xffff0ff0, /* clear breakpoint status */
  32. };
  33. /** Hardware breakpoint, fields stored in x86 bit pattern form */
  34. struct hwbp {
  35. int type; /* type (1=write watchpoint, 3=access watchpoint) */
  36. unsigned long addr; /* linear address */
  37. size_t len; /* length (0=1-byte, 1=2-byte, 3=4-byte) */
  38. int enabled;
  39. };
  40. static struct hwbp hwbps [ 4 ];
  41. static gdbreg_t dr7 = DR7_CLEAR;
  42. static struct hwbp *gdbmach_find_hwbp ( int type, unsigned long addr, size_t len ) {
  43. struct hwbp *available = NULL;
  44. unsigned int i;
  45. for ( i = 0; i < sizeof hwbps / sizeof hwbps [ 0 ]; i++ ) {
  46. if ( hwbps [ i ].type == type && hwbps [ i ].addr == addr && hwbps [ i ].len == len ) {
  47. return &hwbps [ i ];
  48. }
  49. if ( !hwbps [ i ].enabled ) {
  50. available = &hwbps [ i ];
  51. }
  52. }
  53. return available;
  54. }
  55. static void gdbmach_commit_hwbp ( struct hwbp *bp ) {
  56. unsigned int regnum = bp - hwbps;
  57. /* Set breakpoint address */
  58. assert ( regnum < ( sizeof hwbps / sizeof hwbps [ 0 ] ) );
  59. switch ( regnum ) {
  60. case 0:
  61. __asm__ __volatile__ ( "movl %0, %%dr0\n" : : "r" ( bp->addr ) );
  62. break;
  63. case 1:
  64. __asm__ __volatile__ ( "movl %0, %%dr1\n" : : "r" ( bp->addr ) );
  65. break;
  66. case 2:
  67. __asm__ __volatile__ ( "movl %0, %%dr2\n" : : "r" ( bp->addr ) );
  68. break;
  69. case 3:
  70. __asm__ __volatile__ ( "movl %0, %%dr3\n" : : "r" ( bp->addr ) );
  71. break;
  72. }
  73. /* Set type */
  74. dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
  75. dr7 |= bp->type << ( 16 + 4 * regnum );
  76. /* Set length */
  77. dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
  78. dr7 |= bp->len << ( 18 + 4 * regnum );
  79. /* Set/clear local enable bit */
  80. dr7 &= ~( 0x3 << 2 * regnum );
  81. dr7 |= bp->enabled << 2 * regnum;
  82. }
  83. int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len, int enable ) {
  84. struct hwbp *bp;
  85. /* Check and convert breakpoint type to x86 type */
  86. switch ( type ) {
  87. case GDBMACH_WATCH:
  88. type = 0x1;
  89. break;
  90. case GDBMACH_AWATCH:
  91. type = 0x3;
  92. break;
  93. default:
  94. return 0; /* unsupported breakpoint type */
  95. }
  96. /* Only lengths 1, 2, and 4 are supported */
  97. if ( len != 2 && len != 4 ) {
  98. len = 1;
  99. }
  100. len--; /* convert to x86 breakpoint length bit pattern */
  101. /* Calculate linear address by adding segment base */
  102. addr += virt_offset;
  103. /* Set up the breakpoint */
  104. bp = gdbmach_find_hwbp ( type, addr, len );
  105. if ( !bp ) {
  106. return 0; /* ran out of hardware breakpoints */
  107. }
  108. bp->type = type;
  109. bp->addr = addr;
  110. bp->len = len;
  111. bp->enabled = enable;
  112. gdbmach_commit_hwbp ( bp );
  113. return 1;
  114. }
  115. static void gdbmach_disable_hwbps ( void ) {
  116. /* Store and clear hardware breakpoints */
  117. __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
  118. }
  119. static void gdbmach_enable_hwbps ( void ) {
  120. /* Clear breakpoint status register */
  121. __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
  122. /* Restore hardware breakpoints */
  123. __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( dr7 ) );
  124. }
  125. __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
  126. gdbmach_disable_hwbps();
  127. gdbstub_handler ( signo, regs );
  128. gdbmach_enable_hwbps();
  129. }