This is a rather large commit that is widely focused on cleaning things out of the 'junk drawer' that is src/include. Most notably, several things that were put in there because they needed somewhere where both the kernel, boot, and init could read them have been moved to a new lib, 'bootproto'. - Moved kernel_args.h and init_args.h to bootproto as kernel.h and init.h, respectively. - Moved counted.h and pointer_manipulation.h into util, renaming the latter to util/pointers.h. - Created a new src/include/arch for very arch-dependent definitions, and moved some kernel_memory.h constants like frame size, page table entry count, etc to arch/amd64/memory.h. Also created arch/memory.h which detects platform and includes the former. - Got rid of kernel_memory.h entirely in favor of a new, cog-based approach. The new definitions/memory_layout.csv lists memory regions in descending order from the top of memory, their sizes, and whether they are shared outside the kernel (ie, boot needs to know them). The new header bootproto/memory.h exposes the addresses of the shared regions, while the kernel's memory.h gains the start and size of all the regions. Also renamed the badly-named page-offset area the linear area. - The python build scripts got a few new features: the ability to parse the csv mentioned above in a new memory.py module; the ability to add dependencies to existing source files (The list of files that I had to pull out of the main list just to add them with the dependency on memory.h was getting too large. So I put them back into the sources list, and added the dependency post-hoc.); and the ability to reference 'source_root', 'build_root', and 'module_root' variables in .module files. - Some utility functions that were in the kernel's memory.h got moved to util/pointers.h and util/misc.h, and misc.h's byteswap was renamed byteswap32 to be more specific.
180 lines
4.8 KiB
C++
180 lines
4.8 KiB
C++
#include <stdint.h>
|
|
|
|
|
|
#include "assert.h"
|
|
#include "cpu.h"
|
|
#include "device_manager.h"
|
|
#include "idt.h"
|
|
#include "interrupts.h"
|
|
#include "io.h"
|
|
#include "memory.h"
|
|
#include "objects/process.h"
|
|
#include "printf/printf.h"
|
|
#include "scheduler.h"
|
|
#include "vm_space.h"
|
|
|
|
static const uint16_t PIC1 = 0x20;
|
|
static const uint16_t PIC2 = 0xa0;
|
|
|
|
constexpr uintptr_t apic_eoi_addr = 0xfee000b0 + mem::linear_offset;
|
|
|
|
extern "C" {
|
|
void isr_handler(cpu_state*);
|
|
void irq_handler(cpu_state*);
|
|
}
|
|
|
|
uint8_t
|
|
get_irq(unsigned vector)
|
|
{
|
|
switch (vector) {
|
|
#define ISR(i, s, name)
|
|
#define EISR(i, s, name)
|
|
#define NISR(i, s, name)
|
|
#define IRQ(i, q, name) case i : return q;
|
|
#include "interrupt_isrs.inc"
|
|
#undef IRQ
|
|
#undef NISR
|
|
#undef EISR
|
|
#undef ISR
|
|
|
|
default: return 0xff;
|
|
}
|
|
}
|
|
|
|
void
|
|
disable_legacy_pic()
|
|
{
|
|
// Mask all interrupts
|
|
outb(PIC2+1, 0xfc);
|
|
outb(PIC1+1, 0xff);
|
|
|
|
// Start initialization sequence
|
|
outb(PIC1, 0x11); io_wait();
|
|
outb(PIC2, 0x11); io_wait();
|
|
|
|
// Remap into ignore ISRs
|
|
outb(PIC1+1, static_cast<uint8_t>(isr::isrIgnore0)); io_wait();
|
|
outb(PIC2+1, static_cast<uint8_t>(isr::isrIgnore8)); io_wait();
|
|
|
|
// Tell PICs about each other
|
|
outb(PIC1+1, 0x04); io_wait();
|
|
outb(PIC2+1, 0x02); io_wait();
|
|
}
|
|
|
|
void
|
|
isr_handler(cpu_state *regs)
|
|
{
|
|
uint8_t vector = regs->interrupt & 0xff;
|
|
if ((vector & 0xf0) == 0xf0) {
|
|
*reinterpret_cast<uint32_t *>(apic_eoi_addr) = 0;
|
|
return;
|
|
}
|
|
|
|
// Clear out the IST for this vector so we just keep using
|
|
// this stack
|
|
IDT &idt = IDT::current();
|
|
uint8_t old_ist = idt.get_ist(vector);
|
|
if (old_ist)
|
|
idt.set_ist(vector, 0);
|
|
|
|
char message[200];
|
|
|
|
switch (static_cast<isr>(vector)) {
|
|
|
|
case isr::isrDebug:
|
|
asm volatile ("mov %%dr0, %%r8" ::: "r8");
|
|
asm volatile ("mov %%dr1, %%r9" ::: "r9");
|
|
asm volatile ("mov %%dr2, %%r10" ::: "r10");
|
|
asm volatile ("mov %%dr3, %%r11" ::: "r11");
|
|
asm volatile ("mov %%dr4, %%r12" ::: "r12");
|
|
asm volatile ("mov %%dr5, %%r13" ::: "r13");
|
|
asm volatile ("mov %%dr6, %%r14" ::: "r14");
|
|
asm volatile ("mov %%dr7, %%r15" ::: "r15");
|
|
kassert(false, "Debug exception");
|
|
break;
|
|
|
|
case isr::isrDoubleFault:
|
|
kassert(false, "Double fault");
|
|
break;
|
|
|
|
case isr::isrGPFault:
|
|
if (regs->errorcode & 0xfff0) {
|
|
int index = (regs->errorcode & 0xffff) >> 4;
|
|
int ti = (regs->errorcode & 0x07) >> 1;
|
|
char const *table =
|
|
(ti & 1) ? "IDT" :
|
|
(!ti) ? "GDT" :
|
|
"LDT";
|
|
|
|
snprintf(message, sizeof(message), "General Protection Fault, error:%lx%s %s[%d]",
|
|
regs->errorcode, regs->errorcode & 1 ? " external" : "", table, index);
|
|
} else {
|
|
snprintf(message, sizeof(message), "General Protection Fault, error:%lx%s",
|
|
regs->errorcode, regs->errorcode & 1 ? " external" : "");
|
|
}
|
|
kassert(false, message);
|
|
break;
|
|
|
|
case isr::isrPageFault: {
|
|
uintptr_t cr2 = 0;
|
|
__asm__ __volatile__ ("mov %%cr2, %0" : "=r"(cr2));
|
|
|
|
bool user = cr2 < mem::kernel_offset;
|
|
vm_space::fault_type ft =
|
|
static_cast<vm_space::fault_type>(regs->errorcode);
|
|
|
|
vm_space &space = user
|
|
? process::current().space()
|
|
: vm_space::kernel_space();
|
|
|
|
if (cr2 && space.handle_fault(cr2, ft))
|
|
break;
|
|
|
|
snprintf(message, sizeof(message),
|
|
"Page fault: %016lx%s%s%s%s%s", cr2,
|
|
(regs->errorcode & 0x01) ? " present" : "",
|
|
(regs->errorcode & 0x02) ? " write" : "",
|
|
(regs->errorcode & 0x04) ? " user" : "",
|
|
(regs->errorcode & 0x08) ? " reserved" : "",
|
|
(regs->errorcode & 0x10) ? " ip" : "");
|
|
kassert(false, message);
|
|
}
|
|
break;
|
|
|
|
case isr::isrTimer:
|
|
scheduler::get().schedule();
|
|
break;
|
|
|
|
case isr::isrLINT0:
|
|
case isr::isrLINT1:
|
|
break;
|
|
|
|
case isr::isrSpurious:
|
|
// No EOI for the spurious interrupt
|
|
return;
|
|
|
|
default:
|
|
snprintf(message, sizeof(message), "Unknown interrupt 0x%lx", regs->interrupt);
|
|
kassert(false, message);
|
|
}
|
|
|
|
// Return the IST for this vector to what it was
|
|
if (old_ist)
|
|
idt.set_ist(vector, old_ist);
|
|
*reinterpret_cast<uint32_t *>(apic_eoi_addr) = 0;
|
|
}
|
|
|
|
void
|
|
irq_handler(cpu_state *regs)
|
|
{
|
|
uint8_t irq = get_irq(regs->interrupt);
|
|
if (! device_manager::get().dispatch_irq(irq)) {
|
|
char message[100];
|
|
snprintf(message, sizeof(message),
|
|
"Unknown IRQ: %d (vec 0x%lx)", irq, regs->interrupt);
|
|
kassert(false, message);
|
|
}
|
|
|
|
*reinterpret_cast<uint32_t *>(apic_eoi_addr) = 0;
|
|
}
|