Skip to contentSkip to conversation

Refactor x86 trap handling code

PATCH Superseded Wei Liu 3 participants 21 messages Apr 6, 2017 – Apr 21, 2017

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover Refactor x86 trap handling code

This series splits PV code related to trap handling to files under pv directory.

The final two patches are clean-up that seem easy enough to do.

Wei Liu (7):
x86: move x86_64/entry.S to pv directory
x86: move x86_64/compat/entry.S to pv/compat x86/traps: lift do_guest_trap to traps.h
x86/traps: move all PV emulation and hypercalls to pv/traps.c
x86_64: move PV specific code under pv/x86_64
x86: s/bool_t/bool/g in pv/traps.c
x86: clean up traps.c

 xen/arch/x86/pv/Makefile                    |    5 +
 xen/arch/x86/{x86_64 => pv}/compat/Makefile |    0
 xen/arch/x86/{x86_64 => pv}/compat/entry.S  |    0
 xen/arch/x86/{x86_64 => pv}/entry.S         |    0
 xen/arch/x86/pv/traps.c                     | 2152 ++++++++++++++++++++++++
 xen/arch/x86/pv/x86_64/Makefile             |    1 +
 xen/arch/x86/{ => pv}/x86_64/compat/traps.c |    0
 xen/arch/x86/pv/x86_64/traps.c              |  398 +++++
 xen/arch/x86/traps.c                        | 2346 ++-------------------------
 xen/arch/x86/x86_64/Makefile                |    3 -
 xen/arch/x86/x86_64/traps.c                 |  377 -----
 xen/include/asm-x86/domain.h                |   12 +
 xen/include/asm-x86/traps.h                 |   19 +
 13 files changed, 2699 insertions(+), 2614 deletions(-)
 rename xen/arch/x86/{x86_64 => pv}/compat/Makefile (100%)
 rename xen/arch/x86/{x86_64 => pv}/compat/entry.S (100%)
 rename xen/arch/x86/{x86_64 => pv}/entry.S (100%)
 create mode 100644 xen/arch/x86/pv/traps.c
 create mode 100644 xen/arch/x86/pv/x86_64/Makefile
 rename xen/arch/x86/{ => pv}/x86_64/compat/traps.c (100%)
 create mode 100644 xen/arch/x86/pv/x86_64/traps.c
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#1 lore raw

PATCH 1/7 x86: move x86_64/entry.S to pv directory

That file contains 64 bit PV guest entry points.

No functional change.

---
 xen/arch/x86/pv/Makefile            | 1 +
 xen/arch/x86/{x86_64 => pv}/entry.S | 0
 xen/arch/x86/x86_64/Makefile        | 1 -
 3 files changed, 1 insertion(+), 1 deletion(-)
 rename xen/arch/x86/{x86_64 => pv}/entry.S (100%)
xen/arch/x86/pv/Makefile +1 -0
index ea94599438..08b6a3af64 100644
@@ -1,2 +1,3 @@
 obj-y += hypercall.o
 obj-bin-y += dom0_build.init.o
+obj-bin-y += entry.o
xen/arch/x86/pv/entry.S renamed from xen/arch/x86/x86_64/entry.S +0 -0
similarity index 100%
rename from xen/arch/x86/x86_64/entry.S
rename to xen/arch/x86/pv/entry.S
xen/arch/x86/x86_64/Makefile +0 -1
index d8815e78b0..1f0a4f7bb2 100644
@@ -1,6 +1,5 @@
 subdir-y += compat
 
-obj-bin-y += entry.o
 obj-bin-y += gpr_switch.o
 obj-y += traps.o
 obj-$(CONFIG_KEXEC) += machine_kexec.o
Signature · 7 lines
2.11.0


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

Re: [PATCH for-next 1/7] x86: move x86_64/entry.S to pv directory

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

That file contains 64 bit PV guest entry points.

And a lot more, like interrupt and exception handling ones, which aren't PV-specific at all. I'm sorry, but NAK.

Jan

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

author
#3 lore raw

Re: [PATCH for-next 1/7] x86: move x86_64/entry.S to pv directory

Quoted 6 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:

That file contains 64 bit PV guest entry points.

And a lot more, like interrupt and exception handling ones, which aren't PV-specific at all. I'm sorry, but NAK.

Yeah, I discovered that later. I need to further split that file unfortunately...

Quoted 2 lines

Jan

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

author
#4 lore raw

PATCH 2/7 x86: move x86_64/compat/entry.S to pv/compat

In reply to Wei Liu

That file contains 32 bit PV guest entry points.

No functional change.

---
 xen/arch/x86/pv/Makefile                    | 2 ++
 xen/arch/x86/{x86_64 => pv}/compat/Makefile | 0
 xen/arch/x86/{x86_64 => pv}/compat/entry.S  | 0
 xen/arch/x86/x86_64/Makefile                | 2 --
 4 files changed, 2 insertions(+), 2 deletions(-)
 rename xen/arch/x86/{x86_64 => pv}/compat/Makefile (100%)
 rename xen/arch/x86/{x86_64 => pv}/compat/entry.S (100%)
xen/arch/x86/pv/Makefile +2 -0
index 08b6a3af64..d8fc13f6fe 100644
@@ -1,3 +1,5 @@
+subdir-y += compat
+
 obj-y += hypercall.o
 obj-bin-y += dom0_build.init.o
 obj-bin-y += entry.o
