Skip to contentSkip to conversation

Alternate p2m: support multiple copies of host p2m

PATCH Superseded Ed White 6 participants 56 messages Jul 14, 2015 – Jul 22, 2015

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Ed White

author
#0 lore raw

v5 cover Alternate p2m: support multiple copies of host p2m

This set of patches adds support to hvm domains for EPTP switching by creating multiple copies of the host p2m (currently limited to 10 copies).

The primary use of this capability is expected to be in scenarios where access to memory needs to be monitored and/or restricted below the level at which the guest OS page tables operate. Two examples that were discussed at the 2014 Xen developer summit are:

VM introspection:
http://www.slideshare.net/xen_com_mgr/
zero-footprint-guest-memory-introspection-from-xen

Secure inter-VM communication:
http://www.slideshare.net/xen_com_mgr/nakajima-nvf

A more detailed design specification can be found at:

    http://lists.xenproject.org/archives/html/xen-devel/2015-06/msg01319.html

Each p2m copy is populated lazily on EPT violations.
Permissions for pages in alternate p2m's can be changed in a similar way to the existing memory access interface, and gfn->mfn mappings can be changed.

All this is done through extra HVMOP types.

The cross-domain HVMOP code has been compile-tested only. Also, the cross-domain code is hypervisor-only, the toolstack has not been modified.

The intra-domain code has been tested. Violation notifications can only be received for pages that have been modified (access permissions and/or gfn->mfn mapping) intra-domain, and only on VCPU's that have enabled notification.

VMFUNC and #VE will both be emulated on hardware without native support.

This code is not compatible with nested hvm functionality and will refuse to work with nested hvm active. It is also not compatible with migration. It should be considered experimental.

Changes since v4:

Patch 3: don't set bit 63 of top-level entries.

Patch 5: extra locking order description in mm-locks.h
don't initialise altp2m data unless altp2m is enabled globally
and hardware supports it
removed some hardware-specific wrappers that were not being used renamed ap2m... interfaces to altp2m...
fixed error path in p2m_init_altp2m

Patch 7: addressed remaining feedback

Patch 8: made suppress_ve preservation consistent

Patch 9: changed flag bit to avoid collision with recently applied series

Patch 10: check pad fields for zero
minor formatting changes

Patch 11: renamed HVM parameter

Patch 15: removed v3 workaround

Changes since v3:

Major changes are:

Replaced patch 8.

Refactored patch 11 to use a single HVMOP with subcodes.

Addressed feedback in patch 7, and some other patches.

Added two tools/test patches from Tamas. Both are optional.

Added various ack's and reviewed-by's.

Rebased.

Ravi Sahita will now be the point of contact for this series.

Changes since v2:

Addressed all v2 feedback *except*:

In patch 5, the per-domain EPTP list page is still allocated from the Xen heap. If allocated from the domain heap Xen panics - IIRC on Haswell hardware when walking the EPTP list during exit processing in patch 6.

HVM_ops are not merged. Tamas suggested merging the memory access ops, but in practice they are not as similar as they appear on the surface. Razvan suggested merging the implementation code in p2m.c, but that is also not as common as it appears on the surface.
Andrew suggested merging all altp2m ops into one with a subop code in the input stucture. His point that only 255 ops can be defined is well taken, but altp2m uses only 2 more ops than the recently introduced ioreq ops, and <15% of the available ops have been defined. Since we don't know how to implement XSM hooks and policy with the subop model, we have not adopted this suggestion.

The p2m set/get interface is not modified. The altp2m code needs to write suppress_ve in 2 places and read it in 1 place. The original patch series managed this by coupling the state of suppress_ve to the p2m memory type, which Tim disliked. In v2 of the series, special set/get interaces were added to access suppress_ve only when required. Jan has suggested changing the existing interfaces, but we feel this is inappropriate for this experimental patch series. Changing the existing interfaces would require a design agreement to be reached and would impact a large amount of existing code.

Andrew kindly added some reviewed-by's to v2. I have not carried his reviewed-by of the memory event patch forward because Tamas requested significant changes to the patch.

Changes since v1:

Many changes since v1 in response to maintainer feedback, including:

Suppress_ve state is now decoupled from memory type
VMFUNC emulation handled in x86 emulator
Lazy-copy algorithm copies any page where mfn != INVALID_MFN All nested page fault handling except lazy-copy is now in
top-level (hvm.c) nested page fault handler
Split p2m lock type (as suggested by Tim) to avoid lock order violations XSM hooks
Xen parameter to globally enable altp2m (default disabled) and HVM parameter Altp2m reference counting no longer uses dirty_cpu bitmap Remapped page tracking to invalidate altp2m's where needed to protect Xen Many other minor changes

The altp2m invalidation is implemented to a level that I believe satisifies the requirements of protecting Xen. Invalidation notification is not yet implemented, and there may be other cases where invalidation is warranted to protect the integrity of the restrictions placed through altp2m. We may add further patches in this area.

Testability is still a potential issue. We have offered to make our internal Windows test binaries available for intra-domain testing. Tamas has been working on toolstack support for cross-domain testing with a slightly earlier patch series, and we hope he will submit that support.

Not all of the patches will be of interest to everyone copied here. I've copied everyone on this initial mailing to give context.

Andrew Cooper (1):
common/domain: Helpers to pause a domain while in context

Ed White (9):
VMX: VMFUNC and #VE definitions and detection.
VMX: implement suppress #VE.
x86/HVM: Hardware alternate p2m support detection. x86/altp2m: basic data structures and support routines. VMX/altp2m: add code to support EPTP switching and #VE. x86/altp2m: alternate p2m memory events. x86/altp2m: add remaining support routines. x86/altp2m: define and implement alternate p2m HVMOP types. x86/altp2m: Add altp2mhvm HVM domain parameter.

George Dunlap (1):
x86/altp2m: add control of suppress_ve.

Ravi Sahita (2):
VMX: add VMFUNC leaf 0 (EPTP switching) to emulator. x86/altp2m: XSM hooks for altp2m HVM ops

Tamas K Lengyel (2):
tools/libxc: add support to altp2m hvmops
tools/xen-access: altp2m testcases

 docs/man/xl.cfg.pod.5                        |  12 +
 docs/misc/xen-command-line.markdown          |   7 +
 tools/flask/policy/policy/modules/xen/xen.if |   4 +-
 tools/libxc/Makefile                         |   1 +
 tools/libxc/include/xenctrl.h                |  21 +
 tools/libxc/xc_altp2m.c                      | 237 ++++++++++++
 tools/libxl/libxl.h                          |   6 +
 tools/libxl/libxl_create.c                   |   1 +
 tools/libxl/libxl_dom.c                      |   2 +
 tools/libxl/libxl_types.idl                  |   1 +
 tools/libxl/xl_cmdimpl.c                     |  10 +
 tools/tests/xen-access/xen-access.c          | 173 +++++++--
 xen/arch/x86/hvm/Makefile                    |   1 +
 xen/arch/x86/hvm/altp2m.c                    |  77 ++++
 xen/arch/x86/hvm/emulate.c                   |  19 +-
 xen/arch/x86/hvm/hvm.c                       | 253 +++++++++++-
 xen/arch/x86/hvm/vmx/vmcs.c                  |  42 +-
 xen/arch/x86/hvm/vmx/vmx.c                   | 177 +++++++++
 xen/arch/x86/mm/hap/Makefile                 |   1 +
 xen/arch/x86/mm/hap/altp2m_hap.c             |  98 +++++
 xen/arch/x86/mm/hap/hap.c                    |  38 +-
 xen/arch/x86/mm/mem_sharing.c                |   4 +-
 xen/arch/x86/mm/mm-locks.h                   |  46 ++-
 xen/arch/x86/mm/p2m-ept.c                    |  37 +-
 xen/arch/x86/mm/p2m-pod.c                    |  12 +-
 xen/arch/x86/mm/p2m-pt.c                     |  10 +-
 xen/arch/x86/mm/p2m.c                        | 554 +++++++++++++++++++++++++--
 xen/arch/x86/x86_emulate/x86_emulate.c       |  19 +-
 xen/arch/x86/x86_emulate/x86_emulate.h       |   4 +
 xen/common/domain.c                          |  28 ++
 xen/common/vm_event.c                        |   4 +
 xen/include/asm-arm/p2m.h                    |   6 +
 xen/include/asm-x86/domain.h                 |  10 +
 xen/include/asm-x86/hvm/altp2m.h             |  42 ++
 xen/include/asm-x86/hvm/hvm.h                |  25 ++
 xen/include/asm-x86/hvm/vcpu.h               |   9 +
 xen/include/asm-x86/hvm/vmx/vmcs.h           |  14 +-
 xen/include/asm-x86/hvm/vmx/vmx.h            |  13 +-
 xen/include/asm-x86/msr-index.h              |   1 +
 xen/include/asm-x86/p2m.h                    |  92 ++++-
 xen/include/public/hvm/hvm_op.h              |  82 ++++
 xen/include/public/hvm/params.h              |   5 +-
 xen/include/public/vm_event.h                |  11 +
 xen/include/xen/sched.h                      |   5 +
 xen/include/xsm/dummy.h                      |  12 +
 xen/include/xsm/xsm.h                        |  12 +
 xen/xsm/dummy.c                              |   2 +
 xen/xsm/flask/hooks.c                        |  12 +
 xen/xsm/flask/policy/access_vectors          |   7 +
 49 files changed, 2156 insertions(+), 103 deletions(-)
 create mode 100644 tools/libxc/xc_altp2m.c
 create mode 100644 xen/arch/x86/hvm/altp2m.c
 create mode 100644 xen/arch/x86/mm/hap/altp2m_hap.c
 create mode 100644 xen/include/asm-x86/hvm/altp2m.h
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v5 1/15 common/domain: Helpers to pause a domain while in context

From: Andrew Cooper <[email protected]>

For use on codepaths which would need to use domain_pause() but might be in the target domain's context. In the case that the target domain is in context, all other vcpus are paused.

---
 xen/common/domain.c     | 28 ++++++++++++++++++++++++++++
 xen/include/xen/sched.h |  5 +++++
 2 files changed, 33 insertions(+)
xen/common/domain.c +28 -0
index 3bc52e6..1bb24ae 100644
@@ -1010,6 +1010,34 @@ int domain_unpause_by_systemcontroller(struct domain *d)
     return 0;
 }
 
+void domain_pause_except_self(struct domain *d)
+{
+    struct vcpu *v, *curr = current;
+
+    if ( curr->domain == d )
+    {
+        for_each_vcpu( d, v )
+            if ( likely(v != curr) )
+                vcpu_pause(v);
+    }
+    else
+        domain_pause(d);
+}
+
+void domain_unpause_except_self(struct domain *d)
+{
+    struct vcpu *v, *curr = current;
+
+    if ( curr->domain == d )
+    {
+        for_each_vcpu( d, v )
+            if ( likely(v != curr) )
+                vcpu_unpause(v);
+    }
+    else
+        domain_unpause(d);
+}
+
 int vcpu_reset(struct vcpu *v)
 {
     struct domain *d = v->domain;
xen/include/xen/sched.h +5 -0
index b29d9e7..73d3bc8 100644
@@ -804,6 +804,11 @@ static inline int domain_pause_by_systemcontroller_nosync(struct domain *d)
 {
     return __domain_pause_by_systemcontroller(d, domain_pause_nosync);
 }
+
+/* domain_pause() but safe against trying to pause current. */
+void domain_pause_except_self(struct domain *d);
+void domain_unpause_except_self(struct domain *d);
+
 void cpu_init(void);
 
 struct scheduler;
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v5 2/15 VMX: VMFUNC and #VE definitions and detection.

In reply to Ed White

Currently, neither is enabled globally but may be enabled on a per-VCPU basis by the altp2m code.

Remove the check for EPTE bit 63 == zero in ept_split_super_page(), as that bit is now hardware-defined.

---
 xen/arch/x86/hvm/vmx/vmcs.c        | 42 +++++++++++++++++++++++++++++++++++---
 xen/arch/x86/mm/p2m-ept.c          |  1 -
 xen/include/asm-x86/hvm/vmx/vmcs.h | 14 +++++++++++--
 xen/include/asm-x86/hvm/vmx/vmx.h  | 13 +++++++++++-
 xen/include/asm-x86/msr-index.h    |  1 +
 5 files changed, 64 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +39 -3
index 4c5ceb5..bc1cabd 100644
@@ -101,6 +101,8 @@ u32 vmx_secondary_exec_control __read_mostly;
 u32 vmx_vmexit_control __read_mostly;
 u32 vmx_vmentry_control __read_mostly;
 u64 vmx_ept_vpid_cap __read_mostly;
+u64 vmx_vmfunc __read_mostly;
+bool_t vmx_virt_exception __read_mostly;
 
 const u32 vmx_introspection_force_enabled_msrs[] = {
     MSR_IA32_SYSENTER_EIP,
@@ -140,6 +142,8 @@ static void __init vmx_display_features(void)
     P(cpu_has_vmx_virtual_intr_delivery, "Virtual Interrupt Delivery");
     P(cpu_has_vmx_posted_intr_processing, "Posted Interrupt Processing");
     P(cpu_has_vmx_vmcs_shadowing, "VMCS shadowing");
+    P(cpu_has_vmx_vmfunc, "VM Functions");
+    P(cpu_has_vmx_virt_exceptions, "Virtualisation Exceptions");
     P(cpu_has_vmx_pml, "Page Modification Logging");
 #undef P
 
@@ -185,6 +189,7 @@ static int vmx_init_vmcs_config(void)
     u64 _vmx_misc_cap = 0;
     u32 _vmx_vmexit_control;
     u32 _vmx_vmentry_control;
+    u64 _vmx_vmfunc = 0;
     bool_t mismatch = 0;
 
     rdmsr(MSR_IA32_VMX_BASIC, vmx_basic_msr_low, vmx_basic_msr_high);
@@ -230,7 +235,9 @@ static int vmx_init_vmcs_config(void)
                SECONDARY_EXEC_ENABLE_EPT |
                SECONDARY_EXEC_ENABLE_RDTSCP |
                SECONDARY_EXEC_PAUSE_LOOP_EXITING |
-               SECONDARY_EXEC_ENABLE_INVPCID);
+               SECONDARY_EXEC_ENABLE_INVPCID |
+               SECONDARY_EXEC_ENABLE_VM_FUNCTIONS |
+               SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS);
         rdmsrl(MSR_IA32_VMX_MISC, _vmx_misc_cap);
         if ( _vmx_misc_cap & VMX_MISC_VMWRITE_ALL )
             opt |= SECONDARY_EXEC_ENABLE_VMCS_SHADOWING;
@@ -341,6 +348,24 @@ static int vmx_init_vmcs_config(void)
           || !(_vmx_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT) )
         _vmx_pin_based_exec_control  &= ~ PIN_BASED_POSTED_INTERRUPT;
 
