Просмотр исходного кода

cpu: initialise the x87 FPU on every CPU and switch it per process

An AP leaves INIT with CR0.NE clear and an x87 control word of 0x0040:
every exception unmasked and 24-bit precision.  With NE clear, an
unmasked exception -- an inexact result, i.e. most floating-point
operations -- freezes the CPU at the next FPU instruction until an
external interrupt, the next 1 ms timer tick.  QuickJS computes with
doubles, so the browser's scripts ran hundreds of times slower whenever
it was scheduled on an AP (minutes at 100% CPU for google.com on the
ION, with single-precision results), while everything else stayed fast.
QEMU models neither the freeze nor the precision.

- cpu_init_fpu() on the BSP and every AP: CR0.NE|MP, clear EM/TS, FNINIT.
- The x87 registers were shared by every process on a CPU.  schedule()
  now FNSAVEs the outgoing process into struct process.fpu and FRSTORs
  the incoming one (FNINIT for a process that never ran).
Johannes Zellner 2 дней назад
Родитель
Сommit
e256ccbf31
8 измененных файлов с 65 добавлено и 1 удалено
  1. 9 0
      AGENTS.md
  2. 9 1
      docs/cpu.html
  3. 20 0
      kernel/cpu.c
  4. 2 0
      kernel/cpu.h
  5. 1 0
      kernel/kernel.c
  6. 4 0
      kernel/process.h
  7. 19 0
      kernel/sched.c
  8. 1 0
      kernel/smp.c

+ 9 - 0
AGENTS.md

@@ -362,6 +362,15 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   trampoline at `0x6000`), and `spinlock.h` (the IRQ-safe spinlocks).
   trampoline at `0x6000`), and `spinlock.h` (the IRQ-safe spinlocks).
   `cpu_enable_sse()` runs on every AP: an AP starts with firmware CR4, and the
   `cpu_enable_sse()` runs on every AP: an AP starts with firmware CR4, and the
   SSE2 drawing fast paths #UD until OSFXSR/OSXMMEXCPT are set.
   SSE2 drawing fast paths #UD until OSFXSR/OSXMMEXCPT are set.
+  `cpu_init_fpu()` runs on the BSP and every AP: CR0.NE|MP, no EM/TS, and
+  FNINIT.  An AP leaves INIT with CR0.NE clear and the x87 control word
+  0x0040 (every exception unmasked, 24-bit precision); each inexact result
+  then froze the CPU until the next timer tick, so QuickJS ran several
+  hundred times slower on an AP at 100% CPU (QEMU models none of it).  Each
+  process owns its x87 state: `schedule()` FNSAVEs the outgoing process
+  into `struct process.fpu` and FRSTORs the incoming one (FNINIT for a
+  process that never ran); the kernel uses no x87, and its SSE helpers
+  touch only XMM registers, which ring 3 (`-mno-sse`) never uses.
 - **Memory**: `phys.h` — `MM_MAX_PHYS` (1 GB, the lowmem/identity-map
 - **Memory**: `phys.h` — `MM_MAX_PHYS` (1 GB, the lowmem/identity-map
   ceiling, also used by the UEFI loader for the pack buffer), `MM_KMAP_VA`
   ceiling, also used by the UEFI loader for the pack buffer), `MM_KMAP_VA`
   (`0x4FC00000`) and `MM_HIGH_LIMIT` (4 GB).  Lowmem ends where ring-3 space
   (`0x4FC00000`) and `MM_HIGH_LIMIT` (4 GB).  Lowmem ends where ring-3 space

+ 9 - 1
docs/cpu.html

@@ -170,7 +170,15 @@
 			Kernel threads stay pinned to the BSP; only ring-3 processes may run
 			Kernel threads stay pinned to the BSP; only ring-3 processes may run
 			on an AP. Each AP runs <code>cpu_enable_sse()</code> because it starts
 			on an AP. Each AP runs <code>cpu_enable_sse()</code> because it starts
 			with firmware <code>CR4</code>; without OSFXSR/OSXMMEXCPT the SSE2
 			with firmware <code>CR4</code>; without OSFXSR/OSXMMEXCPT the SSE2
-			drawing fast paths would <code>#UD</code>.
+			drawing fast paths would <code>#UD</code>. Every CPU also runs
+			<code>cpu_init_fpu()</code> (<code>CR0.NE</code>/<code>MP</code>,
+			<code>FNINIT</code>): an AP leaves INIT with <code>CR0.NE</code> clear
+			and an x87 control word of <code>0x0040</code>, so every inexact
+			floating-point result froze it until the next timer tick, and the
+			browser's JavaScript ran hundreds of times slower on an AP. Each
+			process owns its x87 state; <code>schedule()</code> saves it with
+			<code>FNSAVE</code> and restores it with <code>FRSTOR</code> on every
+			switch (108 bytes in <code>struct process</code>, no FXSR needed).
 		</p>
 		</p>
 		<div class="warn">
 		<div class="warn">
 			<p><strong>No IOAPIC driver.</strong> Device IRQs never reach an AP,
 			<p><strong>No IOAPIC driver.</strong> Device IRQs never reach an AP,

+ 20 - 0
kernel/cpu.c

@@ -37,6 +37,26 @@ unsigned int cpu_family_model(void)
 	return (family << 8) | model;
 	return (family << 8) | model;
 }
 }
 
 
