Collect cpu specific information at one place

This commit is contained in:
Thomas Lovén 2018-02-27 13:57:18 +01:00
parent 18481604f9
commit 01ff25eefb
6 changed files with 42 additions and 41 deletions

View File

@ -1,12 +1,15 @@
#include <cpu.h>
#include <interrupts.h>
#include <memory.h>
uint64_t global_gdt[6];
uint8_t global_tss[104];
void gdt_init();
void gdt_init(struct cpu *cpu);
struct cpu *cpu;
void cpu_init()
{
cpu = P2V(pmm_calloc());
interrupt_init();
gdt_init();
gdt_init(cpu);
}

View File

@ -52,12 +52,12 @@ struct gdt BootGDT[] = {
struct gdtp GDTp = {2*8-1, BootGDT};
void gdt_init()
void gdt_init(struct cpu *c)
{
struct gdt *gdt = GDT();
struct gdt *gdt = (void *)c->gdt;
memcpy(gdt, BootGDT, sizeof(BootGDT));
struct tss *tss = TSS();
struct tss *tss = (void *)c->tss;
tss->io_mba = sizeof(struct tss);
uint32_t tss_limit = sizeof(struct tss);
@ -77,6 +77,6 @@ void gdt_init()
void interrupt_stack(void *rsp0)
{
struct tss *tss = TSS();
struct tss *tss = (void *)cpu->tss;
tss->rsp0 = (uint64_t)rsp0;
}

View File

@ -1,16 +1,21 @@
#pragma once
#include <stdint.h>
#include <process.h>
struct cpu
{
uint64_t gdt[6];
uint8_t tss[104];
struct process *proc;
struct process *scheduler;
};
extern struct cpu *cpu;
void cpu_init();
extern uint64_t global_gdt[];
extern uint8_t global_tss[];
#define GDT() ((void *)global_gdt)
#define TSS() ((void *)global_tss)
void interrupt_stack(void *rsp0);
void load_idt(void *);
void load_gdt(void *);

View File

@ -3,7 +3,7 @@
#include <scheduler.h>
#include <stdint.h>
#include <interrupts.h>
#include <stddef.h>
#include <cpu.h>
struct process
{
@ -17,8 +17,6 @@ struct process
QUEUE_SPOT(runQ);
};
struct process *process();
struct process *new_process(void (*function)(void));
void yield();
void start_scheduler();

View File

@ -17,12 +17,12 @@ struct swtch_stack
registers r;
}__attribute__((packed));
struct process *sched_proc = 0;
struct process *_proc = 0;
void procmm_init(struct process *p);
registers *proc_pagefault(registers *r);
#define csched cpu->scheduler
#define cproc cpu->proc
uint64_t next_pid = 1;
struct process *new_process(void (*function)(void))
{
@ -51,14 +51,9 @@ struct process *new_process(void (*function)(void))
return proc;
}
struct process *process()
{
return _proc;
}
void yield()
{
switch_stack(&_proc->stack_ptr, &sched_proc->stack_ptr);
switch_stack(&cproc->stack_ptr, &csched->stack_ptr);
}
void scheduler()
@ -68,28 +63,28 @@ void scheduler()
struct process *new = 0;
while(!(new = scheduler_next()));
_proc = new;
cproc = new;
write_cr3(new->P4);
interrupt_stack(incptr(new, PAGE_SIZE));
switch_stack(&sched_proc->stack_ptr, &new->stack_ptr);
switch_stack(&csched->stack_ptr, &new->stack_ptr);
ready(_proc);
_proc = 0;
ready(cproc);
cproc = 0;
}
}
void start_scheduler()
{
sched_proc = P2V(pmm_calloc());
sched_proc->pid = (uint64_t)-1;
sched_proc->stack_ptr = incptr(sched_proc, PAGE_SIZE - sizeof(struct swtch_stack) + sizeof(registers));
sched_proc->P4 = kernel_P4;
struct process *sched = csched = P2V(pmm_calloc());
sched->pid = (uint64_t)-1;
sched->stack_ptr = incptr(sched, PAGE_SIZE - sizeof(struct swtch_stack) + sizeof(registers));
sched->P4 = kernel_P4;
struct swtch_stack *stk = sched_proc->stack_ptr;
struct swtch_stack *stk = sched->stack_ptr;
stk->RBP = (uint64_t)&stk->RBP2;
stk->ret = (uint64_t)scheduler;
uint64_t stack;
switch_stack(&stack, &sched_proc->stack_ptr);
switch_stack(&stack, &sched->stack_ptr);
}

View File

@ -35,17 +35,17 @@ registers *proc_pagefault(registers *r)
}
uint64_t fault_addr = read_cr2();
uint64_t stack = PROCESS()->stack;
uint64_t stack = cpu->proc->stack;
if(fault_addr + PAGE_SIZE >= stack)
{
// Page fault happened just below stack. Add another page to it.
// Unless it's about to run into brk.
if(stack - PAGE_SIZE <= PROCESS()->brk)
PANIC("Stack overflow in process %d\n", PROCESS()->pid);
if(stack - PAGE_SIZE <= cpu->proc->brk)
PANIC("Stack overflow in process %d\n", cpu->proc->pid);
stack -= PAGE_SIZE;
vmm_set_page(PROCESS()->P4, stack, pmm_alloc(), PAGE_USER | PAGE_WRITE | PAGE_PRESENT);
PROCESS()->stack = stack;
vmm_set_page(cpu->proc->P4, stack, pmm_alloc(), PAGE_USER | PAGE_WRITE | PAGE_PRESENT);
cpu->proc->stack = stack;
return r;
}