blob: ef5757d23bf6874d935a6c275abdc1a03aabfa15 [file] [log] [blame]
Paolo Bonzini267f6852016-08-28 03:45:14 +02001/*
2 * CPU thread main loop - common bits for user and system mode emulation
3 *
4 * Copyright (c) 2003-2005 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
Chetan Pantd6ea4232020-10-23 12:33:53 +00009 * version 2.1 of the License, or (at your option) any later version.
Paolo Bonzini267f6852016-08-28 03:45:14 +020010 *
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"
Paolo Bonzini53f5ed92016-08-28 05:38:24 +020021#include "qemu/main-loop.h"
Paolo Bonzini267f6852016-08-28 03:45:14 +020022#include "exec/cpu-common.h"
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020023#include "hw/core/cpu.h"
Daniel Brodsky6e8a3552020-04-03 21:21:08 -070024#include "qemu/lockable.h"
Philippe Mathieu-Daudé8a8dc262022-11-24 16:36:49 +010025#include "trace/trace-root.h"
Paolo Bonzini267f6852016-08-28 03:45:14 +020026
Jamie Iles370ed602023-04-27 03:09:24 +010027QemuMutex qemu_cpu_list_lock;
Paolo Bonziniab129972016-08-31 16:56:04 +020028static QemuCond exclusive_cond;
29static QemuCond exclusive_resume;
Sergey Fedorovd148d902016-08-29 09:51:00 +020030static QemuCond qemu_work_cond;
Paolo Bonzini267f6852016-08-28 03:45:14 +020031
Paolo Bonzinic265e972016-08-31 21:33:58 +020032/* >= 1 if a thread is inside start_exclusive/end_exclusive. Written
33 * under qemu_cpu_list_lock, read with atomic operations.
34 */
Paolo Bonziniab129972016-08-31 16:56:04 +020035static int pending_cpus;
36
Paolo Bonzini267f6852016-08-28 03:45:14 +020037void qemu_init_cpu_list(void)
38{
Paolo Bonziniab129972016-08-31 16:56:04 +020039 /* This is needed because qemu_init_cpu_list is also called by the
40 * child process in a fork. */
41 pending_cpus = 0;
42
Paolo Bonzini267f6852016-08-28 03:45:14 +020043 qemu_mutex_init(&qemu_cpu_list_lock);
Paolo Bonziniab129972016-08-31 16:56:04 +020044 qemu_cond_init(&exclusive_cond);
45 qemu_cond_init(&exclusive_resume);
Sergey Fedorovd148d902016-08-29 09:51:00 +020046 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini267f6852016-08-28 03:45:14 +020047}
48
49void cpu_list_lock(void)
50{
51 qemu_mutex_lock(&qemu_cpu_list_lock);
52}
53
54void cpu_list_unlock(void)
55{
56 qemu_mutex_unlock(&qemu_cpu_list_lock);
57}
58
Paolo Bonzini267f6852016-08-28 03:45:14 +020059
Harsh Prateek Bora18530e72024-06-18 13:53:53 +053060int cpu_get_free_index(void)
Paolo Bonzini267f6852016-08-28 03:45:14 +020061{
62 CPUState *some_cpu;
Alex Bennée716386e2020-05-20 15:05:38 +010063 int max_cpu_index = 0;
Paolo Bonzini267f6852016-08-28 03:45:14 +020064
Paolo Bonzini267f6852016-08-28 03:45:14 +020065 CPU_FOREACH(some_cpu) {
Alex Bennée716386e2020-05-20 15:05:38 +010066 if (some_cpu->cpu_index >= max_cpu_index) {
67 max_cpu_index = some_cpu->cpu_index + 1;
68 }
Paolo Bonzini267f6852016-08-28 03:45:14 +020069 }
Alex Bennée716386e2020-05-20 15:05:38 +010070 return max_cpu_index;
Paolo Bonzini267f6852016-08-28 03:45:14 +020071}
72
Philippe Mathieu-Daudé3c55dd52023-10-09 09:02:04 +020073CPUTailQ cpus_queue = QTAILQ_HEAD_INITIALIZER(cpus_queue);
Hyman Huang(黄勇)ab1a1612022-06-26 01:38:31 +080074static unsigned int cpu_list_generation_id;
75
76unsigned int cpu_list_generation_id_get(void)
77{
78 return cpu_list_generation_id;
79}
Philippe Mathieu-Daudé421a75e2020-07-02 12:40:17 +020080
Paolo Bonzini267f6852016-08-28 03:45:14 +020081void cpu_list_add(CPUState *cpu)
82{
Harsh Prateek Bora18530e72024-06-18 13:53:53 +053083 static bool cpu_index_auto_assigned;
84
Daniel Brodsky6e8a3552020-04-03 21:21:08 -070085 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
Paolo Bonzini267f6852016-08-28 03:45:14 +020086 if (cpu->cpu_index == UNASSIGNED_CPU_INDEX) {
Harsh Prateek Bora18530e72024-06-18 13:53:53 +053087 cpu_index_auto_assigned = true;
Paolo Bonzini267f6852016-08-28 03:45:14 +020088 cpu->cpu_index = cpu_get_free_index();
89 assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
90 } else {
91 assert(!cpu_index_auto_assigned);
92 }
Philippe Mathieu-Daudé3c55dd52023-10-09 09:02:04 +020093 QTAILQ_INSERT_TAIL_RCU(&cpus_queue, cpu, node);
Hyman Huang(黄勇)ab1a1612022-06-26 01:38:31 +080094 cpu_list_generation_id++;
Paolo Bonzini267f6852016-08-28 03:45:14 +020095}
96
97void cpu_list_remove(CPUState *cpu)
98{
Daniel Brodsky6e8a3552020-04-03 21:21:08 -070099 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
Paolo Bonzini267f6852016-08-28 03:45:14 +0200100 if (!QTAILQ_IN_USE(cpu, node)) {
101 /* there is nothing to undo since cpu_exec_init() hasn't been called */
Paolo Bonzini267f6852016-08-28 03:45:14 +0200102 return;
103 }
104
Philippe Mathieu-Daudé3c55dd52023-10-09 09:02:04 +0200105 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
Paolo Bonzini267f6852016-08-28 03:45:14 +0200106 cpu->cpu_index = UNASSIGNED_CPU_INDEX;
Hyman Huang(黄勇)ab1a1612022-06-26 01:38:31 +0800107 cpu_list_generation_id++;
Paolo Bonzini267f6852016-08-28 03:45:14 +0200108}
Sergey Fedorovd148d902016-08-29 09:51:00 +0200109
Philippe Mathieu-Daudé421a75e2020-07-02 12:40:17 +0200110CPUState *qemu_get_cpu(int index)
111{
112 CPUState *cpu;
113
114 CPU_FOREACH(cpu) {
115 if (cpu->cpu_index == index) {
116 return cpu;
117 }
118 }
119
120 return NULL;
121}
122
123/* current CPU in the current thread. It is only valid inside cpu_exec() */
124__thread CPUState *current_cpu;
125
Sergey Fedorovd148d902016-08-29 09:51:00 +0200126struct qemu_work_item {
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +0100127 QSIMPLEQ_ENTRY(qemu_work_item) node;
Sergey Fedorovd148d902016-08-29 09:51:00 +0200128 run_on_cpu_func func;
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100129 run_on_cpu_data data;
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200130 bool free, exclusive, done;
Sergey Fedorovd148d902016-08-29 09:51:00 +0200131};
132
133static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi)
134{
135 qemu_mutex_lock(&cpu->work_mutex);
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +0100136 QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node);
Sergey Fedorovd148d902016-08-29 09:51:00 +0200137 wi->done = false;
138 qemu_mutex_unlock(&cpu->work_mutex);
139
140 qemu_cpu_kick(cpu);
141}
142
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100143void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
Sergey Fedorovd148d902016-08-29 09:51:00 +0200144 QemuMutex *mutex)
145{
146 struct qemu_work_item wi;
147
148 if (qemu_cpu_is_self(cpu)) {
149 func(cpu, data);
150 return;
151 }
152
153 wi.func = func;
154 wi.data = data;
Paolo Bonzini0e555392016-09-06 17:28:03 +0200155 wi.done = false;
Sergey Fedorovd148d902016-08-29 09:51:00 +0200156 wi.free = false;
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200157 wi.exclusive = false;
Sergey Fedorovd148d902016-08-29 09:51:00 +0200158
159 queue_work_on_cpu(cpu, &wi);
Paolo Bonzini42abcc52023-03-03 11:07:04 +0100160 while (!qatomic_load_acquire(&wi.done)) {
Sergey Fedorovd148d902016-08-29 09:51:00 +0200161 CPUState *self_cpu = current_cpu;
162
163 qemu_cond_wait(&qemu_work_cond, mutex);
164 current_cpu = self_cpu;
165 }
166}
167
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100168void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
Sergey Fedorovd148d902016-08-29 09:51:00 +0200169{
170 struct qemu_work_item *wi;
171
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100172 wi = g_new0(struct qemu_work_item, 1);
Sergey Fedorovd148d902016-08-29 09:51:00 +0200173 wi->func = func;
174 wi->data = data;
175 wi->free = true;
176
177 queue_work_on_cpu(cpu, wi);
178}
179
Paolo Bonziniab129972016-08-31 16:56:04 +0200180/* Wait for pending exclusive operations to complete. The CPU list lock
181 must be held. */
182static inline void exclusive_idle(void)
183{
184 while (pending_cpus) {
185 qemu_cond_wait(&exclusive_resume, &qemu_cpu_list_lock);
186 }
187}
188
189/* Start an exclusive operation.
Paolo Bonzini758e1b22016-09-02 23:33:38 +0200190 Must only be called from outside cpu_exec. */
Paolo Bonziniab129972016-08-31 16:56:04 +0200191void start_exclusive(void)
192{
193 CPUState *other_cpu;
Paolo Bonzinic265e972016-08-31 21:33:58 +0200194 int running_cpus;
Paolo Bonziniab129972016-08-31 16:56:04 +0200195
Pierrick Bouvier779f30a2024-10-25 10:58:57 -0700196 /* Ensure we are not running, or start_exclusive will be blocked. */
197 g_assert(!current_cpu->running);
198
Ilya Leoshkevichdf8a6882023-02-14 15:08:27 +0100199 if (current_cpu->exclusive_context_count) {
200 current_cpu->exclusive_context_count++;
201 return;
202 }
203
Paolo Bonziniab129972016-08-31 16:56:04 +0200204 qemu_mutex_lock(&qemu_cpu_list_lock);
205 exclusive_idle();
206
207 /* Make all other cpus stop executing. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100208 qatomic_set(&pending_cpus, 1);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200209
210 /* Write pending_cpus before reading other_cpu->running. */
211 smp_mb();
212 running_cpus = 0;
Paolo Bonziniab129972016-08-31 16:56:04 +0200213 CPU_FOREACH(other_cpu) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100214 if (qatomic_read(&other_cpu->running)) {
Paolo Bonzinic265e972016-08-31 21:33:58 +0200215 other_cpu->has_waiter = true;
216 running_cpus++;
Paolo Bonziniab129972016-08-31 16:56:04 +0200217 qemu_cpu_kick(other_cpu);
218 }
219 }
Paolo Bonzinic265e972016-08-31 21:33:58 +0200220
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100221 qatomic_set(&pending_cpus, running_cpus + 1);
Paolo Bonziniab129972016-08-31 16:56:04 +0200222 while (pending_cpus > 1) {
223 qemu_cond_wait(&exclusive_cond, &qemu_cpu_list_lock);
224 }
Paolo Bonzini758e1b22016-09-02 23:33:38 +0200225
226 /* Can release mutex, no one will enter another exclusive
227 * section until end_exclusive resets pending_cpus to 0.
228 */
229 qemu_mutex_unlock(&qemu_cpu_list_lock);
Emilio G. Cotacfbc3c62018-11-26 17:14:43 -0500230
Ilya Leoshkevichdf8a6882023-02-14 15:08:27 +0100231 current_cpu->exclusive_context_count = 1;
Paolo Bonziniab129972016-08-31 16:56:04 +0200232}
233
Paolo Bonzini758e1b22016-09-02 23:33:38 +0200234/* Finish an exclusive operation. */
Paolo Bonziniab129972016-08-31 16:56:04 +0200235void end_exclusive(void)
236{
Ilya Leoshkevichdf8a6882023-02-14 15:08:27 +0100237 current_cpu->exclusive_context_count--;
238 if (current_cpu->exclusive_context_count) {
239 return;
240 }
Emilio G. Cotacfbc3c62018-11-26 17:14:43 -0500241
Paolo Bonzini758e1b22016-09-02 23:33:38 +0200242 qemu_mutex_lock(&qemu_cpu_list_lock);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100243 qatomic_set(&pending_cpus, 0);
Paolo Bonziniab129972016-08-31 16:56:04 +0200244 qemu_cond_broadcast(&exclusive_resume);
245 qemu_mutex_unlock(&qemu_cpu_list_lock);
246}
247
248/* Wait for exclusive ops to finish, and begin cpu execution. */
249void cpu_exec_start(CPUState *cpu)
250{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100251 qatomic_set(&cpu->running, true);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200252
253 /* Write cpu->running before reading pending_cpus. */
254 smp_mb();
255
256 /* 1. start_exclusive saw cpu->running == true and pending_cpus >= 1.
257 * After taking the lock we'll see cpu->has_waiter == true and run---not
258 * for long because start_exclusive kicked us. cpu_exec_end will
259 * decrement pending_cpus and signal the waiter.
260 *
261 * 2. start_exclusive saw cpu->running == false but pending_cpus >= 1.
262 * This includes the case when an exclusive item is running now.
263 * Then we'll see cpu->has_waiter == false and wait for the item to
264 * complete.
265 *
266 * 3. pending_cpus == 0. Then start_exclusive is definitely going to
267 * see cpu->running == true, and it will kick the CPU.
268 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100269 if (unlikely(qatomic_read(&pending_cpus))) {
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700270 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200271 if (!cpu->has_waiter) {
272 /* Not counted in pending_cpus, let the exclusive item
273 * run. Since we have the lock, just set cpu->running to true
274 * while holding it; no need to check pending_cpus again.
275 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100276 qatomic_set(&cpu->running, false);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200277 exclusive_idle();
278 /* Now pending_cpus is zero. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100279 qatomic_set(&cpu->running, true);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200280 } else {
281 /* Counted in pending_cpus, go ahead and release the
282 * waiter at cpu_exec_end.
283 */
284 }
Paolo Bonzinic265e972016-08-31 21:33:58 +0200285 }
Paolo Bonziniab129972016-08-31 16:56:04 +0200286}
287
288/* Mark cpu as not executing, and release pending exclusive ops. */
289void cpu_exec_end(CPUState *cpu)
290{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100291 qatomic_set(&cpu->running, false);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200292
293 /* Write cpu->running before reading pending_cpus. */
294 smp_mb();
295
296 /* 1. start_exclusive saw cpu->running == true. Then it will increment
297 * pending_cpus and wait for exclusive_cond. After taking the lock
298 * we'll see cpu->has_waiter == true.
299 *
300 * 2. start_exclusive saw cpu->running == false but here pending_cpus >= 1.
301 * This includes the case when an exclusive item started after setting
302 * cpu->running to false and before we read pending_cpus. Then we'll see
303 * cpu->has_waiter == false and not touch pending_cpus. The next call to
304 * cpu_exec_start will run exclusive_idle if still necessary, thus waiting
305 * for the item to complete.
306 *
307 * 3. pending_cpus == 0. Then start_exclusive is definitely going to
308 * see cpu->running == false, and it can ignore this CPU until the
309 * next cpu_exec_start.
310 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100311 if (unlikely(qatomic_read(&pending_cpus))) {
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700312 QEMU_LOCK_GUARD(&qemu_cpu_list_lock);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200313 if (cpu->has_waiter) {
314 cpu->has_waiter = false;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100315 qatomic_set(&pending_cpus, pending_cpus - 1);
Paolo Bonzinic265e972016-08-31 21:33:58 +0200316 if (pending_cpus == 1) {
317 qemu_cond_signal(&exclusive_cond);
318 }
Paolo Bonziniab129972016-08-31 16:56:04 +0200319 }
320 }
Paolo Bonziniab129972016-08-31 16:56:04 +0200321}
322
Paolo Bonzini14e6fe12016-10-31 10:36:08 +0100323void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func,
324 run_on_cpu_data data)
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200325{
326 struct qemu_work_item *wi;
327
Markus Armbrusterb21e2382022-03-15 15:41:56 +0100328 wi = g_new0(struct qemu_work_item, 1);
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200329 wi->func = func;
330 wi->data = data;
331 wi->free = true;
332 wi->exclusive = true;
333
334 queue_work_on_cpu(cpu, wi);
335}
336
Akihiko Odakif8b64d32024-07-14 19:46:52 +0900337void free_queued_cpu_work(CPUState *cpu)
338{
339 while (!QSIMPLEQ_EMPTY(&cpu->work_list)) {
340 struct qemu_work_item *wi = QSIMPLEQ_FIRST(&cpu->work_list);
341 QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node);
342 if (wi->free) {
343 g_free(wi);
344 }
345 }
346}
347
Sergey Fedorovd148d902016-08-29 09:51:00 +0200348void process_queued_cpu_work(CPUState *cpu)
349{
350 struct qemu_work_item *wi;
351
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +0100352 qemu_mutex_lock(&cpu->work_mutex);
353 if (QSIMPLEQ_EMPTY(&cpu->work_list)) {
354 qemu_mutex_unlock(&cpu->work_mutex);
Sergey Fedorovd148d902016-08-29 09:51:00 +0200355 return;
356 }
Emilio G. Cota0c0fcc22020-06-12 20:02:24 +0100357 while (!QSIMPLEQ_EMPTY(&cpu->work_list)) {
358 wi = QSIMPLEQ_FIRST(&cpu->work_list);
359 QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node);
Sergey Fedorovd148d902016-08-29 09:51:00 +0200360 qemu_mutex_unlock(&cpu->work_mutex);
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200361 if (wi->exclusive) {
362 /* Running work items outside the BQL avoids the following deadlock:
363 * 1) start_exclusive() is called with the BQL taken while another
364 * CPU is running; 2) cpu_exec in the other CPU tries to takes the
365 * BQL, so it goes to sleep; start_exclusive() is sleeping too, so
366 * neither CPU can proceed.
367 */
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500368 bql_unlock();
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200369 start_exclusive();
370 wi->func(cpu, wi->data);
371 end_exclusive();
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500372 bql_lock();
Paolo Bonzini53f5ed92016-08-28 05:38:24 +0200373 } else {
374 wi->func(cpu, wi->data);
375 }
Sergey Fedorovd148d902016-08-29 09:51:00 +0200376 qemu_mutex_lock(&cpu->work_mutex);
377 if (wi->free) {
378 g_free(wi);
379 } else {
Paolo Bonzini42abcc52023-03-03 11:07:04 +0100380 qatomic_store_release(&wi->done, true);
Sergey Fedorovd148d902016-08-29 09:51:00 +0200381 }
382 }
383 qemu_mutex_unlock(&cpu->work_mutex);
384 qemu_cond_broadcast(&qemu_work_cond);
385}
Philippe Mathieu-Daudé8a8dc262022-11-24 16:36:49 +0100386
387/* Add a breakpoint. */
388int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
389 CPUBreakpoint **breakpoint)
390{
Philippe Mathieu-Daudé8a8dc262022-11-24 16:36:49 +0100391 CPUBreakpoint *bp;
392
Philippe Mathieu-Daudé30e76632025-01-21 12:12:35 +0100393 if (cpu->cc->gdb_adjust_breakpoint) {
394 pc = cpu->cc->gdb_adjust_breakpoint(cpu, pc);
Philippe Mathieu-Daudé8a8dc262022-11-24 16:36:49 +0100395 }
396
397 bp = g_malloc(sizeof(*bp));
398
399 bp->pc = pc;
400 bp->flags = flags;
401
402 /* keep all GDB-injected breakpoints in front */
403 if (flags & BP_GDB) {
404 QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
405 } else {
406 QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
407 }
408
409 if (breakpoint) {
410 *breakpoint = bp;
411 }
412
413 trace_breakpoint_insert(cpu->cpu_index, pc, flags);
414 return 0;
415}
416
417/* Remove a specific breakpoint. */
418int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
419{
Philippe Mathieu-Daudé8a8dc262022-11-24 16:36:49 +0100420 CPUBreakpoint *bp;
421
Philippe Mathieu-Daudé30e76632025-01-21 12:12:35 +0100422 if (cpu->cc->gdb_adjust_breakpoint) {
423 pc = cpu->cc->gdb_adjust_breakpoint(cpu, pc);
Philippe Mathieu-Daudé8a8dc262022-11-24 16:36:49 +0100424 }
425
426 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
427 if (bp->pc == pc && bp->flags == flags) {
428 cpu_breakpoint_remove_by_ref(cpu, bp);
429 return 0;
430 }
431 }
432 return -ENOENT;
433}
434
435/* Remove a specific breakpoint by reference. */
436void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *bp)
437{
438 QTAILQ_REMOVE(&cpu->breakpoints, bp, entry);
439
440 trace_breakpoint_remove(cpu->cpu_index, bp->pc, bp->flags);
441 g_free(bp);
442}
443
444/* Remove all matching breakpoints. */
445void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
446{
447 CPUBreakpoint *bp, *next;
448
449 QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
450 if (bp->flags & mask) {
451 cpu_breakpoint_remove_by_ref(cpu, bp);
452 }
453 }
454}