userland.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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>Userland | NebOS Architecture</title>
  7. <meta name="description" content="NebOS userland: ring-3 address space, ELF loading, the syscall ABI, the shell and PATH dispatch, the 42-command pack, GUI applications and the browser.">
  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">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" aria-current="page">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">Userland</p>
  34. <h1>Commands are ELFs. The shell is a dispatcher.</h1>
  35. <p class="lede">
  36. Ring-3 programs are static ELFs linked at <code>0x40000000</code>.
  37. The shell resolves a command name against builtins, the per-terminal
  38. PATH and the kernel command registry, then spawns it with one
  39. argument string. The command pack supplies 42 PATH commands by default.
  40. </p>
  41. <div class="meta">
  42. <span class="pill">int 0x80</span>
  43. <span class="pill">one string argument</span>
  44. <span class="pill">42 PATH commands</span>
  45. <span class="pill">8 TTYs · 8 windows</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="#model">Ring-3 model</a></li>
  52. <li><a href="#libc">Ring-3 libc</a></li>
  53. <li><a href="#spawn">Spawn path</a></li>
  54. <li><a href="#syscalls">Syscall ABI</a></li>
  55. <li><a href="#shell">Shell &amp; PATH</a></li>
  56. <li><a href="#registry">Kernel commands</a></li>
  57. <li><a href="#pack">Command pack</a></li>
  58. <li><a href="#apps">GUI applications</a></li>
  59. <li><a href="#browser">Browser</a></li>
  60. <li><a href="#sources">Source map</a></li>
  61. </ul>
  62. </nav>
  63. <section id="model">
  64. <h2>Ring-3 model</h2>
  65. <p>
  66. A user process gets a page directory cloned from the kernel identity
  67. map, a 2 MB image/stack window at <code>0x40000000</code>, an optional
  68. 4 MB canvas at <code>0x50000000</code> and a growable heap at
  69. <code>0x60000000</code>. The kernel remains mapped because paging is
  70. identity-mapped; this is isolation in privilege level only, not a
  71. separate address space.
  72. </p>
  73. <ul>
  74. <li><strong>Optional libc.</strong> Commands include <span class="file">ucmd_lib.h</span> for syscall wrappers; commands that need allocation or standard string/stdlib functions link the in-tree <span class="file">libc.a</span>.</li>
  75. <li><strong>One argument.</strong> The shell passes the raw remainder of the line as a C string at the top of the user stack; <code>_start(const char *arg)</code> needs no crt0.</li>
  76. <li><strong>Inherited context.</strong> Each process inherits the spawning terminal's cwd and TTY id; the file syscalls resolve relative paths against the PCB cwd.</li>
  77. <li><strong>Page-fault policy.</strong> A ring-3 fault kills the process and prints a short message; it does not halt the kernel.</li>
  78. </ul>
  79. <div class="note">
  80. <p><strong>Not a sandbox.</strong> Because physical and virtual addresses
  81. are identical and the kernel is mapped, a ring-3 bug can read or write
  82. any mapped kernel page. The privilege boundary protects against
  83. accidental corruption from ordinary code, not against malicious code.</p>
  84. </div>
  85. </section>
  86. <section id="libc">
  87. <h2>Ring-3 libc</h2>
  88. <p>
  89. <span class="file">libc.a</span> is optional: the Makefile links it
  90. only into commands that reference it, and the archive plus
  91. <code>--gc-sections</code> keeps unused members and functions out of
  92. the legacy command pack. The headers live in
  93. <span class="file">libc/include/</span> and cover
  94. <code>stddef</code>, <code>stdint</code>, <code>stdarg</code>,
  95. <code>stdlib</code>, <code>string</code>, <code>ctype</code>,
  96. <code>assert</code> and the OS-specific <code>ucmd_heap.h</code>.
  97. </p>
  98. <ul>
  99. <li><strong>malloc.</strong> Boundary-tag blocks, first-fit free
  100. list, immediate coalescing, 16-byte alignment. The heap maps 64 KB
  101. on first use and grows in page-rounded steps with 64 KB slack up to
  102. the 32 MB <code>SYS_MEM_*</code> arena cap; <code>calloc</code>
  103. zeroes and <code>realloc</code> grows in place when the next block
  104. is free.</li>
  105. <li><strong>heap_reset.</strong> Discards every allocation and
  106. unmaps the whole arena. Page-scoped programs such as the browser
  107. use it on navigation instead of tracking individual frees.</li>
  108. <li><strong>Ownership.</strong> Commands allocate through libc
  109. <code>malloc</code>; the <code>SYS_MEM_ALLOC/GROW/FREE</code>
  110. syscalls are the backend in <span class="file">libc/ucmd_mem.c</span>
  111. and are not called by application code.</li>
  112. <li><strong>Limits.</strong> No threads or locking, no
  113. <code>free()</code>-to-page unmap, and requests near 32 MB fail
  114. because headers are charged against the same arena. Console
  115. <code>printf</code>/<code>FILE</code> I/O is not part of this layer;
  116. file access still uses the whole-file syscalls.</li>
  117. <li><strong>Test.</strong> <code>make test-libc</code> runs a host
  118. stress test over a simulated 32 MB arena, checking alignment,
  119. coalescing, <code>realloc</code>/<code>calloc</code>, growth and
  120. <code>heap_reset</code>.</li>
  121. </ul>
  122. </section>
  123. <section id="spawn">
  124. <h2>Spawn path</h2>
  125. <figure class="figure">
  126. <svg viewBox="0 0 920 232" role="img" aria-labelledby="spawn-title spawn-desc">
  127. <title id="spawn-title">Command spawn path</title>
  128. <desc id="spawn-desc">A shell line is matched against builtins, then the PATH, then the kernel registry. A PATH hit loads the ELF through the VFS and creates a ring-3 process; a kernel registry hit spawns a kernel thread.</desc>
  129. <rect class="svg-box" x="20" y="24" width="160" height="58" rx="9"/>
  130. <text class="svg-text" x="38" y="49">shell line</text>
  131. <text class="svg-sub" x="38" y="69">word + remainder</text>
  132. <rect class="svg-box blue" x="216" y="24" width="160" height="58" rx="9"/>
  133. <text class="svg-text" x="234" y="49">builtins</text>
  134. <text class="svg-sub" x="234" y="69">cd · path · help · clear</text>
  135. <rect class="svg-box accent" x="412" y="24" width="180" height="58" rx="9"/>
  136. <text class="svg-text" x="430" y="49">PATH lookup</text>
  137. <text class="svg-sub" x="430" y="69">/ramfs/bin by default</text>
  138. <rect class="svg-box" x="628" y="24" width="270" height="58" rx="9"/>
  139. <text class="svg-text" x="646" y="49">kernel registry</text>
  140. <text class="svg-sub" x="646" y="69">privileged / inline commands</text>
  141. <path class="svg-line" d="M180 53h24"/>
  142. <path class="svg-line" d="M196 47l7 6-7 6"/>
  143. <path class="svg-line" d="M376 53h24"/>
  144. <path class="svg-line" d="M392 47l7 6-7 6"/>
  145. <path class="svg-line" d="M592 53h24"/>
  146. <path class="svg-line" d="M608 47l7 6-7 6"/>
  147. <rect class="svg-box green" x="216" y="146" width="376" height="58" rx="9"/>
  148. <text class="svg-text" x="236" y="171">VFS load → process_create_user</text>
  149. <text class="svg-sub" x="236" y="191">ELF segments · page directory · 8-page stack · arg</text>
  150. <rect class="svg-box amber" x="628" y="146" width="270" height="58" rx="9"/>
  151. <text class="svg-text" x="646" y="171">kernel process</text>
  152. <text class="svg-sub" x="646" y="191">ring 0 · pinned to the BSP</text>
  153. <path class="svg-line accent" d="M502 82v54"/>
  154. <path class="svg-line accent" d="M496 128l6 7 6-7"/>
  155. <path class="svg-line" d="M763 82v54"/>
  156. <path class="svg-line" d="M757 128l6 7 6-7"/>
  157. </svg>
  158. <figcaption>
  159. <strong>A packed command can shadow a kernel command.</strong>
  160. The PATH is searched before the kernel registry, so a new
  161. <code>bin/&lt;name&gt;</code> changes behaviour without editing the
  162. kernel. Builtins are handled inline, before either lookup.
  163. </figcaption>
  164. </figure>
  165. </section>
  166. <section id="syscalls">
  167. <h2>Syscall ABI</h2>
  168. <p>
  169. The gate is <code>int 0x80</code>. <code>eax</code> carries the
  170. number, <code>ebx</code>/<code>ecx</code>/<code>edx</code> the first
  171. three arguments, and the negative <code>SYS_E*</code> codes are
  172. stable. The wrappers in <span class="file">ucmd_lib.h</span> cover
  173. zero to three arguments.
  174. </p>
  175. <div class="table-wrap">
  176. <table>
  177. <thead>
  178. <tr><th>Group</th><th>Numbers</th><th>Purpose</th></tr>
  179. </thead>
  180. <tbody>
  181. <tr><td>Console &amp; time</td><td>1–4, 56, 69</td><td>putstr, ticks, sleep, exit, RTC time/date</td></tr>
  182. <tr><td>Files</td><td>5–11, 48, 98</td><td>whole-file read/write, list, unlink, mkdir/rmdir, mounts, cwd, stat</td></tr>
  183. <tr><td>System info</td><td>12–16, 49–51, 91–92, 102–103</td><td>memory, E820, mouse, storage, blkdev, procinfo, cursor, CPU temp, sensors, EC sensors, AC/battery</td></tr>
  184. <tr><td>Network</td><td>17–47</td><td>NIC config/acquire, raw frames, DHCP/DNS, TCP client, ARP, cwd</td></tr>
  185. <tr><td>Window &amp; WM</td><td>55, 57–72, 80, 83–87</td><td>key poll, WM attach/info/map/present/create_term, window create/present/input/resize</td></tr>
  186. <tr><td>Process &amp; memory</td><td>73–79, 81, 88–90</td><td>TTY, spawn, alive, kill, chdir, scheduler info, heap alloc/grow/free</td></tr>
  187. <tr><td>TCP server</td><td>93–97, 99–101</td><td>listen, accept, server recv/send/close, peer, speed, MAC set</td></tr>
  188. </tbody>
  189. </table>
  190. </div>
  191. <p>
  192. The file service is whole-file, not stream-oriented. The network
  193. service has a single TCP connection and one NIC owner. There is no
  194. <code>fork</code>, <code>exec</code> or shared memory: a command
  195. either runs as its own process from an ELF or calls a kernel command.
  196. </p>
  197. </section>
  198. <section id="shell">
  199. <h2>Shell &amp; PATH</h2>
  200. <p>
  201. The windowed shell is <span class="file">ucmd_term.c</span>; the
  202. kernel-side <span class="file">terminal.c</span> holds the privileged
  203. command registry used through <code>SYS_SPAWN_KCMD</code>. There is
  204. no kernel terminal fallback; the WM spawns the first terminal once
  205. storage is ready. Each terminal has a current working directory and
  206. a PATH (default <code>/ramfs/bin</code>). Pressing Tab completes
  207. builtin and PATH names; <code>help</code> lists both.
  208. </p>
  209. <ul>
  210. <li><strong>Dispatch order.</strong> Builtins → PATH file → kernel command. A PATH hit loads the ELF through the VFS and spawns it as ring 3.</li>
  211. <li><strong>Builtins.</strong> <code>cd</code>, <code>path</code>, <code>help</code>, <code>clear</code>; the ring-3 shell also handles <code>ps</code> and <code>kill</code> inline. <code>bootlog</code> is a kernel command.</li>
  212. <li><strong>Arguments.</strong> The raw remainder of the line is passed verbatim; commands parse their own flags.</li>
  213. <li><strong>Working directory.</strong> The prompt shows <code>[dir] &gt;</code>; <code>cd</code> changes only the current terminal's cwd, and spawned commands inherit it.</li>
  214. <li><strong>Windows.</strong> A window receives keys through its window key queue (<code>SYS_WIN_KEY_WAIT</code>) or the generic <code>SYS_KEY_POLL</code>; closing the window kills its owner process.</li>
  215. </ul>
  216. </section>
  217. <section id="registry">
  218. <h2>Kernel commands</h2>
  219. <p>
  220. These commands must run in ring 0 or inline in the shell; they are not
  221. files in the pack. The split keeps privileged hardware access out of
  222. ring 3 while most user-facing tools remain replaceable ELFs.
  223. </p>
  224. <div class="table-wrap">
  225. <table>
  226. <thead>
  227. <tr><th>Group</th><th>Commands</th></tr>
  228. </thead>
  229. <tbody>
  230. <tr><td>Shell</td><td><code>cd</code> <code>path</code> <code>help</code> <code>clear</code> <code>bootlog</code></td></tr>
  231. <tr><td>Process / scheduler</td><td><code>ps</code> <code>sched</code> <code>renice</code> <code>kill</code> <code>idle</code> <code>cpufreq</code> <code>power</code></td></tr>
  232. <tr><td>Hardware / debug</td><td><code>display</code> <code>gfxbench</code> <code>lsusb</code> <code>lspci</code> <code>peek</code> <code>poke</code> <code>format</code> <code>dbg</code> <code>serial</code></td></tr>
  233. <tr><td>Network control</td><td><code>netdev</code></td></tr>
  234. <tr><td>Boot / power</td><td><code>reboot</code> <code>poweroff</code> <code>kexec</code></td></tr>
  235. </tbody>
  236. </table>
  237. </div>
  238. </section>
  239. <section id="pack">
  240. <h2>Command pack</h2>
  241. <p>
  242. The pack is built from the <code>UCMDS</code> list in the Makefile as
  243. <code>bin/&lt;name&gt;</code>, plus <code>hello</code> and the real
  244. <code>browser.app</code> and <code>gallery.app</code>. It is mounted
  245. read-only at <code>/ramfs</code>; the shell's default PATH points at
  246. <code>/ramfs/bin</code>.
  247. </p>
  248. <div class="grid">
  249. <div class="card">
  250. <h3>Files &amp; text</h3>
  251. <p><code>ls</code> <code>cat</code> <code>save</code> <code>rm</code> <code>mkdir</code> <code>rmdir</code> <code>edit</code> <code>files</code> <code>view</code></p>
  252. </div>
  253. <div class="card">
  254. <h3>System</h3>
  255. <p><code>cpuinfo</code> <code>meminfo</code> <code>storage</code> <code>top</code> <code>mouseinfo</code> <code>sensors</code> <code>powerinfo</code> <code>spin</code> <code>clock</code> <code>colors</code></p>
  256. </div>
  257. <div class="card">
  258. <h3>Network</h3>
  259. <p><code>netinfo</code> <code>netip</code> <code>netdns</code> <code>host</code> <code>dhcp</code> <code>ping</code> <code>pong</code> <code>netsend</code> <code>netrecv</code> <code>netlog</code> <code>netconsole</code> <code>netregs</code> <code>netloop</code> <code>netreset</code> <code>net100</code> <code>netmac</code> <code>curl</code> <code>websrv</code></p>
  260. </div>
  261. <div class="card">
  262. <h3>GUI</h3>
  263. <p><code>wm</code> <code>term</code> <code>files</code> <code>view</code> <code>mines</code> <code>solitaire</code> <code>gallery</code> <code>browser</code> <code>clock</code> <code>colors</code></p>
  264. </div>
  265. </div>
  266. <p>
  267. The launchers <code>bin/browser</code> and <code>bin/gallery</code>
  268. forward their argument and cwd, and prefer a development copy under
  269. <code>/share</code> (<code>BROWSER</code> or <code>GALLERY</code>)
  270. before the pack copy and the FAT fallback on <code>root.img</code>.
  271. This lets <code>make browser-dev</code> and <code>make gallery-dev</code>
  272. update an app without rebuilding the pack.
  273. </p>
  274. </section>
  275. <section id="apps">
  276. <h2>GUI applications</h2>
  277. <ul>
  278. <li><strong>files</strong> — browser for the VFS; opens text in <code>edit</code>, images in <code>view</code>, HTML in <code>browser</code>. First user of ngui.</li>
  279. <li><strong>gallery</strong> — live widget and primitive showcase; the real app ships in the pack.</li>
  280. <li><strong>solitaire</strong> — Klondike with drag-and-drop, scoring and a win animation.</li>
  281. <li><strong>mines</strong> — second ngui canvas app; shares the toolkit and event model.</li>
  282. <li><strong>view</strong> — fit/1:1/zoom/pan image viewer, animated GIF playback.</li>
  283. <li><strong>edit</strong> — text editor that draws its own text with the 8x16 font.</li>
  284. <li><strong>clock</strong> / <strong>colors</strong> — small window canvas demos.</li>
  285. <li><strong>wm</strong> / <strong>term</strong> — the ring-3 window manager and terminal launcher used by the desktop.</li>
  286. </ul>
  287. <p>
  288. The WM chrome, ngui labels and the browser use the fixed-point
  289. TrueType engine in <span class="file">ufont.c</span> with the Arimo
  290. and Liberation Mono subsets in the pack. CSS and ngui can select
  291. regular/bold/italic/mono faces at 8–48 px; missing faces fall back to
  292. the 8×16 bitmap. Terminal contents, the editor and text-input cells
  293. stay bitmap. The browser detaches the font arena before
  294. <code>heap_reset()</code> and reattaches after the page heap is
  295. mapped.
  296. </p>
  297. </section>
  298. <section id="browser">
  299. <h2>Browser</h2>
  300. <p>
  301. The real browser is <code>bin/browser.app</code> in the pack. It links
  302. ngui, the image library, inflate, TLS and uhttp. It owns a 960×640
  303. window, a URL bar, history, a status bar and inline images. Network
  304. work runs in a headless <code>curl __fetch</code> helper that writes
  305. the body and metadata under <code>/share</code>, so the UI keeps
  306. drawing and scrolling.
  307. </p>
  308. <ul>
  309. <li><strong>HTML/CSS subset.</strong> External and inline stylesheets, tag/class/id and descendant/child selectors, colours, backgrounds, real <code>font-size</code> (8–48 px), bold/italic faces, <code>font-family: monospace</code>, margins, padding, borders, alignment, percentage widths, basic floats and <code>@media</code> blocks evaluated at a fixed 1000 px viewport. Text is Latin-1; there is no shaping, bidi or <code>@font-face</code> yet.</li>
  310. <li><strong>Not supported.</strong> JavaScript, forms, pseudo-classes, border-radius clipping of descendants, gradients/box-shadow/transitions and CSS <code>color-mix</code>. Flex rows, card grids, basic tables, inline SVG and CSS variables are supported.</li>
  311. <li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
  312. <li><strong>Fetching.</strong> Each network fetch starts a one-shot helper process and a fresh TCP/TLS connection; images load one per loop within the viewport plus 160 px.</li>
  313. </ul>
  314. <p>
  315. The browser's default canvas is dark, matching the ngui theme. Pages
  316. that set dark text but rely on a white default background can be hard
  317. to read; pages with their own full background and colour rules are
  318. unaffected. See <a href="graphics.html#images">Graphics → Image
  319. library</a> for the decoder limits.
  320. </p>
  321. </section>
  322. <section id="sources">
  323. <h2>Source map</h2>
  324. <ul class="filelist">
  325. <li><code>syscall.c</code>, <code>syscall_stub.S</code>, <code>syscall.h</code></li>
  326. <li><code>sys_info.c</code> — kernel-state snapshots (memory, sensors, power)</li>
  327. <li><code>process.c</code> — ELF load and spawn</li>
  328. <li><code>elf.c</code> — ELF parser</li>
  329. <li><code>ucmd_term.c</code> — windowed shell, scrollback, PATH</li>
  330. <li><code>terminal.c</code> — privileged kernel command registry</li>
  331. <li><code>ucmd_lib.h</code> — user shim</li>
  332. <li><code>libc/</code> — headers, malloc, string/stdlib support and the <code>SYS_MEM_*</code> backend</li>
  333. <li><code>libc.a</code> — optional static ring-3 libc</li>
  334. <li><code>ucmd.ld</code> — ring-3 link address</li>
  335. <li><code>Makefile</code> — <code>UCMDS</code> and pack rules</li>
  336. <li><code>ucmd_*.c</code> — commands</li>
  337. <li><code>ufont.c</code>, <code>ufont_ttf.c</code> — fixed-point TrueType text</li>
  338. <li><code>ngui.c</code> — GUI toolkit; <code>fonts/*.ttf</code> — UI subsets</li>
  339. <li><code>browser.c</code>, <code>browser_html.c</code>, <code>gallery.c</code></li>
  340. </ul>
  341. </section>
  342. </main>
  343. <footer class="site-footer">
  344. <div class="inner">
  345. <p><strong>NebOS 0.2.0</strong> · Architecture documentation · <a href="index.html">overview</a></p>
  346. <p>Maintain alongside <code>AGENTS.md</code> and the source files listed on each page.</p>
  347. </div>
  348. </footer>
  349. </body>
  350. </html>