metal-cos/sys/amd64/machine.c

173 lines
3.4 KiB
C
Raw Normal View History

#include <stdbool.h>
2014-02-12 13:47:13 -08:00
#include <stdint.h>
2014-07-21 23:43:01 -07:00
#include <sys/kconfig.h>
#include <sys/kassert.h>
#include <sys/kmem.h>
#include <sys/mp.h>
#include <sys/irq.h>
#include <sys/spinlock.h>
2014-02-12 13:47:13 -08:00
2014-07-21 23:43:01 -07:00
#include <machine/amd64.h>
#include <machine/ioapic.h>
#include <machine/lapic.h>
#include <machine/trap.h>
#include <machine/pmap.h>
2014-02-12 13:47:13 -08:00
#include <sys/thread.h>
#include <sys/disk.h>
#include <sys/diskcache.h>
#include <sys/vfs.h>
2014-07-27 17:09:31 -07:00
#include <sys/elf64.h>
#include "../dev/console.h"
2014-11-24 22:21:37 -08:00
extern void KTime_Init();
2014-11-22 14:35:18 -08:00
extern void RTC_Init();
2014-08-19 19:05:57 -07:00
extern void PS2_Init();
extern void PCI_Init();
extern void IDE_Init();
2014-07-16 01:27:58 -07:00
extern void MachineBoot_AddMem();
2014-09-04 18:21:18 -07:00
extern void Loader_LoadInit();
2014-02-12 13:47:13 -08:00
2014-07-18 13:37:46 -07:00
#define GDT_MAX 8
2014-02-12 13:47:13 -08:00
static SegmentDescriptor GDT[MAX_CPUS][GDT_MAX];
static PseudoDescriptor GDTDescriptor[MAX_CPUS];
2014-11-29 15:46:17 -08:00
TaskStateSegment64 TSS[MAX_CPUS];
2014-02-12 13:47:13 -08:00
static char df_stack[4096];
void Machine_GDTInit()
{
uint64_t offset;
uint64_t tmp;
2014-07-18 13:37:46 -07:00
int c = CPU();
2014-02-12 13:47:13 -08:00
kprintf("Initializing GDT... "); // Caused pagefault??
2014-07-18 13:37:46 -07:00
GDT[c][0] = 0x0;
GDT[c][1] = 0x00AF9A000000FFFFULL; /* Kernel CS */
GDT[c][2] = 0x00CF92000000FFFFULL; /* Kernel DS */
GDT[c][3] = 0x0;
2014-02-12 13:47:13 -08:00
// TSS
2014-07-27 17:09:31 -07:00
offset = (uint64_t)&TSS[c];
2014-07-18 13:37:46 -07:00
GDT[c][4] = sizeof(TaskStateSegment64);
2014-02-12 13:47:13 -08:00
tmp = offset & 0x00FFFFFF;
2014-07-18 13:37:46 -07:00
GDT[c][4] |= (tmp << 16);
2014-02-12 13:47:13 -08:00
tmp = offset & 0xFF000000;
2014-07-18 13:37:46 -07:00
GDT[c][4] |= (tmp << 56);
GDT[c][4] |= 0x89ULL << 40;
2014-07-27 17:09:31 -07:00
GDT[c][5] = offset >> 32;
2014-02-12 13:47:13 -08:00
2014-07-18 13:37:46 -07:00
GDT[c][6] = 0x00AFFA000000FFFFULL; /* User CS */
GDT[c][7] = 0x00CFF2000000FFFFULL; /* User DS */
2014-02-12 13:47:13 -08:00
2014-07-18 13:37:46 -07:00
GDTDescriptor[c].off = (uint64_t)&GDT[0];
GDTDescriptor[c].lim = 8*GDT_MAX - 1;
lgdt(&GDTDescriptor[c]);
2014-02-12 13:47:13 -08:00
kprintf("Done!\n");
}
void Machine_TSSInit()
{
2014-07-18 13:37:46 -07:00
int c = CPU();
2014-02-12 13:47:13 -08:00
kprintf("Initializing TSS... ");
2014-07-18 13:37:46 -07:00
TSS[c]._unused0 = 0;
TSS[c]._unused1 = 0;
TSS[c]._unused2 = 0;
TSS[c]._unused3 = 0;
TSS[c]._unused4 = 0;
TSS[c].ist1 = ((uint64_t)&df_stack) + 4096;
2014-07-18 13:37:46 -07:00
TSS[c].ist2 = 0x0;
TSS[c].ist3 = 0x0;
TSS[c].ist4 = 0x0;
TSS[c].ist5 = 0x0;
TSS[c].ist6 = 0x0;
TSS[c].ist7 = 0x0;
TSS[c].rsp0 = ((uint64_t)&df_stack) + 4096;
2014-07-18 13:37:46 -07:00
TSS[c].rsp1 = 0;
TSS[c].rsp2 = 0;
2014-02-12 13:47:13 -08:00
ltr(SEL_TSS);
kprintf("Done!\n");
}
void Machine_SyscallInit()
{
kprintf("Initializing Syscall... ");
wrmsr(MSR_STAR, (uint64_t)SEL_KCS << 32 | (uint64_t)SEL_UCS << 48);
2014-07-10 14:43:52 -07:00
wrmsr(MSR_LSTAR, 0);
wrmsr(MSR_CSTAR, 0);
wrmsr(MSR_SFMASK, 0);
2014-02-12 13:47:13 -08:00
kprintf("Done!\n");
}
2014-07-13 23:16:54 -07:00
void Machine_EarlyInit()
{
Critical_Init();
Critical_Enter();
Console_Init();
PAlloc_Init();
}
void Machine_IdleThread(void *test)
{
while (1) { enable_interrupts(); hlt(); }
}
2014-02-12 13:47:13 -08:00
void Machine_Init()
{
Machine_GDTInit();
Machine_TSSInit();
Trap_Init();
//Machine_SyscallInit();
2014-07-18 15:07:25 -07:00
PAlloc_AddRegion(DMPA2VA(16*1024*1024), 16*1024*1024);
PMap_Init();
XMem_Init();
PAlloc_LateInit();
MachineBoot_AddMem();
2014-11-24 22:21:37 -08:00
KTime_Init();
2014-07-10 14:01:15 -07:00
IRQ_Init();
2014-02-12 13:47:13 -08:00
LAPIC_Init();
IOAPIC_Init();
2014-08-19 19:05:57 -07:00
IOAPIC_Enable(0); // Enable timer interrupts
Thread_Init();
2014-11-22 14:35:18 -08:00
RTC_Init();
2014-08-19 19:05:57 -07:00
PS2_Init();
PCI_Init();
IDE_Init();
DiskCache_Init();
2014-07-13 13:07:19 -07:00
Disk *root = Disk_GetByID(0, 0);
if (!root)
Panic("No boot disk!");
VFS_MountRoot(root);
2014-07-13 23:16:54 -07:00
Critical_Exit();
2014-09-04 18:21:18 -07:00
Thread *thr = Thread_KThreadCreate(&Machine_IdleThread, NULL);
if (thr == NULL) {
kprintf("Couldn't create idle thread!\n");
}
Thread_SetRunnable(thr);
2014-07-27 17:09:31 -07:00
Loader_LoadInit();
2014-07-13 13:07:19 -07:00
breakpoint();
2014-02-12 13:47:13 -08:00
}