From a1777f7f6462c66e1ee6e98f0d5c431bfe988aa5 Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Mon, 4 Jul 2016 13:06:35 +0100 Subject: memory: Provide memory_region_init_rom() Provide a new helper function memory_region_init_rom() for memory regions which are read-only (and unlike those created by memory_region_init_rom_device() don't have special behaviour for writes). This has the same behaviour as calling memory_region_init_ram() and then memory_region_set_readonly() (which is what we do today in boards with pure ROMs) but is a more easily discoverable API for the purpose. Signed-off-by: Peter Maydell Message-id: 1467122287-24974-2-git-send-email-peter.maydell@linaro.org --- memory.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'memory.c') diff --git a/memory.c b/memory.c index 33799e810b..ecb565ea81 100644 --- a/memory.c +++ b/memory.c @@ -1376,6 +1376,21 @@ void memory_region_init_alias(MemoryRegion *mr, mr->alias_offset = offset; } +void memory_region_init_rom(MemoryRegion *mr, + struct Object *owner, + const char *name, + uint64_t size, + Error **errp) +{ + memory_region_init(mr, owner, name, size); + mr->ram = true; + mr->readonly = true; + mr->terminates = true; + mr->destructor = memory_region_destructor_ram; + mr->ram_block = qemu_ram_alloc(size, mr, errp); + mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0; +} + void memory_region_init_rom_device(MemoryRegion *mr, Object *owner, const MemoryRegionOps *ops, -- cgit 1.4.1 From 39e0b03dec518254fabd2acff29548d3f1d2b754 Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Mon, 4 Jul 2016 13:06:35 +0100 Subject: memory: Assert that memory_region_init_rom_device() ops aren't NULL It doesn't make sense to pass a NULL ops argument to memory_region_init_rom_device(), because the effect will be that if the guest tries to write to the memory region then QEMU will segfault. Catch the bug earlier by sanity checking the arguments to this function, and remove the misleading documentation that suggests that passing NULL might be sensible. Signed-off-by: Peter Maydell Message-id: 1467122287-24974-4-git-send-email-peter.maydell@linaro.org --- include/exec/memory.h | 5 +---- memory.c | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'memory.c') diff --git a/include/exec/memory.h b/include/exec/memory.h index 2d9ea3c088..3e4d4164cd 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -467,12 +467,9 @@ void memory_region_init_rom(MemoryRegion *mr, * memory_region_init_rom_device: Initialize a ROM memory region. Writes are * handled via callbacks. * - * If NULL callbacks pointer is given, then I/O space is not supposed to be - * handled by QEMU itself. Any access via the memory API will cause an abort(). - * * @mr: the #MemoryRegion to be initialized. * @owner: the object that tracks the region's reference count - * @ops: callbacks for write access handling. + * @ops: callbacks for write access handling (must not be NULL). * @name: the name of the region. * @size: size of the region. * @errp: pointer to Error*, to store an error if it happens. diff --git a/memory.c b/memory.c index ecb565ea81..0eb6895fe6 100644 --- a/memory.c +++ b/memory.c @@ -1399,6 +1399,7 @@ void memory_region_init_rom_device(MemoryRegion *mr, uint64_t size, Error **errp) { + assert(ops); memory_region_init(mr, owner, name, size); mr->ops = ops; mr->opaque = opaque; -- cgit 1.4.1