xen/arch/x86/pv/compat/Makefile renamed from xen/arch/x86/x86_64/compat/Makefile +0 -0
similarity index 100%
rename from xen/arch/x86/x86_64/compat/Makefile
rename to xen/arch/x86/pv/compat/Makefile
xen/arch/x86/pv/compat/entry.S renamed from xen/arch/x86/x86_64/compat/entry.S +0 -0
similarity index 100%
rename from xen/arch/x86/x86_64/compat/entry.S
rename to xen/arch/x86/pv/compat/entry.S
xen/arch/x86/x86_64/Makefile +0 -2
index 1f0a4f7bb2..53e2f95e77 100644
@@ -1,5 +1,3 @@
-subdir-y += compat
-
 obj-bin-y += gpr_switch.o
 obj-y += traps.o
 obj-$(CONFIG_KEXEC) += machine_kexec.o
Signature · 7 lines
2.11.0


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

Re: [PATCH for-next 2/7] x86: move x86_64/compat/entry.S to pv/compat

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

That file contains 32 bit PV guest entry points.

No functional change.

---
 xen/arch/x86/pv/Makefile                    | 2 ++
 xen/arch/x86/{x86_64 => pv}/compat/Makefile | 0
 xen/arch/x86/{x86_64 => pv}/compat/entry.S  | 0

While this one indeed looks fine to be moved as a whole, I'm not convinced we want to retain this compat/ subdirectory level, as we don't really have CONFIG_COMPAT anymore anyway (there are only very few leftovers in x86-specific code, and it looks like they've been wrongly re-introduced not so long ago). I.e. I'd rather see this being merged into pv/entry.S.

Jan

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

#6 lore raw

Re: [PATCH for-next 2/7] x86: move x86_64/compat/entry.S to pv/compat

Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:

That file contains 32 bit PV guest entry points.

No functional change.

---
 xen/arch/x86/pv/Makefile                    | 2 ++
 xen/arch/x86/{x86_64 => pv}/compat/Makefile | 0
 xen/arch/x86/{x86_64 => pv}/compat/entry.S  | 0
Quoted 6 lines

While this one indeed looks fine to be moved as a whole, I'm not convinced we want to retain this compat/ subdirectory level, as we don't really have CONFIG_COMPAT anymore anyway (there are only very few leftovers in x86-specific code, and it looks like they've been wrongly re-introduced not so long ago). I.e. I'd rather see this being merged into pv/entry.S.

We most certainly do have CONFIG_COMPAT (and it is used), but I also want to get rid of the compat/ directories.

+1 for pv/entry.S (or alternatively to follow the Linux model and using entry_64.S and entry_64_compat.S. I guess it depends how much code we end up having which is guest-type specific, once I have finished moving all I reasonably can up into C.)

~Andrew

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

#7 lore raw

Re: [PATCH for-next 2/7] x86: move x86_64/compat/entry.S to pv/compat

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 21.04.17 at 11:50, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:

That file contains 32 bit PV guest entry points.

No functional change.

---
 xen/arch/x86/pv/Makefile                    | 2 ++
 xen/arch/x86/{x86_64 => pv}/compat/Makefile | 0
 xen/arch/x86/{x86_64 => pv}/compat/entry.S  | 0
Quoted 8 lines
Quoted 6 lines

While this one indeed looks fine to be moved as a whole, I'm not convinced we want to retain this compat/ subdirectory level, as we don't really have CONFIG_COMPAT anymore anyway (there are only very few leftovers in x86-specific code, and it looks like they've been wrongly re-introduced not so long ago). I.e. I'd rather see this being merged into pv/entry.S.

We most certainly do have CONFIG_COMPAT (and it is used),

Outside of x86-specific code, yes. In x86-specific code there's just one pair of instances, in the PV-hypercall handling code. I think this should be cleaned up.

Jan

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

author
#8 lore raw

PATCH 3/7 x86/traps: lift do_guest_trap to traps.h

In reply to Wei Liu

And rename it to pv_inject_guest_trap. It will be needed later when some PV specific code is moved.

No functional change.

---
 xen/arch/x86/traps.c         | 61 ++++++++++++++++++--------------------------
 xen/include/asm-x86/domain.h | 12 +++++++++
 2 files changed, 37 insertions(+), 36 deletions(-)
xen/arch/x86/traps.c +25 -36
index 0d54baf20e..a1981289a4 100644
@@ -684,18 +684,6 @@ void pv_inject_event(const struct x86_event *event)
     }
 }
 
-static inline void do_guest_trap(unsigned int trapnr,
-                                 const struct cpu_user_regs *regs)
-{
-    const struct x86_event event = {
-        .vector = trapnr,
-        .error_code = (((trapnr < 32) && (TRAP_HAVE_EC & (1u << trapnr)))
-                       ? regs->error_code : X86_EVENT_NO_EC),
-    };
-
-    pv_inject_event(&event);
-}
-
 static void instruction_done(struct cpu_user_regs *regs, unsigned long rip)
 {
     regs->rip = rip;
@@ -703,7 +691,7 @@ static void instruction_done(struct cpu_user_regs *regs, unsigned long rip)
     if ( regs->eflags & X86_EFLAGS_TF )
     {
         current->arch.debugreg[6] |= DR_STEP | DR_STATUS_RESERVED_ONE;
-        do_guest_trap(TRAP_debug, regs);
+        pv_inject_guest_trap(TRAP_debug, regs);
     }
 }
 
@@ -751,7 +739,7 @@ int set_guest_machinecheck_trapbounce(void)
     struct vcpu *v = current;
     struct trap_bounce *tb = &v->arch.pv_vcpu.trap_bounce;
  
-    do_guest_trap(TRAP_machine_check, guest_cpu_user_regs());
+    pv_inject_guest_trap(TRAP_machine_check, guest_cpu_user_regs());
     tb->flags &= ~TBF_EXCEPTION; /* not needed for MCE delivery path */
     return !null_trap_bounce(v, tb);
 }
