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 <cpu.h>
#include <interrupts.h> #include <interrupts.h>
#include <memory.h>
uint64_t global_gdt[6]; void gdt_init(struct cpu *cpu);
uint8_t global_tss[104];
void gdt_init(); struct cpu *cpu;
void cpu_init() void cpu_init()
{ {
cpu = P2V(pmm_calloc());
interrupt_init(); interrupt_init();
gdt_init(); gdt_init(cpu);
} }

View File

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

View File

@ -1,16 +1,21 @@
#pragma once #pragma once
#include <stdint.h> #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(); 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 interrupt_stack(void *rsp0);
void load_idt(void *); void load_idt(void *);
void load_gdt(void *); void load_gdt(void *);

View File

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

View File

@ -17,12 +17,12 @@ struct swtch_stack
registers r; registers r;
}__attribute__((packed)); }__attribute__((packed));
struct process *sched_proc = 0;
struct process *_proc = 0;
void procmm_init(struct process *p); void procmm_init(struct process *p);
registers *proc_pagefault(registers *r); registers *proc_pagefault(registers *r);
#define csched cpu->scheduler
#define cproc cpu->proc
uint64_t next_pid = 1; uint64_t next_pid = 1;
struct process *new_process(void (*function)(void)) struct process *new_process(void (*function)(void))
{ {
@ -51,14 +51,9 @@ struct process *new_process(void (*function)(void))
return proc; return proc;
} }
struct process *process()
{
return _proc;
}
void yield() void yield()
{ {
switch_stack(&_proc->stack_ptr, &sched_proc->stack_ptr); switch_stack(&cproc->stack_ptr, &csched->stack_ptr);
} }
void scheduler() void scheduler()
@ -68,28 +63,28 @@ void scheduler()
struct process *new = 0; struct process *new = 0;
while(!(new = scheduler_next())); while(!(new = scheduler_next()));
_proc = new; cproc = new;
write_cr3(new->P4); write_cr3(new->P4);
interrupt_stack(incptr(new, PAGE_SIZE)); 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); ready(cproc);
_proc = 0; cproc = 0;
} }
} }
void start_scheduler() void start_scheduler()
{ {
sched_proc = P2V(pmm_calloc()); struct process *sched = csched = P2V(pmm_calloc());
sched_proc->pid = (uint64_t)-1; sched->pid = (uint64_t)-1;
sched_proc->stack_ptr = incptr(sched_proc, PAGE_SIZE - sizeof(struct swtch_stack) + sizeof(registers)); sched->stack_ptr = incptr(sched, PAGE_SIZE - sizeof(struct swtch_stack) + sizeof(registers));
sched_proc->P4 = kernel_P4; 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->RBP = (uint64_t)&stk->RBP2;
stk->ret = (uint64_t)scheduler; stk->ret = (uint64_t)scheduler;
uint64_t stack; 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 fault_addr = read_cr2();
uint64_t stack = PROCESS()->stack; uint64_t stack = cpu->proc->stack;
if(fault_addr + PAGE_SIZE >= stack) if(fault_addr + PAGE_SIZE >= stack)
{ {
// Page fault happened just below stack. Add another page to it. // Page fault happened just below stack. Add another page to it.
// Unless it's about to run into brk. // Unless it's about to run into brk.
if(stack - PAGE_SIZE <= PROCESS()->brk) if(stack - PAGE_SIZE <= cpu->proc->brk)
PANIC("Stack overflow in process %d\n", PROCESS()->pid); PANIC("Stack overflow in process %d\n", cpu->proc->pid);
stack -= PAGE_SIZE; stack -= PAGE_SIZE;
vmm_set_page(PROCESS()->P4, stack, pmm_alloc(), PAGE_USER | PAGE_WRITE | PAGE_PRESENT); vmm_set_page(cpu->proc->P4, stack, pmm_alloc(), PAGE_USER | PAGE_WRITE | PAGE_PRESENT);
PROCESS()->stack = stack; cpu->proc->stack = stack;
return r; return r;
} }