+/* An AP leaves INIT with CR0.NE clear and an x87 control word of 0x0040:
+ * every exception unmasked and 24-bit precision.  With NE clear an
+ * unmasked exception (an inexact result, i.e. most floating-point
+ * operations) freezes the CPU at the next FPU instruction until an external
+ * interrupt -- the next 1 ms timer tick.  QuickJS computes with doubles, so
+ * the browser's scripts ran several hundred times slower whenever it was
+ * scheduled on an AP, at 100% CPU, with single-precision results.  QEMU
+ * models neither the freeze nor the precision.  Native #MF reporting (NE),
+ * MP for WAIT, no emulation or task-switch trap, and FNINIT's 0x037F. */
+void cpu_init_fpu(void)
+{
+	unsigned int cr0;
+
+	__asm__ volatile ("mov %%cr0, %0" : "=r"(cr0));
+	cr0 |= (1u << 5) | (1u << 1);           /* NE | MP */
+	cr0 &= ~((1u << 2) | (1u << 3));        /* EM | TS */
+	__asm__ volatile ("mov %0, %%cr0" :: "r"(cr0) : "memory");
+	__asm__ volatile ("fninit");
+}
+
 void cpu_enable_sse(void)
 void cpu_enable_sse(void)
 {
 {
 	unsigned int eax, ebx, ecx, edx, cr4;
 	unsigned int eax, ebx, ecx, edx, cr4;

+ 2 - 0
kernel/cpu.h

@@ -21,5 +21,7 @@ int cpu_has_sse2(void);
  * does this lazily in cpu_has_sse2(); an AP starts with firmware CR4 and must
  * does this lazily in cpu_has_sse2(); an AP starts with firmware CR4 and must
  * call this before it can run the SSE2 drawing fast paths. */
  * call this before it can run the SSE2 drawing fast paths. */
 void cpu_enable_sse(void);
 void cpu_enable_sse(void);
+/* Native x87 error reporting and a clean FPU on this CPU (BSP and APs). */
+void cpu_init_fpu(void);
 
 
 #endif
 #endif

+ 1 - 0
kernel/kernel.c

@@ -467,6 +467,7 @@ void kernel_main(void)
 	/* paging_init only ORs in PSE; make sure the BSP's SSE state is on
 	/* paging_init only ORs in PSE; make sure the BSP's SSE state is on
 	 * before any drawing path can select an SSE2 helper. */
 	 * before any drawing path can select an SSE2 helper. */
 	cpu_enable_sse();
 	cpu_enable_sse();
+	cpu_init_fpu();
 	/* ACPI needs paging for table access; a missing or unusable MADT simply
 	/* ACPI needs paging for table access; a missing or unusable MADT simply
 	 * leaves the machine at one CPU. */
 	 * leaves the machine at one CPU. */
 	acpi_init();
 	acpi_init();

+ 4 - 0
kernel/process.h

@@ -78,6 +78,10 @@ struct process {
 	unsigned int kernel_thread;
 	unsigned int kernel_thread;
 	/* BKL depth saved when the process blocks and must reacquire it. */
 	/* BKL depth saved when the process blocks and must reacquire it. */
 	unsigned int bkl_saved;
 	unsigned int bkl_saved;
+	/* x87 state (FNSAVE image) while switched out; fpu_valid is 0 until
+	 * the first switch-out, and such a process starts from FNINIT. */
+	unsigned char fpu[108];
+	unsigned int fpu_valid;
 };
 };
 
 
 /* User-mode virtual address layout (ring-3 processes). */
 /* User-mode virtual address layout (ring-3 processes). */

+ 19 - 0
kernel/sched.c

@@ -579,6 +579,24 @@ void sched_init(void)
 	}
 	}
 }
 }
 
 
+/* Each process owns its x87 state; the CPU's registers are not shared
+ * between them any more (a preempted floating-point computation used to
+ * continue with another process's stack and control word).  Eager and
+ * cheap enough at 1 kHz: FNSAVE/FRSTOR move 108 bytes and need no FXSR,
+ * so they work back to the Pentium.  The kernel uses no x87 itself; its
+ * SSE fill helpers touch only XMM registers, which ring 3 never uses. */
+static void fpu_switch(struct process *cur, struct process *next)
+{
+	if (cur) {
+		__asm__ volatile ("fnsave %0" : "=m"(cur->fpu));
+		cur->fpu_valid = 1;
+	}
+	if (next->fpu_valid)
+		__asm__ volatile ("frstor %0" :: "m"(next->fpu));
+	else
+		__asm__ volatile ("fninit");
+}
+
 static void sched_switch_to(struct process *next)
 static void sched_switch_to(struct process *next)
 {
 {
 	struct cpu *c = this_cpu();
 	struct cpu *c = this_cpu();
@@ -672,6 +690,7 @@ void schedule(void)
 		if (cur)
 		if (cur)
 			cur->on_cpu = PROC_NO_CPU;
 			cur->on_cpu = PROC_NO_CPU;
 		c->switches++;
 		c->switches++;
+		fpu_switch(cur, next);
 	}
 	}
 	runq_del_locked(next);
 	runq_del_locked(next);
 	next->state = PROC_RUNNING;
 	next->state = PROC_RUNNING;

+ 1 - 0
kernel/smp.c

@@ -160,6 +160,7 @@ void smp_ap_main(unsigned int cpu)
 	/* An AP starts with the firmware's CR4; enable the SSE state before it
 	/* An AP starts with the firmware's CR4; enable the SSE state before it
 	 * can reach the drawing fast paths (the BSP does this lazily). */
 	 * can reach the drawing fast paths (the BSP does this lazily). */
 	cpu_enable_sse();
 	cpu_enable_sse();
+	cpu_init_fpu();
 	cpus[cpu].id = cpu;
 	cpus[cpu].id = cpu;
 	cpus[cpu].lapic_id = lapic_id();
 	cpus[cpu].lapic_id = lapic_id();
 	idle = cpus[cpu].idle;
 	idle = cpus[cpu].idle;