process.c 25 KB

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