process.c 19 KB

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