cpu.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>CPU &amp; Scheduling | NebOS Architecture</title>
  7. <meta name="description" content="NebOS CPU architecture: GDT/TSS, PIC/PIT and LAPIC interrupts, IOAPIC masking, ACPI SMP bring-up, CFS-lite scheduling, wait queues, the big kernel lock, idle, power and AMD/Intel thermal management.">
  8. <link rel="icon" href="favicon.svg" type="image/svg+xml">
  9. <link rel="stylesheet" href="style.css">
  10. </head>
  11. <body>
  12. <a class="skip" href="#main">Skip to content</a>
  13. <header class="site-header">
  14. <a class="brand" href="index.html">
  15. <span class="brand-mark" aria-hidden="true"></span>
  16. <span>NebOS <span class="brand-sub">Architecture</span></span>
  17. </a>
  18. <nav class="site-nav" aria-label="Architecture sections">
  19. <a href="index.html">Overview</a>
  20. <a href="boot.html">Boot</a>
  21. <a href="memory.html">Memory</a>
  22. <a href="cpu.html" aria-current="page">CPU</a>
  23. <a href="drivers.html">Drivers</a>
  24. <a href="filesystems.html">Filesystems</a>
  25. <a href="graphics.html">Graphics</a>
  26. <a href="network.html">Network</a>
  27. <a href="userland.html">Userland</a>
  28. <a href="development.html">Development</a>
  29. </nav>
  30. </header>
  31. <main id="main" class="page">
  32. <div class="page-head">
  33. <p class="kicker">CPU &amp; scheduling</p>
  34. <h1>Weighted fairness, one global run queue, one big lock.</h1>
  35. <p class="lede">
  36. NebOS runs a CFS-lite scheduler over a single global run queue and
  37. per-CPU <code>current</code> pointers. ACPI/MADT brings up the APs,
  38. each CPU has its own GDT, TSS and timer, and a sleeping recursive
  39. mutex — the big kernel lock — serializes kernel execution.
  40. </p>
  41. <div class="meta">
  42. <span class="pill">8 CPUs max</span>
  43. <span class="pill">64 process slots</span>
  44. <span class="pill">10 ms slice</span>
  45. <span class="pill">1 kHz busy / 100 Hz idle</span>
  46. </div>
  47. </div>
  48. <nav class="toc" aria-label="On this page">
  49. <h2>On this page</h2>
  50. <ul>
  51. <li><a href="#bringup">Boot CPU bring-up</a></li>
  52. <li><a href="#interrupts">Interrupts &amp; timers</a></li>
  53. <li><a href="#smp">SMP bring-up</a></li>
  54. <li><a href="#process">Process model</a></li>
  55. <li><a href="#scheduler">CFS-lite scheduler</a></li>
  56. <li><a href="#bkl">Big kernel lock</a></li>
  57. <li><a href="#idle">Idle &amp; power</a></li>
  58. <li><a href="#sources">Source map</a></li>
  59. </ul>
  60. </nav>
  61. <section id="bringup">
  62. <h2>Boot CPU bring-up</h2>
  63. <p>
  64. After <code>kernel_main</code> clears BSS and starts the boot console,
  65. the first structural step is <code>gdt_tss_init(0)</code>. The GDT/TSS
  66. and the per-CPU GS base must exist before any code that calls
  67. <code>current_proc()</code> or <code>this_cpu()</code>.
  68. </p>
  69. <ul>
  70. <li>Each CPU has a <code>struct cpu</code> holding <code>current</code>, <code>idle</code>, its TSS, LAPIC id, tick period and scheduling flags.</li>
  71. <li><code>%gs:0</code> points at the current CPU's <code>current</code> field; <span class="file">switch.S</span> reads it directly. A static assert locks the offsets.</li>
  72. <li>The TSS <code>esp0</code> is updated by <code>schedule()</code>, so ring-3 entry always lands on the process's kernel stack.</li>
  73. <li><code>process_init()</code> must run before <code>timer_init()</code>: once the PIT starts, the timer stub can switch context on every tick.</li>
  74. </ul>
  75. </section>
  76. <section id="interrupts">
  77. <h2>Interrupts &amp; timers</h2>
  78. <p>
  79. The BSP keeps the legacy 8259 PIC and PIT; there is no IOAPIC
  80. driver. After ACPI init the kernel masks every normal IOAPIC
  81. redirection entry (leaving ExtINT entries alone, in case the 8259
  82. is routed through an IOAPIC pin), so firmware-left vectors cannot
  83. collide with the PIC vectors or the LAPIC timer. The BSP LAPIC is
  84. initialized in <code>timer_init()</code> before <code>sti</code>
  85. (SVR, LINT0 ExtINT, the firmware LAPIC timer stopped), and the PIC
  86. IRQ stubs send the LAPIC EOI as well as the PIC EOI; a firmware
  87. that leaves the LAPIC enabled therefore cannot block the second
  88. ExtINT tick. <code>smp_init()</code> re-runs
  89. <code>lapic_init_bsp()</code>. APs mask their LINT0/LINT1 inputs and
  90. use only their local APIC timer and IPIs.
  91. </p>
  92. <div class="table-wrap">
  93. <table>
  94. <thead>
  95. <tr><th>Source</th><th>Vector</th><th>Role</th></tr>
  96. </thead>
  97. <tbody>
  98. <tr><td>8259 PIC + PIT</td><td>IRQ0, etc.</td><td>BSP global clock and device interrupts on LAPIC LINT0 ExtINT; the stubs send both EOIs; the PIT tick advances the millisecond clock.</td></tr>
  99. <tr><td>LAPIC timer</td><td><code>0x30</code></td><td>Per-AP scheduling tick; 1 ms busy, 10 ms idle.</td></tr>
  100. <tr><td>LAPIC IPI</td><td><code>0x31</code></td><td>Reschedule request; must be EOIed on the target LAPIC.</td></tr>
  101. <tr><td><code>int 0x80</code></td><td>syscall gate</td><td>Ring-3 service entry.</td></tr>
  102. <tr><td><code>int 0x81</code></td><td>voluntary yield</td><td>Blocking helpers and <code>schedule()</code>.</td></tr>
  103. </tbody>
  104. </table>
  105. </div>
  106. <p>
  107. The timer period is not fixed. While a process runs, the BSP PIT ticks
  108. at 1 kHz and AP LAPIC timers tick every 1 ms. While a CPU is idle, the
  109. BSP drops to 100 Hz and APs to 10 ms so MWAIT/HLT can hold a deeper
  110. state. The global millisecond clock remains accurate because the PIT
  111. stub advances it by the current tick period.
  112. </p>
  113. <figure class="figure">
  114. <div class="bars" role="img" aria-label="Timer rates: 1000 Hz while busy, 100 Hz while idle. Bar widths are relative to 1000 Hz.">
  115. <div class="bar-row">
  116. <span class="bar-label">BSP busy</span>
  117. <div class="bar-track"><span class="bar-fill green" style="--w:100%"></span></div>
  118. <span class="bar-value">1000 Hz</span>
  119. </div>
  120. <div class="bar-row">
  121. <span class="bar-label">BSP idle</span>
  122. <div class="bar-track"><span class="bar-fill" style="--w:10%"></span></div>
  123. <span class="bar-value">100 Hz</span>
  124. </div>
  125. <div class="bar-row">
  126. <span class="bar-label">AP busy</span>
  127. <div class="bar-track"><span class="bar-fill green" style="--w:100%"></span></div>
  128. <span class="bar-value">1 ms</span>
  129. </div>
  130. <div class="bar-row">
  131. <span class="bar-label">AP idle</span>
  132. <div class="bar-track"><span class="bar-fill" style="--w:10%"></span></div>
  133. <span class="bar-value">10 ms</span>
  134. </div>
  135. </div>
  136. <figcaption>
  137. <strong>Race to idle, then stay there.</strong> When a wake-up
  138. arrives, <code>smp_resched_wake()</code> shortens the idle period so
  139. the woken process runs at the next millisecond instead of waiting
  140. out a 10 ms nap.
  141. </figcaption>
  142. </figure>
  143. </section>
  144. <section id="smp">
  145. <h2>SMP bring-up</h2>
  146. <p>
  147. SMP is ACPI-only: RSDP → RSDT/XSDT → MADT → CPU/LAPIC list. There is
  148. deliberately no Intel MP-table parser. A machine without ACPI, a
  149. usable MADT, the CPUID APIC bit or a LAPIC stays uniprocessor and
  150. follows exactly the old 8259/PIT path.
  151. </p>
  152. <p>
  153. The same table walk reads the FADT's <code>century</code> byte (offset
  154. 108) and exposes the CMOS register to <span class="file">rtc.c</span>,
  155. which writes it when setting the clock so the two-digit year stays in
  156. the intended century. Boards without ACPI get no century write and
  157. keep the 1970–2069 read window.
  158. </p>
  159. <p>
  160. It also caches the FADT power-management blocks and scans the DSDT
  161. (or an SSDT) for <code>\_S5</code>. The <code>poweroff</code> command
  162. read-modify-writes the sleep type into PM1a/PM1b, keeping the existing
  163. <code>SCI_EN</code> bit set — writing only the sleep bits clears it,
  164. and some real chipsets then ignore the request. It writes
  165. <code>SLP_TYP</code> first and <code>SLP_EN</code> in a second write.
  166. Intel-style southbridges clear <code>SMI_EN.SLP_SMI_EN</code> (bit 4
  167. at <code>PMBASE+0x30</code>): while that bit is set the chipset turns
  168. the <code>SLP_EN</code> write into an SMI instead of a power
  169. transition. NVIDIA nForce/MCP boards (the ION) run S5 through the
  170. firmware SMM instead: the kernel writes the sleep state into the
  171. BIOS NVS (<code>ASSB=5</code>, Linux <code>AOTB</code>/<code>OSTP</code>,
  172. <code>AAXB=0</code>) and arms <code>PS1S</code>/<code>PS1E</code> in
  173. the SMI block, matching the DSDT's <code>_PTS(5)</code>, before the
  174. <code>SLP_EN</code> write. If <code>SCI_EN</code> is clear it first
  175. enables ACPI mode through the FADT SMI command (or the conventional
  176. <code>0xB2</code>/<code>0xF1</code> pair). BIOS and UEFI boots share
  177. this path because both hand the kernel the same ACPI tables; only the
  178. RSDP discovery differs (EBDA scan versus the loader's boot info).
  179. Emulators whose tables do not decode <code>\_S5</code> fall back to
  180. the Bochs/QEMU shutdown ports. A failed poweroff returns to the
  181. desktop instead of halting, and the boot/poweroff log carries the
  182. PM1, <code>\_S5</code> and <code>SCI_EN</code> state. The same FADT
  183. walk caches the ACPI 2.0 <code>RESET_REG</code>/<code>RESET_VALUE</code>
  184. (when <code>RESET_REG_SUP</code> is set); <code>reboot</code> writes
  185. it first, then tries the Intel PCH <code>0xCF9</code>, the keyboard
  186. controller and a triple fault. That is what resets a UEFI machine
  187. with no working legacy keyboard controller.
  188. </p>
  189. <ol>
  190. <li>Copy the AP trampoline to <code>0x6000</code> (SIPI vector <code>0x06</code>).</li>
  191. <li>Allocate an AP idle PCB and kernel stack via <code>process_init_ap()</code>.</li>
  192. <li>Fill the startup block at <code>0x6F00</code> with CR3, stack top, C entry and CPU index.</li>
  193. <li>Send INIT, then SIPI-SIPI, with a second SIPI retry.</li>
  194. <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>
  195. <li>The AP enters <code>smp_ap_main</code>, initialises its LAPIC timer and idle loop, and waits.</li>
  196. <li>After device/FS setup, <code>smp_enable_sched()</code> lets APs take user work.</li>
  197. </ol>
  198. <p>
  199. Kernel threads stay pinned to the BSP; only ring-3 processes may run
  200. on an AP. Each AP runs <code>cpu_enable_sse()</code> because it starts
  201. with firmware <code>CR4</code>; without OSFXSR/OSXMMEXCPT the SSE2
  202. drawing fast paths would <code>#UD</code>. Every CPU also runs
  203. <code>cpu_init_fpu()</code> (<code>CR0.NE</code>/<code>MP</code>,
  204. <code>FNINIT</code>): an AP leaves INIT with <code>CR0.NE</code> clear
  205. and an x87 control word of <code>0x0040</code>, so every inexact
  206. floating-point result froze it until the next timer tick, and the
  207. browser's JavaScript ran hundreds of times slower on an AP. Each
  208. process owns its x87 state; <code>schedule()</code> saves it with
  209. <code>FNSAVE</code> and restores it with <code>FRSTOR</code> on every
  210. switch (108 bytes in <code>struct process</code>, no FXSR needed).
  211. </p>
  212. <div class="warn">
  213. <p><strong>No IOAPIC driver.</strong> Device IRQs never reach an AP,
  214. and <code>pit_irq_stub</code> is BSP-only. The kernel masks the
  215. IOAPIC's normal redirection entries after ACPI init
  216. (<span class="file">ioapic.c</span>) and keeps the 8259 path on
  217. LAPIC LINT0; ExtINT entries survive so firmware that routes the
  218. 8259 through an IOAPIC pin still works. The BSP LAPIC is brought up
  219. before the first interrupt and every PIC stub also EOIs it.</p>
  220. </div>
  221. </section>
  222. <section id="process">
  223. <h2>Process model</h2>
  224. <p>
  225. Every process is a stable heap-allocated PCB referenced from a sparse
  226. <code>procs[64]</code> table, so a process pointer never moves when
  227. another slot changes. The idle context is not a process slot: the
  228. <code>kernel_main</code> context becomes the BSP idle state, and APs
  229. have their own idle PCBs.
  230. </p>
  231. <ul>
  232. <li><strong>Kernel threads</strong> are created with <code>process_create</code> and take the BKL automatically.</li>
  233. <li><strong>Ring-3 processes</strong> are created from an ELF image with their own page directory, 32-page (128 KB) user stack and cwd.</li>
  234. <li><strong>The kworker</strong> is an ordinary kernel process that performs housekeeping and zombie reaping, so it cannot be starved by a busy system.</li>
  235. <li><strong>Stable naming:</strong> process names are 12 bytes; the PCB holds the TTY id, cwd, affinity mask, CPU accounting, the parent pid and the scheduling entity.</li>
  236. <li><strong>PID 1 is init.</strong> The kernel starts <code>/ramfs/bin/init</code> after the ramfs and <code>/share</code> mounts and before the WM. Init is a root process and the only creator of the WM and services; the kernel restarts it if it dies. <code>SYS_IS_INIT</code> lets the binary refuse a user-run start.</li>
  237. <li><strong>Parent death is the default.</strong> <code>process_exit()</code>, <code>process_kill()</code> and the reaper kill a process's whole descendant subtree. A child born while its parent is dying is aborted, and the process monitor's <em>End task</em> kills the subtree too.</li>
  238. <li><strong>Detached spawns.</strong> <code>SYS_SPAWN</code> takes a flags word. The default parents the child to the caller; <code>SYS_SPAWN_DETACH</code> skips the immediate parent and attaches the child to its parent (the terminal/WM session), or to 0 if that parent is gone. This keeps launched apps in the session tree: they outlive a file manager or chat window, but not the terminal/WM. Kernel commands (<code>SYS_SPAWN_KCMD</code>) stay parented. <code>SYS_PROCINFO</code> exposes <code>parent_pid</code>, and the graphical <code>process</code> monitor draws the table as an indented tree under each parent.</li>
  239. <li><strong>Fatal faults.</strong> A ring-3 fault kills that process (and its descendants) and keeps the kernel running; a ring-0 fault still shows the panic banner and halts.</li>
  240. </ul>
  241. <figure class="figure">
  242. <svg viewBox="0 0 920 260" role="img" aria-labelledby="states-title states-desc">
  243. <title id="states-title">Process state transitions</title>
  244. <desc id="states-desc">Processes move between ready, running, sleeping, blocked and dead states. Wake-ups return to ready, and exit or kill moves to dead.</desc>
  245. <rect class="svg-box green" x="48" y="96" width="150" height="58" rx="9"/>
  246. <text class="svg-text" x="123" y="120" text-anchor="middle">READY</text>
  247. <text class="svg-sub" x="123" y="140" text-anchor="middle">run queue</text>
  248. <rect class="svg-box accent" x="288" y="96" width="150" height="58" rx="9"/>
  249. <text class="svg-text" x="363" y="120" text-anchor="middle">RUNNING</text>
  250. <text class="svg-sub" x="363" y="140" text-anchor="middle">one per CPU</text>
  251. <rect class="svg-box blue" x="528" y="28" width="150" height="58" rx="9"/>
  252. <text class="svg-text" x="603" y="52" text-anchor="middle">SLEEPING</text>
  253. <text class="svg-sub" x="603" y="72" text-anchor="middle">deadline in sleep list</text>
  254. <rect class="svg-box blue" x="528" y="164" width="150" height="58" rx="9"/>
  255. <text class="svg-text" x="603" y="188" text-anchor="middle">BLOCKED</text>
  256. <text class="svg-sub" x="603" y="208" text-anchor="middle">wait queue</text>
  257. <rect class="svg-box amber" x="756" y="96" width="136" height="58" rx="9"/>
  258. <text class="svg-text" x="824" y="120" text-anchor="middle">DEAD</text>
  259. <text class="svg-sub" x="824" y="140" text-anchor="middle">zombie queue</text>
  260. <path class="svg-line green" d="M198 118h84"/>
  261. <path class="svg-line green" d="M276 112l7 6-7 6"/>
  262. <text class="svg-tiny" x="240" y="108" text-anchor="middle">pick</text>
  263. <path class="svg-line" d="M363 154v28H198v-24"/>
  264. <path class="svg-line" d="M192 162l6-7 6 7"/>
  265. <text class="svg-tiny" x="300" y="196" text-anchor="middle">slice / yield</text>
  266. <path class="svg-line blue" d="M438 112h84"/>
  267. <path class="svg-line blue" d="M516 106l7 6-7 6"/>
  268. <text class="svg-tiny" x="480" y="103" text-anchor="middle">sleep</text>
  269. <path class="svg-line blue" d="M438 138v46h84"/>
  270. <path class="svg-line blue" d="M516 178l7 6-7 6"/>
  271. <text class="svg-tiny" x="480" y="176" text-anchor="middle">wait</text>
  272. <path class="svg-line" d="M603 86V62h-240v34"/>
  273. <path class="svg-line" d="M357 90l6-7 6 7"/>
  274. <text class="svg-tiny" x="475" y="56" text-anchor="middle">wake</text>
  275. <path class="svg-line" d="M603 222v18H363v-18"/>
  276. <path class="svg-line" d="M357 228l6-7 6 7"/>
  277. <text class="svg-tiny" x="478" y="248" text-anchor="middle">wake</text>
  278. <path class="svg-line amber" d="M438 125h312"/>
  279. <path class="svg-line amber" d="M744 119l7 6-7 6"/>
  280. <text class="svg-tiny" x="594" y="119" text-anchor="middle">exit / kill</text>
  281. </svg>
  282. <figcaption>
  283. <strong>Five states, two queues.</strong> The run queue is global;
  284. the sleep list is sorted by deadline; wait queues are per-object.
  285. A woken process is clamped to the current minimum vruntime so an
  286. interactive wake-up is scheduled promptly without accumulating
  287. unbounded credit.
  288. </figcaption>
  289. </figure>
  290. </section>
  291. <section id="scheduler">
  292. <h2>CFS-lite scheduler</h2>
  293. <p>
  294. Weight is an integer from 1 to 16. Each ready process carries a
  295. 64-bit <code>vruntime</code> and a <code>stride = 2<sup>20</sup> / weight</code>.
  296. While a process runs, its vruntime advances by
  297. <code>elapsed_ms × stride</code>; the scheduler picks the runnable
  298. process with the smallest vruntime. The result is a long-term CPU
  299. share proportional to weight.
  300. </p>
  301. <ul>
  302. <li><strong>10 ms slice.</strong> A running process is preempted at the slice boundary unless it holds the BKL or has preemption disabled.</li>
  303. <li><strong>Global run queue.</strong> A single linked list protected by <code>sched_lock</code>; every CPU scans it for the minimum vruntime.</li>
  304. <li><strong>Wake-up clamp.</strong> A process waking from sleep or a wait queue is moved forward to the current minimum vruntime rather than receiving unlimited credit.</li>
  305. <li><strong>Affinity.</strong> A CPU bitmask restricts where a process may run; kernel threads are pinned to the BSP.</li>
  306. <li><strong><code>renice</code></strong> changes a live process's weight from the shell; the new stride takes effect immediately.</li>
  307. </ul>
  308. <figure class="figure">
  309. <div class="bars" role="img" aria-label="Scheduler weights: window manager 16, default processes and terminals 8, kworker 4, minimum 1. Bar widths are relative to 16.">
  310. <div class="bar-row">
  311. <span class="bar-label">WM / max</span>
  312. <div class="bar-track"><span class="bar-fill" style="--w:100%"></span></div>
  313. <span class="bar-value">16</span>
  314. </div>
  315. <div class="bar-row">
  316. <span class="bar-label">default / terminal</span>
  317. <div class="bar-track"><span class="bar-fill blue" style="--w:50%"></span></div>
  318. <span class="bar-value">8</span>
  319. </div>
  320. <div class="bar-row">
  321. <span class="bar-label">kworker</span>
  322. <div class="bar-track"><span class="bar-fill green" style="--w:25%"></span></div>
  323. <span class="bar-value">4</span>
  324. </div>
  325. <div class="bar-row">
  326. <span class="bar-label">minimum</span>
  327. <div class="bar-track"><span class="bar-fill" style="--w:6.25%"></span></div>
  328. <span class="bar-value">1</span>
  329. </div>
  330. </div>
  331. <figcaption>
  332. <strong>Weights seen in the tree.</strong> The ring-3 WM is raised
  333. to 16, the kworker is set to 4 and a new process starts at 8. Bar
  334. widths are relative to the maximum weight of 16.
  335. </figcaption>
  336. </figure>
  337. <p>
  338. The run queue is a flat list rather than a red-black tree; picking
  339. the minimum is a scan. With at most 64 processes this is simple and
  340. predictable, and the scheduler core stays small enough to audit.
  341. Sleeping processes sit in a sorted sleep list, so the tick handler
  342. wakes due sleepers without scanning every process.
  343. </p>
  344. </section>
  345. <section id="bkl">
  346. <h2>The big kernel lock</h2>
  347. <p>
  348. <code>bkl_*</code> is a sleeping recursive mutex, not a spinlock.
  349. Every ring-3 syscall and every kernel thread takes it; voluntary
  350. block and sleep points release it through <code>sched_yield</code>.
  351. Long serial and network waits use <code>bkl_pause()</code> /
  352. <code>bkl_resume()</code> so another CPU can make progress.
  353. </p>
  354. <ul>
  355. <li><strong>Not timer-preempted.</strong> A CPU running a BKL holder is skipped by the tick decision, so the owner can safely be in the middle of kernel data structures.</li>
  356. <li><strong>Recursive.</strong> The owner may re-enter; the depth is restored across a block in the PCB's <code>bkl_saved</code> field.</li>
  357. <li><strong>Sleep, not spin.</strong> A waiter blocks on the BKL wait queue and is woken when the owner releases.</li>
  358. <li><strong>Lock-free clock.</strong> <code>SYS_GET_TICKS_MS</code> is answered before the lock is taken (one aligned counter read). It is the hottest syscall: QuickJS's interrupt check called it every ~10000 operations and slept on a contended lock each time, and the browser's interrupt hook now reads the clock only every 16th check.</li>
  359. <li><strong>Contention statistics.</strong> <code>sched</code> prints how often a taker had to sleep, the total wait and the longest single hold with its holder, all since the previous <code>sched</code>.</li>
  360. <li><strong>Idle uses trylock.</strong> The idle fallback never blocks on the BKL; kworker and kernel commands take it normally.</li>
  361. </ul>
  362. <div class="note">
  363. <p><strong>Why one big lock?</strong> The kernel is small and not
  364. designed around fine-grained locking. The BKL keeps every kernel
  365. invariant simple, at the cost of serializing long kernel operations.
  366. Disk I/O still holds it, except a destructive format's raw-sector
  367. loop, which releases it so the WM and terminals keep running;
  368. network and serial waits drop it.</p>
  369. </div>
  370. </section>
  371. <section id="idle">
  372. <h2>Idle, power &amp; thermals</h2>
  373. <p>
  374. <code>idle auto</code> is the default. It asks <span class="file">power.c</span>
  375. for the deepest usable hint: C6 (<code>0x20</code>) on
  376. Nehalem/Westmere, C6N (<code>0x58</code>) on Airmont/Braswell when
  377. CPUID advertises the C6 substates, and HLT on the older Atom boards
  378. where an old USB-legacy SMM hang made deep states risky.
  379. </p>
  380. <ul>
  381. <li><strong>Nehalem/Westmere P-states.</strong> <code>power_init</code> enables SpeedStep, raises the package C-state limit to C6 when unlocked, reads min/max ratios from <code>PLATFORM_INFO</code> and starts at the maximum non-turbo ratio. The target ratio goes in bits 15:8 of <code>IA32_PERF_CTL</code> with the low byte zero.</li>
  382. <li><strong>Airmont/Braswell P-states.</strong> Family 6 model <code>0x4C</code> reads the Atom ratio/VID MSRs (<code>0x66A..0x66D</code>), <code>MSR_FSB_FREQ</code> and the FSB table. The N3060 reports pstates 6..20, BCLK 80 MHz (1600 MHz nominal), turbo pstate 31 (2480 MHz) and VID 0x2D..0x3C. <code>power_init</code> enters at max non-turbo and the governor drops to pstate 6 (480 MHz); <code>IA32_PERF_CTL</code> carries the pstate in 15:8 and the Linux-interpolated VID in 7:0. Turbo is detected but not requested. The probe deliberately avoids package/power-SKU MSRs; an unsupported <code>rdmsr</code> before the IDT triple-faults.</li>
  383. <li><strong>Kaby Lake HWP P-states.</strong> Family 6 model <code>0x8E</code> (the Yoga 720's i7-7500U) uses Speed Shift. <code>IA32_HWP_CAPABILITIES</code> (<code>0x771</code>) gives the highest, guaranteed, most-efficient and lowest ratios; the governor uses guaranteed..lowest (max non-turbo..minimum) and leaves turbo unrequested. <code>IA32_HWP_REQUEST</code> (<code>0x774</code>) carries min/max/desired plus the EPP (<code>balance_performance</code> = 128); <code>IA32_PM_ENABLE</code> bit 0 selects HWP, with legacy <code>IA32_PERF_CTL</code> as the fallback. Linux reports <code>intel_pstate</code> active, 400-3500 MHz, 32 pstates, 25% turbo and <code>balance_performance</code>; the same <code>power</code>/<code>cpufreq</code>/Settings path drives it.</li>
  384. <li><strong>Governor.</strong> A 100 ms window picks min/max/linear target pstates; short busy streaks jump to max immediately. Each CPU writes its own <code>IA32_PERF_CTL</code> (or <code>IA32_HWP_REQUEST</code> on Kaby Lake); <code>cpufreq [auto|min|max|off|&lt;pstate&gt;]</code> overrides it, and <code>SYS_CPUFREQ</code> exposes the same auto/min/max/off mode to the Settings Power page.</li>
  385. <li><strong>Telemetry.</strong> <code>power</code> prints effective MHz from APERF/MPERF, the current/requested pstate and, on Airmont, the FSB, nominal MHz, turbo pstate/VID, VID range and live <code>PERF_CTL</code>; <code>sensors</code> prints the CPU temperature, board sensors and all three EC types; <code>powerinfo</code> prints the AC adapter and battery state, and the WM top bar shows the temperature plus a far-right AC plug or level-filled battery icon on ThinkPads, IdeaPads/Yogas and Chromebooks.</li>
  386. <li><strong>Thermal.</strong> On Intel, each CPU samples its DTS once a second from its scheduler tick; the hottest core is exposed to ring 3. TjMax comes from <code>IA32_TEMPERATURE_TARGET</code> on Nehalem/Westmere and Kaby Lake, the coretemp 90 °C table on Airmont, and the Bonnell table on older Atoms. Kaby Lake-U (<code>0x8E</code>) reports the package DTS (Linux <code>x86_pkg_temp</code> 61-71 °C on the Yoga; the PCH <code>pch_skylake</code> 49-52 °C is not read because the DPTF/INT3403 tables are not parsed). On AMD family 15h models below <code>0x60</code> (Bulldozer/Piledriver/Steamroller), <span class="file">thermal.c</span> reads the package Tctl from <code>D18F3xA4</code> at PCI <code>0:18.3</code> (k10temp's <code>(raw × 125) m°C</code>, with the 49 °C range offset when bits 17:16 are both set) and publishes it to every CPU from the kworker under the BKL; PCI config space is deliberately not touched from the per-CPU timer interrupt.</li>
  387. <li><strong>Limits.</strong> Package C-states/S0ix remain firmware-managed; Airmont reports no C3/C6 residency and does not request the turbo pstate, and Kaby Lake HWP leaves turbo unrequested too. The explicit <code>idle c6</code> override is the Nehalem <code>0x20</code> hint; use <code>idle auto</code> for the Airmont C6N (<code>0x58</code>) path.</li>
  388. </ul>
  389. </section>
  390. <section id="sources">
  391. <h2>Source map</h2>
  392. <ul class="filelist">
  393. <li><code>kernel/sched.c</code>, <code>kernel/sched.h</code> — scheduler core and BKL</li>
  394. <li><code>kernel/process.c</code>, <code>kernel/process.h</code> — PCBs, parent tracking, subtree kill and lifecycle</li>
  395. <li><code>kernel/init.c</code>, <code>kernel/init.h</code> — PID-1 init bootstrap, protection and restart</li>
  396. <li><code>kernel/switch.S</code> — context switch</li>
  397. <li><code>kernel/tss.c</code>, <code>kernel/tss.h</code> — per-CPU TSS</li>
  398. <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>
  399. <li><code>kernel/poweroff.c</code> — ACPI S5 poweroff with the emulator port fallback</li>
  400. <li><code>kernel/reboot.c</code> — FADT ACPI reset with PCH/KBC/triple-fault fallbacks</li>
  401. <li><code>kernel/ap_tramp.S</code> — AP startup blob</li>
  402. <li><code>kernel/timer.S</code>, <code>kernel/idle.c</code>, <code>kernel/power.c</code></li>
  403. <li><code>kernel/thermal.c</code> — Intel DTS and AMD D18F3xA4 sampling</li>
  404. <li><code>kernel/spinlock.h</code> — IRQ-safe spinlocks</li>
  405. </ul>
  406. </section>
  407. </main>
  408. <footer class="site-footer">
  409. <div class="inner">
  410. <p><strong>NebOS 0.2.0</strong> · Architecture documentation · <a href="index.html">overview</a></p>
  411. <p>Maintain alongside <code>AGENTS.md</code> and the source files listed on each page.</p>
  412. </div>
  413. </footer>
  414. </body>
  415. </html>