process.c 25 KB

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