usr: implement user process switching
This is a rather large commit, so the gist of it is this: User context switching is now functional, after a change to the TSS, and a few minor details in the scheduler and loader. ELF parsing is rather primitive, but basically functional, as long as it only loads code. The interrupt 0x80 is for syscalls, and will switch from user mode to kernel mode. It is currently the only interrupt that can be called from user mode This adds the syscall functionality, which currently uses a red-black tree to store the function pointers, although when the table is more flushed out, it will probably be replaced with an array The pager now correctly marks the parent nodes with attributes as it allocates them. Without doing this, the page table entries will never be reached in lower privilege modes, causing page faults Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
d60da85470
commit
37518aea81
19 changed files with 262 additions and 69 deletions
52
sched/proc.c
52
sched/proc.c
|
|
@ -8,10 +8,16 @@
|
|||
static struct rbtree *proc_tree;
|
||||
static pid_t *proc_current;
|
||||
|
||||
static struct proc kernel_procs[256] = { 0 };
|
||||
|
||||
void _return_to_user(struct procregs *regs, paddr_t cr3);
|
||||
extern uintptr_t kstacks[256];
|
||||
|
||||
struct procregs *proc_current_regs()
|
||||
{
|
||||
if (proc_current[lapic_idno()] == 0)
|
||||
return &kernel_procs[lapic_idno()].regs;
|
||||
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, proc_current[lapic_idno()]);
|
||||
|
||||
if (proc_node) {
|
||||
|
|
@ -23,8 +29,16 @@ struct procregs *proc_current_regs()
|
|||
}
|
||||
}
|
||||
|
||||
pid_t getpid()
|
||||
{
|
||||
return proc_current[lapic_idno()];
|
||||
}
|
||||
|
||||
struct proc *proc_find(pid_t pid)
|
||||
{
|
||||
if (pid == 0)
|
||||
return &kernel_procs[lapic_idno()];
|
||||
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||
|
||||
if (proc_node)
|
||||
|
|
@ -33,6 +47,26 @@ struct proc *proc_find(pid_t pid)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
void proc_set_current(pid_t pid)
|
||||
{
|
||||
proc_current[lapic_idno()] = pid;
|
||||
}
|
||||
|
||||
void proc_term(pid_t pid)
|
||||
{
|
||||
struct rbnode *proc_node = rbt_search(proc_tree, pid);
|
||||
|
||||
if (proc_node) {
|
||||
struct proc *proc = (void *)proc_node->value;
|
||||
|
||||
rbt_delete(proc_tree, proc_node);
|
||||
kfree(proc);
|
||||
} else {
|
||||
kprintf(LOG_ERROR "proc: proc_term: proc %u not found\n", pid);
|
||||
panic();
|
||||
}
|
||||
}
|
||||
|
||||
void schedule()
|
||||
{
|
||||
uint8_t id = lapic_idno();
|
||||
|
|
@ -79,15 +113,23 @@ void proc_init(unsigned num_cpus)
|
|||
for (unsigned i = 0; i < num_cpus; i++) {
|
||||
proc_current[i] = -1;
|
||||
|
||||
struct proc *proc = kzalloc(sizeof(struct proc), ALLOC_KERN);
|
||||
struct proc *proc = &kernel_procs[i];
|
||||
|
||||
proc->pid = rbt_next_key(proc_tree);
|
||||
proc->pid = 0;
|
||||
proc->is_kernel = true;
|
||||
proc->cr3 = kcr3;
|
||||
|
||||
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
|
||||
proc_node->value = (uintptr_t)proc;
|
||||
proc->regs.rip = (uint64_t)yield;
|
||||
proc->regs.cs = 0x28;
|
||||
proc->regs.rflags = 0x246;
|
||||
proc->regs.rsp = kstacks[i] + 0xFF0;
|
||||
|
||||
proc_current[i] = proc->pid;
|
||||
proc_current[i] = 0;
|
||||
}
|
||||
|
||||
/* ensure that the first process has PID 0
|
||||
* this value is never actually used, it prevents other processes from
|
||||
* having PID 0
|
||||
*/
|
||||
(void)rbt_insert(proc_tree, 0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue