Procházet zdrojové kódy

acpi: reset via the FADT RESET_REG on UEFI reboot

The reboot command only pulsed the 8042 controller, which does nothing on
the Chromebook and left the BSP halted with the BKL held.  Cache the
ACPI 2.0 RESET_REG/RESET_VALUE pair (RESET_REG_SUP) and write it first;
fall back to the Intel PCH 0xCF9 sequence, the 8042 pulse and a triple
fault so a reboot cannot end in a frozen hlt loop.

terminal.o, process.o and reboot.o build with -Oz to keep _end below the
0x90000 wall after the S5/reset code crossed the 0x69000 data/.bss
boundary.  QEMU BIOS and UEFI both boot twice through the reboot command.
Johannes Zellner před 2 dny
rodič
revize
2fa060e0ee
7 změnil soubory, kde provedl 109 přidání a 13 odebrání
  1. 11 6
      AGENTS.md
  2. 5 0
      Makefile
  3. 7 1
      docs/cpu.html
  4. 1 1
      docs/index.html
  5. 43 0
      kernel/acpi.c
  6. 4 0
      kernel/acpi.h
  7. 38 5
      kernel/reboot.c

+ 11 - 6
AGENTS.md

@@ -391,7 +391,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   (both hand the kernel the same ACPI tables); the Bochs/QEMU ports are
   only the emulator fallback.  If no path works the command returns instead
   of halting: the WM shows `poweroff failed` and the FADT/S5/`SCI_EN` state