@@ -764,7 +752,7 @@ int set_guest_nmi_trapbounce(void)
 {
     struct vcpu *v = current;
     struct trap_bounce *tb = &v->arch.pv_vcpu.trap_bounce;
-    do_guest_trap(TRAP_nmi, guest_cpu_user_regs());
+    pv_inject_guest_trap(TRAP_nmi, guest_cpu_user_regs());
     tb->flags &= ~TBF_EXCEPTION; /* not needed for NMI delivery path */
     return !null_trap_bounce(v, tb);
 }
@@ -794,7 +782,7 @@ void do_trap(struct cpu_user_regs *regs)
 
     if ( guest_mode(regs) )
     {
-        do_guest_trap(trapnr, regs);
+        pv_inject_guest_trap(trapnr, regs);
         return;
     }
 
@@ -1060,7 +1048,7 @@ static int emulate_forced_invalid_op(struct cpu_user_regs *regs)
     if ( current->arch.cpuid_faulting && !guest_kernel_mode(current, regs) )
     {
         regs->rip = eip;
-        do_guest_trap(TRAP_gp_fault, regs);
+        pv_inject_guest_trap(TRAP_gp_fault, regs);
         return EXCRET_fault_fixed;
     }
 
@@ -1096,7 +1084,7 @@ void do_invalid_op(struct cpu_user_regs *regs)
     {
         if ( !emulate_invalid_rdtscp(regs) &&
              !emulate_forced_invalid_op(regs) )
-            do_guest_trap(TRAP_invalid_op, regs);
+            pv_inject_guest_trap(TRAP_invalid_op, regs);
         return;
     }
 
@@ -1210,7 +1198,7 @@ void do_int3(struct cpu_user_regs *regs)
         return;
     } 
 
-    do_guest_trap(TRAP_int3, regs);
+    pv_inject_guest_trap(TRAP_int3, regs);
 }
 
 static void reserved_bit_page_fault(
@@ -3023,7 +3011,7 @@ static int emulate_privileged_op(struct cpu_user_regs *regs)
         {
             curr->arch.debugreg[6] |= ctxt.bpmatch | DR_STATUS_RESERVED_ONE;
             if ( !(curr->arch.pv_vcpu.trap_bounce.flags & TBF_EXCEPTION) )
-                do_guest_trap(TRAP_debug, regs);
+                pv_inject_guest_trap(TRAP_debug, regs);
         }
         /* fall through */
     case X86EMUL_RETRY:
@@ -3138,12 +3126,12 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
          (((ar >> 13) & 3) < (regs->cs & 3)) ||
          ((ar & _SEGMENT_TYPE) != 0xc00) )
     {
-        do_guest_trap(TRAP_gp_fault, regs);
+        pv_inject_guest_trap(TRAP_gp_fault, regs);
         return;
     }
     if ( !(ar & _SEGMENT_P) )
     {
-        do_guest_trap(TRAP_no_segment, regs);
+        pv_inject_guest_trap(TRAP_no_segment, regs);
         return;
     }
     dpl = (ar >> 13) & 3;
@@ -3159,7 +3147,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
          !(ar & _SEGMENT_P) ||
          !(ar & _SEGMENT_CODE) )
     {
-        do_guest_trap(TRAP_gp_fault, regs);
+        pv_inject_guest_trap(TRAP_gp_fault, regs);
         return;
     }
 
@@ -3177,7 +3165,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
         else
         {
             ASSERT(!ctxt.ctxt.event_pending);
-            do_guest_trap(TRAP_gp_fault, regs);
+            pv_inject_guest_trap(TRAP_gp_fault, regs);
         }
         return;
     }
@@ -3231,7 +3219,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
          (opnd_sel & ~3) != regs->error_code ||
          dpl < (opnd_sel & 3) )
     {
-        do_guest_trap(TRAP_gp_fault, regs);
+        pv_inject_guest_trap(TRAP_gp_fault, regs);
         return;
     }
 
@@ -3279,7 +3267,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
             /* Inner stack known only for kernel ring. */
             if ( (sel & 3) != GUEST_KERNEL_RPL(v->domain) )
             {
-                do_guest_trap(TRAP_gp_fault, regs);
+                pv_inject_guest_trap(TRAP_gp_fault, regs);
                 return;
             }
             esp = v->arch.pv_vcpu.kernel_sp;
@@ -3303,7 +3291,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
             stkp = (unsigned int *)(unsigned long)((unsigned int)base + esp);
             if ( !compat_access_ok(stkp - 4 - nparm, (4 + nparm) * 4) )
             {
-                do_guest_trap(TRAP_gp_fault, regs);
+                pv_inject_guest_trap(TRAP_gp_fault, regs);
                 return;
             }
             push(regs->ss);
@@ -3318,12 +3306,12 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
                      (ar & _SEGMENT_CODE) ||
                      !(ar & _SEGMENT_WR) ||
                      !check_stack_limit(ar, limit, esp + nparm * 4, nparm * 4) )
