process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /* Process management: create, exit, round-robin schedule, ps, kill. */
  2. #include "process.h"
  3. #include "elf.h"
  4. #include "idle.h"
  5. #include "io.h"
  6. #include "kmalloc.h"
  7. #include "mm.h"
  8. #include "paging.h"
  9. #include "serial.h"
  10. #include "sys_fs.h"
  11. #include "sys_gfx.h"
  12. #include "sys_net.h"
  13. #include "syscall.h"
  14. #include "timer.h"
  15. #include "tty.h"
  16. #include "vga.h"
  17. #include "wm_abi.h"
  18. struct process *procs; /* dynamically-sized process table */
  19. volatile unsigned int current_pid;
  20. static unsigned int proc_cap; /* current capacity of procs[] */
  21. static unsigned int next_pid_counter = 1; /* monotonic, wraps */
  22. static unsigned int fallback_stack_next; /* 0..PROC_FALLBACK_STACK_MAX */
  23. /* Top of the in-image bootstrap stack (see kernel.ld). */
  24. extern char __boot_stack_top[];
  25. #define INITIAL_PROC_CAP 16
  26. /* switch.S addresses the table entries with an immediate PCB_SIZE/PCB_CPU_TICKS
  27. * and cannot see this struct; keep the two in sync. */
  28. _Static_assert(sizeof(struct process) == 124,
  29. "struct process layout changed: update PCB_SIZE/PCB_CPU_TICKS in switch.S");
  30. /* ---- interrupt flag save/restore (for short critical sections) ---- */
  31. static inline unsigned int irq_save_disable(void)
  32. {
  33. unsigned int flags;
  34. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  35. return flags;
  36. }
  37. static inline void irq_restore(unsigned int flags)
  38. {
  39. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  40. }
  41. /* Fallback pool: a static array, so its address is not a fixed memory-map
  42. * entry (same reasoning as the in-image stack in kernel.ld). */
  43. static unsigned int fallback_stacks[PROC_FALLBACK_STACK_MAX][PROC_STACK_SIZE / 4]
  44. __attribute__((aligned(16)));
  45. /* Allocate a process stack and return its BASE address (page-aligned), or 0.
  46. * *owned is set to 1 if it came from the frame allocator (must be freed on
  47. * exit), 0 if it came from the fallback pool. */
  48. static unsigned int stack_alloc(int *owned)
  49. {
  50. unsigned int base = mm_alloc_page();
  51. if (base) {
  52. *owned = 1;
  53. return base;
  54. }
  55. if (fallback_stack_next < PROC_FALLBACK_STACK_MAX) {
  56. base = (unsigned int)&fallback_stacks[fallback_stack_next][0];
  57. fallback_stack_next++;
  58. *owned = 0;
  59. return base;
  60. }
  61. return 0;
  62. }
  63. /* Clear one control block. kmalloc memory is not zeroed and QEMU hides that
  64. * (guest RAM starts cleared), so a fresh slot would otherwise carry garbage in
  65. * cr3/pid/state; schedule() and process_map_mmio_all() trust those fields and
  66. * a garbage cr3 made the boot-time MMIO mapping fault on real hardware. */
  67. static void clear_proc(struct process *p)
  68. {
  69. unsigned char *b = (unsigned char *)p;
  70. unsigned int i;
  71. for (i = 0; i < sizeof(*p); i++)
  72. b[i] = 0;
  73. }
  74. static void name_copy(char *dst, const char *src)
  75. {
  76. unsigned int i;
  77. for (i = 0; i < PROC_NAME_LEN - 1 && src[i]; i++)
  78. dst[i] = src[i];
  79. dst[i] = '\0';
  80. }
  81. static void inherit_cwd(struct process *p)
  82. {
  83. unsigned int i;
  84. for (i = 0; i + 1 < sizeof(p->cwd) && procs[current_pid].cwd[i]; i++)
  85. p->cwd[i] = procs[current_pid].cwd[i];
  86. p->cwd[i] = '\0';
  87. }
  88. /* Double the process table. Called with interrupts disabled. */
  89. static int grow_proc_table(void)
  90. {
  91. unsigned int new_cap = proc_cap * 2;
  92. struct process *np =
  93. (struct process *)kmalloc(new_cap * sizeof(struct process));
  94. unsigned int i;
  95. if (!np)
  96. return 0;
  97. for (i = 0; i < proc_cap; i++)
  98. np[i] = procs[i];
  99. for (i = proc_cap; i < new_cap; i++)
  100. clear_proc(&np[i]);
  101. kfree(procs);
  102. procs = np;
  103. proc_cap = new_cap;
  104. return 1;
  105. }
  106. /* Find a free process slot (1..proc_cap-1), growing the table if needed.
  107. * Slot 0 is the kernel idle process and is never returned. Called with interrupts
  108. * disabled. Returns the slot index, or -1 on failure. */
  109. static int alloc_slot(void)
  110. {
  111. unsigned int slot;
  112. for (slot = 1; slot < proc_cap; slot++)
  113. if (procs[slot].state == PROC_UNUSED)
  114. return (int)slot;
  115. if (!grow_proc_table())
  116. return -1;
  117. for (slot = 1; slot < proc_cap; slot++)
  118. if (procs[slot].state == PROC_UNUSED)
  119. return (int)slot;
  120. return -1;
  121. }
  122. void process_init(void)
  123. {
  124. unsigned int i;
  125. procs = (struct process *)kmalloc(INITIAL_PROC_CAP * sizeof(struct process));
  126. if (!procs) {
  127. serial_puts(SERIAL_COM1, "process_init: out of memory\n");
  128. for (;;)
  129. __asm__ volatile ("cli; hlt");
  130. }
  131. proc_cap = INITIAL_PROC_CAP;
  132. for (i = 0; i < proc_cap; i++)
  133. clear_proc(&procs[i]);
  134. procs[0].state = PROC_RUNNING;
  135. procs[0].pid = 0;
  136. procs[0].esp = 0;
  137. procs[0].screen = (void *)0;
  138. procs[0].stack_base = 0; /* bootstrap stack, never freed */
  139. procs[0].cr3 = paging_current_cr3();
  140. procs[0].kstack_top = (unsigned int)__boot_stack_top;
  141. procs[0].cpu_ticks = 0;
  142. procs[0].start_ticks = 0; /* the WM "starts" at boot */
  143. name_copy(procs[0].name, "idle");
  144. current_pid = 0;
  145. }
  146. /*
  147. * Build initial stack frame for a new process so that popa + iret
  148. * "returns" to the entry point. For arg-taking entries the argument
  149. * string is copied onto the stack and a pointer to it passed as the
  150. * first C parameter.
  151. *
  152. * Stack layout (high to low):
  153. *
  154. * [copied arg string, if any, rounded up to 4-byte boundary]
  155. * arg pointer -- first C parameter (only if has_arg)
  156. * process_exit address -- return address seen by the entry function
  157. * EFLAGS (0x202) -- IF=1
  158. * CS (0x08)
  159. * EIP (entry) -- iret target
  160. * EAX ECX EDX EBX ESP_orig EBP ESI EDI (pusha frame)
  161. * <- saved ESP
  162. */
  163. static int setup_process(const char *name, unsigned int entry,
  164. int has_arg, const char *arg)
  165. {
  166. int slot, owned;
  167. unsigned int base, top, *sp, arg_ptr = 0, cr3;
  168. unsigned int flags;
  169. flags = irq_save_disable();
  170. slot = alloc_slot();
  171. if (slot < 0) {
  172. irq_restore(flags);
  173. return -1;
  174. }
  175. cr3 = paging_new_directory();
  176. if (!cr3) {
  177. irq_restore(flags);
  178. return -1;
  179. }
  180. base = stack_alloc(&owned);
  181. if (!base) {
  182. mm_free_page(cr3);
  183. irq_restore(flags);
  184. return -1;
  185. }
  186. top = base + PROC_STACK_SIZE;
  187. sp = (unsigned int *)top;
  188. if (has_arg && arg) {
  189. unsigned int len, aligned, i;
  190. char *dst;
  191. for (len = 0; arg[len]; len++)
  192. ;
  193. len++;
  194. aligned = (len + 3) & ~3u;
  195. sp = (unsigned int *)((unsigned int)sp - aligned);
  196. dst = (char *)sp;
  197. for (i = 0; i < len; i++)
  198. dst[i] = arg[i];
  199. for (; i < aligned; i++)
  200. dst[i] = '\0';
  201. arg_ptr = (unsigned int)sp;
  202. }
  203. if (has_arg)
  204. *--sp = arg_ptr;
  205. *--sp = (unsigned int)process_exit;
  206. *--sp = 0x202; /* EFLAGS: IF=1 */
  207. *--sp = 0x08; /* CS */
  208. *--sp = entry; /* EIP */
  209. *--sp = 0; /* EAX */
  210. *--sp = 0; /* ECX */
  211. *--sp = 0; /* EDX */
  212. *--sp = 0; /* EBX */
  213. *--sp = 0; /* ESP (ignored by popa) */
  214. *--sp = 0; /* EBP */
  215. *--sp = 0; /* ESI */
  216. *--sp = 0; /* EDI */
  217. procs[slot].esp = (unsigned int)sp;
  218. procs[slot].state = PROC_READY;
  219. procs[slot].pid = next_pid_counter++;
  220. procs[slot].stack_base = owned ? base : 0;
  221. procs[slot].cr3 = cr3;
  222. procs[slot].kstack_top = top;
  223. procs[slot].cpu_ticks = 0;
  224. procs[slot].start_ticks = get_ticks_ms();
  225. name_copy(procs[slot].name, name);
  226. /* Child inherits the parent's output screen/tty (command output lands
  227. in the terminal that spawned it). */
  228. procs[slot].screen = procs[current_pid].screen;
  229. procs[slot].tty = procs[current_pid].tty;
  230. inherit_cwd(&procs[slot]);
  231. irq_restore(flags);
  232. return (int)procs[slot].pid;
  233. }
  234. int process_create(const char *name, void (*entry)(void))
  235. {
  236. return setup_process(name, (unsigned int)entry, 0, (void *)0);
  237. }
  238. int process_create_arg(const char *name, void (*entry)(const char *),
  239. const char *arg)
  240. {
  241. return setup_process(name, (unsigned int)entry, 1, arg);
  242. }
  243. /* Create a process whose entry takes a raw pointer argument (no string copy). */
  244. int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
  245. {
  246. int slot, owned;
  247. unsigned int base, top, *sp, cr3;
  248. unsigned int flags;
  249. flags = irq_save_disable();
  250. slot = alloc_slot();
  251. if (slot < 0) {
  252. irq_restore(flags);
  253. return -1;
  254. }
  255. cr3 = paging_new_directory();
  256. if (!cr3) {
  257. irq_restore(flags);
  258. return -1;
  259. }
  260. base = stack_alloc(&owned);
  261. if (!base) {
  262. mm_free_page(cr3);
  263. irq_restore(flags);
  264. return -1;
  265. }
  266. top = base + PROC_STACK_SIZE;
  267. sp = (unsigned int *)top;
  268. *--sp = (unsigned int)arg; /* first C parameter */
  269. *--sp = (unsigned int)process_exit;/* return address */
  270. *--sp = 0x202; /* EFLAGS: IF=1 */
  271. *--sp = 0x08; /* CS */
  272. *--sp = (unsigned int)entry; /* EIP */
  273. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  274. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  275. procs[slot].esp = (unsigned int)sp;
  276. procs[slot].state = PROC_READY;
  277. procs[slot].pid = next_pid_counter++;
  278. procs[slot].stack_base = owned ? base : 0;
  279. procs[slot].cr3 = cr3;
  280. procs[slot].kstack_top = top;
  281. procs[slot].cpu_ticks = 0;
  282. procs[slot].start_ticks = get_ticks_ms();
  283. name_copy(procs[slot].name, name);
  284. procs[slot].screen = procs[current_pid].screen;
  285. procs[slot].tty = procs[current_pid].tty;
  286. inherit_cwd(&procs[slot]);
  287. irq_restore(flags);
  288. return (int)procs[slot].pid;
  289. }
  290. /* Load an ELF image into a fresh address space and run it as a ring-3 process. */
  291. int process_create_user_tty(const char *name, const void *image, unsigned int size,
  292. const char *arg, const char *cwd, unsigned int tty)
  293. {
  294. int slot, owned;
  295. unsigned int base, top, *sp, cr3, entry;
  296. unsigned int flags;
  297. int i;
  298. flags = irq_save_disable();
  299. slot = alloc_slot();
  300. if (slot < 0) {
  301. irq_restore(flags);
  302. return -1;
  303. }
  304. cr3 = paging_new_directory();
  305. if (!cr3) {
  306. irq_restore(flags);
  307. return -1;
  308. }
  309. entry = elf_load(cr3, image, size);
  310. if (!entry) {
  311. mm_free_page(cr3);
  312. irq_restore(flags);
  313. return -2; /* not a loadable ELF */
  314. }
  315. /* Map the user stack just below USER_STACK_TOP. Keep the top page's
  316. * physical address: the argument string is written through it (identity
  317. * mapped) after it is mapped in `cr3`. */
  318. {
  319. unsigned int top_phys = 0;
  320. unsigned int user_esp = USER_STACK_TOP;
  321. for (i = 0; i < USER_STACK_PAGES; i++) {
  322. unsigned int phys = mm_alloc_page();
  323. unsigned int va = USER_STACK_TOP - (unsigned int)(i + 1) * 0x1000;
  324. if (!phys ||
  325. !paging_map_page(cr3, va, phys,
  326. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  327. mm_free_page(cr3);
  328. irq_restore(flags);
  329. return -1;
  330. }
  331. if (i == 0)
  332. top_phys = phys;
  333. }
  334. /* One C string argument at the top of the user stack with a fake
  335. * return address below it, so `_start(const char *arg)` reads it
  336. * as its first parameter without needing a crt0. The virtual
  337. * addresses go into the frame; the writes go through the identity
  338. * map at the same offset from the physical page top. */
  339. {
  340. unsigned int va_top = USER_STACK_TOP;
  341. unsigned char *base_phys = (unsigned char *)(top_phys + 0x1000);
  342. unsigned char *str_phys;
  343. unsigned int *frame_phys;
  344. unsigned int len = 0;
  345. unsigned int va, k;
  346. if (arg)
  347. while (arg[len])
  348. len++;
  349. va = (va_top - (len + 1)) & ~15u;
  350. if (va - 8 < USER_STACK_TOP - USER_STACK_PAGES * 0x1000) {
  351. mm_free_page(cr3);
  352. irq_restore(flags);
  353. return -1; /* argument does not fit the stack */
  354. }
  355. str_phys = base_phys - (va_top - va);
  356. for (k = 0; k <= len; k++)
  357. str_phys[k] = arg ? arg[k] : '\0';
  358. frame_phys = (unsigned int *)(str_phys - 8);
  359. frame_phys[0] = 0; /* fake return address */
  360. frame_phys[1] = va; /* the argument pointer (user VA) */
  361. user_esp = va - 8;
  362. }
  363. base = stack_alloc(&owned);
  364. if (!base) {
  365. mm_free_page(cr3);
  366. irq_restore(flags);
  367. return -1;
  368. }
  369. top = base + PROC_STACK_SIZE;
  370. sp = (unsigned int *)top;
  371. *--sp = 0x23; /* SS: user data */
  372. *--sp = user_esp; /* ESP: user stack + arg frame */
  373. *--sp = 0x202; /* EFLAGS: IF=1 */
  374. *--sp = 0x1B; /* CS: user code */
  375. *--sp = entry; /* EIP */
  376. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  377. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  378. }
  379. procs[slot].esp = (unsigned int)sp;
  380. procs[slot].state = PROC_READY;
  381. procs[slot].pid = next_pid_counter++;
  382. procs[slot].stack_base = owned ? base : 0;
  383. procs[slot].cr3 = cr3;
  384. procs[slot].kstack_top = top;
  385. procs[slot].cpu_ticks = 0;
  386. procs[slot].start_ticks = get_ticks_ms();
  387. name_copy(procs[slot].name, name);
  388. procs[slot].screen = procs[current_pid].screen;
  389. procs[slot].tty = tty ? (int)tty : procs[current_pid].tty;
  390. {
  391. unsigned int k;
  392. for (k = 0; cwd && cwd[k] && k + 1 < sizeof(procs[slot].cwd); k++)
  393. procs[slot].cwd[k] = cwd[k];
  394. procs[slot].cwd[k] = '\0';
  395. }
  396. irq_restore(flags);
  397. return (int)procs[slot].pid;
  398. }
  399. int process_create_user(const char *name, const void *image, unsigned int size,
  400. const char *arg, const char *cwd)
  401. {
  402. return process_create_user_tty(name, image, size, arg, cwd, 0);
  403. }
  404. /* Set the output screen of the currently-running process (used by a terminal
  405. at startup to bind itself to its own screen). */
  406. void process_set_screen(struct text_screen *screen)
  407. {
  408. procs[current_pid].screen = screen;
  409. }
  410. /* Render target override for the running process (0 = the screen). Lives in
  411. * the PCB so a preempted layer draw can never leak into another process. */
  412. struct surface *process_render_target(void)
  413. {
  414. if (!procs || current_pid >= proc_cap)
  415. return 0;
  416. return procs[current_pid].target;
  417. }
  418. void process_set_render_target(struct surface *s)
  419. {
  420. if (!procs || current_pid >= proc_cap)
  421. return;
  422. procs[current_pid].target = s;
  423. }
  424. void process_map_mmio_all(unsigned int phys_base, unsigned int size)
  425. {
  426. unsigned int i, kernel_cr3 = paging_current_cr3();
  427. paging_map_mmio(phys_base, size);
  428. for (i = 0; i < proc_cap; i++) {
  429. if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
  430. continue;
  431. if (procs[i].cr3 && procs[i].cr3 != kernel_cr3)
  432. paging_map_mmio_cr3(procs[i].cr3, phys_base, size);
  433. }
  434. }
  435. void process_exit(void)
  436. {
  437. procs[current_pid].state = PROC_DEAD;
  438. __asm__ volatile ("sti"); /* re-enable IRQs so the PIT can switch us out */
  439. for (;;)
  440. __asm__ volatile ("hlt");
  441. }
  442. /*
  443. * Round-robin scheduler. Called from the PIT IRQ stub with interrupts
  444. * disabled. Picks the next runnable process and updates current_pid.
  445. * The caller (switch.S) takes care of actually switching ESP.
  446. */
  447. void schedule(void)
  448. {
  449. unsigned int i, next;
  450. /* Reap dead processes, freeing their stacks. Skip the current slot: we
  451. * may be running on its stack right now (it just called process_exit),
  452. * so it is reaped on a later schedule() from another process's context. */
  453. for (i = 0; i < proc_cap; i++) {
  454. if (procs[i].state == PROC_DEAD && i != current_pid) {
  455. sys_net_reap(procs[i].pid); /* release a held NIC */
  456. sys_gfx_reap(procs[i].pid); /* flag a gfx session dead */
  457. tty_reap(procs[i].pid); /* drop its terminal queue */
  458. if (procs[i].stack_base)
  459. mm_free_page(procs[i].stack_base);
  460. if (procs[i].cr3 &&
  461. procs[i].cr3 != paging_current_cr3()) {
  462. /* User code/stack pages belong to the process.
  463. * The gfx canvas mapping is only a window into
  464. * pages owned by the session: release the PTEs
  465. * and page tables, not the pages. */
  466. paging_release_user_range(procs[i].cr3,
  467. USER_BASE, USER_STACK_TOP - USER_BASE, 1);
  468. paging_release_user_range(procs[i].cr3,
  469. USER_GFX_BASE, USER_GFX_RANGE, 0);
  470. /* WM surface mappings: page tables only; the
  471. * shadow/terminal/canvas pages belong to the
  472. * window server. */
  473. paging_release_user_range(procs[i].cr3,
  474. WM_SHADOW_VA, 0x00800000, 0);
  475. paging_release_user_range(procs[i].cr3,
  476. WM_CONTENT_VA_BASE,
  477. WM_CONTENT_SLOT * WM_MAX_WINDOWS, 0);
  478. mm_free_page(procs[i].cr3);
  479. }
  480. procs[i].stack_base = 0;
  481. procs[i].cr3 = 0;
  482. procs[i].state = PROC_UNUSED;
  483. }
  484. }
  485. /* A file syscall or a shadow-buffer flush is in progress: stay on the
  486. * current process. The FAT driver has static state, and the flush walks
  487. * and trims a shared damage list (and must not be abandoned halfway
  488. * through by a context switch). Interrupts stay enabled, so IRQs are
  489. * still serviced. */
  490. if (sys_fs_busy || vga_present_busy()) {
  491. if (procs[current_pid].state == PROC_READY)
  492. procs[current_pid].state = PROC_RUNNING;
  493. return;
  494. }
  495. /* Wake sleeping processes whose deadline has passed. */
  496. for (i = 0; i < proc_cap; i++) {
  497. if (procs[i].state == PROC_SLEEPING &&
  498. (int)(procs[i].wake_ms - get_ticks_ms()) <= 0)
  499. procs[i].state = PROC_READY;
  500. }
  501. /* Mark current as READY (it was RUNNING). */
  502. if (procs[current_pid].state == PROC_RUNNING)
  503. procs[current_pid].state = PROC_READY;
  504. /* Find next runnable slot. */
  505. next = current_pid;
  506. for (i = 0; i < proc_cap; i++) {
  507. next = (next + 1) % proc_cap;
  508. if (procs[next].state == PROC_READY) {
  509. procs[next].state = PROC_RUNNING;
  510. current_pid = next;
  511. return;
  512. }
  513. }
  514. /* Nothing else runnable -- stay on current if still alive. */
  515. if (procs[current_pid].state == PROC_READY) {
  516. procs[current_pid].state = PROC_RUNNING;
  517. return;
  518. }
  519. /* Everything dead: fall back to slot 0 (kernel idle). */
  520. procs[0].state = PROC_RUNNING;
  521. current_pid = 0;
  522. }
  523. /* ---- user-facing helpers ---- */
  524. static const char *state_str(unsigned int s)
  525. {
  526. switch (s) {
  527. case PROC_RUNNING: return "run ";
  528. case PROC_READY: return "ready";
  529. default: return "? ";
  530. }
  531. }
  532. static void put_dec(unsigned int n)
  533. {
  534. char buf[12];
  535. unsigned int i = sizeof(buf) - 1;
  536. buf[i] = '\0';
  537. if (n == 0) {
  538. io_putchar('0');
  539. return;
  540. }
  541. while (n && i > 0) {
  542. buf[--i] = (char)('0' + (n % 10));
  543. n /= 10;
  544. }
  545. io_putstr(buf + i);
  546. }
  547. void process_ps(void)
  548. {
  549. unsigned int i;
  550. io_putstr("PID STATE NAME\n");
  551. for (i = 0; i < proc_cap; i++) {
  552. if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
  553. continue;
  554. io_putchar(' ');
  555. put_dec(procs[i].pid);
  556. io_putstr(" ");
  557. io_putstr(state_str(procs[i].state));
  558. io_putstr(" ");
  559. io_putstr(procs[i].name);
  560. io_putchar('\n');
  561. }
  562. }
  563. /* Count the 4 KB user pages mapped between USER_BASE and USER_STACK_TOP.
  564. * A read-only page-directory walk (paging keeps everything identity-mapped,
  565. * so the tables are directly addressable). Used by `top` for the memory
  566. * hint; ring-3 processes currently leak these pages on reap (known issue),
  567. * but ex-processes are gone from the table by then anyway. */
  568. static unsigned int count_user_pages(unsigned int cr3)
  569. {
  570. const unsigned int *pd;
  571. unsigned int i, pages = 0;
  572. if (!cr3)
  573. return 0;
  574. pd = (const unsigned int *)cr3;
  575. for (i = USER_BASE >> 22; i <= (USER_STACK_TOP - 1) >> 22; i++) {
  576. unsigned int pde = pd[i];
  577. const unsigned int *pt;
  578. if (!(pde & PTE_PRESENT) || (pde & 0x80)) /* 0x80: 4 MB page */
  579. continue;
  580. pt = (const unsigned int *)(pde & 0xFFFFF000u);
  581. {
  582. unsigned int j;
  583. for (j = 0; j < 1024; j++)
  584. if (pt[j] & PTE_PRESENT)
  585. pages++;
  586. }
  587. }
  588. return pages;
  589. }
  590. /* Snapshot of the index-th live process, for the `top` syscall. Indexes over
  591. * live processes in slot order, so callers iterate 0, 1, ... until 0. */
  592. int process_procinfo(unsigned int index, struct sys_procinfo *out)
  593. {
  594. unsigned int i, seen = 0, life;
  595. if (!out)
  596. return 0;
  597. for (i = 0; i < proc_cap; i++) {
  598. if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
  599. continue;
  600. if (seen++ != index)
  601. continue;
  602. life = get_ticks_ms() - procs[i].start_ticks;
  603. out->pid = procs[i].pid;
  604. out->state = procs[i].state;
  605. out->cpu_ticks = procs[i].cpu_ticks;
  606. out->life_ticks = life ? life : 1;
  607. /* Kernel stack page + page directory + user pages (the WM shares
  608. * the kernel directory; counting one is close enough). */
  609. out->pages = 2 + count_user_pages(procs[i].cr3);
  610. name_copy(out->name, procs[i].name);
  611. return 1;
  612. }
  613. return 0;
  614. }
  615. int process_kill(unsigned int pid)
  616. {
  617. unsigned int i;
  618. if (pid == 0)
  619. return -1; /* cannot kill the kernel idle process */
  620. for (i = 1; i < proc_cap; i++) {
  621. if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
  622. procs[i].pid == pid) {
  623. procs[i].state = PROC_DEAD;
  624. return 0;
  625. }
  626. }
  627. return -1;
  628. }
  629. /* Return 1 if a process with `pid` is still alive (READY/RUNNING/SLEEPING). */
  630. int process_is_alive(unsigned int pid)
  631. {
  632. unsigned int i;
  633. for (i = 0; i < proc_cap; i++)
  634. if (procs[i].pid == pid &&
  635. (procs[i].state == PROC_READY ||
  636. procs[i].state == PROC_RUNNING ||
  637. procs[i].state == PROC_SLEEPING))
  638. return 1;
  639. return 0;
  640. }
  641. int process_is_sleeping(unsigned int pid)
  642. {
  643. unsigned int i;
  644. for (i = 0; i < proc_cap; i++)
  645. if (procs[i].pid == pid)
  646. return procs[i].state == PROC_SLEEPING;
  647. return 0;
  648. }
  649. void process_set_tty(unsigned int pid, int tty)
  650. {
  651. unsigned int i;
  652. for (i = 0; i < proc_cap; i++)
  653. if (procs[i].pid == pid && procs[i].state != PROC_UNUSED) {
  654. procs[i].tty = tty;
  655. return;
  656. }
  657. }
  658. /* Block the current process for `ms` milliseconds. Marks it sleeping and
  659. * yields to the scheduler; it is woken when its deadline passes. */
  660. void process_sleep_ms(unsigned int ms)
  661. {
  662. procs[current_pid].wake_ms = get_ticks_ms() + ms;
  663. procs[current_pid].state = PROC_SLEEPING;
  664. __asm__ volatile ("sti"); /* re-enable IRQs so the PIT can switch us out */
  665. while (procs[current_pid].state == PROC_SLEEPING)
  666. cpu_idle();
  667. }
  668. /* Wait (yield) until the process with `pid` has exited. */
  669. void process_wait(unsigned int pid)
  670. {
  671. while (process_is_alive(pid))
  672. process_sleep_ms(10);
  673. }