hvmctl: convert HVMOP_modified_memory Also limiting "nr" at the libxc level to 32 bits (the high 32 bits of the previous 64-bit parameter got ignore so far). Signed-off-by: Jan Beulich Reviewed-by: Wei Liu Reviewed-by: Andrew Cooper --- a/tools/libxc/include/xenctrl.h +++ b/tools/libxc/include/xenctrl.h @@ -1620,7 +1620,7 @@ int xc_hvm_track_dirty_vram( * Notify that some pages got modified by the Device Model */ int xc_hvm_modified_memory( - xc_interface *xch, domid_t dom, uint64_t first_pfn, uint64_t nr); + xc_interface *xch, domid_t dom, uint64_t first_gfn, uint32_t nr); /* * Set a range of memory to a specific type. --- a/tools/libxc/xc_misc.c +++ b/tools/libxc/xc_misc.c @@ -558,29 +558,13 @@ int xc_hvm_track_dirty_vram( } int xc_hvm_modified_memory( - xc_interface *xch, domid_t dom, uint64_t first_pfn, uint64_t nr) + xc_interface *xch, domid_t dom, uint64_t first_gfn, uint32_t nr) { - DECLARE_HYPERCALL_BUFFER(struct xen_hvm_modified_memory, arg); - int rc; + DECLARE_HVMCTL(modified_memory, dom, + .first_gfn = first_gfn, + .nr = nr); - arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg)); - if ( arg == NULL ) - { - PERROR("Could not allocate memory for xc_hvm_modified_memory hypercall"); - return -1; - } - - arg->domid = dom; - arg->first_pfn = first_pfn; - arg->nr = nr; - - rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op, - HVMOP_modified_memory, - HYPERCALL_BUFFER_AS_ARG(arg)); - - xc_hypercall_buffer_free(xch, arg); - - return rc; + return do_hvmctl(xch, &hvmctl); } int xc_hvm_set_mem_type( --- a/xen/arch/x86/hvm/control.c +++ b/xen/arch/x86/hvm/control.c @@ -14,6 +14,7 @@ * this program; If not, see . */ +#include #include #include #include @@ -90,6 +91,51 @@ static int track_dirty_vram(struct domai : hap_track_dirty_vram(d, op->first_gfn, op->nr, op->dirty_bitmap); } +static int modified_memory(struct domain *d, + const struct xen_hvm_modified_memory *op, + uint64_t *iter) +{ + if ( !is_hvm_domain(d) ) + return -EINVAL; + + if ( op->rsvd || op->nr < *iter || + ((op->first_gfn + op->nr - 1) < op->first_gfn) || + ((op->first_gfn + op->nr - 1) > domain_get_maximum_gpfn(d)) ) + return -EINVAL; + + if ( !paging_mode_log_dirty(d) ) + return 0; + + while ( op->nr > *iter ) + { + unsigned long gfn = op->first_gfn + *iter; + struct page_info *page = get_page_from_gfn(d, gfn, NULL, P2M_UNSHARE); + + if ( page ) + { + unsigned long mfn = page_to_mfn(page); + + paging_mark_dirty(d, mfn); + /* + * These are most probably not page tables any more: + * Don't take a long time, and don't die either. + */ + sh_remove_shadows(d, _mfn(mfn), 1, 0); + put_page(page); + } + + /* + * Check for continuation every once in a while, and if it's not the + * last interation. + */ + if ( op->nr > ++*iter && !(*iter & 0xff) && + hypercall_preempt_check() ) + return -ERESTART; + } + + return 0; +} + long do_hvmctl(XEN_GUEST_HANDLE_PARAM(xen_hvmctl_t) u_hvmctl) { xen_hvmctl_t op; @@ -140,6 +186,10 @@ long do_hvmctl(XEN_GUEST_HANDLE_PARAM(xe rc = track_dirty_vram(d, &op.u.track_dirty_vram); break; + case XEN_HVMCTL_modified_memory: + rc = modified_memory(d, &op.u.modified_memory, &op.opaque); + break; + default: rc = -EOPNOTSUPP; break; --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -5233,7 +5233,6 @@ long do_hvm_op(unsigned long op, XEN_GUE default: mask = ~0UL; break; - case HVMOP_modified_memory: case HVMOP_set_mem_type: mask = HVMOP_op_mask; break; @@ -5296,65 +5295,6 @@ long do_hvm_op(unsigned long op, XEN_GUE rc = guest_handle_is_null(arg) ? hvmop_flush_tlb_all() : -ENOSYS; break; - case HVMOP_modified_memory: - { - struct xen_hvm_modified_memory a; - struct domain *d; - - if ( copy_from_guest(&a, arg, 1) ) - return -EFAULT; - - rc = rcu_lock_remote_domain_by_id(a.domid, &d); - if ( rc != 0 ) - return rc; - - rc = -EINVAL; - if ( !is_hvm_domain(d) ) - goto modmem_fail; - - rc = xsm_hvm_control(XSM_DM_PRIV, d, op); - if ( rc ) - goto modmem_fail; - - rc = -EINVAL; - if ( a.nr < start_iter || - ((a.first_pfn + a.nr - 1) < a.first_pfn) || - ((a.first_pfn + a.nr - 1) > domain_get_maximum_gpfn(d)) ) - goto modmem_fail; - - rc = 0; - if ( !paging_mode_log_dirty(d) ) - goto modmem_fail; - - while ( a.nr > start_iter ) - { - unsigned long pfn = a.first_pfn + start_iter; - struct page_info *page; - - page = get_page_from_gfn(d, pfn, NULL, P2M_UNSHARE); - if ( page ) - { - paging_mark_dirty(d, page_to_mfn(page)); - /* These are most probably not page tables any more */ - /* don't take a long time and don't die either */ - sh_remove_shadows(d, _mfn(page_to_mfn(page)), 1, 0); - put_page(page); - } - - /* Check for continuation if it's not the last interation */ - if ( a.nr > ++start_iter && !(start_iter & HVMOP_op_mask) && - hypercall_preempt_check() ) - { - rc = -ERESTART; - break; - } - } - - modmem_fail: - rcu_unlock_domain(d); - break; - } - case HVMOP_get_mem_type: { struct xen_hvm_get_mem_type a; --- a/xen/include/public/hvm/control.h +++ b/xen/include/public/hvm/control.h @@ -67,6 +67,16 @@ struct xen_hvm_track_dirty_vram { XEN_GUEST_HANDLE_64(uint8) dirty_bitmap; }; +/* XEN_HVMCTL_modified_memory */ +/* Notify that some pages got modified by the Device Model. */ +struct xen_hvm_modified_memory { + /* Number of pages. */ + uint32_t nr; + uint32_t rsvd; + /* First GFN. */ + uint64_aligned_t first_gfn; +}; + struct xen_hvmctl { uint16_t interface_version; /* XEN_HVMCTL_INTERFACE_VERSION */ domid_t domain; @@ -75,12 +85,14 @@ struct xen_hvmctl { #define XEN_HVMCTL_set_isa_irq_level 2 #define XEN_HVMCTL_set_pci_link_route 3 #define XEN_HVMCTL_track_dirty_vram 4 +#define XEN_HVMCTL_modified_memory 5 uint64_t opaque; /* Must be zero on initial invocation. */ union { struct xen_hvm_set_pci_intx_level set_pci_intx_level; struct xen_hvm_set_isa_irq_level set_isa_irq_level; struct xen_hvm_set_pci_link_route set_pci_link_route; struct xen_hvm_track_dirty_vram track_dirty_vram; + struct xen_hvm_modified_memory modified_memory; uint8_t pad[120]; } u; }; --- a/xen/include/public/hvm/hvm_op.h +++ b/xen/include/public/hvm/hvm_op.h @@ -98,19 +98,6 @@ typedef enum { /* Following tools-only interfaces may change in future. */ #if defined(__XEN__) || defined(__XEN_TOOLS__) -/* Notify that some pages got modified by the Device Model. */ -#define HVMOP_modified_memory 7 -struct xen_hvm_modified_memory { - /* Domain to be updated. */ - domid_t domid; - /* Number of pages. */ - uint32_t nr; - /* First pfn. */ - uint64_aligned_t first_pfn; -}; -typedef struct xen_hvm_modified_memory xen_hvm_modified_memory_t; -DEFINE_XEN_GUEST_HANDLE(xen_hvm_modified_memory_t); - #define HVMOP_set_mem_type 8 /* Notify that a region of memory is to be treated in a specific way. */ struct xen_hvm_set_mem_type { --- a/xen/xsm/flask/policy/access_vectors +++ b/xen/xsm/flask/policy/access_vectors @@ -272,7 +272,7 @@ class hvm cacheattr # XEN_HVMCTL_track_dirty_vram trackdirtyvram -# HVMOP_modified_memory, HVMOP_get_mem_type, HVMOP_set_mem_type, +# XEN_HVMCTL_modified_memory, HVMOP_get_mem_type, HVMOP_set_mem_type, # HVMOP_set_mem_access, HVMOP_get_mem_access, HVMOP_pagetable_dying, # HVMOP_inject_trap hvmctl