2014-07-07 17:57:57 +00:00
|
|
|
#include <stdbool.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <kconfig.h>
|
|
|
|
#include <kassert.h>
|
2014-07-06 21:30:09 +00:00
|
|
|
#include <kmem.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
#include "amd64.h"
|
2014-07-06 21:30:09 +00:00
|
|
|
#include "ioapic.h"
|
|
|
|
#include "lapic.h"
|
2014-07-07 17:57:57 +00:00
|
|
|
#include "trap.h"
|
|
|
|
#include "pmap.h"
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-07-07 17:57:57 +00:00
|
|
|
extern void PCI_Init();
|
|
|
|
extern void IDE_Init();
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
#define GDT_MAX 7
|
|
|
|
|
|
|
|
static SegmentDescriptor GDT[MAX_CPUS][GDT_MAX];
|
|
|
|
static PseudoDescriptor GDTDescriptor[MAX_CPUS];
|
|
|
|
static TaskStateSegment64 TSS[MAX_CPUS];
|
|
|
|
|
|
|
|
static char df_stack[4096];
|
|
|
|
|
|
|
|
void Machine_GDTInit()
|
|
|
|
{
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t tmp;
|
|
|
|
|
|
|
|
kprintf("Initializing GDT... "); // Caused pagefault??
|
|
|
|
|
|
|
|
GDT[0][0] = 0x0;
|
|
|
|
GDT[0][1] = 0x00AF9A000000FFFFULL; /* Kernel CS */
|
|
|
|
GDT[0][2] = 0x00CF92000000FFFFULL; /* Kernel DS */
|
|
|
|
GDT[5][1] = 0x00AFFA000000FFFFULL; /* User CS */
|
|
|
|
GDT[6][2] = 0x00CFF2000000FFFFULL; /* User DS */
|
|
|
|
|
|
|
|
// TSS
|
|
|
|
GDT[0][3] = 0x0; /* TSS */
|
|
|
|
GDT[0][4] = sizeof(TaskStateSegment64);
|
|
|
|
offset = (uint64_t)&TSS[0];
|
|
|
|
tmp = offset & 0x00FFFFFF;
|
|
|
|
GDT[0][4] |= (tmp << 16);
|
|
|
|
tmp = offset & 0xFF000000;
|
|
|
|
GDT[0][4] |= (tmp << 56);
|
|
|
|
GDT[0][4] |= 0x89ULL << 40;
|
|
|
|
|
|
|
|
GDTDescriptor[0].off = (uint64_t)&GDT[0];
|
|
|
|
GDTDescriptor[0].lim = 8*GDT_MAX - 1;
|
|
|
|
|
|
|
|
lgdt(&GDTDescriptor[0]);
|
|
|
|
|
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine_TSSInit()
|
|
|
|
{
|
|
|
|
kprintf("Initializing TSS... ");
|
|
|
|
|
|
|
|
TSS[0]._unused0 = 0;
|
|
|
|
TSS[0]._unused1 = 0;
|
|
|
|
TSS[0]._unused2 = 0;
|
|
|
|
TSS[0]._unused3 = 0;
|
|
|
|
TSS[0]._unused4 = 0;
|
2014-07-07 17:57:57 +00:00
|
|
|
TSS[0].ist1 = (uint64_t)&df_stack;
|
2014-02-12 21:47:13 +00:00
|
|
|
TSS[0].rsp0 = 0;
|
|
|
|
TSS[0].rsp1 = 0;
|
|
|
|
TSS[0].rsp2 = 0;
|
|
|
|
|
|
|
|
ltr(SEL_TSS);
|
|
|
|
|
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine_SyscallInit()
|
|
|
|
{
|
|
|
|
kprintf("Initializing Syscall... ");
|
|
|
|
|
2014-07-10 21:43:52 +00:00
|
|
|
wrmsr(MSR_STAR, SEL_KCS << 32 | SEL_UCS << 48);
|
|
|
|
wrmsr(MSR_LSTAR, 0);
|
|
|
|
wrmsr(MSR_CSTAR, 0);
|
|
|
|
wrmsr(MSR_SFMASK, 0);
|
|
|
|
|
2014-02-12 21:47:13 +00:00
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Machine_Init()
|
|
|
|
{
|
|
|
|
Machine_GDTInit();
|
|
|
|
Machine_TSSInit();
|
|
|
|
Trap_Init();
|
|
|
|
//Machine_SyscallInit();
|
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
PAlloc_AddRegion(16*1024*1024, 16*1024*1024);
|
|
|
|
PMap_Init();
|
|
|
|
|
2014-07-10 21:01:15 +00:00
|
|
|
IRQ_Init();
|
2014-02-12 21:47:13 +00:00
|
|
|
LAPIC_Init();
|
|
|
|
IOAPIC_Init();
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
PCI_Init();
|
2014-07-06 01:46:59 +00:00
|
|
|
IDE_Init();
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
|
|
|
|