process.c 22 KB

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