您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

gdbmach.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #include <ipxe/uaccess.h>
  24. #include <ipxe/gdbstub.h>
  25. #include <librm.h>
  26. #include <gdbmach.h>
  27. /** @file
  28. *
  29. * GDB architecture-specific bits for i386
  30. *
  31. */
  32. enum {
  33. DR7_CLEAR = 0x00000400, /* disable hardware breakpoints */
  34. DR6_CLEAR = 0xffff0ff0, /* clear breakpoint status */
  35. };
  36. /** Hardware breakpoint, fields stored in x86 bit pattern form */
  37. struct hwbp {
  38. int type; /* type (1=write watchpoint, 3=access watchpoint) */
  39. unsigned long addr; /* linear address */
  40. size_t len; /* length (0=1-byte, 1=2-byte, 3=4-byte) */
  41. int enabled;
  42. };
  43. static struct hwbp hwbps [ 4 ];
  44. static gdbreg_t dr7 = DR7_CLEAR;
  45. static struct hwbp *gdbmach_find_hwbp ( int type, unsigned long addr, size_t len ) {
  46. struct hwbp *available = NULL;
  47. unsigned int i;
  48. for ( i = 0; i < sizeof hwbps / sizeof hwbps [ 0 ]; i++ ) {
  49. if ( hwbps [ i ].type == type && hwbps [ i ].addr == addr && hwbps [ i ].len == len ) {
  50. return &hwbps [ i ];
  51. }
  52. if ( !hwbps [ i ].enabled ) {
  53. available = &hwbps [ i ];
  54. }
  55. }
  56. return available;
  57. }
  58. static void gdbmach_commit_hwbp ( struct hwbp *bp ) {
  59. unsigned int regnum = bp - hwbps;
  60. /* Set breakpoint address */
  61. assert ( regnum < ( sizeof hwbps / sizeof hwbps [ 0 ] ) );
  62. switch ( regnum ) {
  63. case 0:
  64. __asm__ __volatile__ ( "movl %0, %%dr0\n" : : "r" ( bp->addr ) );
  65. break;
  66. case 1:
  67. __asm__ __volatile__ ( "movl %0, %%dr1\n" : : "r" ( bp->addr ) );
  68. break;
  69. case 2:
  70. __asm__ __volatile__ ( "movl %0, %%dr2\n" : : "r" ( bp->addr ) );
  71. break;
  72. case 3:
  73. __asm__ __volatile__ ( "movl %0, %%dr3\n" : : "r" ( bp->addr ) );
  74. break;
  75. }
  76. /* Set type */
  77. dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
  78. dr7 |= bp->type << ( 16 + 4 * regnum );
  79. /* Set length */
  80. dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
  81. dr7 |= bp->len << ( 18 + 4 * regnum );
  82. /* Set/clear local enable bit */
  83. dr7 &= ~( 0x3 << 2 * regnum );
  84. dr7 |= bp->enabled << 2 * regnum;
  85. }
  86. int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len, int enable ) {
  87. struct hwbp *bp;
  88. /* Check and convert breakpoint type to x86 type */
  89. switch ( type ) {
  90. case GDBMACH_WATCH:
  91. type = 0x1;
  92. break;
  93. case GDBMACH_AWATCH:
  94. type = 0x3;
  95. break;
  96. default:
  97. return 0; /* unsupported breakpoint type */
  98. }
  99. /* Only lengths 1, 2, and 4 are supported */
  100. if ( len != 2 && len != 4 ) {
  101. len = 1;
  102. }
  103. len--; /* convert to x86 breakpoint length bit pattern */
  104. /* Calculate linear address by adding segment base */
  105. addr += virt_offset;
  106. /* Set up the breakpoint */
  107. bp = gdbmach_find_hwbp ( type, addr, len );
  108. if ( !bp ) {
  109. return 0; /* ran out of hardware breakpoints */
  110. }
  111. bp->type = type;
  112. bp->addr = addr;
  113. bp->len = len;
  114. bp->enabled = enable;
  115. gdbmach_commit_hwbp ( bp );
  116. return 1;
  117. }
  118. static void gdbmach_disable_hwbps ( void ) {
  119. /* Store and clear hardware breakpoints */
  120. __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
  121. }
  122. static void gdbmach_enable_hwbps ( void ) {
  123. /* Clear breakpoint status register */
  124. __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
  125. /* Restore hardware breakpoints */
  126. __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( dr7 ) );
  127. }
  128. __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
  129. gdbmach_disable_hwbps();
  130. gdbstub_handler ( signo, regs );
  131. gdbmach_enable_hwbps();
  132. }
  133. static void * gdbmach_interrupt_vectors[] = {
  134. gdbmach_nocode_sigfpe, /* Divide by zero */
  135. gdbmach_nocode_sigtrap, /* Debug trap */
  136. NULL, /* Non-maskable interrupt */
  137. gdbmach_nocode_sigtrap, /* Breakpoint */
  138. gdbmach_nocode_sigstkflt, /* Overflow */
  139. gdbmach_nocode_sigstkflt, /* Bound range exceeded */
  140. gdbmach_nocode_sigill, /* Invalid opcode */
  141. NULL, /* Device not available */
  142. gdbmach_withcode_sigbus, /* Double fault */
  143. NULL, /* Coprocessor segment overrun */
  144. gdbmach_withcode_sigsegv, /* Invalid TSS */
  145. gdbmach_withcode_sigsegv, /* Segment not present */
  146. gdbmach_withcode_sigsegv, /* Stack segment fault */
  147. gdbmach_withcode_sigsegv, /* General protection fault */
  148. gdbmach_withcode_sigsegv, /* Page fault */
  149. };
  150. void gdbmach_init ( void ) {
  151. unsigned int i;
  152. for ( i = 0 ; i < ( sizeof ( gdbmach_interrupt_vectors ) /
  153. sizeof ( gdbmach_interrupt_vectors[0] ) ) ; i++ ) {
  154. set_interrupt_vector ( i, gdbmach_interrupt_vectors[i] );
  155. }
  156. }