-                    return do_guest_trap(TRAP_gp_fault, regs);
+                    return pv_inject_guest_trap(TRAP_gp_fault, regs);
                 ustkp = (unsigned int *)(unsigned long)
                         ((unsigned int)base + regs->esp + nparm * 4);
                 if ( !compat_access_ok(ustkp - nparm, nparm * 4) )
                 {
-                    do_guest_trap(TRAP_gp_fault, regs);
+                    pv_inject_guest_trap(TRAP_gp_fault, regs);
                     return;
                 }
                 do
@@ -3334,7 +3322,8 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
                     rc = __get_user(parm, ustkp);
                     if ( rc )
                     {
-                        pv_inject_page_fault(0, (unsigned long)(ustkp + 1) - rc);
+                        pv_inject_page_fault(0,
+                                             (unsigned long)(ustkp + 1) - rc);
                         return;
                     }
                     push(parm);
@@ -3349,7 +3338,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
             if ( !read_descriptor(ss, v, &base, &limit, &ar, 0) ||
                  ((ar >> 13) & 3) != (sel & 3) )
             {
-                do_guest_trap(TRAP_gp_fault, regs);
+                pv_inject_guest_trap(TRAP_gp_fault, regs);
                 return;
             }
             if ( !check_stack_limit(ar, limit, esp, 2 * 4) )
@@ -3360,7 +3349,7 @@ static void emulate_gate_op(struct cpu_user_regs *regs)
             stkp = (unsigned int *)(unsigned long)((unsigned int)base + esp);
             if ( !compat_access_ok(stkp - 2, 2 * 4) )
             {
-                do_guest_trap(TRAP_gp_fault, regs);
+                pv_inject_guest_trap(TRAP_gp_fault, regs);
                 return;
             }
         }
@@ -3421,7 +3410,7 @@ void do_general_protection(struct cpu_user_regs *regs)
         if ( permit_softint(TI_GET_DPL(ti), v, regs) )
         {
             regs->rip += 2;
-            do_guest_trap(vector, regs);
+            pv_inject_guest_trap(vector, regs);
             return;
         }
     }
@@ -3440,7 +3429,7 @@ void do_general_protection(struct cpu_user_regs *regs)
     }
 
     /* Pass on GPF as is. */
-    do_guest_trap(TRAP_gp_fault, regs);
+    pv_inject_guest_trap(TRAP_gp_fault, regs);
     return;
 
  gp_in_kernel:
@@ -3660,7 +3649,7 @@ void do_device_not_available(struct cpu_user_regs *regs)
 
     if ( curr->arch.pv_vcpu.ctrlreg[0] & X86_CR0_TS )
     {
-        do_guest_trap(TRAP_no_device, regs);
+        pv_inject_guest_trap(TRAP_no_device, regs);
         curr->arch.pv_vcpu.ctrlreg[0] &= ~X86_CR0_TS;
     }
     else
@@ -3733,7 +3722,7 @@ void do_debug(struct cpu_user_regs *regs)
     v->arch.debugreg[6] = read_debugreg(6);
 
     ler_enable();
-    do_guest_trap(TRAP_debug, regs);
+    pv_inject_guest_trap(TRAP_debug, regs);
     return;
 
  out:
xen/include/asm-x86/domain.h +12 -0
index 6420ca24b9..af47bb48e8 100644
@@ -654,6 +654,18 @@ static inline void pv_inject_page_fault(int errcode, unsigned long cr2)
     pv_inject_event(&event);
 }
 
