| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /* Process management: create, exit, round-robin schedule, ps, kill. */
- #include "process.h"
- #include "io.h"
- struct process procs[MAX_PROCS];
- volatile unsigned int current_pid;
- static unsigned int next_pid_counter = 1; /* monotonic, wraps */
- /* Stack top for process slot (1..MAX_PROCS-1). Slot 0 uses bootstrap stack. */
- static unsigned int stack_top_for(unsigned int slot)
- {
- return PROC_STACK_BASE + slot * PROC_STACK_SIZE;
- }
- static void name_copy(char *dst, const char *src)
- {
- unsigned int i;
- for (i = 0; i < PROC_NAME_LEN - 1 && src[i]; i++)
- dst[i] = src[i];
- dst[i] = '\0';
- }
- void process_init(void)
- {
- unsigned int i;
- for (i = 0; i < MAX_PROCS; i++)
- procs[i].state = PROC_UNUSED;
- procs[0].state = PROC_RUNNING;
- procs[0].pid = 0;
- name_copy(procs[0].name, "terminal");
- current_pid = 0;
- }
- /*
- * Build initial stack frame for a new process so that popa + iret
- * "returns" to the entry point. For arg-taking entries the argument
- * string is copied onto the stack and a pointer to it passed as the
- * first C parameter.
- *
- * Stack layout (high to low):
- *
- * [copied arg string, if any, rounded up to 4-byte boundary]
- * arg pointer -- first C parameter (only if has_arg)
- * process_exit address -- return address seen by the entry function
- * EFLAGS (0x202) -- IF=1
- * CS (0x08)
- * EIP (entry) -- iret target
- * EAX ECX EDX EBX ESP_orig EBP ESI EDI (pusha frame)
- * <- saved ESP
- */
- static int setup_process(const char *name, unsigned int entry,
- int has_arg, const char *arg)
- {
- unsigned int slot, top, *sp, arg_ptr = 0;
- for (slot = 1; slot < MAX_PROCS; slot++)
- if (procs[slot].state == PROC_UNUSED)
- break;
- if (slot == MAX_PROCS)
- return -1;
- top = stack_top_for(slot);
- sp = (unsigned int *)top;
- if (has_arg && arg) {
- unsigned int len, aligned, i;
- char *dst;
- for (len = 0; arg[len]; len++)
- ;
- len++;
- aligned = (len + 3) & ~3u;
- sp = (unsigned int *)((unsigned int)sp - aligned);
- dst = (char *)sp;
- for (i = 0; i < len; i++)
- dst[i] = arg[i];
- for (; i < aligned; i++)
- dst[i] = '\0';
- arg_ptr = (unsigned int)sp;
- }
- if (has_arg)
- *--sp = arg_ptr;
- *--sp = (unsigned int)process_exit;
- *--sp = 0x202; /* EFLAGS: IF=1 */
- *--sp = 0x08; /* CS */
- *--sp = entry; /* EIP */
- *--sp = 0; /* EAX */
- *--sp = 0; /* ECX */
- *--sp = 0; /* EDX */
- *--sp = 0; /* EBX */
- *--sp = 0; /* ESP (ignored by popa) */
- *--sp = 0; /* EBP */
- *--sp = 0; /* ESI */
- *--sp = 0; /* EDI */
- procs[slot].esp = (unsigned int)sp;
- procs[slot].state = PROC_READY;
- procs[slot].pid = next_pid_counter++;
- name_copy(procs[slot].name, name);
- return (int)procs[slot].pid;
- }
- int process_create(const char *name, void (*entry)(void))
- {
- return setup_process(name, (unsigned int)entry, 0, (void *)0);
- }
- int process_create_arg(const char *name, void (*entry)(const char *),
- const char *arg)
- {
- return setup_process(name, (unsigned int)entry, 1, arg);
- }
- void process_exit(void)
- {
- procs[current_pid].state = PROC_DEAD;
- for (;;)
- __asm__ volatile ("hlt");
- }
- /*
- * Round-robin scheduler. Called from the PIT IRQ stub with interrupts
- * disabled. Picks the next runnable process and updates current_pid.
- * The caller (switch.S) takes care of actually switching ESP.
- */
- void schedule(void)
- {
- unsigned int i, next;
- /* Clean up dead processes. */
- for (i = 0; i < MAX_PROCS; i++)
- if (procs[i].state == PROC_DEAD)
- procs[i].state = PROC_UNUSED;
- /* Mark current as READY (it was RUNNING). */
- if (procs[current_pid].state == PROC_RUNNING)
- procs[current_pid].state = PROC_READY;
- /* Find next runnable slot. */
- next = current_pid;
- for (i = 0; i < MAX_PROCS; i++) {
- next = (next + 1) % MAX_PROCS;
- if (procs[next].state == PROC_READY) {
- procs[next].state = PROC_RUNNING;
- current_pid = next;
- return;
- }
- }
- /* Nothing else runnable -- stay on current if still alive. */
- if (procs[current_pid].state == PROC_READY) {
- procs[current_pid].state = PROC_RUNNING;
- return;
- }
- /* Everything dead: fall back to slot 0 (terminal). */
- procs[0].state = PROC_RUNNING;
- current_pid = 0;
- }
- /* ---- user-facing helpers ---- */
- static const char *state_str(unsigned int s)
- {
- switch (s) {
- case PROC_RUNNING: return "run ";
- case PROC_READY: return "ready";
- default: return "? ";
- }
- }
- void process_ps(void)
- {
- unsigned int i;
- io_putstr("PID STATE NAME\n");
- for (i = 0; i < MAX_PROCS; i++) {
- if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
- continue;
- io_putchar(' ');
- if (procs[i].pid < 10)
- io_putchar(' ');
- if (procs[i].pid >= 10)
- io_putchar((char)('0' + procs[i].pid / 10));
- io_putchar((char)('0' + procs[i].pid % 10));
- io_putstr(" ");
- io_putstr(state_str(procs[i].state));
- io_putstr(" ");
- io_putstr(procs[i].name);
- io_putchar('\n');
- }
- }
- int process_kill(unsigned int pid)
- {
- unsigned int i;
- if (pid == 0)
- return -1; /* cannot kill terminal */
- for (i = 1; i < MAX_PROCS; i++) {
- if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
- procs[i].pid == pid) {
- procs[i].state = PROC_DEAD;
- return 0;
- }
- }
- return -1;
- }
|