process.c 6.5 KB

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