Przeglądaj źródła

smp: enable the caches on the APs

INIT leaves an AP with CR0 = 0x60000010: CD and NW set, caching off
until the OS clears them.  The trampoline never did, so every ring-3
process the scheduler put on an AP ran from uncached memory on real
hardware (QEMU does not model caches).  ap_tramp.S now clears CR0.CD/NW
before paging is enabled, writes the wake-up CR0 to the startup block
(0x6F10), and smp.c logs it as "smp: AP n start cr0 ...".
Johannes Zellner 2 dni temu
rodzic
commit
58236ab095
4 zmienionych plików z 20 dodań i 1 usunięć
  1. 1 1
      AGENTS.md
  2. 1 0
      docs/cpu.html
  3. 9 0
      kernel/ap_tramp.S
  4. 9 0
      kernel/smp.c

+ 1 - 1
AGENTS.md

@@ -285,7 +285,7 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
 | `0x6000`  | AP real/protected-mode trampoline (`ap_tramp.S`/`ap_tramp.ld`; SIPI vector 0x06, image ends below `0x6C00`) |
 | `0x6C00`  | AP trampoline real-mode stack top (grows down) |
-| `0x6F00`  | AP startup info block (CR3, stack top, C entry, CPU index) |
+| `0x6F00`  | AP startup info block (CR3, stack top, C entry, CPU index; +16: the AP's CR0 at wake-up, written back by the trampoline) |
 | `0x60000` | No longer a rootfs staging area: the legacy stage copies each 16 KB chunk straight to `0x900000`.  The kernel BSS/boot stack may use this range |
 | `_end`    | Kernel end (linker symbol; `kernel.ld` keeps `_end <= 0x90000` so the top of the low-memory gap and the EBDA stay untouched).  BSS and the boot stack may extend past `0x60000` |
 | (in-image) | Bootstrap/protected-mode stack: 8 KB `.stack` NOLOAD section, `__boot_stack_top`/`__boot_stack_bottom` (travels with the kernel) |

+ 1 - 0
docs/cpu.html

@@ -162,6 +162,7 @@
 			<li>Allocate an AP idle PCB and kernel stack via <code>process_init_ap()</code>.</li>
 			<li>Fill the startup block at <code>0x6F00</code> with CR3, stack top, C entry and CPU index.</li>
 			<li>Send INIT, then SIPI-SIPI, with a second SIPI retry.</li>
+			<li>The trampoline clears <code>CR0.CD</code>/<code>CR0.NW</code>: INIT leaves an AP with caching disabled (<code>CR0 = 0x60000010</code>), and before this every ring-3 process on an AP ran about 100× slower on real hardware (QEMU does not model caches). The wake-up value is written back to <code>0x6F10</code> and logged as <code>smp: AP n start cr0 …</code>.</li>
 			<li>The AP enters <code>smp_ap_main</code>, initialises its LAPIC timer and idle loop, and waits.</li>
 			<li>After device/FS setup, <code>smp_enable_sched()</code> lets APs take user work.</li>
 		</ol>

+ 9 - 0
kernel/ap_tramp.S

@@ -18,6 +18,7 @@
 .equ AP_INFO_STACK, AP_INFO + 4
 .equ AP_INFO_ENTRY, AP_INFO + 8
 .equ AP_INFO_CPU,  AP_INFO + 12
+.equ AP_INFO_CR0,  AP_INFO + 16   /* written back: CR0 as the AP woke up */
 
 .globl ap_tramp_start
 ap_tramp_start:
@@ -81,7 +82,15 @@ ap_pm32:
 	andl  $~0x100, %eax
 	wrmsr
 3:
+	/* INIT leaves an AP with CR0 = 0x60000010: CD and NW set, so its
+	 * caches are off until the OS clears them.  Nothing did, and every
+	 * ring-3 process the scheduler put on an AP (the browser, its TLS
+	 * helpers) ran from uncached memory, about 100x slower; QEMU does
+	 * not model caches, so only real hardware showed it.  The original
+	 * value goes back to the BSP for the boot log. */
 	movl  %cr0, %eax
+	movl  %eax, AP_INFO_CR0
+	andl  $~0x60000000, %eax      /* clear CR0.CD | CR0.NW */
 	orl   $0x80010000, %eax       /* CR0.PG | CR0.WP */
 	movl  %eax, %cr0
 	jmp   1f

+ 9 - 0
kernel/smp.c

@@ -61,6 +61,7 @@ static void start_one_ap(unsigned int cpu, unsigned int top)
 	info[1] = top;
 	info[2] = (unsigned int)smp_ap_main;
 	info[3] = cpu;
+	info[4] = 0;
 	__sync_synchronize();
 
 	ap_started[cpu] = 0;
@@ -130,6 +131,14 @@ void smp_init(void)
 		}
 		online_aps++;
 		cpu_count = online_aps + 1;
+		/* CR0 as the AP woke up (its CD/NW are cleared by the
+		 * trampoline): 0x60000011 means it would have run uncached. */
+		serial_puts(SERIAL_COM1, "smp: AP ");
+		serial_putdec(SERIAL_COM1, i);
+		serial_puts(SERIAL_COM1, " start cr0 ");
+		serial_puthex(SERIAL_COM1,
+			      ((volatile unsigned int *)AP_INFO_ADDR)[4]);
+		serial_puts(SERIAL_COM1, "\n");
 	}
 
 	serial_puts(SERIAL_COM1, "smp: ");