| 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 | |
| 20 | #include "qemu/osdep.h" |
| 21 | #include "qemu/error-report.h" |
| 22 | #include "exec/cputlb.h" |
| 23 | #include "exec/target_page.h" |
| 24 | #include "exec/watchpoint.h" |
| 25 | #include "hw/core/cpu.h" |
| 26 | |
| 27 | /* Add a watchpoint. */ |
| 28 | int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, |
| 29 | int flags, CPUWatchpoint **watchpoint) |
| 30 | { |
| 31 | CPUWatchpoint *wp; |
| 32 | vaddr in_page; |
| 33 | |
| 34 | /* forbid ranges which are empty or run off the end of the address space */ |
| 35 | if (len == 0 || (addr + len - 1) < addr) { |
| 36 | error_report("tried to set invalid watchpoint at %" |
| 37 | VADDR_PRIx ", len=%" VADDR_PRIu, addr, len); |
| 38 | return -EINVAL; |
| 39 | } |
| 40 | wp = g_malloc(sizeof(*wp)); |
| 41 | |
| 42 | wp->vaddr = addr; |
| 43 | wp->len = len; |
| 44 | wp->flags = flags; |
| 45 | |
| 46 | /* keep all GDB-injected watchpoints in front */ |
| 47 | if (flags & BP_GDB) { |
| 48 | QTAILQ_INSERT_HEAD(&cpu->watchpoints, wp, entry); |
| 49 | } else { |
| 50 | QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry); |
| 51 | } |
| 52 | |
| 53 | in_page = -(addr | TARGET_PAGE_MASK); |
| 54 | if (len <= in_page) { |
| 55 | tlb_flush_page(cpu, addr); |
| 56 | } else { |
| 57 | tlb_flush(cpu); |
| 58 | } |
| 59 | |
| 60 | if (watchpoint) { |
| 61 | *watchpoint = wp; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* Remove a specific watchpoint. */ |
| 67 | int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len, |
| 68 | int flags) |
| 69 | { |
| 70 | CPUWatchpoint *wp; |
| 71 | |
| 72 | QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) { |
| 73 | if (addr == wp->vaddr && len == wp->len |
| 74 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
| 75 | cpu_watchpoint_remove_by_ref(cpu, wp); |
| 76 | return 0; |
| 77 | } |
| 78 | } |
| 79 | return -ENOENT; |
| 80 | } |
| 81 | |
| 82 | /* Remove a specific watchpoint by reference. */ |
| 83 | void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint) |
| 84 | { |
| 85 | QTAILQ_REMOVE(&cpu->watchpoints, watchpoint, entry); |
| 86 | |
| 87 | tlb_flush_page(cpu, watchpoint->vaddr); |
| 88 | |
| 89 | g_free(watchpoint); |
| 90 | } |
| 91 | |
| 92 | /* Remove all matching watchpoints. */ |
| 93 | void cpu_watchpoint_remove_all(CPUState *cpu, int mask) |
| 94 | { |
| 95 | CPUWatchpoint *wp, *next; |
| 96 | |
| 97 | QTAILQ_FOREACH_SAFE(wp, &cpu->watchpoints, entry, next) { |
| 98 | if (wp->flags & mask) { |
| 99 | cpu_watchpoint_remove_by_ref(cpu, wp); |
| 100 | } |
| 101 | } |
| 102 | } |