Skip to contentSkip to conversation

x86/xenoprof: Fix kernel/user mode detection for HVM

PATCH New Jacob Shin 3 participants 3 messages Oct 12, 2012 – Oct 15, 2012

lore
Share & tools
lore

Jacob Shin

author
#0 lore raw

PATCH 1/1 x86/xenoprof: Fix kernel/user mode detection for HVM

x86/xenoprof: Fix kernel/user mode detection for HVM

While trying oprofile under Xen, I noticed that HVM passive domain's kernel addresses were showing up as user application. It turns out under HVM get_cpu_user_regs()->cs contains 0x0000beef.

xen/arch/x86/oprofile/xenoprof.c +5 -1
diff -r e0e1350dfe9b xen/arch/x86/oprofile/xenoprof.c
@@ -81,7 +81,11 @@ int xenoprofile_get_mode(const struct vc
         return 2;
 
     if ( is_hvm_vcpu(v) )
-        return ((regs->cs & 3) != 3);
+    {
+        struct segment_register cs;
+        hvm_get_segment_register((struct vcpu *)v, x86_seg_cs, &cs);
+        return ((cs.sel & 3) != 3);
+    }
 
     return guest_kernel_mode(v, regs);  
 }
#1 lore raw

PATCH v2 1/1 x86/xenoprof: fix kernel/user mode detection for HVM

While trying oprofile under Xen, I noticed that HVM passive domain's kernel addresses were showing up as user application. It turns out under HVM get_cpu_user_regs()->cs contains 0x0000beef.

Don't cast away const-ness. Use SS instead of CS to determine ring. Special-case real and protected mode.

xen/arch/x86/oprofile/xenoprof.c +15 -5
@@ -74,16 +74,26 @@ int compat_oprof_arch_counter(XEN_GUEST_
     return 0;
 }
 
-int xenoprofile_get_mode(const struct vcpu *v,
-                         const struct cpu_user_regs *regs)
+int xenoprofile_get_mode(struct vcpu *curr, const struct cpu_user_regs *regs)
 {
     if ( !guest_mode(regs) )
         return 2;
 
-    if ( is_hvm_vcpu(v) )
-        return ((regs->cs & 3) != 3);
+    if ( !is_hvm_vcpu(curr) )
+        return guest_kernel_mode(curr, regs);
 
-    return guest_kernel_mode(v, regs);  
+    switch ( hvm_guest_x86_mode(curr) )
+    {
+        struct segment_register ss;
+
+    case 0: /* real mode */
+        return 1;
+    case 1: /* vm86 mode */
+        return 0;
+    default:
+        hvm_get_segment_register(curr, x86_seg_ss, &ss);
+        return (ss.sel & 3) != 3;
+    }
 }
 
 /*
xen/include/asm-x86/xenoprof.h +1 -1
@@ -51,7 +51,7 @@ struct cpu_user_regs;
 void ibs_init(void);
 extern u32 ibs_caps;
 
-int xenoprofile_get_mode(const struct vcpu *, const struct cpu_user_regs *);
+int xenoprofile_get_mode(struct vcpu *, const struct cpu_user_regs *);
 
 static inline int xenoprof_backtrace_supported(void)
 {

x86/xenoprof: fix kernel/user mode detection for HVM

While trying oprofile under Xen, I noticed that HVM passive domain's kernel addresses were showing up as user application. It turns out under HVM get_cpu_user_regs()->cs contains 0x0000beef.

Don't cast away const-ness. Use SS instead of CS to determine ring. Special-case real and protected mode.

xen/arch/x86/oprofile/xenoprof.c +15 -5
@@ -74,16 +74,26 @@ int compat_oprof_arch_counter(XEN_GUEST_
     return 0;
 }
 
-int xenoprofile_get_mode(const struct vcpu *v,
-                         const struct cpu_user_regs *regs)
+int xenoprofile_get_mode(struct vcpu *curr, const struct cpu_user_regs *regs)
 {
     if ( !guest_mode(regs) )
         return 2;
 
-    if ( is_hvm_vcpu(v) )
-        return ((regs->cs & 3) != 3);
+    if ( !is_hvm_vcpu(curr) )
+        return guest_kernel_mode(curr, regs);
 
-    return guest_kernel_mode(v, regs);  
+    switch ( hvm_guest_x86_mode(curr) )
+    {
+        struct segment_register ss;
+
+    case 0: /* real mode */
+        return 1;
+    case 1: /* vm86 mode */
+        return 0;
+    default:
+        hvm_get_segment_register(curr, x86_seg_ss, &ss);
+        return (ss.sel & 3) != 3;
+    }
 }
 
 /*
xen/include/asm-x86/xenoprof.h +1 -1
@@ -51,7 +51,7 @@ struct cpu_user_regs;
 void ibs_init(void);
 extern u32 ibs_caps;
 
-int xenoprofile_get_mode(const struct vcpu *, const struct cpu_user_regs *);
+int xenoprofile_get_mode(struct vcpu *, const struct cpu_user_regs *);
 
 static inline int xenoprof_backtrace_supported(void)
 {

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

Acked-by on v2 1/1
#2 lore raw

Re: [PATCH, v2] x86/xenoprof: fix kernel/user mode detection for HVM

Quoted 10 lines · Jan Beulich

While trying oprofile under Xen, I noticed that HVM passive domain's kernel addresses were showing up as user application. It turns out under HVM get_cpu_user_regs()->cs contains 0x0000beef.

Don't cast away const-ness. Use SS instead of CS to determine ring. Special-case real and protected mode.

Quoted 52 lines
xen/arch/x86/oprofile/xenoprof.c +15 -5
@@ -74,16 +74,26 @@ int compat_oprof_arch_counter(XEN_GUEST_
     return 0;
 }
 
-int xenoprofile_get_mode(const struct vcpu *v,
-                         const struct cpu_user_regs *regs)
+int xenoprofile_get_mode(struct vcpu *curr, const struct cpu_user_regs *regs)
 {
     if ( !guest_mode(regs) )
         return 2;
 
-    if ( is_hvm_vcpu(v) )
-        return ((regs->cs & 3) != 3);
+    if ( !is_hvm_vcpu(curr) )
+        return guest_kernel_mode(curr, regs);
 
-    return guest_kernel_mode(v, regs);
+    switch ( hvm_guest_x86_mode(curr) )
+    {
+        struct segment_register ss;
+
+    case 0: /* real mode */
+        return 1;
+    case 1: /* vm86 mode */
+        return 0;
+    default:
+        hvm_get_segment_register(curr, x86_seg_ss, &ss);
+        return (ss.sel & 3) != 3;
+    }
 }
 
 /*
xen/include/asm-x86/xenoprof.h +1 -1
@@ -51,7 +51,7 @@ struct cpu_user_regs;
 void ibs_init(void);
 extern u32 ibs_caps;
 
-int xenoprofile_get_mode(const struct vcpu *, const struct cpu_user_regs *);
+int xenoprofile_get_mode(struct vcpu *, const struct cpu_user_regs *);
 
 static inline int xenoprof_backtrace_supported(void)
 {

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