-  is in the netlog.
+  is in the netlog.  `acpi_reset()` caches and writes the ACPI 2.0
+  `RESET_REG`/`RESET_VALUE` pair (`RESET_REG_SUP`), and is what `reboot`
+  tries before the Intel PCH, keyboard-controller and triple-fault
+  fallbacks.
 - **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`
   (`0x4FC00000`) and `MM_HIGH_LIMIT` (4 GB).  Lowmem ends where ring-3 space
@@ -3269,11 +3272,13 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     OVMF delivers the ISA IRQs through the 8259 path; the kernel nevertheless
     masks the IOAPIC's normal redirection entries after ACPI init, so
     firmware-left vectors cannot collide with the PIC or LAPIC timer.
-    UEFI runtime services are not used: `reboot` still pulses the keyboard
-    controller and `poweroff` uses ACPI S5 (the FADT PM1a/PM1b control blocks
-    and the DSDT `\_S5` sleep type, enabling ACPI through the SMI command
-    when `SCI_EN` is clear) on both BIOS and UEFI boots, with the
-    Bochs/QEMU ports as an emulator fallback.  The image is 17 MB: the
+    UEFI runtime services are not used: `reboot` writes the ACPI 2.0
+    `RESET_REG`/`RESET_VALUE` when `RESET_REG_SUP` is set, then falls back
+    to the Intel PCH 0xCF9, the keyboard controller and a triple fault;
+    `poweroff` uses ACPI S5 (the FADT PM1a/PM1b control blocks and the DSDT
+    `\_S5` sleep type, enabling ACPI through the SMI command when `SCI_EN`
+    is clear) on both BIOS and UEFI boots, with the Bochs/QEMU ports as an
+    emulator fallback.  The image is 17 MB: the
     9 MB kernel + rootfs disk and the 8 MB ESP.  Maxwell GPUs such as the
     GTX 960 use the firmware VBE/GOP framebuffer only; there is no native
     NVIDIA driver, and `nv50_probe()` is gated to the ION/nv50 chipsets.

+ 5 - 0
Makefile

@@ -133,6 +133,11 @@ $(B)/kernel/e1000e.o: OPT = -Oz
 # and the kernel is close to the low-memory wall; squeeze them hard.
 $(B)/kernel/acpi.o: OPT = -Oz
 $(B)/kernel/poweroff.o: OPT = -Oz
+$(B)/kernel/reboot.o: OPT = -Oz
+$(B)/kernel/terminal.o: OPT = -Oz
+# process.o is the largest -O2 object and not on a drawing path; -Oz keeps
+# the kernel under the 0x90000 wall now that S5/reset live in it.
+$(B)/kernel/process.o: OPT = -Oz
 
 # The AP trampoline is linked at 0x6000 and embedded as a binary blob; ld
 # names its symbols after the input path, so run it inside the build dir

+ 7 - 1
docs/cpu.html

@@ -180,7 +180,12 @@
 			Emulators whose tables do not decode <code>\_S5</code> fall back to
 			the Bochs/QEMU shutdown ports. A failed poweroff returns to the
 			desktop instead of halting, and the boot/poweroff log carries the
-			PM1, <code>\_S5</code> and <code>SCI_EN</code> state.
+			PM1, <code>\_S5</code> and <code>SCI_EN</code> state. The same FADT
+			walk caches the ACPI 2.0 <code>RESET_REG</code>/<code>RESET_VALUE</code>
+			(when <code>RESET_REG_SUP</code> is set); <code>reboot</code> writes
+			it first, then tries the Intel PCH <code>0xCF9</code>, the keyboard
+			controller and a triple fault. That is what resets a UEFI machine
+			with no working legacy keyboard controller.
 		</p>
 		<ol>
 			<li>Copy the AP trampoline to <code>0x6000</code> (SIPI vector <code>0x06</code>).</li>
@@ -406,6 +411,7 @@
 			<li><code>kernel/tss.c</code>, <code>kernel/tss.h</code> — per-CPU TSS</li>
 			<li><code>kernel/acpi.c</code>, <code>kernel/ioapic.c</code>, <code>kernel/lapic.c</code>, <code>kernel/smp.c</code> — SMP, IOAPIC masking and the FADT S5 shutdown data</li>
 			<li><code>kernel/poweroff.c</code> — ACPI S5 poweroff with the emulator port fallback</li>
+			<li><code>kernel/reboot.c</code> — FADT ACPI reset with PCH/KBC/triple-fault fallbacks</li>
 			<li><code>kernel/ap_tramp.S</code> — AP startup blob</li>
 			<li><code>kernel/timer.S</code>, <code>kernel/idle.c</code>, <code>kernel/power.c</code></li>
 			<li><code>kernel/thermal.c</code> — Intel DTS and AMD D18F3xA4 sampling</li>

+ 1 - 1
docs/index.html

@@ -159,7 +159,7 @@
 			<li><strong>Identity-mapped paging.</strong> Physical and virtual addresses are equal. There is no per-process address space or isolation.</li>
 			<li><strong>1 GB lowmem, 4 GB highmem.</strong> The identity map ends where ring-3 space starts (<code>0x40000000</code>); RAM up to 4 GB backs ring-3 pages through per-CPU <code>kmap</code> slots.</li>
 			<li><strong>Hardcoded low memory.</strong> Boot info, the E820 map, the framebuffer block and the AP trampoline have fixed addresses that every boot path must honour.</li>
-			<li><strong>No firmware runtime.</strong> <code>reboot</code> resets through the keyboard controller and <code>poweroff</code> writes ACPI S5 from the FADT/DSDT; UEFI runtime services are not called after <code>ExitBootServices</code>.</li>
+			<li><strong>No firmware runtime.</strong> <code>reboot</code> uses the ACPI reset register (with PCH/KBC fallbacks) and <code>poweroff</code> writes ACPI S5 from the FADT/DSDT; UEFI runtime services are not called after <code>ExitBootServices</code>.</li>
 		</ul>
 		<div class="warn">
 			<p><strong>Fresh memory is not zeroed.</strong> <code>mm_alloc_page</code> and <code>kmalloc</code> return whatever RAM contained. Every structure read before it is written must be cleared on allocation; QEMU hides this because its RAM starts clear.</p>

+ 43 - 0
kernel/acpi.c

@@ -66,6 +66,11 @@ static unsigned int fadt_pm1_cnt_len;
 static unsigned int fadt_smi_cmd;
 static unsigned int fadt_acpi_enable;
 static unsigned int fadt_flags;
+static unsigned int fadt_reset_space;
+static unsigned int fadt_reset_addr;
+static unsigned int fadt_reset_value;
+static unsigned int fadt_reset_width;
+static unsigned int fadt_reset_access;
 static unsigned int dsdt_addr;
 static unsigned int ssdt_addr[4];
 static unsigned int nssdt;
@@ -325,6 +330,16 @@ static void parse_fadt(const unsigned char *t, unsigned int len)
 	fadt_acpi_enable = t[52];
 	if (len >= 116)
 		fadt_flags = *(const unsigned int *)(t + 112);
+	/* ACPI 2.0+ reset register (GAS at 116, value at 128). */
+	if (len >= 129) {
+		fadt_reset_space = t[116];
+		fadt_reset_width = t[117];
+		fadt_reset_access = t[119];
+		fadt_reset_value = t[128];
+		x = *(const unsigned long long *)(t + 120);
+		if (x && (unsigned int)(x >> 32) == 0)
+			fadt_reset_addr = (unsigned int)x;
+	}
 	/* ACPI 2.0+ firmware may publish the DSDT only in X_DSDT. */
 	if (len >= 148) {
 		x = *(const unsigned long long *)(t + 140);
@@ -498,6 +513,11 @@ int acpi_init(void)
 	fadt_smi_cmd = 0;
 	fadt_acpi_enable = 0;
 	fadt_flags = 0;
+	fadt_reset_space = 0;
+	fadt_reset_addr = 0;
+	fadt_reset_value = 0;
+	fadt_reset_width = 0;
+	fadt_reset_access = 0;
 	dsdt_addr = 0;
 	nssdt = 0;
 	dsdt_ptr = 0;
@@ -640,6 +660,29 @@ static void nvidia_s5_prepare(unsigned int smi_base)
 	}
 }
 
+/* Reset the machine through the FADT reset register (ACPI 2.0+), the only
+ * reliable reset on machines without a working 8042.  Returns 0 when the
+ * register write was issued, -1 when the firmware does not provide one. */
+int acpi_reset(void)
+{
+	unsigned int addr = fadt_reset_addr;
+
+	if (!fadt_ok || !(fadt_flags & (1u << 10)) || !addr)
+		return -1;
+	if (fadt_reset_space == 1) {          /* System I/O */
+		if (addr > 0xFFFFu)
+			return -1;
+		if (fadt_reset_access >= 2 || fadt_reset_width >= 16)
+			port_outw((unsigned short)addr,
+				  (unsigned short)fadt_reset_value);
+		else
+			port_outb((unsigned short)addr,
+				  (unsigned char)fadt_reset_value);
+		return 0;
+	}
+	return -1;
+}
+
 int acpi_poweroff(void)
 {
 	unsigned int t0, sci, smi, cmd;

+ 4 - 0
kernel/acpi.h

@@ -28,6 +28,10 @@ unsigned int acpi_rtc_century_reg(void);
  * issued, -1 when the firmware provides no usable S5 path. */
 int acpi_poweroff(void);
 
+/* Reset through the FADT RESET_REG/RESET_VALUE.  Returns 0 when the register
+ * write was issued, -1 when the firmware provides no usable reset register. */
+int acpi_reset(void);
+
 /* Write the DSDT (as mapped at boot) to a VFS path, for diagnostics. */
 int acpi_dump_dsdt(const char *path);
 

+ 38 - 5
kernel/reboot.c

@@ -1,14 +1,47 @@
-/* Reboot command: trigger a CPU reset via the keyboard controller. */
+/* Reboot command: ACPI reset register first, then the legacy fallbacks.
+ *
+ * The 8042 pulse alone does nothing on machines without a legacy keyboard
+ * controller (the Chromebook), which used to leave the BSP halted with the
+ * BKL held.  The FADT RESET_REG/RESET_VALUE pair is the firmware-provided
+ * reset path; 0xCF9 is the Intel PCH fallback, and a triple fault is the
+ * last resort. */
 
-static inline void outb(unsigned short port, unsigned char v)
+#include "acpi.h"
+#include "pci.h"
+#include "timer.h"
+
+static void wait_ms(unsigned int ms)
 {
-	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+	unsigned int t0 = get_ticks_ms();
+
+	while (get_ticks_ms() - t0 < ms)
+		__asm__ volatile ("hlt");
 }
 
 void program_reboot_main(void)
 {
-	/* Pulse the CPU reset line via the keyboard controller (port 0x64). */
-	outb(0x64, 0xFE);
+	if (acpi_reset() == 0)
+		wait_ms(500);
+
+	/* Intel PCH reset control: SYS_RST, then RST_CPU|SYS_RST. */
+	port_outb(0xCF9, 0x02);
+	port_outb(0xCF9, 0x06);
+	wait_ms(300);
+
+	/* Legacy 8042 reset pulse, for boards where that is the only path. */
+	port_outb(0x64, 0xFE);
+	wait_ms(300);
+
+	/* Zero IDT + int3: a triple fault forces a CPU reset. */
+	{
+		struct {
+			unsigned short limit;
+			unsigned int base;
+		} __attribute__((packed)) idt = { 0, 0 };
+
+		__asm__ volatile ("lidt %0" : : "m"(idt));
+		__asm__ volatile ("int3");
+	}
 	for (;;)
 		__asm__ volatile ("hlt");
 }