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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <gdbmach.h>
  26. /** @file
  27. *
  28. * GDB architecture-specific bits for i386
  29. *
  30. */
  31. enum {
  32. DR7_CLEAR = 0x00000400, /* disable hardware breakpoints */
  33. DR6_CLEAR = 0xffff0ff0, /* clear breakpoint status */
  34. };
  35. /** Hardware breakpoint, fields stored in x86 bit pattern form */
  36. struct hwbp {
  37. int type; /* type (1=write watchpoint, 3=access watchpoint) */
  38. unsigned long addr; /* linear address */
  39. size_t len; /* length (0=1-byte, 1=2-byte, 3=4-byte) */
  40. int enabled;
  41. };
  42. static struct hwbp hwbps [ 4 ];
  43. static gdbreg_t dr7 = DR7_CLEAR;
  44. static struct hwbp *gdbmach_find_hwbp ( int type, unsigned long addr, size_t len ) {
  45. struct hwbp *available = NULL;
  46. unsigned int i;
  47. for ( i = 0; i < sizeof hwbps / sizeof hwbps [ 0 ]; i++ ) {
  48. if ( hwbps [ i ].type == type && hwbps [ i ].addr == addr && hwbps [ i ].len == len ) {
  49. return &hwbps [ i ];
  50. }
  51. if ( !hwbps [ i ].enabled ) {
  52. available = &hwbps [ i ];
  53. }
  54. }
  55. return available;
  56. }
  57. static void gdbmach_commit_hwbp ( struct hwbp *bp ) {
  58. unsigned int regnum = bp - hwbps;
  59. /* Set breakpoint address */
  60. assert ( regnum < ( sizeof hwbps / sizeof hwbps [ 0 ] ) );
  61. switch ( regnum ) {
  62. case 0:
  63. __asm__ __volatile__ ( "movl %0, %%dr0\n" : : "r" ( bp->addr ) );
  64. break;
  65. case 1:
  66. __asm__ __volatile__ ( "movl %0, %%dr1\n" : : "r" ( bp->addr ) );
  67. break;
  68. case 2:
  69. __asm__ __volatile__ ( "movl %0, %%dr2\n" : : "r" ( bp->addr ) );
  70. break;
  71. case 3:
  72. __asm__ __volatile__ ( "movl %0, %%dr3\n" : : "r" ( bp->addr ) );
  73. break;
  74. }
  75. /* Set type */
  76. dr7 &= ~( 0x3 << ( 16 + 4 * regnum ) );
  77. dr7 |= bp->type << ( 16 + 4 * regnum );
  78. /* Set length */
  79. dr7 &= ~( 0x3 << ( 18 + 4 * regnum ) );
  80. dr7 |= bp->len << ( 18 + 4 * regnum );
  81. /* Set/clear local enable bit */
  82. dr7 &= ~( 0x3 << 2 * regnum );
  83. dr7 |= bp->enabled << 2 * regnum;
  84. }
  85. int gdbmach_set_breakpoint ( int type, unsigned long addr, size_t len, int enable ) {
  86. struct hwbp *bp;
  87. /* Check and convert breakpoint type to x86 type */
  88. switch ( type ) {
  89. case GDBMACH_WATCH:
  90. type = 0x1;
  91. break;
  92. case GDBMACH_AWATCH:
  93. type = 0x3;
  94. break;
  95. default:
  96. return 0; /* unsupported breakpoint type */
  97. }
  98. /* Only lengths 1, 2, and 4 are supported */
  99. if ( len != 2 && len != 4 ) {
  100. len = 1;
  101. }
  102. len--; /* convert to x86 breakpoint length bit pattern */
  103. /* Calculate linear address by adding segment base */
  104. addr += virt_offset;
  105. /* Set up the breakpoint */
  106. bp = gdbmach_find_hwbp ( type, addr, len );
  107. if ( !bp ) {
  108. return 0; /* ran out of hardware breakpoints */
  109. }
  110. bp->type = type;
  111. bp->addr = addr;
  112. bp->len = len;
  113. bp->enabled = enable;
  114. gdbmach_commit_hwbp ( bp );
  115. return 1;
  116. }
  117. static void gdbmach_disable_hwbps ( void ) {
  118. /* Store and clear hardware breakpoints */
  119. __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( DR7_CLEAR ) );
  120. }
  121. static void gdbmach_enable_hwbps ( void ) {
  122. /* Clear breakpoint status register */
  123. __asm__ __volatile__ ( "movl %0, %%dr6\n" : : "r" ( DR6_CLEAR ) );
  124. /* Restore hardware breakpoints */
  125. __asm__ __volatile__ ( "movl %0, %%dr7\n" : : "r" ( dr7 ) );
  126. }
  127. __asmcall void gdbmach_handler ( int signo, gdbreg_t *regs ) {
  128. gdbmach_disable_hwbps();
  129. gdbstub_handler ( signo, regs );
  130. gdbmach_enable_hwbps();
  131. }