process.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* Process management: create, exit, reap, ps, kill.
  2. *
  3. * The scheduler itself lives in sched.c. This file owns the stable PCB
  4. * objects and the lifecycle around them: allocation, initial stack frames,
  5. * address-space cleanup, and the user-facing helpers.
  6. */
  7. #include "process.h"
  8. #include "elf.h"
  9. #include "idle.h"
  10. #include "io.h"
  11. #include "kmalloc.h"
  12. #include "mm.h"
  13. #include "paging.h"
  14. #include "serial.h"
  15. #include "smp.h"
  16. #include "sys_fs.h"
  17. #include "sys_mem.h"
  18. #include "sys_net.h"
  19. #include "syscall.h"
  20. #include "timer.h"
  21. #include "tty.h"
  22. #include "vga.h"
  23. #include "wm_abi.h"
  24. struct process *procs[PROC_MAX];
  25. static struct process idle_procs[MAX_CPUS];
  26. static unsigned int next_pid_counter = 1; /* monotonic, wraps */
  27. static unsigned int fallback_stack_next; /* 0..PROC_FALLBACK_STACK_MAX */
  28. static struct process *zombie_head, *zombie_tail;
  29. static struct wait_queue exit_wq;
  30. /* Top of the in-image bootstrap stack (see kernel.ld). */
  31. extern char __boot_stack_top[];
  32. /* ---- interrupt flag save/restore (for short critical sections) ---- */
  33. static inline unsigned int irq_save_disable(void)
  34. {
  35. unsigned int flags;
  36. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  37. return flags;
  38. }
  39. static inline void irq_restore(unsigned int flags)
  40. {
  41. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  42. }
  43. /* Fallback pool: a static array, so its address is not a fixed memory-map
  44. * entry (same reasoning as the in-image stack in kernel.ld). */
  45. static unsigned int fallback_stacks[PROC_FALLBACK_STACK_MAX][PROC_STACK_SIZE / 4]
  46. __attribute__((aligned(16)));
  47. /* Allocate a process stack and return its BASE address (page-aligned), or 0.
  48. * *owned is set to 1 if it came from the frame allocator (must be freed on
  49. * exit), 0 if it came from the fallback pool. */
  50. static unsigned int stack_alloc(int *owned)
  51. {
  52. unsigned int base = mm_alloc_page();
  53. if (base) {
  54. *owned = 1;
  55. return base;
  56. }
  57. if (fallback_stack_next < PROC_FALLBACK_STACK_MAX) {
  58. base = (unsigned int)&fallback_stacks[fallback_stack_next][0];
  59. fallback_stack_next++;
  60. *owned = 0;
  61. return base;
  62. }
  63. return 0;
  64. }
  65. /* Clear one control block. kmalloc memory is not zeroed and QEMU hides that
  66. * (guest RAM starts cleared), so a fresh PCB would otherwise carry garbage in
  67. * cr3/pid/state; the scheduler and MMIO mapping code trust those fields. */
  68. static void clear_proc(struct process *p)
  69. {
  70. unsigned char *b = (unsigned char *)p;
  71. unsigned int i;
  72. for (i = 0; i < sizeof(*p); i++)
  73. b[i] = 0;
  74. }
  75. static void name_copy(char *dst, const char *src)
  76. {
  77. unsigned int i;
  78. for (i = 0; i < PROC_NAME_LEN - 1 && src[i]; i++)
  79. dst[i] = src[i];
  80. dst[i] = '\0';
  81. }
  82. static void cwd_copy(struct process *p, const char *cwd)
  83. {
  84. unsigned int i;
  85. for (i = 0; cwd && cwd[i] && i + 1 < sizeof(p->cwd); i++)
  86. p->cwd[i] = cwd[i];
  87. p->cwd[i] = '\0';
  88. }
  89. /* Find a free slot in the stable pointer table. Called with interrupts
  90. * disabled. Returns the slot index, or -1 when the table is full. */
  91. static int alloc_slot(void)
  92. {
  93. unsigned int slot;
  94. for (slot = 0; slot < PROC_MAX; slot++)
  95. if (!procs[slot])
  96. return (int)slot;
  97. return -1;
  98. }
  99. /* Allocate and clear a PCB, then reserve a table slot (but do not publish it
  100. * yet). Called with interrupts disabled. */
  101. static struct process *proc_alloc(int *slot_out)
  102. {
  103. struct process *p = (struct process *)kmalloc(sizeof(*p));
  104. int slot;
  105. if (!p)
  106. return 0;
  107. clear_proc(p);
  108. slot = alloc_slot();
  109. if (slot < 0) {
  110. kfree(p);
  111. return 0;
  112. }
  113. *slot_out = slot;
  114. return p;
  115. }
  116. /* Fill in the common fields and publish the process in its slot. The caller
  117. * has already built the initial kernel stack frame. Called with interrupts
  118. * disabled and with parent = current_proc() (possibly idle). */
  119. static void proc_commit(struct process *p, int slot, const char *name,
  120. unsigned int esp, int owned, unsigned int stack_base,
  121. unsigned int cr3, unsigned int kstack_top,
  122. void (*kentry)(void), int kernel)
  123. {
  124. struct process *parent = current_proc();
  125. p->esp = esp;
  126. p->pid = next_pid_counter++;
  127. p->slot = (unsigned int)slot;
  128. p->stack_base = owned ? stack_base : 0;
  129. p->cr3 = cr3;
  130. p->kstack_top = kstack_top;
  131. p->cpu_ticks = 0;
  132. p->start_ticks = get_ticks_ms();
  133. p->on_cpu = PROC_NO_CPU;
  134. p->last_cpu = PROC_NO_CPU;
  135. /* Kernel threads touch global kernel state; keep them on the BSP.
  136. * Ring-3 processes may use every CPU. */
  137. p->affinity = kernel ? 1u : 0xFFFFFFFFu;
  138. p->kentry = kentry;
  139. p->kernel_thread = (unsigned int)kernel;
  140. p->weight = 0;
  141. p->stride = 0;
  142. p->vruntime = 0;
  143. p->slice_start = 0;
  144. p->tty = parent ? parent->tty : 0;
  145. if (parent)
  146. cwd_copy(p, parent->cwd);
  147. else
  148. p->cwd[0] = '\0';
  149. name_copy(p->name, name);
  150. procs[slot] = p;
  151. /* The caller publishes the process as runnable only after applying any
  152. * per-process overrides (cwd/tty): an AP can pick it as soon as it is
  153. * on the run queue. */
  154. }
  155. void process_init(void)
  156. {
  157. unsigned int i;
  158. sched_init();
  159. zombie_head = zombie_tail = 0;
  160. wait_queue_init(&exit_wq);
  161. for (i = 0; i < PROC_MAX; i++)
  162. procs[i] = 0;
  163. clear_proc(&idle_procs[0]);
  164. idle_procs[0].state = PROC_RUNNING;
  165. idle_procs[0].pid = 0;
  166. idle_procs[0].slot = PROC_MAX;
  167. idle_procs[0].esp = 0;
  168. idle_procs[0].stack_base = 0; /* bootstrap stack, never freed */
  169. idle_procs[0].cr3 = paging_current_cr3();
  170. idle_procs[0].kstack_top = (unsigned int)__boot_stack_top;
  171. idle_procs[0].cpu_ticks = 0;
  172. idle_procs[0].start_ticks = 0;
  173. idle_procs[0].on_cpu = 0;
  174. idle_procs[0].last_cpu = 0;
  175. idle_procs[0].affinity = 1;
  176. idle_procs[0].weight = 0;
  177. idle_procs[0].stride = 0;
  178. idle_procs[0].vruntime = 0;
  179. idle_procs[0].on_runq = 0;
  180. name_copy(idle_procs[0].name, "idle");
  181. cpus[0].idle = &idle_procs[0];
  182. cpus[0].current = &idle_procs[0];
  183. }
  184. /* Prepare an AP idle process and its kernel stack. Called by the BSP before
  185. * SIPI; the AP itself publishes cpus[cpu].current in smp_ap_main(). */
  186. unsigned int process_init_ap(unsigned int cpu)
  187. {
  188. struct process *p;
  189. unsigned int base;
  190. if (cpu == 0 || cpu >= MAX_CPUS)
  191. return 0;
  192. base = mm_alloc_page();
  193. if (!base)
  194. return 0;
  195. p = &idle_procs[cpu];
  196. clear_proc(p);
  197. p->state = PROC_RUNNING;
  198. p->pid = 0;
  199. p->slot = PROC_MAX;
  200. p->esp = 0;
  201. p->cr3 = paging_kernel_cr3();
  202. p->kstack_top = base + PROC_STACK_SIZE;
  203. p->on_cpu = cpu;
  204. p->last_cpu = cpu;
  205. p->affinity = 1u << cpu;
  206. p->start_ticks = 0;
  207. name_copy(p->name, "idle");
  208. cpus[cpu].idle = p;
  209. return p->kstack_top;
  210. }
  211. /*
  212. * Build initial stack frame for a new process so that popa + iret
  213. * "returns" to the entry point. For arg-taking entries the argument
  214. * string is copied onto the stack and a pointer to it passed as the
  215. * first C parameter.
  216. *
  217. * Stack layout (high to low):
  218. *
  219. * [copied arg string, if any, rounded up to 4-byte boundary]
  220. * arg pointer -- first C parameter (only if has_arg)
  221. * process_exit address -- return address seen by the entry function
  222. * EFLAGS (0x202) -- IF=1
  223. * CS (0x08)
  224. * EIP (entry) -- iret target
  225. * EAX ECX EDX EBX ESP_orig EBP ESI EDI (pusha frame)
  226. * <- saved ESP
  227. */
  228. /* Kernel-thread entry wrappers: take the BKL for the thread's lifetime and
  229. * hand the real entry point the argument the stack frame supplied. */
  230. static void process_kernel_start0(void)
  231. {
  232. struct process *p = current_proc();
  233. void (*fn)(void) = (void (*)(void))p->kentry;
  234. bkl_lock();
  235. if (fn)
  236. fn();
  237. bkl_unlock();
  238. process_exit();
  239. }
  240. static void process_kernel_start_arg(const char *arg)
  241. {
  242. struct process *p = current_proc();
  243. void (*fn)(const char *) = (void (*)(const char *))p->kentry;
  244. bkl_lock();
  245. if (fn)
  246. fn(arg);
  247. bkl_unlock();
  248. process_exit();
  249. }
  250. static int setup_process(const char *name, unsigned int entry,
  251. int has_arg, const char *arg,
  252. void (*kentry)(void), int kernel)
  253. {
  254. int slot, owned, pid;
  255. unsigned int base, top, *sp, arg_ptr = 0, cr3;
  256. unsigned int flags;
  257. struct process *p;
  258. flags = irq_save_disable();
  259. p = proc_alloc(&slot);
  260. if (!p) {
  261. irq_restore(flags);
  262. return -1;
  263. }
  264. cr3 = paging_new_directory();
  265. if (!cr3) {
  266. kfree(p);
  267. irq_restore(flags);
  268. return -1;
  269. }
  270. base = stack_alloc(&owned);
  271. if (!base) {
  272. mm_free_page(cr3);
  273. kfree(p);
  274. irq_restore(flags);
  275. return -1;
  276. }
  277. top = base + PROC_STACK_SIZE;
  278. sp = (unsigned int *)top;
  279. if (has_arg && arg) {
  280. unsigned int len, aligned, i;
  281. char *dst;
  282. for (len = 0; arg[len]; len++)
  283. ;
  284. len++;
  285. aligned = (len + 3) & ~3u;
  286. sp = (unsigned int *)((unsigned int)sp - aligned);
  287. dst = (char *)sp;
  288. for (i = 0; i < len; i++)
  289. dst[i] = arg[i];
  290. for (; i < aligned; i++)
  291. dst[i] = '\0';
  292. arg_ptr = (unsigned int)sp;
  293. }
  294. if (has_arg)
  295. *--sp = arg_ptr;
  296. *--sp = (unsigned int)process_exit;
  297. *--sp = 0x202; /* EFLAGS: IF=1 */
  298. *--sp = 0x08; /* CS */
  299. *--sp = entry; /* EIP */
  300. *--sp = 0; /* EAX */
  301. *--sp = 0; /* ECX */
  302. *--sp = 0; /* EDX */
  303. *--sp = 0; /* EBX */
  304. *--sp = 0; /* ESP (ignored by popa) */
  305. *--sp = 0; /* EBP */
  306. *--sp = 0; /* ESI */
  307. *--sp = 0; /* EDI */
  308. proc_commit(p, slot, name, (unsigned int)sp, owned, base, cr3, top,
  309. kentry, kernel);
  310. sched_add_ready(p);
  311. pid = (int)p->pid;
  312. irq_restore(flags);
  313. return pid;
  314. }
  315. int process_create(const char *name, void (*entry)(void))
  316. {
  317. return setup_process(name, (unsigned int)process_kernel_start0, 0,
  318. (void *)0, entry, 1);
  319. }
  320. int process_create_arg(const char *name, void (*entry)(const char *),
  321. const char *arg)
  322. {
  323. return setup_process(name, (unsigned int)process_kernel_start_arg, 1,
  324. arg, (void (*)(void))entry, 1);
  325. }
  326. /* Create a process whose entry takes a raw pointer argument (no string copy). */
  327. int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
  328. {
  329. int slot, owned, pid;
  330. unsigned int base, top, *sp, cr3;
  331. unsigned int flags;
  332. struct process *p;
  333. flags = irq_save_disable();
  334. p = proc_alloc(&slot);
  335. if (!p) {
  336. irq_restore(flags);
  337. return -1;
  338. }
  339. cr3 = paging_new_directory();
  340. if (!cr3) {
  341. kfree(p);
  342. irq_restore(flags);
  343. return -1;
  344. }
  345. base = stack_alloc(&owned);
  346. if (!base) {
  347. mm_free_page(cr3);
  348. kfree(p);
  349. irq_restore(flags);
  350. return -1;
  351. }
  352. top = base + PROC_STACK_SIZE;
  353. sp = (unsigned int *)top;
  354. *--sp = (unsigned int)arg; /* first C parameter */
  355. *--sp = (unsigned int)process_exit;/* return address */
  356. *--sp = 0x202; /* EFLAGS: IF=1 */
  357. *--sp = 0x08; /* CS */
  358. *--sp = (unsigned int)process_kernel_start_arg; /* EIP */
  359. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  360. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  361. proc_commit(p, slot, name, (unsigned int)sp, owned, base, cr3, top,
  362. (void (*)(void))entry, 1);
  363. sched_add_ready(p);
  364. pid = (int)p->pid;
  365. irq_restore(flags);
  366. return pid;
  367. }
  368. /* Load an ELF image into a fresh address space and run it as a ring-3 process. */
  369. int process_create_user_tty(const char *name, const void *image, unsigned int size,
  370. const char *arg, const char *cwd, unsigned int tty)
  371. {
  372. int slot, owned, pid;
  373. unsigned int base, top, *sp, cr3, entry;
  374. unsigned int flags;
  375. struct process *p;
  376. int i;
  377. flags = irq_save_disable();
  378. p = proc_alloc(&slot);
  379. if (!p) {
  380. irq_restore(flags);
  381. return -1;
  382. }
  383. cr3 = paging_new_directory();
  384. if (!cr3) {
  385. kfree(p);
  386. irq_restore(flags);
  387. return -1;
  388. }
  389. entry = elf_load(cr3, image, size);
  390. if (!entry) {
  391. mm_free_page(cr3);
  392. kfree(p);
  393. irq_restore(flags);
  394. return -2; /* not a loadable ELF */
  395. }
  396. /* Map the user stack just below USER_STACK_TOP. Keep the top page's
  397. * physical address: the argument string is written through a kmap of
  398. * it (the frame may be highmem) after it is mapped in `cr3`. */
  399. {
  400. unsigned int top_phys = 0;
  401. unsigned int user_esp = USER_STACK_TOP;
  402. for (i = 0; i < USER_STACK_PAGES; i++) {
  403. unsigned int phys = mm_alloc_user_page();
  404. unsigned int va = USER_STACK_TOP - (unsigned int)(i + 1) * 0x1000;
  405. if (!phys ||
  406. !paging_map_page(cr3, va, phys,
  407. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  408. mm_free_page(cr3);
  409. kfree(p);
  410. irq_restore(flags);
  411. return -1;
  412. }
  413. if (i == 0)
  414. top_phys = phys;
  415. }
  416. /* One C string argument at the top of the user stack with a fake
  417. * return address below it, so `_start(const char *arg)` reads it
  418. * as its first parameter without needing a crt0. The virtual
  419. * addresses go into the frame; the writes go through a kmap of the
  420. * top stack page at the same offset from its end. */
  421. {
  422. unsigned int va_top = USER_STACK_TOP;
  423. unsigned char *top_map = kmap(top_phys);
  424. unsigned char *base_phys = top_map + 0x1000;
  425. unsigned char *str_phys;
  426. unsigned int *frame_phys;
  427. unsigned int len = 0;
  428. unsigned int va, k;
  429. if (arg)
  430. while (arg[len])
  431. len++;
  432. va = (va_top - (len + 1)) & ~15u;
  433. /* The string and frame are written through one kmap'd
  434. * page, so they must fit the top stack page. */
  435. if (va - 8 < USER_STACK_TOP - 0x1000) {
  436. kunmap(top_map);
  437. mm_free_page(cr3);
  438. kfree(p);
  439. irq_restore(flags);
  440. return -1; /* argument does not fit the stack */
  441. }
  442. str_phys = base_phys - (va_top - va);
  443. for (k = 0; k <= len; k++)
  444. str_phys[k] = arg ? arg[k] : '\0';
  445. frame_phys = (unsigned int *)(str_phys - 8);
  446. frame_phys[0] = 0; /* fake return address */
  447. frame_phys[1] = va; /* the argument pointer (user VA) */
  448. user_esp = va - 8;
  449. kunmap(top_map);
  450. }
  451. base = stack_alloc(&owned);
  452. if (!base) {
  453. mm_free_page(cr3);
  454. kfree(p);
  455. irq_restore(flags);
  456. return -1;
  457. }
  458. top = base + PROC_STACK_SIZE;
  459. sp = (unsigned int *)top;
  460. *--sp = 0x23; /* SS: user data */
  461. *--sp = user_esp; /* ESP: user stack + arg frame */
  462. *--sp = 0x202; /* EFLAGS: IF=1 */
  463. *--sp = 0x1B; /* CS: user code */
  464. *--sp = entry; /* EIP */
  465. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  466. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  467. }
  468. proc_commit(p, slot, name, (unsigned int)sp, owned, base, cr3, top,
  469. 0, 0);
  470. if (cwd)
  471. cwd_copy(p, cwd);
  472. if (tty)
  473. p->tty = (int)tty;
  474. sched_add_ready(p);
  475. pid = (int)p->pid;
  476. irq_restore(flags);
  477. return pid;
  478. }
  479. int process_create_user(const char *name, const void *image, unsigned int size,
  480. const char *arg, const char *cwd)
  481. {
  482. return process_create_user_tty(name, image, size, arg, cwd, 0);
  483. }
  484. /* Set the output screen of the currently-running process (used by a terminal
  485. at startup to bind itself to its own screen). */
  486. /* Render target override for the running process (0 = the screen). Lives in
  487. * the PCB so a preempted layer draw can never leak into another process. */
  488. struct surface *process_render_target(void)
  489. {
  490. struct process *p = current_proc();
  491. return p ? p->target : 0;
  492. }
  493. void process_set_render_target(struct surface *s)
  494. {
  495. struct process *p = current_proc();
  496. if (p)
  497. p->target = s;
  498. }
  499. void process_map_mmio_all(unsigned int phys_base, unsigned int size)
  500. {
  501. unsigned int i, flags;
  502. /* Map the kernel directory (the one new process directories clone) and
  503. * every live process copy. paging_current_cr3() is the *current* process
  504. * directory, which may be one of these; mapping it twice is harmless.
  505. * The reaper frees PCBs, so hold the process lock while iterating. */
  506. flags = sched_lock_acquire();
  507. paging_map_mmio(phys_base, size);
  508. for (i = 0; i < PROC_MAX; i++) {
  509. struct process *p = procs[i];
  510. if (!p || p->state == PROC_DEAD)
  511. continue;
  512. if (p->cr3)
  513. paging_map_mmio_cr3(p->cr3, phys_base, size);
  514. }
  515. sched_lock_release(flags);
  516. }
  517. /* Unpublish a dead process and hand it to the kworker. Takes sched_lock
  518. * itself; callers must have already switched away from its kernel stack (or
  519. * never run it), so the reaper cannot free memory still in use. */
  520. void process_zombie_add(struct process *p)
  521. {
  522. unsigned int flags;
  523. if (!p)
  524. return;
  525. flags = sched_lock_acquire();
  526. if (p->zombie_queued) {
  527. sched_lock_release(flags);
  528. return;
  529. }
  530. p->zombie_queued = 1;
  531. p->on_cpu = PROC_NO_CPU;
  532. if (p->slot < PROC_MAX)
  533. procs[p->slot] = 0;
  534. p->zombie_next = 0;
  535. if (zombie_tail)
  536. zombie_tail->zombie_next = p;
  537. else
  538. zombie_head = p;
  539. zombie_tail = p;
  540. sched_lock_release(flags);
  541. }
  542. unsigned int process_zombie_count_locked(void)
  543. {
  544. unsigned int n = 0;
  545. struct process *p;
  546. for (p = zombie_head; p; p = p->zombie_next)
  547. n++;
  548. return n;
  549. }
  550. unsigned int process_zombie_count(void)
  551. {
  552. unsigned int flags, n;
  553. flags = sched_lock_acquire();
  554. n = process_zombie_count_locked();
  555. sched_lock_release(flags);
  556. return n;
  557. }
  558. /* Free switched-out dead processes. Runs in the kworker (or the idle
  559. * fallback), not in the PIT handler. Preemption is disabled while the
  560. * resources are torn down: tty_reap()/the page-table walk are
  561. * not re-entrant, matching the old IRQ-context reaper. */
  562. void process_reap_dead(void)
  563. {
  564. unsigned int cur_cr3 = paging_current_cr3();
  565. unsigned int flags;
  566. struct process *p, *list;
  567. preempt_disable();
  568. flags = sched_lock_acquire();
  569. list = zombie_head;
  570. zombie_head = zombie_tail = 0;
  571. sched_lock_release(flags);
  572. for (p = list; p; ) {
  573. struct process *next = p->zombie_next;
  574. sys_net_reap(p->pid); /* release a held NIC */
  575. tty_reap(p->pid); /* drop its terminal queue */
  576. if (p->stack_base)
  577. mm_free_page(p->stack_base);
  578. if (p->cr3 && p->cr3 != cur_cr3) {
  579. /* User code/stack pages belong to the process. The gfx
  580. * canvas mapping is only a window into pages owned by
  581. * the session: release the PTEs and page tables, not
  582. * the pages. */
  583. paging_release_user_range(p->cr3,
  584. USER_BASE, USER_STACK_TOP - USER_BASE, 1);
  585. paging_release_user_range(p->cr3,
  586. USER_WIN_BASE, USER_WIN_RANGE, 0);
  587. sys_mem_release(p);
  588. /* WM surface mappings: page tables only; the shadow/
  589. * terminal/canvas pages belong to the window server. */
  590. paging_release_user_range(p->cr3,
  591. WM_SHADOW_VA, 0x00800000, 0);
  592. paging_release_user_range(p->cr3,
  593. WM_CONTENT_VA_BASE,
  594. WM_CONTENT_SLOT * WM_MAX_WINDOWS, 0);
  595. mm_free_page(p->cr3);
  596. }
  597. p->stack_base = 0;
  598. p->cr3 = 0;
  599. kfree(p);
  600. p = next;
  601. }
  602. preempt_enable();
  603. }
  604. void process_exit(void)
  605. {
  606. struct process *p = current_proc();
  607. if (p)
  608. p->state = PROC_DEAD;
  609. wait_wake_all(&exit_wq);
  610. sched_yield(); /* switch away immediately via int 0x81 */
  611. for (;;)
  612. __asm__ volatile ("hlt"); /* the scheduler never resumes us */
  613. }
  614. /* ---- user-facing helpers ---- */
  615. static const char *state_str(unsigned int s)
  616. {
  617. switch (s) {
  618. case PROC_RUNNING: return "run ";
  619. case PROC_READY: return "ready";
  620. case PROC_SLEEPING: return "sleep";
  621. case PROC_BLOCKED: return "block";
  622. default: return "? ";
  623. }
  624. }
  625. static void put_dec(unsigned int n)
  626. {
  627. char buf[12];
  628. unsigned int i = sizeof(buf) - 1;
  629. buf[i] = '\0';
  630. if (n == 0) {
  631. io_putchar('0');
  632. return;
  633. }
  634. while (n && i > 0) {
  635. buf[--i] = (char)('0' + (n % 10));
  636. n /= 10;
  637. }
  638. io_putstr(buf + i);
  639. }
  640. void process_ps(void)
  641. {
  642. unsigned int i, j;
  643. io_putstr("PID STATE W CPU NAME\n");
  644. for (i = 0; i < PROC_MAX; i++) {
  645. struct process *p;
  646. unsigned int pid, state, weight, cpu, flags;
  647. char name[PROC_NAME_LEN];
  648. /* The reaper frees PCBs; snapshot this slot under the process
  649. * lock and print the copy outside the critical section. */
  650. flags = sched_lock_acquire();
  651. p = procs[i];
  652. if (!p || p->state == PROC_DEAD) {
  653. sched_lock_release(flags);
  654. continue;
  655. }
  656. pid = p->pid;
  657. state = p->state;
  658. weight = p->weight;
  659. cpu = p->on_cpu;
  660. for (j = 0; j < sizeof(name) - 1 && p->name[j]; j++)
  661. name[j] = p->name[j];
  662. name[j] = '\0';
  663. sched_lock_release(flags);
  664. io_putchar(' ');
  665. put_dec(pid);
  666. io_putstr(" ");
  667. io_putstr(state_str(state));
  668. io_putstr(" ");
  669. put_dec(weight);
  670. io_putstr(" ");
  671. if (cpu != PROC_NO_CPU)
  672. put_dec(cpu);
  673. else
  674. io_putchar('-');
  675. io_putstr(" ");
  676. io_putstr(name);
  677. io_putchar('\n');
  678. }
  679. }
  680. /* Count the 4 KB user pages mapped between USER_BASE and USER_STACK_TOP.
  681. * A read-only page-directory walk (paging keeps everything identity-mapped,
  682. * so the tables are directly addressable). Used by `top` for the memory
  683. * hint. */
  684. static unsigned int count_user_pages(unsigned int cr3)
  685. {
  686. const unsigned int *pd;
  687. unsigned int i, pages = 0;
  688. if (!cr3)
  689. return 0;
  690. pd = (const unsigned int *)cr3;
  691. for (i = USER_BASE >> 22; i <= (USER_STACK_TOP - 1) >> 22; i++) {
  692. unsigned int pde = pd[i];
  693. const unsigned int *pt;
  694. if (!(pde & PTE_PRESENT) || (pde & 0x80)) /* 0x80: 4 MB page */
  695. continue;
  696. pt = (const unsigned int *)(pde & 0xFFFFF000u);
  697. {
  698. unsigned int j;
  699. for (j = 0; j < 1024; j++)
  700. if (pt[j] & PTE_PRESENT)
  701. pages++;
  702. }
  703. }
  704. return pages;
  705. }
  706. /* Snapshot of the index-th live process, for the `top` syscall. Indexes over
  707. * live processes in slot order, so callers iterate 0, 1, ... until 0. */
  708. int process_procinfo(unsigned int index, struct sys_procinfo *out)
  709. {
  710. unsigned int i, seen = 0, life, flags;
  711. if (!out)
  712. return 0;
  713. flags = sched_lock_acquire();
  714. for (i = 0; i < PROC_MAX; i++) {
  715. struct process *p = procs[i];
  716. if (!p || p->state == PROC_DEAD)
  717. continue;
  718. if (seen++ != index)
  719. continue;
  720. life = get_ticks_ms() - p->start_ticks;
  721. out->pid = p->pid;
  722. out->state = p->state;
  723. out->cpu_ticks = p->cpu_ticks;
  724. out->life_ticks = life ? life : 1;
  725. /* Kernel stack page + page directory + user pages (the WM shares
  726. * the kernel directory; counting one is close enough). */
  727. out->pages = 2 + count_user_pages(p->cr3) + p->heap_pages;
  728. out->weight = p->weight;
  729. out->cpu = p->on_cpu;
  730. name_copy(out->name, p->name);
  731. sched_lock_release(flags);
  732. return 1;
  733. }
  734. sched_lock_release(flags);
  735. return 0;
  736. }
  737. int process_kill(unsigned int pid)
  738. {
  739. struct process *target = 0;
  740. unsigned int i, flags, on_cpu;
  741. if (pid == 0)
  742. return -1; /* cannot kill the kernel idle process */
  743. flags = sched_lock_acquire();
  744. for (i = 0; i < PROC_MAX; i++) {
  745. struct process *p = procs[i];
  746. if (p && p->state != PROC_DEAD && p->pid == pid) {
  747. target = p;
  748. break;
  749. }
  750. }
  751. if (!target) {
  752. sched_lock_release(flags);
  753. return -1;
  754. }
  755. if (target == current_proc()) {
  756. target->state = PROC_DEAD;
  757. sched_lock_release(flags);
  758. wait_wake_all(&exit_wq);
  759. sched_yield(); /* never resumes */
  760. return 0;
  761. }
  762. on_cpu = target->on_cpu;
  763. sched_unlink_locked(target);
  764. target->state = PROC_DEAD;
  765. sched_lock_release(flags);
  766. wait_wake_all(&exit_wq);
  767. if (on_cpu == PROC_NO_CPU) {
  768. /* Not running anywhere: its page directory is no longer in any
  769. * CR3, so the reaper may take it now. */
  770. process_zombie_add(target);
  771. } else {
  772. /* The target CPU's next schedule queues the zombie only after it
  773. * has switched away from it. */
  774. smp_resched_cpu(on_cpu);
  775. }
  776. return 0;
  777. }
  778. int process_set_weight(unsigned int pid, unsigned int weight)
  779. {
  780. unsigned int i, flags;
  781. if (weight < 1)
  782. weight = 1;
  783. if (weight > SCHED_WEIGHT_MAX)
  784. weight = SCHED_WEIGHT_MAX;
  785. flags = sched_lock_acquire();
  786. for (i = 0; i < PROC_MAX; i++) {
  787. struct process *p = procs[i];
  788. if (p && p->state != PROC_DEAD && p->pid == pid) {
  789. sched_set_weight_locked(p, weight);
  790. sched_lock_release(flags);
  791. return 0;
  792. }
  793. }
  794. sched_lock_release(flags);
  795. return -1;
  796. }
  797. int process_set_affinity(unsigned int pid, unsigned int mask)
  798. {
  799. unsigned int i, flags;
  800. if (!mask)
  801. return -1;
  802. flags = sched_lock_acquire();
  803. for (i = 0; i < PROC_MAX; i++) {
  804. struct process *p = procs[i];
  805. if (p && p->state != PROC_DEAD && p->pid == pid) {
  806. p->affinity = mask;
  807. sched_lock_release(flags);
  808. return 0;
  809. }
  810. }
  811. sched_lock_release(flags);
  812. return -1;
  813. }
  814. /* Interrupts off. */
  815. static int process_alive_locked(unsigned int pid)
  816. {
  817. unsigned int i;
  818. if (pid == 0)
  819. return 1; /* idle is not in the table but always exists */
  820. for (i = 0; i < PROC_MAX; i++) {
  821. struct process *p = procs[i];
  822. if (p && p->pid == pid &&
  823. (p->state == PROC_READY ||
  824. p->state == PROC_RUNNING ||
  825. p->state == PROC_SLEEPING ||
  826. p->state == PROC_BLOCKED))
  827. return 1;
  828. }
  829. return 0;
  830. }
  831. /* Return 1 if a process with `pid` is still alive. */
  832. int process_is_alive(unsigned int pid)
  833. {
  834. unsigned int flags;
  835. int alive;
  836. flags = sched_lock_acquire();
  837. alive = process_alive_locked(pid);
  838. sched_lock_release(flags);
  839. return alive;
  840. }
  841. int process_is_sleeping(unsigned int pid)
  842. {
  843. unsigned int i, flags;
  844. int sleeping = 0;
  845. if (pid == 0)
  846. return 0;
  847. flags = sched_lock_acquire();
  848. for (i = 0; i < PROC_MAX; i++) {
  849. struct process *p = procs[i];
  850. if (p && p->pid == pid) {
  851. sleeping = (p->state == PROC_SLEEPING);
  852. break;
  853. }
  854. }
  855. sched_lock_release(flags);
  856. return sleeping;
  857. }
  858. void process_set_tty(unsigned int pid, int tty)
  859. {
  860. unsigned int i, flags;
  861. flags = sched_lock_acquire();
  862. for (i = 0; i < PROC_MAX; i++) {
  863. struct process *p = procs[i];
  864. if (p && p->pid == pid && p->state != PROC_DEAD) {
  865. p->tty = tty;
  866. break;
  867. }
  868. }
  869. sched_lock_release(flags);
  870. }
  871. /* Block the current process for `ms` milliseconds. The sleep list wakes it
  872. * from the PIT; no polling loop is needed. */
  873. void process_sleep_ms(unsigned int ms)
  874. {
  875. sched_sleep_ms(ms);
  876. }
  877. /* Wait until the process with `pid` has exited. The exit path wakes the
  878. * queue, so this is event-driven rather than a 10 ms poll. */
  879. void process_wait(unsigned int pid)
  880. {
  881. for (;;) {
  882. unsigned int flags = sched_lock_acquire();
  883. if (!process_alive_locked(pid)) {
  884. sched_lock_release(flags);
  885. return;
  886. }
  887. sched_block_on_locked(&exit_wq);
  888. sched_lock_release(flags);
  889. sched_yield();
  890. }
  891. }