kernel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "acpi.h"
  3. #include "asix.h"
  4. #include "bootcon.h"
  5. #include "bootinfo.h"
  6. #include "cpu.h"
  7. #include "cros_ec.h"
  8. #include "idle.h"
  9. #include "io.h"
  10. #include "ioapic.h"
  11. #include "lapic.h"
  12. #include "panic.h"
  13. #include "kmalloc.h"
  14. #include "mm.h"
  15. #include "mouse.h"
  16. #include "netdev.h"
  17. #include "net.h"
  18. #include "dhcp.h"
  19. #include "netlog.h"
  20. #include "paging.h"
  21. #include "power.h"
  22. #include "process.h"
  23. #include "ramfs.h"
  24. #include "cursor.h"
  25. #include "rndis.h"
  26. #include "rtl8821au.h"
  27. #include "sched.h"
  28. #include "sys_wm.h"
  29. #include "thermal.h"
  30. #include "timer.h"
  31. #include "tpec.h"
  32. #include "tss.h"
  33. #include "usbhid.h"
  34. #include "usbmsc.h"
  35. #include "wm.h"
  36. #include "serial.h"
  37. #include "smp.h"
  38. #include "version.h"
  39. #include "vga.h"
  40. #include "vfs.h"
  41. #include "wifi_cfg.h"
  42. static inline void outb(unsigned short port, unsigned char v)
  43. {
  44. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  45. }
  46. volatile unsigned int ticks;
  47. static unsigned int timer_hz;
  48. static unsigned int tick_ms = 1;
  49. struct idt_entry {
  50. unsigned short offset_lo;
  51. unsigned short selector;
  52. unsigned char zero;
  53. unsigned char type;
  54. unsigned short offset_hi;
  55. } __attribute__((packed));
  56. struct idtr_desc {
  57. unsigned short limit;
  58. unsigned int base;
  59. } __attribute__((packed));
  60. static struct idt_entry idt[256];
  61. static struct idtr_desc idtr;
  62. extern void pit_irq_stub(void);
  63. extern void kbd_irq_stub(void);
  64. extern void mouse_irq_stub(void);
  65. extern void fdc_irq_stub(void);
  66. extern void default_irq_stub(void);
  67. extern void resched_stub(void);
  68. extern void lapic_timer_stub(void);
  69. extern void lapic_resched_stub(void);
  70. extern void lapic_spurious_stub(void);
  71. extern void syscall_stub(void);
  72. extern unsigned int exc_stub_table[];
  73. unsigned int get_ticks_ms(void)
  74. {
  75. return ticks;
  76. }
  77. unsigned int timer_tick_ms(void)
  78. {
  79. return tick_ms;
  80. }
  81. void timer_tick_advance(void)
  82. {
  83. ticks += tick_ms;
  84. }
  85. void timer_set_hz(unsigned int hz)
  86. {
  87. unsigned int flags, divisor;
  88. if (hz < 100 || hz > 1000)
  89. hz = 1000;
  90. if (hz == timer_hz)
  91. return;
  92. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  93. __asm__ volatile ("cli" ::: "memory");
  94. timer_hz = hz;
  95. tick_ms = 1000 / hz;
  96. divisor = 1193182u / hz;
  97. outb(0x43, 0x36);
  98. outb(0x40, (unsigned char)(divisor & 0xFF));
  99. outb(0x40, (unsigned char)((divisor >> 8) & 0xFF));
  100. if (flags & (1u << 9))
  101. __asm__ volatile ("sti" ::: "memory");
  102. }
  103. void exc_handler(struct exc_state *st);
  104. void exc_handler(struct exc_state *st)
  105. {
  106. unsigned int vector = st->vector;
  107. unsigned int cr2 = 0;
  108. static const char * const names[] = {
  109. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  110. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  111. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  112. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  113. "#GP General Protection", "#PF Page Fault", "Reserved",
  114. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  115. "#XM SIMD Error",
  116. };
  117. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  118. /* A fault from ring 3 is a bug in a user command (or its ELF): kill that
  119. * process and keep the kernel running. Kernel faults still halt. */
  120. if ((st->cs & 3) == 3) {
  121. struct process *p = current_proc();
  122. serial_puts(SERIAL_COM1, "\n*** user fault vec ");
  123. serial_puthex(SERIAL_COM1, vector);
  124. serial_puts(SERIAL_COM1, " eip=");
  125. serial_puthex(SERIAL_COM1, st->eip);
  126. serial_puts(SERIAL_COM1, " cr2=");
  127. serial_puthex(SERIAL_COM1, cr2);
  128. if (p) {
  129. serial_puts(SERIAL_COM1, " pid=");
  130. serial_putdec(SERIAL_COM1, p->pid);
  131. serial_puts(SERIAL_COM1, " ");
  132. serial_puts(SERIAL_COM1, p->name);
  133. }
  134. serial_puts(SERIAL_COM1, " -- process killed\n");
  135. io_putstr("\n*** fault: ");
  136. io_putstr(vector < 20 ? names[vector] : "unknown vector");
  137. io_putstr(" -- command killed\n");
  138. process_exit(); /* marks us dead and waits to be switched out */
  139. }
  140. /* Fatal kernel fault: the banner goes straight into the LFB (no serial
  141. * console needed) and the full state is mirrored to COM1. */
  142. panic_report(st, vector < 20 ? names[vector] : "unknown vector");
  143. for (;;)
  144. __asm__ volatile ("cli; hlt");
  145. }
  146. static void set_idt_gate(int vec, unsigned int handler)
  147. {
  148. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  149. idt[vec].selector = 0x08;
  150. idt[vec].zero = 0;
  151. idt[vec].type = 0x8E;
  152. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  153. }
  154. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  155. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  156. static void set_idt_gate_user(int vec, unsigned int handler)
  157. {
  158. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  159. idt[vec].selector = 0x08;
  160. idt[vec].zero = 0;
  161. idt[vec].type = 0xEF;
  162. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  163. }
  164. static void timer_init(void)
  165. {
  166. int i;
  167. /* Remap PICs: master at 0x20, slave at 0x28. */
  168. outb(0x20, 0x11);
  169. outb(0xA0, 0x11);
  170. outb(0x21, 0x20);
  171. outb(0xA1, 0x28);
  172. outb(0x21, 0x04);
  173. outb(0xA1, 0x02);
  174. outb(0x21, 0x01);
  175. outb(0xA1, 0x01);
  176. outb(0x21, 0xFC);
  177. outb(0xA1, 0xFF);
  178. /* Default handler for all 256 vectors: absorbs any unexpected
  179. interrupt (including spurious IRQ 7) with an EOI + iret. */
  180. for (i = 0; i < 256; i++)
  181. set_idt_gate(i, (unsigned int)default_irq_stub);
  182. /* Exception handlers 0-31: ring-3 faults kill the process, kernel
  183. faults print the panic banner and halt (see exc_handler). */
  184. for (i = 0; i < 32; i++)
  185. set_idt_gate(i, exc_stub_table[i]);
  186. /* Hardware IRQ handlers. */
  187. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  188. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  189. set_idt_gate(0x2c, (unsigned int)mouse_irq_stub);
  190. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  191. /* Local-APIC vectors: the AP timer and reschedule IPI, plus the
  192. * spurious vector the LAPIC can deliver at any time. */
  193. set_idt_gate(LAPIC_TIMER_VECTOR, (unsigned int)lapic_timer_stub);
  194. set_idt_gate(LAPIC_RESCHED_VECTOR, (unsigned int)lapic_resched_stub);
  195. set_idt_gate(LAPIC_SPURIOUS_VECTOR, (unsigned int)lapic_spurious_stub);
  196. /* Syscall gate (ring-3 can int 0x80). */
  197. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  198. /* Kernel-only voluntary reschedule gate (int 0x81). */
  199. set_idt_gate(0x81, (unsigned int)resched_stub);
  200. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  201. idtr.base = (unsigned int)idt;
  202. __asm__ volatile ("lidt %0" : : "m"(idtr));
  203. /* Start the PIT at 1 kHz; schedule() drops it to 100 Hz while the BSP
  204. * is idle (see timer_set_hz). */
  205. timer_set_hz(1000);
  206. __asm__ volatile ("sti");
  207. }
  208. /* The BSP built the IDT once; APs only need to load the same pointer. */
  209. void idt_load_ap(void)
  210. {
  211. __asm__ volatile ("lidt %0" : : "m"(idtr));
  212. }
  213. extern unsigned char __bss_start[], __bss_end[];
  214. static int kworker_pid = -1;
  215. /* Parts of the system that must keep running independently of the WM:
  216. * input routing, thermal polling, cursor blink, kexec recovery, netlog
  217. * flushing, gfx-session cleanup and the shadow-buffer flush. */
  218. static void system_housekeeping(void)
  219. {
  220. process_reap_dead();
  221. thermal_housekeeping();
  222. mouse_poll_update();
  223. #ifndef NOWM
  224. wm_pump_keyboard();
  225. #endif
  226. netlog_flush();
  227. #ifndef NOWM
  228. wm_handle_kexec();
  229. wm_server_supervise();
  230. if (!wm_server_frame_active())
  231. vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
  232. #else
  233. /* Diagnostic build: keep the direct-LFB boot console visible and do
  234. * not let the WM shadow flush overwrite it. */
  235. #endif
  236. }
  237. static void kworker_main(void)
  238. {
  239. for (;;) {
  240. system_housekeeping();
  241. process_sleep_ms(2);
  242. }
  243. }
  244. /* Idle is not a normal runnable task: schedule() selects it only when no
  245. * READY process exists. The kworker does the housekeeping; if it dies (or
  246. * could not be created), idle takes over as a degraded fallback. */
  247. static void kernel_idle_loop(void)
  248. {
  249. for (;;) {
  250. if (kworker_pid <= 0 ||
  251. !process_is_alive((unsigned int)kworker_pid)) {
  252. /* Idle must never sleep on the BKL: if a user process on
  253. * another CPU owns it, just keep idling and retry. */
  254. if (bkl_trylock()) {
  255. system_housekeeping();
  256. bkl_unlock();
  257. }
  258. }
  259. cpu_idle();
  260. }
  261. }
  262. /* Boot-stage tracing: COM1 (netlog mirrors it) plus the direct-LFB boot
  263. * console, so a machine without serial still shows where the boot stops. */
  264. static unsigned int boot_t0;
  265. static void boot_trace(const char *what)
  266. {
  267. unsigned int ms = get_ticks_ms() - boot_t0;
  268. bootcon_puts("[");
  269. bootcon_puts(what);
  270. bootcon_puts("] +");
  271. bootcon_dec(ms);
  272. bootcon_puts(" ms\n");
  273. serial_puts(SERIAL_COM1, "[boot] ");
  274. serial_puts(SERIAL_COM1, what);
  275. serial_puts(SERIAL_COM1, " +");
  276. serial_putdec(SERIAL_COM1, ms);
  277. serial_puts(SERIAL_COM1, " ms\n");
  278. }
  279. static void boot_val(const char *name, unsigned int v)
  280. {
  281. bootcon_puts(name);
  282. bootcon_puts(": ");
  283. bootcon_dec(v);
  284. bootcon_puts("\n");
  285. serial_puts(SERIAL_COM1, name);
  286. serial_puts(SERIAL_COM1, ": ");
  287. serial_putdec(SERIAL_COM1, v);
  288. serial_puts(SERIAL_COM1, "\n");
  289. }
  290. /* Bounded PIT self-test. If the 8259 does not deliver IRQ0, every later
  291. * delay/timeout would spin on a frozen tick counter; say so and stop. */
  292. static int pit_ticking(void)
  293. {
  294. unsigned int t0 = get_ticks_ms();
  295. unsigned int n;
  296. for (n = 0; n < 5000000u; n++) {
  297. if (get_ticks_ms() != t0)
  298. return 1;
  299. __asm__ volatile ("pause");
  300. }
  301. return 0;
  302. }
  303. /* A kexec/net-dev reload starts a fresh kernel with zeroed BSS, so the
  304. * leased address and resolver would otherwise fall back to the static
  305. * default until someone types dhcp. The previous kernel stashed them in
  306. * the boot-info reserved words; apply them before the boot DHCP attempt. */
  307. static int net_restore_boot_config(void)
  308. {
  309. const volatile unsigned int *p;
  310. unsigned char ip[4], mask[4], gw[4], dns[4];
  311. unsigned int i;
  312. if (!bootinfo_valid() || !(bootinfo_flags() & BOOTINFO_FLAG_NET))
  313. return 0;
  314. p = bootinfo_words();
  315. for (i = 0; i < 4; i++) {
  316. ip[i] = (unsigned char)(p[7] >> (8 * i));
  317. mask[i] = (unsigned char)(p[8] >> (8 * i));
  318. gw[i] = (unsigned char)(p[9] >> (8 * i));
  319. dns[i] = (unsigned char)(p[10] >> (8 * i));
  320. }
  321. if (!(ip[0] || ip[1] || ip[2] || ip[3]))
  322. return 0;
  323. net_configure(ip, mask, gw);
  324. if (dns[0] || dns[1] || dns[2] || dns[3])
  325. net_set_dns(dns);
  326. boot_trace("net retained");
  327. /* A resolver is what net-dev users notice missing; without one, let
  328. * the boot DHCP attempt learn it. */
  329. return dns[0] || dns[1] || dns[2] || dns[3];
  330. }
  331. /* Saved WiFi networks on the writable /data volume: let the normal
  332. * supplicant scan and join any known network in the background. It leaves a
  333. * wired link that is physically up alone, and a missing /data or wifi helper
  334. * is harmless. */
  335. static void boot_wifi_autoconnect(void)
  336. {
  337. unsigned int sz = 0;
  338. int is_dir = 0;
  339. if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
  340. !is_dir && sz > 0) {
  341. struct vfs_file f;
  342. struct vfs_mount *m = 0;
  343. if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
  344. if (process_create_user("wifi", f.data, f.size,
  345. "autoconnect", "/") > 0)
  346. serial_puts(SERIAL_COM1,
  347. "wifi: autoconnect started\n");
  348. vfs_release_file(m, &f);
  349. }
  350. }
  351. }
  352. /* Boot-time storage scan in its own kernel thread. A process (unlike the
  353. * idle context kernel_main runs in) may sleep, so the probes' long waits
  354. * (floppy motor, ATA/AHCI timeouts) go through vfs_scan_relax() and the WM
  355. * and the first terminal run meanwhile. Disk file operations wait for
  356. * vfs_scan_busy; /ramfs and /share do not. The WiFi autoconnect needs
  357. * /data, so it runs here after the mount. */
  358. static volatile int boot_dhcp_done;
  359. static void boot_storage_main(void)
  360. {
  361. vfs_mount_all();
  362. boot_trace("storage");
  363. /* As before, WiFi only starts after the boot DHCP attempt on the
  364. * wired NIC, so two DHCP clients never overlap. */
  365. while (!boot_dhcp_done)
  366. process_sleep_ms(20);
  367. boot_wifi_autoconnect();
  368. boot_trace("wifi auto");
  369. }
  370. void kernel_main(void);
  371. void kernel_main(void)
  372. {
  373. unsigned char *p = __bss_start;
  374. int net_kept, storage_inline;
  375. while (p < __bss_end)
  376. *p++ = 0;
  377. /* Earliest possible output: straight into the boot framebuffer, before
  378. * paging, drivers, the heap or the serial port. Everything below logs
  379. * here as well as to COM1, so a machine without serial (the X201) can
  380. * show where the boot stops. */
  381. bootcon_init();
  382. bootcon_puts(OS_NAME " v" OS_VERSION "\n");
  383. serial_init(SERIAL_COM1);
  384. serial_ring_init();
  385. /* Network console mirror from the very first line on; the ring holds the
  386. * early boot output until the WM loop starts flushing it. */
  387. netlog_init();
  388. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  389. boot_trace("start");
  390. /* GDT/TSS and the per-CPU GS base must exist before anything that can
  391. * use current_proc()/this_cpu(). The BSS is already cleared and the
  392. * GDT/TSS live at identity-mapped addresses. */
  393. gdt_tss_init(0);
  394. boot_trace("gdt");
  395. mm_init();
  396. mm_dump();
  397. boot_trace("mm");
  398. boot_val("mm free KB", mm_free_pages() * 4);
  399. kmalloc_init();
  400. /* Report the VBE geometry before paging, so a boot that dies in the
  401. * translation switch still says which mode the BIOS picked. */
  402. bootcon_fb_info();
  403. paging_init();
  404. boot_trace("paging");
  405. /* Keep a RAM copy of the boot console text; the WM hides the
  406. * framebuffer console later and this is what `bootlog` prints. */
  407. bootcon_log_init();
  408. /* paging_init only ORs in PSE; make sure the BSP's SSE state is on
  409. * before any drawing path can select an SSE2 helper. */
  410. cpu_enable_sse();
  411. /* ACPI needs paging for table access; a missing or unusable MADT simply
  412. * leaves the machine at one CPU. */
  413. acpi_init();
  414. acpi_dump();
  415. /* The kernel keeps the 8259 on LAPIC LINT0; firmware-left IOAPIC
  416. * entries would otherwise deliver into the PIC/LAPIC vector space. */
  417. ioapic_mask_all();
  418. boot_trace("acpi");
  419. boot_val("acpi cpus", acpi_cpu_count());
  420. io_init();
  421. boot_trace("io");
  422. /* P-states/deep C-states first: idle_init() picks its default from
  423. * the power module (deep MWAIT on Nehalem, HLT elsewhere). */
  424. power_init();
  425. idle_init();
  426. boot_trace("power");
  427. thermal_init();
  428. boot_trace("thermal");
  429. /* Board sensors: the ThinkPad EC probe is gated on SMBIOS identifying a
  430. * Lenovo/IBM machine, so it is a no-op everywhere else. The Chrome EC
  431. * probe is skipped when the ThinkPad EC was found. */
  432. tpec_init();
  433. cros_ec_init();
  434. boot_trace("ec");
  435. /* process_init must run before timer_init: once the PIT is started (sti
  436. * in timer_init) the context-switch stub runs on every tick and reads
  437. * the current process, its CR3 and the TSS, so those must be valid. */
  438. process_init();
  439. boot_trace("process");
  440. timer_init();
  441. boot_t0 = get_ticks_ms();
  442. boot_trace("timer");
  443. if (!pit_ticking()) {
  444. bootcon_puts("*** PIT NOT TICKING ***\n");
  445. serial_puts(SERIAL_COM1, "*** PIT NOT TICKING ***\n");
  446. for (;;)
  447. __asm__ volatile ("cli; hlt");
  448. }
  449. boot_val("timer ticks", get_ticks_ms());
  450. /* The PIT (and its sti) is live, so the AP bring-up can use it as a
  451. * timeout clock. APs idle until smp_enable_sched() after boot. */
  452. smp_init();
  453. boot_trace("smp");
  454. boot_val("smp cpus", cpu_count);
  455. mouse_init();
  456. boot_trace("ps2");
  457. vga_shadow_init();
  458. vga_fb_flush();
  459. boot_trace("vga");
  460. /* The command pack: read-only, mounted at /ramfs (see ramfs.c). The
  461. * boot path copied it into RAM (the legacy stage's low gap, the UEFI
  462. * loader or kexec); ramfs_init takes its own copy so the boot region
  463. * can be released. The mount is created even without a pack, so a running
  464. * board can be recovered with `make cmds-dev`/`make net-cmds`. */
  465. {
  466. unsigned int pack_addr = bootinfo_pack_addr();
  467. unsigned int pack_len = bootinfo_pack_size();
  468. int ok = 0, gz = 0;
  469. if (!pack_addr || !pack_len)
  470. serial_puts(SERIAL_COM1, "ramfs: no boot pack\n");
  471. else {
  472. const unsigned char *p = (const unsigned char *)pack_addr;
  473. gz = pack_len >= 2 && p[0] == 0x1F && p[1] == 0x8B;
  474. serial_puts(SERIAL_COM1, "ramfs: boot pack at ");
  475. serial_puthex(SERIAL_COM1, pack_addr);
  476. serial_puts(SERIAL_COM1, ", ");
  477. serial_putdec(SERIAL_COM1, pack_len);
  478. serial_puts(SERIAL_COM1, gz ? " bytes (gzip)\n" :
  479. " bytes\n");
  480. if (gz)
  481. ramfs_set_boot_image((const void *)pack_addr,
  482. pack_len, 1);
  483. ok = ramfs_init((const void *)pack_addr, pack_len) == 0;
  484. if (!ok) {
  485. serial_puts(SERIAL_COM1, "ramfs: bad boot pack\n");
  486. ramfs_set_boot_image(0, 0, 0);
  487. mm_free_range(pack_addr, pack_len);
  488. } else {
  489. serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
  490. serial_putdec(SERIAL_COM1, ramfs_count());
  491. serial_puts(SERIAL_COM1, " files, ");
  492. serial_putdec(SERIAL_COM1, ramfs_pack_size());
  493. serial_puts(SERIAL_COM1, " bytes\n");
  494. cursor_load();
  495. if (!gz) {
  496. /* ramfs has its own copy now; keep that
  497. * as the self-update image. */
  498. ramfs_set_boot_image(ramfs_pack_base(),
  499. ramfs_pack_size(), 0);
  500. mm_free_range(pack_addr, pack_len);
  501. }
  502. /* A gzip boot image stays reserved so
  503. * `install self` can write it to P2. */
  504. }
  505. }
  506. (void)ok;
  507. vfs_mount_ramfs();
  508. /* /share too: the WM and the first terminal may use it while the
  509. * bootscan thread is still probing the disks, and a path that
  510. * does not resolve yet would wait for the whole scan. */
  511. vfs_mount_share();
  512. }
  513. boot_trace("ramfs");
  514. boot_val("ramfs files", ramfs_count());
  515. #ifndef NOWM
  516. /* Start the WM before touching disks: the first frame can be painted
  517. * while storage and the network are still being probed. */
  518. wm_server_init();
  519. boot_trace("wm init");
  520. wm_server_start();
  521. boot_trace("wm");
  522. #else
  523. /* Diagnostic build: boot to the console, no WM, no terminal. */
  524. bootcon_puts("WM disabled (diagnostic build)\n");
  525. serial_puts(SERIAL_COM1, "nowm: window manager disabled\n");
  526. boot_trace("wm off");
  527. #endif
  528. /* Publish the storage scan before anything can run a command: disk
  529. * paths wait for vfs_scan_busy (owned by the idle context until the
  530. * bootscan thread's vfs_mount_all() takes it over), /ramfs and /share
  531. * do not. Then let the WM spawn the first terminal at once; neither
  532. * it nor the WM needs the NIC probe or the disks below. */
  533. vfs_scan_owner_pid = 0;
  534. vfs_scan_busy = 1;
  535. #ifndef NOWM
  536. wm_server_boot_ready();
  537. boot_trace("terminal ready");
  538. #else
  539. boot_trace("terminal off");
  540. #endif
  541. #ifndef NOWM
  542. /* Let the WM put the first terminal on screen before the USB bring-up
  543. * below: kernel_main is the idle context, so halting here hands the
  544. * CPU to the WM and the new terminal. Bounded, in case no terminal
  545. * ever presents (missing /ramfs/bin/term). */
  546. {
  547. unsigned int t0 = get_ticks_ms();
  548. while (!wm_first_window_presented &&
  549. get_ticks_ms() - t0 < 1000)
  550. __asm__ volatile ("sti; hlt");
  551. }
  552. boot_trace("terminal shown");
  553. #endif
  554. /* USB HID backend (mouse + keyboard) and the USB device drivers:
  555. * controller bring-up + first scan; devices plugged in later are
  556. * picked up by the hotplug watcher. Non-preemptible, exactly like
  557. * the bring-up before the WM existed: port resets and the SOF a
  558. * freshly enabled low-speed device needs within 3 ms must not wait
  559. * for a WM frame. Before the NIC probe: ASIX/RNDIS/RTL8821AU bind
  560. * during this scan, and USB disks before the bootscan thread. */
  561. asix_register();
  562. usbmsc_register();
  563. rtl8821au_register();
  564. rndis_register();
  565. preempt_disable();
  566. usbhid_init();
  567. preempt_enable();
  568. boot_trace("usb");
  569. /* Probe the NIC (link negotiation is asynchronous now) before the
  570. * kworker starts, so its netlog flushes have a device to use. */
  571. netdev_active();
  572. net_kept = net_restore_boot_config();
  573. boot_trace("net");
  574. /* The idle process is the kernel_main context; housekeeping runs in
  575. * its own process so it cannot be starved by a busy system. */
  576. kworker_pid = process_create("kworker", kworker_main);
  577. if (kworker_pid < 0) {
  578. serial_puts(SERIAL_COM1, "kworker: cannot create, using idle fallback\n");
  579. kworker_pid = -1;
  580. } else {
  581. process_set_weight((unsigned int)kworker_pid, 4);
  582. process_set_affinity((unsigned int)kworker_pid, 1);
  583. }
  584. boot_trace("kworker");
  585. /* Slow storage bring-up happens behind the usable desktop, in a
  586. * thread that may sleep through its probe timeouts. The scan was
  587. * published above; its vfs_mount_all() takes over the ownership.
  588. * Without a thread slot, scan inline as before. */
  589. boot_trace("storage begin");
  590. storage_inline = 0;
  591. if (process_create("bootscan", boot_storage_main) <= 0) {
  592. vfs_mount_all();
  593. boot_trace("storage");
  594. storage_inline = 1;
  595. }
  596. /* With a linked NIC present, try DHCP; on failure the static default
  597. * stays. A wired NIC without a cable is skipped so the WiFi fallback
  598. * below does not have to wait out the 4 s timeout first. Deliberately
  599. * after the WM: a missing cable or DHCP server must not delay the first
  600. * frame. */
  601. {
  602. const struct netdev *nd = netdev_active();
  603. int linked = nd && netdev_link(nd);
  604. if (linked && !net_kept)
  605. dhcp_run(4000);
  606. boot_trace(net_kept ? "dhcp skipped" :
  607. (linked ? "dhcp" : "dhcp no link"));
  608. }
  609. boot_dhcp_done = 1;
  610. if (storage_inline) {
  611. boot_wifi_autoconnect();
  612. boot_trace("wifi auto");
  613. }
  614. /* Dump the active NIC's counters after the boot DHCP attempt. With
  615. * the ASIX as the management link this appears in netmon even when
  616. * the active e1000e cannot pass traffic. */
  617. {
  618. const struct netdev *nd = netdev_active();
  619. if (nd && (nd->dump_brief || nd->dump_regs)) {
  620. io_putstr("\n[boot netregs: ");
  621. io_putstr(nd->name);
  622. io_putstr("]\n");
  623. if (nd->dump_brief)
  624. nd->dump_brief();
  625. else
  626. nd->dump_regs();
  627. }
  628. }
  629. /* Boot-time device/FS setup is complete; APs may now take user work.
  630. * Kernel threads stay pinned to the BSP and serialize on the BKL. */
  631. smp_enable_sched();
  632. boot_trace("sched");
  633. kernel_idle_loop();
  634. }