Skip to contentSkip to conversation

x86/pv: Factor out the calculation of LDT/GDT descriptor pointers

PATCH In review Andrew Cooper 3 participants 4 messages Oct 17, 2017 – Oct 26, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/pv: Factor out the calculation of LDT/GDT descriptor pointers

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.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/pv/emul-gate-op.c |  5 +----
 xen/arch/x86/pv/emulate.c      |  6 +-----
 xen/arch/x86/pv/emulate.h      | 11 +++++++++++
 3 files changed, 13 insertions(+), 9 deletions(-)
xen/arch/x86/pv/emul-gate-op.c +1 -4
index 0f89c91..14ce95e 100644
@@ -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) )
xen/arch/x86/pv/emulate.c +1 -5
index 5750c76..1b60911 100644
@@ -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;
xen/arch/x86/pv/emulate.h +11 -0
index 656c12f..9d58794 100644
@@ -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__ */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper

xen/arch/x86/pv/emulate.h static inline int pv_emul_is_mem_write(const struct x86_emulate_state *state, · line 21

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.10.17 at 17:05, <[email protected]> wrote:
xen/arch/x86/pv/emulate.h +2 -0
@@ -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)

I guess returning a pointer to const here is on the assumption that you hope we would never have a need to fiddle with the descriptor?

xen/arch/x86/pv/emulate.h static inline int pv_emul_is_mem_write(const struct x86_emulate_state *state, · line 25

Quoted 4 lines
xen/arch/x86/pv/emulate.h +4 -0
+{
+    const struct vcpu *curr = current;
+    const struct desc_struct *tbl = (void *)
+        ((sel & X86_XEC_TI) ? LDT_VIRT_START(curr) : GDT_VIRT_START(curr));

While the two happen to match, using an error code related constant with something named "selector" doesn't look to be really correct. But given the match, I don't mind it being this way.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 9 lines · Jan Beulich

xen/arch/x86/pv/emulate.h static inline int pv_emul_is_mem_write(const struct x86_emulate_state *state, · line 21

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.10.17 at 17:05, <[email protected]> wrote:
xen/arch/x86/pv/emulate.h +2 -0
@@ -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)

I guess returning a pointer to const here is on the assumption that you hope we would never have a need to fiddle with the descriptor?

PV guests only have a single method to alter the entries of exiting descriptor tables, HYPERVISOR_update_descriptor(), and that is indexed by machine address.

We don't even support updates via emulation, and I don't expect this to change moving forwards.

Of course, we can drop the const if things do change in the future.

Quoted 11 lines

xen/arch/x86/pv/emulate.h static inline int pv_emul_is_mem_write(const struct x86_emulate_state *state, · line 25

Quoted 5 lines
xen/arch/x86/pv/emulate.h +4 -0
+{
+    const struct vcpu *curr = current;
+    const struct desc_struct *tbl = (void *)
+        ((sel & X86_XEC_TI) ? LDT_VIRT_START(curr) : GDT_VIRT_START(curr));

While the two happen to match, using an error code related constant with something named "selector" doesn't look to be really correct. But given the match, I don't mind it being this way.

Thanks.

As some point, I was thinking of moving some of my XTF x86 library into Xen, and clean up the multiple/inconsistent definitions we have of various architectural bits, but that is a job for a different day.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel