Reference cpu specific information via gs segment

This commit is contained in:
Thomas Lovén 2018-03-15 12:58:03 +01:00
parent 01ff25eefb
commit cce444f546
5 changed files with 45 additions and 5 deletions

View File

@ -1,15 +1,20 @@
#include <cpu.h>
#include <interrupts.h>
#include <memory.h>
#include <debug.h>
void gdt_init(struct cpu *cpu);
struct cpu *cpu;
struct cpu __seg_gs *cpu = 0;
void cpu_init()
{
cpu = P2V(pmm_calloc());
struct cpu *c = P2V(pmm_calloc());
c->cpu = c;
write_msr(0xc0000102, (uint64_t)c);
asm("swapgs");
interrupt_init();
gdt_init(cpu);
gdt_init(cpu->cpu);
}

View File

@ -77,6 +77,6 @@ void gdt_init(struct cpu *c)
void interrupt_stack(void *rsp0)
{
struct tss *tss = (void *)cpu->tss;
struct tss __seg_gs *tss = (void __seg_gs *)(cpu->tss);
tss->rsp0 = (uint64_t)rsp0;
}

View File

@ -20,12 +20,26 @@ isr_common:
push rcx
push rbx
push rax
mov rax, [rsp + 152]
and rax, (3<<12)
jz .kernel_interrupt
swapgs
.kernel_interrupt:
mov rdi, rsp
call int_handler
mov rdi, rax
isr_return:
mov rsp, rdi
mov rax, [rsp + 152]
and rax, (3<<12)
jz .kernel_return
swapgs
.kernel_return:
pop rax
pop rbx
pop rcx

View File

@ -41,3 +41,20 @@ write_cr3:
read_cr4:
mov rax, cr4
ret
.global read_msr
read_msr:
mov rcx, rdi
rdmsr
shl rdx, 32
add rax, rdx
ret
.global write_msr
write_msr:
mov rcx, rdi
mov rax, rsi
mov rdx, rsi
shr rdx, 32
wrmsr
ret

View File

@ -4,13 +4,14 @@
struct cpu
{
void *cpu;
uint64_t gdt[6];
uint8_t tss[104];
struct process *proc;
struct process *scheduler;
};
extern struct cpu *cpu;
extern struct cpu __seg_gs *cpu;
void cpu_init();
@ -24,3 +25,6 @@ uint64_t read_cr2();
uint64_t read_cr3();
void write_cr3(uint64_t);
uint64_t read_cr4();
void write_msr(uint64_t reg, uint64_t value);
uint64_t read_msr(uint64_t reg);