2014-07-07 17:57:57 +00:00
|
|
|
#include <stdbool.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-07-22 06:43:01 +00: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 21:47:13 +00:00
|
|
|
|
2014-07-22 06:43:01 +00:00
|
|
|
#include <machine/amd64.h>
|
|
|
|
#include <machine/ioapic.h>
|
|
|
|
#include <machine/lapic.h>
|
|
|
|
#include <machine/trap.h>
|
|
|
|
#include <machine/pmap.h>
|
2015-02-16 21:04:40 +00:00
|
|
|
#include <machine/mp.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
#include <sys/thread.h>
|
2014-07-31 00:19:24 +00:00
|
|
|
#include <sys/disk.h>
|
2022-12-10 04:51:45 +00:00
|
|
|
#include <sys/bufcache.h>
|
2014-08-01 21:15:06 +00:00
|
|
|
#include <sys/vfs.h>
|
2014-07-28 00:09:31 +00:00
|
|
|
#include <sys/elf64.h>
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2014-07-16 00:00:16 +00:00
|
|
|
#include "../dev/console.h"
|
|
|
|
|
2014-11-25 06:21:37 +00:00
|
|
|
extern void KTime_Init();
|
2015-01-17 21:52:04 +00:00
|
|
|
extern void KTimer_Init();
|
2014-11-22 22:35:18 +00:00
|
|
|
extern void RTC_Init();
|
2014-08-20 02:05:57 +00:00
|
|
|
extern void PS2_Init();
|
2014-07-07 17:57:57 +00:00
|
|
|
extern void PCI_Init();
|
|
|
|
extern void IDE_Init();
|
2014-07-16 08:27:58 +00:00
|
|
|
extern void MachineBoot_AddMem();
|
2014-09-05 01:21:18 +00:00
|
|
|
extern void Loader_LoadInit();
|
2015-01-16 22:32:47 +00:00
|
|
|
extern void PAlloc_LateInit();
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-07-18 20:37:46 +00:00
|
|
|
#define GDT_MAX 8
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
static SegmentDescriptor GDT[MAX_CPUS][GDT_MAX];
|
|
|
|
static PseudoDescriptor GDTDescriptor[MAX_CPUS];
|
2014-11-29 23:46:17 +00:00
|
|
|
TaskStateSegment64 TSS[MAX_CPUS];
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
static char df_stack[4096];
|
2023-08-20 23:06:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Machine_GDTInit --
|
|
|
|
*
|
|
|
|
* Configures the Global Descriptor Table (GDT) that lists all segments
|
|
|
|
* and privilege levels in x86. 64-bit mode uses flat 64-bit segments and
|
|
|
|
* doesn't support offsets and limits except for the special FS/GS segment
|
|
|
|
* registers. We create four segments for the kernel code/data and user
|
|
|
|
* code/data.
|
|
|
|
*/
|
2015-02-16 21:32:14 +00:00
|
|
|
static void
|
|
|
|
Machine_GDTInit()
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t tmp;
|
2014-07-18 20:37:46 +00:00
|
|
|
int c = CPU();
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
kprintf("Initializing GDT... "); // Caused pagefault??
|
|
|
|
|
2014-07-18 20:37:46 +00:00
|
|
|
GDT[c][0] = 0x0;
|
|
|
|
GDT[c][1] = 0x00AF9A000000FFFFULL; /* Kernel CS */
|
|
|
|
GDT[c][2] = 0x00CF92000000FFFFULL; /* Kernel DS */
|
|
|
|
GDT[c][3] = 0x0;
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
// TSS
|
2014-07-28 00:09:31 +00:00
|
|
|
offset = (uint64_t)&TSS[c];
|
2014-07-18 20:37:46 +00:00
|
|
|
GDT[c][4] = sizeof(TaskStateSegment64);
|
2014-02-12 21:47:13 +00:00
|
|
|
tmp = offset & 0x00FFFFFF;
|
2014-07-18 20:37:46 +00:00
|
|
|
GDT[c][4] |= (tmp << 16);
|
2014-02-12 21:47:13 +00:00
|
|
|
tmp = offset & 0xFF000000;
|
2014-07-18 20:37:46 +00:00
|
|
|
GDT[c][4] |= (tmp << 56);
|
|
|
|
GDT[c][4] |= 0x89ULL << 40;
|
2014-07-28 00:09:31 +00:00
|
|
|
GDT[c][5] = offset >> 32;
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-07-18 20:37:46 +00:00
|
|
|
GDT[c][6] = 0x00AFFA000000FFFFULL; /* User CS */
|
|
|
|
GDT[c][7] = 0x00CFF2000000FFFFULL; /* User DS */
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2015-02-16 01:00:55 +00:00
|
|
|
GDTDescriptor[c].off = (uint64_t)&GDT[c];
|
2014-07-18 20:37:46 +00:00
|
|
|
GDTDescriptor[c].lim = 8*GDT_MAX - 1;
|
|
|
|
|
|
|
|
lgdt(&GDTDescriptor[c]);
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Machine_TSSInit --
|
|
|
|
*
|
|
|
|
* Configures the Task State Segment (TSS) that specifies the kernel stack
|
|
|
|
* pointer during an interrupt.
|
|
|
|
*/
|
2015-02-16 21:32:14 +00:00
|
|
|
static void
|
|
|
|
Machine_TSSInit()
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
2014-07-18 20:37:46 +00:00
|
|
|
int c = CPU();
|
|
|
|
|
2014-02-12 21:47:13 +00:00
|
|
|
kprintf("Initializing TSS... ");
|
|
|
|
|
2014-07-18 20:37:46 +00:00
|
|
|
TSS[c]._unused0 = 0;
|
|
|
|
TSS[c]._unused1 = 0;
|
|
|
|
TSS[c]._unused2 = 0;
|
|
|
|
TSS[c]._unused3 = 0;
|
|
|
|
TSS[c]._unused4 = 0;
|
2014-11-26 21:49:17 +00:00
|
|
|
TSS[c].ist1 = ((uint64_t)&df_stack) + 4096;
|
2014-07-18 20:37:46 +00: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;
|
2014-11-26 21:49:17 +00:00
|
|
|
TSS[c].rsp0 = ((uint64_t)&df_stack) + 4096;
|
2014-07-18 20:37:46 +00:00
|
|
|
TSS[c].rsp1 = 0;
|
|
|
|
TSS[c].rsp2 = 0;
|
2014-02-12 21:47:13 +00:00
|
|
|
|
|
|
|
ltr(SEL_TSS);
|
|
|
|
|
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Machine_SyscallInit --
|
|
|
|
*
|
|
|
|
* Configure the model specific registers (MSRs) that specify how to
|
|
|
|
* transfer control to the operating system when the system call
|
|
|
|
* instruction is invoked.
|
|
|
|
*/
|
2015-02-16 21:32:14 +00:00
|
|
|
static void
|
|
|
|
Machine_SyscallInit()
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
|
|
|
kprintf("Initializing Syscall... ");
|
|
|
|
|
2014-07-10 23:18:58 +00:00
|
|
|
wrmsr(MSR_STAR, (uint64_t)SEL_KCS << 32 | (uint64_t)SEL_UCS << 48);
|
2014-07-10 21:43:52 +00:00
|
|
|
wrmsr(MSR_LSTAR, 0);
|
|
|
|
wrmsr(MSR_CSTAR, 0);
|
|
|
|
wrmsr(MSR_SFMASK, 0);
|
|
|
|
|
2014-02-12 21:47:13 +00:00
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Machine_EarlyInit --
|
|
|
|
*
|
|
|
|
* Initializes early kernel state.
|
|
|
|
*/
|
2015-02-16 21:32:14 +00:00
|
|
|
void
|
|
|
|
Machine_EarlyInit()
|
2014-07-14 06:16:54 +00:00
|
|
|
{
|
2015-02-16 04:05:24 +00:00
|
|
|
Spinlock_EarlyInit();
|
2014-07-14 06:16:54 +00:00
|
|
|
Critical_Init();
|
|
|
|
Critical_Enter();
|
2023-08-30 16:49:23 +00:00
|
|
|
WaitChannel_EarlyInit();
|
2014-07-14 06:16:54 +00:00
|
|
|
Console_Init();
|
|
|
|
PAlloc_Init();
|
|
|
|
}
|
|
|
|
|
2015-02-16 21:32:14 +00:00
|
|
|
static void
|
|
|
|
Machine_IdleThread(void *test)
|
2014-07-24 01:07:07 +00:00
|
|
|
{
|
|
|
|
while (1) { enable_interrupts(); hlt(); }
|
|
|
|
}
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Machine_Init --
|
|
|
|
*
|
|
|
|
* At this point the assembly startup code has setup temporary processor
|
|
|
|
* data structures sufficient to execute C code and make it through this
|
|
|
|
* initialization routine.
|
|
|
|
*/
|
2014-02-12 21:47:13 +00:00
|
|
|
void Machine_Init()
|
|
|
|
{
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Initialize Processor State
|
|
|
|
*/
|
2014-02-12 21:47:13 +00:00
|
|
|
Machine_GDTInit();
|
|
|
|
Machine_TSSInit();
|
|
|
|
Trap_Init();
|
2015-02-16 21:32:14 +00:00
|
|
|
Machine_SyscallInit();
|
2015-02-25 00:57:30 +00:00
|
|
|
clts(); // Enable FPU/XMM
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Initialize Memory Allocation and Virtual Memory
|
|
|
|
*/
|
2014-07-18 22:07:25 +00:00
|
|
|
PAlloc_AddRegion(DMPA2VA(16*1024*1024), 16*1024*1024);
|
2014-06-30 22:52:35 +00:00
|
|
|
PMap_Init();
|
2014-07-17 00:21:18 +00:00
|
|
|
XMem_Init();
|
2014-12-09 06:37:40 +00:00
|
|
|
PAlloc_LateInit();
|
|
|
|
MachineBoot_AddMem();
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Initialize Time Keeping
|
|
|
|
*/
|
2014-11-25 06:21:37 +00:00
|
|
|
KTime_Init();
|
2015-01-16 22:32:47 +00:00
|
|
|
RTC_Init(); // Finishes initializing KTime
|
2014-06-30 22:52:35 +00:00
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Initialize Interrupts
|
|
|
|
*/
|
2014-07-10 21:01:15 +00:00
|
|
|
IRQ_Init();
|
2014-02-12 21:47:13 +00:00
|
|
|
LAPIC_Init();
|
|
|
|
IOAPIC_Init();
|
2014-08-20 02:05:57 +00:00
|
|
|
IOAPIC_Enable(0); // Enable timer interrupts
|
2014-07-24 01:07:07 +00:00
|
|
|
Thread_Init();
|
2014-06-30 22:52:35 +00:00
|
|
|
|
2015-01-17 21:52:04 +00:00
|
|
|
KTimer_Init(); // Depends on RTC and KTime
|
2014-11-22 22:35:18 +00:00
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Initialize Additional Processors
|
|
|
|
*/
|
2015-02-16 01:00:55 +00:00
|
|
|
MP_Init();
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Initialize Basic Devices
|
|
|
|
*/
|
|
|
|
PS2_Init(); // PS2 Keyboard
|
|
|
|
PCI_Init(); // PCI BUS
|
|
|
|
IDE_Init(); // IDE Disk Controller
|
2022-12-10 04:51:45 +00:00
|
|
|
BufCache_Init();
|
2014-07-13 20:07:19 +00:00
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Open the primary disk and mount the root file system
|
|
|
|
*/
|
2014-08-01 21:15:06 +00:00
|
|
|
Disk *root = Disk_GetByID(0, 0);
|
|
|
|
if (!root)
|
|
|
|
Panic("No boot disk!");
|
|
|
|
VFS_MountRoot(root);
|
|
|
|
|
2014-07-14 06:16:54 +00:00
|
|
|
Critical_Exit();
|
2014-09-05 01:21:18 +00:00
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Create the idle thread
|
|
|
|
*/
|
2014-07-24 01:07:07 +00:00
|
|
|
Thread *thr = Thread_KThreadCreate(&Machine_IdleThread, NULL);
|
|
|
|
if (thr == NULL) {
|
|
|
|
kprintf("Couldn't create idle thread!\n");
|
|
|
|
}
|
2015-01-20 23:07:31 +00:00
|
|
|
Sched_SetRunnable(thr);
|
2014-07-24 01:07:07 +00:00
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Load the init processor
|
|
|
|
*/
|
2014-07-28 00:09:31 +00:00
|
|
|
Loader_LoadInit();
|
|
|
|
|
2014-07-13 20:07:19 +00:00
|
|
|
breakpoint();
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 23:06:38 +00:00
|
|
|
/*
|
|
|
|
* Machine_InitAP --
|
|
|
|
*
|
|
|
|
* Shorter initialization routine for co-processors.
|
|
|
|
*/
|
2015-02-16 01:00:55 +00:00
|
|
|
void Machine_InitAP()
|
|
|
|
{
|
2015-02-16 03:15:18 +00:00
|
|
|
Critical_Enter();
|
|
|
|
|
|
|
|
// Setup CPU state
|
2015-02-16 01:00:55 +00:00
|
|
|
Trap_InitAP();
|
|
|
|
PMap_InitAP();
|
|
|
|
Machine_GDTInit();
|
|
|
|
Machine_TSSInit();
|
2015-02-16 21:32:14 +00:00
|
|
|
Machine_SyscallInit();
|
2015-02-25 00:57:30 +00:00
|
|
|
clts(); // Enable FPU/XMM
|
2015-02-16 01:00:55 +00:00
|
|
|
|
2015-02-16 03:15:18 +00:00
|
|
|
// Setup LAPIC
|
2015-02-16 02:50:43 +00:00
|
|
|
LAPIC_Init();
|
|
|
|
|
2015-02-16 03:15:18 +00:00
|
|
|
// Boot processor
|
|
|
|
MP_InitAP();
|
|
|
|
Thread_InitAP();
|
|
|
|
Critical_Exit();
|
2015-02-16 01:00:55 +00:00
|
|
|
|
2015-02-16 03:15:18 +00:00
|
|
|
Machine_IdleThread(NULL);
|
2015-02-16 01:00:55 +00:00
|
|
|
}
|
|
|
|
|