process.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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 "init.h"
  11. #include "io.h"
  12. #include "kmalloc.h"
  13. #include "mm.h"
  14. #include "paging.h"
  15. #include "serial.h"
  16. #include "smp.h"
  17. #include "sys_fs.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. static void process_kill_children(unsigned int pid);
  32. /* Top of the in-image bootstrap stack (see kernel.ld). */
  33. extern char __boot_stack_top[];
  34. /* ---- interrupt flag save/restore (for short critical sections) ---- */
  35. static inline unsigned int irq_save_disable(void)
  36. {
  37. unsigned int flags;
  38. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  39. return flags;
  40. }
  41. static inline void irq_restore(unsigned int flags)
  42. {
  43. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  44. }
  45. /* Fallback pool: a static array, so its address is not a fixed memory-map
  46. * entry (same reasoning as the in-image stack in kernel.ld). */
  47. static unsigned int fallback_stacks[PROC_FALLBACK_STACK_MAX][PROC_STACK_SIZE / 4]
  48. __attribute__((aligned(16)));
  49. /* Allocate a process stack and return its BASE address (page-aligned), or 0.
  50. * *owned is set to 1 if it came from the frame allocator (must be freed on
  51. * exit), 0 if it came from the fallback pool. */
  52. static unsigned int stack_alloc(int *owned)
  53. {
  54. unsigned int base = mm_alloc_page();
  55. if (base) {
  56. *owned = 1;
  57. return base;
  58. }
  59. if (fallback_stack_next < PROC_FALLBACK_STACK_MAX) {
  60. base = (unsigned int)&fallback_stacks[fallback_stack_next][0];
  61. fallback_stack_next++;
  62. *owned = 0;
  63. return base;
  64. }
  65. return 0;
  66. }
  67. /* Clear one control block. kmalloc memory is not zeroed and QEMU hides that
  68. * (guest RAM starts cleared), so a fresh PCB would otherwise carry garbage in
  69. * cr3/pid/state; the scheduler and MMIO mapping code trust those fields. */
  70. static void clear_proc(struct process *p)
  71. {
  72. unsigned char *b = (unsigned char *)p;
  73. unsigned int i;
  74. for (i = 0; i < sizeof(*p); i++)
  75. b[i] = 0;
  76. }
  77. static void name_copy(char *dst, const char *src)
  78. {
  79. unsigned int i;
  80. for (i = 0; i < PROC_NAME_LEN - 1 && src[i]; i++)
  81. dst[i] = src[i];
  82. dst[i] = '\0';
  83. }
  84. static void cwd_copy(struct process *p, const char *cwd)
  85. {
  86. unsigned int i;
  87. for (i = 0; cwd && cwd[i] && i + 1 < sizeof(p->cwd); i++)
  88. p->cwd[i] = cwd[i];
  89. p->cwd[i] = '\0';
  90. }
  91. /* Find a free slot in the stable pointer table. Called with interrupts
  92. * disabled. Returns the slot index, or -1 when the table is full. */
  93. static int alloc_slot(void)
  94. {
  95. unsigned int slot;
  96. for (slot = 0; slot < PROC_MAX; slot++)
  97. if (!procs[slot])
  98. return (int)slot;
  99. return -1;
  100. }
  101. /* Allocate and clear a PCB, then reserve a table slot (but do not publish it
  102. * yet). Called with interrupts disabled. */
  103. static struct process *proc_alloc(int *slot_out)
  104. {
  105. struct process *p = (struct process *)kmalloc(sizeof(*p));
  106. int slot;
  107. if (!p)
  108. return 0;
  109. clear_proc(p);
  110. slot = alloc_slot();
  111. if (slot < 0) {
  112. kfree(p);
  113. return 0;
  114. }
  115. *slot_out = slot;
  116. return p;
  117. }
  118. /* Fill in the common fields and publish the process in its slot. The caller
  119. * has already built the initial kernel stack frame. Called with interrupts
  120. * disabled and with parent = current_proc() (possibly idle). */
  121. static void proc_commit(struct process *p, int slot, const char *name,
  122. unsigned int esp, int owned, unsigned int stack_base,
  123. unsigned int cr3, unsigned int kstack_top,
  124. void (*kentry)(void), int kernel)
  125. {
  126. struct process *parent = current_proc();
  127. p->esp = esp;
  128. p->pid = next_pid_counter++;
  129. p->parent_pid = parent ? parent->pid : 0;
  130. p->slot = (unsigned int)slot;
  131. p->stack_base = owned ? stack_base : 0;
  132. p->cr3 = cr3;
  133. p->kstack_top = kstack_top;
  134. p->cpu_ticks = 0;
  135. p->start_ticks = get_ticks_ms();
  136. p->on_cpu = PROC_NO_CPU;
  137. p->last_cpu = PROC_NO_CPU;
  138. /* Kernel threads touch global kernel state; keep them on the BSP.
  139. * Ring-3 processes may use every CPU. */
  140. p->affinity = kernel ? 1u : 0xFFFFFFFFu;
  141. p->kentry = kentry;
  142. p->kernel_thread = (unsigned int)kernel;
  143. p->weight = 0;
  144. p->stride = 0;
  145. p->vruntime = 0;
  146. p->slice_start = 0;
  147. p->tty = parent ? parent->tty : 0;
  148. if (parent)
  149. cwd_copy(p, parent->cwd);
  150. else
  151. p->cwd[0] = '\0';
  152. name_copy(p->name, name);
  153. procs[slot] = p;
  154. /* The caller publishes the process as runnable only after applying any
  155. * per-process overrides (cwd/tty): an AP can pick it as soon as it is
  156. * on the run queue. */
  157. }
  158. void process_init(void)
  159. {
  160. unsigned int i;
  161. sched_init();
  162. zombie_head = zombie_tail = 0;
  163. wait_queue_init(&exit_wq);
  164. for (i = 0; i < PROC_MAX; i++)
  165. procs[i] = 0;
  166. clear_proc(&idle_procs[0]);
  167. idle_procs[0].state = PROC_RUNNING;
  168. idle_procs[0].pid = 0;
  169. idle_procs[0].slot = PROC_MAX;
  170. idle_procs[0].esp = 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. * `parent_pid` is recorded before the process becomes runnable. */
  373. int process_create_user_full(const char *name, const void *image, unsigned int size,
  374. const char *arg, const char *cwd, unsigned int tty,
  375. unsigned int parent_pid, unsigned int cflags)
  376. {
  377. int slot, owned, pid;
  378. unsigned int base, top, *sp, cr3, entry;
  379. unsigned int flags;
  380. struct process *p;
  381. int i;
  382. flags = irq_save_disable();
  383. p = proc_alloc(&slot);
  384. if (!p) {
  385. irq_restore(flags);
  386. return -1;
  387. }
  388. cr3 = paging_new_directory();
  389. if (!cr3) {
  390. kfree(p);
  391. irq_restore(flags);
  392. return -1;
  393. }
  394. entry = elf_load(cr3, image, size);
  395. if (!entry) {
  396. mm_free_page(cr3);
  397. kfree(p);
  398. irq_restore(flags);
  399. return -2; /* not a loadable ELF */
  400. }
  401. /* Map the user stack just below USER_STACK_TOP. Keep the top page's
  402. * physical address: the argument string is written through a kmap of
  403. * it (the frame may be highmem) after it is mapped in `cr3`. */
  404. {
  405. unsigned int top_phys = 0;
  406. unsigned int user_esp = USER_STACK_TOP;
  407. for (i = 0; i < USER_STACK_PAGES; i++) {
  408. unsigned int phys = mm_alloc_user_page();
  409. unsigned int va = USER_STACK_TOP - (unsigned int)(i + 1) * 0x1000;
  410. if (!phys ||
  411. !paging_map_page(cr3, va, phys,
  412. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  413. mm_free_page(cr3);
  414. kfree(p);
  415. irq_restore(flags);
  416. return -1;
  417. }
  418. if (i == 0)
  419. top_phys = phys;
  420. }
  421. /* One C string argument at the top of the user stack with a fake
  422. * return address below it, so `_start(const char *arg)` reads it
  423. * as its first parameter without needing a crt0. The virtual
  424. * addresses go into the frame; the writes go through a kmap of the
  425. * top stack page at the same offset from its end. */
  426. {
  427. unsigned int va_top = USER_STACK_TOP;
  428. unsigned char *top_map = kmap(top_phys);
  429. unsigned char *base_phys = top_map + 0x1000;
  430. unsigned char *str_phys;
  431. unsigned int *frame_phys;
  432. unsigned int len = 0;
  433. unsigned int va, k;
  434. if (arg)
  435. while (arg[len])
  436. len++;
  437. va = (va_top - (len + 1)) & ~15u;
  438. /* The string and frame are written through one kmap'd
  439. * page, so they must fit the top stack page. */
  440. if (va - 8 < USER_STACK_TOP - 0x1000) {
  441. kunmap(top_map);
  442. mm_free_page(cr3);
  443. kfree(p);
  444. irq_restore(flags);
  445. return -1; /* argument does not fit the stack */
  446. }
  447. str_phys = base_phys - (va_top - va);
  448. for (k = 0; k <= len; k++)
  449. str_phys[k] = arg ? arg[k] : '\0';
  450. frame_phys = (unsigned int *)(str_phys - 8);
  451. frame_phys[0] = 0; /* fake return address */
  452. frame_phys[1] = va; /* the argument pointer (user VA) */
  453. user_esp = va - 8;
  454. kunmap(top_map);
  455. }
  456. base = stack_alloc(&owned);
  457. if (!base) {
  458. mm_free_page(cr3);
  459. kfree(p);
  460. irq_restore(flags);
  461. return -1;
  462. }
  463. top = base + PROC_STACK_SIZE;
  464. sp = (unsigned int *)top;
  465. *--sp = 0x23; /* SS: user data */
  466. *--sp = user_esp; /* ESP: user stack + arg frame */
  467. *--sp = 0x202; /* EFLAGS: IF=1 */
  468. *--sp = 0x1B; /* CS: user code */
  469. *--sp = entry; /* EIP */
  470. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  471. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  472. }
  473. proc_commit(p, slot, name, (unsigned int)sp, owned, base, cr3, top,
  474. 0, 0);
  475. p->parent_pid = parent_pid;
  476. if (cwd)
  477. cwd_copy(p, cwd);
  478. if (tty)
  479. p->tty = (int)tty;
  480. /* A process may not outlive the parent that is creating it. A detached
  481. * process falls back to the root when the session parent raced with the
  482. * spawn; a normal child is aborted instead. */
  483. if (parent_pid && !process_is_alive(parent_pid)) {
  484. if (cflags & PROC_CREATE_DETACH) {
  485. p->parent_pid = 0;
  486. } else {
  487. process_kill(p->pid);
  488. irq_restore(flags);
  489. return -1;
  490. }
  491. }
  492. /* Record the init pid before sched_add_ready(): an AP could otherwise
  493. * run init and fail SYS_IS_INIT or its WM's attach check. */
  494. if (cflags & PROC_CREATE_INIT)
  495. init_server_note_pid(p->pid);
  496. sched_add_ready(p);
  497. pid = (int)p->pid;
  498. irq_restore(flags);
  499. return pid;
  500. }
  501. int process_create_user_tty(const char *name, const void *image, unsigned int size,
  502. const char *arg, const char *cwd, unsigned int tty)
  503. {
  504. unsigned int parent = current_proc() ? current_proc()->pid : 0;
  505. return process_create_user_full(name, image, size, arg, cwd, tty,
  506. parent, 0);
  507. }
  508. int process_create_user(const char *name, const void *image, unsigned int size,
  509. const char *arg, const char *cwd)
  510. {
  511. return process_create_user_tty(name, image, size, arg, cwd, 0);
  512. }
  513. /* Set the output screen of the currently-running process (used by a terminal
  514. at startup to bind itself to its own screen). */
  515. /* Render target override for the running process (0 = the screen). Lives in
  516. * the PCB so a preempted layer draw can never leak into another process. */
  517. struct surface *process_render_target(void)
  518. {
  519. struct process *p = current_proc();
  520. return p ? p->target : 0;
  521. }
  522. void process_set_render_target(struct surface *s)
  523. {
  524. struct process *p = current_proc();
  525. if (p)
  526. p->target = s;
  527. }
  528. void process_sync_kernel_pde(unsigned int index)
  529. {
  530. const unsigned int *kpd = (const unsigned int *)paging_kernel_cr3();
  531. unsigned int i, flags;
  532. /* The reaper frees PCBs, so hold the process lock while iterating. */
  533. flags = sched_lock_acquire();
  534. for (i = 0; i < PROC_MAX; i++) {
  535. struct process *p = procs[i];
  536. if (!p || p->state == PROC_DEAD || !p->cr3 ||
  537. p->cr3 == (unsigned int)kpd)
  538. continue;
  539. ((unsigned int *)p->cr3)[index] = kpd[index];
  540. }
  541. sched_lock_release(flags);
  542. }
  543. /* Unpublish a dead process and hand it to the kworker. Takes sched_lock
  544. * itself; callers must have already switched away from its kernel stack (or
  545. * never run it), so the reaper cannot free memory still in use. */
  546. void process_zombie_add(struct process *p)
  547. {
  548. unsigned int flags;
  549. if (!p)
  550. return;
  551. flags = sched_lock_acquire();
  552. if (p->zombie_queued) {
  553. sched_lock_release(flags);
  554. return;
  555. }
  556. p->zombie_queued = 1;
  557. p->on_cpu = PROC_NO_CPU;
  558. if (p->slot < PROC_MAX)
  559. procs[p->slot] = 0;
  560. p->zombie_next = 0;
  561. if (zombie_tail)
  562. zombie_tail->zombie_next = p;
  563. else
  564. zombie_head = p;
  565. zombie_tail = p;
  566. sched_lock_release(flags);
  567. }
  568. unsigned int process_zombie_count_locked(void)
  569. {
  570. unsigned int n = 0;
  571. struct process *p;
  572. for (p = zombie_head; p; p = p->zombie_next)
  573. n++;
  574. return n;
  575. }
  576. unsigned int process_zombie_count(void)
  577. {
  578. unsigned int flags, n;
  579. flags = sched_lock_acquire();
  580. n = process_zombie_count_locked();
  581. sched_lock_release(flags);
  582. return n;
  583. }
  584. /* Free switched-out dead processes. Runs in the kworker (or the idle
  585. * fallback), not in the PIT handler. Preemption is disabled while the
  586. * resources are torn down: tty_reap()/the page-table walk are
  587. * not re-entrant, matching the old IRQ-context reaper. */
  588. void process_reap_dead(void)
  589. {
  590. unsigned int cur_cr3 = paging_current_cr3();
  591. unsigned int flags;
  592. struct process *p, *list;
  593. preempt_disable();
  594. flags = sched_lock_acquire();
  595. list = zombie_head;
  596. zombie_head = zombie_tail = 0;
  597. sched_lock_release(flags);
  598. for (p = list; p; ) {
  599. struct process *next = p->zombie_next;
  600. /* Safety net for a child born while its parent was dying. */
  601. process_kill_children(p->pid);
  602. sys_net_reap(p->pid); /* release a held NIC */
  603. tty_reap(p->pid); /* drop its terminal queue */
  604. if (p->stack_base)
  605. mm_free_page(p->stack_base);
  606. if (p->cr3 && p->cr3 != cur_cr3) {
  607. /* User code/stack pages belong to the process. The gfx
  608. * canvas mapping is only a window into pages owned by
  609. * the session: release the PTEs and page tables, not
  610. * the pages. */
  611. paging_release_user_range(p->cr3,
  612. USER_BASE, USER_STACK_TOP - USER_BASE, 1);
  613. paging_release_user_range(p->cr3,
  614. USER_WIN_BASE, USER_WIN_RANGE, 0);
  615. sys_mem_release(p);
  616. /* WM surface mappings: page tables only; the shadow/
  617. * terminal/canvas pages belong to the window server. */
  618. paging_release_user_range(p->cr3,
  619. WM_SHADOW_VA, 0x00800000, 0);
  620. paging_release_user_range(p->cr3,
  621. WM_CONTENT_VA_BASE,
  622. WM_CONTENT_SLOT * WM_MAX_WINDOWS, 0);
  623. mm_free_page(p->cr3);
  624. }
  625. p->stack_base = 0;
  626. p->cr3 = 0;
  627. kfree(p);
  628. p = next;
  629. }
  630. preempt_enable();
  631. }
  632. void process_exit(void)
  633. {
  634. struct process *p = current_proc();
  635. if (p) {
  636. unsigned int flags = sched_lock_acquire();
  637. p->state = PROC_DEAD;
  638. sched_lock_release(flags);
  639. /* A parent owns its subtree: no child may outlive it. */
  640. process_kill_children(p->pid);
  641. }
  642. wait_wake_all(&exit_wq);
  643. sched_yield(); /* switch away immediately via int 0x81 */
  644. for (;;)
  645. __asm__ volatile ("hlt"); /* the scheduler never resumes us */
  646. }
  647. /* ---- user-facing helpers ---- */
  648. static const char *state_str(unsigned int s)
  649. {
  650. switch (s) {
  651. case PROC_RUNNING: return "run ";
  652. case PROC_READY: return "ready";
  653. case PROC_SLEEPING: return "sleep";
  654. case PROC_BLOCKED: return "block";
  655. default: return "? ";
  656. }
  657. }
  658. static void put_dec(unsigned int n)
  659. {
  660. char buf[12];
  661. unsigned int i = sizeof(buf) - 1;
  662. buf[i] = '\0';
  663. if (n == 0) {
  664. io_putchar('0');
  665. return;
  666. }
  667. while (n && i > 0) {
  668. buf[--i] = (char)('0' + (n % 10));
  669. n /= 10;
  670. }
  671. io_putstr(buf + i);
  672. }
  673. void process_ps(void)
  674. {
  675. unsigned int i, j;
  676. io_putstr("PID STATE W CPU NAME\n");
  677. for (i = 0; i < PROC_MAX; i++) {
  678. struct process *p;
  679. unsigned int pid, state, weight, cpu, flags;
  680. char name[PROC_NAME_LEN];
  681. /* The reaper frees PCBs; snapshot this slot under the process
  682. * lock and print the copy outside the critical section. */
  683. flags = sched_lock_acquire();
  684. p = procs[i];
  685. if (!p || p->state == PROC_DEAD) {
  686. sched_lock_release(flags);
  687. continue;
  688. }
  689. pid = p->pid;
  690. state = p->state;
  691. weight = p->weight;
  692. cpu = p->on_cpu;
  693. for (j = 0; j < sizeof(name) - 1 && p->name[j]; j++)
  694. name[j] = p->name[j];
  695. name[j] = '\0';
  696. sched_lock_release(flags);
  697. io_putchar(' ');
  698. put_dec(pid);
  699. io_putstr(" ");
  700. io_putstr(state_str(state));
  701. io_putstr(" ");
  702. put_dec(weight);
  703. io_putstr(" ");
  704. if (cpu != PROC_NO_CPU)
  705. put_dec(cpu);
  706. else
  707. io_putchar('-');
  708. io_putstr(" ");
  709. io_putstr(name);
  710. io_putchar('\n');
  711. }
  712. }
  713. /* Count the 4 KB user pages mapped between USER_BASE and USER_STACK_TOP.
  714. * A read-only page-directory walk (paging keeps everything identity-mapped,
  715. * so the tables are directly addressable). Used by `top` for the memory
  716. * hint. */
  717. static unsigned int count_user_pages(unsigned int cr3)
  718. {
  719. const unsigned int *pd;
  720. unsigned int i, pages = 0;
  721. if (!cr3)
  722. return 0;
  723. pd = (const unsigned int *)cr3;
  724. for (i = USER_BASE >> 22; i <= (USER_STACK_TOP - 1) >> 22; i++) {
  725. unsigned int pde = pd[i];
  726. const unsigned int *pt;
  727. if (!(pde & PTE_PRESENT) || (pde & 0x80)) /* 0x80: 4 MB page */
  728. continue;
  729. pt = (const unsigned int *)(pde & 0xFFFFF000u);
  730. {
  731. unsigned int j;
  732. for (j = 0; j < 1024; j++)
  733. if (pt[j] & PTE_PRESENT)
  734. pages++;
  735. }
  736. }
  737. return pages;
  738. }
  739. /* Snapshot of the index-th live process, for the `top` syscall. Indexes over
  740. * live processes in slot order, so callers iterate 0, 1, ... until 0. */
  741. int process_procinfo(unsigned int index, struct sys_procinfo *out)
  742. {
  743. unsigned int i, seen = 0, life, flags;
  744. if (!out)
  745. return 0;
  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)
  750. continue;
  751. if (seen++ != index)
  752. continue;
  753. life = get_ticks_ms() - p->start_ticks;
  754. out->pid = p->pid;
  755. out->parent_pid = p->parent_pid;
  756. out->state = p->state;
  757. out->cpu_ticks = p->cpu_ticks;
  758. out->life_ticks = life ? life : 1;
  759. /* Kernel stack page + page directory + user pages (the WM shares
  760. * the kernel directory; counting one is close enough). */
  761. out->pages = 2 + count_user_pages(p->cr3) + p->heap_pages;
  762. out->weight = p->weight;
  763. out->cpu = p->on_cpu;
  764. out->kernel_thread = p->kernel_thread ? 1 : 0;
  765. name_copy(out->name, p->name);
  766. sched_lock_release(flags);
  767. return 1;
  768. }
  769. sched_lock_release(flags);
  770. return 0;
  771. }
  772. int process_kill(unsigned int pid)
  773. {
  774. struct process *target = 0;
  775. unsigned int i, flags, on_cpu;
  776. if (pid == 0)
  777. return -1; /* cannot kill the kernel idle process */
  778. if (pid == init_server_pid())
  779. return -2; /* init is protected; the kernel restarts it */
  780. flags = sched_lock_acquire();
  781. for (i = 0; i < PROC_MAX; i++) {
  782. struct process *p = procs[i];
  783. if (p && p->state != PROC_DEAD && p->pid == pid) {
  784. target = p;
  785. break;
  786. }
  787. }
  788. if (!target) {
  789. sched_lock_release(flags);
  790. return -1;
  791. }
  792. if (target == current_proc()) {
  793. target->state = PROC_DEAD;
  794. sched_lock_release(flags);
  795. process_kill_children(pid);
  796. wait_wake_all(&exit_wq);
  797. sched_yield(); /* never resumes */
  798. return 0;
  799. }
  800. on_cpu = target->on_cpu;
  801. sched_unlink_locked(target);
  802. target->state = PROC_DEAD;
  803. sched_lock_release(flags);
  804. wait_wake_all(&exit_wq);
  805. if (on_cpu == PROC_NO_CPU) {
  806. /* Not running anywhere: its page directory is no longer in any
  807. * CR3, so the reaper may take it now. */
  808. process_zombie_add(target);
  809. } else {
  810. /* The target CPU's next schedule queues the zombie only after it
  811. * has switched away from it. */
  812. smp_resched_cpu(on_cpu);
  813. }
  814. /* Queue the target first: if one of its descendants is the caller, the
  815. * recursive kill will switch away from this call. */
  816. process_kill_children(pid);
  817. return 0;
  818. }
  819. /* Kill every direct child of `pid`; process_kill() recurses. The scan is
  820. * repeated because a child may already have been reaped or marked dead while
  821. * we were walking the table. Called without sched_lock held. */
  822. static void process_kill_children(unsigned int pid)
  823. {
  824. unsigned int i;
  825. if (!pid)
  826. return; /* PID 0's child is init, which is protected */
  827. for (;;) {
  828. unsigned int child = 0;
  829. unsigned int flags = sched_lock_acquire();
  830. for (i = 0; i < PROC_MAX; i++) {
  831. struct process *p = procs[i];
  832. if (p && p->parent_pid == pid &&
  833. p->state != PROC_DEAD) {
  834. child = p->pid;
  835. break;
  836. }
  837. }
  838. sched_lock_release(flags);
  839. if (!child || process_kill(child) != 0)
  840. break;
  841. }
  842. }
  843. int process_set_weight(unsigned int pid, unsigned int weight)
  844. {
  845. unsigned int i, flags;
  846. if (weight < 1)
  847. weight = 1;
  848. if (weight > SCHED_WEIGHT_MAX)
  849. weight = SCHED_WEIGHT_MAX;
  850. flags = sched_lock_acquire();
  851. for (i = 0; i < PROC_MAX; i++) {
  852. struct process *p = procs[i];
  853. if (p && p->state != PROC_DEAD && p->pid == pid) {
  854. sched_set_weight_locked(p, weight);
  855. sched_lock_release(flags);
  856. return 0;
  857. }
  858. }
  859. sched_lock_release(flags);
  860. return -1;
  861. }
  862. int process_set_affinity(unsigned int pid, unsigned int mask)
  863. {
  864. unsigned int i, flags;
  865. if (!mask)
  866. return -1;
  867. flags = sched_lock_acquire();
  868. for (i = 0; i < PROC_MAX; i++) {
  869. struct process *p = procs[i];
  870. if (p && p->state != PROC_DEAD && p->pid == pid) {
  871. p->affinity = mask;
  872. sched_lock_release(flags);
  873. return 0;
  874. }
  875. }
  876. sched_lock_release(flags);
  877. return -1;
  878. }
  879. /* Interrupts off. */
  880. static int process_alive_locked(unsigned int pid)
  881. {
  882. unsigned int i;
  883. if (pid == 0)
  884. return 1; /* idle is not in the table but always exists */
  885. for (i = 0; i < PROC_MAX; i++) {
  886. struct process *p = procs[i];
  887. if (p && p->pid == pid &&
  888. (p->state == PROC_READY ||
  889. p->state == PROC_RUNNING ||
  890. p->state == PROC_SLEEPING ||
  891. p->state == PROC_BLOCKED))
  892. return 1;
  893. }
  894. return 0;
  895. }
  896. /* Return 1 if a process with `pid` is still alive. */
  897. int process_is_alive(unsigned int pid)
  898. {
  899. unsigned int flags;
  900. int alive;
  901. flags = sched_lock_acquire();
  902. alive = process_alive_locked(pid);
  903. sched_lock_release(flags);
  904. return alive;
  905. }
  906. int process_is_sleeping(unsigned int pid)
  907. {
  908. unsigned int i, flags;
  909. int sleeping = 0;
  910. if (pid == 0)
  911. return 0;
  912. flags = sched_lock_acquire();
  913. for (i = 0; i < PROC_MAX; i++) {
  914. struct process *p = procs[i];
  915. if (p && p->pid == pid) {
  916. sleeping = (p->state == PROC_SLEEPING);
  917. break;
  918. }
  919. }
  920. sched_lock_release(flags);
  921. return sleeping;
  922. }
  923. void process_set_tty(unsigned int pid, int tty)
  924. {
  925. unsigned int i, flags;
  926. flags = sched_lock_acquire();
  927. for (i = 0; i < PROC_MAX; i++) {
  928. struct process *p = procs[i];
  929. if (p && p->pid == pid && p->state != PROC_DEAD) {
  930. p->tty = tty;
  931. break;
  932. }
  933. }
  934. sched_lock_release(flags);
  935. }
  936. /* Block the current process for `ms` milliseconds. The sleep list wakes it
  937. * from the PIT; no polling loop is needed. */
  938. void process_sleep_ms(unsigned int ms)
  939. {
  940. sched_sleep_ms(ms);
  941. }
  942. /* Wait until the process with `pid` has exited. The exit path wakes the
  943. * queue, so this is event-driven rather than a 10 ms poll. */
  944. void process_wait(unsigned int pid)
  945. {
  946. for (;;) {
  947. unsigned int flags = sched_lock_acquire();
  948. if (!process_alive_locked(pid)) {
  949. sched_lock_release(flags);
  950. return;
  951. }
  952. sched_block_on_locked(&exit_wq);
  953. sched_lock_release(flags);
  954. sched_yield();
  955. }
  956. }