[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] Initialising MXCSR in PV guests



We're seeing floating point exceptions on some (but not all) machines when 
doing FP operations inside MiniOS.

It looks like the FPU and SSE control registers are not set to good default 
values (which by default mask various FP exceptions) when MiniOS is started as 
a PV guest. If the call to fpu_init below is not made, the division generates a 
precision error and fails.  

Do PV guests all need to explicitly initialise MXCSR, or has something changed 
in Xen to trigger this now?  It seems to have been set in the past, and only 
happens on some hosts.

--- extras/mini-os/kernelorig.c    2012-11-30 15:59:46.308837388 +0000
+++ extras/mini-os/kernel.c    2012-11-30 16:02:12.532408020 +0000
@@ -64,10 +64,32 @@
     }
 }
 
+#define MXCSR_DEFAULT 0x1f80
+
+static void fpu_init(void)
+{
+  unsigned long val = MXCSR_DEFAULT;
+  asm volatile ( "fninit; ldmxcsr %0" : : "m" (val) );
+}
+
 /* This should be overridden by the application we are linked against. */
 __attribute__((weak)) int app_main(start_info_t *si)
 {
     printk("Dummy main: start_info=%p\n", si);
+
+    double x = 10.0;
+    double y = 3.0;
+
+    /* This division only works when the fpu_init call is made.
+     * The call sets the fpu and sse control registers to default values,
+     * which mask various exceptions including the precision exception.
+     * When the fpu_init call is not made the division below fails with
+     * a precision exception.
+     */
+    double z = x/y;
+
+    printk("x = %d, y = %d, z = %d\n", (int) x, (int) y, (int) z);
+
     return 0;
 }
 
@@ -98,6 +120,9 @@
     printk("    cmd_line: %s\n",  
            si->cmd_line ? (const char *)si->cmd_line : "NULL");
 
+    /* Initialize fpu and sse */
+    fpu_init();
+
     /* Set up events. */
     init_events();
     



The offending host is:

$ uname -a
Linux Monday 3.6.2 #1 SMP PREEMPT Wed Oct 17 22:22:30 BST 2012 x86_64 GNU/Linux

$ cat /proc/cpuinfo 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model    : 58
model name      : Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
stepping        : 9
microcode       : 0x12
cpu MHz  : 3392.428
cache size      : 8192 KB
physical id     : 0
siblings        : 8
core id  : 0
cpu cores       : 4
apicid   : 0
initial apicid  : 0
fpu      : yes
fpu_exception   : yes
cpuid level     : 13
wp       : yes
flags    : fpu de tsc msr pae mce cx8 apic sep mca cmov pat clflush acpi mmx 
fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl nonstop_tsc pni 
pclmulqdq est ssse3 cx16 sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes 
xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dtherm 
fsgsbase erms
bogomips        : 6784.85
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:





_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.