process.c 10 KB

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