process.c 19 KB

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