+static inline void pv_inject_guest_trap(unsigned int trapnr,
+                                        const struct cpu_user_regs *regs)
+{
+    const struct x86_event event = {
+        .vector = trapnr,
+        .error_code = (((trapnr < 32) && (TRAP_HAVE_EC & (1u << trapnr)))
+                       ? regs->error_code : X86_EVENT_NO_EC),
+    };
+
+    pv_inject_event(&event);
+}
+
 #endif /* __ASM_DOMAIN_H__ */
 
 /*
Signature · 7 lines
2.11.0


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

Re: [PATCH for-next 3/7] x86/traps: lift do_guest_trap to traps.h

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:
xen/include/asm-x86/domain.h +0 -0
@@ -654,6 +654,18 @@ static inline void pv_inject_page_fault(int errcode, unsigned long cr2)
     pv_inject_event(&event);
 }
 

xen/include/asm-x86/domain.h static inline void pv_inject_page_fault(int errcode, unsigned long cr2) · line 666

Quoted 10 lines
xen/include/asm-x86/domain.h +10 -0
+static inline void pv_inject_guest_trap(unsigned int trapnr,
+                                        const struct cpu_user_regs *regs)
+{
+    const struct x86_event event = {
+        .vector = trapnr,
+        .error_code = (((trapnr < 32) && (TRAP_HAVE_EC & (1u << trapnr)))
+                       ? regs->error_code : X86_EVENT_NO_EC),
+    };
+
+    pv_inject_event(&event);

Considering this function's name, why do we need the "guest" infix in the renamed function? Also, when you move callers of this, why would you not move all of them (eliminating the need for this to be put in a header)?

Jan

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

author
#10 lore raw

Re: [PATCH for-next 3/7] x86/traps: lift do_guest_trap to traps.h

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:
xen/include/asm-x86/domain.h +0 -0
@@ -654,6 +654,18 @@ static inline void pv_inject_page_fault(int errcode, unsigned long cr2)
     pv_inject_event(&event);
 }
 
Quoted 13 lines

xen/include/asm-x86/domain.h static inline void pv_inject_page_fault(int errcode, unsigned long cr2) · line 666

Quoted 10 lines
xen/include/asm-x86/domain.h +10 -0
+static inline void pv_inject_guest_trap(unsigned int trapnr,
+                                        const struct cpu_user_regs *regs)
+{
+    const struct x86_event event = {
+        .vector = trapnr,
+        .error_code = (((trapnr < 32) && (TRAP_HAVE_EC & (1u << trapnr)))
+                       ? regs->error_code : X86_EVENT_NO_EC),
+    };
+
+    pv_inject_event(&event);

Considering this function's name, why do we need the "guest" infix in the renamed function? Also, when you move callers of this, why

No particular reason. I can delete the "guest" infix.

Quoted 3 lines

would you not move all of them (eliminating the need for this to be put in a header)?

Yes I can do that.

Quoted 2 lines

Jan

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

author
#11 lore raw

PATCH 4/7 x86/traps: move all PV emulation and hypercalls to pv/traps.c

In reply to Wei Liu

Move the following emulation code:
1. invalid op
2. rdtsc
3. privilege instructions
4. gate operation

Move the following hypercalls:
1. do_set_trap_table
2. do_set_debugreg
3. do_get_debugreg
4. do_fpu_taskswitch
5. do_set_trap_table

Some helper functions which are PV only are also moved.

The code movement requires making a functions non-static. They are added to traps.h.

No functional change.

---
 xen/arch/x86/pv/Makefile    |    1 +
 xen/arch/x86/pv/traps.c     | 2152 +++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/traps.c        | 2357 +++----------------------------------------
 xen/include/asm-x86/traps.h |   19 +
 4 files changed, 2295 insertions(+), 2234 deletions(-)
 create mode 100644 xen/arch/x86/pv/traps.c
xen/arch/x86/pv/Makefile +1 -0
index d8fc13f6fe..c996dcc149 100644
@@ -3,3 +3,4 @@ subdir-y += compat
 obj-y += hypercall.o
 obj-bin-y += dom0_build.init.o
 obj-bin-y += entry.o
+obj-y += traps.o
xen/arch/x86/pv/traps.c new file +2152 -0

2,152 lines not on this page · raw message

xen/arch/x86/traps.c +123 -2234

844 lines not on this page · raw message

xen/include/asm-x86/traps.h +19 -0
index e3884d8406..a8656c3574 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 7 lines
2.11.0


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

Part of the diff is not shown here. View the raw message.

#12 lore raw

Re: [PATCH for-next 4/7] x86/traps: move all PV emulation and hypercalls to pv/traps.c

xen/arch/x86/traps.c void __init do_early_page_fault(struct · line 1365

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:
xen/arch/x86/traps.c +2 -1
@@ -1576,1963 +1358,173 @@ void __init do_early_page_fault(struct 
 cpu_user_regs *regs)
     }
 }
 
-long do_fpu_taskswitch(int set)
+void do_general_protection(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
+    unsigned long fixup;

Same comment as on one of the patches in the other series: For reviewers' sake, if the diff can't be produced such that plain removals also come out as such, please split this into smaller steps.

Quoted 14 lines
xen/include/asm-x86/traps.h +8 -0
@@ -51,4 +51,23 @@ uint32_t guest_io_read(unsigned int port, unsigned int bytes,
 void guest_io_write(unsigned int port, unsigned int bytes, uint32_t data,
                     struct domain *);
 
+int emulate_privileged_op(struct cpu_user_regs *regs);
+int emulate_invalid_rdtscp(struct cpu_user_regs *regs);
+int emulate_forced_invalid_op(struct cpu_user_regs *regs);
+void emulate_gate_op(struct cpu_user_regs *regs);
+
+static inline const char *trapstr(unsigned int trapnr)
+{
+    static const char * const strings[] = {

xen/include/asm-x86/traps.h uint32_t guest_io_read(unsigned int port, unsigned int bytes, · line 71

Quoted 10 lines
xen/include/asm-x86/traps.h +10 -0
+        "divide error", "debug", "nmi", "bkpt", "overflow", "bounds",
+        "invalid opcode", "device not available", "double fault",
+        "coprocessor segment", "invalid tss", "segment not found",
+        "stack error", "general protection fault", "page fault",
+        "spurious interrupt", "coprocessor error", "alignment check",
+        "machine check", "simd error", "virtualisation exception"
+    };
+
+    return trapnr < ARRAY_SIZE(strings) ? strings[trapnr] : "???";
+}

This doesn't belong into a header - using it from more than one CU would mean multiple instances of the static array and strings. This isn't C++, where they can be folded by the linker. We don't need this on any fast paths (as the intention here is to provide input to printk(), which is inherently slow), so there's no reason not to make this an out of line function.

Jan

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

author
#13 lore raw

PATCH 5/7 x86_64: move PV specific code under pv/x86_64

In reply to Wei Liu

No functional change.

---
 xen/arch/x86/pv/Makefile                    |   1 +
 xen/arch/x86/pv/x86_64/Makefile             |   1 +
 xen/arch/x86/{ => pv}/x86_64/compat/traps.c |   0
 xen/arch/x86/pv/x86_64/traps.c              | 398 ++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/traps.c                 | 377 --------------------------
 5 files changed, 400 insertions(+), 377 deletions(-)
 create mode 100644 xen/arch/x86/pv/x86_64/Makefile
 rename xen/arch/x86/{ => pv}/x86_64/compat/traps.c (100%)
 create mode 100644 xen/arch/x86/pv/x86_64/traps.c
xen/arch/x86/pv/Makefile +1 -0
index c996dcc149..d4aca44c06 100644
@@ -1,4 +1,5 @@
 subdir-y += compat
+subdir-y += x86_64
 
 obj-y += hypercall.o
 obj-bin-y += dom0_build.init.o
xen/arch/x86/pv/x86_64/Makefile new file +1 -0
new file mode 100644
index 0000000000..c55c340bc1
@@ -0,0 +1 @@
+obj-y += traps.o
xen/arch/x86/pv/x86_64/compat/traps.c renamed from xen/arch/x86/x86_64/compat/traps.c +0 -0
similarity index 100%
rename from xen/arch/x86/x86_64/compat/traps.c
rename to xen/arch/x86/pv/x86_64/compat/traps.c
xen/arch/x86/pv/x86_64/traps.c new file +398 -0

398 lines not on this page · raw message

xen/arch/x86/x86_64/traps.c +0 -377

383 lines not on this page · raw message

Signature · 7 lines
2.11.0


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

Re: [PATCH for-next 5/7] x86_64: move PV specific code under pv/x86_64

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:

No functional change.

Quoted 10 lines
---
 xen/arch/x86/pv/Makefile                    |   1 +
 xen/arch/x86/pv/x86_64/Makefile             |   1 +
 xen/arch/x86/{ => pv}/x86_64/compat/traps.c |   0
 xen/arch/x86/pv/x86_64/traps.c              | 398 ++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/traps.c                 | 377 --------------------------
 5 files changed, 400 insertions(+), 377 deletions(-)
 create mode 100644 xen/arch/x86/pv/x86_64/Makefile
 rename xen/arch/x86/{ => pv}/x86_64/compat/traps.c (100%)
 create mode 100644 xen/arch/x86/pv/x86_64/traps.c

Please don't - we really should try to remove all those x86_64/ subdirectories (and I must have overlooked this same aspect earlier on). There's no reason to believe the current leftovers from the 32-/64-bit split are going to be appropriate for a hypothetical successor architecture to x86-64.

xen/arch/x86/x86_64/traps.c void do_double_fault(struct cpu_user_regs *regs) · line 385

Quoted 9 lines
xen/arch/x86/x86_64/traps.c +0 -9
-void subarch_percpu_traps_init(void)
-{
-    unsigned long stack_bottom = get_stack_bottom();
-    unsigned long stub_va = this_cpu(stubs.addr);
-    unsigned char *stub_page;
-    unsigned int offset;
-
-    /* IST_MAX IST pages + 1 syscall page + 1 guard page + primary stack. */
-    BUILD_BUG_ON((IST_MAX + 2) * PAGE_SIZE + PRIMARY_STACK_SIZE > STACK_SIZE);

