process.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 it (identity
  398. * mapped) 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_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 the identity
  420. * map at the same offset from the physical page top. */
  421. {
  422. unsigned int va_top = USER_STACK_TOP;
  423. unsigned char *base_phys = (unsigned char *)(top_phys + 0x1000);
  424. unsigned char *str_phys;
  425. unsigned int *frame_phys;
  426. unsigned int len = 0;
  427. unsigned int va, k;
  428. if (arg)
  429. while (arg[len])
  430. len++;
  431. va = (va_top - (len + 1)) & ~15u;
  432. if (va - 8 < USER_STACK_TOP - USER_STACK_PAGES * 0x1000) {
  433. mm_free_page(cr3);
  434. kfree(p);
  435. irq_restore(flags);
  436. return -1; /* argument does not fit the stack */
  437. }
  438. str_phys = base_phys - (va_top - va);
  439. for (k = 0; k <= len; k++)
  440. str_phys[k] = arg ? arg[k] : '\0';
  441. frame_phys = (unsigned int *)(str_phys - 8);
  442. frame_phys[0] = 0; /* fake return address */
  443. frame_phys[1] = va; /* the argument pointer (user VA) */
  444. user_esp = va - 8;
  445. }
  446. base = stack_alloc(&owned);
  447. if (!base) {
  448. mm_free_page(cr3);
  449. kfree(p);
  450. irq_restore(flags);
  451. return -1;
  452. }
  453. top = base + PROC_STACK_SIZE;
  454. sp = (unsigned int *)top;
  455. *--sp = 0x23; /* SS: user data */
  456. *--sp = user_esp; /* ESP: user stack + arg frame */
  457. *--sp = 0x202; /* EFLAGS: IF=1 */
  458. *--sp = 0x1B; /* CS: user code */
  459. *--sp = entry; /* EIP */
  460. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  461. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  462. }
  463. proc_commit(p, slot, name, (unsigned int)sp, owned, base, cr3, top,
  464. 0, 0);
  465. if (cwd)
  466. cwd_copy(p, cwd);
  467. if (tty)
  468. p->tty = (int)tty;
  469. sched_add_ready(p);
  470. pid = (int)p->pid;
  471. irq_restore(flags);
  472. return pid;
  473. }
  474. int process_create_user(const char *name, const void *image, unsigned int size,
  475. const char *arg, const char *cwd)
  476. {
  477. return process_create_user_tty(name, image, size, arg, cwd, 0);
  478. }
  479. /* Set the output screen of the currently-running process (used by a terminal
  480. at startup to bind itself to its own screen). */
  481. /* Render target override for the running process (0 = the screen). Lives in
  482. * the PCB so a preempted layer draw can never leak into another process. */
  483. struct surface *process_render_target(void)
  484. {
  485. struct process *p = current_proc();
  486. return p ? p->target : 0;
  487. }
  488. void process_set_render_target(struct surface *s)
  489. {
  490. struct process *p = current_proc();
  491. if (p)
  492. p->target = s;
  493. }
  494. void process_map_mmio_all(unsigned int phys_base, unsigned int size)
  495. {
  496. unsigned int i, flags;
  497. /* Map the kernel directory (the one new process directories clone) and
  498. * every live process copy. paging_current_cr3() is the *current* process
  499. * directory, which may be one of these; mapping it twice is harmless.
  500. * The reaper frees PCBs, so hold the process lock while iterating. */
  501. flags = sched_lock_acquire();
  502. paging_map_mmio(phys_base, size);
  503. for (i = 0; i < PROC_MAX; i++) {
  504. struct process *p = procs[i];
  505. if (!p || p->state == PROC_DEAD)
  506. continue;
  507. if (p->cr3)
  508. paging_map_mmio_cr3(p->cr3, phys_base, size);
  509. }
  510. sched_lock_release(flags);
  511. }
  512. /* Unpublish a dead process and hand it to the kworker. Takes sched_lock
  513. * itself; callers must have already switched away from its kernel stack (or
  514. * never run it), so the reaper cannot free memory still in use. */
  515. void process_zombie_add(struct process *p)
  516. {
  517. unsigned int flags;
  518. if (!p)
  519. return;
  520. flags = sched_lock_acquire();
  521. if (p->zombie_queued) {
  522. sched_lock_release(flags);
  523. return;
  524. }
  525. p->zombie_queued = 1;
  526. p->on_cpu = PROC_NO_CPU;
  527. if (p->slot < PROC_MAX)
  528. procs[p->slot] = 0;
  529. p->zombie_next = 0;
  530. if (zombie_tail)
  531. zombie_tail->zombie_next = p;
  532. else
  533. zombie_head = p;
  534. zombie_tail = p;
  535. sched_lock_release(flags);
  536. }
  537. unsigned int process_zombie_count_locked(void)
  538. {
  539. unsigned int n = 0;
  540. struct process *p;
  541. for (p = zombie_head; p; p = p->zombie_next)
  542. n++;
  543. return n;
  544. }
  545. unsigned int process_zombie_count(void)
  546. {
  547. unsigned int flags, n;
  548. flags = sched_lock_acquire();
  549. n = process_zombie_count_locked();
  550. sched_lock_release(flags);
  551. return n;
  552. }
  553. /* Free switched-out dead processes. Runs in the kworker (or the idle
  554. * fallback), not in the PIT handler. Preemption is disabled while the
  555. * resources are torn down: tty_reap()/the page-table walk are
  556. * not re-entrant, matching the old IRQ-context reaper. */
  557. void process_reap_dead(void)
  558. {
  559. unsigned int cur_cr3 = paging_current_cr3();
  560. unsigned int flags;
  561. struct process *p, *list;
  562. preempt_disable();
  563. flags = sched_lock_acquire();
  564. list = zombie_head;
  565. zombie_head = zombie_tail = 0;
  566. sched_lock_release(flags);
  567. for (p = list; p; ) {
  568. struct process *next = p->zombie_next;
  569. sys_net_reap(p->pid); /* release a held NIC */
  570. tty_reap(p->pid); /* drop its terminal queue */
  571. if (p->stack_base)
  572. mm_free_page(p->stack_base);
  573. if (p->cr3 && p->cr3 != cur_cr3) {
  574. /* User code/stack pages belong to the process. The gfx
  575. * canvas mapping is only a window into pages owned by
  576. * the session: release the PTEs and page tables, not
  577. * the pages. */
  578. paging_release_user_range(p->cr3,
  579. USER_BASE, USER_STACK_TOP - USER_BASE, 1);
  580. paging_release_user_range(p->cr3,
  581. USER_WIN_BASE, USER_WIN_RANGE, 0);
  582. sys_mem_release(p);
  583. /* WM surface mappings: page tables only; the shadow/
  584. * terminal/canvas pages belong to the window server. */
  585. paging_release_user_range(p->cr3,
  586. WM_SHADOW_VA, 0x00800000, 0);
  587. paging_release_user_range(p->cr3,
  588. WM_CONTENT_VA_BASE,
  589. WM_CONTENT_SLOT * WM_MAX_WINDOWS, 0);
  590. mm_free_page(p->cr3);
  591. }
  592. p->stack_base = 0;
  593. p->cr3 = 0;
  594. kfree(p);
  595. p = next;
  596. }
  597. preempt_enable();
  598. }
  599. void process_exit(void)
  600. {
  601. struct process *p = current_proc();
  602. if (p)
  603. p->state = PROC_DEAD;
  604. wait_wake_all(&exit_wq);
  605. sched_yield(); /* switch away immediately via int 0x81 */
  606. for (;;)
  607. __asm__ volatile ("hlt"); /* the scheduler never resumes us */
  608. }
  609. /* ---- user-facing helpers ---- */
  610. static const char *state_str(unsigned int s)
  611. {
  612. switch (s) {
  613. case PROC_RUNNING: return "run ";
  614. case PROC_READY: return "ready";
  615. case PROC_SLEEPING: return "sleep";
  616. case PROC_BLOCKED: return "block";
  617. default: return "? ";
  618. }
  619. }
  620. static void put_dec(unsigned int n)
  621. {
  622. char buf[12];
  623. unsigned int i = sizeof(buf) - 1;
  624. buf[i] = '\0';
  625. if (n == 0) {
  626. io_putchar('0');
  627. return;
  628. }
  629. while (n && i > 0) {
  630. buf[--i] = (char)('0' + (n % 10));
  631. n /= 10;
  632. }
  633. io_putstr(buf + i);
  634. }
  635. void process_ps(void)
  636. {
  637. unsigned int i, j;
  638. io_putstr("PID STATE W CPU NAME\n");
  639. for (i = 0; i < PROC_MAX; i++) {
  640. struct process *p;
  641. unsigned int pid, state, weight, cpu, flags;
  642. char name[PROC_NAME_LEN];
  643. /* The reaper frees PCBs; snapshot this slot under the process
  644. * lock and print the copy outside the critical section. */
  645. flags = sched_lock_acquire();
  646. p = procs[i];
  647. if (!p || p->state == PROC_DEAD) {
  648. sched_lock_release(flags);
  649. continue;
  650. }
  651. pid = p->pid;
  652. state = p->state;
  653. weight = p->weight;
  654. cpu = p->on_cpu;
  655. for (j = 0; j < sizeof(name) - 1 && p->name[j]; j++)
  656. name[j] = p->name[j];
  657. name[j] = '\0';
  658. sched_lock_release(flags);
  659. io_putchar(' ');
  660. put_dec(pid);
  661. io_putstr(" ");
  662. io_putstr(state_str(state));
  663. io_putstr(" ");
  664. put_dec(weight);
  665. io_putstr(" ");
  666. if (cpu != PROC_NO_CPU)
  667. put_dec(cpu);
  668. else
  669. io_putchar('-');
  670. io_putstr(" ");
  671. io_putstr(name);
  672. io_putchar('\n');
  673. }
  674. }
  675. /* Count the 4 KB user pages mapped between USER_BASE and USER_STACK_TOP.
  676. * A read-only page-directory walk (paging keeps everything identity-mapped,
  677. * so the tables are directly addressable). Used by `top` for the memory
  678. * hint. */
  679. static unsigned int count_user_pages(unsigned int cr3)
  680. {
  681. const unsigned int *pd;
  682. unsigned int i, pages = 0;
  683. if (!cr3)
  684. return 0;
  685. pd = (const unsigned int *)cr3;
  686. for (i = USER_BASE >> 22; i <= (USER_STACK_TOP - 1) >> 22; i++) {
  687. unsigned int pde = pd[i];
  688. const unsigned int *pt;
  689. if (!(pde & PTE_PRESENT) || (pde & 0x80)) /* 0x80: 4 MB page */
  690. continue;
  691. pt = (const unsigned int *)(pde & 0xFFFFF000u);
  692. {
  693. unsigned int j;
  694. for (j = 0; j < 1024; j++)
  695. if (pt[j] & PTE_PRESENT)
  696. pages++;
  697. }
  698. }
  699. return pages;
  700. }
  701. /* Snapshot of the index-th live process, for the `top` syscall. Indexes over
  702. * live processes in slot order, so callers iterate 0, 1, ... until 0. */
  703. int process_procinfo(unsigned int index, struct sys_procinfo *out)
  704. {
  705. unsigned int i, seen = 0, life, flags;
  706. if (!out)
  707. return 0;
  708. flags = sched_lock_acquire();
  709. for (i = 0; i < PROC_MAX; i++) {
  710. struct process *p = procs[i];
  711. if (!p || p->state == PROC_DEAD)
  712. continue;
  713. if (seen++ != index)
  714. continue;
  715. life = get_ticks_ms() - p->start_ticks;
  716. out->pid = p->pid;
  717. out->state = p->state;
  718. out->cpu_ticks = p->cpu_ticks;
  719. out->life_ticks = life ? life : 1;
  720. /* Kernel stack page + page directory + user pages (the WM shares
  721. * the kernel directory; counting one is close enough). */
  722. out->pages = 2 + count_user_pages(p->cr3) + p->heap_pages;
  723. out->weight = p->weight;
  724. out->cpu = p->on_cpu;
  725. name_copy(out->name, p->name);
  726. sched_lock_release(flags);
  727. return 1;
  728. }
  729. sched_lock_release(flags);
  730. return 0;
  731. }
  732. int process_kill(unsigned int pid)
  733. {
  734. struct process *target = 0;
  735. unsigned int i, flags, on_cpu;
  736. if (pid == 0)
  737. return -1; /* cannot kill the kernel idle process */
  738. flags = sched_lock_acquire();
  739. for (i = 0; i < PROC_MAX; i++) {
  740. struct process *p = procs[i];
  741. if (p && p->state != PROC_DEAD && p->pid == pid) {
  742. target = p;
  743. break;
  744. }
  745. }
  746. if (!target) {
  747. sched_lock_release(flags);
  748. return -1;
  749. }
  750. if (target == current_proc()) {
  751. target->state = PROC_DEAD;
  752. sched_lock_release(flags);
  753. wait_wake_all(&exit_wq);
  754. sched_yield(); /* never resumes */
  755. return 0;
  756. }
  757. on_cpu = target->on_cpu;
  758. sched_unlink_locked(target);
  759. target->state = PROC_DEAD;
  760. sched_lock_release(flags);
  761. wait_wake_all(&exit_wq);
  762. if (on_cpu == PROC_NO_CPU) {
  763. /* Not running anywhere: its page directory is no longer in any
  764. * CR3, so the reaper may take it now. */
  765. process_zombie_add(target);
  766. } else {
  767. /* The target CPU's next schedule queues the zombie only after it
  768. * has switched away from it. */
  769. smp_resched_cpu(on_cpu);
  770. }
  771. return 0;
  772. }
  773. int process_set_weight(unsigned int pid, unsigned int weight)
  774. {
  775. unsigned int i, flags;
  776. if (weight < 1)
  777. weight = 1;
  778. if (weight > SCHED_WEIGHT_MAX)
  779. weight = SCHED_WEIGHT_MAX;
  780. flags = sched_lock_acquire();
  781. for (i = 0; i < PROC_MAX; i++) {
  782. struct process *p = procs[i];
  783. if (p && p->state != PROC_DEAD && p->pid == pid) {
  784. sched_set_weight_locked(p, weight);
  785. sched_lock_release(flags);
  786. return 0;
  787. }
  788. }
  789. sched_lock_release(flags);
  790. return -1;
  791. }
  792. int process_set_affinity(unsigned int pid, unsigned int mask)
  793. {
  794. unsigned int i, flags;
  795. if (!mask)
  796. return -1;
  797. flags = sched_lock_acquire();
  798. for (i = 0; i < PROC_MAX; i++) {
  799. struct process *p = procs[i];
  800. if (p && p->state != PROC_DEAD && p->pid == pid) {
  801. p->affinity = mask;
  802. sched_lock_release(flags);
  803. return 0;
  804. }
  805. }
  806. sched_lock_release(flags);
  807. return -1;
  808. }
  809. /* Interrupts off. */
  810. static int process_alive_locked(unsigned int pid)
  811. {
  812. unsigned int i;
  813. if (pid == 0)
  814. return 1; /* idle is not in the table but always exists */
  815. for (i = 0; i < PROC_MAX; i++) {
  816. struct process *p = procs[i];
  817. if (p && p->pid == pid &&
  818. (p->state == PROC_READY ||
  819. p->state == PROC_RUNNING ||
  820. p->state == PROC_SLEEPING ||
  821. p->state == PROC_BLOCKED))
  822. return 1;
  823. }
  824. return 0;
  825. }
  826. /* Return 1 if a process with `pid` is still alive. */
  827. int process_is_alive(unsigned int pid)
  828. {
  829. unsigned int flags;
  830. int alive;
  831. flags = sched_lock_acquire();
  832. alive = process_alive_locked(pid);
  833. sched_lock_release(flags);
  834. return alive;
  835. }
  836. int process_is_sleeping(unsigned int pid)
  837. {
  838. unsigned int i, flags;
  839. int sleeping = 0;
  840. if (pid == 0)
  841. return 0;
  842. flags = sched_lock_acquire();
  843. for (i = 0; i < PROC_MAX; i++) {
  844. struct process *p = procs[i];
  845. if (p && p->pid == pid) {
  846. sleeping = (p->state == PROC_SLEEPING);
  847. break;
  848. }
  849. }
  850. sched_lock_release(flags);
  851. return sleeping;
  852. }
  853. void process_set_tty(unsigned int pid, int tty)
  854. {
  855. unsigned int i, flags;
  856. flags = sched_lock_acquire();
  857. for (i = 0; i < PROC_MAX; i++) {
  858. struct process *p = procs[i];
  859. if (p && p->pid == pid && p->state != PROC_DEAD) {
  860. p->tty = tty;
  861. break;
  862. }
  863. }
  864. sched_lock_release(flags);
  865. }
  866. /* Block the current process for `ms` milliseconds. The sleep list wakes it
  867. * from the PIT; no polling loop is needed. */
  868. void process_sleep_ms(unsigned int ms)
  869. {
  870. sched_sleep_ms(ms);
  871. }
  872. /* Wait until the process with `pid` has exited. The exit path wakes the
  873. * queue, so this is event-driven rather than a 10 ms poll. */
  874. void process_wait(unsigned int pid)
  875. {
  876. for (;;) {
  877. unsigned int flags = sched_lock_acquire();
  878. if (!process_alive_locked(pid)) {
  879. sched_lock_release(flags);
  880. return;
  881. }
  882. sched_block_on_locked(&exit_wq);
  883. sched_lock_release(flags);
  884. sched_yield();
  885. }
  886. }