Big code refactor, get rid of HAL entirely
This commit is contained in:
92
kernel/cpu/gdt.c
Normal file
92
kernel/cpu/gdt.c
Normal file
@ -0,0 +1,92 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "compiler/attr.h"
|
||||
#include "cpu/gdt.h"
|
||||
#include "std/string.h"
|
||||
|
||||
#define GDT_PRESENT 0x80
|
||||
#define GDT_TSS 0x89
|
||||
|
||||
#define KSTACK (1024*2*4096)
|
||||
ALIGNED(16) static uint8_t kernelstack[KSTACK];
|
||||
|
||||
typedef struct {
|
||||
uint16_t limitlow;
|
||||
uint16_t baselow;
|
||||
uint8_t basemid;
|
||||
uint8_t access;
|
||||
uint8_t gran;
|
||||
uint8_t basehigh;
|
||||
} PACKED GdtEntry;
|
||||
|
||||
typedef struct {
|
||||
uint16_t limit;
|
||||
uint64_t base;
|
||||
} PACKED GdtPtr;
|
||||
|
||||
typedef struct {
|
||||
GdtEntry old[5];
|
||||
GdtEntry tsslow;
|
||||
GdtEntry tsshigh;
|
||||
} PACKED ExtendedGdt;
|
||||
|
||||
ALIGNED(16) Tss tss = {0};
|
||||
ALIGNED(16) static ExtendedGdt gdt = {0};
|
||||
|
||||
void gdt_setentry(GdtEntry *ent, uint32_t base, uint32_t limit, uint8_t acc, uint8_t gran) {
|
||||
ent->baselow = base & 0xffff;
|
||||
ent->basemid = (base >> 16) & 0xff;
|
||||
ent->basehigh = (base >> 24) & 0xff;
|
||||
ent->limitlow = limit & 0xffff;
|
||||
ent->gran = ((limit >> 16) & 0x0f) | (gran & 0xf0);
|
||||
ent->access = acc;
|
||||
}
|
||||
|
||||
void gdt_init(void) {
|
||||
memset(&tss, 0, sizeof(tss));
|
||||
tss.iopb_off = sizeof(tss);
|
||||
|
||||
tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack));
|
||||
|
||||
uint64_t tss_base = (uint64_t)&tss;
|
||||
uint32_t tss_limit = sizeof(tss) - 1;
|
||||
|
||||
gdt_setentry(&gdt.old[0], 0, 0, 0, 0);
|
||||
gdt_setentry(&gdt.old[1], 0, 0xFFFFF, 0x9a, 0xA0);
|
||||
gdt_setentry(&gdt.old[2], 0, 0xFFFFF, 0x92, 0xC0);
|
||||
gdt_setentry(&gdt.old[3], 0, 0xFFFFF, 0xfa, 0xA0);
|
||||
gdt_setentry(&gdt.old[4], 0, 0xFFFFF, 0xf2, 0xC0);
|
||||
|
||||
gdt_setentry(&gdt.tsslow, tss_base & 0xFFFFFFFF, tss_limit, GDT_PRESENT | GDT_TSS, 0x0);
|
||||
|
||||
uint32_t tssbasehi = tss_base >> 32;
|
||||
gdt.tsshigh.limitlow = tssbasehi & 0xffff;
|
||||
gdt.tsshigh.baselow = (tssbasehi >> 16) & 0xffff;
|
||||
gdt.tsshigh.basemid = 0;
|
||||
gdt.tsshigh.basehigh = 0;
|
||||
gdt.tsshigh.access = 0;
|
||||
gdt.tsshigh.gran = 0;
|
||||
|
||||
GdtPtr gdtr;
|
||||
gdtr.limit = sizeof(gdt) - 1;
|
||||
gdtr.base = (uint64_t)&gdt;
|
||||
asm volatile("lgdt %0" :: "m"(gdtr));
|
||||
|
||||
asm volatile(
|
||||
"pushq %[kcode]\n"
|
||||
"lea 1f(%%rip), %%rax\n"
|
||||
"pushq %%rax\n"
|
||||
"lretq\n"
|
||||
"1:\n"
|
||||
"movw %[kdata], %%ax\n"
|
||||
"movw %%ax, %%ds\n"
|
||||
"movw %%ax, %%es\n"
|
||||
"movw %%ax, %%ss\n"
|
||||
:
|
||||
: [kcode] "i"(KCODE), [kdata] "i"(KDATA)
|
||||
: "rax", "memory"
|
||||
);
|
||||
|
||||
asm volatile("ltr %0" :: "r"((uint16_t)TSS));
|
||||
}
|
||||
|
||||
29
kernel/cpu/gdt.h
Normal file
29
kernel/cpu/gdt.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef CPU_GDT_H_
|
||||
#define CPU_GDT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "compiler/attr.h"
|
||||
|
||||
#define KCODE 0x08
|
||||
#define KDATA 0x10
|
||||
#define UCODE 0x18
|
||||
#define UDATA 0x20
|
||||
#define TSS 0x28
|
||||
|
||||
typedef struct {
|
||||
uint32_t resv0;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t resv1;
|
||||
uint64_t ist[7];
|
||||
uint64_t resv2;
|
||||
uint16_t resv3;
|
||||
uint16_t iopb_off;
|
||||
} PACKED Tss;
|
||||
|
||||
ALIGNED(16) extern Tss tss;
|
||||
|
||||
void gdt_init(void);
|
||||
|
||||
#endif // CPU_GDT_H_
|
||||
10
kernel/cpu/hang.c
Normal file
10
kernel/cpu/hang.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include "cpu/hang.h"
|
||||
#include "intr/intr.h"
|
||||
|
||||
void cpu_hang(void) {
|
||||
intr_disable();
|
||||
for(;;) {
|
||||
asm("hlt");
|
||||
}
|
||||
}
|
||||
|
||||
6
kernel/cpu/hang.h
Normal file
6
kernel/cpu/hang.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef CPU_HANG_H_
|
||||
#define CPU_HANG_H_
|
||||
|
||||
void cpu_hang(void);
|
||||
|
||||
#endif // CPU_HANG_H_
|
||||
33
kernel/cpu/regs.S
Normal file
33
kernel/cpu/regs.S
Normal file
@ -0,0 +1,33 @@
|
||||
.macro _push_regs
|
||||
push %rax
|
||||
push %rcx
|
||||
push %rdx
|
||||
push %rsi
|
||||
push %rdi
|
||||
push %rbp
|
||||
push %r8
|
||||
push %r9
|
||||
push %r10
|
||||
push %r11
|
||||
push %r12
|
||||
push %r13
|
||||
push %r14
|
||||
push %r15
|
||||
.endm
|
||||
|
||||
.macro _pop_regs
|
||||
pop %r15
|
||||
pop %r14
|
||||
pop %r13
|
||||
pop %r12
|
||||
pop %r11
|
||||
pop %r10
|
||||
pop %r9
|
||||
pop %r8
|
||||
pop %rbp
|
||||
pop %rdi
|
||||
pop %rsi
|
||||
pop %rdx
|
||||
pop %rcx
|
||||
pop %rax
|
||||
.endm
|
||||
Reference in New Issue
Block a user