watchpoint.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * CPU watchpoints
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/main-loop.h"
  21. #include "qemu/error-report.h"
  22. #include "exec/exec-all.h"
  23. #include "exec/translate-all.h"
  24. #include "system/tcg.h"
  25. #include "system/replay.h"
  26. #include "hw/core/tcg-cpu-ops.h"
  27. #include "hw/core/cpu.h"
  28. /*
  29. * Return true if this watchpoint address matches the specified
  30. * access (ie the address range covered by the watchpoint overlaps
  31. * partially or completely with the address range covered by the
  32. * access).
  33. */
  34. static inline bool watchpoint_address_matches(CPUWatchpoint *wp,
  35. vaddr addr, vaddr len)
  36. {
  37. /*
  38. * We know the lengths are non-zero, but a little caution is
  39. * required to avoid errors in the case where the range ends
  40. * exactly at the top of the address space and so addr + len
  41. * wraps round to zero.
  42. */
  43. vaddr wpend = wp->vaddr + wp->len - 1;
  44. vaddr addrend = addr + len - 1;
  45. return !(addr > wpend || wp->vaddr > addrend);
  46. }
  47. /* Return flags for watchpoints that match addr + prot. */
  48. int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len)
  49. {
  50. CPUWatchpoint *wp;
  51. int ret = 0;
  52. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  53. if (watchpoint_address_matches(wp, addr, len)) {
  54. ret |= wp->flags;
  55. }
  56. }
  57. return ret;
  58. }
  59. /* Generate a debug exception if a watchpoint has been hit. */
  60. void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len,
  61. MemTxAttrs attrs, int flags, uintptr_t ra)
  62. {
  63. CPUClass *cc = CPU_GET_CLASS(cpu);
  64. CPUWatchpoint *wp;
  65. assert(tcg_enabled());
  66. if (cpu->watchpoint_hit) {
  67. /*
  68. * We re-entered the check after replacing the TB.
  69. * Now raise the debug interrupt so that it will
  70. * trigger after the current instruction.
  71. */
  72. bql_lock();
  73. cpu_interrupt(cpu, CPU_INTERRUPT_DEBUG);
  74. bql_unlock();
  75. return;
  76. }
  77. if (cc->tcg_ops->adjust_watchpoint_address) {
  78. /* this is currently used only by ARM BE32 */
  79. addr = cc->tcg_ops->adjust_watchpoint_address(cpu, addr, len);
  80. }
  81. assert((flags & ~BP_MEM_ACCESS) == 0);
  82. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  83. int hit_flags = wp->flags & flags;
  84. if (hit_flags && watchpoint_address_matches(wp, addr, len)) {
  85. if (replay_running_debug()) {
  86. /*
  87. * replay_breakpoint reads icount.
  88. * Force recompile to succeed, because icount may
  89. * be read only at the end of the block.
  90. */
  91. if (!cpu->neg.can_do_io) {
  92. /* Force execution of one insn next time. */
  93. cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(cpu);
  94. cpu_loop_exit_restore(cpu, ra);
  95. }
  96. /*
  97. * Don't process the watchpoints when we are
  98. * in a reverse debugging operation.
  99. */
  100. replay_breakpoint();
  101. return;
  102. }
  103. wp->flags |= hit_flags << BP_HIT_SHIFT;
  104. wp->hitaddr = MAX(addr, wp->vaddr);
  105. wp->hitattrs = attrs;
  106. if (wp->flags & BP_CPU
  107. && cc->tcg_ops->debug_check_watchpoint
  108. && !cc->tcg_ops->debug_check_watchpoint(cpu, wp)) {
  109. wp->flags &= ~BP_WATCHPOINT_HIT;
  110. continue;
  111. }
  112. cpu->watchpoint_hit = wp;
  113. mmap_lock();
  114. /* This call also restores vCPU state */
  115. tb_check_watchpoint(cpu, ra);
  116. if (wp->flags & BP_STOP_BEFORE_ACCESS) {
  117. cpu->exception_index = EXCP_DEBUG;
  118. mmap_unlock();
  119. cpu_loop_exit(cpu);
  120. } else {
  121. /* Force execution of one insn next time. */
  122. cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(cpu);
  123. mmap_unlock();
  124. cpu_loop_exit_noexc(cpu);
  125. }
  126. } else {
  127. wp->flags &= ~BP_WATCHPOINT_HIT;
  128. }
  129. }
  130. }