From cce444f546a20374b81b3e04c9e2472ba98e9c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Thu, 15 Mar 2018 12:58:03 +0100 Subject: [PATCH] Reference cpu specific information via gs segment --- src/kernel/cpu/cpu.c | 11 ++++++++--- src/kernel/cpu/gdt.c | 2 +- src/kernel/cpu/isr_common.S | 14 ++++++++++++++ src/kernel/cpu/registers.S | 17 +++++++++++++++++ src/kernel/include/cpu.h | 6 +++++- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/kernel/cpu/cpu.c b/src/kernel/cpu/cpu.c index 86c36ae..d27cb27 100644 --- a/src/kernel/cpu/cpu.c +++ b/src/kernel/cpu/cpu.c @@ -1,15 +1,20 @@ #include #include #include +#include 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); } diff --git a/src/kernel/cpu/gdt.c b/src/kernel/cpu/gdt.c index 8ffc369..08ad4df 100644 --- a/src/kernel/cpu/gdt.c +++ b/src/kernel/cpu/gdt.c @@ -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; } diff --git a/src/kernel/cpu/isr_common.S b/src/kernel/cpu/isr_common.S index e1b888a..1af1a7d 100644 --- a/src/kernel/cpu/isr_common.S +++ b/src/kernel/cpu/isr_common.S @@ -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 diff --git a/src/kernel/cpu/registers.S b/src/kernel/cpu/registers.S index f99166d..2f0c7ea 100644 --- a/src/kernel/cpu/registers.S +++ b/src/kernel/cpu/registers.S @@ -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 diff --git a/src/kernel/include/cpu.h b/src/kernel/include/cpu.h index 80ef952..c34fdac 100644 --- a/src/kernel/include/cpu.h +++ b/src/kernel/include/cpu.h @@ -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);