|
@@ -3,16 +3,51 @@
|
|
|
#include "process.h"
|
|
#include "process.h"
|
|
|
#include "idle.h"
|
|
#include "idle.h"
|
|
|
#include "io.h"
|
|
#include "io.h"
|
|
|
|
|
+#include "kmalloc.h"
|
|
|
|
|
+#include "mm.h"
|
|
|
|
|
+#include "serial.h"
|
|
|
|
|
|
|
|
-struct process procs[MAX_PROCS];
|
|
|
|
|
|
|
+struct process *procs; /* dynamically-sized process table */
|
|
|
volatile unsigned int current_pid;
|
|
volatile unsigned int current_pid;
|
|
|
|
|
|
|
|
|
|
+static unsigned int proc_cap; /* current capacity of procs[] */
|
|
|
static unsigned int next_pid_counter = 1; /* monotonic, wraps */
|
|
static unsigned int next_pid_counter = 1; /* monotonic, wraps */
|
|
|
|
|
+static unsigned int fallback_stack_next; /* 0..PROC_FALLBACK_STACK_MAX */
|
|
|
|
|
|
|
|
-/* Stack top for process slot (1..MAX_PROCS-1). Slot 0 uses bootstrap stack. */
|
|
|
|
|
-static unsigned int stack_top_for(unsigned int slot)
|
|
|
|
|
|
|
+#define INITIAL_PROC_CAP 16
|
|
|
|
|
+
|
|
|
|
|
+/* ---- interrupt flag save/restore (for short critical sections) ---- */
|
|
|
|
|
+static inline unsigned int irq_save_disable(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int flags;
|
|
|
|
|
+ __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
|
|
|
|
|
+ return flags;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static inline void irq_restore(unsigned int flags)
|
|
|
{
|
|
{
|
|
|
- return PROC_STACK_BASE + slot * PROC_STACK_SIZE;
|
|
|
|
|
|
|
+ __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Allocate a process stack and return its BASE address (page-aligned), or 0.
|
|
|
|
|
+ * *owned is set to 1 if it came from the frame allocator (must be freed on
|
|
|
|
|
+ * exit), 0 if it came from the fallback pool. */
|
|
|
|
|
+static unsigned int stack_alloc(int *owned)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int base = mm_alloc_page();
|
|
|
|
|
+ if (base) {
|
|
|
|
|
+ *owned = 1;
|
|
|
|
|
+ return base;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (fallback_stack_next < PROC_FALLBACK_STACK_MAX) {
|
|
|
|
|
+ unsigned int top = PROC_FALLBACK_STACK_BASE
|
|
|
|
|
+ + fallback_stack_next * PROC_STACK_SIZE
|
|
|
|
|
+ + PROC_STACK_SIZE;
|
|
|
|
|
+ fallback_stack_next++;
|
|
|
|
|
+ *owned = 0;
|
|
|
|
|
+ return top - PROC_STACK_SIZE;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void name_copy(char *dst, const char *src)
|
|
static void name_copy(char *dst, const char *src)
|
|
@@ -23,15 +58,62 @@ static void name_copy(char *dst, const char *src)
|
|
|
dst[i] = '\0';
|
|
dst[i] = '\0';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* Double the process table. Called with interrupts disabled. */
|
|
|
|
|
+static int grow_proc_table(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int new_cap = proc_cap * 2;
|
|
|
|
|
+ struct process *np =
|
|
|
|
|
+ (struct process *)kmalloc(new_cap * sizeof(struct process));
|
|
|
|
|
+ unsigned int i;
|
|
|
|
|
+
|
|
|
|
|
+ if (!np)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ for (i = 0; i < proc_cap; i++)
|
|
|
|
|
+ np[i] = procs[i];
|
|
|
|
|
+ for (i = proc_cap; i < new_cap; i++)
|
|
|
|
|
+ np[i].state = PROC_UNUSED;
|
|
|
|
|
+ kfree(procs);
|
|
|
|
|
+ procs = np;
|
|
|
|
|
+ proc_cap = new_cap;
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Find a free process slot (1..proc_cap-1), growing the table if needed.
|
|
|
|
|
+ * Slot 0 is the window manager and is never returned. Called with interrupts
|
|
|
|
|
+ * disabled. Returns the slot index, or -1 on failure. */
|
|
|
|
|
+static int alloc_slot(void)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int slot;
|
|
|
|
|
+ for (slot = 1; slot < proc_cap; slot++)
|
|
|
|
|
+ if (procs[slot].state == PROC_UNUSED)
|
|
|
|
|
+ return (int)slot;
|
|
|
|
|
+ if (!grow_proc_table())
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ for (slot = 1; slot < proc_cap; slot++)
|
|
|
|
|
+ if (procs[slot].state == PROC_UNUSED)
|
|
|
|
|
+ return (int)slot;
|
|
|
|
|
+ return -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void process_init(void)
|
|
void process_init(void)
|
|
|
{
|
|
{
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
|
- for (i = 0; i < MAX_PROCS; i++)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ procs = (struct process *)kmalloc(INITIAL_PROC_CAP * sizeof(struct process));
|
|
|
|
|
+ if (!procs) {
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "process_init: out of memory\n");
|
|
|
|
|
+ for (;;)
|
|
|
|
|
+ __asm__ volatile ("cli; hlt");
|
|
|
|
|
+ }
|
|
|
|
|
+ proc_cap = INITIAL_PROC_CAP;
|
|
|
|
|
+ for (i = 0; i < proc_cap; i++)
|
|
|
procs[i].state = PROC_UNUSED;
|
|
procs[i].state = PROC_UNUSED;
|
|
|
|
|
|
|
|
- procs[0].state = PROC_RUNNING;
|
|
|
|
|
- procs[0].pid = 0;
|
|
|
|
|
- procs[0].screen = (void *)0;
|
|
|
|
|
|
|
+ procs[0].state = PROC_RUNNING;
|
|
|
|
|
+ procs[0].pid = 0;
|
|
|
|
|
+ procs[0].esp = 0;
|
|
|
|
|
+ procs[0].screen = (void *)0;
|
|
|
|
|
+ procs[0].stack_base = 0; /* bootstrap stack, never freed */
|
|
|
name_copy(procs[0].name, "wm");
|
|
name_copy(procs[0].name, "wm");
|
|
|
current_pid = 0;
|
|
current_pid = 0;
|
|
|
}
|
|
}
|
|
@@ -56,16 +138,24 @@ void process_init(void)
|
|
|
static int setup_process(const char *name, unsigned int entry,
|
|
static int setup_process(const char *name, unsigned int entry,
|
|
|
int has_arg, const char *arg)
|
|
int has_arg, const char *arg)
|
|
|
{
|
|
{
|
|
|
- unsigned int slot, top, *sp, arg_ptr = 0;
|
|
|
|
|
|
|
+ int slot, owned;
|
|
|
|
|
+ unsigned int base, top, *sp, arg_ptr = 0;
|
|
|
|
|
+ unsigned int flags;
|
|
|
|
|
|
|
|
- for (slot = 1; slot < MAX_PROCS; slot++)
|
|
|
|
|
- if (procs[slot].state == PROC_UNUSED)
|
|
|
|
|
- break;
|
|
|
|
|
- if (slot == MAX_PROCS)
|
|
|
|
|
- return -1;
|
|
|
|
|
|
|
+ flags = irq_save_disable();
|
|
|
|
|
|
|
|
- top = stack_top_for(slot);
|
|
|
|
|
|
|
+ slot = alloc_slot();
|
|
|
|
|
+ if (slot < 0) {
|
|
|
|
|
+ irq_restore(flags);
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ base = stack_alloc(&owned);
|
|
|
|
|
+ if (!base) {
|
|
|
|
|
+ irq_restore(flags);
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ top = base + PROC_STACK_SIZE;
|
|
|
sp = (unsigned int *)top;
|
|
sp = (unsigned int *)top;
|
|
|
|
|
|
|
|
if (has_arg && arg) {
|
|
if (has_arg && arg) {
|
|
@@ -99,14 +189,16 @@ static int setup_process(const char *name, unsigned int entry,
|
|
|
*--sp = 0; /* ESI */
|
|
*--sp = 0; /* ESI */
|
|
|
*--sp = 0; /* EDI */
|
|
*--sp = 0; /* EDI */
|
|
|
|
|
|
|
|
- procs[slot].esp = (unsigned int)sp;
|
|
|
|
|
- procs[slot].state = PROC_READY;
|
|
|
|
|
- procs[slot].pid = next_pid_counter++;
|
|
|
|
|
|
|
+ procs[slot].esp = (unsigned int)sp;
|
|
|
|
|
+ procs[slot].state = PROC_READY;
|
|
|
|
|
+ procs[slot].pid = next_pid_counter++;
|
|
|
|
|
+ procs[slot].stack_base = owned ? base : 0;
|
|
|
name_copy(procs[slot].name, name);
|
|
name_copy(procs[slot].name, name);
|
|
|
/* Child inherits the parent's output screen (command output lands in the
|
|
/* Child inherits the parent's output screen (command output lands in the
|
|
|
terminal that spawned it). */
|
|
terminal that spawned it). */
|
|
|
procs[slot].screen = procs[current_pid].screen;
|
|
procs[slot].screen = procs[current_pid].screen;
|
|
|
|
|
|
|
|
|
|
+ irq_restore(flags);
|
|
|
return (int)procs[slot].pid;
|
|
return (int)procs[slot].pid;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -124,15 +216,24 @@ int process_create_arg(const char *name, void (*entry)(const char *),
|
|
|
/* Create a process whose entry takes a raw pointer argument (no string copy). */
|
|
/* Create a process whose entry takes a raw pointer argument (no string copy). */
|
|
|
int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
|
|
int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
|
|
|
{
|
|
{
|
|
|
- unsigned int slot, top, *sp;
|
|
|
|
|
|
|
+ int slot, owned;
|
|
|
|
|
+ unsigned int base, top, *sp;
|
|
|
|
|
+ unsigned int flags;
|
|
|
|
|
|
|
|
- for (slot = 1; slot < MAX_PROCS; slot++)
|
|
|
|
|
- if (procs[slot].state == PROC_UNUSED)
|
|
|
|
|
- break;
|
|
|
|
|
- if (slot == MAX_PROCS)
|
|
|
|
|
|
|
+ flags = irq_save_disable();
|
|
|
|
|
+
|
|
|
|
|
+ slot = alloc_slot();
|
|
|
|
|
+ if (slot < 0) {
|
|
|
|
|
+ irq_restore(flags);
|
|
|
return -1;
|
|
return -1;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- top = stack_top_for(slot);
|
|
|
|
|
|
|
+ base = stack_alloc(&owned);
|
|
|
|
|
+ if (!base) {
|
|
|
|
|
+ irq_restore(flags);
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ top = base + PROC_STACK_SIZE;
|
|
|
sp = (unsigned int *)top;
|
|
sp = (unsigned int *)top;
|
|
|
|
|
|
|
|
*--sp = (unsigned int)arg; /* first C parameter */
|
|
*--sp = (unsigned int)arg; /* first C parameter */
|
|
@@ -143,12 +244,14 @@ int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
|
|
|
*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
|
|
*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* EAX ECX EDX EBX */
|
|
|
*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
|
|
*--sp = 0; *--sp = 0; *--sp = 0; *--sp = 0; /* ESP EBP ESI EDI */
|
|
|
|
|
|
|
|
- procs[slot].esp = (unsigned int)sp;
|
|
|
|
|
- procs[slot].state = PROC_READY;
|
|
|
|
|
- procs[slot].pid = next_pid_counter++;
|
|
|
|
|
|
|
+ procs[slot].esp = (unsigned int)sp;
|
|
|
|
|
+ procs[slot].state = PROC_READY;
|
|
|
|
|
+ procs[slot].pid = next_pid_counter++;
|
|
|
|
|
+ procs[slot].stack_base = owned ? base : 0;
|
|
|
name_copy(procs[slot].name, name);
|
|
name_copy(procs[slot].name, name);
|
|
|
procs[slot].screen = procs[current_pid].screen;
|
|
procs[slot].screen = procs[current_pid].screen;
|
|
|
|
|
|
|
|
|
|
+ irq_restore(flags);
|
|
|
return (int)procs[slot].pid;
|
|
return (int)procs[slot].pid;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -175,10 +278,17 @@ void schedule(void)
|
|
|
{
|
|
{
|
|
|
unsigned int i, next;
|
|
unsigned int i, next;
|
|
|
|
|
|
|
|
- /* Clean up dead processes. */
|
|
|
|
|
- for (i = 0; i < MAX_PROCS; i++)
|
|
|
|
|
- if (procs[i].state == PROC_DEAD)
|
|
|
|
|
|
|
+ /* Reap dead processes, freeing their stacks. Skip the current slot: we
|
|
|
|
|
+ * may be running on its stack right now (it just called process_exit),
|
|
|
|
|
+ * so it is reaped on a later schedule() from another process's context. */
|
|
|
|
|
+ for (i = 0; i < proc_cap; i++) {
|
|
|
|
|
+ if (procs[i].state == PROC_DEAD && i != current_pid) {
|
|
|
|
|
+ if (procs[i].stack_base)
|
|
|
|
|
+ mm_free_page(procs[i].stack_base);
|
|
|
|
|
+ procs[i].stack_base = 0;
|
|
|
procs[i].state = PROC_UNUSED;
|
|
procs[i].state = PROC_UNUSED;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/* Mark current as READY (it was RUNNING). */
|
|
/* Mark current as READY (it was RUNNING). */
|
|
|
if (procs[current_pid].state == PROC_RUNNING)
|
|
if (procs[current_pid].state == PROC_RUNNING)
|
|
@@ -186,8 +296,8 @@ void schedule(void)
|
|
|
|
|
|
|
|
/* Find next runnable slot. */
|
|
/* Find next runnable slot. */
|
|
|
next = current_pid;
|
|
next = current_pid;
|
|
|
- for (i = 0; i < MAX_PROCS; i++) {
|
|
|
|
|
- next = (next + 1) % MAX_PROCS;
|
|
|
|
|
|
|
+ for (i = 0; i < proc_cap; i++) {
|
|
|
|
|
+ next = (next + 1) % proc_cap;
|
|
|
if (procs[next].state == PROC_READY) {
|
|
if (procs[next].state == PROC_READY) {
|
|
|
procs[next].state = PROC_RUNNING;
|
|
procs[next].state = PROC_RUNNING;
|
|
|
current_pid = next;
|
|
current_pid = next;
|
|
@@ -201,7 +311,7 @@ void schedule(void)
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /* Everything dead: fall back to slot 0 (terminal). */
|
|
|
|
|
|
|
+ /* Everything dead: fall back to slot 0 (window manager). */
|
|
|
procs[0].state = PROC_RUNNING;
|
|
procs[0].state = PROC_RUNNING;
|
|
|
current_pid = 0;
|
|
current_pid = 0;
|
|
|
}
|
|
}
|
|
@@ -217,19 +327,31 @@ static const char *state_str(unsigned int s)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static void put_dec(unsigned int n)
|
|
|
|
|
+{
|
|
|
|
|
+ char buf[12];
|
|
|
|
|
+ unsigned int i = sizeof(buf) - 1;
|
|
|
|
|
+ buf[i] = '\0';
|
|
|
|
|
+ if (n == 0) {
|
|
|
|
|
+ io_putchar('0');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ while (n && i > 0) {
|
|
|
|
|
+ buf[--i] = (char)('0' + (n % 10));
|
|
|
|
|
+ n /= 10;
|
|
|
|
|
+ }
|
|
|
|
|
+ io_putstr(buf + i);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void process_ps(void)
|
|
void process_ps(void)
|
|
|
{
|
|
{
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
|
io_putstr("PID STATE NAME\n");
|
|
io_putstr("PID STATE NAME\n");
|
|
|
- for (i = 0; i < MAX_PROCS; i++) {
|
|
|
|
|
|
|
+ for (i = 0; i < proc_cap; i++) {
|
|
|
if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
|
|
if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
|
|
|
continue;
|
|
continue;
|
|
|
io_putchar(' ');
|
|
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));
|
|
|
|
|
|
|
+ put_dec(procs[i].pid);
|
|
|
io_putstr(" ");
|
|
io_putstr(" ");
|
|
|
io_putstr(state_str(procs[i].state));
|
|
io_putstr(state_str(procs[i].state));
|
|
|
io_putstr(" ");
|
|
io_putstr(" ");
|
|
@@ -242,8 +364,8 @@ int process_kill(unsigned int pid)
|
|
|
{
|
|
{
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
|
if (pid == 0)
|
|
if (pid == 0)
|
|
|
- return -1; /* cannot kill terminal */
|
|
|
|
|
- for (i = 1; i < MAX_PROCS; i++) {
|
|
|
|
|
|
|
+ return -1; /* cannot kill window manager */
|
|
|
|
|
+ for (i = 1; i < proc_cap; i++) {
|
|
|
if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
|
|
if (procs[i].state != PROC_UNUSED && procs[i].state != PROC_DEAD &&
|
|
|
procs[i].pid == pid) {
|
|
procs[i].pid == pid) {
|
|
|
procs[i].state = PROC_DEAD;
|
|
procs[i].state = PROC_DEAD;
|
|
@@ -257,7 +379,7 @@ int process_kill(unsigned int pid)
|
|
|
int process_is_alive(unsigned int pid)
|
|
int process_is_alive(unsigned int pid)
|
|
|
{
|
|
{
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
|
- for (i = 0; i < MAX_PROCS; i++)
|
|
|
|
|
|
|
+ for (i = 0; i < proc_cap; i++)
|
|
|
if (procs[i].pid == pid &&
|
|
if (procs[i].pid == pid &&
|
|
|
(procs[i].state == PROC_READY ||
|
|
(procs[i].state == PROC_READY ||
|
|
|
procs[i].state == PROC_RUNNING))
|
|
procs[i].state == PROC_RUNNING))
|