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

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