process.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* Process management: create, exit, round-robin schedule, ps, kill. */
  2. #include "process.h"
  3. #include "elf.h"
  4. #include "idle.h"
  5. #include "io.h"
  6. #include "kmalloc.h"
  7. #include "mm.h"
  8. #include "paging.h"
  9. #include "serial.h"
  10. struct process *procs; /* dynamically-sized process table */
  11. volatile unsigned int current_pid;
  12. static unsigned int proc_cap; /* current capacity of procs[] */
  13. static unsigned int next_pid_counter = 1; /* monotonic, wraps */
  14. static unsigned int fallback_stack_next; /* 0..PROC_FALLBACK_STACK_MAX */
  15. #define INITIAL_PROC_CAP 16
  16. /* ---- interrupt flag save/restore (for short critical sections) ---- */
  17. static inline unsigned int irq_save_disable(void)
  18. {
  19. unsigned int flags;
  20. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  21. return flags;
  22. }
  23. static inline void irq_restore(unsigned int flags)
  24. {
  25. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  26. }
  27. /* Allocate a process stack and return its BASE address (page-aligned), or 0.
  28. * *owned is set to 1 if it came from the frame allocator (must be freed on
  29. * exit), 0 if it came from the fallback pool. */
  30. static unsigned int stack_alloc(int *owned)
  31. {
  32. unsigned int base = mm_alloc_page();
  33. if (base) {
  34. *owned = 1;
  35. return base;
  36. }
  37. if (fallback_stack_next < PROC_FALLBACK_STACK_MAX) {
  38. unsigned int top = PROC_FALLBACK_STACK_BASE
  39. + fallback_stack_next * PROC_STACK_SIZE
  40. + PROC_STACK_SIZE;
  41. fallback_stack_next++;
  42. *owned = 0;
  43. return top - PROC_STACK_SIZE;
  44. }
  45. return 0;
  46. }
  47. static void name_copy(char *dst, const char *src)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < PROC_NAME_LEN - 1 && src[i]; i++)
  51. dst[i] = src[i];
  52. dst[i] = '\0';
  53. }
  54. /* Double the process table. Called with interrupts disabled. */
  55. static int grow_proc_table(void)
  56. {
  57. unsigned int new_cap = proc_cap * 2;
  58. struct process *np =
  59. (struct process *)kmalloc(new_cap * sizeof(struct process));
  60. unsigned int i;
  61. if (!np)
  62. return 0;
  63. for (i = 0; i < proc_cap; i++)
  64. np[i] = procs[i];
  65. for (i = proc_cap; i < new_cap; i++)
  66. np[i].state = PROC_UNUSED;
  67. kfree(procs);
  68. procs = np;
  69. proc_cap = new_cap;
  70. return 1;
  71. }
  72. /* Find a free process slot (1..proc_cap-1), growing the table if needed.
  73. * Slot 0 is the window manager and is never returned. Called with interrupts
  74. * disabled. Returns the slot index, or -1 on failure. */
  75. static int alloc_slot(void)
  76. {
  77. unsigned int slot;
  78. for (slot = 1; slot < proc_cap; slot++)
  79. if (procs[slot].state == PROC_UNUSED)
  80. return (int)slot;
  81. if (!grow_proc_table())
  82. return -1;
  83. for (slot = 1; slot < proc_cap; slot++)
  84. if (procs[slot].state == PROC_UNUSED)
  85. return (int)slot;
  86. return -1;
  87. }
  88. void process_init(void)
  89. {
  90. unsigned int i;
  91. procs = (struct process *)kmalloc(INITIAL_PROC_CAP * sizeof(struct process));
  92. if (!procs) {
  93. serial_puts(SERIAL_COM1, "process_init: out of memory\n");
  94. for (;;)
  95. __asm__ volatile ("cli; hlt");
  96. }
  97. proc_cap = INITIAL_PROC_CAP;
  98. for (i = 0; i < proc_cap; i++)
  99. procs[i].state = PROC_UNUSED;
  100. procs[0].state = PROC_RUNNING;
  101. procs[0].pid = 0;
  102. procs[0].esp = 0;
  103. procs[0].screen = (void *)0;
  104. procs[0].stack_base = 0; /* bootstrap stack, never freed */
  105. procs[0].cr3 = paging_current_cr3();
  106. procs[0].kstack_top = 0x30800; /* bootstrap stack top */
  107. name_copy(procs[0].name, "wm");
  108. current_pid = 0;
  109. }
  110. /*
  111. * Build initial stack frame for a new process so that popa + iret
  112. * "returns" to the entry point. For arg-taking entries the argument
  113. * string is copied onto the stack and a pointer to it passed as the
  114. * first C parameter.
  115. *
  116. * Stack layout (high to low):
  117. *
  118. * [copied arg string, if any, rounded up to 4-byte boundary]
  119. * arg pointer -- first C parameter (only if has_arg)
  120. * process_exit address -- return address seen by the entry function
  121. * EFLAGS (0x202) -- IF=1
  122. * CS (0x08)
  123. * EIP (entry) -- iret target
  124. * EAX ECX EDX EBX ESP_orig EBP ESI EDI (pusha frame)
  125. * <- saved ESP
  126. */
  127. static int setup_process(const char *name, unsigned int entry,
  128. int has_arg, const char *arg)
  129. {
  130. int slot, owned;
  131. unsigned int base, top, *sp, arg_ptr = 0, cr3;
  132. unsigned int flags;
  133. flags = irq_save_disable();
  134. slot = alloc_slot();
  135. if (slot < 0) {
  136. irq_restore(flags);
  137. return -1;
  138. }
  139. cr3 = paging_new_directory();
  140. if (!cr3) {
  141. irq_restore(flags);
  142. return -1;
  143. }
  144. base = stack_alloc(&owned);
  145. if (!base) {
  146. mm_free_page(cr3);
  147. irq_restore(flags);
  148. return -1;
  149. }
  150. top = base + PROC_STACK_SIZE;
  151. sp = (unsigned int *)top;
  152. if (has_arg && arg) {
  153. unsigned int len, aligned, i;
  154. char *dst;
  155. for (len = 0; arg[len]; len++)
  156. ;
  157. len++;
  158. aligned = (len + 3) & ~3u;
  159. sp = (unsigned int *)((unsigned int)sp - aligned);
  160. dst = (char *)sp;
  161. for (i = 0; i < len; i++)
  162. dst[i] = arg[i];
  163. for (; i < aligned; i++)
  164. dst[i] = '\0';
  165. arg_ptr = (unsigned int)sp;
  166. }
  167. if (has_arg)
  168. *--sp = arg_ptr;
  169. *--sp = (unsigned int)process_exit;
  170. *--sp = 0x202; /* EFLAGS: IF=1 */
  171. *--sp = 0x08; /* CS */
  172. *--sp = entry; /* EIP */
  173. *--sp = 0; /* EAX */
  174. *--sp = 0; /* ECX */
  175. *--sp = 0; /* EDX */
  176. *--sp = 0; /* EBX */
  177. *--sp = 0; /* ESP (ignored by popa) */
  178. *--sp = 0; /* EBP */
  179. *--sp = 0; /* ESI */
  180. *--sp = 0; /* EDI */
  181. procs[slot].esp = (unsigned int)sp;
  182. procs[slot].state = PROC_READY;
  183. procs[slot].pid = next_pid_counter++;
  184. procs[slot].stack_base = owned ? base : 0;
  185. procs[slot].cr3 = cr3;
  186. procs[slot].kstack_top = top;
  187. name_copy(procs[slot].name, name);
  188. /* Child inherits the parent's output screen (command output lands in the
  189. terminal that spawned it). */
  190. procs[slot].screen = procs[current_pid].screen;
  191. irq_restore(flags);
  192. return (int)procs[slot].pid;
  193. }
  194. int process_create(const char *name, void (*entry)(void))
  195. {
  196. return setup_process(name, (unsigned int)entry, 0, (void *)0);
  197. }
  198. int process_create_arg(const char *name, void (*entry)(const char *),
  199. const char *arg)
  200. {
  201. return setup_process(name, (unsigned int)entry, 1, arg);
  202. }
  203. /* Create a process whose entry takes a raw pointer argument (no string copy). */
  204. int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
  205. {
  206. int slot, owned;
  207. unsigned int base, top, *sp, cr3;
  208. unsigned int flags;
  209. flags = irq_save_disable();
  210. slot = alloc_slot();
  211. if (slot < 0) {
  212. irq_restore(flags);
  213. return -1;
  214. }
  215. cr3 = paging_new_directory();
  216. if (!cr3) {
  217. irq_restore(flags);
  218. return -1;
  219. }
  220. base = stack_alloc(&owned);
  221. if (!base) {
  222. mm_free_page(cr3);
  223. irq_restore(flags);
  224. return -1;
  225. }
  226. top = base + PROC_STACK_SIZE;
  227. sp = (unsigned int *)top;
  228. *--sp = (unsigned int)arg; /* first C parameter */
  229. *--sp = (unsigned int)process_exit;/* return address */
  230. *--sp = 0x202; /* EFLAGS: IF=1 */
  231. *--sp = 0x08; /* CS */
  232. *--sp = (unsigned int)entry; /* EIP */
  233. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  234. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  235. procs[slot].esp = (unsigned int)sp;
  236. procs[slot].state = PROC_READY;
  237. procs[slot].pid = next_pid_counter++;
  238. procs[slot].stack_base = owned ? base : 0;
  239. procs[slot].cr3 = cr3;
  240. procs[slot].kstack_top = top;
  241. name_copy(procs[slot].name, name);
  242. procs[slot].screen = procs[current_pid].screen;
  243. irq_restore(flags);
  244. return (int)procs[slot].pid;
  245. }
  246. /* Load an ELF image into a fresh address space and run it as a ring-3 process. */
  247. int process_create_user(const char *name, const void *image, unsigned int size)
  248. {
  249. int slot, owned;
  250. unsigned int base, top, *sp, cr3, entry;
  251. unsigned int flags;
  252. int i;
  253. flags = irq_save_disable();
  254. slot = alloc_slot();
  255. if (slot < 0) {
  256. irq_restore(flags);
  257. return -1;
  258. }
  259. cr3 = paging_new_directory();
  260. if (!cr3) {
  261. irq_restore(flags);
  262. return -1;
  263. }
  264. entry = elf_load(cr3, image, size);
  265. if (!entry) {
  266. mm_free_page(cr3);
  267. irq_restore(flags);
  268. return -1;
  269. }
  270. /* Map the user stack just below USER_STACK_TOP. */
  271. for (i = 0; i < USER_STACK_PAGES; i++) {
  272. unsigned int phys = mm_alloc_page();
  273. unsigned int va = USER_STACK_TOP - (unsigned int)(i + 1) * 0x1000;
  274. if (!phys ||
  275. !paging_map_page(cr3, va, phys,
  276. PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
  277. mm_free_page(cr3);
  278. irq_restore(flags);
  279. return -1;
  280. }
  281. }
  282. base = stack_alloc(&owned);
  283. if (!base) {
  284. mm_free_page(cr3);
  285. irq_restore(flags);
  286. return -1;
  287. }
  288. top = base + PROC_STACK_SIZE;
  289. sp = (unsigned int *)top;
  290. /* Initial frame so that popa + iret drops us into ring-3 at `entry`. */
  291. *--sp = 0x23; /* SS: user data */
  292. *--sp = USER_STACK_TOP; /* ESP: user stack */
  293. *--sp = 0x202; /* EFLAGS: IF=1 */
  294. *--sp = 0x1B; /* CS: user code */
  295. *--sp = entry; /* EIP */
  296. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  297. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  298. procs[slot].esp = (unsigned int)sp;
  299. procs[slot].state = PROC_READY;
  300. procs[slot].pid = next_pid_counter++;
  301. procs[slot].stack_base = owned ? base : 0;
  302. procs[slot].cr3 = cr3;
  303. procs[slot].kstack_top = top;
  304. name_copy(procs[slot].name, name);
  305. procs[slot].screen = procs[current_pid].screen;
  306. irq_restore(flags);
  307. return (int)procs[slot].pid;
  308. }
  309. /* Set the output screen of the currently-running process (used by a terminal
  310. at startup to bind itself to its own screen). */
  311. void process_set_screen(struct text_screen *screen)
  312. {
  313. procs[current_pid].screen = screen;
  314. }
  315. void process_exit(void)
  316. {
  317. procs[current_pid].state = PROC_DEAD;
  318. for (;;)
  319. __asm__ volatile ("hlt");
  320. }
  321. /*
  322. * Round-robin scheduler. Called from the PIT IRQ stub with interrupts
  323. * disabled. Picks the next runnable process and updates current_pid.
  324. * The caller (switch.S) takes care of actually switching ESP.
  325. */
  326. void schedule(void)
  327. {
  328. unsigned int i, next;
  329. /* Reap dead processes, freeing their stacks. Skip the current slot: we
  330. * may be running on its stack right now (it just called process_exit),
  331. * so it is reaped on a later schedule() from another process's context. */
  332. for (i = 0; i < proc_cap; i++) {
  333. if (procs[i].state == PROC_DEAD && i != current_pid) {
  334. if (procs[i].stack_base)
  335. mm_free_page(procs[i].stack_base);
  336. if (procs[i].cr3 && procs[i].cr3 != paging_current_cr3())
  337. mm_free_page(procs[i].cr3);
  338. procs[i].stack_base = 0;
  339. procs[i].cr3 = 0;
  340. procs[i].state = PROC_UNUSED;
  341. }
  342. }
  343. /* Mark current as READY (it was RUNNING). */
  344. if (procs[current_pid].state == PROC_RUNNING)
  345. procs[current_pid].state = PROC_READY;
  346. /* Find next runnable slot. */
  347. next = current_pid;
  348. for (i = 0; i < proc_cap; i++) {
  349. next = (next + 1) % proc_cap;
  350. if (procs[next].state == PROC_READY) {
  351. procs[next].state = PROC_RUNNING;
  352. current_pid = next;
  353. return;
  354. }
  355. }
  356. /* Nothing else runnable -- stay on current if still alive. */
  357. if (procs[current_pid].state == PROC_READY) {
  358. procs[current_pid].state = PROC_RUNNING;
  359. return;
  360. }
  361. /* Everything dead: fall back to slot 0 (window manager). */
  362. procs[0].state = PROC_RUNNING;
  363. current_pid = 0;
  364. }
  365. /* ---- user-facing helpers ---- */
  366. static const char *state_str(unsigned int s)
  367. {
  368. switch (s) {
  369. case PROC_RUNNING: return "run ";
  370. case PROC_READY: return "ready";
  371. default: return "? ";
  372. }
  373. }
  374. static void put_dec(unsigned int n)
  375. {
  376. char buf[12];
  377. unsigned int i = sizeof(buf) - 1;
  378. buf[i] = '\0';
  379. if (n == 0) {
  380. io_putchar('0');
  381. return;
  382. }
  383. while (n && i > 0) {
  384. buf[--i] = (char)('0' + (n % 10));
  385. n /= 10;
  386. }
  387. io_putstr(buf + i);
  388. }
  389. void process_ps(void)
  390. {
  391. unsigned int i;
  392. io_putstr("PID STATE NAME\n");
  393. for (i = 0; i < proc_cap; i++) {
  394. if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
  395. continue;
  396. io_putchar(' ');
  397. put_dec(procs[i].pid);
  398. io_putstr(" ");
  399. io_putstr(state_str(procs[i].state));
  400. io_putstr(" ");
  401. io_putstr(procs[i].name);
  402. io_putchar('\n');
  403. }
  404. }
  405. int process_kill(unsigned int pid)
  406. {
  407. unsigned int i;
  408. if (pid == 0)
  409. return -1; /* cannot kill window manager */
  410. for (i = 1; i < proc_cap; i++) {
  411. if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
  412. procs[i].pid == pid) {
  413. procs[i].state = PROC_DEAD;
  414. return 0;
  415. }
  416. }
  417. return -1;
  418. }
  419. /* Return 1 if a process with `pid` is still runnable (READY/RUNNING). */
  420. int process_is_alive(unsigned int pid)
  421. {
  422. unsigned int i;
  423. for (i = 0; i < proc_cap; i++)
  424. if (procs[i].pid == pid &&
  425. (procs[i].state == PROC_READY ||
  426. procs[i].state == PROC_RUNNING))
  427. return 1;
  428. return 0;
  429. }
  430. /* Wait (yield) until the process with `pid` has exited. The scheduler
  431. * preempts us every timeslice so the child runs and process_exit() reaps
  432. * it to PROC_UNUSED. */
  433. void process_wait(unsigned int pid)
  434. {
  435. while (process_is_alive(pid))
  436. cpu_idle();
  437. }