x86/pv: Factor out the calculation of LDT/GDT descriptor pointers
authorAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 13 Oct 2017 10:55:00 +0000 (10:55 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 1 Dec 2017 19:03:27 +0000 (19:03 +0000)
Rather than opencoding it in two places.  While only used in the PV emulation
code, this helper is in principle usable anywhere in the hypervisor.

No functional change.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/pv/emul-gate-op.c
xen/arch/x86/pv/emulate.c
xen/arch/x86/pv/emulate.h

index 0f89c91dff9b19fae24ba056cc54d4e82c25daf8..14ce95e26eb5cae1a701d2f87d440944fd853e6d 100644 (file)
@@ -54,11 +54,8 @@ static int read_gate_descriptor(unsigned int gate_sel,
                                 unsigned int *ar)
 {
     struct desc_struct desc;
-    const struct desc_struct *pdesc;
+    const struct desc_struct *pdesc = gdt_ldt_desc_ptr(gate_sel);
 
-    pdesc = (const struct desc_struct *)
-        (!(gate_sel & 4) ? GDT_VIRT_START(v) : LDT_VIRT_START(v))
-        + (gate_sel >> 3);
     if ( (gate_sel < 4) ||
          ((gate_sel >= FIRST_RESERVED_GDT_BYTE) && !(gate_sel & 4)) ||
          __get_user(desc, pdesc) )
index 5750c7699b52d6be5333770c694e46b064c067cc..1b609117a3a257637fef126c37702bf01050f346 100644 (file)
@@ -33,11 +33,7 @@ int pv_emul_read_descriptor(unsigned int sel, const struct vcpu *v,
 
     if ( sel < 4)
         desc.b = desc.a = 0;
-    else if ( __get_user(desc,
-                         (const struct desc_struct *)(!(sel & 4)
-                                                      ? GDT_VIRT_START(v)
-                                                      : LDT_VIRT_START(v))
-                         + (sel >> 3)) )
+    else if ( __get_user(desc, gdt_ldt_desc_ptr(sel)) )
         return 0;
     if ( !insn_fetch )
         desc.b &= ~_SEGMENT_L;
index 656c12f62de90bd970e3cbc0119a6700c4e23a54..9d587947f5aba1fae6ee88fcf5ad2ef88558f58c 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef __PV_EMULATE_H__
 #define __PV_EMULATE_H__
 
+#include <asm/processor.h>
 #include <asm/x86_emulate.h>
 
 int pv_emul_read_descriptor(unsigned int sel, const struct vcpu *v,
@@ -16,4 +17,14 @@ static inline int pv_emul_is_mem_write(const struct x86_emulate_state *state,
                                               : X86EMUL_UNHANDLEABLE;
 }
 
+/* Return a pointer to the GDT/LDT descriptor referenced by sel. */
+static inline const struct desc_struct *gdt_ldt_desc_ptr(unsigned int sel)
+{
+    const struct vcpu *curr = current;
+    const struct desc_struct *tbl = (void *)
+        ((sel & X86_XEC_TI) ? LDT_VIRT_START(curr) : GDT_VIRT_START(curr));
+
+    return &tbl[sel >> 3];
+}
+
 #endif /* __PV_EMULATE_H__ */