Even if it's only this one line - this isn't PV-specific, and hence shouldn't be moved. It is also inappropriate for a function with this name to live in PV-specific code.

xen/arch/x86/x86_64/traps.c void do_double_fault(struct cpu_user_regs *regs) · line 632

Quoted 10 lines
xen/arch/x86/x86_64/traps.c +0 -10
-void hypercall_page_initialise(struct domain *d, void *hypercall_page)
-{
-    memset(hypercall_page, 0xCC, PAGE_SIZE);
-    if ( is_hvm_domain(d) )
-        hvm_hypercall_page_initialise(d, hypercall_page);
-    else if ( !is_pv_32bit_domain(d) )
-        hypercall_page_initialise_ring3_kernel(hypercall_page);
-    else
-        hypercall_page_initialise_ring1_kernel(hypercall_page);
-}

Same for this function, the more that it calls a HVM one.

Jan

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

author
#15 lore raw

Re: [PATCH for-next 5/7] x86_64: move PV specific code under pv/x86_64

Quoted 15 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:

No functional change.

Quoted 10 lines
---
 xen/arch/x86/pv/Makefile                    |   1 +
 xen/arch/x86/pv/x86_64/Makefile             |   1 +
 xen/arch/x86/{ => pv}/x86_64/compat/traps.c |   0
 xen/arch/x86/pv/x86_64/traps.c              | 398 ++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/traps.c                 | 377 --------------------------
 5 files changed, 400 insertions(+), 377 deletions(-)
 create mode 100644 xen/arch/x86/pv/x86_64/Makefile
 rename xen/arch/x86/{ => pv}/x86_64/compat/traps.c (100%)
 create mode 100644 xen/arch/x86/pv/x86_64/traps.c
Quoted 6 lines

Please don't - we really should try to remove all those x86_64/ subdirectories (and I must have overlooked this same aspect earlier on). There's no reason to believe the current leftovers from the 32-/64-bit split are going to be appropriate for a hypothetical successor architecture to x86-64.

