process.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Process management: create, exit, round-robin schedule, ps, kill. */
  2. #include "process.h"
  3. #include "idle.h"
  4. #include "io.h"
  5. struct process procs[MAX_PROCS];
  6. volatile unsigned int current_pid;
  7. static unsigned int next_pid_counter = 1; /* monotonic, wraps */
  8. /* Stack top for process slot (1..MAX_PROCS-1). Slot 0 uses bootstrap stack. */
  9. static unsigned int stack_top_for(unsigned int slot)
  10. {
  11. return PROC_STACK_BASE + slot * PROC_STACK_SIZE;
  12. }
  13. static void name_copy(char *dst, const char *src)
  14. {
  15. unsigned int i;
  16. for (i = 0; i < PROC_NAME_LEN - 1 && src[i]; i++)
  17. dst[i] = src[i];
  18. dst[i] = '\0';
  19. }
  20. void process_init(void)
  21. {
  22. unsigned int i;
  23. for (i = 0; i < MAX_PROCS; i++)
  24. procs[i].state = PROC_UNUSED;
  25. procs[0].state = PROC_RUNNING;
  26. procs[0].pid = 0;
  27. procs[0].screen = (void *)0;
  28. name_copy(procs[0].name, "wm");
  29. current_pid = 0;
  30. }
  31. /*
  32. * Build initial stack frame for a new process so that popa + iret
  33. * "returns" to the entry point. For arg-taking entries the argument
  34. * string is copied onto the stack and a pointer to it passed as the
  35. * first C parameter.
  36. *
  37. * Stack layout (high to low):
  38. *
  39. * [copied arg string, if any, rounded up to 4-byte boundary]
  40. * arg pointer -- first C parameter (only if has_arg)
  41. * process_exit address -- return address seen by the entry function
  42. * EFLAGS (0x202) -- IF=1
  43. * CS (0x08)
  44. * EIP (entry) -- iret target
  45. * EAX ECX EDX EBX ESP_orig EBP ESI EDI (pusha frame)
  46. * <- saved ESP
  47. */
  48. static int setup_process(const char *name, unsigned int entry,
  49. int has_arg, const char *arg)
  50. {
  51. unsigned int slot, top, *sp, arg_ptr = 0;
  52. for (slot = 1; slot < MAX_PROCS; slot++)
  53. if (procs[slot].state == PROC_UNUSED)
  54. break;
  55. if (slot == MAX_PROCS)
  56. return -1;
  57. top = stack_top_for(slot);
  58. sp = (unsigned int *)top;
  59. if (has_arg && arg) {
  60. unsigned int len, aligned, i;
  61. char *dst;
  62. for (len = 0; arg[len]; len++)
  63. ;
  64. len++;
  65. aligned = (len + 3) & ~3u;
  66. sp = (unsigned int *)((unsigned int)sp - aligned);
  67. dst = (char *)sp;
  68. for (i = 0; i < len; i++)
  69. dst[i] = arg[i];
  70. for (; i < aligned; i++)
  71. dst[i] = '\0';
  72. arg_ptr = (unsigned int)sp;
  73. }
  74. if (has_arg)
  75. *--sp = arg_ptr;
  76. *--sp = (unsigned int)process_exit;
  77. *--sp = 0x202; /* EFLAGS: IF=1 */
  78. *--sp = 0x08; /* CS */
  79. *--sp = entry; /* EIP */
  80. *--sp = 0; /* EAX */
  81. *--sp = 0; /* ECX */
  82. *--sp = 0; /* EDX */
  83. *--sp = 0; /* EBX */
  84. *--sp = 0; /* ESP (ignored by popa) */
  85. *--sp = 0; /* EBP */
  86. *--sp = 0; /* ESI */
  87. *--sp = 0; /* EDI */
  88. procs[slot].esp = (unsigned int)sp;
  89. procs[slot].state = PROC_READY;
  90. procs[slot].pid = next_pid_counter++;
  91. name_copy(procs[slot].name, name);
  92. /* Child inherits the parent's output screen (command output lands in the
  93. terminal that spawned it). */
  94. procs[slot].screen = procs[current_pid].screen;
  95. return (int)procs[slot].pid;
  96. }
  97. int process_create(const char *name, void (*entry)(void))
  98. {
  99. return setup_process(name, (unsigned int)entry, 0, (void *)0);
  100. }
  101. int process_create_arg(const char *name, void (*entry)(const char *),
  102. const char *arg)
  103. {
  104. return setup_process(name, (unsigned int)entry, 1, arg);
  105. }
  106. /* Create a process whose entry takes a raw pointer argument (no string copy). */
  107. int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
  108. {
  109. unsigned int slot, top, *sp;
  110. for (slot = 1; slot < MAX_PROCS; slot++)
  111. if (procs[slot].state == PROC_UNUSED)
  112. break;
  113. if (slot == MAX_PROCS)
  114. return -1;
  115. top = stack_top_for(slot);
  116. sp = (unsigned int *)top;
  117. *--sp = (unsigned int)arg; /* first C parameter */
  118. *--sp = (unsigned int)process_exit;/* return address */
  119. *--sp = 0x202; /* EFLAGS: IF=1 */
  120. *--sp = 0x08; /* CS */
  121. *--sp = (unsigned int)entry; /* EIP */
  122. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
  123. *--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
  124. procs[slot].esp = (unsigned int)sp;
  125. procs[slot].state = PROC_READY;
  126. procs[slot].pid = next_pid_counter++;
  127. name_copy(procs[slot].name, name);
  128. procs[slot].screen = procs[current_pid].screen;
  129. return (int)procs[slot].pid;
  130. }
  131. /* Set the output screen of the currently-running process (used by a terminal
  132. at startup to bind itself to its own screen). */
  133. void process_set_screen(struct text_screen *screen)
  134. {
  135. procs[current_pid].screen = screen;
  136. }
  137. void process_exit(void)
  138. {
  139. procs[current_pid].state = PROC_DEAD;
  140. for (;;)
  141. __asm__ volatile ("hlt");
  142. }
  143. /*
  144. * Round-robin scheduler. Called from the PIT IRQ stub with interrupts
  145. * disabled. Picks the next runnable process and updates current_pid.
  146. * The caller (switch.S) takes care of actually switching ESP.
  147. */
  148. void schedule(void)
  149. {
  150. unsigned int i, next;
  151. /* Clean up dead processes. */
  152. for (i = 0; i < MAX_PROCS; i++)
  153. if (procs[i].state == PROC_DEAD)
  154. procs[i].state = PROC_UNUSED;
  155. /* Mark current as READY (it was RUNNING). */
  156. if (procs[current_pid].state == PROC_RUNNING)
  157. procs[current_pid].state = PROC_READY;
  158. /* Find next runnable slot. */
  159. next = current_pid;
  160. for (i = 0; i < MAX_PROCS; i++) {
  161. next = (next + 1) % MAX_PROCS;
  162. if (procs[next].state == PROC_READY) {
  163. procs[next].state = PROC_RUNNING;
  164. current_pid = next;
  165. return;
  166. }
  167. }
  168. /* Nothing else runnable -- stay on current if still alive. */
  169. if (procs[current_pid].state == PROC_READY) {
  170. procs[current_pid].state = PROC_RUNNING;
  171. return;
  172. }
  173. /* Everything dead: fall back to slot 0 (terminal). */
  174. procs[0].state = PROC_RUNNING;
  175. current_pid = 0;
  176. }
  177. /* ---- user-facing helpers ---- */
  178. static const char *state_str(unsigned int s)
  179. {
  180. switch (s) {
  181. case PROC_RUNNING: return "run ";
  182. case PROC_READY: return "ready";
  183. default: return "? ";
  184. }
  185. }
  186. void process_ps(void)
  187. {
  188. unsigned int i;
  189. io_putstr("PID STATE NAME\n");
  190. for (i = 0; i < MAX_PROCS; i++) {
  191. if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
  192. continue;
  193. io_putchar(' ');
  194. if (procs[i].pid < 10)
  195. io_putchar(' ');
  196. if (procs[i].pid >= 10)
  197. io_putchar((char)('0' + procs[i].pid / 10));
  198. io_putchar((char)('0' + procs[i].pid % 10));
  199. io_putstr(" ");
  200. io_putstr(state_str(procs[i].state));
  201. io_putstr(" ");
  202. io_putstr(procs[i].name);
  203. io_putchar('\n');
  204. }
  205. }
  206. int process_kill(unsigned int pid)
  207. {
  208. unsigned int i;
  209. if (pid == 0)
  210. return -1; /* cannot kill terminal */
  211. for (i = 1; i < MAX_PROCS; i++) {
  212. if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
  213. procs[i].pid == pid) {
  214. procs[i].state = PROC_DEAD;
  215. return 0;
  216. }
  217. }
  218. return -1;
  219. }
  220. /* Wait (yield) until the process with `pid` has exited. The scheduler
  221. * preempts us every timeslice so the child runs and process_exit() reaps
  222. * it to PROC_UNUSED. */
  223. void process_wait(unsigned int pid)
  224. {
  225. unsigned int i;
  226. for (;;) {
  227. int alive = 0;
  228. for (i = 0; i < MAX_PROCS; i++)
  229. if (procs[i].pid == pid &&
  230. (procs[i].state == PROC_READY ||
  231. procs[i].state == PROC_RUNNING))
  232. alive = 1;
  233. if (!alive)
  234. return;
  235. cpu_idle();
  236. }
  237. }