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.

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