Fine with me to remove x86_64 directories.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/x86_64/traps.c +0 -5
-void subarch_percpu_traps_init(void)
-{
-    unsigned long stack_bottom = get_stack_bottom();
-    unsigned long stub_va = this_cpu(stubs.addr);
-    unsigned char *stub_page;
Quoted 9 lines

xen/arch/x86/x86_64/traps.c void do_double_fault(struct cpu_user_regs *regs) · line 385

Quoted 4 lines
xen/arch/x86/x86_64/traps.c +0 -4
-    unsigned int offset;
-
-    /* IST_MAX IST pages + 1 syscall page + 1 guard page + primary stack. */
-    BUILD_BUG_ON((IST_MAX + 2) * PAGE_SIZE + PRIMARY_STACK_SIZE > STACK_SIZE);

Even if it's only this one line - this isn't PV-specific, and hence shouldn't be moved. It is also inappropriate for a function with this name to live in PV-specific code.

I see your point for this BUILD_BUG_ON. But the rest of this function is setting up trampoline stub for syscall and sysenter, which, AFAICT, are only ever going to be used by PV guests. What did I miss? May be I should rename this function?

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/x86_64/traps.c +0 -4
-void hypercall_page_initialise(struct domain *d, void *hypercall_page)
-{
-    memset(hypercall_page, 0xCC, PAGE_SIZE);
-    if ( is_hvm_domain(d) )
Quoted 9 lines

xen/arch/x86/x86_64/traps.c void do_double_fault(struct cpu_user_regs *regs) · line 632

Quoted 6 lines
xen/arch/x86/x86_64/traps.c +0 -6
-        hvm_hypercall_page_initialise(d, hypercall_page);
-    else if ( !is_pv_32bit_domain(d) )
-        hypercall_page_initialise_ring3_kernel(hypercall_page);
-    else
-        hypercall_page_initialise_ring1_kernel(hypercall_page);
-}

Same for this function, the more that it calls a HVM one.

Yes this one needs to stay in commmon code.

Wei.

Quoted 2 lines

Jan

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

#16 lore raw

Re: [PATCH for-next 5/7] x86_64: move PV specific code under pv/x86_64

Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 21.04.17 at 14:52, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich

xen/arch/x86/x86_64/traps.c void do_double_fault(struct cpu_user_regs *regs) · line 385

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:
xen/arch/x86/x86_64/traps.c +0 -9
-void subarch_percpu_traps_init(void)
-{
-    unsigned long stack_bottom = get_stack_bottom();
-    unsigned long stub_va = this_cpu(stubs.addr);
-    unsigned char *stub_page;
-    unsigned int offset;
-
-    /* IST_MAX IST pages + 1 syscall page + 1 guard page + primary stack. */
-    BUILD_BUG_ON((IST_MAX + 2) * PAGE_SIZE + PRIMARY_STACK_SIZE > STACK_SIZE);

Even if it's only this one line - this isn't PV-specific, and hence shouldn't be moved. It is also inappropriate for a function with this name to live in PV-specific code.

Quoted 4 lines

I see your point for this BUILD_BUG_ON. But the rest of this function is setting up trampoline stub for syscall and sysenter, which, AFAICT, are only ever going to be used by PV guests. What did I miss? May be I should rename this function?

No, you should keep it for this BUILD_BUG_ON() and then call the new PV function from here. Or alternatively both could be moved into the caller (in which case, yes, you should effectively rename the function).

Jan

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

author
#17 lore raw

PATCH 6/7 x86: s/bool_t/bool/g in pv/traps.c

In reply to Wei Liu

No functional change.

---
 xen/arch/x86/pv/traps.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
xen/arch/x86/pv/traps.c +6 -6
index 0f09d858f6..64f2ffbd69 100644
@@ -373,7 +373,7 @@ static int read_descriptor(unsigned int sel,
                            unsigned long *base,
                            unsigned long *limit,
                            unsigned int *ar,
-                           bool_t insn_fetch)
+                           bool insn_fetch)
 {
     struct desc_struct desc;
 
@@ -640,7 +640,7 @@ static int priv_op_read_segment(enum x86_segment seg,
 }
 
 /* Perform IOPL check between the vcpu's shadowed IOPL, and the assumed cpl. */
-static bool_t iopl_ok(const struct vcpu *v, const struct cpu_user_regs *regs)
+static bool iopl_ok(const struct vcpu *v, const struct cpu_user_regs *regs)
 {
     unsigned int cpl = guest_kernel_mode(v, regs) ?
         (VM_ASSIST(v->domain, architectural_iopl) ? 0 : 1) : 3;
@@ -690,8 +690,8 @@ static int guest_io_okay(
 }
 
 /* Has the administrator granted sufficient permission for this I/O access? */
-static bool_t admin_io_okay(unsigned int port, unsigned int bytes,
-                            const struct domain *d)
+static bool admin_io_okay(unsigned int port, unsigned int bytes,
+                          const struct domain *d)
 {
     /*
      * Port 0xcf8 (CONFIG_ADDRESS) is only visible for DWORD accesses.
@@ -707,8 +707,8 @@ static bool_t admin_io_okay(unsigned int port, unsigned int bytes,
     return ioports_access_permitted(d, port, port + bytes - 1);
 }
 
-static bool_t pci_cfg_ok(struct domain *currd, unsigned int start,
-                         unsigned int size, uint32_t *write)
+static bool pci_cfg_ok(struct domain *currd, unsigned int start,
+                       unsigned int size, uint32_t *write)
 {
     uint32_t machine_bdf;
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 6/7
#18 lore raw

Re: [PATCH for-next 6/7] x86: s/bool_t/bool/g in pv/traps.c

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 06.04.17 at 19:14, <[email protected]> wrote:

No functional change.

In fact I wouldn't mind such purely cosmetic adjustments to be done while moving code around.

Jan

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

author
#19 lore raw

PATCH 7/7 x86: clean up traps.c

In reply to Wei Liu

Delete all trailing white spaces. Replace bool_t with bool.

No functional change.

---
 xen/arch/x86/traps.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
xen/arch/x86/traps.c +25 -25
index 351bb950d8..4481615e2c 100644
@@ -1,18 +1,18 @@
 /******************************************************************************
  * arch/x86/traps.c
- * 
+ *
  * Modifications to Linux original are copyright (c) 2002-2004, K A Fraser
- * 
+ *
  * 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, see <http://www.gnu.org/licenses/>.
  */
@@ -110,7 +110,7 @@ void (*ioemul_handle_quirk)(
 static int debug_stack_lines = 20;
 integer_param("debug_stack_lines", debug_stack_lines);
 
-static bool_t opt_ler;
+static bool opt_ler;
 boolean_param("ler", opt_ler);
 
 #define stack_words_per_line 4
@@ -525,7 +525,7 @@ void vcpu_show_execution_state(struct vcpu *v)
 }
 
 static cpumask_t show_state_mask;
-static bool_t opt_show_all;
+static bool opt_show_all;
 boolean_param("async-show-all", opt_show_all);
 
 static int nmi_show_execution_state(const struct cpu_user_regs *regs, int cpu)
@@ -548,7 +548,7 @@ static int nmi_show_execution_state(const struct cpu_user_regs *regs, int cpu)
  * are disabled). In such situations we can't do much that is safe. We try to
  * print out some tracing and then we just spin.
  */
-void fatal_trap(const struct cpu_user_regs *regs, bool_t show_remote)
+void fatal_trap(const struct cpu_user_regs *regs, bool show_remote)
 {
     static DEFINE_PER_CPU(char, depth);
     unsigned int trapnr = regs->entry_vector;
@@ -978,7 +978,7 @@ void do_int3(struct cpu_user_regs *regs)
     {
         debugger_trap_fatal(TRAP_int3, regs);
         return;
-    } 
+    }
 
     pv_inject_guest_trap(TRAP_int3, regs);
 }
@@ -1328,9 +1328,9 @@ void do_page_fault(struct cpu_user_regs *regs)
 
 /*
  * Early #PF handler to print CR2, error code, and stack.
- * 
+ *
  * We also deal with spurious faults here, even though they should never happen
- * during early boot (an issue was seen once, but was most likely a hardware 
+ * during early boot (an issue was seen once, but was most likely a hardware
  * problem).
  */
 void __init do_early_page_fault(struct cpu_user_regs *regs)
@@ -1374,7 +1374,7 @@ void do_general_protection(struct cpu_user_regs *regs)
 
     /*
      * Cunning trick to allow arbitrary "INT n" handling.
-     * 
+     *
      * We set DPL == 0 on all vectors in the IDT. This prevents any INT <n>
      * instruction from trapping to the appropriate vector, when that might not
      * be expected by Xen or the guest OS. For example, that entry might be for
@@ -1382,12 +1382,12 @@ void do_general_protection(struct cpu_user_regs *regs)
      * expect an error code on the stack (which a software trap never
      * provides), or might be a hardware interrupt handler that doesn't like
      * being called spuriously.
-     * 
+     *
      * Instead, a GPF occurs with the faulting IDT vector in the error code.
-     * Bit 1 is set to indicate that an IDT entry caused the fault. Bit 0 is 
+     * Bit 1 is set to indicate that an IDT entry caused the fault. Bit 0 is
      * clear (which got already checked above) to indicate that it's a software
      * fault, not a hardware one.
-     * 
+     *
      * NOTE: Vectors 3 and 4 are dealt with from their own handler. This is
      * okay because they can only be triggered by an explicit DPL-checked
      * instruction. The DPL specified by the guest OS for these vectors is NOT
@@ -1586,14 +1586,14 @@ static int dummy_nmi_callback(const struct cpu_user_regs *regs, int cpu)
 {
     return 0;
 }
- 
+
 static nmi_callback_t *nmi_callback = dummy_nmi_callback;
 
 void do_nmi(const struct cpu_user_regs *regs)
 {
     unsigned int cpu = smp_processor_id();
     unsigned char reason;
-    bool_t handle_unknown = 0;
+    bool handle_unknown = false;
 
     ++nmi_count(cpu);
 
@@ -1602,7 +1602,7 @@ void do_nmi(const struct cpu_user_regs *regs)
 
     if ( (nmi_watchdog == NMI_NONE) ||
          (!nmi_watchdog_tick(regs) && watchdog_force) )
-        handle_unknown = 1;
+        handle_unknown = true;
 
     /* Only the BSP gets external NMIs from the system. */
     if ( cpu == 0 )
@@ -1982,28 +1982,28 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)
 
     switch ( reg )
     {
-    case 0: 
+    case 0:
         if ( !access_ok(value, sizeof(long)) )
             return -EPERM;
-        if ( v == curr ) 
+        if ( v == curr )
             write_debugreg(0, value);
         break;
-    case 1: 
+    case 1:
         if ( !access_ok(value, sizeof(long)) )
             return -EPERM;
-        if ( v == curr ) 
+        if ( v == curr )
             write_debugreg(1, value);
         break;
-    case 2: 
+    case 2:
         if ( !access_ok(value, sizeof(long)) )
             return -EPERM;
-        if ( v == curr ) 
+        if ( v == curr )
             write_debugreg(2, value);
         break;
     case 3:
         if ( !access_ok(value, sizeof(long)) )
             return -EPERM;
-        if ( v == curr ) 
+        if ( v == curr )
             write_debugreg(3, value);
         break;
     case 6:
@@ -2013,7 +2013,7 @@ long set_debugreg(struct vcpu *v, unsigned int reg, unsigned long value)
          */
         value &= ~DR_STATUS_RESERVED_ZERO; /* reserved bits => 0 */
         value |=  DR_STATUS_RESERVED_ONE;  /* reserved bits => 1 */
-        if ( v == curr ) 
+        if ( v == curr )
             write_debugreg(6, value);
         break;
     case 7:
Signature · 7 lines
2.11.0


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