[kernel] Clean up main.cpp and others

The kernel/main.cpp and kernel/memory_bootstrap.cpp files had become
something of a junk drawer. This change cleans them up in the following
ways:

- Most CPU initialization has moved to cpu.cpp, allowing several
  functions to be made static and removed from cpu.h
- Multi-core startup code has moved to the new smp.h and smp.cpp, and
  ap_startup.s has been renamed smp.s to match.
- run_constructors() has moved to memory_bootstrap.cpp, and all the
  functionality of that file has been hidden behind a new public
  interface mem::initialize().
- load_init_server() has moved from memory_bootstrap.cpp to main.cpp
This commit is contained in:
Justin C. Miller
2022-01-27 19:28:35 -08:00
parent 3f8dfbd5b2
commit fd25d3babc
15 changed files with 341 additions and 313 deletions

View File

@@ -1,3 +1,4 @@
#include <new>
#include <stdint.h>
#include <string.h>
@@ -10,7 +11,6 @@
#include "logger.h"
#include "msr.h"
#include "objects/thread.h"
#include "objects/vm_area.h"
#include "scheduler.h"
#include "syscall.h"
#include "tss.h"
@@ -22,6 +22,9 @@ panic_data *g_panic_data_p = &g_panic_data;
cpu_data g_bsp_cpu_data;
// Validate the required CPU features are present. Really, the bootloader already
// validated the required features, but still iterate the options and log about them.
void
cpu_validate()
{
@@ -45,18 +48,12 @@ cpu_validate()
#undef CPU_FEATURE_REQ
}
void
global_cpu_init()
{
memset(&g_panic_data, 0, sizeof(g_panic_data));
}
void
// Do early (before cpu_init) initialization work. Only needs to be called manually for
// the BSP, otherwise cpu_init will call it.
static void
cpu_early_init(cpu_data *cpu)
{
if (cpu->index == 0)
global_cpu_init();
cpu->idt->install();
cpu->gdt->install();
@@ -64,6 +61,70 @@ cpu_early_init(cpu_data *cpu)
wrmsr(msr::ia32_gs_base, reinterpret_cast<uintptr_t>(cpu));
}
cpu_data *
bsp_early_init()
{
cpu_validate();
memset(&g_panic_data, 0, sizeof(g_panic_data));
extern IDT &g_bsp_idt;
extern TSS &g_bsp_tss;
extern GDT &g_bsp_gdt;
extern uintptr_t idle_stack_end;
cpu_data *cpu = &g_bsp_cpu_data;
memset(cpu, 0, sizeof(cpu_data));
cpu->self = cpu;
cpu->idt = new (&g_bsp_idt) IDT;
cpu->tss = new (&g_bsp_tss) TSS;
cpu->gdt = new (&g_bsp_gdt) GDT {cpu->tss};
cpu->rsp0 = idle_stack_end;
cpu_early_init(cpu);
return cpu;
}
void
bsp_late_init()
{
// BSP didn't set up IST stacks yet
extern TSS &g_bsp_tss;
uint8_t ist_entries = IDT::used_ist_entries();
g_bsp_tss.create_ist_stacks(ist_entries);
uint64_t cr0, cr4;
asm ("mov %%cr0, %0" : "=r"(cr0));
asm ("mov %%cr4, %0" : "=r"(cr4));
uint64_t efer = rdmsr(msr::ia32_efer);
log::debug(logs::boot, "Control regs: cr0:%lx cr4:%lx efer:%lx", cr0, cr4, efer);
syscall_initialize();
}
cpu_data *
cpu_create(uint16_t id, uint16_t index)
{
// Set up the CPU data structures
IDT *idt = new IDT;
TSS *tss = new TSS;
GDT *gdt = new GDT {tss};
cpu_data *cpu = new cpu_data;
memset(cpu, 0, sizeof(cpu_data));
cpu->self = cpu;
cpu->id = id;
cpu->index = index;
cpu->idt = idt;
cpu->tss = tss;
cpu->gdt = gdt;
uint8_t ist_entries = IDT::used_ist_entries();
tss->create_ist_stacks(ist_entries);
return cpu;
}
void
cpu_init(cpu_data *cpu, bool bsp)
{
@@ -93,4 +154,17 @@ cpu_init(cpu_data *cpu, bool bsp)
wrmsr(msr::ia32_pat, pat);
cpu->idt->add_ist_entries();
uintptr_t apic_base =
device_manager::get().get_lapic_base();
lapic *apic = new lapic(apic_base);
cpu->apic = apic;
apic->enable();
if (bsp) {
// BSP never got an id, set that up now
cpu->id = apic->get_id();
apic->calibrate_timer();
}
}