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