+    /* The IA32_VMX_VMFUNC MSR exists only when VMFUNC is available */
+    if ( _vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VM_FUNCTIONS )
+    {
+        rdmsrl(MSR_IA32_VMX_VMFUNC, _vmx_vmfunc);
+
+        /*
+         * VMFUNC leaf 0 (EPTP switching) must be supported.
+         *
+         * Or we just don't use VMFUNC.
+         */
+        if ( !(_vmx_vmfunc & VMX_VMFUNC_EPTP_SWITCHING) )
+            _vmx_secondary_exec_control &= ~SECONDARY_EXEC_ENABLE_VM_FUNCTIONS;
+    }
+
+    /* Virtualization exceptions are only enabled if VMFUNC is enabled */
+    if ( !(_vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )
+        _vmx_secondary_exec_control &= ~SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;
+
     min = 0;
     opt = VM_ENTRY_LOAD_GUEST_PAT | VM_ENTRY_LOAD_BNDCFGS;
     _vmx_vmentry_control = adjust_vmx_controls(
@@ -361,6 +386,9 @@ static int vmx_init_vmcs_config(void)
         vmx_vmentry_control        = _vmx_vmentry_control;
         vmx_basic_msr              = ((u64)vmx_basic_msr_high << 32) |
                                      vmx_basic_msr_low;
+        vmx_vmfunc                 = _vmx_vmfunc;
+        vmx_virt_exception         = !!(_vmx_secondary_exec_control &
+                                       SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS);
         vmx_display_features();
 
         /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
@@ -397,6 +425,9 @@ static int vmx_init_vmcs_config(void)
         mismatch |= cap_check(
             "EPT and VPID Capability",
             vmx_ept_vpid_cap, _vmx_ept_vpid_cap);
+        mismatch |= cap_check(
+            "VMFUNC Capability",
+            vmx_vmfunc, _vmx_vmfunc);
         if ( cpu_has_vmx_ins_outs_instr_info !=
              !!(vmx_basic_msr_high & (VMX_BASIC_INS_OUT_INFO >> 32)) )
         {
@@ -967,6 +998,11 @@ static int construct_vmcs(struct vcpu *v)
     /* Do not enable Monitor Trap Flag unless start single step debug */
     v->arch.hvm_vmx.exec_control &= ~CPU_BASED_MONITOR_TRAP_FLAG;
 
+    /* Disable VMFUNC and #VE for now: they may be enabled later by altp2m. */
+    v->arch.hvm_vmx.secondary_exec_control &=
+        ~(SECONDARY_EXEC_ENABLE_VM_FUNCTIONS |
+          SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS);
+
     if ( is_pvh_domain(d) )
     {
         /* Disable virtual apics, TPR */
@@ -1790,9 +1826,9 @@ void vmcs_dump_vcpu(struct vcpu *v)
         printk("PLE Gap=%08x Window=%08x\n",
                vmr32(PLE_GAP), vmr32(PLE_WINDOW));
     if ( v->arch.hvm_vmx.secondary_exec_control &
-         (SECONDARY_EXEC_ENABLE_VPID | SECONDARY_EXEC_ENABLE_VMFUNC) )
+         (SECONDARY_EXEC_ENABLE_VPID | SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )
         printk("Virtual processor ID = 0x%04x VMfunc controls = %016lx\n",
-               vmr16(VIRTUAL_PROCESSOR_ID), vmr(VMFUNC_CONTROL));
+               vmr16(VIRTUAL_PROCESSOR_ID), vmr(VM_FUNCTION_CONTROL));
 
     vmx_vmcs_exit(v);
 }
xen/arch/x86/mm/p2m-ept.c +0 -1
index 5133eb6..a6c9adf 100644
@@ -281,7 +281,6 @@ static int ept_split_super_page(struct p2m_domain *p2m, ept_entry_t *ept_entry,
         epte->sp = (level > 1);
         epte->mfn += i * trunk;
         epte->snp = (iommu_enabled && iommu_snoop);
-        ASSERT(!epte->avail3);
 
         ept_p2m_type_to_flags(p2m, epte, epte->sa_p2mt, epte->access);
 
xen/include/asm-x86/hvm/vmx/vmcs.h +12 -2
index 1104bda..cb0ee6c 100644
@@ -222,9 +222,10 @@ extern u32 vmx_vmentry_control;
 #define SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200
 #define SECONDARY_EXEC_PAUSE_LOOP_EXITING       0x00000400
 #define SECONDARY_EXEC_ENABLE_INVPCID           0x00001000
-#define SECONDARY_EXEC_ENABLE_VMFUNC            0x00002000
+#define SECONDARY_EXEC_ENABLE_VM_FUNCTIONS      0x00002000
 #define SECONDARY_EXEC_ENABLE_VMCS_SHADOWING    0x00004000
 #define SECONDARY_EXEC_ENABLE_PML               0x00020000
+#define SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS   0x00040000
 extern u32 vmx_secondary_exec_control;
 
 #define VMX_EPT_EXEC_ONLY_SUPPORTED             0x00000001
@@ -285,6 +286,10 @@ extern u32 vmx_secondary_exec_control;
     (vmx_pin_based_exec_control & PIN_BASED_POSTED_INTERRUPT)
 #define cpu_has_vmx_vmcs_shadowing \
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VMCS_SHADOWING)
+#define cpu_has_vmx_vmfunc \
+    (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VM_FUNCTIONS)
+#define cpu_has_vmx_virt_exceptions \
+    (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS)
 #define cpu_has_vmx_pml \
     (vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_PML)
 
@@ -316,6 +321,9 @@ extern u64 vmx_basic_msr;
 #define VMX_GUEST_INTR_STATUS_SUBFIELD_BITMASK  0x0FF
 #define VMX_GUEST_INTR_STATUS_SVI_OFFSET        8
 
+/* VMFUNC leaf definitions */
+#define VMX_VMFUNC_EPTP_SWITCHING   (1ULL << 0)
+
 /* VMCS field encodings. */
 #define VMCS_HIGH(x) ((x) | 1)
 enum vmcs_field {
@@ -350,12 +358,14 @@ enum vmcs_field {
     VIRTUAL_APIC_PAGE_ADDR          = 0x00002012,
     APIC_ACCESS_ADDR                = 0x00002014,
     PI_DESC_ADDR                    = 0x00002016,
-    VMFUNC_CONTROL                  = 0x00002018,
+    VM_FUNCTION_CONTROL             = 0x00002018,
     EPT_POINTER                     = 0x0000201a,
     EOI_EXIT_BITMAP0                = 0x0000201c,
 #define EOI_EXIT_BITMAP(n) (EOI_EXIT_BITMAP0 + (n) * 2) /* n = 0...3 */
+    EPTP_LIST_ADDR                  = 0x00002024,
     VMREAD_BITMAP                   = 0x00002026,
     VMWRITE_BITMAP                  = 0x00002028,
+    VIRT_EXCEPTION_INFO             = 0x0000202a,
     GUEST_PHYSICAL_ADDRESS          = 0x00002400,
     VMCS_LINK_POINTER               = 0x00002800,
     GUEST_IA32_DEBUGCTL             = 0x00002802,
xen/include/asm-x86/hvm/vmx/vmx.h +12 -1
index 35f804a..5b59d3c 100644
@@ -47,7 +47,7 @@ typedef union {
         access      :   4,  /* bits 61:58 - p2m_access_t */
         tm          :   1,  /* bit 62 - VT-d transient-mapping hint in
                                shared EPT/VT-d usage */
-        avail3      :   1;  /* bit 63 - Software available 3 */
+        suppress_ve :   1;  /* bit 63 - suppress #VE */
     };
     u64 epte;
 } ept_entry_t;
@@ -186,6 +186,7 @@ static inline unsigned long pi_get_pir(struct pi_desc *pi_desc, int group)
 #define EXIT_REASON_XSETBV              55
 #define EXIT_REASON_APIC_WRITE          56
 #define EXIT_REASON_INVPCID             58
+#define EXIT_REASON_VMFUNC              59
 #define EXIT_REASON_PML_FULL            62
 
 /*
@@ -554,4 +555,14 @@ void p2m_init_hap_data(struct p2m_domain *p2m);
 #define EPT_L4_PAGETABLE_SHIFT      39
 #define EPT_PAGETABLE_ENTRIES       512
 
+/* #VE information page */
+typedef struct {
+    u32 exit_reason;
+    u32 semaphore;
+    u64 exit_qualification;
+    u64 gla;
+    u64 gpa;
+    u16 eptp_index;
+} ve_info_t;
+
 #endif /* __ASM_X86_HVM_VMX_VMX_H__ */
xen/include/asm-x86/msr-index.h +1 -0
index 83f2f70..8069d60 100644
@@ -130,6 +130,7 @@
 #define MSR_IA32_VMX_TRUE_PROCBASED_CTLS        0x48e
 #define MSR_IA32_VMX_TRUE_EXIT_CTLS             0x48f
 #define MSR_IA32_VMX_TRUE_ENTRY_CTLS            0x490
+#define MSR_IA32_VMX_VMFUNC                     0x491
 #define IA32_FEATURE_CONTROL_MSR                0x3a
 #define IA32_FEATURE_CONTROL_MSR_LOCK                     0x0001
 #define IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON_INSIDE_SMX  0x0002
Signature · 1 line
1.9.1
author
#3 lore raw

PATCH v5 3/15 VMX: implement suppress #VE.

In reply to Ed White

In preparation for selectively enabling #VE in a later patch, set suppress #VE on all EPTE's.

Suppress #VE should always be the default condition for two reasons:
it is generally not safe to deliver #VE into a guest unless that guest has been modified to receive it; and even then for most EPT violations only the hypervisor is able to handle the violation.

---
 xen/arch/x86/mm/p2m-ept.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
xen/arch/x86/mm/p2m-ept.c +16 -1
index a6c9adf..59410ea 100644
@@ -41,7 +41,8 @@
 #define is_epte_superpage(ept_entry)    ((ept_entry)->sp)
 static inline bool_t is_epte_valid(ept_entry_t *e)
 {
-    return (e->epte != 0 && e->sa_p2mt != p2m_invalid);
+    /* suppress_ve alone is not considered valid, so mask it off */
+    return ((e->epte & ~(1ul << 63)) != 0 && e->sa_p2mt != p2m_invalid);
 }
 
 /* returns : 0 for success, -errno otherwise */
@@ -219,6 +220,8 @@ static void ept_p2m_type_to_flags(struct p2m_domain *p2m, ept_entry_t *entry,
 static int ept_set_middle_entry(struct p2m_domain *p2m, ept_entry_t *ept_entry)
 {
     struct page_info *pg;
+    ept_entry_t *table;
+    unsigned int i;
 
     pg = p2m_alloc_ptp(p2m, 0);
     if ( pg == NULL )
@@ -232,6 +235,15 @@ static int ept_set_middle_entry(struct p2m_domain *p2m, ept_entry_t *ept_entry)
     /* Manually set A bit to avoid overhead of MMU having to write it later. */
     ept_entry->a = 1;
 
+    ept_entry->suppress_ve = 1;
+
+    table = __map_domain_page(pg);
+
+    for ( i = 0; i < EPT_PAGETABLE_ENTRIES; i++ )
+        table[i].suppress_ve = 1;
+
+    unmap_domain_page(table);
+
     return 1;
 }
 
@@ -281,6 +293,7 @@ static int ept_split_super_page(struct p2m_domain *p2m, ept_entry_t *ept_entry,
         epte->sp = (level > 1);
         epte->mfn += i * trunk;
         epte->snp = (iommu_enabled && iommu_snoop);
+        epte->suppress_ve = 1;
 
         ept_p2m_type_to_flags(p2m, epte, epte->sa_p2mt, epte->access);
 
@@ -790,6 +803,8 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         ept_p2m_type_to_flags(p2m, &new_entry, p2mt, p2ma);
     }
 
+    new_entry.suppress_ve = 1;
+
     rc = atomic_write_ept_entry(ept_entry, new_entry, target);
     if ( unlikely(rc) )
         old_entry.epte = 0;
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH v5 03/15] VMX: implement suppress #VE.

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:

In preparation for selectively enabling #VE in a later patch, set suppress #VE on all EPTE's.

Suppress #VE should always be the default condition for two reasons:
it is generally not safe to deliver #VE into a guest unless that guest has been modified to receive it; and even then for most EPT violations only the hypervisor is able to handle the violation.

Quoted 6 lines

---

Missing revision log here. And I think the dropping of the adjustment to ept_p2m_init() invalidates reviews as well as Jun's ack - just consider if my request to do so was wrong for some reason, and neither of them saw me asking for this.

Jan

Acked-by on v5 3/15
#5 lore raw

Re: [PATCH v5 03/15] VMX: implement suppress #VE.

In reply to Ed White
Quoted 8 lines · Ed White

In preparation for selectively enabling #VE in a later patch, set suppress #VE on all EPTE's.

Suppress #VE should always be the default condition for two reasons:
it is generally not safe to deliver #VE into a guest unless that guest has been modified to receive it; and even then for most EPT violations only the hypervisor is able to handle the violation.

Quoted 4 lines

As Jan says, you should have dropped the reviewed-by's here.

author
#6 lore raw

PATCH v5 4/15 x86/HVM: Hardware alternate p2m support detection.

In reply to Ed White

As implemented here, only supported on platforms with VMX HAP.

By default this functionality is force-disabled, it can be enabled by specifying altp2m=1 on the Xen command line.

---
 docs/misc/xen-command-line.markdown | 7 +++++++
 xen/arch/x86/hvm/hvm.c              | 7 +++++++
 xen/arch/x86/hvm/vmx/vmx.c          | 1 +
 xen/include/asm-x86/hvm/hvm.h       | 9 +++++++++
 4 files changed, 24 insertions(+)
docs/misc/xen-command-line.markdown +7 -0
index aa684c0..3391c66 100644
@@ -139,6 +139,13 @@ mode during S3 resume.
 > Default: `true`
 
 Permit Xen to use superpages when performing memory management.
+ 
+### altp2m (Intel)
+> `= <boolean>`
+
++> Default: `false`
+
+Permit multiple copies of host p2m.
 
 ### apic
 > `= bigsmp | default`
xen/arch/x86/hvm/hvm.c +7 -0
index 535d622..4019658 100644
@@ -94,6 +94,10 @@ bool_t opt_hvm_fep;
 boolean_param("hvm_fep", opt_hvm_fep);
 #endif
 
+/* Xen command-line option to enable altp2m */
+static bool_t __initdata opt_altp2m_enabled = 0;
+boolean_param("altp2m", opt_altp2m_enabled);
+
 static int cpu_callback(
     struct notifier_block *nfb, unsigned long action, void *hcpu)
 {
@@ -160,6 +164,9 @@ static int __init hvm_enable(void)
     if ( !fns->pvh_supported )
         printk(XENLOG_INFO "HVM: PVH mode not supported on this platform\n");
 
+    if ( !opt_altp2m_enabled )
+        hvm_funcs.altp2m_supported = 0;
+
     /*
      * Allow direct access to the PC debug ports 0x80 and 0xed (they are
      * often used for I/O delays, but the vmexits simply slow things down).
xen/arch/x86/hvm/vmx/vmx.c +1 -0
index fc29b89..07527dd 100644
@@ -1841,6 +1841,7 @@ const struct hvm_function_table * __init start_vmx(void)
     if ( cpu_has_vmx_ept && (cpu_has_vmx_pat || opt_force_ept) )
     {
         vmx_function_table.hap_supported = 1;
+        vmx_function_table.altp2m_supported = 1;
 
         vmx_function_table.hap_capabilities = 0;
 
xen/include/asm-x86/hvm/hvm.h +9 -0
index 57f9605..c61cfe7 100644
@@ -94,6 +94,9 @@ struct hvm_function_table {
     /* Necessary hardware support for PVH mode? */
     int pvh_supported;
 
+    /* Necessary hardware support for alternate p2m's? */
+    bool_t altp2m_supported;
+
     /* Indicate HAP capabilities. */
     int hap_capabilities;
 
@@ -509,6 +512,12 @@ bool_t nhvm_vmcx_hap_enabled(struct vcpu *v);
 /* interrupt */
 enum hvm_intblk nhvm_interrupt_blocked(struct vcpu *v);
 
+/* returns true if hardware supports alternate p2m's */
+static inline bool_t hvm_altp2m_supported(void)
+{
+    return hvm_funcs.altp2m_supported;
+}
+
 #ifndef NDEBUG
 /* Permit use of the Forced Emulation Prefix in HVM guests */
 extern bool_t opt_hvm_fep;
Signature · 1 line
1.9.1
author
#7 lore raw

PATCH v5 5/15 x86/altp2m: basic data structures and support routines.

In reply to Ed White

Add the basic data structures needed to support alternate p2m's and the functions to initialise them and tear them down.

Although Intel hardware can handle 512 EPTP's per hardware thread concurrently, only 10 per domain are supported in this patch for performance reasons.

The iterator in hap_enable() does need to handle 512, so that is now uint16_t.

This change also splits the p2m lock into one lock type for altp2m's and another type for all other p2m's. The purpose of this is to place the altp2m list lock between the types, so the list lock can be acquired whilst holding the host p2m lock.

---
 xen/arch/x86/hvm/Makefile        |   1 +
 xen/arch/x86/hvm/altp2m.c        |  77 +++++++++++++++++++++++++++++
 xen/arch/x86/hvm/hvm.c           |  21 ++++++++
 xen/arch/x86/mm/hap/hap.c        |  38 ++++++++++++++-
 xen/arch/x86/mm/mm-locks.h       |  46 +++++++++++++++++-
 xen/arch/x86/mm/p2m.c            | 102 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h     |  10 ++++
 xen/include/asm-x86/hvm/altp2m.h |  38 +++++++++++++++
 xen/include/asm-x86/hvm/hvm.h    |  14 ++++++
 xen/include/asm-x86/hvm/vcpu.h   |   9 ++++
 xen/include/asm-x86/p2m.h        |  32 +++++++++++-
 11 files changed, 384 insertions(+), 4 deletions(-)
 create mode 100644 xen/arch/x86/hvm/altp2m.c
 create mode 100644 xen/include/asm-x86/hvm/altp2m.h
xen/arch/x86/hvm/Makefile +1 -0
index 69af47f..eb1a37b 100644
@@ -1,6 +1,7 @@
 subdir-y += svm
 subdir-y += vmx
 
+obj-y += altp2m.o
 obj-y += asid.o
 obj-y += emulate.o
 obj-y += event.o
xen/arch/x86/hvm/altp2m.c new file +77 -0

77 lines not on this page · raw message

xen/arch/x86/hvm/hvm.c +21 -0
index 4019658..38deedc 100644
@@ -58,6 +58,7 @@
 #include <asm/hvm/cacheattr.h>
 #include <asm/hvm/trace.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/altp2m.h>
 #include <asm/hvm/event.h>
 #include <asm/mtrr.h>
 #include <asm/apic.h>
@@ -2380,6 +2381,7 @@ void hvm_vcpu_destroy(struct vcpu *v)
 {
     hvm_all_ioreq_servers_remove_vcpu(v->domain, v);
 
+    altp2m_vcpu_destroy(v);
     nestedhvm_vcpu_destroy(v);
 
     free_compat_arg_xlat(v);
@@ -6498,6 +6500,25 @@ enum hvm_intblk nhvm_interrupt_blocked(struct vcpu *v)
     return hvm_funcs.nhvm_intr_blocked(v);
 }
 
+void altp2m_vcpu_update_eptp(struct vcpu *v)
+{
+    if ( hvm_funcs.altp2m_vcpu_update_eptp )
+        hvm_funcs.altp2m_vcpu_update_eptp(v);
+}
+
+void altp2m_vcpu_update_vmfunc_ve(struct vcpu *v)
+{
+    if ( hvm_funcs.altp2m_vcpu_update_vmfunc_ve )
+        hvm_funcs.altp2m_vcpu_update_vmfunc_ve(v);
+}
+
+bool_t altp2m_vcpu_emulate_ve(struct vcpu *v)
+{
+    if ( hvm_funcs.altp2m_vcpu_emulate_ve )
+        return hvm_funcs.altp2m_vcpu_emulate_ve(v);
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/x86/mm/hap/hap.c +37 -1
index d0d3f1e..8dd3c20 100644
@@ -459,7 +459,7 @@ void hap_domain_init(struct domain *d)
 int hap_enable(struct domain *d, u32 mode)
 {
     unsigned int old_pages;
-    uint8_t i;
+    uint16_t i;
     int rv = 0;
 
     domain_pause(d);
@@ -498,6 +498,28 @@ int hap_enable(struct domain *d, u32 mode)
            goto out;
     }
 
+    if ( hvm_altp2m_supported() )
+    {
+        /* Init alternate p2m data */
+        if ( (d->arch.altp2m_eptp = alloc_xenheap_page()) == NULL )
+        {
+            rv = -ENOMEM;
+            goto out;
+        }
+
+        for ( i = 0; i < MAX_EPTP; i++ )
+            d->arch.altp2m_eptp[i] = INVALID_MFN;
+
+        for ( i = 0; i < MAX_ALTP2M; i++ )
+        {
+            rv = p2m_alloc_table(d->arch.altp2m_p2m[i]);
+            if ( rv != 0 )
+               goto out;
+        }
+
+        d->arch.altp2m_active = 0;
+    }
+
     /* Now let other users see the new mode */
     d->arch.paging.mode = mode | PG_HAP_enable;
 
@@ -510,6 +532,20 @@ void hap_final_teardown(struct domain *d)
 {
     uint8_t i;
 
+    if ( hvm_altp2m_supported() )
+    {
+        d->arch.altp2m_active = 0;
+
+        if ( d->arch.altp2m_eptp )
+        {
+            free_xenheap_page(d->arch.altp2m_eptp);
+            d->arch.altp2m_eptp = NULL;
+        }
+
+        for ( i = 0; i < MAX_ALTP2M; i++ )
+            p2m_teardown(d->arch.altp2m_p2m[i]);
+    }
+
     /* Destroy nestedp2m's first */
     for (i = 0; i < MAX_NESTEDP2M; i++) {
         p2m_teardown(d->arch.nested_p2m[i]);
xen/arch/x86/mm/mm-locks.h +44 -2

61 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +102 -0

127 lines not on this page · raw message

xen/include/asm-x86/domain.h +10 -0
index 96bde65..294e089 100644
@@ -234,6 +234,10 @@ struct paging_vcpu {
 typedef xen_domctl_cpuid_t cpuid_input_t;
 
 #define MAX_NESTEDP2M 10
+
+#define MAX_ALTP2M      ((uint16_t)10)
+#define INVALID_ALTP2M  ((uint16_t)~0)
+#define MAX_EPTP        (PAGE_SIZE / sizeof(uint64_t))
 struct p2m_domain;
 struct time_scale {
     int shift;
@@ -293,6 +297,12 @@ struct arch_domain
     struct p2m_domain *nested_p2m[MAX_NESTEDP2M];
     mm_lock_t nested_p2m_lock;
 
+    /* altp2m: allow multiple copies of host p2m */
+    bool_t altp2m_active;
+    struct p2m_domain *altp2m_p2m[MAX_ALTP2M];
+    mm_lock_t altp2m_list_lock;
+    uint64_t *altp2m_eptp;
+
     /* NB. protected by d->event_lock and by irq_desc[irq].lock */
     struct radix_tree_root irq_pirq;
 
xen/include/asm-x86/hvm/altp2m.h new file +38 -0
new file mode 100644
index 0000000..38de494
@@ -0,0 +1,38 @@
+/*
+ * Alternate p2m HVM
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _ALTP2M_H
+#define _ALTP2M_H
+
+#include <xen/types.h>
+#include <xen/sched.h>         /* for struct vcpu, struct domain */
+#include <asm/hvm/vcpu.h>      /* for vcpu_altp2m */
+
+/* Alternate p2m HVM on/off per domain */
+static inline bool_t altp2m_active(const struct domain *d)
+{
+    return d->arch.altp2m_active;
+}
+
+/* Alternate p2m VCPU */
+void altp2m_vcpu_initialise(struct vcpu *v);
+void altp2m_vcpu_destroy(struct vcpu *v);
+void altp2m_vcpu_reset(struct vcpu *v);
+
+#endif /* _ALTP2M_H */
+
xen/include/asm-x86/hvm/hvm.h +14 -0
index c61cfe7..473ac30 100644
@@ -210,6 +210,11 @@ struct hvm_function_table {
                                   uint32_t *ecx, uint32_t *edx);
 
     void (*enable_msr_exit_interception)(struct domain *d);
+
+    /* Alternate p2m */
+    void (*altp2m_vcpu_update_eptp)(struct vcpu *v);
+    void (*altp2m_vcpu_update_vmfunc_ve)(struct vcpu *v);
+    bool_t (*altp2m_vcpu_emulate_ve)(struct vcpu *v);
 };
 
 extern struct hvm_function_table hvm_funcs;
@@ -525,6 +530,15 @@ extern bool_t opt_hvm_fep;
 #define opt_hvm_fep 0
 #endif
 
+/* updates the current EPTP in VMCS */
+void altp2m_vcpu_update_eptp(struct vcpu *v);
+
+/* updates VMCS fields related to VMFUNC and #VE */
+void altp2m_vcpu_update_vmfunc_ve(struct vcpu *v);
+
+/* emulates #VE */
+bool_t altp2m_vcpu_emulate_ve(struct vcpu *v);
+
 #endif /* __ASM_X86_HVM_HVM_H__ */
 
 /*
xen/include/asm-x86/hvm/vcpu.h +9 -0
index 3d8f4dc..09f25c1 100644
@@ -118,6 +118,13 @@ struct nestedvcpu {
 
 #define vcpu_nestedhvm(v) ((v)->arch.hvm_vcpu.nvcpu)
 
+struct altp2mvcpu {
+    uint16_t    p2midx;         /* alternate p2m index */
+    gfn_t       veinfo_gfn;     /* #VE information page gfn */
+};
+
+#define vcpu_altp2m(v) ((v)->arch.hvm_vcpu.avcpu)
+
 struct hvm_vcpu {
     /* Guest control-register and EFER values, just as the guest sees them. */
     unsigned long       guest_cr[5];
@@ -163,6 +170,8 @@ struct hvm_vcpu {
 
     struct nestedvcpu   nvcpu;
 
+    struct altp2mvcpu   avcpu;
+
     struct mtrr_state   mtrr;
     u64                 pat_cr;
 
xen/include/asm-x86/p2m.h +31 -1
index b49c09b..f38b452 100644
@@ -175,6 +175,7 @@ typedef unsigned int p2m_query_t;
 typedef enum {
     p2m_host,
     p2m_nested,
+    p2m_alternate,
 } p2m_class_t;
 
 /* Per-p2m-table state */
@@ -193,7 +194,7 @@ struct p2m_domain {
 
     struct domain     *domain;   /* back pointer to domain */
 
-    p2m_class_t       p2m_class; /* host/nested/? */
+    p2m_class_t       p2m_class; /* host/nested/alternate */
 
     /* Nested p2ms only: nested p2m base value that this p2m shadows.
      * This can be cleared to P2M_BASE_EADDR under the per-p2m lock but
@@ -219,6 +220,9 @@ struct p2m_domain {
      * host p2m's lock. */
     int                defer_nested_flush;
 
+    /* Alternate p2m: count of vcpu's currently using this p2m. */
+    atomic_t           active_vcpus;
+
     /* Pages used to construct the p2m */
     struct page_list_head pages;
 
@@ -317,6 +321,11 @@ static inline bool_t p2m_is_nestedp2m(const struct p2m_domain *p2m)
     return p2m->p2m_class == p2m_nested;
 }
 
+static inline bool_t p2m_is_altp2m(const struct p2m_domain *p2m)
+{
+    return p2m->p2m_class == p2m_alternate;
+}
+
 #define p2m_get_pagetable(p2m)  ((p2m)->phys_table)
 
 /**** p2m query accessors. They lock p2m_lock, and thus serialize
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn,
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory views
+ */
+
+/* get current alternate p2m table */
+static inline struct p2m_domain *p2m_get_altp2m(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
+
+    return (index == INVALID_ALTP2M) ? NULL : d->arch.altp2m_p2m[index];
+}
+
+/* Locate an alternate p2m by its EPTP */
+uint16_t p2m_find_altp2m_by_eptp(struct domain *d, uint64_t eptp);
+
+/* Switch alternate p2m for a single vcpu */
+bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx);
+
+/*
  * p2m type to IOMMU flags
  */
 static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt)
Signature · 1 line
1.9.1
#8 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/altp2m.c new file +25 -0
+void
+altp2m_vcpu_initialise(struct vcpu *v)
+{
+    if ( v != current )
+        vcpu_pause(v);
+
+    altp2m_vcpu_reset(v);
+    vcpu_altp2m(v).p2midx = 0;
+    atomic_inc(&p2m_get_altp2m(v)->active_vcpus);
+
+    altp2m_vcpu_update_eptp(v);
+
+    if ( v != current )
+        vcpu_unpause(v);
+}
+
+void
+altp2m_vcpu_destroy(struct vcpu *v)
+{
+    struct p2m_domain *p2m;
+
+    if ( v != current )
+        vcpu_pause(v);
+
+    if ( (p2m = p2m_get_altp2m(v)) )

xen/arch/x86/hvm/altp2m.c line 67

Quoted 10 lines
xen/arch/x86/hvm/altp2m.c new file +10 -0
+        atomic_dec(&p2m->active_vcpus);
+
+    altp2m_vcpu_reset(v);
+
+    altp2m_vcpu_update_eptp(v);
+    altp2m_vcpu_update_vmfunc_ve(v);
+
+    if ( v != current )
+        vcpu_unpause(v);
+}

There not being any caller of altp2m_vcpu_initialise() I can't judge about its pausing requirements, but for the destroy case I can't see what the pausing is good for. Considering its sole user it's also not really clear why the two update operations need to be done while destroying.

Quoted 12 lines
xen/arch/x86/hvm/hvm.c +8 -0
@@ -6498,6 +6500,25 @@ enum hvm_intblk nhvm_interrupt_blocked(struct vcpu *v)
     return hvm_funcs.nhvm_intr_blocked(v);
 }
 
+void altp2m_vcpu_update_eptp(struct vcpu *v)
+{
+    if ( hvm_funcs.altp2m_vcpu_update_eptp )
+        hvm_funcs.altp2m_vcpu_update_eptp(v);
+}
+
+void altp2m_vcpu_update_vmfunc_ve(struct vcpu *v)
+{

xen/arch/x86/hvm/hvm.c enum hvm_intblk nhvm_interrupt_blocked(struct vcpu *v) · line 6520

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    if ( hvm_funcs.altp2m_vcpu_update_vmfunc_ve )
+        hvm_funcs.altp2m_vcpu_update_vmfunc_ve(v);
+}
+
+bool_t altp2m_vcpu_emulate_ve(struct vcpu *v)
+{
+    if ( hvm_funcs.altp2m_vcpu_emulate_ve )
+        return hvm_funcs.altp2m_vcpu_emulate_ve(v);
+    return 0;
+}

The patch context here suggests that you're using pretty outdated a tree - nhvm_interrupt_blocked() went away a week ago. In line with the commit doing so, all of the above should become inline functions.

Quoted 13 lines
xen/arch/x86/mm/p2m.c +13 -0
+uint16_t p2m_find_altp2m_by_eptp(struct domain *d, uint64_t eptp)
+{
+    struct p2m_domain *p2m;
+    struct ept_data *ept;
+    uint16_t i;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+

xen/arch/x86/mm/p2m.c int unmap_mmio_regions(struct domain *d, · line 1992

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+        p2m = d->arch.altp2m_p2m[i];
+        ept = &p2m->ept;
+
+        if ( eptp == ept_get_eptp(ept) )
+            goto out;
+    }
+
+    i = INVALID_ALTP2M;
+
+out:

Labels should be indented by at least one space.

Pending the rename, the function may also live in the wrong file (the use of ept_get_eptp() suggests so even if the function itself got renamed).

Quoted 7 lines
xen/arch/x86/mm/p2m.c +7 -0
+bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx)
+{
+    struct domain *d = v->domain;
+    bool_t rc = 0;
+
+    if ( idx > MAX_ALTP2M )
+        return rc;

xen/arch/x86/mm/p2m.c int unmap_mmio_regions(struct domain *d, · line 2013

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+
+    altp2m_list_lock(d);
+
+    if ( d->arch.altp2m_eptp[idx] != INVALID_MFN )
+    {
+        if ( idx != vcpu_altp2m(v).p2midx )
+        {
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = idx;
+            atomic_inc(&p2m_get_altp2m(v)->active_vcpus);

Are the two results of p2m_get_altp2m(v) here guaranteed to be distinct? If they aren't, is it safe to decrement first (potentially dropping the count to zero)?

Quoted 7 lines
xen/include/asm-x86/p2m.h +3 -0
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn,
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory views
+ */
+

xen/include/asm-x86/p2m.h void nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn, · line 746

Quoted 10 lines
xen/include/asm-x86/p2m.h +10 -0
+/* get current alternate p2m table */
+static inline struct p2m_domain *p2m_get_altp2m(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
+
+    return (index == INVALID_ALTP2M) ? NULL : d->arch.altp2m_p2m[index];
+}

Looking at this again, I'm afraid I'd still prefer index < MAX_ALTP2M in the return statement (and the ASSERT() dropped): The ASSERT() does nothing in a debug=n build, and hence wouldn't shield us from possibly having to issue an XSA if somehow an access outside the array's bounds turned out possible.

I've also avoided to repeat any of the un-addressed points that I raised against earlier versions.

Jan

#9 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/altp2m.c new file +33 -0
+void
+altp2m_vcpu_initialise(struct vcpu *v)
+{
+    if ( v != current )
+        vcpu_pause(v);
+
+    altp2m_vcpu_reset(v);
+    vcpu_altp2m(v).p2midx = 0;
+    atomic_inc(&p2m_get_altp2m(v)->active_vcpus);
+
+    altp2m_vcpu_update_eptp(v);
+
+    if ( v != current )
+        vcpu_unpause(v);
+}
+
+void
+altp2m_vcpu_destroy(struct vcpu *v)
+{
+    struct p2m_domain *p2m;
+
+    if ( v != current )
+        vcpu_pause(v);
+
+    if ( (p2m = p2m_get_altp2m(v)) )
+        atomic_dec(&p2m->active_vcpus);
+
+    altp2m_vcpu_reset(v);
+
+    altp2m_vcpu_update_eptp(v);
+    altp2m_vcpu_update_vmfunc_ve(v);
+
+    if ( v != current )
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+        vcpu_unpause(v);
+}

There not being any caller of altp2m_vcpu_initialise() I can't judge about its pausing requirements, but for the destroy case I can't see what the pausing is good for. Considering its sole user it's also not really clear why the two update operations need to be done while destroying.

So looking at this after all the patches have been applied, it looks like initialise() and destroy() are called from the altp2m_set_domain_state(), which the guest uses to enable or disable the altp2m functionality. In that case, it seems like pausing is probably appropriate.

-George

#10 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 16:45, <[email protected]> wrote:
Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/altp2m.c new file +33 -0
+void
+altp2m_vcpu_initialise(struct vcpu *v)
+{
+    if ( v != current )
+        vcpu_pause(v);
+
+    altp2m_vcpu_reset(v);
+    vcpu_altp2m(v).p2midx = 0;
+    atomic_inc(&p2m_get_altp2m(v)->active_vcpus);
+
+    altp2m_vcpu_update_eptp(v);
+
+    if ( v != current )
+        vcpu_unpause(v);
+}
+
+void
+altp2m_vcpu_destroy(struct vcpu *v)
+{
+    struct p2m_domain *p2m;
+
+    if ( v != current )
+        vcpu_pause(v);
+
+    if ( (p2m = p2m_get_altp2m(v)) )
+        atomic_dec(&p2m->active_vcpus);
+
+    altp2m_vcpu_reset(v);
+
+    altp2m_vcpu_update_eptp(v);
+    altp2m_vcpu_update_vmfunc_ve(v);
+
+    if ( v != current )
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+        vcpu_unpause(v);
+}

There not being any caller of altp2m_vcpu_initialise() I can't judge about its pausing requirements, but for the destroy case I can't see what the pausing is good for. Considering its sole user it's also not really clear why the two update operations need to be done while destroying.

Quoted 5 lines

So looking at this after all the patches have been applied, it looks like initialise() and destroy() are called from the altp2m_set_domain_state(), which the guest uses to enable or disable the altp2m functionality. In that case, it seems like pausing is probably appropriate.

Right. Albeit I'd then question whether it wouldn't better be the caller to do the pausing (via domain_pause_except_self()), considering that the functions are being called in a fir_each_vcpu() loop.

Jan

#11 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

In reply to Jan Beulich
Quoted 35 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:13 AM

Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/altp2m.c new file +31 -0
+void
+altp2m_vcpu_initialise(struct vcpu *v) {
+    if ( v != current )
+        vcpu_pause(v);
+
+    altp2m_vcpu_reset(v);
+    vcpu_altp2m(v).p2midx = 0;
+    atomic_inc(&p2m_get_altp2m(v)->active_vcpus);
+
+    altp2m_vcpu_update_eptp(v);
+
+    if ( v != current )
+        vcpu_unpause(v);
+}
+
+void
+altp2m_vcpu_destroy(struct vcpu *v)
+{
+    struct p2m_domain *p2m;
+
+    if ( v != current )
+        vcpu_pause(v);
+
+    if ( (p2m = p2m_get_altp2m(v)) )
+        atomic_dec(&p2m->active_vcpus);
+
+    altp2m_vcpu_reset(v);
+
+    altp2m_vcpu_update_eptp(v);
+    altp2m_vcpu_update_vmfunc_ve(v);
+
Quoted 9 lines

xen/arch/x86/hvm/altp2m.c line 47

Quoted 3 lines
xen/arch/x86/hvm/altp2m.c new file +3 -0
+    if ( v != current )
+        vcpu_unpause(v);
+}

There not being any caller of altp2m_vcpu_initialise() I can't judge about its pausing requirements, but for the destroy case I can't see what the pausing is good for. Considering its sole user it's also not really clear why the two update operations need to be done while destroying.

initialise doesn't get called until the HVMOP implementation in a later patch.
initialise and destroy both operate on a running domain and modify vmcs state, so the vcpu has to be paused.

Quoted 15 lines

Review enum hvm_intblk

Quoted 1 line
(quoted diff) +0 -0
@@ -6498,6 +6500,25 @@ enum hvm_intblk

nhvm_interrupt_blocked(struct vcpu *v)

Quoted 13 lines
xen/arch/x86/hvm/hvm.c +11 -0
     return hvm_funcs.nhvm_intr_blocked(v);  }
 
+void altp2m_vcpu_update_eptp(struct vcpu *v) {
+    if ( hvm_funcs.altp2m_vcpu_update_eptp )
+        hvm_funcs.altp2m_vcpu_update_eptp(v);
+}
+
+void altp2m_vcpu_update_vmfunc_ve(struct vcpu *v) {
+    if ( hvm_funcs.altp2m_vcpu_update_vmfunc_ve )
+        hvm_funcs.altp2m_vcpu_update_vmfunc_ve(v);
+}
+
+bool_t altp2m_vcpu_emulate_ve(struct vcpu *v) {
Quoted 9 lines

xen/arch/x86/hvm/hvm.c enum hvm_intblk nhvm_interrupt_blocked(struct vcpu *v) · line 6520

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +4 -0
+    if ( hvm_funcs.altp2m_vcpu_emulate_ve )
+        return hvm_funcs.altp2m_vcpu_emulate_ve(v);
+    return 0;
+}

The patch context here suggests that you're using pretty outdated a tree - nhvm_interrupt_blocked() went away a week ago. In line with the commit doing so, all of the above should become inline functions.

We are rebasing on staging and testing now

Quoted 16 lines
Quoted 16 lines
xen/arch/x86/mm/p2m.c +16 -0
+uint16_t p2m_find_altp2m_by_eptp(struct domain *d, uint64_t eptp) {
+    struct p2m_domain *p2m;
+    struct ept_data *ept;
+    uint16_t i;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        ept = &p2m->ept;
+
+        if ( eptp == ept_get_eptp(ept) )
Quoted 8 lines

xen/arch/x86/mm/p2m.c int unmap_mmio_regions(struct domain *d, · line 1992

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+            goto out;
+    }
+
+    i = INVALID_ALTP2M;
+
+out:

Labels should be indented by at least one space.

Ok

Quoted 4 lines

Pending the rename, the function may also live in the wrong file (the use of ept_get_eptp() suggests so even if the function itself got renamed).

We will look at this one to see if its just a rename.

Quoted 12 lines
Quoted 12 lines
xen/arch/x86/mm/p2m.c +12 -0
+bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) {
+    struct domain *d = v->domain;
+    bool_t rc = 0;
+
+    if ( idx > MAX_ALTP2M )
+        return rc;
+
+    altp2m_list_lock(d);
+
+    if ( d->arch.altp2m_eptp[idx] != INVALID_MFN )
+    {
+        if ( idx != vcpu_altp2m(v).p2midx )
Quoted 9 lines

xen/arch/x86/mm/p2m.c int unmap_mmio_regions(struct domain *d, · line 2013

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+        {
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = idx;
+            atomic_inc(&p2m_get_altp2m(v)->active_vcpus);

Are the two results of p2m_get_altp2m(v) here guaranteed to be distinct? If they aren't, is it safe to decrement first (potentially dropping the count to zero)?

Yes on both.

Quoted 14 lines

Review void nestedp2m_write_p2m_entry(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct

p2m_domain *p2m, unsigned long gfn,

Quoted 12 lines
xen/include/asm-x86/p2m.h +9 -0
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory views
+ */
+
+/* get current alternate p2m table */ static inline struct p2m_domain
+*p2m_get_altp2m(struct vcpu *v) {
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
Quoted 9 lines

Review void nestedp2m_write_p2m_entry(struct · line 745

Quoted 3 lines
(quoted diff) +3 -0
+
+    return (index == INVALID_ALTP2M) ? NULL :
+d->arch.altp2m_p2m[index]; }

Looking at this again, I'm afraid I'd still prefer index < MAX_ALTP2M in the return statement (and the ASSERT() dropped): The ASSERT() does nothing in a debug=n build, and hence wouldn't shield us from possibly having to issue an XSA if somehow an access outside the array's bounds turned out possible.

the assert was breaking v5 anyway. BUG_ON (with the right check) is probably the right thing to do, as we do in the exit handling that checks for a VMFUNC having changed the index. So will make that change.

Quoted 3 lines

I've also avoided to repeat any of the un-addressed points that I raised against earlier versions.

I went back and looked at the earlier versions of the comments on this patch and afaict we have either addressed (accepted) those points or described why they shouldn't cause changes with reasoning . so if I missed something please let me know.

Ravi

Quoted 1 line

Jan

#12 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 28 lines
Quoted 18 lines
Quoted 1 line · On 16.07.15 at 10:57, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:13 AM

Review void nestedp2m_write_p2m_entry(struct

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct

p2m_domain *p2m, unsigned long gfn,

Quoted 12 lines
xen/include/asm-x86/p2m.h +9 -0
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory views
+ */
+
+/* get current alternate p2m table */ static inline struct p2m_domain
+*p2m_get_altp2m(struct vcpu *v) {
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
Quoted 9 lines

Review void nestedp2m_write_p2m_entry(struct · line 745

Quoted 3 lines
(quoted diff) +3 -0
+
+    return (index == INVALID_ALTP2M) ? NULL :
+d->arch.altp2m_p2m[index]; }

Looking at this again, I'm afraid I'd still prefer index < MAX_ALTP2M in the return statement (and the ASSERT() dropped): The ASSERT() does nothing in a debug=n build, and hence wouldn't shield us from possibly having to issue an XSA if somehow an access outside the array's bounds turned out possible.

Quoted 4 lines

the assert was breaking v5 anyway. BUG_ON (with the right check) is probably the right thing to do, as we do in the exit handling that checks for a VMFUNC having changed the index.
So will make that change.

But why use a BUG_ON() when you can deal with this more gracefully? Please try to avoid crashing the hypervisor when there are other ways to recover.

Quoted 9 lines
Quoted 1 line

I've also avoided to repeat any of the un-addressed points that I raised

against

Quoted 2 lines

earlier versions.

I went back and looked at the earlier versions of the comments on this patch and afaict we have either addressed (accepted) those points or described why they shouldn't cause changes with reasoning . so if I missed something please let me know.

Just one example of what wasn't done is the conversion of local variable, function return, and function parameter types from (bogus) uint8_t / uint16_t to unsigned int (iirc also in other patches).

Jan

#13 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 32 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:08 AM

Quoted 18 lines
Quoted 18 lines
Quoted 1 line · On 16.07.15 at 10:57, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:13 AM

Review void nestedp2m_write_p2m_entry(struct

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct

p2m_domain *p2m, unsigned long gfn,

Quoted 12 lines
xen/include/asm-x86/p2m.h +9 -0
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory views
+ */
+
+/* get current alternate p2m table */ static inline struct
+p2m_domain *p2m_get_altp2m(struct vcpu *v) {
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
Quoted 8 lines
Quoted 8 lines

Review void nestedp2m_write_p2m_entry(struct · line 745

Quoted 3 lines
(quoted diff) +3 -0
+
+    return (index == INVALID_ALTP2M) ? NULL :
+d->arch.altp2m_p2m[index]; }

Looking at this again, I'm afraid I'd still prefer index < MAX_ALTP2M in the return statement (and the ASSERT() dropped): The ASSERT() does nothing in a debug=n build, and hence wouldn't shield us from possibly having to issue an XSA if somehow an access outside the array's bounds

turned out possible.

Quoted 2 lines
Quoted 1 line
Quoted 8 lines
Quoted 4 lines

the assert was breaking v5 anyway. BUG_ON (with the right check) is probably the right thing to do, as we do in the exit handling that checks for a VMFUNC having changed the index.
So will make that change.

But why use a BUG_ON() when you can deal with this more gracefully? Please try to avoid crashing the hypervisor when there are other ways to recover.

So in this case there isnt a graceful fallback; this case can happen only if there is a bug in the hypervisor - which should be reported via the BUG_ON.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

I've also avoided to repeat any of the un-addressed points that I raised

against

Quoted 2 lines

earlier versions.

Quoted 8 lines
Quoted 4 lines

I went back and looked at the earlier versions of the comments on this patch and afaict we have either addressed (accepted) those points or described why they shouldn't cause changes with reasoning . so if I missed something please let me know.

Just one example of what wasn't done is the conversion of local variable, function return, and function parameter types from
(bogus) uint8_t / uint16_t to unsigned int (iirc also in other patches).

Working through these as best as can (treating it as Category 4)

Thanks,
Ravi

Quoted 2 lines

Jan

#14 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 38 lines
Quoted 38 lines
Quoted 1 line · On 18.07.15 at 00:36, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:08 AM

Quoted 18 lines
Quoted 18 lines
Quoted 1 line · On 16.07.15 at 10:57, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:13 AM

Review void nestedp2m_write_p2m_entry(struct

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct

p2m_domain *p2m, unsigned long gfn,

Quoted 12 lines
xen/include/asm-x86/p2m.h +9 -0
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory views
+ */
+
+/* get current alternate p2m table */ static inline struct
+p2m_domain *p2m_get_altp2m(struct vcpu *v) {
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
Quoted 8 lines
Quoted 8 lines

Review void nestedp2m_write_p2m_entry(struct · line 745

Quoted 3 lines
(quoted diff) +3 -0
+
+    return (index == INVALID_ALTP2M) ? NULL :
+d->arch.altp2m_p2m[index]; }

Looking at this again, I'm afraid I'd still prefer index < MAX_ALTP2M in the return statement (and the ASSERT() dropped): The ASSERT() does nothing in a debug=n build, and hence wouldn't shield us from possibly having to issue an XSA if somehow an access outside the array's bounds

turned out possible.

Quoted 6 lines
Quoted 1 line

the assert was breaking v5 anyway. BUG_ON (with the right check) is probably the right thing to do, as we do in the exit handling that checks for a VMFUNC having changed the index.
So will make that change.

Quoted 6 lines
Quoted 3 lines

But why use a BUG_ON() when you can deal with this more gracefully? Please try to avoid crashing the hypervisor when there are other ways to recover.

So in this case there isnt a graceful fallback; this case can happen only if there is a bug in the hypervisor - which should be reported via the BUG_ON.

Generally (as an example), if a hypervisor bug can be confined to a guest, killing just the guest instead of the hypervisor would still be preferred (allowing the admin to attempt to gracefully shut down other guests before updating/restarting).

Jan

#15 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 49 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Sunday, July 19, 2015 11:20 PM

Quoted 34 lines
Quoted 34 lines
Quoted 1 line · On 18.07.15 at 00:36, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:08 AM

Quoted 19 lines
Quoted 19 lines
Quoted 1 line · On 16.07.15 at 10:57, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:13 AM

Review void nestedp2m_write_p2m_entry(struct

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -722,6 +731,27 @@ void nestedp2m_write_p2m_entry(struct

p2m_domain *p2m, unsigned long gfn,

Quoted 13 lines
xen/include/asm-x86/p2m.h +10 -0
     l1_pgentry_t *p, l1_pgentry_t new, unsigned int level);
 
 /*
+ * Alternate p2m: shadow p2m tables used for alternate memory
+ views */
+
+/* get current alternate p2m table */ static inline struct
+p2m_domain *p2m_get_altp2m(struct vcpu *v) {
+    struct domain *d = v->domain;
+    uint16_t index = vcpu_altp2m(v).p2midx;
+
+    ASSERT(index < MAX_ALTP2M);
+
Quoted 8 lines
Quoted 8 lines

Review void nestedp2m_write_p2m_entry(struct · line 745

Quoted 2 lines
(quoted diff) +2 -0
+    return (index == INVALID_ALTP2M) ? NULL :
+d->arch.altp2m_p2m[index]; }

Looking at this again, I'm afraid I'd still prefer index < MAX_ALTP2M in the return statement (and the ASSERT() dropped): The ASSERT() does nothing in a debug=n build, and hence wouldn't shield us from possibly having to issue an XSA if somehow an access outside the array's bounds

turned out possible.

Quoted 2 lines
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

the assert was breaking v5 anyway. BUG_ON (with the right check) is probably the right thing to do, as we do in the exit handling that checks for a VMFUNC having changed the index.
So will make that change.

But why use a BUG_ON() when you can deal with this more gracefully? Please try to avoid crashing the hypervisor when there are other ways to

recover.

Quoted 4 lines
Quoted 1 line

So in this case there isnt a graceful fallback; this case can happen only if there is a bug in the hypervisor - which should be reported via the

Quoted 8 lines

BUG_ON.

Generally (as an example), if a hypervisor bug can be confined to a guest, killing just the guest instead of the hypervisor would still be preferred (allowing the admin to attempt to gracefully shut down other guests before updating/restarting).

Jan

I agree with that principle, and that's what I was looking to do in the last iteration, but in this sort of error condition there is no telling what else could have gone wrong on the hypervisor side to cause this, so it seems the crash treatment seems suitable.

Ravi

#16 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

In reply to Ed White
Quoted 10 lines · Ed White

Add the basic data structures needed to support alternate p2m's and the functions to initialise them and tear them down.

Although Intel hardware can handle 512 EPTP's per hardware thread concurrently, only 10 per domain are supported in this patch for performance reasons.

The iterator in hap_enable() does need to handle 512, so that is now uint16_t.

Quoted 8 lines

This change also splits the p2m lock into one lock type for altp2m's and another type for all other p2m's. The purpose of this is to place the altp2m list lock between the types, so the list lock can be acquired whilst holding the host p2m lock.

With the number of major changes you made here, you definitely should have dropped this reviewed-by.

Quoted 49 lines
---
 xen/arch/x86/hvm/Makefile        |   1 +
 xen/arch/x86/hvm/altp2m.c        |  77 +++++++++++++++++++++++++++++
 xen/arch/x86/hvm/hvm.c           |  21 ++++++++
 xen/arch/x86/mm/hap/hap.c        |  38 ++++++++++++++-
 xen/arch/x86/mm/mm-locks.h       |  46 +++++++++++++++++-
 xen/arch/x86/mm/p2m.c            | 102 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h     |  10 ++++
 xen/include/asm-x86/hvm/altp2m.h |  38 +++++++++++++++
 xen/include/asm-x86/hvm/hvm.h    |  14 ++++++
 xen/include/asm-x86/hvm/vcpu.h   |   9 ++++
 xen/include/asm-x86/p2m.h        |  32 +++++++++++-
 11 files changed, 384 insertions(+), 4 deletions(-)
 create mode 100644 xen/arch/x86/hvm/altp2m.c
 create mode 100644 xen/include/asm-x86/hvm/altp2m.h
xen/arch/x86/hvm/Makefile +1 -0
index 69af47f..eb1a37b 100644
@@ -1,6 +1,7 @@
 subdir-y += svm
 subdir-y += vmx
 
+obj-y += altp2m.o
 obj-y += asid.o
 obj-y += emulate.o
 obj-y += event.o
xen/arch/x86/hvm/altp2m.c new file +15 -0
new file mode 100644
index 0000000..a10f347
@@ -0,0 +1,77 @@
+/*
+ * Alternate p2m HVM
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple

xen/arch/x86/hvm/altp2m.c line 25

Quoted 10 lines
xen/arch/x86/hvm/altp2m.c new file +10 -0
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <asm/hvm/support.h>
+#include <asm/hvm/hvm.h>
+#include <asm/p2m.h>
+#include <asm/hvm/altp2m.h>
+
+void
+altp2m_vcpu_reset(struct vcpu *v)

OK, so it looks like at the end of this patch series:
* altp2m_vcpu_reset() isn't called outside this file
* altp2m_vcpu_initialise() is only called from hvm.c when the guest enables the altp2m functionality
* altp2m_vcpu_destroy() is called when the guest disables altp2m funcitonality, or when the vcpu is destroyed

Looking at the "vcpu_destroy" case, it's hard to tell exactly how much on that path is actually useful; but it looks like the only thing that's critical is decreasing the active_vcpu count of the p2m that's being used.

Also, it looks like these functions don't do anything specifically with the HVM side of things.

So on the whole, it seems like these would better go along with the other altp2m functions inside p2m.c.

Thoughts?

-George

#17 lore raw

Re: [PATCH v5 05/15] x86/altp2m: basic data structures and support routines.

Quoted 104 lines

From: George Dunlap [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 8:57 AM

Quoted 10 lines · Ed White

Add the basic data structures needed to support alternate p2m's and the functions to initialise them and tear them down.

Although Intel hardware can handle 512 EPTP's per hardware thread concurrently, only 10 per domain are supported in this patch for performance reasons.

The iterator in hap_enable() does need to handle 512, so that is now uint16_t.

Quoted 8 lines

This change also splits the p2m lock into one lock type for altp2m's and another type for all other p2m's. The purpose of this is to place the altp2m list lock between the types, so the list lock can be acquired whilst holding the host p2m lock.

With the number of major changes you made here, you definitely should have dropped this reviewed-by.

Quoted 7 lines
---
 xen/arch/x86/hvm/Makefile        |   1 +
 xen/arch/x86/hvm/altp2m.c        |  77 +++++++++++++++++++++++++++++
 xen/arch/x86/hvm/hvm.c           |  21 ++++++++
 xen/arch/x86/mm/hap/hap.c        |  38 ++++++++++++++-
 xen/arch/x86/mm/mm-locks.h       |  46 +++++++++++++++++-
 xen/arch/x86/mm/p2m.c            | 102

+++++++++++++++++++++++++++++++++++++++

Quoted 28 lines
 xen/include/asm-x86/domain.h     |  10 ++++
 xen/include/asm-x86/hvm/altp2m.h |  38 +++++++++++++++
 xen/include/asm-x86/hvm/hvm.h    |  14 ++++++
 xen/include/asm-x86/hvm/vcpu.h   |   9 ++++
 xen/include/asm-x86/p2m.h        |  32 +++++++++++-
 11 files changed, 384 insertions(+), 4 deletions(-)  create mode

100644 xen/arch/x86/hvm/altp2m.c create mode 100644 xen/include/asm-x86/hvm/altp2m.h

xen/arch/x86/hvm/Makefile +1 -0
index 69af47f..eb1a37b 100644
@@ -1,6 +1,7 @@
 subdir-y += svm
 subdir-y += vmx
 
+obj-y += altp2m.o
 obj-y += asid.o
 obj-y += emulate.o
 obj-y += event.o
xen/arch/x86/hvm/altp2m.c new renamed from xen/arch/x86/hvm/altp2m.c +0 -0

file mode 100644 index 0000000..a10f347

xen/arch/x86/hvm/altp2m.c new file +2 -0
@@ -0,0 +1,77 @@
+/*
+ * Alternate p2m HVM

xen/arch/x86/hvm/altp2m.c line 12

Quoted 10 lines
xen/arch/x86/hvm/altp2m.c new file +10 -0
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but
+WITHOUT
+ * ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY

Quoted 9 lines
xen/arch/x86/hvm/altp2m.c new file +9 -0
+or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
+License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+59 Temple

xen/arch/x86/hvm/altp2m.c line 25

Quoted 10 lines
xen/arch/x86/hvm/altp2m.c new file +10 -0
+ * Place - Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <asm/hvm/support.h>
+#include <asm/hvm/hvm.h>
+#include <asm/p2m.h>
+#include <asm/hvm/altp2m.h>
+
+void
+altp2m_vcpu_reset(struct vcpu *v)

OK, so it looks like at the end of this patch series:
* altp2m_vcpu_reset() isn't called outside this file
* altp2m_vcpu_initialise() is only called from hvm.c when the guest enables the altp2m functionality
* altp2m_vcpu_destroy() is called when the guest disables altp2m funcitonality, or when the vcpu is destroyed

Looking at the "vcpu_destroy" case, it's hard to tell exactly how much on that path is actually useful; but it looks like the only thing that's critical is decreasing the active_vcpu count of the p2m that's being used.

Quoted 7 lines

Also, it looks like these functions don't do anything specifically with the HVM side of things.

So on the whole, it seems like these would better go along with the other altp2m functions inside p2m.c.

Thoughts?

George, apologies on this one - I completely missed this email from you -

We could move these functions into p2m.c, except destroy, the VMCS updates are critical. We will try to get this into what will be our final rev (at least for 4.6 candidate). Again sorry for the snafu on this email.

Ravi

Quoted 2 lines

-George

author
#18 lore raw

PATCH v5 6/15 VMX/altp2m: add code to support EPTP switching and #VE.

In reply to Ed White

Implement and hook up the code to enable VMX support of VMFUNC and #VE.

VMFUNC leaf 0 (EPTP switching) emulation is added in a later patch.

---
 xen/arch/x86/hvm/vmx/vmx.c | 138 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 138 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +138 -0
index 07527dd..38dba6b 100644
@@ -56,6 +56,7 @@
 #include <asm/debugger.h>
 #include <asm/apic.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/altp2m.h>
 #include <asm/event.h>
 #include <asm/monitor.h>
 #include <public/arch-x86/cpuid.h>
@@ -1763,6 +1764,104 @@ static void vmx_enable_msr_exit_interception(struct domain *d)
                                          MSR_TYPE_W);
 }
 
+static void vmx_vcpu_update_eptp(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m = NULL;
+    struct ept_data *ept;
+
+    if ( altp2m_active(d) )
+        p2m = p2m_get_altp2m(v);
+    if ( !p2m )
+        p2m = p2m_get_hostp2m(d);
+
+    ept = &p2m->ept;
+    ept->asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+
+    vmx_vmcs_enter(v);
+
+    __vmwrite(EPT_POINTER, ept_get_eptp(ept));
+
+    if ( v->arch.hvm_vmx.secondary_exec_control &
+        SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+        __vmwrite(EPTP_INDEX, vcpu_altp2m(v).p2midx);
+
+    vmx_vmcs_exit(v);
+}
+
+static void vmx_vcpu_update_vmfunc_ve(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    u32 mask = SECONDARY_EXEC_ENABLE_VM_FUNCTIONS;
+
+    if ( !cpu_has_vmx_vmfunc )
+        return;
+
+    if ( cpu_has_vmx_virt_exceptions )
+        mask |= SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;
+
+    vmx_vmcs_enter(v);
+
+    if ( !d->is_dying && altp2m_active(d) )
+    {
+        v->arch.hvm_vmx.secondary_exec_control |= mask;
+        __vmwrite(VM_FUNCTION_CONTROL, VMX_VMFUNC_EPTP_SWITCHING);
+        __vmwrite(EPTP_LIST_ADDR, virt_to_maddr(d->arch.altp2m_eptp));
+
+        if ( cpu_has_vmx_virt_exceptions )
+        {
+            p2m_type_t t;
+            mfn_t mfn;
+
+            mfn = get_gfn_query_unlocked(d, gfn_x(vcpu_altp2m(v).veinfo_gfn), &t);
+
+            if ( mfn_x(mfn) != INVALID_MFN )
+                __vmwrite(VIRT_EXCEPTION_INFO, mfn_x(mfn) << PAGE_SHIFT);
+            else
+                mask &= ~SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;
+        }
+    }
+    else
+        v->arch.hvm_vmx.secondary_exec_control &= ~mask;
+
+    __vmwrite(SECONDARY_VM_EXEC_CONTROL,
+        v->arch.hvm_vmx.secondary_exec_control);
+
+    vmx_vmcs_exit(v);
+}
+
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v)
+{
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) != INVALID_GFN ?
+        hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn), 0) : NULL;
+
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;
+    veinfo->eptp_index = vcpu_altp2m(v).p2midx;
+
+    vmx_vmcs_enter(v);
+    __vmread(EXIT_QUALIFICATION, &veinfo->exit_qualification);
+    __vmread(GUEST_LINEAR_ADDRESS, &veinfo->gla);
+    __vmread(GUEST_PHYSICAL_ADDRESS, &veinfo->gpa);
+    vmx_vmcs_exit(v);
+
+    hvm_inject_hw_exception(TRAP_virtualisation,
+                            HVM_DELIVER_NO_ERROR_CODE);
+
+out:
+    hvm_unmap_guest_frame(veinfo, 0);
+    return rc;
+}
+
 static struct hvm_function_table __initdata vmx_function_table = {
     .name                 = "VMX",
     .cpu_up_prepare       = vmx_cpu_up_prepare,
@@ -1822,6 +1921,9 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .nhvm_hap_walk_L1_p2m = nvmx_hap_walk_L1_p2m,
     .hypervisor_cpuid_leaf = vmx_hypervisor_cpuid_leaf,
     .enable_msr_exit_interception = vmx_enable_msr_exit_interception,
+    .altp2m_vcpu_update_eptp = vmx_vcpu_update_eptp,
+    .altp2m_vcpu_update_vmfunc_ve = vmx_vcpu_update_vmfunc_ve,
+    .altp2m_vcpu_emulate_ve = vmx_vcpu_emulate_ve,
 };
 
 const struct hvm_function_table * __init start_vmx(void)
@@ -2744,6 +2846,42 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     /* Now enable interrupts so it's safe to take locks. */
     local_irq_enable();
 
+    /*
+     * If the guest has the ability to switch EPTP without an exit,
+     * figure out whether it has done so and update the altp2m data.
+     */
+    if ( altp2m_active(v->domain) &&
+        (v->arch.hvm_vmx.secondary_exec_control &
+        SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
+            unsigned long eptp;
+
+            __vmread(EPT_POINTER, &eptp);
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate p2m list\n");
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;
+            atomic_inc(&p2m_get_altp2m(v)->active_vcpus);
+        }
+    }
+
     /* XXX: This looks ugly, but we need a mechanism to ensure
      * any pending vmresume has really happened
      */
Signature · 1 line
1.9.1
#19 lore raw

Re: [PATCH v5 06/15] VMX/altp2m: add code to support EPTP switching and #VE.

Quoted 76 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:

Implement and hook up the code to enable VMX support of VMFUNC and #VE.

VMFUNC leaf 0 (EPTP switching) emulation is added in a later patch.

---
 xen/arch/x86/hvm/vmx/vmx.c | 138 
+++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 138 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +46 -0
index 07527dd..38dba6b 100644
@@ -56,6 +56,7 @@
 #include <asm/debugger.h>
 #include <asm/apic.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/altp2m.h>
 #include <asm/event.h>
 #include <asm/monitor.h>
 #include <public/arch-x86/cpuid.h>
@@ -1763,6 +1764,104 @@ static void vmx_enable_msr_exit_interception(struct 
 domain *d)
                                          MSR_TYPE_W);
 }
 
+static void vmx_vcpu_update_eptp(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m = NULL;
+    struct ept_data *ept;
+
+    if ( altp2m_active(d) )
+        p2m = p2m_get_altp2m(v);
+    if ( !p2m )
+        p2m = p2m_get_hostp2m(d);
+
+    ept = &p2m->ept;
+    ept->asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+
+    vmx_vmcs_enter(v);
+
+    __vmwrite(EPT_POINTER, ept_get_eptp(ept));
+
+    if ( v->arch.hvm_vmx.secondary_exec_control &
+        SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+        __vmwrite(EPTP_INDEX, vcpu_altp2m(v).p2midx);
+
+    vmx_vmcs_exit(v);
+}
+
+static void vmx_vcpu_update_vmfunc_ve(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    u32 mask = SECONDARY_EXEC_ENABLE_VM_FUNCTIONS;
+
+    if ( !cpu_has_vmx_vmfunc )
+        return;
+
+    if ( cpu_has_vmx_virt_exceptions )
+        mask |= SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;
+
+    vmx_vmcs_enter(v);
+
+    if ( !d->is_dying && altp2m_active(d) )
+    {
+        v->arch.hvm_vmx.secondary_exec_control |= mask;
+        __vmwrite(VM_FUNCTION_CONTROL, VMX_VMFUNC_EPTP_SWITCHING);
+        __vmwrite(EPTP_LIST_ADDR, virt_to_maddr(d->arch.altp2m_eptp));
+
+        if ( cpu_has_vmx_virt_exceptions )

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct · line 1822

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+        {
+            p2m_type_t t;
+            mfn_t mfn;
+
+            mfn = get_gfn_query_unlocked(d, gfn_x(vcpu_altp2m(v).veinfo_gfn), &t);
+
+            if ( mfn_x(mfn) != INVALID_MFN )
+                __vmwrite(VIRT_EXCEPTION_INFO, mfn_x(mfn) << PAGE_SHIFT);
+            else
+                mask &= ~SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;

Considering the rest of the function, this is dead code.

Quoted 17 lines
xen/arch/x86/hvm/vmx/vmx.c +17 -0
+        }
+    }
+    else
+        v->arch.hvm_vmx.secondary_exec_control &= ~mask;
+
+    __vmwrite(SECONDARY_VM_EXEC_CONTROL,
+        v->arch.hvm_vmx.secondary_exec_control);
+
+    vmx_vmcs_exit(v);
+}
+
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v)
+{
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) != INVALID_GFN ?
+        hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn), 0) : NULL;
+

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1848

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;

Isn't semaphore a 32-bit quantity?

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1860

Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +12 -0
+    veinfo->eptp_index = vcpu_altp2m(v).p2midx;
+
+    vmx_vmcs_enter(v);
+    __vmread(EXIT_QUALIFICATION, &veinfo->exit_qualification);
+    __vmread(GUEST_LINEAR_ADDRESS, &veinfo->gla);
+    __vmread(GUEST_PHYSICAL_ADDRESS, &veinfo->gpa);
+    vmx_vmcs_exit(v);
+
+    hvm_inject_hw_exception(TRAP_virtualisation,
+                            HVM_DELIVER_NO_ERROR_CODE);
+
+out:

Un-indented label again.

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2855

Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +7 -0
@@ -2744,6 +2846,42 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
     /* Now enable interrupts so it's safe to take locks. */
     local_irq_enable();
 
+    /*
+     * If the guest has the ability to switch EPTP without an exit,
+     * figure out whether it has done so and update the altp2m data.
+     */
+    if ( altp2m_active(v->domain) &&
+        (v->arch.hvm_vmx.secondary_exec_control &
+        SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )

Indentation.

Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +11 -0
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
+            unsigned long eptp;
+
+            __vmread(EPT_POINTER, &eptp);

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2876

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate p2m list\n");
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )

Is this cast really necessary?

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2880

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;

This one surely isn't (or else the field type is wrong).

Jan

#20 lore raw

Re: [PATCH v5 06/15] VMX/altp2m: add code to support EPTP switching and #VE.

Quoted 85 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:57 AM

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:

Implement and hook up the code to enable VMX support of VMFUNC and

#VE.

Quoted 59 lines

VMFUNC leaf 0 (EPTP switching) emulation is added in a later patch.

---
 xen/arch/x86/hvm/vmx/vmx.c | 138
+++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 138 insertions(+)
xen/arch/x86/hvm/vmx/vmx.c +1 -0
index 07527dd..38dba6b 100644
@@ -56,6 +56,7 @@
 #include <asm/debugger.h>
 #include <asm/apic.h>
 #include <asm/hvm/nestedhvm.h>
+#include <asm/hvm/altp2m.h>
 #include <asm/event.h>
 #include <asm/monitor.h>
 #include <public/arch-x86/cpuid.h>
@@ -1763,6 +1764,104 @@ static void

vmx_enable_msr_exit_interception(struct
domain *d)

                                          MSR_TYPE_W);  }
xen/arch/x86/hvm/vmx/vmx.c +30 -0
+static void vmx_vcpu_update_eptp(struct vcpu *v) {
+    struct domain *d = v->domain;
+    struct p2m_domain *p2m = NULL;
+    struct ept_data *ept;
+
+    if ( altp2m_active(d) )
+        p2m = p2m_get_altp2m(v);
+    if ( !p2m )
+        p2m = p2m_get_hostp2m(d);
+
+    ept = &p2m->ept;
+    ept->asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+
+    vmx_vmcs_enter(v);
+
+    __vmwrite(EPT_POINTER, ept_get_eptp(ept));
+
+    if ( v->arch.hvm_vmx.secondary_exec_control &
+        SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+        __vmwrite(EPTP_INDEX, vcpu_altp2m(v).p2midx);
+
+    vmx_vmcs_exit(v);
+}
+
+static void vmx_vcpu_update_vmfunc_ve(struct vcpu *v) {
+    struct domain *d = v->domain;
+    u32 mask = SECONDARY_EXEC_ENABLE_VM_FUNCTIONS;
+
+    if ( !cpu_has_vmx_vmfunc )
+        return;

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d)

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+
+    if ( cpu_has_vmx_virt_exceptions )
+        mask |= SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;
+
+    vmx_vmcs_enter(v);
+
+    if ( !d->is_dying && altp2m_active(d) )
+    {
+        v->arch.hvm_vmx.secondary_exec_control |= mask;
+        __vmwrite(VM_FUNCTION_CONTROL,

VMX_VMFUNC_EPTP_SWITCHING);

Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +9 -0
+        __vmwrite(EPTP_LIST_ADDR,
+ virt_to_maddr(d->arch.altp2m_eptp));
+
+        if ( cpu_has_vmx_virt_exceptions )
+        {
+            p2m_type_t t;
+            mfn_t mfn;
+
+            mfn = get_gfn_query_unlocked(d,
Quoted 9 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1821

Quoted 6 lines

+ gfn_x(vcpu_altp2m(v).veinfo_gfn), &t);

xen/arch/x86/hvm/vmx/vmx.c +5 -0
+
+            if ( mfn_x(mfn) != INVALID_MFN )
+                __vmwrite(VIRT_EXCEPTION_INFO, mfn_x(mfn) << PAGE_SHIFT);
+            else
+                mask &= ~SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS;

Considering the rest of the function, this is dead code.

Right, should be v->arch.hvm_vmx.secondary_exec_control &=...

Quoted 15 lines
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        }
+    }
+    else
+        v->arch.hvm_vmx.secondary_exec_control &= ~mask;

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d)

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10 -0
+
+    __vmwrite(SECONDARY_VM_EXEC_CONTROL,
+        v->arch.hvm_vmx.secondary_exec_control);
+
+    vmx_vmcs_exit(v);
+}
+
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v) {
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) != INVALID_GFN

?

Quoted 16 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1848

Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +13 -0
+        hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn), 0) :
+NULL;
+
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;

Isn't semaphore a 32-bit quantity?

Yes.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+    veinfo->eptp_index = vcpu_altp2m(v).p2midx;
+
+    vmx_vmcs_enter(v);
+    __vmread(EXIT_QUALIFICATION, &veinfo->exit_qualification);
+    __vmread(GUEST_LINEAR_ADDRESS, &veinfo->gla);
+    __vmread(GUEST_PHYSICAL_ADDRESS, &veinfo->gpa);
Quoted 9 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1860

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+    vmx_vmcs_exit(v);
+
+    hvm_inject_hw_exception(TRAP_virtualisation,
+                            HVM_DELIVER_NO_ERROR_CODE);
+
+out:

Un-indented label again.

Got it.

Quoted 13 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs

Quoted 1 line
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -2744,6 +2846,42 @@ void vmx_vmexit_handler(struct cpu_user_regs

*regs)

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2855

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +7 -0
     /* Now enable interrupts so it's safe to take locks. */
     local_irq_enable();
 
+    /*
+     * If the guest has the ability to switch EPTP without an exit,
+     * figure out whether it has done so and update the altp2m data.
+     */
+    if ( altp2m_active(v->domain) &&
+        (v->arch.hvm_vmx.secondary_exec_control &
+        SECONDARY_EXEC_ENABLE_VM_FUNCTIONS) )
Quoted 2 lines

Indentation.

Yep.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/hvm/vmx/vmx.c +15 -0
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
+            unsigned long eptp;
+
+            __vmread(EPT_POINTER, &eptp);
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
Quoted 9 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2876

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate p2m list\n");
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )

Is this cast really necessary?

Yes - The index is 16-bits, this reflects how the field is specified in the vmcs also.

Quoted 7 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2880

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;

This one surely isn't (or else the field type is wrong).

Again required. idx can't be uint16_t because __vmread() requires unsigned long*, but the index is 16 bits.

Thanks,
Ravi

Quoted 1 line

Jan

#21 lore raw

Re: [PATCH v5 06/15] VMX/altp2m: add code to support EPTP switching and #VE.

Quoted 24 lines
Quoted 8 lines
Quoted 1 line · On 16.07.15 at 11:20, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:57 AM

Review

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +3 -0
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v) {
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) != INVALID_GFN

?

Quoted 15 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1848

Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +13 -0
+        hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn), 0) :
+NULL;
+
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;

Isn't semaphore a 32-bit quantity?

Quoted 1 line

Yes.

I.e. the l suffix can and should be dropped.

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/hvm/vmx/vmx.c +15 -0
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
+            unsigned long eptp;
+
+            __vmread(EPT_POINTER, &eptp);
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2876

Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate p2m list\n");
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )

Is this cast really necessary?

Quoted 2 lines

Yes - The index is 16-bits, this reflects how the field is specified in the vmcs also.

While "yes" answers the question, the explanation you give suggests that the answer may be wrong: Can idx indeed have bits set beyond bit 15? Because if it can't, the cast is pointless.

Quoted 9 lines
Quoted 6 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2880

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;

This one surely isn't (or else the field type is wrong).

Again required. idx can't be uint16_t because __vmread() requires unsigned long*, but the index is 16 bits.

But it's a 16-bit VMCS field that you read it from, and hence the upper 48 bits are necessarily zero.

Just to re-iterate: Casts are necessary in certain places, yes, but I see them used pointlessly or even wrongly more often than not.

Jan

#22 lore raw

Re: [PATCH v5 06/15] VMX/altp2m: add code to support EPTP switching and #VE.

Quoted 28 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:39 AM

Quoted 25 lines
Quoted 24 lines
Quoted 1 line · On 16.07.15 at 11:20, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:57 AM

Review

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +4 -0
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v) {
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) !=
+INVALID_GFN

?

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1848

Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +13 -0
+        hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn), 0) :
+NULL;
+
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;

Isn't semaphore a 32-bit quantity?

Quoted 4 lines
Quoted 1 line

Yes.

I.e. the l suffix can and should be dropped.

Ok.

Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs)

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
+            unsigned long eptp;
+
+            __vmread(EPT_POINTER, &eptp);
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate p2m

list\n");

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5 -0
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Is this cast really necessary?

Yes - The index is 16-bits, this reflects how the field is specified in the vmcs also.

While "yes" answers the question, the explanation you give suggests that the answer may be wrong: Can idx indeed have bits set beyond bit 15? Because if it can't, the cast is pointless.

We were just trying to ensure we matched the hardware behavior (I think there was a message George had posted earlier for SVE that asked for that). Since hardware considers only a 16 bit field we were doing the same.

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2880

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;

This one surely isn't (or else the field type is wrong).

Again required. idx can't be uint16_t because __vmread() requires unsigned long*, but the index is 16 bits.

Quoted 7 lines

But it's a 16-bit VMCS field that you read it from, and hence the upper 48 bits are necessarily zero.

Just to re-iterate: Casts are necessary in certain places, yes, but I see them used pointlessly or even wrongly more often than not.

Jan

Same approach as above - emulating hardware exactly. Should we add a comment?

Thanks,
Ravi

#23 lore raw

Re: [PATCH v5 06/15] VMX/altp2m: add code to support EPTP switching and #VE.

Quoted 84 lines
Quoted 29 lines
Quoted 1 line · On 17.07.15 at 23:08, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:39 AM

Quoted 25 lines
Quoted 24 lines
Quoted 1 line · On 16.07.15 at 11:20, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:57 AM

Review

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +4 -0
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v) {
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) !=
+INVALID_GFN

?

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1848

Quoted 13 lines
xen/arch/x86/hvm/vmx/vmx.c +13 -0
+        hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn), 0) :
+NULL;
+
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;

Isn't semaphore a 32-bit quantity?

Quoted 4 lines
Quoted 1 line

Yes.

I.e. the l suffix can and should be dropped.

Ok.

Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs)

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
+            unsigned long eptp;
+
+            __vmread(EPT_POINTER, &eptp);
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate p2m

