Skip to contentSkip to conversation

x86 FPU switches routines.

Xenomai-core Gilles Chanteperdrix 3 participants 4 messages Oct 23, 2006 – Nov 11, 2006

lore
Share & tools
lore
#0 lore raw

Hi,

As once discussed with Heikki, and after a first missed attempt, here is a patch for x86 which attempts to cope correctly with Linux (non real-time) drivers using FPU in kernel-space.

I tested it on my laptop without drivers using FPU, and it appears to work correctly, but it would be nice if people that had problems with the previous attempt (if I remember correctly, on old pentiums and on AMD processors) could test this patch.

Thanks in advance.

Signature · 1 line
                                                 Gilles Chanteperdrix
asm-i386/system.h +1 -0
Index: include/asm-i386/system.h
===================================================================
@@ -69,6 +69,7 @@
     /* FPU context bits for root thread. */
     unsigned is_root: 1;
     unsigned ts_usedfpu: 1;
+    unsigned cr0_ts: 1;
 
 } xnarchtcb_t;
 
asm-i386/bits/pod.h +19 -8
Index: include/asm-i386/bits/pod.h
===================================================================
@@ -44,6 +44,7 @@
 	/* Remember the preempted Linux task pointer. */
 	rootcb->user_task = rootcb->active_task = current;
 	rootcb->ts_usedfpu = wrap_test_fpu_used(current) != 0;
+	rootcb->cr0_ts = (read_cr0() & 8) != 0;
 	/* So that xnarch_save_fpu() will operate on the right FPU area. */
 	rootcb->fpup = &rootcb->user_task->thread.i387;
 }
@@ -265,12 +266,21 @@
 {
 	struct task_struct *task = tcb->user_task;
 
-	if (task) {
-		if (!wrap_test_fpu_used(task))
+	if (!tcb->is_root) {
+		if (task) {
+			/* fpu not used or already saved by __switch_to. */
+			if (!wrap_test_fpu_used(task))
+				return;
+
+			/* Tell Linux that we already saved the state of the FPU
+		   	hardware of this task. */
+			wrap_clear_fpu_used(task);
+		}
+	} else {
+		if (tcb->cr0_ts || 
+		    (tcb->ts_usedfpu && !wrap_test_fpu_used(task)))
 			return;
 
-		/* Tell Linux that we already saved the state of the FPU
-		   hardware of this task. */
 		wrap_clear_fpu_used(task);
 	}
 
@@ -298,13 +308,14 @@
 			wrap_set_fpu_used(task);
 		}
 	} else {
-		/* Restore state of FPU if TS_USEFPU bit was armed. */
-		if (!tcb->ts_usedfpu) {
+		/* Restore state of FPU only if TS bit in cr0 was clear. */
+		if (tcb->cr0_ts) {
 			stts();
 			return;
 		}
 
-		wrap_set_fpu_used(task);
+		if (tcb->ts_usedfpu)
+			wrap_set_fpu_used(task);
 	}
 
 	/* Restore the FPU hardware with valid fp registers from a
@@ -336,7 +347,7 @@
 			}
 		}
 	} else {
-		if (!tcb->ts_usedfpu)
+		if (tcb->cr0_ts)
 			return;
 
 		xnarch_restore_fpu(tcb);
#1 lore raw
Quoted 11 lines · Gilles Chanteperdrix

Hi,

As once discussed with Heikki, and after a first missed attempt, here is a patch for x86 which attempts to cope correctly with Linux (non real-time) drivers using FPU in kernel-space.

I tested it on my laptop without drivers using FPU, and it appears to work correctly, but it would be nice if people that had problems with the previous attempt (if I remember correctly, on old pentiums and on AMD processors) could test this patch.

Just fired up on a Pentium MMX which used to have problems with the first approad. Switchtest reports no problems.

What about writing a simple test case for the non-RT in-kernel FPU usage?

Jan

#2 lore raw
Quoted 14 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

Hi,

As once discussed with Heikki, and after a first missed attempt, here is a patch for x86 which attempts to cope correctly with Linux (non real-time) drivers using FPU in kernel-space.

I tested it on my laptop without drivers using FPU, and it appears to work correctly, but it would be nice if people that had problems with the previous attempt (if I remember correctly, on old pentiums and on AMD processors) could test this patch.

Quoted 4 lines

Just fired up on a Pentium MMX which used to have problems with the first approad. Switchtest reports no problems.

What about writing a simple test case for the non-RT in-kernel FPU usage?

I plan to add it to the switchtest driver, but I wanted to make sure that the new support already works in the usual case.

Signature · 1 line
                                                 Gilles Chanteperdrix
#3 lore raw
In reply to Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

As once discussed with Heikki, and after a first missed attempt, here is a patch for x86 which attempts to cope correctly with Linux (non real-time) drivers using FPU in kernel-space.

I tested it on my laptop without drivers using FPU, and it appears to work correctly, but it would be nice if people that had problems with the previous attempt (if I remember correctly, on old pentiums and on AMD processors) could test this patch.

Ok, nobody has complained so far, it works for me too on a few test boxen, so I'm going to merge this patch for 2.3-rc2.

So please everybody, let's give this hell _now_ on as many x86 boxen as we can, checking out the trunk, so that we won't reiterate the 2.2.3 mess.

Signature · 1 line
Philippe.