process.c 25 KB

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