list\n");

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5 -0
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Is this cast really necessary?

Yes - The index is 16-bits, this reflects how the field is specified in the vmcs also.

While "yes" answers the question, the explanation you give suggests that the answer may be wrong: Can idx indeed have bits set beyond bit 15? Because if it can't, the cast is pointless.

We were just trying to ensure we matched the hardware behavior (I think there was a message George had posted earlier for SVE that asked for that). Since hardware considers only a 16 bit field we were doing the same.

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2880

Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;

This one surely isn't (or else the field type is wrong).

Again required. idx can't be uint16_t because __vmread() requires unsigned long*, but the index is 16 bits.

But it's a 16-bit VMCS field that you read it from, and hence the upper 48

Quoted 8 lines

bits

Quoted 4 lines

are necessarily zero.

Just to re-iterate: Casts are necessary in certain places, yes, but I see them used pointlessly or even wrongly more often than not.

Same approach as above - emulating hardware exactly. Should we add a comment?

No, drop the casts.

Jan

#24 lore raw

Re: [PATCH v5 06/15] VMX/altp2m: add code to support EPTP switching and #VE.

Quoted 97 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Sunday, July 19, 2015 11:22 PM

Quoted 7 lines
Quoted 7 lines
Quoted 1 line · On 17.07.15 at 23:08, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:39 AM

