[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v9 33/74] cpu: define cpu_interrupt_request helpers
From: |
Robert Foley |
Subject: |
[PATCH v9 33/74] cpu: define cpu_interrupt_request helpers |
Date: |
Thu, 21 May 2020 12:39:30 -0400 |
From: "Emilio G. Cota" <address@hidden>
Add a comment about how atomic_read works here. The comment refers to
a "BQL-less CPU loop", which will materialize toward the end
of this series.
Note that the modifications to cpu_reset_interrupt are there to
avoid deadlock during the CPU lock transition; once that is complete,
cpu_interrupt_request will be simple again.
Reviewed-by: Richard Henderson <address@hidden>
Reviewed-by: Alex Bennée <address@hidden>
Signed-off-by: Emilio G. Cota <address@hidden>
Signed-off-by: Robert Foley <address@hidden>
---
hw/core/cpu.c | 27 +++++++++++++++++++++------
include/hw/core/cpu.h | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 6 deletions(-)
diff --git a/hw/core/cpu.c b/hw/core/cpu.c
index 67a9c2bb16..be9efbeaf4 100644
--- a/hw/core/cpu.c
+++ b/hw/core/cpu.c
@@ -99,14 +99,29 @@ static void cpu_common_get_memory_mapping(CPUState *cpu,
* BQL here if we need to. cpu_interrupt assumes it is held.*/
void cpu_reset_interrupt(CPUState *cpu, int mask)
{
- bool need_lock = !qemu_mutex_iothread_locked();
+ bool has_bql = qemu_mutex_iothread_locked();
+ bool has_cpu_lock = cpu_mutex_locked(cpu);
- if (need_lock) {
- qemu_mutex_lock_iothread();
+ if (has_bql) {
+ if (has_cpu_lock) {
+ atomic_set(&cpu->interrupt_request, cpu->interrupt_request &
~mask);
+ } else {
+ cpu_mutex_lock(cpu);
+ atomic_set(&cpu->interrupt_request, cpu->interrupt_request &
~mask);
+ cpu_mutex_unlock(cpu);
+ }
+ return;
+ }
+
+ if (has_cpu_lock) {
+ cpu_mutex_unlock(cpu);
}
- cpu->interrupt_request &= ~mask;
- if (need_lock) {
- qemu_mutex_unlock_iothread();
+ qemu_mutex_lock_iothread();
+ cpu_mutex_lock(cpu);
+ atomic_set(&cpu->interrupt_request, cpu->interrupt_request & ~mask);
+ qemu_mutex_unlock_iothread();
+ if (!has_cpu_lock) {
+ cpu_mutex_unlock(cpu);
}
}
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 820c5b0a9a..6ed806db60 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -522,6 +522,43 @@ static inline void cpu_halted_set(CPUState *cpu, uint32_t
val)
cpu_mutex_unlock(cpu);
}
+/*
+ * When sending an interrupt, setters OR the appropriate bit and kick the
+ * destination vCPU. The latter can then read interrupt_request without
+ * acquiring the CPU lock, because once the kick-induced completes, they'll
read
+ * an up-to-date interrupt_request.
+ * Setters always acquire the lock, which guarantees that (1) concurrent
+ * updates from different threads won't result in data races, and (2) the
+ * BQL-less CPU loop will always see an up-to-date interrupt_request, since the
+ * loop holds the CPU lock.
+ */
+static inline uint32_t cpu_interrupt_request(CPUState *cpu)
+{
+ return atomic_read(&cpu->interrupt_request);
+}
+
+static inline void cpu_interrupt_request_or(CPUState *cpu, uint32_t mask)
+{
+ if (cpu_mutex_locked(cpu)) {
+ atomic_set(&cpu->interrupt_request, cpu->interrupt_request | mask);
+ return;
+ }
+ cpu_mutex_lock(cpu);
+ atomic_set(&cpu->interrupt_request, cpu->interrupt_request | mask);
+ cpu_mutex_unlock(cpu);
+}
+
+static inline void cpu_interrupt_request_set(CPUState *cpu, uint32_t val)
+{
+ if (cpu_mutex_locked(cpu)) {
+ atomic_set(&cpu->interrupt_request, val);
+ return;
+ }
+ cpu_mutex_lock(cpu);
+ atomic_set(&cpu->interrupt_request, val);
+ cpu_mutex_unlock(cpu);
+}
+
static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
{
unsigned int i;
--
2.17.1
- [PATCH v9 22/74] lm32: convert to cpu_halted, (continued)
- [PATCH v9 22/74] lm32: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 24/74] mips: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 25/74] riscv: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 28/74] xtensa: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 29/74] gdbstub: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 30/74] openrisc: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 26/74] s390x: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 27/74] sparc: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 31/74] cpu-exec: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 32/74] cpu: convert to cpu_halted, Robert Foley, 2020/05/21
- [PATCH v9 33/74] cpu: define cpu_interrupt_request helpers,
Robert Foley <=
- [PATCH v9 34/74] ppc: use cpu_reset_interrupt, Robert Foley, 2020/05/21
- [PATCH v9 35/74] exec: use cpu_reset_interrupt, Robert Foley, 2020/05/21
- [PATCH v9 36/74] i386: use cpu_reset_interrupt, Robert Foley, 2020/05/21
- [PATCH v9 37/74] s390x: use cpu_reset_interrupt, Robert Foley, 2020/05/21
- [PATCH v9 38/74] openrisc: use cpu_reset_interrupt, Robert Foley, 2020/05/21
- [PATCH v9 39/74] arm: convert to cpu_interrupt_request, Robert Foley, 2020/05/21
- [PATCH v9 40/74] i386: convert to cpu_interrupt_request, Robert Foley, 2020/05/21
- [PATCH v9 41/74] i386/kvm: convert to cpu_interrupt_request, Robert Foley, 2020/05/21
- [PATCH v9 42/74] i386/hax-all: convert to cpu_interrupt_request, Robert Foley, 2020/05/21
- [PATCH v9 43/74] i386/whpx-all: convert to cpu_interrupt_request, Robert Foley, 2020/05/21