aboutsummaryrefslogtreecommitdiff
path: root/backends/hostmem-file.c
diff options
context:
space:
mode:
authorAlexander Graf <graf@amazon.com>2023-04-03 22:14:21 +0000
committerDavid Hildenbrand <david@redhat.com>2023-05-23 16:47:03 +0200
commit4b870dc4d0c0895859d34d14ce0272a4bcbccf78 (patch)
tree8af35d1db6ba0cd63419627870a0a75aec9bf150 /backends/hostmem-file.c
parent886c0453cbf10eebd42a9ccf89c3e46eb389c357 (diff)
hostmem-file: add offset option
Add an option for hostmem-file to start the memory object at an offset into the target file. This is useful if multiple memory objects reside inside the same target file, such as a device node. In particular, it's useful to map guest memory directly into /dev/mem for experimentation. To make this work consistently, also fix up all places in QEMU that expect fd offsets to be 0. Signed-off-by: Alexander Graf <graf@amazon.com> Message-Id: <20230403221421.60877-1-graf@amazon.com> Acked-by: Markus Armbruster <armbru@redhat.com> Acked-by: Peter Xu <peterx@redhat.com> Reviewed-by: David Hildenbrand <david@redhat.com> Signed-off-by: David Hildenbrand <david@redhat.com>
Diffstat (limited to 'backends/hostmem-file.c')
-rw-r--r--backends/hostmem-file.c40
1 files changed, 39 insertions, 1 deletions
diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c
index 25141283c4..38ea65bec5 100644
--- a/backends/hostmem-file.c
+++ b/backends/hostmem-file.c
@@ -27,6 +27,7 @@ struct HostMemoryBackendFile {
char *mem_path;
uint64_t align;
+ uint64_t offset;
bool discard_data;
bool is_pmem;
bool readonly;
@@ -58,7 +59,8 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
backend->size, fb->align, ram_flags,
- fb->mem_path, fb->readonly, errp);
+ fb->mem_path, fb->offset, fb->readonly,
+ errp);
g_free(name);
#endif
}
@@ -125,6 +127,36 @@ static void file_memory_backend_set_align(Object *o, Visitor *v,
fb->align = val;
}
+static void file_memory_backend_get_offset(Object *o, Visitor *v,
+ const char *name, void *opaque,
+ Error **errp)
+{
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
+ uint64_t val = fb->offset;
+
+ visit_type_size(v, name, &val, errp);
+}
+
+static void file_memory_backend_set_offset(Object *o, Visitor *v,
+ const char *name, void *opaque,
+ Error **errp)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(o);
+ HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
+ uint64_t val;
+
+ if (host_memory_backend_mr_inited(backend)) {
+ error_setg(errp, "cannot change property '%s' of %s", name,
+ object_get_typename(o));
+ return;
+ }
+
+ if (!visit_type_size(v, name, &val, errp)) {
+ return;
+ }
+ fb->offset = val;
+}
+
#ifdef CONFIG_LIBPMEM
static bool file_memory_backend_get_pmem(Object *o, Error **errp)
{
@@ -197,6 +229,12 @@ file_backend_class_init(ObjectClass *oc, void *data)
file_memory_backend_get_align,
file_memory_backend_set_align,
NULL, NULL);
+ object_class_property_add(oc, "offset", "int",
+ file_memory_backend_get_offset,
+ file_memory_backend_set_offset,
+ NULL, NULL);
+ object_class_property_set_description(oc, "offset",
+ "Offset into the target file (ex: 1G)");
#ifdef CONFIG_LIBPMEM
object_class_property_add_bool(oc, "pmem",
file_memory_backend_get_pmem, file_memory_backend_set_pmem);