Quoted 3 lines
Quoted 3 lines
Quoted 1 line · On 16.07.15 at 11:20, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 6:57 AM

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +4 -0
+static bool_t vmx_vcpu_emulate_ve(struct vcpu *v) {
+    bool_t rc = 0;
+    ve_info_t *veinfo = gfn_x(vcpu_altp2m(v).veinfo_gfn) !=
+INVALID_GFN

?

Quoted 1 line

+ hvm_map_guest_frame_rw(gfn_x(vcpu_altp2m(v).veinfo_gfn),

0) :

Quoted 51 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

xen/arch/x86/hvm/vmx/vmx.c static void vmx_enable_msr_exit_interception(struct domain *d) · line 1848

Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +12 -0
+NULL;
+
+    if ( !veinfo )
+        return 0;
+
+    if ( veinfo->semaphore != 0 )
+        goto out;
+
+    rc = 1;
+
+    veinfo->exit_reason = EXIT_REASON_EPT_VIOLATION;
+    veinfo->semaphore = ~0l;

Isn't semaphore a 32-bit quantity?

Quoted 4 lines
Quoted 1 line

Yes.

I.e. the l suffix can and should be dropped.

Ok.

Quoted 29 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +9 -0
+    {
+        unsigned long idx;
+
+        if ( v->arch.hvm_vmx.secondary_exec_control &
+            SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS )
+            __vmread(EPTP_INDEX, &idx);
+        else
+        {
+            unsigned long eptp;
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs)

Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +8 -0
+
+            __vmread(EPT_POINTER, &eptp);
+
+            if ( (idx = p2m_find_altp2m_by_eptp(v->domain, eptp)) ==
+                 INVALID_ALTP2M )
+            {
+                gdprintk(XENLOG_ERR, "EPTP not found in alternate
+ p2m

list\n");

Quoted 10 lines
Quoted 7 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2876

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +5 -0
+                domain_crash(v->domain);
+            }
+        }
+
+        if ( (uint16_t)idx != vcpu_altp2m(v).p2midx )

Is this cast really necessary?

Yes - The index is 16-bits, this reflects how the field is specified in the vmcs also.

Quoted 7 lines
Quoted 4 lines

While "yes" answers the question, the explanation you give suggests that the answer may be wrong: Can idx indeed have bits set beyond bit 15? Because if it can't, the cast is pointless.

We were just trying to ensure we matched the hardware behavior (I think there was a message George had posted earlier for SVE that asked for

that).

Quoted 20 lines

Since hardware considers only a 16 bit field we were doing the same.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4 -0
+        {
+            BUG_ON(idx >= MAX_ALTP2M);
+            atomic_dec(&p2m_get_altp2m(v)->active_vcpus);
+            vcpu_altp2m(v).p2midx = (uint16_t)idx;
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

This one surely isn't (or else the field type is wrong).

Again required. idx can't be uint16_t because __vmread() requires unsigned long*, but the index is 16 bits.

But it's a 16-bit VMCS field that you read it from, and hence the upper 48

bits

Quoted 4 lines

are necessarily zero.

Just to re-iterate: Casts are necessary in certain places, yes, but I see them used pointlessly or even wrongly more often than not.

Quoted 6 lines
Quoted 2 lines

Same approach as above - emulating hardware exactly. Should we add a comment?

No, drop the casts.

Jan

Ok

Ravi

author
#25 lore raw

PATCH v5 7/15 VMX: add VMFUNC leaf 0 (EPTP switching) to emulator.

In reply to Ed White

From: Ravi Sahita <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             | 19 +++++++++++++++--
 xen/arch/x86/hvm/vmx/vmx.c             | 38 ++++++++++++++++++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.c | 19 +++++++++++------
 xen/arch/x86/x86_emulate/x86_emulate.h |  4 ++++
 xen/include/asm-x86/hvm/hvm.h          |  2 ++
 5 files changed, 74 insertions(+), 8 deletions(-)
xen/arch/x86/hvm/emulate.c +17 -2
index fe5661d..1aa8af4 100644
@@ -1436,6 +1436,19 @@ static int hvmemul_invlpg(
     return rc;
 }
 
+static int hvmemul_vmfunc(
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
+    if ( rc != X86EMUL_OKAY )
+    {
+        hvmemul_inject_hw_exception(TRAP_invalid_op, 0, ctxt);
+    }
+    return rc;
+}
+
 static const struct x86_emulate_ops hvm_emulate_ops = {
     .read          = hvmemul_read,
     .insn_fetch    = hvmemul_insn_fetch,
@@ -1459,7 +1472,8 @@ static const struct x86_emulate_ops hvm_emulate_ops = {
     .inject_sw_interrupt = hvmemul_inject_sw_interrupt,
     .get_fpu       = hvmemul_get_fpu,
     .put_fpu       = hvmemul_put_fpu,
-    .invlpg        = hvmemul_invlpg
+    .invlpg        = hvmemul_invlpg,
+    .vmfunc        = hvmemul_vmfunc,
 };
 
 static const struct x86_emulate_ops hvm_emulate_ops_no_write = {
@@ -1485,7 +1499,8 @@ static const struct x86_emulate_ops hvm_emulate_ops_no_write = {
     .inject_sw_interrupt = hvmemul_inject_sw_interrupt,
     .get_fpu       = hvmemul_get_fpu,
     .put_fpu       = hvmemul_put_fpu,
-    .invlpg        = hvmemul_invlpg
+    .invlpg        = hvmemul_invlpg,
+    .vmfunc        = hvmemul_vmfunc,
 };
 
 static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
xen/arch/x86/hvm/vmx/vmx.c +38 -0
index 38dba6b..845cdbc 100644
@@ -82,6 +82,7 @@ static void vmx_fpu_dirty_intercept(void);
 static int vmx_msr_read_intercept(unsigned int msr, uint64_t *msr_content);
 static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content);
 static void vmx_invlpg_intercept(unsigned long vaddr);
+static int vmx_vmfunc_intercept(struct cpu_user_regs *regs);
 
 uint8_t __read_mostly posted_intr_vector;
 
@@ -1830,6 +1831,19 @@ static void vmx_vcpu_update_vmfunc_ve(struct vcpu *v)
     vmx_vmcs_exit(v);
 }
 
+static int vmx_vcpu_emulate_vmfunc(struct cpu_user_regs *regs)
+{
+    int rc = X86EMUL_EXCEPTION;
+    struct vcpu *curr = current;
+
+    if ( !cpu_has_vmx_vmfunc && altp2m_active(curr->domain) &&
+         regs->eax == 0 &&
+         p2m_switch_vcpu_altp2m_by_id(curr, (uint16_t)regs->ecx) )
+        rc = X86EMUL_OKAY;
+
+    return rc;
+}
+
 static bool_t vmx_vcpu_emulate_ve(struct vcpu *v)
 {
     bool_t rc = 0;
@@ -1898,6 +1912,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .msr_read_intercept   = vmx_msr_read_intercept,
     .msr_write_intercept  = vmx_msr_write_intercept,
     .invlpg_intercept     = vmx_invlpg_intercept,
+    .vmfunc_intercept     = vmx_vmfunc_intercept,
     .handle_cd            = vmx_handle_cd,
     .set_info_guest       = vmx_set_info_guest,
     .set_rdtsc_exiting    = vmx_set_rdtsc_exiting,
@@ -1924,6 +1939,7 @@ static struct hvm_function_table __initdata vmx_function_table = {
     .altp2m_vcpu_update_eptp = vmx_vcpu_update_eptp,
     .altp2m_vcpu_update_vmfunc_ve = vmx_vcpu_update_vmfunc_ve,
     .altp2m_vcpu_emulate_ve = vmx_vcpu_emulate_ve,
+    .altp2m_vcpu_emulate_vmfunc = vmx_vcpu_emulate_vmfunc,
 };
 
 const struct hvm_function_table * __init start_vmx(void)
@@ -2095,6 +2111,19 @@ static void vmx_invlpg_intercept(unsigned long vaddr)
         vpid_sync_vcpu_gva(curr, vaddr);
 }
 
+static int vmx_vmfunc_intercept(struct cpu_user_regs *regs)
+{
+    /*
+     * This handler is a placeholder for future where Xen may
+     * want to handle VMFUNC exits and resume a domain normally without
+     * injecting a #UD to the guest - for example, in a VT-nested
+     * scenario where Xen may want to lazily shadow the alternate
+     * EPTP list.
+     */
+    gdprintk(XENLOG_ERR, "Failed guest VMFUNC execution\n");
+    return X86EMUL_EXCEPTION;
+}
+
 static int vmx_cr_access(unsigned long exit_qualification)
 {
     struct vcpu *curr = current;
@@ -3234,6 +3263,15 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
             update_guest_eip();
         break;
 
+    case EXIT_REASON_VMFUNC:
+        if ( (vmx_vmfunc_intercept(regs) == X86EMUL_EXCEPTION) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_UNHANDLEABLE) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_RETRY) )
+            hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        else
+            update_guest_eip();
+        break;
+
     case EXIT_REASON_MWAIT_INSTRUCTION:
     case EXIT_REASON_MONITOR_INSTRUCTION:
     case EXIT_REASON_GETSEC:
xen/arch/x86/x86_emulate/x86_emulate.c +13 -6
index c017c69..e596131 100644
@@ -3786,6 +3786,7 @@ x86_emulate(
         break;
     }
 
+ no_writeback:
     /* Inject #DB if single-step tracing was enabled at instruction start. */
     if ( (ctxt->regs->eflags & EFLG_TF) && (rc == X86EMUL_OKAY) &&
          (ops->inject_hw_exception != NULL) )
@@ -3816,19 +3817,17 @@ x86_emulate(
         struct segment_register reg;
         unsigned long base, limit, cr0, cr0w;
 
-        if ( modrm == 0xdf ) /* invlpga */
+        switch( modrm )
         {
+        case 0xdf: /* invlpga */
             generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, -1);
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
             fail_if(ops->invlpg == NULL);
             if ( (rc = ops->invlpg(x86_seg_none, truncate_ea(_regs.eax),
                                    ctxt)) )
                 goto done;
-            break;
-        }
-
-        if ( modrm == 0xf9 ) /* rdtscp */
-        {
+            goto no_writeback;
+        case 0xf9: /* rdtscp */ {
             uint64_t tsc_aux;
             fail_if(ops->read_msr == NULL);
             if ( (rc = ops->read_msr(MSR_TSC_AUX, &tsc_aux, ctxt)) != 0 )
@@ -3836,6 +3835,14 @@ x86_emulate(
             _regs.ecx = (uint32_t)tsc_aux;
             goto rdtsc;
         }
+        case 0xd4: /* vmfunc */
+            generate_exception_if(lock_prefix | rep_prefix() | (vex.pfx == vex_66),
+                                  EXC_UD, -1);
+            fail_if(ops->vmfunc == NULL);
+            if ( (rc = ops->vmfunc(ctxt) != X86EMUL_OKAY) )
+                goto done;
+            goto no_writeback;
+        }
 
         switch ( modrm_reg & 7 )
         {
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
index 064b8f4..a4d4ec8 100644
@@ -397,6 +397,10 @@ struct x86_emulate_ops
         enum x86_segment seg,
         unsigned long offset,
         struct x86_emulate_ctxt *ctxt);
+
+    /* vmfunc: Emulate VMFUNC via given set of EAX ECX inputs */
+    int (*vmfunc)(
+        struct x86_emulate_ctxt *ctxt);
 };
 
 struct cpu_user_regs;
xen/include/asm-x86/hvm/hvm.h +2 -0
index 473ac30..335b7a6 100644
@@ -167,6 +167,7 @@ struct hvm_function_table {
     int (*msr_read_intercept)(unsigned int msr, uint64_t *msr_content);
     int (*msr_write_intercept)(unsigned int msr, uint64_t msr_content);
     void (*invlpg_intercept)(unsigned long vaddr);
+    int (*vmfunc_intercept)(struct cpu_user_regs *regs);
     void (*handle_cd)(struct vcpu *v, unsigned long value);
     void (*set_info_guest)(struct vcpu *v);
     void (*set_rdtsc_exiting)(struct vcpu *v, bool_t);
@@ -215,6 +216,7 @@ struct hvm_function_table {
     void (*altp2m_vcpu_update_eptp)(struct vcpu *v);
     void (*altp2m_vcpu_update_vmfunc_ve)(struct vcpu *v);
     bool_t (*altp2m_vcpu_emulate_ve)(struct vcpu *v);
+    int (*altp2m_vcpu_emulate_vmfunc)(struct cpu_user_regs *regs);
 };
 
 extern struct hvm_function_table hvm_funcs;
Signature · 1 line
1.9.1
#26 lore raw

Re: [PATCH v5 07/15] VMX: add VMFUNC leaf 0 (EPTP switching) to emulator.

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +1 -0
@@ -1436,6 +1436,19 @@ static int hvmemul_invlpg(
     return rc;
 }
 
+static int hvmemul_vmfunc(

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1449

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
+    if ( rc != X86EMUL_OKAY )
+    {
+        hvmemul_inject_hw_exception(TRAP_invalid_op, 0, ctxt);
+    }
+    return rc;

Pointless braces and missing blank line before final return.

xen/arch/x86/hvm/emulate.c static void vmx_vcpu_update_vmfunc_ve(struct vcpu *v) · line 1841

Quoted 12 lines
xen/arch/x86/hvm/emulate.c +8 -0
@@ -1830,6 +1831,19 @@ static void vmx_vcpu_update_vmfunc_ve(struct vcpu *v)
     vmx_vmcs_exit(v);
 }
 
+static int vmx_vcpu_emulate_vmfunc(struct cpu_user_regs *regs)
+{
+    int rc = X86EMUL_EXCEPTION;
+    struct vcpu *curr = current;
+
+    if ( !cpu_has_vmx_vmfunc && altp2m_active(curr->domain) &&
+         regs->eax == 0 &&
+         p2m_switch_vcpu_altp2m_by_id(curr, (uint16_t)regs->ecx) )

Documentation suggests that the upper 32 bits of RAX are being ignored, and that all 32 bits of ECX are being used.

xen/arch/x86/hvm/emulate.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 3269

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +4 -0
@@ -3234,6 +3263,15 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
             update_guest_eip();
         break;
 
+    case EXIT_REASON_VMFUNC:
+        if ( (vmx_vmfunc_intercept(regs) == X86EMUL_EXCEPTION) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_UNHANDLEABLE) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_RETRY) )

Why would you want to invoke the function 3 times? How about simply != X86EMUL_OKAY?

Jan

#27 lore raw

Re: [PATCH v5 07/15] VMX: add VMFUNC leaf 0 (EPTP switching) to emulator.

Quoted 10 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:04 AM

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1436,6 +1436,19 @@ static int hvmemul_invlpg(
     return rc;
 }
 
Quoted 13 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1449

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +11 -0
+static int hvmemul_vmfunc(
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
+    if ( rc != X86EMUL_OKAY )
+    {
+        hvmemul_inject_hw_exception(TRAP_invalid_op, 0, ctxt);
+    }
+    return rc;

Pointless braces and missing blank line before final return.

ok

Quoted 22 lines

xen/arch/x86/hvm/emulate.c static void vmx_vcpu_update_vmfunc_ve(struct

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1830,6 +1831,19 @@ static void vmx_vcpu_update_vmfunc_ve(struct

vcpu *v)

xen/arch/x86/hvm/emulate.c static void vmx_vcpu_update_vmfunc_ve(struct · line 1840

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +7 -0
     vmx_vmcs_exit(v);
 }
 
+static int vmx_vcpu_emulate_vmfunc(struct cpu_user_regs *regs) {
+    int rc = X86EMUL_EXCEPTION;
+    struct vcpu *curr = current;
+
+    if ( !cpu_has_vmx_vmfunc && altp2m_active(curr->domain) &&
+         regs->eax == 0 &&
+         p2m_switch_vcpu_altp2m_by_id(curr, (uint16_t)regs->ecx) )

Documentation suggests that the upper 32 bits of RAX are being ignored, and that all 32 bits of ECX are being used.

xen/arch/x86/hvm/emulate.c void vmx_vmexit_handler(struct cpu_user_regs

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -0
@@ -3234,6 +3263,15 @@ void vmx_vmexit_handler(struct cpu_user_regs

*regs)

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
             update_guest_eip();
         break;
 
Quoted 7 lines

xen/arch/x86/hvm/emulate.c void vmx_vmexit_handler(struct cpu_user_regs · line 3269

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+    case EXIT_REASON_VMFUNC:
+        if ( (vmx_vmfunc_intercept(regs) == X86EMUL_EXCEPTION) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_UNHANDLEABLE) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_RETRY) )

Why would you want to invoke the function 3 times? How about simply != X86EMUL_OKAY?

My bad - I mean to invoke vmx_vmfunc_intercept() once and just check the error conditions listed. Which change to !=X86EMUL_OKAY

Ravi

Quoted 2 lines

Jan

#28 lore raw

Re: [PATCH v5 07/15] VMX: add VMFUNC leaf 0 (EPTP switching) to emulator.

In reply to Jan Beulich
Quoted 11 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:04 AM

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1436,6 +1436,19 @@ static int hvmemul_invlpg(
     return rc;
 }
 
Quoted 14 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1449

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +11 -0
+static int hvmemul_vmfunc(
+    struct x86_emulate_ctxt *ctxt)
+{
+    int rc;
+
+    rc = hvm_funcs.altp2m_vcpu_emulate_vmfunc(ctxt->regs);
+    if ( rc != X86EMUL_OKAY )
+    {
+        hvmemul_inject_hw_exception(TRAP_invalid_op, 0, ctxt);
+    }
+    return rc;

Pointless braces and missing blank line before final return.

Will be fixed.

Quoted 5 lines

xen/arch/x86/hvm/emulate.c static void vmx_vcpu_update_vmfunc_ve(struct

Quoted 1 line
xen/arch/x86/hvm/emulate.c +0 -0
@@ -1830,6 +1831,19 @@ static void vmx_vcpu_update_vmfunc_ve(struct

vcpu *v)

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
     vmx_vmcs_exit(v);
 }
 
Quoted 10 lines

xen/arch/x86/hvm/emulate.c static void vmx_vcpu_update_vmfunc_ve(struct · line 1840

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+static int vmx_vcpu_emulate_vmfunc(struct cpu_user_regs *regs) {
+    int rc = X86EMUL_EXCEPTION;
+    struct vcpu *curr = current;
+
+    if ( !cpu_has_vmx_vmfunc && altp2m_active(curr->domain) &&
+         regs->eax == 0 &&
+         p2m_switch_vcpu_altp2m_by_id(curr, (uint16_t)regs->ecx) )

Documentation suggests that the upper 32 bits of RAX are being ignored, and that all 32 bits of ECX are being used.

Will fix to use _eax and _ecx, ECX is expected to be <512 by hardware which the p2m_switch_... function checks.

Quoted 6 lines

xen/arch/x86/hvm/emulate.c void vmx_vmexit_handler(struct cpu_user_regs

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +0 -0
@@ -3234,6 +3263,15 @@ void vmx_vmexit_handler(struct cpu_user_regs

*regs)

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
             update_guest_eip();
         break;
 
Quoted 7 lines

xen/arch/x86/hvm/emulate.c void vmx_vmexit_handler(struct cpu_user_regs · line 3269

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+    case EXIT_REASON_VMFUNC:
+        if ( (vmx_vmfunc_intercept(regs) == X86EMUL_EXCEPTION) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_UNHANDLEABLE) ||
+             (vmx_vmfunc_intercept(regs) == X86EMUL_RETRY) )

Why would you want to invoke the function 3 times? How about simply != X86EMUL_OKAY?

Yuck - sorry introduced in error of course - will fix.

Thanks,
Ravi

Quoted 2 lines

Jan

author
#29 lore raw

PATCH v5 8/15 x86/altp2m: add control of suppress_ve.

In reply to Ed White

From: George Dunlap <[email protected]>

The existing ept_set_entry() and ept_get_entry() routines are extended to optionally set/get suppress_ve. Passing -1 will set suppress_ve on new p2m entries, or retain suppress_ve flag on existing entries.

---
 xen/arch/x86/mm/mem_sharing.c |  4 ++--
 xen/arch/x86/mm/p2m-ept.c     | 18 ++++++++++++----
 xen/arch/x86/mm/p2m-pod.c     | 12 +++++------
 xen/arch/x86/mm/p2m-pt.c      | 10 +++++++--
 xen/arch/x86/mm/p2m.c         | 48 +++++++++++++++++++++----------------------
 xen/include/asm-x86/p2m.h     | 24 ++++++++++++----------
 6 files changed, 67 insertions(+), 49 deletions(-)
xen/arch/x86/mm/mem_sharing.c +2 -2
index 16e329e..1d1918f 100644
@@ -1260,7 +1260,7 @@ int relinquish_shared_pages(struct domain *d)
 
         if ( atomic_read(&d->shr_pages) == 0 )
             break;
-        mfn = p2m->get_entry(p2m, gfn, &t, &a, 0, NULL);
+        mfn = p2m->get_entry(p2m, gfn, &t, &a, 0, NULL, NULL);
         if ( mfn_valid(mfn) && (t == p2m_ram_shared) )
         {
             /* Does not fail with ENOMEM given the DESTROY flag */
@@ -1270,7 +1270,7 @@ int relinquish_shared_pages(struct domain *d)
              * unshare.  Must succeed: we just read the old entry and
              * we hold the p2m lock. */
             set_rc = p2m->set_entry(p2m, gfn, _mfn(0), PAGE_ORDER_4K,
-                                    p2m_invalid, p2m_access_rwx);
+                                    p2m_invalid, p2m_access_rwx, -1);
             ASSERT(set_rc == 0);
             count += 0x10;
         }
xen/arch/x86/mm/p2m-ept.c +14 -4
index 59410ea..9167833 100644
@@ -657,7 +657,8 @@ bool_t ept_handle_misconfig(uint64_t gpa)
  */
 static int
 ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn, 
-              unsigned int order, p2m_type_t p2mt, p2m_access_t p2ma)
+              unsigned int order, p2m_type_t p2mt, p2m_access_t p2ma,
+              int sve)
 {
     ept_entry_t *table, *ept_entry = NULL;
     unsigned long gfn_remainder = gfn;
@@ -803,7 +804,11 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         ept_p2m_type_to_flags(p2m, &new_entry, p2mt, p2ma);
     }
 
-    new_entry.suppress_ve = 1;
+    if ( sve != -1 )
+        new_entry.suppress_ve = !!sve;
+    else
+        new_entry.suppress_ve = is_epte_valid(&old_entry) ?
+                                    old_entry.suppress_ve : 1;
 
     rc = atomic_write_ept_entry(ept_entry, new_entry, target);
     if ( unlikely(rc) )
@@ -850,8 +855,9 @@ out:
 
 /* Read ept p2m entries */
 static mfn_t ept_get_entry(struct p2m_domain *p2m,
-                           unsigned long gfn, p2m_type_t *t, p2m_access_t* a,
-                           p2m_query_t q, unsigned int *page_order)
+                            unsigned long gfn, p2m_type_t *t, p2m_access_t* a,
+                            p2m_query_t q, unsigned int *page_order,
+                            bool_t *sve)
 {
     ept_entry_t *table = map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m)));
     unsigned long gfn_remainder = gfn;
@@ -865,6 +871,8 @@ static mfn_t ept_get_entry(struct p2m_domain *p2m,
 
     *t = p2m_mmio_dm;
     *a = p2m_access_n;
+    if ( sve )
+        *sve = 1;
 
     /* This pfn is higher than the highest the p2m map currently holds */
     if ( gfn > p2m->max_mapped_pfn )
@@ -930,6 +938,8 @@ static mfn_t ept_get_entry(struct p2m_domain *p2m,
         else
             *t = ept_entry->sa_p2mt;
         *a = ept_entry->access;
+        if ( sve )
+            *sve = ept_entry->suppress_ve;
 
         mfn = _mfn(ept_entry->mfn);
         if ( i )
xen/arch/x86/mm/p2m-pod.c +6 -6
index 0679f00..a2f6d02 100644
@@ -536,7 +536,7 @@ recount:
         p2m_access_t a;
         p2m_type_t t;
 
-        (void)p2m->get_entry(p2m, gpfn + i, &t, &a, 0, NULL);
+        (void)p2m->get_entry(p2m, gpfn + i, &t, &a, 0, NULL, NULL);
 
         if ( t == p2m_populate_on_demand )
             pod++;
@@ -587,7 +587,7 @@ recount:
         p2m_type_t t;
         p2m_access_t a;
 
-        mfn = p2m->get_entry(p2m, gpfn + i, &t, &a, 0, NULL);
+        mfn = p2m->get_entry(p2m, gpfn + i, &t, &a, 0, NULL, NULL);
         if ( t == p2m_populate_on_demand )
         {
             p2m_set_entry(p2m, gpfn + i, _mfn(INVALID_MFN), 0, p2m_invalid,
@@ -676,7 +676,7 @@ p2m_pod_zero_check_superpage(struct p2m_domain *p2m, unsigned long gfn)
     for ( i=0; i<SUPERPAGE_PAGES; i++ )
     {
         p2m_access_t a; 
-        mfn = p2m->get_entry(p2m, gfn + i, &type, &a, 0, NULL);
+        mfn = p2m->get_entry(p2m, gfn + i, &type, &a, 0, NULL, NULL);
 
         if ( i == 0 )
         {
@@ -808,7 +808,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, unsigned long *gfns, int count)
     for ( i=0; i<count; i++ )
     {
         p2m_access_t a;
-        mfns[i] = p2m->get_entry(p2m, gfns[i], types + i, &a, 0, NULL);
+        mfns[i] = p2m->get_entry(p2m, gfns[i], types + i, &a, 0, NULL, NULL);
         /* If this is ram, and not a pagetable or from the xen heap, and probably not mapped
            elsewhere, map it; otherwise, skip. */
         if ( p2m_is_ram(types[i])
@@ -947,7 +947,7 @@ p2m_pod_emergency_sweep(struct p2m_domain *p2m)
     for ( i=p2m->pod.reclaim_single; i > 0 ; i-- )
     {
         p2m_access_t a;
-        (void)p2m->get_entry(p2m, i, &t, &a, 0, NULL);
+        (void)p2m->get_entry(p2m, i, &t, &a, 0, NULL, NULL);
         if ( p2m_is_ram(t) )
         {
             gfns[j] = i;
@@ -1135,7 +1135,7 @@ guest_physmap_mark_populate_on_demand(struct domain *d, unsigned long gfn,
     for ( i = 0; i < (1UL << order); i++ )
     {
         p2m_access_t a;
-        omfn = p2m->get_entry(p2m, gfn + i, &ot, &a, 0, NULL);
+        omfn = p2m->get_entry(p2m, gfn + i, &ot, &a, 0, NULL, NULL);
         if ( p2m_is_ram(ot) )
         {
             P2M_DEBUG("gfn_to_mfn returned type %d!\n", ot);
xen/arch/x86/mm/p2m-pt.c +8 -2
index e50b6fa..f6944cd 100644
@@ -482,7 +482,8 @@ int p2m_pt_handle_deferred_changes(uint64_t gpa)
 /* Returns: 0 for success, -errno for failure */
 static int
 p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
-                 unsigned int page_order, p2m_type_t p2mt, p2m_access_t p2ma)
+                 unsigned int page_order, p2m_type_t p2mt, p2m_access_t p2ma,
+                 int sve)
 {
     /* XXX -- this might be able to be faster iff current->domain == d */
     void *table;
@@ -495,6 +496,8 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
     unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt);
     unsigned long old_mfn = 0;
 
+    ASSERT(sve != 0);
+
     if ( tb_init_done )
     {
         struct {
@@ -689,7 +692,7 @@ static inline p2m_type_t recalc_type(bool_t recalc, p2m_type_t t,
 static mfn_t
 p2m_pt_get_entry(struct p2m_domain *p2m, unsigned long gfn,
                  p2m_type_t *t, p2m_access_t *a, p2m_query_t q,
-                 unsigned int *page_order)
+                 unsigned int *page_order, bool_t *sve)
 {
     mfn_t mfn;
     paddr_t addr = ((paddr_t)gfn) << PAGE_SHIFT;
@@ -701,6 +704,9 @@ p2m_pt_get_entry(struct p2m_domain *p2m, unsigned long gfn,
 
     ASSERT(paging_mode_translate(p2m->domain));
 
+    if ( sve )
+        *sve = 1;
+
     /* XXX This is for compatibility with the old model, where anything not 
      * XXX marked as RAM was considered to be emulated MMIO space.
      * XXX Once we start explicitly registering MMIO regions in the p2m 
xen/arch/x86/mm/p2m.c +24 -24

183 lines not on this page · raw message

xen/include/asm-x86/p2m.h +13 -11
index f38b452..36b180b 100644
@@ -226,17 +226,19 @@ struct p2m_domain {
     /* Pages used to construct the p2m */
     struct page_list_head pages;
 
-    int                (*set_entry   )(struct p2m_domain *p2m,
-                                       unsigned long gfn,
-                                       mfn_t mfn, unsigned int page_order,
-                                       p2m_type_t p2mt,
-                                       p2m_access_t p2ma);
-    mfn_t              (*get_entry   )(struct p2m_domain *p2m,
-                                       unsigned long gfn,
-                                       p2m_type_t *p2mt,
-                                       p2m_access_t *p2ma,
-                                       p2m_query_t q,
-                                       unsigned int *page_order);
+    int                (*set_entry)(struct p2m_domain *p2m,
+                                         unsigned long gfn,
+                                         mfn_t mfn, unsigned int page_order,
+                                         p2m_type_t p2mt,
+                                         p2m_access_t p2ma,
+                                         int sve);
+    mfn_t              (*get_entry)(struct p2m_domain *p2m,
+                                         unsigned long gfn,
+                                         p2m_type_t *p2mt,
+                                         p2m_access_t *p2ma,
+                                         p2m_query_t q,
+                                         unsigned int *page_order,
+                                         bool_t *sve);
     void               (*enable_hardware_log_dirty)(struct p2m_domain *p2m);
     void               (*disable_hardware_log_dirty)(struct p2m_domain *p2m);
     void               (*flush_hardware_cached_dirty)(struct p2m_domain *p2m);
Signature · 1 line
1.9.1
Reviewed-by on v5 8/15
#30 lore raw

Re: [PATCH v5 08/15] x86/altp2m: add control of suppress_ve.

Quoted 10 lines · Ed White

From: George Dunlap <[email protected]>

The existing ept_set_entry() and ept_get_entry() routines are extended to optionally set/get suppress_ve. Passing -1 will set suppress_ve on new p2m entries, or retain suppress_ve flag on existing entries.

Thanks!

author
#31 lore raw

PATCH v5 9/15 x86/altp2m: alternate p2m memory events.

In reply to Ed White

Add a flag to indicate that a memory event occurred in an alternate p2m and a field containing the p2m index. Allow any event response to switch to a different alternate p2m using the same flag and field.

Modify p2m_mem_access_check() to handle alternate p2m's.

---
 xen/arch/x86/mm/p2m.c         | 19 ++++++++++++++++++-
 xen/common/vm_event.c         |  4 ++++
 xen/include/asm-arm/p2m.h     |  6 ++++++
 xen/include/asm-x86/p2m.h     |  3 +++
 xen/include/public/vm_event.h | 11 +++++++++++
 5 files changed, 42 insertions(+), 1 deletion(-)
xen/arch/x86/mm/p2m.c +18 -1
index a3636ab..ecec53e 100644
@@ -1518,6 +1518,12 @@ void p2m_mem_access_emulate_check(struct vcpu *v,
     }
 }
 
+void p2m_altp2m_check(struct vcpu *v, uint16_t idx)
+{
+    if ( altp2m_active(v->domain) )
+        p2m_switch_vcpu_altp2m_by_id(v, idx);
+}
+
 bool_t p2m_mem_access_check(paddr_t gpa, unsigned long gla,
                             struct npfec npfec,
                             vm_event_request_t **req_ptr)
@@ -1525,7 +1531,7 @@ bool_t p2m_mem_access_check(paddr_t gpa, unsigned long gla,
     struct vcpu *v = current;
     unsigned long gfn = gpa >> PAGE_SHIFT;
     struct domain *d = v->domain;    
-    struct p2m_domain* p2m = p2m_get_hostp2m(d);
+    struct p2m_domain *p2m = NULL;
     mfn_t mfn;
     p2m_type_t p2mt;
     p2m_access_t p2ma;
@@ -1533,6 +1539,11 @@ bool_t p2m_mem_access_check(paddr_t gpa, unsigned long gla,
     int rc;
     unsigned long eip = guest_cpu_user_regs()->eip;
 
+    if ( altp2m_active(d) )
+        p2m = p2m_get_altp2m(v);
+    if ( !p2m )
+        p2m = p2m_get_hostp2m(d);
+
     /* First, handle rx2rw conversion automatically.
      * These calls to p2m->set_entry() must succeed: we have the gfn
      * locked and just did a successful get_entry(). */
@@ -1639,6 +1650,12 @@ bool_t p2m_mem_access_check(paddr_t gpa, unsigned long gla,
         req->vcpu_id = v->vcpu_id;
 
         p2m_vm_event_fill_regs(req);
+
+        if ( altp2m_active(v->domain) )
+        {
+            req->flags |= VM_EVENT_FLAG_ALTERNATE_P2M;
+            req->altp2m_idx = vcpu_altp2m(v).p2midx;
+        }
     }
 
     /* Pause the current VCPU */
xen/common/vm_event.c +4 -0
index 120a78a..13224e2 100644
@@ -399,6 +399,10 @@ void vm_event_resume(struct domain *d, struct vm_event_domain *ved)
 
         };
 
+        /* Check for altp2m switch */
+        if ( rsp.flags & VM_EVENT_FLAG_ALTERNATE_P2M )
+            p2m_altp2m_check(v, rsp.altp2m_idx);
+
         /* Unpause domain. */
         if ( rsp.flags & VM_EVENT_FLAG_VCPU_PAUSED )
             vm_event_vcpu_unpause(v);
xen/include/asm-arm/p2m.h +6 -0
index 63748ef..08bdce3 100644
@@ -109,6 +109,12 @@ void p2m_mem_access_emulate_check(struct vcpu *v,
     /* Not supported on ARM. */
 }
 
+static inline
+void p2m_altp2m_check(struct vcpu *v, uint16_t idx)
+{
+    /* Not supported on ARM. */
+}
+
 #define p2m_is_foreign(_t)  ((_t) == p2m_map_foreign)
 #define p2m_is_ram(_t)      ((_t) == p2m_ram_rw || (_t) == p2m_ram_ro)
 
xen/include/asm-x86/p2m.h +3 -0
index 36b180b..5c5f4e8 100644
@@ -753,6 +753,9 @@ uint16_t p2m_find_altp2m_by_eptp(struct domain *d, uint64_t eptp);
 /* Switch alternate p2m for a single vcpu */
 bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx);
 
+/* Check to see if vcpu should be switched to a different p2m. */
+void p2m_altp2m_check(struct vcpu *v, uint16_t idx);
+
 /*
  * p2m type to IOMMU flags
  */
xen/include/public/vm_event.h +11 -0
index 577e971..3556e92 100644
@@ -47,6 +47,16 @@
 #define VM_EVENT_FLAG_VCPU_PAUSED     (1 << 0)
 /* Flags to aid debugging mem_event */
 #define VM_EVENT_FLAG_FOREIGN         (1 << 1)
+/*
+ * This flag can be set in a request or a response
+ *
+ * On a request, indicates that the event occurred in the alternate p2m specified by
+ * the altp2m_idx request field.
+ *
+ * On a response, indicates that the VCPU should resume in the alternate p2m specified
+ * by the altp2m_idx response field if possible.
+ */
+#define VM_EVENT_FLAG_ALTERNATE_P2M   (1 << 5)
 
 /*
  * Reasons for the vm event request
@@ -194,6 +204,7 @@ typedef struct vm_event_st {
     uint32_t flags;     /* VM_EVENT_FLAG_* */
     uint32_t reason;    /* VM_EVENT_REASON_* */
     uint32_t vcpu_id;
+    uint16_t altp2m_idx; /* may be used during request and response */
 
     union {
         struct vm_event_paging                mem_paging;
Signature · 1 line
1.9.1
#32 lore raw

Re: [PATCH v5 09/15] x86/altp2m: alternate p2m memory events.

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/include/public/vm_event.h +0 -0
@@ -47,6 +47,16 @@
 #define VM_EVENT_FLAG_VCPU_PAUSED     (1 << 0)
 /* Flags to aid debugging mem_event */
 #define VM_EVENT_FLAG_FOREIGN         (1 << 1)

xen/include/public/vm_event.h line 59

Quoted 10 lines
xen/include/public/vm_event.h +10 -0
+/*
+ * This flag can be set in a request or a response
+ *
+ * On a request, indicates that the event occurred in the alternate p2m specified by
+ * the altp2m_idx request field.
+ *
+ * On a response, indicates that the VCPU should resume in the alternate p2m specified
+ * by the altp2m_idx response field if possible.
+ */
+#define VM_EVENT_FLAG_ALTERNATE_P2M   (1 << 5)

So I suppose you use 5 here because of what went into staging recently. But the patch context doesn't reflect this, i.e. the patch is inconsistent at this point (and won't apply).

Jan

#33 lore raw

Re: [PATCH v5 09/15] x86/altp2m: alternate p2m memory events.

Quoted 27 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:08 AM

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/include/public/vm_event.h +2 -0
@@ -47,6 +47,16 @@
 #define VM_EVENT_FLAG_VCPU_PAUSED     (1 << 0)
 /* Flags to aid debugging mem_event */
 #define VM_EVENT_FLAG_FOREIGN         (1 << 1)
+/*
+ * This flag can be set in a request or a response

xen/include/public/vm_event.h line 61

Quoted 10 lines
xen/include/public/vm_event.h +10 -0
+ *
+ * On a request, indicates that the event occurred in the alternate
+p2m specified by
+ * the altp2m_idx request field.
+ *
+ * On a response, indicates that the VCPU should resume in the
+alternate p2m specified
+ * by the altp2m_idx response field if possible.
+ */
+#define VM_EVENT_FLAG_ALTERNATE_P2M   (1 << 5)

So I suppose you use 5 here because of what went into staging recently. But the patch context doesn't reflect this, i.e. the patch is inconsistent at this point (and won't apply).

Quoted 1 line

Jan

Yes - we didn't have time to rebase on staging - we are working on that now

Ravi

author
#34 lore raw

PATCH v5 10/15 x86/altp2m: add remaining support routines.

In reply to Ed White

Add the remaining routines required to support enabling the alternate p2m functionality.

---
 xen/arch/x86/hvm/hvm.c           |  58 +++++-
 xen/arch/x86/mm/hap/Makefile     |   1 +
 xen/arch/x86/mm/hap/altp2m_hap.c |  98 ++++++++++
 xen/arch/x86/mm/p2m-ept.c        |   3 +
 xen/arch/x86/mm/p2m.c            | 385 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/altp2m.h |   4 +
 xen/include/asm-x86/p2m.h        |  33 ++++
 7 files changed, 576 insertions(+), 6 deletions(-)
 create mode 100644 xen/arch/x86/mm/hap/altp2m_hap.c
xen/arch/x86/hvm/hvm.c +52 -6
index 38deedc..a9f4b1b 100644
@@ -2802,10 +2802,11 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     mfn_t mfn;
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
-    struct p2m_domain *p2m;
+    struct p2m_domain *p2m, *hostp2m;
     int rc, fall_through = 0, paged = 0;
     int sharing_enomem = 0;
     vm_event_request_t *req_ptr = NULL;
+    bool_t ap2m_active = 0;
 
     /* On Nested Virtualization, walk the guest page table.
      * If this succeeds, all is fine.
@@ -2865,11 +2866,31 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         goto out;
     }
 
-    p2m = p2m_get_hostp2m(currd);
-    mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma, 
+    ap2m_active = altp2m_active(currd);
+
+    /* Take a lock on the host p2m speculatively, to avoid potential
+     * locking order problems later and to handle unshare etc.
+     */
+    hostp2m = p2m_get_hostp2m(currd);
+    mfn = get_gfn_type_access(hostp2m, gfn, &p2mt, &p2ma,
                               P2M_ALLOC | (npfec.write_access ? P2M_UNSHARE : 0),
                               NULL);
 
+    if ( ap2m_active )
+    {
+        if ( altp2m_hap_nested_page_fault(curr, gpa, gla, npfec, &p2m) == 1 )
+        {
+            /* entry was lazily copied from host -- retry */
+            __put_gfn(hostp2m, gfn);
+            rc = 1;
+            goto out;
+        }
+
+        mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma, 0, NULL);
+    }
+    else
+        p2m = hostp2m;
+
     /* Check access permissions first, then handle faults */
     if ( mfn_x(mfn) != INVALID_MFN )
     {
@@ -2909,6 +2930,20 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
 
         if ( violation )
         {
+            /* Should #VE be emulated for this fault? */
+            if ( p2m_is_altp2m(p2m) && !cpu_has_vmx_virt_exceptions )
+            {
+                bool_t sve;
+
+                p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL, &sve);
+
+                if ( !sve && altp2m_vcpu_emulate_ve(curr) )
+                {
+                    rc = 1;
+                    goto out_put_gfn;
+                }
+            }
+
             if ( p2m_mem_access_check(gpa, gla, npfec, &req_ptr) )
             {
                 fall_through = 1;
@@ -2928,7 +2963,9 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
          (npfec.write_access &&
           (p2m_is_discard_write(p2mt) || (p2mt == p2m_mmio_write_dm))) )
     {
-        put_gfn(currd, gfn);
+        __put_gfn(p2m, gfn);
+        if ( ap2m_active )
+            __put_gfn(hostp2m, gfn);
 
         rc = 0;
         if ( unlikely(is_pvh_domain(currd)) )
@@ -2957,6 +2994,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     /* Spurious fault? PoD and log-dirty also take this path. */
     if ( p2m_is_ram(p2mt) )
     {
+        rc = 1;
         /*
          * Page log dirty is always done with order 0. If this mfn resides in
          * a large page, we do not change other pages type within that large
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid lock ordering
+             * violation when the change below is propagated from host p2m */
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty, p2m_ram_rw);
+            __put_gfn(ap2m_active ? hostp2m : p2m, gfn);
+
+            goto out;
         }
-        rc = 1;
         goto out_put_gfn;
     }
 
@@ -2977,7 +3021,9 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     rc = fall_through;
 
 out_put_gfn:
-    put_gfn(currd, gfn);
+    __put_gfn(p2m, gfn);
+    if ( ap2m_active )
+        __put_gfn(hostp2m, gfn);
 out:
     /* All of these are delayed until we exit, since we might 
      * sleep on event ring wait queues, and we must not hold
xen/arch/x86/mm/hap/Makefile +1 -0
index 68f2bb5..216cd90 100644
@@ -4,6 +4,7 @@ obj-y += guest_walk_3level.o
 obj-$(x86_64) += guest_walk_4level.o
 obj-y += nested_hap.o
 obj-y += nested_ept.o
+obj-y += altp2m_hap.o
 
 guest_walk_%level.o: guest_walk.c Makefile
 	$(CC) $(CFLAGS) -DGUEST_PAGING_LEVELS=$* -c $< -o $@
xen/arch/x86/mm/hap/altp2m_hap.c new file +98 -0

98 lines not on this page · raw message

xen/arch/x86/mm/p2m-ept.c +3 -0
index 9167833..7ca0598 100644
@@ -850,6 +850,9 @@ out:
     if ( is_epte_present(&old_entry) )
         ept_free_entry(p2m, &old_entry, target);
 
+    if ( rc == 0 && p2m_is_hostp2m(p2m) )
+        p2m_altp2m_propagate_change(d, _gfn(gfn), mfn, order, p2mt, p2ma);
+
     return rc;
 }
 
xen/arch/x86/mm/p2m.c +385 -0

391 lines not on this page · raw message

xen/include/asm-x86/hvm/altp2m.h +4 -0
index 38de494..6aa8c6b 100644
@@ -34,5 +34,9 @@ void altp2m_vcpu_initialise(struct vcpu *v);
 void altp2m_vcpu_destroy(struct vcpu *v);
 void altp2m_vcpu_reset(struct vcpu *v);
 
+/* Alternate p2m paging */
+int altp2m_hap_nested_page_fault(struct vcpu *v, paddr_t gpa,
+    unsigned long gla, struct npfec npfec, struct p2m_domain **ap2m);
+
 #endif /* _ALTP2M_H */
 
xen/include/asm-x86/p2m.h +33 -0
index 5c5f4e8..49d1829 100644
@@ -268,6 +268,11 @@ struct p2m_domain {
     /* Highest guest frame that's ever been mapped in the p2m */
     unsigned long max_mapped_pfn;
 
+    /* Alternate p2m's only: range of gfn's for which underlying
+     * mfn may have duplicate mappings */
+    unsigned long min_remapped_gfn;
+    unsigned long max_remapped_gfn;
+
     /* When releasing shared gfn's in a preemptible manner, recall where
      * to resume the search */
     unsigned long next_shared_gfn_to_relinquish;
@@ -756,6 +761,34 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx);
 /* Check to see if vcpu should be switched to a different p2m. */
 void p2m_altp2m_check(struct vcpu *v, uint16_t idx);
 
+/* Flush all the alternate p2m's for a domain */
+void p2m_flush_altp2m(struct domain *d);
+
+/* Make a specific alternate p2m valid */
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx);
+
+/* Find an available alternate p2m and make it valid */
+long p2m_init_next_altp2m(struct domain *d, uint16_t *idx);
+
+/* Make a specific alternate p2m invalid */
+long p2m_destroy_altp2m_by_id(struct domain *d, uint16_t idx);
+
+/* Switch alternate p2m for entire domain */
+long p2m_switch_domain_altp2m_by_id(struct domain *d, uint16_t idx);
+
+/* Set access type for a gfn */
+long p2m_set_altp2m_mem_access(struct domain *d, uint16_t idx,
+                                 gfn_t gfn, xenmem_access_t access);
+
+/* Change a gfn->mfn mapping */
+long p2m_change_altp2m_gfn(struct domain *d, uint16_t idx,
+                             gfn_t old_gfn, gfn_t new_gfn);
+
+/* Propagate a host p2m change to all alternate p2m's */
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t p2ma);
+
 /*
  * p2m type to IOMMU flags
  */
Signature · 1 line
1.9.1
#35 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2809

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +2 -1
@@ -2802,10 +2802,11 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     mfn_t mfn;
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
-    struct p2m_domain *p2m;
+    struct p2m_domain *p2m, *hostp2m;
     int rc, fall_through = 0, paged = 0;
     int sharing_enomem = 0;
     vm_event_request_t *req_ptr = NULL;
+    bool_t ap2m_active = 0;

Pointless initializer afaict.

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2873

Quoted 11 lines
xen/arch/x86/hvm/hvm.c +5 -2
@@ -2865,11 +2866,31 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         goto out;
     }
 
-    p2m = p2m_get_hostp2m(currd);
-    mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma, 
+    ap2m_active = altp2m_active(currd);
+
+    /* Take a lock on the host p2m speculatively, to avoid potential
+     * locking order problems later and to handle unshare etc.
+     */

Comment style.

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 3010

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +4 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid lock ordering
+             * violation when the change below is propagated from host p2m */
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty, p2m_ram_rw);

And this won't result in any races?

Also - comment style again (and more elsewhere).

Quoted 8 lines
xen/arch/x86/mm/p2m.c +2 -0
@@ -2037,6 +2037,391 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx)
     return rc;
 }
 
+void p2m_flush_altp2m(struct domain *d)
+{

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2051

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+    uint16_t i;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        p2m_flush_table(d->arch.altp2m_p2m[i]);
+        /* Uninit and reinit ept to force TLB shootdown */
+        ept_p2m_uninit(d->arch.altp2m_p2m[i]);
+        ept_p2m_init(d->arch.altp2m_p2m[i]);

ept_... in non-EPT code again.

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+        d->arch.altp2m_eptp[i] = INVALID_MFN;
+    }
+
+    altp2m_list_unlock(d);
+}
+

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2067

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+static void p2m_init_altp2m_helper(struct domain *d, uint16_t i)
+{
+    struct p2m_domain *p2m = d->arch.altp2m_p2m[i];
+    struct ept_data *ept;
+
+    p2m->min_remapped_gfn = INVALID_GFN;
+    p2m->max_remapped_gfn = INVALID_GFN;
+    ept = &p2m->ept;
+    ept->asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+    d->arch.altp2m_eptp[i] = ept_get_eptp(ept);

Same here.

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2072

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx)
+{
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2092

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+long p2m_init_next_altp2m(struct domain *d, uint16_t *idx)
+{
+    long rc = -EINVAL;
+    uint16_t i;

As in the earlier patch(es) - unsigned int.

Quoted 62 lines
xen/arch/x86/mm/p2m.c +61 -0
+long p2m_change_altp2m_gfn(struct domain *d, uint16_t idx,
+                             gfn_t old_gfn, gfn_t new_gfn)
+{
+    struct p2m_domain *hp2m, *ap2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t mfn;
+    unsigned int page_order;
+    long rc = -EINVAL;
+
+    if ( idx > MAX_ALTP2M || d->arch.altp2m_eptp[idx] == INVALID_MFN )
+        return rc;
+
+    hp2m = p2m_get_hostp2m(d);
+    ap2m = d->arch.altp2m_p2m[idx];
+
+    p2m_lock(ap2m);
+
+    mfn = ap2m->get_entry(ap2m, gfn_x(old_gfn), &t, &a, 0, NULL, NULL);
+
+    if ( gfn_x(new_gfn) == INVALID_GFN )
+    {
+        if ( mfn_valid(mfn) )
+            p2m_remove_page(ap2m, gfn_x(old_gfn), mfn_x(mfn), PAGE_ORDER_4K);
+        rc = 0;
+        goto out;
+    }
+
+    /* Check host p2m if no valid entry in alternate */
+    if ( !mfn_valid(mfn) )
+    {
+        mfn = hp2m->get_entry(hp2m, gfn_x(old_gfn), &t, &a,
+                              P2M_ALLOC | P2M_UNSHARE, &page_order, NULL);
+
+        if ( !mfn_valid(mfn) || t != p2m_ram_rw )
+            goto out;
+
+        /* If this is a superpage, copy that first */
+        if ( page_order != PAGE_ORDER_4K )
+        {
+            gfn_t gfn;
+            unsigned long mask;
+
+            mask = ~((1UL << page_order) - 1);
+            gfn = _gfn(gfn_x(old_gfn) & mask);
+            mfn = _mfn(mfn_x(mfn) & mask);
+
+            if ( ap2m->set_entry(ap2m, gfn_x(gfn), mfn, page_order, t, a, 1) 
 )
+                goto out;
+        }
+    }
+
+    mfn = ap2m->get_entry(ap2m, gfn_x(new_gfn), &t, &a, 0, NULL, NULL);
+
+    if ( !mfn_valid(mfn) )
+        mfn = hp2m->get_entry(hp2m, gfn_x(new_gfn), &t, &a, 0, NULL, NULL);
+
+    if ( !mfn_valid(mfn) || (t != p2m_ram_rw) )
+        goto out;
+
+    if ( !ap2m->set_entry(ap2m, gfn_x(old_gfn), mfn, PAGE_ORDER_4K, t, a,

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2336

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+                          (current->domain != d)) )
+    {
+        rc = 0;
+
+        if ( ap2m->min_remapped_gfn == INVALID_GFN ||
+             gfn_x(new_gfn) < ap2m->min_remapped_gfn )
+            ap2m->min_remapped_gfn = gfn_x(new_gfn);
+        if ( ap2m->max_remapped_gfn == INVALID_GFN ||
+             gfn_x(new_gfn) > ap2m->max_remapped_gfn )
+            ap2m->max_remapped_gfn = gfn_x(new_gfn);

For the purpose here (and without conflict with the consumer side) it would seem to be better to initialize max_remapped_gfn to zero, as then both if() can get away with just one comparison.

Quoted 23 lines
xen/arch/x86/mm/p2m.c +23 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t p2ma)
+{
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2386

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0, NULL);
+
+        reset_p2m = 0;
+
+        /* Check for a dropped page that may impact this altp2m */
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>? I.e. can there a long latency operation result that's this way a guest can effect?

Jan

#36 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 18 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:32 AM

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -2802,10 +2802,11 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2809

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +2 -1
     mfn_t mfn;
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
-    struct p2m_domain *p2m;
+    struct p2m_domain *p2m, *hostp2m;
     int rc, fall_through = 0, paged = 0;
     int sharing_enomem = 0;
     vm_event_request_t *req_ptr = NULL;
+    bool_t ap2m_active = 0;
Quoted 2 lines

Pointless initializer afaict.

True - didn't use to be - will fix.

Quoted 13 lines

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 1 line
xen/arch/x86/hvm/hvm.c +0 -0
@@ -2865,11 +2866,31 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla, · line 2873

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +5 -2
         goto out;
     }
 
-    p2m = p2m_get_hostp2m(currd);
-    mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma,
+    ap2m_active = altp2m_active(currd);
+
+    /* Take a lock on the host p2m speculatively, to avoid potential
+     * locking order problems later and to handle unshare etc.
+     */
Quoted 2 lines

Comment style.

Got it

Quoted 12 lines

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 1 line
xen/arch/x86/hvm/hvm.c +0 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +4 -0
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid lock ordering
+             * violation when the change below is propagated from host p2m */
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,
 p2m_ram_rw);
Quoted 2 lines

And this won't result in any races?

No

Quoted 2 lines

Also - comment style again (and more elsewhere).

Got it

Quoted 7 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct

Quoted 3 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -2037,6 +2037,391 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct

vcpu *v, uint16_t idx)

Quoted 3 lines
xen/arch/x86/mm/p2m.c +0 -0
     return rc;
 }
 
Quoted 14 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2051

Quoted 11 lines
xen/arch/x86/mm/p2m.c +11 -0
+void p2m_flush_altp2m(struct domain *d) {
+    uint16_t i;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        p2m_flush_table(d->arch.altp2m_p2m[i]);
+        /* Uninit and reinit ept to force TLB shootdown */
+        ept_p2m_uninit(d->arch.altp2m_p2m[i]);
+        ept_p2m_init(d->arch.altp2m_p2m[i]);

ept_... in non-EPT code again.

There is no non-EPT altp2m implementation, and this file already includes ept.. callouts for p2m's implemented using EPT's.

Quoted 9 lines
Quoted 9 lines
xen/arch/x86/mm/p2m.c +9 -0
+        d->arch.altp2m_eptp[i] = INVALID_MFN;
+    }
+
+    altp2m_list_unlock(d);
+}
+
+static void p2m_init_altp2m_helper(struct domain *d, uint16_t i) {
+    struct p2m_domain *p2m = d->arch.altp2m_p2m[i];
+    struct ept_data *ept;
Quoted 9 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2067

Quoted 6 lines
xen/arch/x86/mm/p2m.c +6 -0
+
+    p2m->min_remapped_gfn = INVALID_GFN;
+    p2m->max_remapped_gfn = INVALID_GFN;
+    ept = &p2m->ept;
+    ept->asr = pagetable_get_pfn(p2m_get_pagetable(p2m));
+    d->arch.altp2m_eptp[i] = ept_get_eptp(ept);

Same here.

Same as above... this file already includes ept.. callouts for p2m's implemented using EPT's.

Quoted 6 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct · line 2067

Quoted 2 lines
xen/arch/x86/mm/p2m.c +2 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx) {
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

Because the error variable in the code that calls these (in hvm.c) is a long, and you had given feedback earlier to propagate the returns from these functions through that calling code.

Quoted 6 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2092

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+long p2m_init_next_altp2m(struct domain *d, uint16_t *idx) {
+    long rc = -EINVAL;
+    uint16_t i;

As in the earlier patch(es) - unsigned int.

Ok, but why does it matter? uint16_t will always suffice.

Quoted 73 lines
Quoted 14 lines
xen/arch/x86/mm/p2m.c +14 -0
+long p2m_change_altp2m_gfn(struct domain *d, uint16_t idx,
+                             gfn_t old_gfn, gfn_t new_gfn) {
+    struct p2m_domain *hp2m, *ap2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t mfn;
+    unsigned int page_order;
+    long rc = -EINVAL;
+
+    if ( idx > MAX_ALTP2M || d->arch.altp2m_eptp[idx] == INVALID_MFN )
+        return rc;
+
+    hp2m = p2m_get_hostp2m(d);
+    ap2m = d->arch.altp2m_p2m[idx];

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx)

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+
+    p2m_lock(ap2m);
+
+    mfn = ap2m->get_entry(ap2m, gfn_x(old_gfn), &t, &a, 0, NULL,
+ NULL);
+
+    if ( gfn_x(new_gfn) == INVALID_GFN )
+    {
+        if ( mfn_valid(mfn) )
+            p2m_remove_page(ap2m, gfn_x(old_gfn), mfn_x(mfn),

PAGE_ORDER_4K);

Quoted 48 lines
xen/arch/x86/mm/p2m.c +47 -0
+        rc = 0;
+        goto out;
+    }
+
+    /* Check host p2m if no valid entry in alternate */
+    if ( !mfn_valid(mfn) )
+    {
+        mfn = hp2m->get_entry(hp2m, gfn_x(old_gfn), &t, &a,
+                              P2M_ALLOC | P2M_UNSHARE, &page_order,
+ NULL);
+
+        if ( !mfn_valid(mfn) || t != p2m_ram_rw )
+            goto out;
+
+        /* If this is a superpage, copy that first */
+        if ( page_order != PAGE_ORDER_4K )
+        {
+            gfn_t gfn;
+            unsigned long mask;
+
+            mask = ~((1UL << page_order) - 1);
+            gfn = _gfn(gfn_x(old_gfn) & mask);
+            mfn = _mfn(mfn_x(mfn) & mask);
+
+            if ( ap2m->set_entry(ap2m, gfn_x(gfn), mfn, page_order,
+ t, a, 1)
 )
+                goto out;
+        }
+    }
+
+    mfn = ap2m->get_entry(ap2m, gfn_x(new_gfn), &t, &a, 0, NULL,
+ NULL);
+
+    if ( !mfn_valid(mfn) )
+        mfn = hp2m->get_entry(hp2m, gfn_x(new_gfn), &t, &a, 0, NULL,
+ NULL);
+
+    if ( !mfn_valid(mfn) || (t != p2m_ram_rw) )
+        goto out;
+
+    if ( !ap2m->set_entry(ap2m, gfn_x(old_gfn), mfn, PAGE_ORDER_4K, t, a,
+                          (current->domain != d)) )
+    {
+        rc = 0;
+
+        if ( ap2m->min_remapped_gfn == INVALID_GFN ||
+             gfn_x(new_gfn) < ap2m->min_remapped_gfn )
Quoted 9 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2336

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+            ap2m->min_remapped_gfn = gfn_x(new_gfn);
+        if ( ap2m->max_remapped_gfn == INVALID_GFN ||
+             gfn_x(new_gfn) > ap2m->max_remapped_gfn )
+            ap2m->max_remapped_gfn = gfn_x(new_gfn);

For the purpose here (and without conflict with the consumer side) it would seem to be better to initialize max_remapped_gfn to zero, as then both if() can get away with just one comparison.

See below for a full explanation of this portion and why max_remapped_gfn cannot be inited to 0 (zero is a valid gfn)...

Quoted 29 lines
Quoted 29 lines
xen/arch/x86/mm/p2m.c +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t p2ma)
+{
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0, NULL);
+
+        reset_p2m = 0;
+
+        /* Check for a dropped page that may impact this altp2m */
Quoted 9 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2386

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>? I.e.
can there a long latency operation result that's this way a guest can effect?

... A p2m is a gfn->mfn map, amongst other things. There is a reverse mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple: do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently valid.

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the invalidate logic is: record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Ravi

Quoted 1 line

Jan

#37 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 13 lines
Quoted 10 lines
Quoted 1 line · On 16.07.15 at 11:16, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:32 AM

Review int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +1 -0
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid lock 

ordering

Quoted 1 line

Review int hvm_hap_nested_page_fault(paddr_t gpa, · line 3007

Quoted 1 line
(quoted diff) +1 -0
+             * violation when the change below is propagated from host p2m 

*/

Quoted 8 lines
Quoted 6 lines

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -0
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,
 p2m_ram_rw);

And this won't result in any races?

No

To be honest I expected a little more than just "no" here. Now I have to ask - why?

Quoted 22 lines
Quoted 7 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct

Quoted 3 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -2037,6 +2037,391 @@ bool_t p2m_switch_vcpu_altp2m_by_id(struct

vcpu *v, uint16_t idx)

Quoted 3 lines
xen/arch/x86/mm/p2m.c +0 -0
     return rc;
 }
 
Quoted 14 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2051

Quoted 11 lines
xen/arch/x86/mm/p2m.c +11 -0
+void p2m_flush_altp2m(struct domain *d) {
+    uint16_t i;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        p2m_flush_table(d->arch.altp2m_p2m[i]);
+        /* Uninit and reinit ept to force TLB shootdown */
+        ept_p2m_uninit(d->arch.altp2m_p2m[i]);
+        ept_p2m_init(d->arch.altp2m_p2m[i]);

ept_... in non-EPT code again.

Quoted 2 lines

There is no non-EPT altp2m implementation, and this file already includes ept.. callouts for p2m's implemented using EPT's.

The only two calls currently there are ept_p2m_{,un}init(), which need to be there with the current code structuring. Everything else that's EPT-specific should be abstracted through hooks set by ept_p2m_init().

Quoted 9 lines
Quoted 5 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct · line 2052

Quoted 2 lines
xen/arch/x86/mm/p2m.c +2 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx) {
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

Because the error variable in the code that calls these (in hvm.c) is a long, and you had given feedback earlier to propagate the returns from these functions through that calling code.

I don't see the connection. The function only returns zero or -E... values, so why would its return type be "long"?

Quoted 7 lines
Quoted 5 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2092

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+long p2m_init_next_altp2m(struct domain *d, uint16_t *idx) {
+    long rc = -EINVAL;
+    uint16_t i;

As in the earlier patch(es) - unsigned int.

Ok, but why does it matter? uint16_t will always suffice.

And will always produce worse code.

Quoted 47 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/x86/mm/p2m.c +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t p2ma)
+{
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0, NULL);
+
+        reset_p2m = 0;
+
+        /* Check for a dropped page that may impact this altp2m */
Quoted 9 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2386

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>? I.e.
can there a long latency operation result that's this way a guest can effect?

... A p2m is a gfn->mfn map, amongst other things. There is a reverse mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple: do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently valid.

Quoted 8 lines

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the invalidate logic is:
record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Sadly it doesn't, it just re-states what I already understood and doesn't answer the question: What happens if min=0 and max=<end-of-address-space>? I.e. can the guest nullify the optimization by careful fiddling issuing some of the new hypercalls, and if so will this have any negative impact on the hypervisor? I'm asking this from a security standpoint ...

Nor do I find my question answered why max can't be initialized to zero:
You don't care whether max is a valid GFN when a certain GFN doesn't fall in the (then empty) [min, max] range. What am I missing?

Jan

#38 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 23 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:34 AM

Quoted 20 lines
Quoted 11 lines
Quoted 1 line · On 16.07.15 at 11:16, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:32 AM

Review int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid
+ lock

ordering

Quoted 2 lines

Review int hvm_hap_nested_page_fault(paddr_t gpa, · line 3009

Quoted 2 lines
(quoted diff) +2 -0
+             * violation when the change below is propagated from
+ host p2m

*/

Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -0
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,
 p2m_ram_rw);
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

And this won't result in any races?

No

To be honest I expected a little more than just "no" here. Now I have to ask - why?

Yes, I should have described it more than that :-) so this part of the code is handling the log dirty transition of the page, and this page permission transition happens always on the hostp2m. Given the way the locking order is setup (hostp2m->altp2m-list-lock->altp2m and there was a separate writeup and discussion with George on that), at this point in this sequence there is a p2m lock (whether it's a hostp2m or altp2m lock depends on the mode of the domain) - the reason we have to drop the lock here first is due to what happens next; the permission changes in hostp2m will be serially propagated to altp2ms and not dropping the lock here would cause a locking order violation. Hope that clarifies.

Quoted 23 lines
Quoted 3 lines
Quoted 3 lines

xen/arch/x86/mm/p2m.c bool_t

Quoted 3 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -2037,6 +2037,391 @@ bool_t

p2m_switch_vcpu_altp2m_by_id(struct

Quoted 19 lines
Quoted 18 lines

vcpu *v, uint16_t idx)

Quoted 4 lines
xen/arch/x86/mm/p2m.c +1 -0
     return rc;
 }
 
+void p2m_flush_altp2m(struct domain *d) {

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2051

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+    uint16_t i;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        p2m_flush_table(d->arch.altp2m_p2m[i]);
+        /* Uninit and reinit ept to force TLB shootdown */
+        ept_p2m_uninit(d->arch.altp2m_p2m[i]);
+        ept_p2m_init(d->arch.altp2m_p2m[i]);

ept_... in non-EPT code again.

Quoted 7 lines
Quoted 2 lines

There is no non-EPT altp2m implementation, and this file already includes ept.. callouts for p2m's implemented using EPT's.

The only two calls currently there are ept_p2m_{,un}init(), which need to be there with the current code structuring. Everything else that's EPT-specific should be abstracted through hooks set by ept_p2m_init().

We hear your input on this one - we would like to treat this change as a Category 2 change (per the preface message I sent earlier). I hope that's acceptable - also we will prioritize this change above the other change you suggested on the altp2m data structure reorganization in patch 5/15.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

xen/arch/x86/mm/p2m.c bool_t · line 2052

Quoted 2 lines
xen/arch/x86/mm/p2m.c +2 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx) {
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

Quoted 7 lines
Quoted 3 lines

Because the error variable in the code that calls these (in hvm.c) is a long, and you had given feedback earlier to propagate the returns from these functions through that calling code.

I don't see the connection. The function only returns zero or -E... values, so why would its return type be "long"?

do_hvm_op declares a rc that is of type "long" and hence this returns a "long"

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2092

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+long p2m_init_next_altp2m(struct domain *d, uint16_t *idx) {
+    long rc = -EINVAL;
+    uint16_t i;

As in the earlier patch(es) - unsigned int.

Ok, but why does it matter? uint16_t will always suffice.

And will always produce worse code.

Ok.

Quoted 57 lines
Quoted 47 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/x86/mm/p2m.c +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t
+p2ma) {
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0, NULL);
+
+        reset_p2m = 0;
+
+        /* Check for a dropped page that may impact this altp2m */
Quoted 9 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2386

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>? I.e.
can there a long latency operation result that's this way a guest can effect?

... A p2m is a gfn->mfn map, amongst other things. There is a reverse mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple: do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently valid.

Quoted 2 lines

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the invalidate

logic is:

Quoted 6 lines

record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Quoted 6 lines

Sadly it doesn't, it just re-states what I already understood and doesn't answer the question: What happens if min=0 and max=<end-of-address- space>? I.e. can the guest nullify the optimization by careful fiddling issuing some of the new hypercalls, and if so will this have any negative impact on the hypervisor? I'm asking this from a security standpoint ...

To take that exact case, If min=0 and max=<end of address space> then any hostp2m change where the first mfn is dropped, will cause all altp2ms to be reset even if the mfn dropped doesn't affect altp2ms at all, which wont serve as an optimization at all - Hope that clarifies.

Quoted 3 lines

Nor do I find my question answered why max can't be initialized to zero:
You don't care whether max is a valid GFN when a certain GFN doesn't fall in the (then empty) [min, max] range. What am I missing?

Since 0 is a valid GFN so we cannot initialize min or max to 0 - since matching this condition (gfn_x(gfn) >= p2m->min_remapped_gfn && gfn_x(gfn) <= p2m->max_remapped_gfn) will cause a reset (throw away) of the altp2m to rebuild it from the hostp2m. So essentially what is being done here is the range is the non-existent set to start with, unless some altp2m changes occur, and then it is grown to be the smallest set around the gfns affected.

Ravi

Quoted 2 lines

Jan

#39 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 32 lines
Quoted 24 lines
Quoted 1 line · On 18.07.15 at 00:32, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:34 AM

Quoted 20 lines
Quoted 11 lines
Quoted 1 line · On 16.07.15 at 11:16, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:32 AM

Review int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid
+ lock

ordering

Quoted 2 lines

Review int hvm_hap_nested_page_fault(paddr_t gpa, · line 3009

Quoted 2 lines
(quoted diff) +2 -0
+             * violation when the change below is propagated from
+ host p2m

*/

Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -0
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,
 p2m_ram_rw);
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

And this won't result in any races?

No

To be honest I expected a little more than just "no" here. Now I have to ask - why?

Quoted 10 lines

Yes, I should have described it more than that :-) so this part of the code is handling the log dirty transition of the page, and this page permission transition happens always on the hostp2m. Given the way the locking order is setup (hostp2m->altp2m-list-lock->altp2m and there was a separate writeup and discussion with George on that), at this point in this sequence there is a p2m lock (whether it's a hostp2m or altp2m lock depends on the mode of the domain) - the reason we have to drop the lock here first is due to what happens next; the permission changes in hostp2m will be serially propagated to altp2ms and not dropping the lock here would cause a locking order violation. Hope that clarifies.

Sadly it doesn't at all: You re-explain why you need to drop the lock, while you fail to say anything on why this won't cause a race.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Review int hvm_hap_nested_page_fault(paddr_t gpa, · line 3015

Quoted 2 lines
(quoted diff) +2 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx) {
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

Because the error variable in the code that calls these (in hvm.c) is a long, and you had given feedback earlier to propagate the returns from these functions through that calling code.

Quoted 6 lines
Quoted 3 lines

I don't see the connection. The function only returns zero or -E... values, so why would its return type be "long"?

do_hvm_op declares a rc that is of type "long" and hence this returns a "long"

What type your caller(s) return is of no interest at all here: What would you do if you had multiple callers with differing return types? A function's return type should be chosen based on the range of values it may return, and the result possibly widened to not yield inefficient code (like in some of the uint16_t cases elsewhere in the series would be necessary).

Quoted 62 lines
Quoted 57 lines
Quoted 47 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/x86/mm/p2m.c +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t
+p2ma) {
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0, NULL);
+
+        reset_p2m = 0;
+
+        /* Check for a dropped page that may impact this altp2m */
Quoted 9 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2386

Quoted 4 lines
xen/arch/x86/mm/p2m.c +4 -0
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>? I.e.
can there a long latency operation result that's this way a guest can effect?

... A p2m is a gfn->mfn map, amongst other things. There is a reverse mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple: do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently valid.

Quoted 2 lines

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the invalidate

logic is:

Quoted 6 lines

record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Quoted 3 lines

Sadly it doesn't, it just re-states what I already understood and doesn't answer the question: What happens if min=0 and max=<end-of-address- space>? I.e. can the guest nullify the optimization by careful fiddling

issuing

Quoted 1 line

some of the new hypercalls, and if so will this have any negative impact on

Quoted 8 lines

the

Quoted 2 lines

hypervisor? I'm asking this from a security standpoint ...

To take that exact case, If min=0 and max=<end of address space> then any hostp2m change where the first mfn is dropped, will cause all altp2ms to be reset even if the mfn dropped doesn't affect altp2ms at all, which wont serve as an optimization at all - Hope that clarifies.

Again - no. I understand the optimization is gone then. But what's the effect? I.e. will the guest, by extending this range to be arbitrarily wide, be able to cause a long latency hypervisor operation (i.e. a DoS)?

Quoted 10 lines
Quoted 3 lines

Nor do I find my question answered why max can't be initialized to zero:
You don't care whether max is a valid GFN when a certain GFN doesn't fall in the (then empty) [min, max] range. What am I missing?

Since 0 is a valid GFN so we cannot initialize min or max to 0 - since matching this condition (gfn_x(gfn) >= p2m->min_remapped_gfn && gfn_x(gfn) <= p2m->max_remapped_gfn) will cause a reset (throw away) of the altp2m to rebuild it from the hostp2m. So essentially what is being done here is the range is the non-existent set to start with, unless some altp2m changes occur, and then it is grown to be the smallest set around the gfns affected.

Again you just re-state what was already clear, yet you neglect answering the actual question. Taking what you wrote above, when max=0 (and min=INVALID_GFN), then

	gfn_x(gfn) >= p2m->min_remapped_gfn &&
	 gfn_x(gfn) <= p2m->max_remapped_gfn

will be false for any value of gfn; in fact the "max" part won't even be looked at because the "min" part will already be false for any valid gfn, i.e. only the INVALID_GFN case would make it to the "max" part.

Jan

#40 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 48 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Sunday, July 19, 2015 11:53 PM

Quoted 9 lines
Quoted 9 lines
Quoted 1 line · On 18.07.15 at 00:32, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:34 AM

Quoted 5 lines
Quoted 5 lines
Quoted 1 line · On 16.07.15 at 11:16, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:32 AM

Review int hvm_hap_nested_page_fault(paddr_t

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t

gpa,

Quoted 23 lines
Quoted 15 lines
Quoted 15 lines
Quoted 6 lines

unsigned long gla,

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2 -0
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid
+ lock

ordering

Quoted 2 lines

Review int hvm_hap_nested_page_fault(paddr_t · line 3009

Quoted 2 lines
(quoted diff) +2 -0
+             * violation when the change below is propagated from
+ host p2m

*/

Quoted 5 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +2 -0
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,
 p2m_ram_rw);
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

And this won't result in any races?

No

To be honest I expected a little more than just "no" here. Now I have to ask - why?

Quoted 11 lines

Yes, I should have described it more than that :-) so this part of the code is handling the log dirty transition of the page, and this page permission transition happens always on the hostp2m. Given the way the locking order is setup (hostp2m->altp2m-list-lock->altp2m and there was a separate writeup and discussion with George on that), at this point in this sequence there is a p2m lock (whether it's a hostp2m or altp2m lock depends on the mode of the
domain) - the reason we have to drop the lock here first is due to what happens next; the permission changes in hostp2m will be serially propagated to altp2ms and not dropping the lock here would cause a locking order violation. Hope that clarifies.

Quoted 3 lines

Sadly it doesn't at all: You re-explain why you need to drop the lock, while you fail to say anything on why this won't cause a race.

It only drops the lock on the altp2m, which is no longer required in this function anyway. The important aspect is that there is still a lock held on the host p2m, and that is dropped after the log-dirty updates, as it would be in the non-altp2m case (maybe that was the part I should have explained clearly in the para above). Does that clarify or do you see a particular race condition here? (We don't ).

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 9 lines
Quoted 5 lines

Review int hvm_hap_nested_page_fault(paddr_t · line 3015

Quoted 2 lines
(quoted diff) +2 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx) {
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

Because the error variable in the code that calls these (in hvm.c) is a long, and you had given feedback earlier to propagate the returns from these functions through that calling code.

I don't see the connection. The function only returns zero or -E... values, so why would its return type be "long"?

Quoted 9 lines
Quoted 2 lines

do_hvm_op declares a rc that is of type "long" and hence this returns a "long"

What type your caller(s) return is of no interest at all here: What would you do if you had multiple callers with differing return types?
A function's return type should be chosen based on the range of values it may return, and the result possibly widened to not yield inefficient code (like in some of the uint16_t cases elsewhere in the series would be necessary).

What do you suggest the return type be?

Quoted 75 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/x86/mm/p2m.c +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t
+p2ma) {
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0,
+ NULL);
+
+        reset_p2m = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/mm/p2m.c bool_t p2m_switch_vcpu_altp2m_by_id(struct vcpu *v, uint16_t idx) · line 2386

Quoted 5 lines
xen/arch/x86/mm/p2m.c +5 -0
+        /* Check for a dropped page that may impact this altp2m */
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>?

I.e.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

can there a long latency operation result that's this way a guest can

effect?

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

... A p2m is a gfn->mfn map, amongst other things. There is a reverse
mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple:
do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently

valid.

Quoted 24 lines
Quoted 12 lines
Quoted 4 lines

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the ->invalidate

logic is:

Quoted 6 lines

record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Quoted 5 lines

Sadly it doesn't, it just re-states what I already understood and doesn't answer the question: What happens if min=0 and max=<end-of-address-
space>? I.e. can the guest nullify the optimization by careful space>fiddling

issuing

Quoted 2 lines

some of the new hypercalls, and if so will this have any negative impact on

the

Quoted 2 lines

hypervisor? I'm asking this from a security standpoint ...

Quoted 9 lines
Quoted 4 lines

To take that exact case, If min=0 and max=<end of address space> then any hostp2m change where the first mfn is dropped, will cause all altp2ms to be reset even if the mfn dropped doesn't affect altp2ms at all, which wont serve as an optimization at all - Hope that clarifies.

Again - no. I understand the optimization is gone then. But what's the effect? I.e. will the guest, by extending this range to be arbitrarily wide, be able to cause a long latency hypervisor operation (i.e. a DoS)?

The extent of the range affects the likelihood of an invalidation. It has no impact on the cost of an invalidation (so no its not a DoS issue). I'm not sure what change you are suggesting here or just clarification (if you think this optimization is confusing perhaps some documentation of this optimization will help?)

Quoted 18 lines
Quoted 4 lines
Quoted 3 lines

Nor do I find my question answered why max can't be initialized to zero:
You don't care whether max is a valid GFN when a certain GFN doesn't fall in the (then empty) [min, max] range. What am I missing?

Quoted 8 lines

Since 0 is a valid GFN so we cannot initialize min or max to 0 - since matching this condition (gfn_x(gfn) >= p2m->min_remapped_gfn && gfn_x(gfn) <=
p2m->max_remapped_gfn) will cause a reset (throw away) of the altp2m p2m->to
rebuild it from the hostp2m. So essentially what is being done here is the range is the non-existent set to start with, unless some altp2m changes occur, and then it is grown to be the smallest set around the gfns

affected.

Again you just re-state what was already clear, yet you neglect answering the actual question. Taking what you wrote above, when max=0 (and min=INVALID_GFN), then

Quoted 7 lines
	gfn_x(gfn) >= p2m->min_remapped_gfn &&
	 gfn_x(gfn) <= p2m->max_remapped_gfn

will be false for any value of gfn; in fact the "max" part won't even be looked at because the "min" part will already be false for any valid gfn, i.e. only the INVALID_GFN case would make it to the "max" part.

You are suggesting an alternative which will work, but what we have will also produce the same results, and the results are correct for both cases - so can we go with the approach we have taken currently?

Ravi

Quoted 1 line

Jan

#41 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 52 lines
Quoted 47 lines
Quoted 1 line · On 21.07.15 at 07:46, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Sunday, July 19, 2015 11:53 PM

Quoted 32 lines
Quoted 24 lines
Quoted 1 line · On 18.07.15 at 00:32, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Thursday, July 16, 2015 2:34 AM

Quoted 20 lines
Quoted 11 lines
Quoted 1 line · On 16.07.15 at 11:16, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:32 AM

Review int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

Quoted 5 lines
(quoted diff) +2 -0
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid
+ lock

ordering

Quoted 2 lines

Review int hvm_hap_nested_page_fault(paddr_t gpa, · line 3009

Quoted 2 lines
(quoted diff) +2 -0
+             * violation when the change below is propagated from
+ host p2m

*/

Quoted 5 lines
Quoted 4 lines
(quoted diff) +2 -0
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,
 p2m_ram_rw);
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

And this won't result in any races?

No

To be honest I expected a little more than just "no" here. Now I have to ask - why?

Quoted 11 lines

Yes, I should have described it more than that :-) so this part of the code is handling the log dirty transition of the page, and this page permission transition happens always on the hostp2m. Given the way the locking order is setup (hostp2m->altp2m-list-lock->altp2m and there was a separate writeup and discussion with George on that), at this point in this sequence there is a p2m lock (whether it's a hostp2m or altp2m lock depends on the mode of the
domain) - the reason we have to drop the lock here first is due to what happens next; the permission changes in hostp2m will be serially propagated to altp2ms and not dropping the lock here would cause a locking order violation. Hope that clarifies.

Quoted 1 line

Sadly it doesn't at all: You re-explain why you need to drop the lock, while

you

Quoted 2 lines

fail to say anything on why this won't cause a race.

Quoted 6 lines

It only drops the lock on the altp2m, which is no longer required in this function anyway. The important aspect is that there is still a lock held on the host p2m, and that is dropped after the log-dirty updates, as it would be in the non-altp2m case (maybe that was the part I should have explained clearly in the para above). Does that clarify or do you see a particular race condition here? (We don't ).

Sounds okay then.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Review int hvm_hap_nested_page_fault(paddr_t gpa, · line 3015

Quoted 2 lines
(quoted diff) +2 -0
+long p2m_init_altp2m_by_id(struct domain *d, uint16_t idx) {
+    long rc = -EINVAL;

Why long (for both variable and function return type)? (More of these in functions below.)

Because the error variable in the code that calls these (in hvm.c) is a long, and you had given feedback earlier to propagate the returns from these functions through that calling code.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I don't see the connection. The function only returns zero or -E... values, so why would its return type be "long"?

do_hvm_op declares a rc that is of type "long" and hence this returns a "long"

What type your caller(s) return is of no interest at all here: What would you

do

Quoted 8 lines
Quoted 3 lines

if you had multiple callers with differing return types?
A function's return type should be chosen based on the range of values it may return, and the result possibly widened to not yield inefficient code (like

in

Quoted 2 lines

some of the uint16_t cases elsewhere in the series would be necessary).

What do you suggest the return type be?

For the case here - int (quite obviously I would say).

For the uint16_t ones - unsigned int.

Quoted 83 lines
Quoted 75 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
(quoted diff) +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t
+p2ma) {
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0,
+ NULL);
+
+        reset_p2m = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+        /* Check for a dropped page that may impact this altp2m */
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-space>?

I.e.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

can there a long latency operation result that's this way a guest can

effect?

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

... A p2m is a gfn->mfn map, amongst other things. There is a reverse
mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple:
do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently

valid.

Quoted 24 lines
Quoted 12 lines
Quoted 4 lines

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the ->invalidate

logic is:

Quoted 6 lines

record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Quoted 5 lines

Sadly it doesn't, it just re-states what I already understood and doesn't answer the question: What happens if min=0 and max=<end-of-address-
space>? I.e. can the guest nullify the optimization by careful space>fiddling

issuing

Quoted 2 lines

some of the new hypercalls, and if so will this have any negative impact on

the

Quoted 2 lines

hypervisor? I'm asking this from a security standpoint ...

Quoted 7 lines
Quoted 4 lines

To take that exact case, If min=0 and max=<end of address space> then any hostp2m change where the first mfn is dropped, will cause all altp2ms to be reset even if the mfn dropped doesn't affect altp2ms at all, which wont serve as an optimization at all - Hope that clarifies.

Again - no. I understand the optimization is gone then. But what's the effect? I.e. will the guest, by extending this range to be arbitrarily wide, be able

to

Quoted 8 lines
Quoted 2 lines

cause a long latency hypervisor operation (i.e. a DoS)?

The extent of the range affects the likelihood of an invalidation. It has no impact on the cost of an invalidation (so no its not a DoS issue). I'm not sure what change you are suggesting here or just clarification (if you think this optimization is confusing perhaps some documentation of this optimization will help?)

Well, the optimization must be optimizing _something_. And hence _something_ must go sub-optimal when the optimization is being subverted. And the question is how much worse un-optimized is compared to optimized.

It _looks like_ the overall effect really is just to avoid a one time (for a given non-preemptible operation) reset, but whether that's really the case depends on the calling contexts (which, as said a couple of times before, is hard to see for a patch that introduces functions without callers - hence the question).

Quoted 23 lines
Quoted 14 lines
Quoted 4 lines
Quoted 3 lines

Nor do I find my question answered why max can't be initialized to zero:
You don't care whether max is a valid GFN when a certain GFN doesn't fall in the (then empty) [min, max] range. What am I missing?

Quoted 8 lines

Since 0 is a valid GFN so we cannot initialize min or max to 0 - since matching this condition (gfn_x(gfn) >= p2m->min_remapped_gfn && gfn_x(gfn) <=
p2m->max_remapped_gfn) will cause a reset (throw away) of the altp2m p2m->to
rebuild it from the hostp2m. So essentially what is being done here is the range is the non-existent set to start with, unless some altp2m changes occur, and then it is grown to be the smallest set around the gfns

affected.

Quoted 8 lines

Again you just re-state what was already clear, yet you neglect answering the actual question. Taking what you wrote above, when max=0 (and min=INVALID_GFN), then

	gfn_x(gfn) >= p2m->min_remapped_gfn &&
	 gfn_x(gfn) <= p2m->max_remapped_gfn

will be false for any value of gfn; in fact the "max" part won't even be

looked

Quoted 8 lines
Quoted 1 line

at because the "min" part will already be false for any valid gfn, i.e. only

the

Quoted 2 lines

INVALID_GFN case would make it to the "max" part.

You are suggesting an alternative which will work, but what we have will also produce the same results, and the results are correct for both cases - so can we go with the approach we have taken currently?

Of course we can, but should we? I.e. why use sub-optimal code when there clearly is a way to improve it? Counter question - why are you insisting to use a model requiring (in the earlier pointed out place) four comparisons when two can do? I realize this is only a small inefficiency here, but you should realize that they add up if we don't look to avoid them where possible.

Jan

#42 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 101 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Monday, July 20, 2015 11:38 PM

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
(quoted diff) +29 -0
+void p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
+                                 mfn_t mfn, unsigned int page_order,
+                                 p2m_type_t p2mt, p2m_access_t
+p2ma) {
+    struct p2m_domain *p2m;
+    p2m_access_t a;
+    p2m_type_t t;
+    mfn_t m;
+    uint16_t i;
+    bool_t reset_p2m;
+    unsigned int reset_count = 0;
+    uint16_t last_reset_idx = ~0;
+
+    if ( !altp2m_active(d) )
+        return;
+
+    altp2m_list_lock(d);
+
+    for ( i = 0; i < MAX_ALTP2M; i++ )
+    {
+        if ( d->arch.altp2m_eptp[i] == INVALID_MFN )
+            continue;
+
+        p2m = d->arch.altp2m_p2m[i];
+        m = get_gfn_type_access(p2m, gfn_x(gfn), &t, &a, 0,
+ NULL);
+
+        reset_p2m = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+        /* Check for a dropped page that may impact this altp2m */
+        if ( mfn_x(mfn) == INVALID_MFN &&
+             gfn_x(gfn) >= p2m->min_remapped_gfn &&
+             gfn_x(gfn) <= p2m->max_remapped_gfn )
+            reset_p2m = 1;

Considering that this looks like an optimization, what's the downside of possibly having min=0 and max=<end-of-address-

space>?

Quoted 36 lines
Quoted 36 lines

I.e.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

can there a long latency operation result that's this way a guest can

effect?

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

... A p2m is a gfn->mfn map, amongst other things. There is a reverse
mfn->gfn map, but that is only valid for the host p2m. Unless the remap altp2m hypercall is used, the gfn->mfn map in every altp2m mirrors the gfn->mfn map in the host p2m (or a subset thereof, due to lazy-copy), so handling removal of an mfn from a guest is simple:
do a reverse look up for the host p2m and mark the relevant gfn as invalid, then do the same for every altp2m where that gfn is currently

valid.

Quoted 20 lines
Quoted 12 lines
Quoted 4 lines

Remap changes things: it says take gfn1 and replace ->mfn with the ->mfn of gfn2. Here is where the optimization is used and the ->invalidate

logic is:

Quoted 6 lines

record the lowest and highest gfn2's that have been used in remap ops; if an mfn is dropped from the hostp2m, for the purposes of altp2m invalidation, see if the gfn derived from the host p2m reverse lookup falls within the range of used gfn2's. If it does, an invalidation is required. Which is why min and max are inited the way they are - hope the explanation clarifies this optimization.

Quoted 5 lines

Sadly it doesn't, it just re-states what I already understood and doesn't answer the question: What happens if min=0 and max=<end-of-address-
space>? I.e. can the guest nullify the optimization by careful space>fiddling

issuing

Quoted 2 lines

some of the new hypercalls, and if so will this have any negative impact on

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

the

Quoted 2 lines

hypervisor? I'm asking this from a security standpoint ...

To take that exact case, If min=0 and max=<end of address space> then any hostp2m change where the first mfn is dropped, will cause all altp2ms to be reset even if the mfn dropped doesn't affect altp2ms at all, which wont serve as an optimization at all - Hope that

clarifies.

Quoted 2 lines
Quoted 2 lines

Again - no. I understand the optimization is gone then. But what's the

effect?

Quoted 11 lines
Quoted 2 lines

I.e. will the guest, by extending this range to be arbitrarily wide, be able

to

Quoted 2 lines

cause a long latency hypervisor operation (i.e. a DoS)?

The extent of the range affects the likelihood of an invalidation. It has no impact on the cost of an invalidation (so no its not a DoS issue). I'm not sure what change you are suggesting here or just clarification (if you think this optimization is confusing perhaps some documentation of this optimization will help?)

Quoted 9 lines

Well, the optimization must be optimizing _something_. And hence _something_ must go sub-optimal when the optimization is being subverted. And the question is how much worse un-optimized is compared to optimized.

It _looks like_ the overall effect really is just to avoid a one time (for a given non-preemptible operation) reset, but whether that's really the case depends on the calling contexts (which, as said a couple of times before, is hard to see for a patch that introduces functions without callers - hence the question).

As you now understand, invalidating an altp2m effectively resets it to be a (lazily-copied) exact duplicate of the host p2m again -- so losing any altp2m permissions restrictions or remaps. This is a first cut at minimizing the likelihood of that happening unnecessarily. There's some discussion on this first cut between Tim and Ed going back to February or March. The intention continues to be that this might be further optimized if real-world use shows this first cut to be inefficient.

Quoted 36 lines
Quoted 28 lines
Quoted 20 lines
Quoted 4 lines
Quoted 3 lines

Nor do I find my question answered why max can't be initialized to zero:
You don't care whether max is a valid GFN when a certain GFN doesn't fall in the (then empty) [min, max] range. What am I missing?

Quoted 10 lines

Since 0 is a valid GFN so we cannot initialize min or max to 0 - since matching this condition (gfn_x(gfn) >= p2m->min_remapped_gfn &&
gfn_x(gfn) <=
p2m->max_remapped_gfn) will cause a reset (throw away) of the altp2m p2m->to
rebuild it from the hostp2m. So essentially what is being done here is the range is the non-existent set to start with, unless some altp2m changes occur, and then it is grown to be the smallest set around the gfns

affected.

Again you just re-state what was already clear, yet you neglect answering the actual question. Taking what you wrote above, when max=0 (and min=INVALID_GFN), then

Quoted 5 lines
	gfn_x(gfn) >= p2m->min_remapped_gfn &&
	 gfn_x(gfn) <= p2m->max_remapped_gfn

will be false for any value of gfn; in fact the "max" part won't even be

looked

Quoted 2 lines

at because the "min" part will already be false for any valid gfn, i.e. only

Quoted 7 lines

the

Quoted 2 lines

INVALID_GFN case would make it to the "max" part.

You are suggesting an alternative which will work, but what we have will also produce the same results, and the results are correct for both cases - so can we go with the approach we have taken currently?

Quoted 6 lines

Of course we can, but should we? I.e. why use sub-optimal code when there clearly is a way to improve it? Counter question - why are you insisting to use a model requiring (in the earlier pointed out
place) four comparisons when two can do? I realize this is only a small inefficiency here, but you should realize that they add up if we don't look to avoid them where possible.

We thought the code was easier to understand with min and max set to INVALID. we can take your approach to avoid this inefficiency if you want.

Ravi

Quoted 2 lines

Jan

#43 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 21.07.15 at 20:33, <[email protected]> wrote:

We thought the code was easier to understand with min and max set to INVALID.

But as you see it caused a lot of discussion instead.

Quoted 1 line

we can take your approach to avoid this inefficiency if you want.

Yes please.

Jan

#44 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

In reply to Ed White
Quoted 208 lines · Ed White

Add the remaining routines required to support enabling the alternate p2m functionality.

---
 xen/arch/x86/hvm/hvm.c           |  58 +++++-
 xen/arch/x86/mm/hap/Makefile     |   1 +
 xen/arch/x86/mm/hap/altp2m_hap.c |  98 ++++++++++
 xen/arch/x86/mm/p2m-ept.c        |   3 +
 xen/arch/x86/mm/p2m.c            | 385 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/altp2m.h |   4 +
 xen/include/asm-x86/p2m.h        |  33 ++++
 7 files changed, 576 insertions(+), 6 deletions(-)
 create mode 100644 xen/arch/x86/mm/hap/altp2m_hap.c
xen/arch/x86/hvm/hvm.c +52 -6
index 38deedc..a9f4b1b 100644
@@ -2802,10 +2802,11 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     mfn_t mfn;
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
-    struct p2m_domain *p2m;
+    struct p2m_domain *p2m, *hostp2m;
     int rc, fall_through = 0, paged = 0;
     int sharing_enomem = 0;
     vm_event_request_t *req_ptr = NULL;
+    bool_t ap2m_active = 0;
 
     /* On Nested Virtualization, walk the guest page table.
      * If this succeeds, all is fine.
@@ -2865,11 +2866,31 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         goto out;
     }
 
-    p2m = p2m_get_hostp2m(currd);
-    mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma,
+    ap2m_active = altp2m_active(currd);
+
+    /* Take a lock on the host p2m speculatively, to avoid potential
+     * locking order problems later and to handle unshare etc.
+     */
+    hostp2m = p2m_get_hostp2m(currd);
+    mfn = get_gfn_type_access(hostp2m, gfn, &p2mt, &p2ma,
                               P2M_ALLOC | (npfec.write_access ? P2M_UNSHARE : 0),
                               NULL);
 
+    if ( ap2m_active )
+    {
+        if ( altp2m_hap_nested_page_fault(curr, gpa, gla, npfec, &p2m) == 1 )
+        {
+            /* entry was lazily copied from host -- retry */
+            __put_gfn(hostp2m, gfn);
+            rc = 1;
+            goto out;
+        }
+
+        mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma, 0, NULL);
+    }
+    else
+        p2m = hostp2m;
+
     /* Check access permissions first, then handle faults */
     if ( mfn_x(mfn) != INVALID_MFN )
     {
@@ -2909,6 +2930,20 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
 
         if ( violation )
         {
+            /* Should #VE be emulated for this fault? */
+            if ( p2m_is_altp2m(p2m) && !cpu_has_vmx_virt_exceptions )
+            {
+                bool_t sve;
+
+                p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL, &sve);
+
+                if ( !sve && altp2m_vcpu_emulate_ve(curr) )
+                {
+                    rc = 1;
+                    goto out_put_gfn;
+                }
+            }
+
             if ( p2m_mem_access_check(gpa, gla, npfec, &req_ptr) )
             {
                 fall_through = 1;
@@ -2928,7 +2963,9 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
          (npfec.write_access &&
           (p2m_is_discard_write(p2mt) || (p2mt == p2m_mmio_write_dm))) )
     {
-        put_gfn(currd, gfn);
+        __put_gfn(p2m, gfn);
+        if ( ap2m_active )
+            __put_gfn(hostp2m, gfn);
 
         rc = 0;
         if ( unlikely(is_pvh_domain(currd)) )
@@ -2957,6 +2994,7 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     /* Spurious fault? PoD and log-dirty also take this path. */
     if ( p2m_is_ram(p2mt) )
     {
+        rc = 1;
         /*
          * Page log dirty is always done with order 0. If this mfn resides in
          * a large page, we do not change other pages type within that large
@@ -2965,9 +3003,15 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
+            /* If p2m is really an altp2m, unlock here to avoid lock ordering
+             * violation when the change below is propagated from host p2m */
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty, p2m_ram_rw);
+            __put_gfn(ap2m_active ? hostp2m : p2m, gfn);
+
+            goto out;
         }
-        rc = 1;
         goto out_put_gfn;
     }
 
@@ -2977,7 +3021,9 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
     rc = fall_through;
 
 out_put_gfn:
-    put_gfn(currd, gfn);
+    __put_gfn(p2m, gfn);
+    if ( ap2m_active )
+        __put_gfn(hostp2m, gfn);
 out:
     /* All of these are delayed until we exit, since we might
      * sleep on event ring wait queues, and we must not hold
xen/arch/x86/mm/hap/Makefile +1 -0
index 68f2bb5..216cd90 100644
@@ -4,6 +4,7 @@ obj-y += guest_walk_3level.o
 obj-$(x86_64) += guest_walk_4level.o
 obj-y += nested_hap.o
 obj-y += nested_ept.o
+obj-y += altp2m_hap.o
 
 guest_walk_%level.o: guest_walk.c Makefile
        $(CC) $(CFLAGS) -DGUEST_PAGING_LEVELS=$* -c $< -o $@
xen/arch/x86/mm/hap/altp2m_hap.c new file +54 -0
new file mode 100644
index 0000000..52c7877
@@ -0,0 +1,98 @@
+/******************************************************************************
+ * arch/x86/mm/hap/altp2m_hap.c
+ *
+ * Copyright (c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <asm/domain.h>
+#include <asm/page.h>
+#include <asm/paging.h>
+#include <asm/p2m.h>
+#include <asm/hap.h>
+#include <asm/hvm/altp2m.h>
+
+#include "private.h"
+
+/*
+ * If the fault is for a not present entry:
+ *     if the entry in the host p2m has a valid mfn, copy it and retry
+ *     else indicate that outer handler should handle fault
+ *
+ * If the fault is for a present entry:
+ *     indicate that outer handler should handle fault
+ */
+
+int
+altp2m_hap_nested_page_fault(struct vcpu *v, paddr_t gpa,
+                                unsigned long gla, struct npfec npfec,
+                                struct p2m_domain **ap2m)
+{
+    struct p2m_domain *hp2m = p2m_get_hostp2m(v->domain);
+    p2m_type_t p2mt;
+    p2m_access_t p2ma;
+    unsigned int page_order;
+    gfn_t gfn = _gfn(paddr_to_pfn(gpa));
+    unsigned long mask;
+    mfn_t mfn;
+    int rv;
+
+    *ap2m = p2m_get_altp2m(v);
+

xen/arch/x86/mm/hap/altp2m_hap.c line 64

Quoted 10 lines
xen/arch/x86/mm/hap/altp2m_hap.c new file +10 -0
+    mfn = get_gfn_type_access(*ap2m, gfn_x(gfn), &p2mt, &p2ma,
+                              0, &page_order);
+    __put_gfn(*ap2m, gfn_x(gfn));
+
+    if ( mfn_x(mfn) != INVALID_MFN )
+        return 0;
+
+    mfn = get_gfn_type_access(hp2m, gfn_x(gfn), &p2mt, &p2ma,
+                              P2M_ALLOC | P2M_UNSHARE, &page_order);
+    put_gfn(hp2m->domain, gfn_x(gfn));

So the reason I said my comments weren't blockers for v3 was so that it could be checked in before the code freeze last Friday if that turned out to be possible.

Please do at least give this function a name that reflects what it does (i.e., try to propagate changes from the host p2m to the altp2m), and change this put_gfn() to match the __put_gfn() above.

I'd prefer it if you moved this into mm/p2m.c as well.

-George

#45 lore raw

Re: [PATCH v5 10/15] x86/altp2m: add remaining support routines.

Quoted 243 lines

From: [email protected] [mailto:[email protected]] On Behalf Of George Dunlap
Sent: Thursday, July 16, 2015 7:45 AM

Quoted 5 lines · Ed White

Add the remaining routines required to support enabling the alternate p2m functionality.

Quoted 7 lines
---
 xen/arch/x86/hvm/hvm.c           |  58 +++++-
 xen/arch/x86/mm/hap/Makefile     |   1 +
 xen/arch/x86/mm/hap/altp2m_hap.c |  98 ++++++++++
 xen/arch/x86/mm/p2m-ept.c        |   3 +
 xen/arch/x86/mm/p2m.c            | 385

+++++++++++++++++++++++++++++++++++++++

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 10 lines
 xen/include/asm-x86/hvm/altp2m.h |   4 +
 xen/include/asm-x86/p2m.h        |  33 ++++
 7 files changed, 576 insertions(+), 6 deletions(-)  create mode

100644 xen/arch/x86/mm/hap/altp2m_hap.c

xen/arch/x86/hvm/hvm.c index renamed from xen/arch/x86/hvm/hvm.c +0 -0

38deedc..a9f4b1b 100644

xen/arch/x86/hvm/hvm.c +0 -0
@@ -2802,10 +2802,11 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 13 lines
xen/arch/x86/hvm/hvm.c +2 -1
     mfn_t mfn;
     struct vcpu *curr = current;
     struct domain *currd = curr->domain;
-    struct p2m_domain *p2m;
+    struct p2m_domain *p2m, *hostp2m;
     int rc, fall_through = 0, paged = 0;
     int sharing_enomem = 0;
     vm_event_request_t *req_ptr = NULL;
+    bool_t ap2m_active = 0;
 
     /* On Nested Virtualization, walk the guest page table.
      * If this succeeds, all is fine.
@@ -2865,11 +2866,31 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 57 lines
xen/arch/x86/hvm/hvm.c +37 -2
         goto out;
     }
 
-    p2m = p2m_get_hostp2m(currd);
-    mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma,
+    ap2m_active = altp2m_active(currd);
+
+    /* Take a lock on the host p2m speculatively, to avoid potential
+     * locking order problems later and to handle unshare etc.
+     */
+    hostp2m = p2m_get_hostp2m(currd);
+    mfn = get_gfn_type_access(hostp2m, gfn, &p2mt, &p2ma,
                               P2M_ALLOC | (npfec.write_access ? P2M_UNSHARE : 0),
                               NULL);
 
+    if ( ap2m_active )
+    {
+        if ( altp2m_hap_nested_page_fault(curr, gpa, gla, npfec, &p2m) == 1 )
+        {
+            /* entry was lazily copied from host -- retry */
+            __put_gfn(hostp2m, gfn);
+            rc = 1;
+            goto out;
+        }
+
+        mfn = get_gfn_type_access(p2m, gfn, &p2mt, &p2ma, 0, NULL);
+    }
+    else
+        p2m = hostp2m;
+
     /* Check access permissions first, then handle faults */
     if ( mfn_x(mfn) != INVALID_MFN )
     {
@@ -2909,6 +2930,20 @@ int hvm_hap_nested_page_fault(paddr_t gpa,
 unsigned long gla,
 
         if ( violation )
         {
+            /* Should #VE be emulated for this fault? */
+            if ( p2m_is_altp2m(p2m) && !cpu_has_vmx_virt_exceptions )
+            {
+                bool_t sve;
+
+                p2m->get_entry(p2m, gfn, &p2mt, &p2ma, 0, NULL,
+ &sve);
+
+                if ( !sve && altp2m_vcpu_emulate_ve(curr) )
+                {
+                    rc = 1;
+                    goto out_put_gfn;
+                }
+            }
+
             if ( p2m_mem_access_check(gpa, gla, npfec, &req_ptr) )
             {
                 fall_through = 1;
@@ -2928,7 +2963,9 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

Quoted 11 lines
xen/arch/x86/hvm/hvm.c +3 -1
          (npfec.write_access &&
           (p2m_is_discard_write(p2mt) || (p2mt == p2m_mmio_write_dm))) )
     {
-        put_gfn(currd, gfn);
+        __put_gfn(p2m, gfn);
+        if ( ap2m_active )
+            __put_gfn(hostp2m, gfn);
 
         rc = 0;
         if ( unlikely(is_pvh_domain(currd)) ) @@ -2957,6 +2994,7 @@

int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,

Quoted 8 lines
     /* Spurious fault? PoD and log-dirty also take this path. */
     if ( p2m_is_ram(p2mt) )
     {

+ rc = 1;
/*
* Page log dirty is always done with order 0. If this mfn resides in
* a large page, we do not change other pages type within
that large @@ -2965,9 +3003,15 @@ int

hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,

xen/arch/x86/hvm/hvm.c int hvm_hap_nested_page_fault(paddr_t gpa,

Quoted 18 lines
         if ( npfec.write_access )
         {
             paging_mark_dirty(currd, mfn_x(mfn));
xen/arch/x86/hvm/hvm.c +4 -0
+            /* If p2m is really an altp2m, unlock here to avoid lock ordering
+             * violation when the change below is propagated from host p2m */
+            if ( ap2m_active )
+                __put_gfn(p2m, gfn);
             p2m_change_type_one(currd, gfn, p2m_ram_logdirty,

p2m_ram_rw);

xen/arch/x86/hvm/hvm.c +3 -1
+            __put_gfn(ap2m_active ? hostp2m : p2m, gfn);
+
+            goto out;
         }
-        rc = 1;
         goto out_put_gfn;
     }
@@ -2977,7 +3021,9 @@ int hvm_hap_nested_page_fault(paddr_t gpa,

unsigned long gla,

xen/arch/x86/mm/hap/altp2m_hap.c

Quoted 30 lines
xen/arch/x86/hvm/hvm.c +3 -2
     rc = fall_through;
 
 out_put_gfn:
-    put_gfn(currd, gfn);
+    __put_gfn(p2m, gfn);
+    if ( ap2m_active )
+        __put_gfn(hostp2m, gfn);
 out:
     /* All of these are delayed until we exit, since we might
      * sleep on event ring wait queues, and we must not hold diff
--git a/xen/arch/x86/mm/hap/Makefile b/xen/arch/x86/mm/hap/Makefile

index 68f2bb5..216cd90 100644

xen/arch/x86/mm/hap/Makefile +1 -1
@@ -4,6 +4,7 @@ obj-y += guest_walk_3level.o
 obj-$(x86_64) += guest_walk_4level.o
 obj-y += nested_hap.o
 obj-y += nested_ept.o
+obj-y += altp2m_hap.o
 
 guest_walk_%level.o: guest_walk.c Makefile
        $(CC) $(CFLAGS) -DGUEST_PAGING_LEVELS=$* -c $< -o $@ diff
--git a/xen/arch/x86/mm/hap/altp2m_hap.c

b/xen/arch/x86/mm/hap/altp2m_hap.c
new file mode 100644
index 0000000..52c7877

xen/arch/x86/mm/hap/altp2m_hap.c new file +0 -0
@@ -0,0 +1,98 @@

+/*********************************************************
***********

Quoted 55 lines
xen/arch/x86/mm/hap/altp2m_hap.c new file +55 -0
+**********
+ * arch/x86/mm/hap/altp2m_hap.c
+ *
+ * Copyright (c) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+modify
+ * it under the terms of the GNU General Public License as published
+by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307  USA  */
+
+#include <asm/domain.h>
+#include <asm/page.h>
+#include <asm/paging.h>
+#include <asm/p2m.h>
+#include <asm/hap.h>
+#include <asm/hvm/altp2m.h>
+
+#include "private.h"
+
+/*
+ * If the fault is for a not present entry:
+ *     if the entry in the host p2m has a valid mfn, copy it and retry
+ *     else indicate that outer handler should handle fault
+ *
+ * If the fault is for a present entry:
+ *     indicate that outer handler should handle fault
+ */
+
+int
+altp2m_hap_nested_page_fault(struct vcpu *v, paddr_t gpa,
+                                unsigned long gla, struct npfec npfec,
+                                struct p2m_domain **ap2m) {
+    struct p2m_domain *hp2m = p2m_get_hostp2m(v->domain);
+    p2m_type_t p2mt;
+    p2m_access_t p2ma;
+    unsigned int page_order;
+    gfn_t gfn = _gfn(paddr_to_pfn(gpa));
+    unsigned long mask;
+    mfn_t mfn;
+    int rv;
+
+    *ap2m = p2m_get_altp2m(v);
+

xen/arch/x86/mm/hap/altp2m_hap.c line 64

Quoted 10 lines
xen/arch/x86/mm/hap/altp2m_hap.c new file +10 -0
+    mfn = get_gfn_type_access(*ap2m, gfn_x(gfn), &p2mt, &p2ma,
+                              0, &page_order);
+    __put_gfn(*ap2m, gfn_x(gfn));
+
+    if ( mfn_x(mfn) != INVALID_MFN )
+        return 0;
+
+    mfn = get_gfn_type_access(hp2m, gfn_x(gfn), &p2mt, &p2ma,
+                              P2M_ALLOC | P2M_UNSHARE, &page_order);
+    put_gfn(hp2m->domain, gfn_x(gfn));

So the reason I said my comments weren't blockers for v3 was so that it could be checked in before the code freeze last Friday if that turned out to be possible.

Quoted 7 lines

Please do at least give this function a name that reflects what it does (i.e., try to propagate changes from the host p2m to the altp2m), and change this put_gfn() to match the __put_gfn() above.

I'd prefer it if you moved this into mm/p2m.c as well.

-George

Will do

Ravi

author
#46 lore raw

PATCH v5 11/15 x86/altp2m: define and implement alternate p2m HVMOP types.

In reply to Ed White
---
 xen/arch/x86/hvm/hvm.c          | 142 ++++++++++++++++++++++++++++++++++++++++
 xen/include/public/hvm/hvm_op.h |  82 +++++++++++++++++++++++
 2 files changed, 224 insertions(+)
xen/arch/x86/hvm/hvm.c +142 -0
index a9f4b1b..df6c6b6 100644
@@ -6443,6 +6443,148 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    case HVMOP_altp2m:
+    {
+        struct xen_hvm_altp2m_op a;
+        struct domain *d = NULL;
+
+        if ( copy_from_guest(&a, arg, 1) )
+            return -EFAULT;
+
+        if ( a.pad[0] || a.pad[1] )
+            return -EINVAL;
+
+        switch ( a.cmd )
+        {
+        case HVMOP_altp2m_get_domain_state:
+        case HVMOP_altp2m_set_domain_state:
+        case HVMOP_altp2m_create_p2m:
+        case HVMOP_altp2m_destroy_p2m:
+        case HVMOP_altp2m_switch_p2m:
+        case HVMOP_altp2m_set_mem_access:
+        case HVMOP_altp2m_change_gfn:
+            d = rcu_lock_domain_by_any_id(a.domain);
+            if ( d == NULL )
+                return -ESRCH;
+
+            if ( !is_hvm_domain(d) || !hvm_altp2m_supported() )
+                rc = -EINVAL;
+            break;
+
+        case HVMOP_altp2m_vcpu_enable_notify:
+           break;
+
+        default:
+            return -ENOSYS;
+        }
+
+        if ( !rc )
+        {
+            switch ( a.cmd )
+            {
+            case HVMOP_altp2m_get_domain_state:
+                a.u.domain_state.state = altp2m_active(d);
+                rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
+                break;
+
+            case HVMOP_altp2m_set_domain_state:
+            {
+                struct vcpu *v;
+                bool_t ostate;
+                
+                if ( nestedhvm_enabled(d) )
+                {
+                    rc = -EINVAL;
+                    break;
+                }
+
+                ostate = d->arch.altp2m_active;
+                d->arch.altp2m_active = !!a.u.domain_state.state;
+
+                /* If the alternate p2m state has changed, handle appropriately */
+                if ( d->arch.altp2m_active != ostate &&
+                     (ostate || !(rc = p2m_init_altp2m_by_id(d, 0))) )
+                {
+                    for_each_vcpu( d, v )
+                    {
+                        if ( !ostate )
+                            altp2m_vcpu_initialise(v);
+                        else
+                            altp2m_vcpu_destroy(v);
+                    }
+
+                    if ( ostate )
+                        p2m_flush_altp2m(d);
+                }
+                break;
+            }
+            default:
+                if ( !(d ? d : current->domain)->arch.altp2m_active )
+                {
+                    rc = -EINVAL;
+                    break;
+                }
+
+                switch ( a.cmd )
+                {
+                case HVMOP_altp2m_vcpu_enable_notify:
+                {
+                    struct vcpu *curr = current;
+                    p2m_type_t p2mt;
+
+                    if ( (gfn_x(vcpu_altp2m(curr).veinfo_gfn) != INVALID_GFN) ||
+                         (mfn_x(get_gfn_query_unlocked(curr->domain,
+                                a.u.enable_notify.gfn, &p2mt)) == INVALID_MFN) )
+                        return -EINVAL;
+
+                    vcpu_altp2m(curr).veinfo_gfn = _gfn(a.u.enable_notify.gfn);
+                    altp2m_vcpu_update_vmfunc_ve(curr);
+                    break;
+                }
+                case HVMOP_altp2m_create_p2m:
+                    if ( !(rc = p2m_init_next_altp2m(d, &a.u.view.view)) )
+                        rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
+                    break;
+
+                case HVMOP_altp2m_destroy_p2m:
+                    rc = p2m_destroy_altp2m_by_id(d, a.u.view.view);
+                    break;
+
+                case HVMOP_altp2m_switch_p2m:
+                    rc = p2m_switch_domain_altp2m_by_id(d, a.u.view.view);
+                    break;
+
+                case HVMOP_altp2m_set_mem_access:
+                    if ( a.u.set_mem_access.pad[0] || a.u.set_mem_access.pad[1] ||
+                         a.u.set_mem_access.pad[2] || a.u.set_mem_access.pad[3] )
+                        rc = -EINVAL;
+                    else
+                        rc = p2m_set_altp2m_mem_access(d, a.u.set_mem_access.view,
+                                _gfn(a.u.set_mem_access.gfn),
+                                a.u.set_mem_access.hvmmem_access);
+                    break;
+
+                case HVMOP_altp2m_change_gfn:
+                    if ( a.u.change_gfn.pad[0] || a.u.change_gfn.pad[1] ||
+                         a.u.change_gfn.pad[2] || a.u.change_gfn.pad[3] ||
+                         a.u.change_gfn.pad[4] || a.u.change_gfn.pad[5] )
+                        rc = -EINVAL;
+                    else
+                        rc = p2m_change_altp2m_gfn(d, a.u.change_gfn.view,
+                                _gfn(a.u.change_gfn.old_gfn),
+                                _gfn(a.u.change_gfn.new_gfn));
+                    break;
+                }
+                break;
+            }
+        }
+
+        if ( d )
+            rcu_unlock_domain(d);
+
+        break;
+    }
+
     default:
     {
         gdprintk(XENLOG_DEBUG, "Bad HVM op %ld.\n", op);
xen/include/public/hvm/hvm_op.h +82 -0

88 lines not on this page · raw message

Signature · 1 line
1.9.1
#47 lore raw

Re: [PATCH v5 11/15] x86/altp2m: define and implement alternate p2m HVMOP types.

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -6443,6 +6443,148 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 6455

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    case HVMOP_altp2m:
+    {
+        struct xen_hvm_altp2m_op a;
+        struct domain *d = NULL;
+
+        if ( copy_from_guest(&a, arg, 1) )
+            return -EFAULT;
+
+        if ( a.pad[0] || a.pad[1] )
+            return -EINVAL;

Why can't the field be uint16_t, making this a single check?

Jan

#48 lore raw

Re: [PATCH v5 11/15] x86/altp2m: define and implement alternate p2m HVMOP types.

Quoted 12 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:37 AM

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op,

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -6443,6 +6443,148 @@ long do_hvm_op(unsigned long op,

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
         break;
     }
 
Quoted 13 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 6455

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    case HVMOP_altp2m:
+    {
+        struct xen_hvm_altp2m_op a;
+        struct domain *d = NULL;
+
+        if ( copy_from_guest(&a, arg, 1) )
+            return -EFAULT;
+
+        if ( a.pad[0] || a.pad[1] )
+            return -EINVAL;

Why can't the field be uint16_t, making this a single check?

Could be of course; we had asked for an example and we found domctl, which pads with uint8_t[] and followed the same approach. Change required?

Ravi

Quoted 1 line

Jan

#49 lore raw

Re: [PATCH v5 11/15] x86/altp2m: define and implement alternate p2m HVMOP types.

Quoted 27 lines
Quoted 13 lines
Quoted 1 line · On 16.07.15 at 11:02, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, July 14, 2015 7:37 AM

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op,

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 14.07.15 at 02:14, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +0 -0
@@ -6443,6 +6443,148 @@ long do_hvm_op(unsigned long op,

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
         break;
     }
 
Quoted 13 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) · line 6455

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    case HVMOP_altp2m:
+    {
+        struct xen_hvm_altp2m_op a;
+        struct domain *d = NULL;
+
+        if ( copy_from_guest(&a, arg, 1) )
+            return -EFAULT;
+
+        if ( a.pad[0] || a.pad[1] )
+            return -EINVAL;

Why can't the field be uint16_t, making this a single check?

Quoted 2 lines

Could be of course; we had asked for an example and we found domctl, which pads with uint8_t[] and followed the same approach.

domctl is a particularly bad example, as there checking of padding space to be zero is pointless as there is no ABI guarantee anyway (same for sysctl). I.e. I'd be surprised if you found such a check in any domctl handling...

Quoted 1 line

Change required?

Yes please - let's avoid ugly code like the above.

Jan

author
#50 lore raw

PATCH v5 12/15 x86/altp2m: Add altp2mhvm HVM domain parameter.

In reply to Ed White

The altp2mhvm and nestedhvm parameters are mutually exclusive and cannot be set together.

---
 docs/man/xl.cfg.pod.5           | 12 ++++++++++++
 tools/libxl/libxl.h             |  6 ++++++
 tools/libxl/libxl_create.c      |  1 +
 tools/libxl/libxl_dom.c         |  2 ++
 tools/libxl/libxl_types.idl     |  1 +
 tools/libxl/xl_cmdimpl.c        | 10 ++++++++++
 xen/arch/x86/hvm/hvm.c          | 23 +++++++++++++++++++++--
 xen/include/public/hvm/params.h |  5 ++++-
 8 files changed, 57 insertions(+), 3 deletions(-)
docs/man/xl.cfg.pod.5 +12 -0
index a3e0e2e..18afd46 100644
@@ -1035,6 +1035,18 @@ enabled by default and you should usually omit it. It may be necessary
 to disable the HPET in order to improve compatibility with guest
 Operating Systems (X86 only)
 
+=item B<altp2mhvm=BOOLEAN>
+
+Enables or disables hvm guest access to alternate-p2m capability.
+Alternate-p2m allows a guest to manage multiple p2m guest physical
+"memory views" (as opposed to a single p2m). This option is
+disabled by default and is available only to hvm domains.
+You may want this option if you want to access-control/isolate
+access to specific guest physical memory pages accessed by
+the guest, e.g. for HVM domain memory introspection or
+for isolation/access-control of memory between components within
+a single guest hvm domain.
+
 =item B<nestedhvm=BOOLEAN>
 
 Enable or disables guest access to hardware virtualisation features,
tools/libxl/libxl.h +6 -0
index a1c5d15..17222e7 100644
@@ -745,6 +745,12 @@ typedef struct libxl__ctx libxl_ctx;
 #define LIBXL_HAVE_BUILDINFO_SERIAL_LIST 1
 
 /*
+ * LIBXL_HAVE_ALTP2M
+ * If this is defined, then libxl supports alternate p2m functionality.
+ */
+#define LIBXL_HAVE_ALTP2M 1
+
+/*
  * LIBXL_HAVE_REMUS
  * If this is defined, then libxl supports remus.
  */
tools/libxl/libxl_create.c +1 -0
index f366a09..418deee 100644
@@ -329,6 +329,7 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
         libxl_defbool_setdefault(&b_info->u.hvm.hpet,               true);
         libxl_defbool_setdefault(&b_info->u.hvm.vpt_align,          true);
         libxl_defbool_setdefault(&b_info->u.hvm.nested_hvm,         false);
+        libxl_defbool_setdefault(&b_info->u.hvm.altp2m,             false);
         libxl_defbool_setdefault(&b_info->u.hvm.usb,                false);
         libxl_defbool_setdefault(&b_info->u.hvm.xen_platform_pci,   true);
 
tools/libxl/libxl_dom.c +2 -0
index bdc0465..1008a16 100644
@@ -300,6 +300,8 @@ static void hvm_set_conf_params(xc_interface *handle, uint32_t domid,
                     libxl_defbool_val(info->u.hvm.vpt_align));
     xc_hvm_param_set(handle, domid, HVM_PARAM_NESTEDHVM,
                     libxl_defbool_val(info->u.hvm.nested_hvm));
+    xc_hvm_param_set(handle, domid, HVM_PARAM_ALTP2M,
+                    libxl_defbool_val(info->u.hvm.altp2m));
 }
 
 int libxl__build_pre(libxl__gc *gc, uint32_t domid,
tools/libxl/libxl_types.idl +1 -0
index e1632fa..fb641fe 100644
@@ -440,6 +440,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
                                        ("mmio_hole_memkb",  MemKB),
                                        ("timer_mode",       libxl_timer_mode),
                                        ("nested_hvm",       libxl_defbool),
+                                       ("altp2m",           libxl_defbool),
                                        ("smbios_firmware",  string),
                                        ("acpi_firmware",    string),
                                        ("nographic",        libxl_defbool),
tools/libxl/xl_cmdimpl.c +10 -0
index c858068..43cf6bf 100644
@@ -1500,6 +1500,16 @@ static void parse_config_data(const char *config_source,
 
         xlu_cfg_get_defbool(config, "nestedhvm", &b_info->u.hvm.nested_hvm, 0);
 
+        xlu_cfg_get_defbool(config, "altp2mhvm", &b_info->u.hvm.altp2m, 0);
+
+        if (!libxl_defbool_is_default(b_info->u.hvm.nested_hvm) &&
+            libxl_defbool_val(b_info->u.hvm.nested_hvm) &&
+            !libxl_defbool_is_default(b_info->u.hvm.altp2m) &&
+            libxl_defbool_val(b_info->u.hvm.altp2m)) {
+            fprintf(stderr, "ERROR: nestedhvm and altp2mhvm cannot be used together\n");
+            exit(1);
+        }
+
         xlu_cfg_replace_string(config, "smbios_firmware",
                                &b_info->u.hvm.smbios_firmware, 0);
         xlu_cfg_replace_string(config, "acpi_firmware",
xen/arch/x86/hvm/hvm.c +21 -2
index df6c6b6..df69c1d 100644
@@ -5750,6 +5750,7 @@ static int hvm_allow_set_param(struct domain *d,
     case HVM_PARAM_VIRIDIAN:
     case HVM_PARAM_IOREQ_SERVER_PFN:
     case HVM_PARAM_NR_IOREQ_SERVER_PAGES:
+    case HVM_PARAM_ALTP2M:
         if ( value != 0 && a->value != value )
             rc = -EEXIST;
         break;
@@ -5872,6 +5873,9 @@ static int hvmop_set_param(
          */
         if ( cpu_has_svm && !paging_mode_hap(d) && a.value )
             rc = -EINVAL;
+        if ( a.value &&
+             d->arch.hvm_domain.params[HVM_PARAM_ALTP2M] )
+            rc = -EINVAL;
         /* Set up NHVM state for any vcpus that are already up. */
         if ( a.value &&
              !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM] )
@@ -5882,6 +5886,13 @@ static int hvmop_set_param(
             for_each_vcpu(d, v)
                 nestedhvm_vcpu_destroy(v);
         break;
+    case HVM_PARAM_ALTP2M:
+        if ( a.value > 1 )
+            rc = -EINVAL;
+        if ( a.value &&
+             d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM] )
+            rc = -EINVAL;
+        break;
     case HVM_PARAM_BUFIOREQ_EVTCHN:
         rc = -EINVAL;
         break;
@@ -5942,6 +5953,7 @@ static int hvm_allow_get_param(struct domain *d,
     case HVM_PARAM_STORE_EVTCHN:
     case HVM_PARAM_CONSOLE_PFN:
     case HVM_PARAM_CONSOLE_EVTCHN:
+    case HVM_PARAM_ALTP2M:
         break;
     /*
      * The following parameters must not be read by the guest
@@ -6483,6 +6495,12 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
             switch ( a.cmd )
             {
             case HVMOP_altp2m_get_domain_state:
+                if ( !d->arch.hvm_domain.params[HVM_PARAM_ALTP2M] )
+                {
+                    rc = -EINVAL;
+                    break;
+                }
+
                 a.u.domain_state.state = altp2m_active(d);
                 rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
                 break;
@@ -6491,8 +6509,9 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
             {
                 struct vcpu *v;
                 bool_t ostate;
-                
-                if ( nestedhvm_enabled(d) )
+
+                if ( !d->arch.hvm_domain.params[HVM_PARAM_ALTP2M] ||
+                     nestedhvm_enabled(d) )
                 {
                     rc = -EINVAL;
                     break;
xen/include/public/hvm/params.h +4 -1
index 7c73089..147d9b8 100644
@@ -187,6 +187,9 @@
 /* Location of the VM Generation ID in guest physical address space. */
 #define HVM_PARAM_VM_GENERATION_ID_ADDR 34
 
-#define HVM_NR_PARAMS          35
+/* Boolean: Enable altp2m */
+#define HVM_PARAM_ALTP2M       35
+
+#define HVM_NR_PARAMS          36
 
 #endif /* __XEN_PUBLIC_HVM_PARAMS_H__ */
Signature · 1 line
1.9.1
author
#51 lore raw

PATCH v5 13/15 x86/altp2m: XSM hooks for altp2m HVM ops

In reply to Ed White

From: Ravi Sahita <[email protected]>

---
 tools/flask/policy/policy/modules/xen/xen.if |  4 ++--
 xen/arch/x86/hvm/hvm.c                       |  6 ++++++
 xen/include/xsm/dummy.h                      | 12 ++++++++++++
 xen/include/xsm/xsm.h                        | 12 ++++++++++++
 xen/xsm/dummy.c                              |  2 ++
 xen/xsm/flask/hooks.c                        | 12 ++++++++++++
 xen/xsm/flask/policy/access_vectors          |  7 +++++++
 7 files changed, 53 insertions(+), 2 deletions(-)
tools/flask/policy/policy/modules/xen/xen.if +2 -2
index f4cde11..6177fe9 100644
@@ -8,7 +8,7 @@
 define(`declare_domain_common', `
 	allow $1 $2:grant { query setup };
 	allow $1 $2:mmu { adjust physmap map_read map_write stat pinpage updatemp mmuext_op };
-	allow $1 $2:hvm { getparam setparam };
+	allow $1 $2:hvm { getparam setparam altp2mhvm_op };
 	allow $1 $2:domain2 get_vnumainfo;
 ')
 
@@ -58,7 +58,7 @@ define(`create_domain_common', `
 	allow $1 $2:mmu { map_read map_write adjust memorymap physmap pinpage mmuext_op updatemp };
 	allow $1 $2:grant setup;
 	allow $1 $2:hvm { cacheattr getparam hvmctl irqlevel pciroute sethvmc
-			setparam pcilevel trackdirtyvram nested };
+			setparam pcilevel trackdirtyvram nested altp2mhvm altp2mhvm_op };
 ')
 
 # create_domain(priv, target)
xen/arch/x86/hvm/hvm.c +6 -0
index df69c1d..ea1c784 100644
@@ -5887,6 +5887,9 @@ static int hvmop_set_param(
                 nestedhvm_vcpu_destroy(v);
         break;
     case HVM_PARAM_ALTP2M:
+        rc = xsm_hvm_param_altp2mhvm(XSM_PRIV, d);
+        if ( rc )
+            break;
         if ( a.value > 1 )
             rc = -EINVAL;
         if ( a.value &&
@@ -6491,6 +6494,9 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
         }
 
         if ( !rc )
+            rc = xsm_hvm_altp2mhvm_op(XSM_TARGET, d ? d : current->domain);
+
+        if ( !rc )
         {
             switch ( a.cmd )
             {
xen/include/xsm/dummy.h +12 -0
index f044c0f..e0b561d 100644
@@ -548,6 +548,18 @@ static XSM_INLINE int xsm_hvm_param_nested(XSM_DEFAULT_ARG struct domain *d)
     return xsm_default_action(action, current->domain, d);
 }
 
+static XSM_INLINE int xsm_hvm_param_altp2mhvm(XSM_DEFAULT_ARG struct domain *d)
+{
+    XSM_ASSERT_ACTION(XSM_PRIV);
+    return xsm_default_action(action, current->domain, d);
+}
+
+static XSM_INLINE int xsm_hvm_altp2mhvm_op(XSM_DEFAULT_ARG struct domain *d)
+{
+    XSM_ASSERT_ACTION(XSM_TARGET);
+    return xsm_default_action(action, current->domain, d);
+}
+
 static XSM_INLINE int xsm_vm_event_control(XSM_DEFAULT_ARG struct domain *d, int mode, int op)
 {
     XSM_ASSERT_ACTION(XSM_PRIV);
xen/include/xsm/xsm.h +12 -0
index c872d44..dc48d23 100644
@@ -147,6 +147,8 @@ struct xsm_operations {
     int (*hvm_param) (struct domain *d, unsigned long op);
     int (*hvm_control) (struct domain *d, unsigned long op);
     int (*hvm_param_nested) (struct domain *d);
+    int (*hvm_param_altp2mhvm) (struct domain *d);
+    int (*hvm_altp2mhvm_op) (struct domain *d);
     int (*get_vnumainfo) (struct domain *d);
 
     int (*vm_event_control) (struct domain *d, int mode, int op);
@@ -586,6 +588,16 @@ static inline int xsm_hvm_param_nested (xsm_default_t def, struct domain *d)
     return xsm_ops->hvm_param_nested(d);
 }
 
+static inline int xsm_hvm_param_altp2mhvm (xsm_default_t def, struct domain *d)
+{
+    return xsm_ops->hvm_param_altp2mhvm(d);
+}
+
+static inline int xsm_hvm_altp2mhvm_op (xsm_default_t def, struct domain *d)
+{
+    return xsm_ops->hvm_altp2mhvm_op(d);
+}
+
 static inline int xsm_get_vnumainfo (xsm_default_t def, struct domain *d)
 {
     return xsm_ops->get_vnumainfo(d);
xen/xsm/dummy.c +2 -0
index e84b0e4..3461d4f 100644
@@ -116,6 +116,8 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, hvm_param);
     set_to_dummy_if_null(ops, hvm_control);
     set_to_dummy_if_null(ops, hvm_param_nested);
+    set_to_dummy_if_null(ops, hvm_param_altp2mhvm);
+    set_to_dummy_if_null(ops, hvm_altp2mhvm_op);
 
     set_to_dummy_if_null(ops, do_xsm_op);
 #ifdef CONFIG_COMPAT
xen/xsm/flask/hooks.c +12 -0
index 6e37d29..2b998c9 100644
@@ -1170,6 +1170,16 @@ static int flask_hvm_param_nested(struct domain *d)
     return current_has_perm(d, SECCLASS_HVM, HVM__NESTED);
 }
 
+static int flask_hvm_param_altp2mhvm(struct domain *d)
+{
+    return current_has_perm(d, SECCLASS_HVM, HVM__ALTP2MHVM);
+}
+
+static int flask_hvm_altp2mhvm_op(struct domain *d)
+{
+    return current_has_perm(d, SECCLASS_HVM, HVM__ALTP2MHVM_OP);
+}
+
 static int flask_vm_event_control(struct domain *d, int mode, int op)
 {
     return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__VM_EVENT);
@@ -1654,6 +1664,8 @@ static struct xsm_operations flask_ops = {
     .hvm_param = flask_hvm_param,
     .hvm_control = flask_hvm_param,
     .hvm_param_nested = flask_hvm_param_nested,
+    .hvm_param_altp2mhvm = flask_hvm_param_altp2mhvm,
+    .hvm_altp2mhvm_op = flask_hvm_altp2mhvm_op,
 
     .do_xsm_op = do_flask_op,
     .get_vnumainfo = flask_get_vnumainfo,
xen/xsm/flask/policy/access_vectors +7 -0
index 68284d5..d168de2 100644
@@ -272,6 +272,13 @@ class hvm
     share_mem
 # HVMOP_set_param setting HVM_PARAM_NESTEDHVM
     nested
+# HVMOP_set_param setting HVM_PARAM_ALTP2MHVM
+    altp2mhvm
+# HVMOP_altp2m_set_domain_state HVMOP_altp2m_get_domain_state
+# HVMOP_altp2m_vcpu_enable_notify HVMOP_altp2m_create_p2m
+# HVMOP_altp2m_destroy_p2m HVMOP_altp2m_switch_p2m
+# HVMOP_altp2m_set_mem_access HVMOP_altp2m_change_gfn
+    altp2mhvm_op
 }
 
 # Class event describes event channels.  Interdomain event channels have their
Signature · 1 line
1.9.1
author
#52 lore raw

PATCH v5 14/15 tools/libxc: add support to altp2m hvmops

In reply to Ed White

From: Tamas K Lengyel <[email protected]>

Wrappers to issue altp2m hvmops.

---
 tools/libxc/Makefile          |   1 +
 tools/libxc/include/xenctrl.h |  21 ++++
 tools/libxc/xc_altp2m.c       | 237 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 259 insertions(+)
 create mode 100644 tools/libxc/xc_altp2m.c
tools/libxc/Makefile +1 -0
index 153b79e..c2c2b1c 100644
@@ -10,6 +10,7 @@ override CONFIG_MIGRATE := n
 endif
 
 CTRL_SRCS-y       :=
+CTRL_SRCS-y       += xc_altp2m.c
 CTRL_SRCS-y       += xc_core.c
 CTRL_SRCS-$(CONFIG_X86) += xc_core_x86.c
 CTRL_SRCS-$(CONFIG_ARM) += xc_core_arm.c
tools/libxc/include/xenctrl.h +21 -0
index d1d2ab3..ecddf28 100644
@@ -2316,6 +2316,27 @@ void xc_tmem_save_done(xc_interface *xch, int dom);
 int xc_tmem_restore(xc_interface *xch, int dom, int fd);
 int xc_tmem_restore_extra(xc_interface *xch, int dom, int fd);
 
+/**
+ * altp2m operations
+ */
+
+int xc_altp2m_get_domain_state(xc_interface *handle, domid_t dom, bool *state);
+int xc_altp2m_set_domain_state(xc_interface *handle, domid_t dom, bool state);
+int xc_altp2m_set_vcpu_enable_notify(xc_interface *handle, xen_pfn_t gfn);
+int xc_altp2m_create_view(xc_interface *handle, domid_t domid,
+                          xenmem_access_t default_access, uint16_t *view_id);
+int xc_altp2m_destroy_view(xc_interface *handle, domid_t domid,
+                           uint16_t view_id);
+/* Switch all vCPUs of the domain to the specified altp2m view */
+int xc_altp2m_switch_to_view(xc_interface *handle, domid_t domid,
+                             uint16_t view_id);
+int xc_altp2m_set_mem_access(xc_interface *handle, domid_t domid,
+                             uint16_t view_id, xen_pfn_t gfn,
+                             xenmem_access_t access);
+int xc_altp2m_change_gfn(xc_interface *handle, domid_t domid,
+                         uint16_t view_id, xen_pfn_t old_gfn,
+                         xen_pfn_t new_gfn);
+
 /** 
  * Mem paging operations.
  * Paging is supported only on the x86 architecture in 64 bit mode, with
tools/libxc/xc_altp2m.c new file +237 -0

237 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#53 lore raw

PATCH v5 15/15 tools/xen-access: altp2m testcases

In reply to Ed White

From: Tamas K Lengyel <[email protected]>

Working altp2m test-case. Extended the test tool to support singlestepping to better highlight the core feature of altp2m view switching.

---
 tools/tests/xen-access/xen-access.c | 173 ++++++++++++++++++++++++++++++------
 1 file changed, 148 insertions(+), 25 deletions(-)
tools/tests/xen-access/xen-access.c +148 -25
index 12ab921..6b69c26 100644
@@ -275,6 +275,19 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     return NULL;
 }
 
+static inline
+int control_singlestep(
+    xc_interface *xch,
+    domid_t domain_id,
+    unsigned long vcpu,
+    bool enable)
+{
+    uint32_t op = enable ?
+        XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_ON : XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_OFF;
+
+    return xc_domain_debug_control(xch, domain_id, op, vcpu);
+}
+
 /*
  * Note that this function is not thread safe.
  */
@@ -317,13 +330,15 @@ static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
 
 void usage(char* progname)
 {
-    fprintf(stderr,
-            "Usage: %s [-m] <domain_id> write|exec|breakpoint\n"
+    fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
+#if defined(__i386__) || defined(__x86_64__)
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec");
+#endif
+            fprintf(stderr,
             "\n"
             "Logs first page writes, execs, or breakpoint traps that occur on the domain.\n"
             "\n"
-            "-m requires this program to run, or else the domain may pause\n",
-            progname);
+            "-m requires this program to run, or else the domain may pause\n");
 }
 
 int main(int argc, char *argv[])
@@ -341,6 +356,8 @@ int main(int argc, char *argv[])
     int required = 0;
     int breakpoint = 0;
     int shutting_down = 0;
+    int altp2m = 0;
+    uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
     argv++;
@@ -379,10 +396,22 @@ int main(int argc, char *argv[])
         default_access = XENMEM_access_rw;
         after_first_access = XENMEM_access_rwx;
     }
+#if defined(__i386__) || defined(__x86_64__)
     else if ( !strcmp(argv[0], "breakpoint") )
     {
         breakpoint = 1;
     }
+    else if ( !strcmp(argv[0], "altp2m_write") )
+    {
+        default_access = XENMEM_access_rx;
+        altp2m = 1;
+    }
+    else if ( !strcmp(argv[0], "altp2m_exec") )
+    {
+        default_access = XENMEM_access_rw;
+        altp2m = 1;
+    }
+#endif
     else
     {
         usage(argv[0]);
@@ -415,22 +444,73 @@ int main(int argc, char *argv[])
         goto exit;
     }
 
-    /* Set the default access type and convert all pages to it */
-    rc = xc_set_mem_access(xch, domain_id, default_access, ~0ull, 0);
-    if ( rc < 0 )
+    /* With altp2m we just create a new, restricted view of the memory */
+    if ( altp2m )
     {
-        ERROR("Error %d setting default mem access type\n", rc);
-        goto exit;
-    }
+        xen_pfn_t gfn = 0;
+        unsigned long perm_set = 0;
+
+        rc = xc_altp2m_set_domain_state( xch, domain_id, 1 );
+        if ( rc < 0 )
+        {
+            ERROR("Error %d enabling altp2m on domain!\n", rc);
+            goto exit;
+        }
+
+        rc = xc_altp2m_create_view( xch, domain_id, default_access, &altp2m_view_id );
+        if ( rc < 0 )
+        {
+            ERROR("Error %d creating altp2m view!\n", rc);
+            goto exit;
+        }
 
-    rc = xc_set_mem_access(xch, domain_id, default_access, START_PFN,
-                           (xenaccess->max_gpfn - START_PFN) );
+        DPRINTF("altp2m view created with id %u\n", altp2m_view_id);
+        DPRINTF("Setting altp2m mem_access permissions.. ");
 
-    if ( rc < 0 )
+        for(; gfn < xenaccess->max_gpfn; ++gfn)
+        {
+            rc = xc_altp2m_set_mem_access( xch, domain_id, altp2m_view_id, gfn,
+                                           default_access);
+            if ( !rc )
+                perm_set++;
+        }
+
+        DPRINTF("done! Permissions set on %lu pages.\n", perm_set);
+
+        rc = xc_altp2m_switch_to_view( xch, domain_id, altp2m_view_id );
+        if ( rc < 0 )
+        {
+            ERROR("Error %d switching to altp2m view!\n", rc);
+            goto exit;
+        }
+
+        rc = xc_monitor_singlestep( xch, domain_id, 1 );
+        if ( rc < 0 )
+        {
+            ERROR("Error %d failed to enable singlestep monitoring!\n", rc);
+            goto exit;
+        }
+    }
+
+    if ( !altp2m )
     {
-        ERROR("Error %d setting all memory to access type %d\n", rc,
-              default_access);
-        goto exit;
+        /* Set the default access type and convert all pages to it */
+        rc = xc_set_mem_access(xch, domain_id, default_access, ~0ull, 0);
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting default mem access type\n", rc);
+            goto exit;
+        }
+
+        rc = xc_set_mem_access(xch, domain_id, default_access, START_PFN,
+                               (xenaccess->max_gpfn - START_PFN) );
+
+        if ( rc < 0 )
+        {
+            ERROR("Error %d setting all memory to access type %d\n", rc,
+                  default_access);
+            goto exit;
+        }
     }
 
     if ( breakpoint )
@@ -448,13 +528,29 @@ int main(int argc, char *argv[])
     {
         if ( interrupted )
         {
+            /* Unregister for every event */
             DPRINTF("xenaccess shutting down on signal %d\n", interrupted);
 
-            /* Unregister for every event */
-            rc = xc_set_mem_access(xch, domain_id, XENMEM_access_rwx, ~0ull, 0);
-            rc = xc_set_mem_access(xch, domain_id, XENMEM_access_rwx, START_PFN,
-                                   (xenaccess->max_gpfn - START_PFN) );
-            rc = xc_monitor_software_breakpoint(xch, domain_id, 0);
+            if ( breakpoint )
+                rc = xc_monitor_software_breakpoint(xch, domain_id, 0);
+
+            if ( altp2m )
+            {
+                uint32_t vcpu_id;
+
+                rc = xc_altp2m_switch_to_view( xch, domain_id, 0 );
+                rc = xc_altp2m_destroy_view(xch, domain_id, altp2m_view_id);
+                rc = xc_altp2m_set_domain_state(xch, domain_id, 0);
+                rc = xc_monitor_singlestep(xch, domain_id, 0);
+
+                for ( vcpu_id = 0; vcpu_id<XEN_LEGACY_MAX_VCPUS; vcpu_id++)
+                    rc = control_singlestep(xch, domain_id, vcpu_id, 0);
+
+            } else {
+                rc = xc_set_mem_access(xch, domain_id, XENMEM_access_rwx, ~0ull, 0);
+                rc = xc_set_mem_access(xch, domain_id, XENMEM_access_rwx, START_PFN,
+                                       (xenaccess->max_gpfn - START_PFN) );
+            }
 
             shutting_down = 1;
         }
@@ -500,7 +596,7 @@ int main(int argc, char *argv[])
                 }
 
                 printf("PAGE ACCESS: %c%c%c for GFN %"PRIx64" (offset %06"
-                       PRIx64") gla %016"PRIx64" (valid: %c; fault in gpt: %c; fault with gla: %c) (vcpu %u)\n",
+                       PRIx64") gla %016"PRIx64" (valid: %c; fault in gpt: %c; fault with gla: %c) (vcpu %u, altp2m view %u)\n",
                        (req.u.mem_access.flags & MEM_ACCESS_R) ? 'r' : '-',
                        (req.u.mem_access.flags & MEM_ACCESS_W) ? 'w' : '-',
                        (req.u.mem_access.flags & MEM_ACCESS_X) ? 'x' : '-',
@@ -510,9 +606,20 @@ int main(int argc, char *argv[])
                        (req.u.mem_access.flags & MEM_ACCESS_GLA_VALID) ? 'y' : 'n',
                        (req.u.mem_access.flags & MEM_ACCESS_FAULT_IN_GPT) ? 'y' : 'n',
                        (req.u.mem_access.flags & MEM_ACCESS_FAULT_WITH_GLA) ? 'y': 'n',
-                       req.vcpu_id);
+                       req.vcpu_id,
+                       req.altp2m_idx);
 
-                if ( default_access != after_first_access )
+                if ( altp2m && req.flags & VM_EVENT_FLAG_ALTERNATE_P2M)
+                {
+                    DPRINTF("\tSwitching back to default view!\n");
+
+                    rsp.reason = req.reason;
+                    rsp.flags = req.flags;
+                    rsp.altp2m_idx = 0;
+
+                    control_singlestep(xch, domain_id, rsp.vcpu_id, 1);
+                }
+                else if ( default_access != after_first_access )
                 {
                     rc = xc_set_mem_access(xch, domain_id, after_first_access,
                                            req.u.mem_access.gfn, 1);
@@ -525,7 +632,6 @@ int main(int argc, char *argv[])
                     }
                 }
 
-
                 rsp.u.mem_access.gfn = req.u.mem_access.gfn;
                 break;
             case VM_EVENT_REASON_SOFTWARE_BREAKPOINT:
@@ -546,6 +652,23 @@ int main(int argc, char *argv[])
                 }
 
                 break;
+            case VM_EVENT_REASON_SINGLESTEP:
+                printf("Singlestep: rip=%016"PRIx64", vcpu %d\n",
+                       req.regs.x86.rip,
+                       req.vcpu_id);
+
+                if ( altp2m )
+                {
+                    printf("\tSwitching altp2m to view %u!\n", altp2m_view_id);
+
+                    rsp.reason = req.reason;
+                    rsp.flags |= VM_EVENT_FLAG_ALTERNATE_P2M;
+                    rsp.altp2m_idx = altp2m_view_id;
+                }
+
+                control_singlestep(xch, domain_id, req.vcpu_id, 0);
+
+                break;
             default:
                 fprintf(stderr, "UNKNOWN REASON CODE %d\n", req.reason);
             }
Signature · 1 line
1.9.1
Acked-by on v5 15/15
#54 lore raw

Re: [PATCH v5 15/15] tools/xen-access: altp2m testcases

Quoted 9 lines · Ed White

From: Tamas K Lengyel <[email protected]>

Working altp2m test-case. Extended the test tool to support singlestepping to better highlight the core feature of altp2m view switching.

Some nits below.

I do notice there is inconsistency in coding style, so I won't ask you to resubmit just for fixing style. But a follow-up patch to fix up all style problem is welcome.

Quoted 35 lines
---
 tools/tests/xen-access/xen-access.c | 173 ++++++++++++++++++++++++++++++------
 1 file changed, 148 insertions(+), 25 deletions(-)
tools/tests/xen-access/xen-access.c +14 -2
index 12ab921..6b69c26 100644
@@ -275,6 +275,19 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     return NULL;
 }
 
+static inline
+int control_singlestep(
+    xc_interface *xch,
+    domid_t domain_id,
+    unsigned long vcpu,
+    bool enable)
+{
+    uint32_t op = enable ?
+        XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_ON : XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_OFF;
+
+    return xc_domain_debug_control(xch, domain_id, op, vcpu);
+}
+
 /*
  * Note that this function is not thread safe.
  */
@@ -317,13 +330,15 @@ static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)
 
 void usage(char* progname)
 {
-    fprintf(stderr,
-            "Usage: %s [-m] <domain_id> write|exec|breakpoint\n"
+    fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);

tools/tests/xen-access/xen-access.c static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp) · line 341

Quoted 10 lines
tools/tests/xen-access/xen-access.c +5 -2
+#if defined(__i386__) || defined(__x86_64__)
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec");
+#endif
+            fprintf(stderr,
             "\n"
             "Logs first page writes, execs, or breakpoint traps that occur on the domain.\n"
             "\n"
-            "-m requires this program to run, or else the domain may pause\n",
-            progname);
+            "-m requires this program to run, or else the domain may pause\n");

Indentation looks wrong, but this is not your fault so don't worry about this.

Quoted 42 lines
tools/tests/xen-access/xen-access.c +14 -3
 }
 
 int main(int argc, char *argv[])
@@ -341,6 +356,8 @@ int main(int argc, char *argv[])
     int required = 0;
     int breakpoint = 0;
     int shutting_down = 0;
+    int altp2m = 0;
+    uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
     argv++;
@@ -379,10 +396,22 @@ int main(int argc, char *argv[])
         default_access = XENMEM_access_rw;
         after_first_access = XENMEM_access_rwx;
     }
+#if defined(__i386__) || defined(__x86_64__)
     else if ( !strcmp(argv[0], "breakpoint") )
     {
         breakpoint = 1;
     }
+    else if ( !strcmp(argv[0], "altp2m_write") )
+    {
+        default_access = XENMEM_access_rx;
+        altp2m = 1;
+    }
+    else if ( !strcmp(argv[0], "altp2m_exec") )
+    {
+        default_access = XENMEM_access_rw;
+        altp2m = 1;
+    }
+#endif
     else
     {
         usage(argv[0]);
@@ -415,22 +444,73 @@ int main(int argc, char *argv[])
         goto exit;
     }
 
-    /* Set the default access type and convert all pages to it */
-    rc = xc_set_mem_access(xch, domain_id, default_access, ~0ull, 0);
-    if ( rc < 0 )

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 453

Quoted 10 lines
tools/tests/xen-access/xen-access.c +6 -3
+    /* With altp2m we just create a new, restricted view of the memory */
+    if ( altp2m )
     {
-        ERROR("Error %d setting default mem access type\n", rc);
-        goto exit;
-    }
+        xen_pfn_t gfn = 0;
+        unsigned long perm_set = 0;
+
+        rc = xc_altp2m_set_domain_state( xch, domain_id, 1 );

Extraneous spaces in brackets.

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 460

Quoted 7 lines
tools/tests/xen-access/xen-access.c +7 -0
+        if ( rc < 0 )
+        {
+            ERROR("Error %d enabling altp2m on domain!\n", rc);
+            goto exit;
+        }
+
+        rc = xc_altp2m_create_view( xch, domain_id, default_access, &altp2m_view_id );

Extraneous spaces and line too long.

Quoted 6 lines
tools/tests/xen-access/xen-access.c +5 -0
+        if ( rc < 0 )
+        {
+            ERROR("Error %d creating altp2m view!\n", rc);
+            goto exit;
+        }
 

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 473

Quoted 10 lines
tools/tests/xen-access/xen-access.c +6 -3
-    rc = xc_set_mem_access(xch, domain_id, default_access, START_PFN,
-                           (xenaccess->max_gpfn - START_PFN) );
+        DPRINTF("altp2m view created with id %u\n", altp2m_view_id);
+        DPRINTF("Setting altp2m mem_access permissions.. ");
 
-    if ( rc < 0 )
+        for(; gfn < xenaccess->max_gpfn; ++gfn)
+        {
+            rc = xc_altp2m_set_mem_access( xch, domain_id, altp2m_view_id, gfn,
+                                           default_access);

Space.

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 480

Quoted 7 lines
tools/tests/xen-access/xen-access.c +7 -0
+            if ( !rc )
+                perm_set++;
+        }
+
+        DPRINTF("done! Permissions set on %lu pages.\n", perm_set);
+
+        rc = xc_altp2m_switch_to_view( xch, domain_id, altp2m_view_id );

Ditto.

The rest of code has similar issues too. I won't point them out one by one. Again, don't worry about this now.

Wei.

#55 lore raw

Re: [PATCH v5 15/15] tools/xen-access: altp2m testcases

Quoted 174 lines · Wei Liu
Quoted 3 lines · Ed White

From: Tamas K Lengyel <[email protected]>

Working altp2m test-case. Extended the test tool to support

singlestepping

Quoted 6 lines

to better highlight the core feature of altp2m view switching.

Some nits below.

I do notice there is inconsistency in coding style, so I won't ask you to resubmit just for fixing style. But a follow-up patch to fix up all style problem is welcome.

Quoted 2 lines
---
 tools/tests/xen-access/xen-access.c | 173

++++++++++++++++++++++++++++++------

Review

Quoted 3 lines
 1 file changed, 148 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/tools/tests/xen-access/xen-access.c

tools/tests/xen-access/xen-access.c xenaccess_t *xenaccess_init(xc_interface **xch_r,

Quoted 4 lines

index 12ab921..6b69c26 100644

tools/tests/xen-access/xen-access.c +0 -0
@@ -275,6 +275,19 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r,

domid_t domain_id)

tools/tests/xen-access/xen-access.c xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)

Quoted 12 lines
tools/tests/xen-access/xen-access.c +9 -0
     return NULL;
 }
 
+static inline
+int control_singlestep(
+    xc_interface *xch,
+    domid_t domain_id,
+    unsigned long vcpu,
+    bool enable)
+{
+    uint32_t op = enable ?
+        XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_ON :

XEN_DOMCTL_DEBUG_OP_SINGLE_STEP_OFF;

tools/tests/xen-access/xen-access.c static void put_response(vm_event_t *vm_event,

Quoted 8 lines
tools/tests/xen-access/xen-access.c +4 -0
+
+    return xc_domain_debug_control(xch, domain_id, op, vcpu);
+}
+
 /*
  * Note that this function is not thread safe.
  */
@@ -317,13 +330,15 @@ static void put_response(vm_event_t *vm_event,

vm_event_response_t *rsp)

tools/tests/xen-access/xen-access.c static void put_response(vm_event_t *vm_event, vm_event_response_t *rsp)

Quoted 12 lines
tools/tests/xen-access/xen-access.c +5 -2
 void usage(char* progname)
 {
-    fprintf(stderr,
-            "Usage: %s [-m] <domain_id> write|exec|breakpoint\n"
+    fprintf(stderr, "Usage: %s [-m] <domain_id> write|exec", progname);
+#if defined(__i386__) || defined(__x86_64__)
+            fprintf(stderr, "|breakpoint|altp2m_write|altp2m_exec");
+#endif
+            fprintf(stderr,
             "\n"
             "Logs first page writes, execs, or breakpoint traps that

occur on the domain.\n"

tools/tests/xen-access/xen-access.c static void put_response(vm_event_t *vm_event, · line 324

Quoted 2 lines
tools/tests/xen-access/xen-access.c +0 -1
             "\n"
-            "-m requires this program to run, or else the domain may

pause\n",

tools/tests/xen-access/xen-access.c static void put_response(vm_event_t *vm_event, · line 340

Quoted 2 lines
tools/tests/xen-access/xen-access.c +1 -1
-            progname);
+            "-m requires this program to run, or else the domain may

pause\n");

Indentation looks wrong, but this is not your fault so don't worry about this.

Quoted 36 lines
tools/tests/xen-access/xen-access.c +14 -0
 }
 
 int main(int argc, char *argv[])
@@ -341,6 +356,8 @@ int main(int argc, char *argv[])
     int required = 0;
     int breakpoint = 0;
     int shutting_down = 0;
+    int altp2m = 0;
+    uint16_t altp2m_view_id = 0;
 
     char* progname = argv[0];
     argv++;
@@ -379,10 +396,22 @@ int main(int argc, char *argv[])
         default_access = XENMEM_access_rw;
         after_first_access = XENMEM_access_rwx;
     }
+#if defined(__i386__) || defined(__x86_64__)
     else if ( !strcmp(argv[0], "breakpoint") )
     {
         breakpoint = 1;
     }
+    else if ( !strcmp(argv[0], "altp2m_write") )
+    {
+        default_access = XENMEM_access_rx;
+        altp2m = 1;
+    }
+    else if ( !strcmp(argv[0], "altp2m_exec") )
+    {
+        default_access = XENMEM_access_rw;
+        altp2m = 1;
+    }
+#endif
     else
     {
         usage(argv[0]);

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 447

Quoted 7 lines
tools/tests/xen-access/xen-access.c +1 -3
@@ -415,22 +444,73 @@ int main(int argc, char *argv[])
         goto exit;
     }
 
-    /* Set the default access type and convert all pages to it */
-    rc = xc_set_mem_access(xch, domain_id, default_access, ~0ull, 0);
-    if ( rc < 0 )
+    /* With altp2m we just create a new, restricted view of the memory

*/

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 453

Quoted 9 lines
tools/tests/xen-access/xen-access.c +5 -3
+    if ( altp2m )
     {
-        ERROR("Error %d setting default mem access type\n", rc);
-        goto exit;
-    }
+        xen_pfn_t gfn = 0;
+        unsigned long perm_set = 0;
+
+        rc = xc_altp2m_set_domain_state( xch, domain_id, 1 );

Extraneous spaces in brackets.

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[])

Quoted 7 lines
tools/tests/xen-access/xen-access.c +7 -0
+        if ( rc < 0 )
+        {
+            ERROR("Error %d enabling altp2m on domain!\n", rc);
+            goto exit;
+        }
+
+        rc = xc_altp2m_create_view( xch, domain_id, default_access,

&altp2m_view_id );

Extraneous spaces and line too long.

Quoted 5 lines
tools/tests/xen-access/xen-access.c +5 -0
+        if ( rc < 0 )
+        {
+            ERROR("Error %d creating altp2m view!\n", rc);
+            goto exit;
+        }

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[])

Quoted 10 lines
tools/tests/xen-access/xen-access.c +5 -3
 
-    rc = xc_set_mem_access(xch, domain_id, default_access, START_PFN,
-                           (xenaccess->max_gpfn - START_PFN) );
+        DPRINTF("altp2m view created with id %u\n", altp2m_view_id);
+        DPRINTF("Setting altp2m mem_access permissions.. ");
 
-    if ( rc < 0 )
+        for(; gfn < xenaccess->max_gpfn; ++gfn)
+        {
+            rc = xc_altp2m_set_mem_access( xch, domain_id,

altp2m_view_id, gfn,

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 473

Quoted 1 line
tools/tests/xen-access/xen-access.c +1 -0
+                                           default_access);

Space.

tools/tests/xen-access/xen-access.c int main(int argc, char *argv[]) · line 480

Quoted 7 lines
tools/tests/xen-access/xen-access.c +7 -0
+            if ( !rc )
+                perm_set++;
+        }
+
+        DPRINTF("done! Permissions set on %lu pages.\n", perm_set);
+
+        rc = xc_altp2m_switch_to_view( xch, domain_id, altp2m_view_id );

Ditto.

Quoted 5 lines

The rest of code has similar issues too. I won't point them out one by one. Again, don't worry about this now.

Wei.

Thanks Wei! I'll submit a patch to fix the style issues after (and if) this gets merged!

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel