From 0adc3fa0122c33e0f46b5a41dcc0d3ff65e346a0 Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 31 Aug 2025 20:18:03 +0900 Subject: [PATCH] drivers: xen: memory: add acquire_resource wrapper Add XENMEM_acquire_resource wrapper to map device model related buffers. This is required for communication with the ioreq server. - memory - xendom_acquire_resource: HYPERVISOR_memory_op(XENMEM_acquire_resource) Signed-off-by: TOKITA Hiroshi --- drivers/xen/memory.c | 22 +++++++++ include/zephyr/xen/memory.h | 25 ++++++++++ include/zephyr/xen/public/memory.h | 78 ++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) diff --git a/drivers/xen/memory.c b/drivers/xen/memory.c index 6472ad74af253..5deefbde10827 100644 --- a/drivers/xen/memory.c +++ b/drivers/xen/memory.c @@ -66,3 +66,25 @@ int xendom_populate_physmap(int domid, unsigned int extent_order, return HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); } + +int xendom_acquire_resource(domid_t domid, uint16_t type, uint32_t id, uint64_t frame, + uint32_t *nr_frames, xen_pfn_t *frame_list) +{ + struct xen_mem_acquire_resource acquire_res = { + .domid = domid, + .type = type, + .id = id, + .pad = 0, + .frame = frame, + .nr_frames = *nr_frames, + }; + int ret; + + set_xen_guest_handle(acquire_res.frame_list, frame_list); + + ret = HYPERVISOR_memory_op(XENMEM_acquire_resource, &acquire_res); + + *nr_frames = acquire_res.nr_frames; + + return ret; +} diff --git a/include/zephyr/xen/memory.h b/include/zephyr/xen/memory.h index 636f09988566b..1614d8fade82f 100644 --- a/include/zephyr/xen/memory.h +++ b/include/zephyr/xen/memory.h @@ -3,6 +3,9 @@ * Copyright (c) 2023 EPAM Systems */ +#ifndef ZEPHYR_XEN_MEMORY_H_ +#define ZEPHYR_XEN_MEMORY_H_ + #include #include #include @@ -64,3 +67,25 @@ int xendom_remove_from_physmap(int domid, xen_pfn_t gpfn); int xendom_populate_physmap(int domid, unsigned int extent_order, unsigned int nr_extents, unsigned int mem_flags, xen_pfn_t *extent_start); + +/** + * @brief Acquire a resource mapping for the Xen domain. + * + * Issues the XENMEM_acquire_resource hypercall to map a resource buffer + * (e.g., I/O request server, grant table, VM trace buffer) into the + * specified domain's physmap, or query its total size. + * + * @param domid Target domain identifier. Use DOMID_SELF for the calling domain. + * @param type Resource type identifier (e.g., XENMEM_resource_ioreq_server). + * @param id Resource-specific identifier (e.g., server ID or table ID). + * @param frame Starting frame number for mapping, or ignored if *nr_frames == 0. + * @param nr_frames [in,out] On input, number of frames to map; on return, + * number of frames actually mapped (or total frames if queried). + * @param frame_list Guest frame list buffer: input GFNs for HVM guests, + * output MFNs for PV guests. + * @return Zero on success, or a negative errno code on failure. + */ +int xendom_acquire_resource(domid_t domid, uint16_t type, uint32_t id, uint64_t frame, + uint32_t *nr_frames, xen_pfn_t *frame_list); + +#endif /* ZEPHYR_XEN_MEMORY_H_ */ diff --git a/include/zephyr/xen/public/memory.h b/include/zephyr/xen/public/memory.h index 2baf69ef2391a..35df28f97a08c 100644 --- a/include/zephyr/xen/public/memory.h +++ b/include/zephyr/xen/public/memory.h @@ -161,4 +161,82 @@ struct xen_remove_from_physmap { typedef struct xen_remove_from_physmap xen_remove_from_physmap_t; DEFINE_XEN_GUEST_HANDLE(xen_remove_from_physmap_t); +/* + * Get the pages for a particular guest resource, so that they can be + * mapped directly by a tools domain. + */ +#define XENMEM_acquire_resource 28 +struct xen_mem_acquire_resource { + /* IN - The domain whose resource is to be mapped */ + domid_t domid; + /* IN - the type of resource */ + uint16_t type; + +#define XENMEM_resource_ioreq_server 0 +#define XENMEM_resource_grant_table 1 +#define XENMEM_resource_vmtrace_buf 2 + + /* + * IN - a type-specific resource identifier, which must be zero + * unless stated otherwise. + * + * type == XENMEM_resource_ioreq_server -> id == ioreq server id + * type == XENMEM_resource_grant_table -> id defined below + */ + uint32_t id; + +#define XENMEM_resource_grant_table_id_shared 0 +#define XENMEM_resource_grant_table_id_status 1 + + /* + * IN/OUT + * + * As an IN parameter number of frames of the resource to be mapped. + * This value may be updated over the course of the operation. + * + * When frame_list is NULL and nr_frames is 0, this is interpreted as a + * request for the size of the resource, which shall be returned in the + * nr_frames field. + * + * The size of a resource will never be zero, but a nonzero result doesn't + * guarantee that a subsequent mapping request will be successful. There + * are further type/id specific constraints which may change between the + * two calls. + */ + uint32_t nr_frames; + /* + * Padding field, must be zero on input. + * In a previous version this was an output field with the lowest bit + * named XENMEM_rsrc_acq_caller_owned. Future versions of this interface + * will not reuse this bit as an output with the field being zero on + * input. + */ + uint32_t pad; + /* + * IN - the index of the initial frame to be mapped. This parameter + * is ignored if nr_frames is 0. This value may be updated + * over the course of the operation. + */ + uint64_t frame; + +#define XENMEM_resource_ioreq_server_frame_bufioreq 0 +#define XENMEM_resource_ioreq_server_frame_ioreq(n) (1 + (n)) + + /* + * IN/OUT - If the tools domain is PV then, upon return, frame_list + * will be populated with the MFNs of the resource. + * If the tools domain is HVM then it is expected that, on + * entry, frame_list will be populated with a list of GFNs + * that will be mapped to the MFNs of the resource. + * If -EIO is returned then the frame_list has only been + * partially mapped and it is up to the caller to unmap all + * the GFNs. + * This parameter may be NULL if nr_frames is 0. This + * value may be updated over the course of the operation. + */ + XEN_GUEST_HANDLE(xen_pfn_t) frame_list; +}; +typedef struct xen_mem_acquire_resource xen_mem_acquire_resource_t; +DEFINE_XEN_GUEST_HANDLE(xen_mem_acquire_resource_t); + #endif /* __XEN_PUBLIC_MEMORY_H__ */