[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH qemu 6/7] hvf: add a lock for memory related functions
From: |
~ubzeme |
Subject: |
[PATCH qemu 6/7] hvf: add a lock for memory related functions |
Date: |
Mon, 28 Feb 2022 09:41:52 +0800 |
From: Yan-Jie Wang <ubzeme@gmail.com>
We follow how KVM accel does in its memory listener (kvm-all.c) and add
a lock for the memory related functions.
Signed-off-by: Yan-Jie Wang <ubzeme@gmail.com>
---
accel/hvf/hvf-mem.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c
index b8d8bf6799..4948578adf 100644
--- a/accel/hvf/hvf-mem.c
+++ b/accel/hvf/hvf-mem.c
@@ -43,6 +43,7 @@ typedef struct HVFSlot {
} HVFSlot;
static HVFSlot memslots[HVF_NUM_SLOTS];
+static QemuMutex memlock;
static HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size)
{
@@ -140,6 +141,8 @@ static void hvf_set_phys_mem(MemoryRegionSection *section,
bool add)
readonly = memory_region_is_rom(area) || memory_region_is_romd(area);
/* setup a slot */
+ qemu_mutex_lock(&memlock);
+
slot = hvf_find_free_slot();
if (!slot) {
error_report("No free slots");
@@ -169,8 +172,12 @@ static void hvf_set_phys_mem(MemoryRegionSection *section,
bool add)
ret = hv_vm_map(host_addr, start, size, flags);
assert_hvf_ok(ret);
+
+ qemu_mutex_unlock(&memlock);
} else {
/* remove memory region */
+ qemu_mutex_lock(&memlock);
+
slot = hvf_find_overlap_slot(start, size);
if (slot) {
@@ -179,6 +186,8 @@ static void hvf_set_phys_mem(MemoryRegionSection *section,
bool add)
slot->size = 0;
}
+
+ qemu_mutex_unlock(&memlock);
}
}
@@ -186,6 +195,8 @@ static void hvf_set_dirty_tracking(MemoryRegionSection
*section, bool on)
{
HVFSlot *slot;
+ qemu_mutex_lock(&memlock);
+
slot = hvf_find_overlap_slot(
section->offset_within_address_space,
int128_get64(section->size));
@@ -201,6 +212,8 @@ static void hvf_set_dirty_tracking(MemoryRegionSection
*section, bool on)
hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size,
HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC);
}
+
+ qemu_mutex_unlock(&memlock);
}
static void hvf_log_start(MemoryListener *listener,
@@ -271,10 +284,13 @@ bool hvf_access_memory(hwaddr address, bool write)
hv_return_t ret;
hwaddr start, size;
+ qemu_mutex_lock(&memlock);
+
slot = hvf_find_overlap_slot(address, 1);
if (!slot || (write && slot->flags & HVF_SLOT_READONLY)) {
/* MMIO or unmapped area, return false */
+ qemu_mutex_unlock(&memlock);
return false;
}
@@ -290,10 +306,12 @@ bool hvf_access_memory(hwaddr address, bool write)
assert_hvf_ok(ret);
}
+ qemu_mutex_unlock(&memlock);
return true;
}
void hvf_init_memslots(void)
{
+ qemu_mutex_init(&memlock);
memory_listener_register(&hvf_memory_listener, &address_space_memory);
}
--
2.34.1
- [PATCH qemu 0/7] Many improvements to HVF memory-related codes, ~ubzeme, 2022/02/28
- [PATCH qemu 1/7] hvf: move memory related functions from hvf-accel-ops.c to hvf-mem.c, ~ubzeme, 2022/02/28
- [PATCH qemu 6/7] hvf: add a lock for memory related functions,
~ubzeme <=
- [PATCH qemu 2/7] hvf: simplify data structures and codes of memory related functions, ~ubzeme, 2022/02/28
- [PATCH qemu 5/7] hvf: fix memory dirty-tracking, ~ubzeme, 2022/02/28
- [PATCH qemu 4/7] hvf: rename struct hvf_slot to HVFSlot, ~ubzeme, 2022/02/28
- [PATCH qemu 3/7] hvf: use correct data types for addresses in memory related functions, ~ubzeme, 2022/02/28
- [PATCH qemu 7/7] hvf: use GTree to store memory slots instead of fixed-size array, ~ubzeme, 2022/02/28
- Re: [PATCH qemu 0/7] Many improvements to HVF memory-related codes, Peter Maydell, 2022/02/28