process.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. name_copy(procs[0].name, "terminal");
  27. current_pid = 0;
  28. }
  29. /*
  30. * Build initial stack frame for a new process so that popa + iret
  31. * "returns" to the entry point. For arg-taking entries the argument
  32. * string is copied onto the stack and a pointer to it passed as the
  33. * first C parameter.
  34. *
  35. * Stack layout (high to low):
  36. *
  37. * [copied arg string, if any, rounded up to 4-byte boundary]
  38. * arg pointer -- first C parameter (only if has_arg)
  39. * process_exit address -- return address seen by the entry function
  40. * EFLAGS (0x202) -- IF=1
  41. * CS (0x08)
  42. * EIP (entry) -- iret target
  43. * EAX ECX EDX EBX ESP_orig EBP ESI EDI (pusha frame)
  44. * <- saved ESP
  45. */
  46. static int setup_process(const char *name, unsigned int entry,
  47. int has_arg, const char *arg)
  48. {
  49. unsigned int slot, top, *sp, arg_ptr = 0;
  50. for (slot = 1; slot < MAX_PROCS; slot++)
  51. if (procs[slot].state == PROC_UNUSED)
  52. break;
  53. if (slot == MAX_PROCS)
  54. return -1;
  55. top = stack_top_for(slot);
  56. sp = (unsigned int *)top;
  57. if (has_arg && arg) {
  58. unsigned int len, aligned, i;
  59. char *dst;
  60. for (len = 0; arg[len]; len++)
  61. ;
  62. len++;
  63. aligned = (len + 3) & ~3u;
  64. sp = (unsigned int *)((unsigned int)sp - aligned);
  65. dst = (char *)sp;
  66. for (i = 0; i < len; i++)
  67. dst[i] = arg[i];
  68. for (; i < aligned; i++)
  69. dst[i] = '\0';
  70. arg_ptr = (unsigned int)sp;
  71. }
  72. if (has_arg)
  73. *--sp = arg_ptr;
  74. *--sp = (unsigned int)process_exit;
  75. *--sp = 0x202; /* EFLAGS: IF=1 */
  76. *--sp = 0x08; /* CS */
  77. *--sp = entry; /* EIP */
  78. *--sp = 0; /* EAX */
  79. *--sp = 0; /* ECX */
  80. *--sp = 0; /* EDX */
  81. *--sp = 0; /* EBX */
  82. *--sp = 0; /* ESP (ignored by popa) */
  83. *--sp = 0; /* EBP */
  84. *--sp = 0; /* ESI */
  85. *--sp = 0; /* EDI */
  86. procs[slot].esp = (unsigned int)sp;
  87. procs[slot].state = PROC_READY;
  88. procs[slot].pid = next_pid_counter++;
  89. name_copy(procs[slot].name, name);
  90. return (int)procs[slot].pid;
  91. }
  92. int process_create(const char *name, void (*entry)(void))
  93. {
  94. return setup_process(name, (unsigned int)entry, 0, (void *)0);
  95. }
  96. int process_create_arg(const char *name, void (*entry)(const char *),
  97. const char *arg)
  98. {
  99. return setup_process(name, (unsigned int)entry, 1, arg);
  100. }
  101. void process_exit(void)
  102. {
  103. procs[current_pid].state = PROC_DEAD;
  104. for (;;)
  105. __asm__ volatile ("hlt");
  106. }
  107. /*
  108. * Round-robin scheduler. Called from the PIT IRQ stub with interrupts
  109. * disabled. Picks the next runnable process and updates current_pid.
  110. * The caller (switch.S) takes care of actually switching ESP.
  111. */
  112. void schedule(void)
  113. {
  114. unsigned int i, next;
  115. /* Clean up dead processes. */
  116. for (i = 0; i < MAX_PROCS; i++)
  117. if (procs[i].state == PROC_DEAD)
  118. procs[i].state = PROC_UNUSED;
  119. /* Mark current as READY (it was RUNNING). */
  120. if (procs[current_pid].state == PROC_RUNNING)
  121. procs[current_pid].state = PROC_READY;
  122. /* Find next runnable slot. */
  123. next = current_pid;
  124. for (i = 0; i < MAX_PROCS; i++) {
  125. next = (next + 1) % MAX_PROCS;
  126. if (procs[next].state == PROC_READY) {
  127. procs[next].state = PROC_RUNNING;
  128. current_pid = next;
  129. return;
  130. }
  131. }
  132. /* Nothing else runnable -- stay on current if still alive. */
  133. if (procs[current_pid].state == PROC_READY) {
  134. procs[current_pid].state = PROC_RUNNING;
  135. return;
  136. }
  137. /* Everything dead: fall back to slot 0 (terminal). */
  138. procs[0].state = PROC_RUNNING;
  139. current_pid = 0;
  140. }
  141. /* ---- user-facing helpers ---- */
  142. static const char *state_str(unsigned int s)
  143. {
  144. switch (s) {
  145. case PROC_RUNNING: return "run ";
  146. case PROC_READY: return "ready";
  147. default: return "? ";
  148. }
  149. }
  150. void process_ps(void)
  151. {
  152. unsigned int i;
  153. io_putstr("PID STATE NAME\n");
  154. for (i = 0; i < MAX_PROCS; i++) {
  155. if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
  156. continue;
  157. io_putchar(' ');
  158. if (procs[i].pid < 10)
  159. io_putchar(' ');
  160. if (procs[i].pid >= 10)
  161. io_putchar((char)('0' + procs[i].pid / 10));
  162. io_putchar((char)('0' + procs[i].pid % 10));
  163. io_putstr(" ");
  164. io_putstr(state_str(procs[i].state));
  165. io_putstr(" ");
  166. io_putstr(procs[i].name);
  167. io_putchar('\n');
  168. }
  169. }
  170. int process_kill(unsigned int pid)
  171. {
  172. unsigned int i;
  173. if (pid == 0)
  174. return -1; /* cannot kill terminal */
  175. for (i = 1; i < MAX_PROCS; i++) {
  176. if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
  177. procs[i].pid == pid) {
  178. procs[i].state = PROC_DEAD;
  179. return 0;
  180. }
  181. }
  182. return -1;
  183. }