cpu.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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, ACPI SMP bring-up, CFS-lite scheduling, wait queues, the big kernel lock, idle and power 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. Device IRQs stay on the
  80. BSP because there is no IOAPIC driver. APs mask their LINT0/LINT1
  81. inputs and use only their local APIC timer and IPIs.
  82. </p>
  83. <div class="table-wrap">
  84. <table>
  85. <thead>
  86. <tr><th>Source</th><th>Vector</th><th>Role</th></tr>
  87. </thead>
  88. <tbody>
  89. <tr><td>8259 PIC + PIT</td><td>IRQ0, etc.</td><td>BSP global clock and device interrupts; PIT tick advances the millisecond clock.</td></tr>
  90. <tr><td>LAPIC timer</td><td><code>0x30</code></td><td>Per-AP scheduling tick; 1 ms busy, 10 ms idle.</td></tr>
  91. <tr><td>LAPIC IPI</td><td><code>0x31</code></td><td>Reschedule request; must be EOIed on the target LAPIC.</td></tr>
  92. <tr><td><code>int 0x80</code></td><td>syscall gate</td><td>Ring-3 service entry.</td></tr>
  93. <tr><td><code>int 0x81</code></td><td>voluntary yield</td><td>Blocking helpers and <code>schedule()</code>.</td></tr>
  94. </tbody>
  95. </table>
  96. </div>
  97. <p>
  98. The timer period is not fixed. While a process runs, the BSP PIT ticks
  99. at 1 kHz and AP LAPIC timers tick every 1 ms. While a CPU is idle, the
  100. BSP drops to 100 Hz and APs to 10 ms so MWAIT/HLT can hold a deeper
  101. state. The global millisecond clock remains accurate because the PIT
  102. stub advances it by the current tick period.
  103. </p>
  104. <figure class="figure">
  105. <div class="bars" role="img" aria-label="Timer rates: 1000 Hz while busy, 100 Hz while idle. Bar widths are relative to 1000 Hz.">
  106. <div class="bar-row">
  107. <span class="bar-label">BSP busy</span>
  108. <div class="bar-track"><span class="bar-fill green" style="--w:100%"></span></div>
  109. <span class="bar-value">1000 Hz</span>
  110. </div>
  111. <div class="bar-row">
  112. <span class="bar-label">BSP idle</span>
  113. <div class="bar-track"><span class="bar-fill" style="--w:10%"></span></div>
  114. <span class="bar-value">100 Hz</span>
  115. </div>
  116. <div class="bar-row">
  117. <span class="bar-label">AP busy</span>
  118. <div class="bar-track"><span class="bar-fill green" style="--w:100%"></span></div>
  119. <span class="bar-value">1 ms</span>
  120. </div>
  121. <div class="bar-row">
  122. <span class="bar-label">AP idle</span>
  123. <div class="bar-track"><span class="bar-fill" style="--w:10%"></span></div>
  124. <span class="bar-value">10 ms</span>
  125. </div>
  126. </div>
  127. <figcaption>
  128. <strong>Race to idle, then stay there.</strong> When a wake-up
  129. arrives, <code>smp_resched_wake()</code> shortens the idle period so
  130. the woken process runs at the next millisecond instead of waiting
  131. out a 10 ms nap.
  132. </figcaption>
  133. </figure>
  134. </section>
  135. <section id="smp">
  136. <h2>SMP bring-up</h2>
  137. <p>
  138. SMP is ACPI-only: RSDP → RSDT/XSDT → MADT → CPU/LAPIC list. There is
  139. deliberately no Intel MP-table parser. A machine without ACPI, a
  140. usable MADT, the CPUID APIC bit or a LAPIC stays uniprocessor and
  141. follows exactly the old 8259/PIT path.
  142. </p>
  143. <p>
  144. The same table walk reads the FADT's <code>century</code> byte (offset
  145. 108) and exposes the CMOS register to <span class="file">rtc.c</span>,
  146. which writes it when setting the clock so the two-digit year stays in
  147. the intended century. Boards without ACPI get no century write and
  148. keep the 1970–2069 read window.
  149. </p>
  150. <ol>
  151. <li>Copy the AP trampoline to <code>0x6000</code> (SIPI vector <code>0x06</code>).</li>
  152. <li>Allocate an AP idle PCB and kernel stack via <code>process_init_ap()</code>.</li>
  153. <li>Fill the startup block at <code>0x6F00</code> with CR3, stack top, C entry and CPU index.</li>
  154. <li>Send INIT, then SIPI-SIPI, with a second SIPI retry.</li>
  155. <li>The AP enters <code>smp_ap_main</code>, initialises its LAPIC timer and idle loop, and waits.</li>
  156. <li>After device/FS setup, <code>smp_enable_sched()</code> lets APs take user work.</li>
  157. </ol>
  158. <p>
  159. Kernel threads stay pinned to the BSP; only ring-3 processes may run
  160. on an AP. Each AP runs <code>cpu_enable_sse()</code> because it starts
  161. with firmware <code>CR4</code>; without OSFXSR/OSXMMEXCPT the SSE2
  162. drawing fast paths would <code>#UD</code>.
  163. </p>
  164. <div class="warn">
  165. <p><strong>No IOAPIC.</strong> Device IRQs never reach an AP, and
  166. <code>pit_irq_stub</code> is BSP-only. A platform that routes ISA IRQs
  167. through the IOAPIC would need a mask-all setup step after ACPI init.</p>
  168. </div>
  169. </section>
  170. <section id="process">
  171. <h2>Process model</h2>
  172. <p>
  173. Every process is a stable heap-allocated PCB referenced from a sparse
  174. <code>procs[64]</code> table, so a process pointer never moves when
  175. another slot changes. The idle context is not a process slot: the
  176. <code>kernel_main</code> context becomes the BSP idle state, and APs
  177. have their own idle PCBs.
  178. </p>
  179. <ul>
  180. <li><strong>Kernel threads</strong> are created with <code>process_create</code> and take the BKL automatically.</li>
  181. <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>
  182. <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>
  183. <li><strong>Stable naming:</strong> process names are 12 bytes; the PCB holds the TTY id, cwd, affinity mask, CPU accounting and the scheduling entity.</li>
  184. </ul>
  185. <figure class="figure">
  186. <svg viewBox="0 0 920 260" role="img" aria-labelledby="states-title states-desc">
  187. <title id="states-title">Process state transitions</title>
  188. <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>
  189. <rect class="svg-box green" x="48" y="96" width="150" height="58" rx="9"/>
  190. <text class="svg-text" x="123" y="120" text-anchor="middle">READY</text>
  191. <text class="svg-sub" x="123" y="140" text-anchor="middle">run queue</text>
  192. <rect class="svg-box accent" x="288" y="96" width="150" height="58" rx="9"/>
  193. <text class="svg-text" x="363" y="120" text-anchor="middle">RUNNING</text>
  194. <text class="svg-sub" x="363" y="140" text-anchor="middle">one per CPU</text>
  195. <rect class="svg-box blue" x="528" y="28" width="150" height="58" rx="9"/>
  196. <text class="svg-text" x="603" y="52" text-anchor="middle">SLEEPING</text>
  197. <text class="svg-sub" x="603" y="72" text-anchor="middle">deadline in sleep list</text>
  198. <rect class="svg-box blue" x="528" y="164" width="150" height="58" rx="9"/>
  199. <text class="svg-text" x="603" y="188" text-anchor="middle">BLOCKED</text>
  200. <text class="svg-sub" x="603" y="208" text-anchor="middle">wait queue</text>
  201. <rect class="svg-box amber" x="756" y="96" width="136" height="58" rx="9"/>
  202. <text class="svg-text" x="824" y="120" text-anchor="middle">DEAD</text>
  203. <text class="svg-sub" x="824" y="140" text-anchor="middle">zombie queue</text>
  204. <path class="svg-line green" d="M198 118h84"/>
  205. <path class="svg-line green" d="M276 112l7 6-7 6"/>
  206. <text class="svg-tiny" x="240" y="108" text-anchor="middle">pick</text>
  207. <path class="svg-line" d="M363 154v28H198v-24"/>
  208. <path class="svg-line" d="M192 162l6-7 6 7"/>
  209. <text class="svg-tiny" x="300" y="196" text-anchor="middle">slice / yield</text>
  210. <path class="svg-line blue" d="M438 112h84"/>
  211. <path class="svg-line blue" d="M516 106l7 6-7 6"/>
  212. <text class="svg-tiny" x="480" y="103" text-anchor="middle">sleep</text>
  213. <path class="svg-line blue" d="M438 138v46h84"/>
  214. <path class="svg-line blue" d="M516 178l7 6-7 6"/>
  215. <text class="svg-tiny" x="480" y="176" text-anchor="middle">wait</text>
  216. <path class="svg-line" d="M603 86V62h-240v34"/>
  217. <path class="svg-line" d="M357 90l6-7 6 7"/>
  218. <text class="svg-tiny" x="475" y="56" text-anchor="middle">wake</text>
  219. <path class="svg-line" d="M603 222v18H363v-18"/>
  220. <path class="svg-line" d="M357 228l6-7 6 7"/>
  221. <text class="svg-tiny" x="478" y="248" text-anchor="middle">wake</text>
  222. <path class="svg-line amber" d="M438 125h312"/>
  223. <path class="svg-line amber" d="M744 119l7 6-7 6"/>
  224. <text class="svg-tiny" x="594" y="119" text-anchor="middle">exit / kill</text>
  225. </svg>
  226. <figcaption>
  227. <strong>Five states, two queues.</strong> The run queue is global;
  228. the sleep list is sorted by deadline; wait queues are per-object.
  229. A woken process is clamped to the current minimum vruntime so an
  230. interactive wake-up is scheduled promptly without accumulating
  231. unbounded credit.
  232. </figcaption>
  233. </figure>
  234. </section>
  235. <section id="scheduler">
  236. <h2>CFS-lite scheduler</h2>
  237. <p>
  238. Weight is an integer from 1 to 16. Each ready process carries a
  239. 64-bit <code>vruntime</code> and a <code>stride = 2<sup>20</sup> / weight</code>.
  240. While a process runs, its vruntime advances by
  241. <code>elapsed_ms × stride</code>; the scheduler picks the runnable
  242. process with the smallest vruntime. The result is a long-term CPU
  243. share proportional to weight.
  244. </p>
  245. <ul>
  246. <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>
  247. <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>
  248. <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>
  249. <li><strong>Affinity.</strong> A CPU bitmask restricts where a process may run; kernel threads are pinned to the BSP.</li>
  250. <li><strong><code>renice</code></strong> changes a live process's weight from the shell; the new stride takes effect immediately.</li>
  251. </ul>
  252. <figure class="figure">
  253. <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.">
  254. <div class="bar-row">
  255. <span class="bar-label">WM / max</span>
  256. <div class="bar-track"><span class="bar-fill" style="--w:100%"></span></div>
  257. <span class="bar-value">16</span>
  258. </div>
  259. <div class="bar-row">
  260. <span class="bar-label">default / terminal</span>
  261. <div class="bar-track"><span class="bar-fill blue" style="--w:50%"></span></div>
  262. <span class="bar-value">8</span>
  263. </div>
  264. <div class="bar-row">
  265. <span class="bar-label">kworker</span>
  266. <div class="bar-track"><span class="bar-fill green" style="--w:25%"></span></div>
  267. <span class="bar-value">4</span>
  268. </div>
  269. <div class="bar-row">
  270. <span class="bar-label">minimum</span>
  271. <div class="bar-track"><span class="bar-fill" style="--w:6.25%"></span></div>
  272. <span class="bar-value">1</span>
  273. </div>
  274. </div>
  275. <figcaption>
  276. <strong>Weights seen in the tree.</strong> The ring-3 WM is raised
  277. to 16, the kworker is set to 4 and a new process starts at 8. Bar
  278. widths are relative to the maximum weight of 16.
  279. </figcaption>
  280. </figure>
  281. <p>
  282. The run queue is a flat list rather than a red-black tree; picking
  283. the minimum is a scan. With at most 64 processes this is simple and
  284. predictable, and the scheduler core stays small enough to audit.
  285. Sleeping processes sit in a sorted sleep list, so the tick handler
  286. wakes due sleepers without scanning every process.
  287. </p>
  288. </section>
  289. <section id="bkl">
  290. <h2>The big kernel lock</h2>
  291. <p>
  292. <code>bkl_*</code> is a sleeping recursive mutex, not a spinlock.
  293. Every ring-3 syscall and every kernel thread takes it; voluntary
  294. block and sleep points release it through <code>sched_yield</code>.
  295. Long serial and network waits use <code>bkl_pause()</code> /
  296. <code>bkl_resume()</code> so another CPU can make progress.
  297. </p>
  298. <ul>
  299. <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>
  300. <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>
  301. <li><strong>Sleep, not spin.</strong> A waiter blocks on the BKL wait queue and is woken when the owner releases.</li>
  302. <li><strong>Idle uses trylock.</strong> The idle fallback never blocks on the BKL; kworker and kernel commands take it normally.</li>
  303. </ul>
  304. <div class="note">
  305. <p><strong>Why one big lock?</strong> The kernel is small and not
  306. designed around fine-grained locking. The BKL keeps every kernel
  307. invariant simple, at the cost of serializing long kernel operations.
  308. Disk I/O still holds it; network and serial waits drop it.</p>
  309. </div>
  310. </section>
  311. <section id="idle">
  312. <h2>Idle, power &amp; thermals</h2>
  313. <p>
  314. <code>idle auto</code> is the default. It asks <span class="file">power.c</span>
  315. for the deepest usable hint: C6 (<code>0x20</code>) on the
  316. Nehalem/Westmere allowlist, HLT everywhere else. The Atom boards keep
  317. HLT because of an old USB-legacy SMM hang; the UHCI/OHCI
  318. <code>0xC0</code> legacy-SMI disable addresses it.
  319. </p>
  320. <ul>
  321. <li><strong>P-states.</strong> On the allowlisted Nehalem/Westmere models, <code>power_init</code> enables SpeedStep, raises the package C-state limit to C6 when unlocked, reads min/max ratios and starts at the maximum non-turbo ratio.</li>
  322. <li><strong>Governor.</strong> A 100 ms window picks min/max/linear target ratios; short busy streaks jump to max immediately. Each CPU writes its own <code>IA32_PERF_CTL</code>.</li>
  323. <li><strong>Telemetry.</strong> <code>power</code> prints effective MHz from APERF/MPERF, ratios and C3/C6 residency; <code>sensors</code> prints DTS, board sensors and ThinkPad EC temperatures/fan; <code>powerinfo</code> prints the AC adapter and battery state, and the WM top bar shows the AC adapter state and battery percentage on ThinkPads.</li>
  324. <li><strong>Thermal.</strong> Each CPU samples its DTS once a second from its scheduler tick; the hottest core is exposed to ring 3.</li>
  325. </ul>
  326. </section>
  327. <section id="sources">
  328. <h2>Source map</h2>
  329. <ul class="filelist">
  330. <li><code>sched.c</code>, <code>sched.h</code> — scheduler core and BKL</li>
  331. <li><code>process.c</code>, <code>process.h</code> — PCBs and lifecycle</li>
  332. <li><code>switch.S</code> — context switch</li>
  333. <li><code>tss.c</code>, <code>tss.h</code> — per-CPU TSS</li>
  334. <li><code>acpi.c</code>, <code>lapic.c</code>, <code>smp.c</code> — SMP</li>
  335. <li><code>ap_tramp.S</code> — AP startup blob</li>
  336. <li><code>timer.S</code>, <code>idle.c</code>, <code>power.c</code></li>
  337. <li><code>thermal.c</code> — DTS sampling</li>
  338. <li><code>spinlock.h</code> — IRQ-safe spinlocks</li>
  339. </ul>
  340. </section>
  341. </main>
  342. <footer class="site-footer">
  343. <div class="inner">
  344. <p><strong>NebOS 0.2.0</strong> · Architecture documentation · <a href="index.html">overview</a></p>
  345. <p>Maintain alongside <code>AGENTS.md</code> and the source files listed on each page.</p>
  346. </div>
  347. </footer>
  348. </body>
  349. </html>