v2 initial commit
This commit is contained in:
parent
2c97dba9f7
commit
d2ef885e8d
118 changed files with 2420 additions and 9320 deletions
57
init/acpi.c
57
init/acpi.c
|
|
@ -1,57 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/acpi.h>
|
||||
#include <dev/apic.h>
|
||||
|
||||
#include <limine/limine.h>
|
||||
|
||||
struct limine_rsdp_request rsdp_req = { .id = LIMINE_RSDP_REQUEST, .revision = 0 };
|
||||
|
||||
static struct rsdp *rsdp = NULL;
|
||||
struct madt *__madt = NULL;
|
||||
struct mcfg *__mcfg = NULL;
|
||||
|
||||
static void rsdt_parse(struct rsdt *rsdt)
|
||||
{
|
||||
int num_sdt = (rsdt->h.length - sizeof(rsdt->h)) >> 2;
|
||||
|
||||
#ifdef KDEBUG
|
||||
kprintf(LOG_DEBUG "Number of RSDT entries: %d\n", num_sdt);
|
||||
#endif /* KDEBUG */
|
||||
|
||||
for (int i = 0; i < num_sdt; i++) {
|
||||
/* 2 steps silences warning */
|
||||
uintptr_t sdt_addr = (uintptr_t)(rsdt->sdt[i] | hhdm_start);
|
||||
struct acpi_sdt_header *h = (struct acpi_sdt_header *)sdt_addr;
|
||||
|
||||
if (!(memcmp(h->signature, "APIC", 4))) {
|
||||
__madt = (struct madt *)h;
|
||||
}
|
||||
|
||||
if (!(memcmp(h->signature, "MCFG", 4))) {
|
||||
__mcfg = (struct mcfg *)h;
|
||||
}
|
||||
|
||||
#ifdef KDEBUG
|
||||
char buf[8] = { 0 };
|
||||
memcpy(buf, h->signature, 4);
|
||||
kprintf(LOG_DEBUG "RSDT entry: %s %d %x \n", buf, h->revision, h->checksum);
|
||||
#endif /* KDEBUG */
|
||||
}
|
||||
}
|
||||
|
||||
void acpi_init()
|
||||
{
|
||||
rsdp_req.response->address = (void *)((uintptr_t)rsdp_req.response->address & ~(hhdm_start));
|
||||
rsdp = kmap_device(rsdp_req.response->address, 0x1000);
|
||||
|
||||
/* 2 steps silences warning */
|
||||
uintptr_t rsdt_addr = (uintptr_t)(rsdp->rsdt_addr & ~(hhdm_start));
|
||||
struct rsdt *rsdt = kmap_device((void *)rsdt_addr, 0x4000);
|
||||
rsdt_parse(rsdt);
|
||||
|
||||
#ifdef KDEBUG
|
||||
kprintf(LOG_DEBUG "ACPI version: %d\n", rsdp->revision);
|
||||
#endif /* KDEBUG */
|
||||
kprintf(LOG_SUCCESS "ACPI initialized\n");
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#define __ASM__
|
||||
#include <kernel/gdt.h>
|
||||
#include <kernel/idt.h>
|
||||
|
||||
/*
|
||||
* Data section
|
||||
*/
|
||||
.data
|
||||
|
||||
.global __gdtr
|
||||
__gdtr:
|
||||
.word 0
|
||||
.quad 0
|
||||
.global __gdt
|
||||
__gdt:
|
||||
.skip GDT_SIZE
|
||||
|
||||
.global __idtr
|
||||
__idtr:
|
||||
.word 0
|
||||
.quad 0
|
||||
.global __idt
|
||||
__idt:
|
||||
.skip IDT_SIZE
|
||||
|
||||
/*
|
||||
* Code Section
|
||||
*/
|
||||
.text
|
||||
|
||||
.global gdt_load
|
||||
gdt_load:
|
||||
lgdt __gdtr
|
||||
ret
|
||||
|
||||
.global idt_load
|
||||
idt_load:
|
||||
lidt __idtr
|
||||
ret
|
||||
|
||||
.global ltr
|
||||
ltr:
|
||||
ltr %di
|
||||
ret
|
||||
72
init/gdt.c
72
init/gdt.c
|
|
@ -1,72 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/gdt.h>
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
void gdt_load();
|
||||
|
||||
static int gdt_index = 0;
|
||||
static int tss_index = 0;
|
||||
static struct tss *tss_table = NULL;
|
||||
|
||||
spinlock_t gdt_lock = 0;
|
||||
|
||||
void gdt_insert(uint8_t offset, uint8_t access, uint8_t flags)
|
||||
{
|
||||
struct gdt_desc *desc = (__gdt + offset);
|
||||
desc->limit = 0;
|
||||
desc->base = 0;
|
||||
desc->base_2 = 0;
|
||||
desc->base_3 = 0;
|
||||
|
||||
desc->access = access;
|
||||
desc->flags = flags;
|
||||
gdt_index++;
|
||||
}
|
||||
|
||||
uint16_t gdt_insert_tss(uintptr_t rsp0)
|
||||
{
|
||||
spinlock_acquire(&gdt_lock);
|
||||
|
||||
tss_table[tss_index].rsp0 = rsp0;
|
||||
|
||||
struct gdt_desc_sys *desc = (struct gdt_desc_sys *)((struct gdt_desc *)__gdt + gdt_index);
|
||||
|
||||
paddr_t tss_addr = (paddr_t)(tss_table + tss_index);
|
||||
desc->limit = sizeof(struct tss) - 1;
|
||||
desc->base = tss_addr & 0xFFFFFF;
|
||||
desc->base_2 = (tss_addr >> 16) & 0xFF;
|
||||
desc->base_3 = (tss_addr >> 24) & 0xFF;
|
||||
desc->base_4 = (tss_addr >> 32) & 0xFFFFFFFF;
|
||||
desc->access = 0xE9;
|
||||
desc->flags = 0x00;
|
||||
|
||||
tss_index += 1;
|
||||
gdt_index += 2;
|
||||
|
||||
spinlock_release(&gdt_lock);
|
||||
/* return the index of the TSS descriptor */
|
||||
return (gdt_index - 2) * sizeof(struct gdt_desc);
|
||||
}
|
||||
|
||||
void gdt_init()
|
||||
{
|
||||
tss_table = kzalloc(sizeof(struct tss) * 128, ALLOC_KERN);
|
||||
|
||||
gdt_insert(0, 0, 0);
|
||||
gdt_insert(1, GDT_ACCESS_CODE_RING0, 0xA0);
|
||||
gdt_insert(2, GDT_ACCESS_DATA_RING0, 0xC0);
|
||||
gdt_insert(3, GDT_ACCESS_CODE_RING0, 0xA0);
|
||||
gdt_insert(4, GDT_ACCESS_DATA_RING0, 0xC0);
|
||||
gdt_insert(5, GDT_ACCESS_CODE_RING0, 0xA0);
|
||||
gdt_insert(6, GDT_ACCESS_DATA_RING0, 0xC0);
|
||||
gdt_insert(7, GDT_ACCESS_DATA_RING3, 0xC0);
|
||||
gdt_insert(8, GDT_ACCESS_CODE_RING3, 0xA0);
|
||||
|
||||
__gdtr.size = (sizeof(__gdt) - 1);
|
||||
__gdtr.offset = (uint64_t)__gdt;
|
||||
|
||||
gdt_load();
|
||||
|
||||
kprintf(LOG_SUCCESS "GDT initialized\n");
|
||||
}
|
||||
182
init/irq.c
182
init/irq.c
|
|
@ -1,182 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/gdt.h>
|
||||
#include <kernel/idt.h>
|
||||
#include <kernel/irq.h>
|
||||
#include <dev/pic.h>
|
||||
#include <dev/serial.h>
|
||||
|
||||
#define exception(_i) _exception_##_i
|
||||
#define exception_decl(_j) void exception(_j)()
|
||||
|
||||
#define irq_decl(_i) void _irq_##_i()
|
||||
#define __irq(_i) _irq_##_i
|
||||
|
||||
static bool irq_mapped[16] = { false };
|
||||
static void *irq_handlers[16] = { NULL };
|
||||
|
||||
void gate_syscall_int80();
|
||||
|
||||
exception_decl(0x00);
|
||||
exception_decl(0x01);
|
||||
exception_decl(0x02);
|
||||
exception_decl(0x03);
|
||||
exception_decl(0x04);
|
||||
exception_decl(0x05);
|
||||
exception_decl(0x06);
|
||||
exception_decl(0x07);
|
||||
exception_decl(0x08);
|
||||
exception_decl(0x09);
|
||||
exception_decl(0x0A);
|
||||
exception_decl(0x0B);
|
||||
exception_decl(0x0C);
|
||||
exception_decl(0x0D);
|
||||
exception_decl(0x0E);
|
||||
exception_decl(0x0F);
|
||||
exception_decl(0x10);
|
||||
exception_decl(0x11);
|
||||
exception_decl(0x12);
|
||||
exception_decl(0x13);
|
||||
exception_decl(0x14);
|
||||
exception_decl(0x15);
|
||||
exception_decl(0x16);
|
||||
exception_decl(0x17);
|
||||
exception_decl(0x18);
|
||||
exception_decl(0x19);
|
||||
exception_decl(0x1A);
|
||||
exception_decl(0x1B);
|
||||
exception_decl(0x1C);
|
||||
exception_decl(0x1D);
|
||||
exception_decl(0x1E);
|
||||
exception_decl(0x1F);
|
||||
|
||||
irq_decl(0x00);
|
||||
irq_decl(0x01);
|
||||
irq_decl(0x02);
|
||||
irq_decl(0x03);
|
||||
irq_decl(0x04);
|
||||
irq_decl(0x05);
|
||||
irq_decl(0x06);
|
||||
irq_decl(0x07);
|
||||
irq_decl(0x08);
|
||||
irq_decl(0x09);
|
||||
irq_decl(0x0A);
|
||||
irq_decl(0x0B);
|
||||
irq_decl(0x0C);
|
||||
irq_decl(0x0D);
|
||||
irq_decl(0x0E);
|
||||
irq_decl(0x0F);
|
||||
|
||||
void idt_load(); /* descriptor.S */
|
||||
|
||||
void idt_insert(uint16_t gate, uint8_t type, uint8_t ist, uint16_t segment, void *func)
|
||||
{
|
||||
uintptr_t f = (uintptr_t)(func);
|
||||
struct irqdesc *irqdesc = (__idt + gate);
|
||||
irqdesc->offset_low = f & 0xFFFF;
|
||||
irqdesc->offset_mid = ((f >> 0x10) & 0xFFFF);
|
||||
irqdesc->offset_high = ((f >> 0x20) & 0xFFFFFFFF);
|
||||
|
||||
irqdesc->segment = segment;
|
||||
irqdesc->ist = ist;
|
||||
irqdesc->attrs = type;
|
||||
}
|
||||
|
||||
int irq_map(uint8_t irq, void *func)
|
||||
{
|
||||
if (irq_mapped[irq])
|
||||
return 1;
|
||||
|
||||
irq_mapped[irq] = true;
|
||||
irq_handlers[irq] = func;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void irq_unmap(uint8_t irq)
|
||||
{
|
||||
irq_mapped[irq] = false;
|
||||
irq_handlers[irq] = NULL;
|
||||
idt_insert(irq + 0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, NULL);
|
||||
}
|
||||
|
||||
int irq_highest_free()
|
||||
{
|
||||
for (int i = 14; i >= 0; i--) {
|
||||
if (!irq_mapped[i])
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void irq(int irq)
|
||||
{
|
||||
if (irq_handlers[irq] != NULL) {
|
||||
void (*handler)() = irq_handlers[irq];
|
||||
handler();
|
||||
}
|
||||
}
|
||||
|
||||
void idt_init()
|
||||
{
|
||||
idt_insert(0x00, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x00));
|
||||
idt_insert(0x01, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x01));
|
||||
idt_insert(0x02, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x02));
|
||||
idt_insert(0x03, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x03));
|
||||
idt_insert(0x04, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x04));
|
||||
idt_insert(0x05, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x05));
|
||||
idt_insert(0x06, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x06));
|
||||
idt_insert(0x07, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x07));
|
||||
idt_insert(0x08, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x08));
|
||||
idt_insert(0x09, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x09));
|
||||
idt_insert(0x0A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0A));
|
||||
idt_insert(0x0B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0B));
|
||||
idt_insert(0x0C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0C));
|
||||
idt_insert(0x0D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0D));
|
||||
idt_insert(0x0E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0E));
|
||||
idt_insert(0x0F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x0F));
|
||||
idt_insert(0x10, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x10));
|
||||
idt_insert(0x11, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x11));
|
||||
idt_insert(0x12, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x12));
|
||||
idt_insert(0x13, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x13));
|
||||
idt_insert(0x14, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x14));
|
||||
idt_insert(0x15, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x15));
|
||||
idt_insert(0x16, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x16));
|
||||
idt_insert(0x17, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x17));
|
||||
idt_insert(0x18, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x18));
|
||||
idt_insert(0x19, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x19));
|
||||
idt_insert(0x1A, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1A));
|
||||
idt_insert(0x1B, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1B));
|
||||
idt_insert(0x1C, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1C));
|
||||
idt_insert(0x1D, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1D));
|
||||
idt_insert(0x1E, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1E));
|
||||
idt_insert(0x1F, GATE_TRAP, 0, GDT_SEGMENT_CODE_RING0, exception(0x1F));
|
||||
|
||||
idt_insert(0x20, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x00));
|
||||
idt_insert(0x21, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x01));
|
||||
idt_insert(0x22, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x02));
|
||||
idt_insert(0x23, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x03));
|
||||
idt_insert(0x24, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x04));
|
||||
idt_insert(0x25, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x05));
|
||||
idt_insert(0x26, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x06));
|
||||
idt_insert(0x27, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x07));
|
||||
idt_insert(0x28, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x08));
|
||||
idt_insert(0x29, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x09));
|
||||
idt_insert(0x2A, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0A));
|
||||
idt_insert(0x2B, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0B));
|
||||
idt_insert(0x2C, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0C));
|
||||
idt_insert(0x2D, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0D));
|
||||
idt_insert(0x2E, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0E));
|
||||
idt_insert(0x2F, GATE_INTR, 0, GDT_SEGMENT_CODE_RING0, __irq(0x0F));
|
||||
|
||||
idt_insert(0x80, GATE_INTR_DPL3, 0, GDT_SEGMENT_CODE_RING0, gate_syscall_int80);
|
||||
|
||||
irq_map(4, serial_trap);
|
||||
|
||||
__idtr.size = (IDT_SIZE - 1);
|
||||
__idtr.offset = (uint64_t)__idt;
|
||||
idt_load();
|
||||
|
||||
kprintf(LOG_SUCCESS "IDT successfully loaded\n");
|
||||
}
|
||||
276
init/main.c
276
init/main.c
|
|
@ -1,264 +1,46 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/acpi.h>
|
||||
#include <kernel/gdt.h>
|
||||
#include <kernel/idt.h>
|
||||
#include <kernel/irq.h>
|
||||
#include <kernel/buddy.h>
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/pio.h>
|
||||
#include <kernel/block.h>
|
||||
#include <kernel/proc.h>
|
||||
#include <kernel/trap.h>
|
||||
#include <kernel/elf.h>
|
||||
#include <kernel/device.h>
|
||||
#include <kernel/requests.h>
|
||||
|
||||
#include <dev/pic.h>
|
||||
#include <dev/serial.h>
|
||||
#include <dev/pci.h>
|
||||
#include <dev/ahci.h>
|
||||
#include <dev/apic.h>
|
||||
#include <kernel/page.h>
|
||||
|
||||
#include <lib/stack.h>
|
||||
#include <lib/sem.h>
|
||||
#include <drivers/tty.h>
|
||||
#include <drivers/serial.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
#include <fs/devfs.h>
|
||||
#include <drivers/apic.h>
|
||||
|
||||
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
|
||||
#include <limine/limine.h>
|
||||
#include <lib/stdio.h>
|
||||
#include <lib/rbtree.h>
|
||||
|
||||
struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST, .revision = 0 };
|
||||
struct limine_kernel_file_request module_req = { .id = LIMINE_KERNEL_FILE_REQUEST, .revision = 0 };
|
||||
struct limine_smp_request smp_req = { .id = LIMINE_SMP_REQUEST, .revision = 0 };
|
||||
struct tty *tty_printf = NULL;
|
||||
|
||||
static char *kcmdline;
|
||||
static char *kpath;
|
||||
static struct limine_uuid kroot_disk;
|
||||
static struct limine_uuid kroot_part;
|
||||
|
||||
uintptr_t hhdm_start;
|
||||
uintptr_t data_end;
|
||||
uintptr_t bss_start;
|
||||
uintptr_t bss_end;
|
||||
|
||||
void gdt_load();
|
||||
void idt_load();
|
||||
void yield();
|
||||
void ap_kmain(struct limine_smp_info *info);
|
||||
void load_stack_and_jump(uintptr_t rsp, uintptr_t rbp, void *func, void *arg);
|
||||
|
||||
uintptr_t kstacks[256] = { 0 };
|
||||
|
||||
static void do_dummy_proc()
|
||||
{
|
||||
sti();
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
|
||||
static inline void kexec(char *cmd)
|
||||
{
|
||||
pid_t pid = elf_load_proc(cmd);
|
||||
|
||||
if (pid < 0) {
|
||||
kprintf("Failed to load %s: %s\n", cmd, strerror(-pid));
|
||||
return;
|
||||
}
|
||||
|
||||
kprintf("Loaded %s as PID %d\n", cmd, pid);
|
||||
|
||||
struct proc *proc = proc_get(pid);
|
||||
if (!proc) {
|
||||
kprintf("Failed to get proc %d\n", pid);
|
||||
return;
|
||||
}
|
||||
|
||||
void _return_to_user(struct procregs * regs, paddr_t cr3);
|
||||
proc_set_current(pid);
|
||||
_return_to_user(&proc->regs, proc->cr3);
|
||||
}
|
||||
|
||||
void panic()
|
||||
{
|
||||
kprintf(LOG_ERROR "KERNEL PANIC\n");
|
||||
cli();
|
||||
while (1)
|
||||
yield();
|
||||
}
|
||||
|
||||
char *kcmdline_get_symbol(const char *sym)
|
||||
{
|
||||
/* symbol=value */
|
||||
/* only parse until the space after the value */
|
||||
size_t sym_len = strlen(sym);
|
||||
size_t cmd_len = strlen(kcmdline);
|
||||
|
||||
char *cmdline_copy = kmalloc(cmd_len + 1, ALLOC_KERN);
|
||||
if (!cmdline_copy) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(cmdline_copy, kcmdline);
|
||||
|
||||
char *strtok_last = NULL;
|
||||
char *token = strtok(cmdline_copy, " ", &strtok_last);
|
||||
while (token) {
|
||||
if (!strncmp(token, sym, sym_len)) {
|
||||
size_t ret_len = strlen(token + sym_len + 1);
|
||||
char *ret = kzalloc(ret_len + 1, ALLOC_KERN);
|
||||
if (!ret) {
|
||||
kfree(cmdline_copy);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(ret, token + sym_len + 1);
|
||||
|
||||
kfree(cmdline_copy);
|
||||
return ret;
|
||||
}
|
||||
token = strtok(NULL, " ", &strtok_last);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* kmain
|
||||
*
|
||||
* main kernel entry point, executed solely by the BSP
|
||||
*/
|
||||
void kmain()
|
||||
{
|
||||
cli();
|
||||
hhdm_start = hhdm_req.response->offset;
|
||||
data_end = (uintptr_t)(&__data_end);
|
||||
bss_start = (uintptr_t)(&__bss_start);
|
||||
bss_end = (uintptr_t)(&__bss_end);
|
||||
/* init tty ASAP */
|
||||
requests_init();
|
||||
mem_init();
|
||||
buddy_init();
|
||||
rbt_slab_init();
|
||||
device_layer_init();
|
||||
tty_layer_init();
|
||||
|
||||
/* Initialize early kernel memory array */
|
||||
const uintptr_t one_gb = 0x40000000;
|
||||
char kmem[one_gb] ALIGN(0x1000);
|
||||
mem_early_init(kmem, one_gb);
|
||||
dev_t serial_port = arch_serial_init();
|
||||
tty_printf = tty_create("ttyS0", serial_port);
|
||||
|
||||
stack_init();
|
||||
gdt_init();
|
||||
idt_init();
|
||||
int rows;
|
||||
int cols;
|
||||
|
||||
struct limine_kernel_file_response *resp = module_req.response;
|
||||
tty_get_size(tty_printf, &rows, &cols);
|
||||
tty_setlf(tty_printf);
|
||||
tty_clear_screen(tty_printf);
|
||||
tty_set_cursor_pos(tty_printf, 0, 0);
|
||||
|
||||
struct limine_file *kfile = (struct limine_file *)((paddr_t)resp->kernel_file | hhdm_start);
|
||||
/* start the kernel */
|
||||
|
||||
kcmdline = kzalloc(strlen(kfile->cmdline) + 1, ALLOC_KERN);
|
||||
strcpy(kcmdline, kfile->cmdline);
|
||||
|
||||
kpath = kzalloc(strlen(kfile->path) + 1, ALLOC_KERN);
|
||||
strcpy(kpath, kfile->path);
|
||||
|
||||
kroot_disk = kfile->gpt_disk_uuid;
|
||||
kroot_part = kfile->gpt_part_uuid;
|
||||
|
||||
#ifdef KDEBUG
|
||||
kprintf(LOG_DEBUG "Kernel file: path='%s' cmdline='%s'\n", kpath, kcmdline);
|
||||
kprintf(LOG_DEBUG "Root partition: disk=%X-%X part=%X-%X\n", kroot_disk.a, kroot_disk.b, kroot_part.a, kroot_part.b);
|
||||
#endif /* KDEBUG */
|
||||
|
||||
acpi_init();
|
||||
|
||||
pci_init();
|
||||
/* finished init, now load TSS */
|
||||
#ifdef KDEBUG
|
||||
kprintf(LOG_DEBUG "Inserting TSS for BSP\n");
|
||||
#endif
|
||||
|
||||
void *kstack = buddy_alloc(KSTACK_SIZE);
|
||||
uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8;
|
||||
|
||||
uint16_t tss = gdt_insert_tss(ptr);
|
||||
ltr(tss);
|
||||
|
||||
pic_init();
|
||||
apic_init();
|
||||
|
||||
/* init the application processors */
|
||||
struct limine_smp_response *smp_resp = smp_req.response;
|
||||
proc_init(smp_resp->cpu_count);
|
||||
void syscall_init();
|
||||
syscall_init();
|
||||
|
||||
ahci_init();
|
||||
|
||||
char *root_path = kcmdline_get_symbol("root");
|
||||
if (!root_path) {
|
||||
kprintf(LOG_ERROR "No root= parameter specified\n");
|
||||
|
||||
kerror_print_blkdevs();
|
||||
panic();
|
||||
} else {
|
||||
vfs_init(root_path);
|
||||
}
|
||||
|
||||
devfs_init();
|
||||
|
||||
sem_t *init_sem = sem_create(0);
|
||||
|
||||
for (unsigned i = 0; i < smp_resp->cpu_count; i++) {
|
||||
struct limine_smp_info *info = smp_resp->cpus[i];
|
||||
info->extra_argument = (uint64_t)init_sem;
|
||||
|
||||
if (info->lapic_id == smp_resp->bsp_lapic_id)
|
||||
continue;
|
||||
|
||||
info->goto_address = ap_kmain;
|
||||
}
|
||||
|
||||
/* wait for the application processors to finish their initialization */
|
||||
for (unsigned i = 0; i < smp_resp->cpu_count - 1; i++) {
|
||||
while (sem_trywait(init_sem))
|
||||
(void)0;
|
||||
}
|
||||
|
||||
sem_destroy(init_sem);
|
||||
exception_init();
|
||||
serial_init();
|
||||
irq_map(0, trap_sched);
|
||||
apic_enable_timer();
|
||||
|
||||
struct proc *dummy_proc = proc_createv(PT_KERN);
|
||||
proc_init_memory(dummy_proc, 0);
|
||||
proc_set_flags(dummy_proc, 0x46);
|
||||
proc_set_exec_addr(dummy_proc, (uintptr_t)do_dummy_proc);
|
||||
proc_set_state(dummy_proc->pid, PROC_ALLOWSCHED);
|
||||
|
||||
load_stack_and_jump(ptr, ptr, kexec, "/bin/shtest.elf");
|
||||
}
|
||||
|
||||
/* kmain for application processors
|
||||
*
|
||||
* This code enables basic processor functions and parks the processor, waiting
|
||||
* for future scheduling
|
||||
*/
|
||||
void ap_kmain(struct limine_smp_info *info)
|
||||
{
|
||||
gdt_load();
|
||||
idt_load();
|
||||
cr3_write(kcr3);
|
||||
|
||||
/* enable the APIC */
|
||||
lapic_enable();
|
||||
|
||||
void *kstack = buddy_alloc(KSTACK_SIZE);
|
||||
uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8;
|
||||
|
||||
kstacks[info->lapic_id] = (uintptr_t)ptr;
|
||||
|
||||
/* insert the TSS for each processor into the GDT */
|
||||
#ifdef KDEBUG
|
||||
kprintf(LOG_DEBUG "Inserting TSS for AP #%d\n", info->lapic_id);
|
||||
#endif
|
||||
uint16_t tss = gdt_insert_tss(ptr);
|
||||
ltr(tss);
|
||||
|
||||
sem_t *init_sem = (sem_t *)info->extra_argument;
|
||||
sem_post(init_sem);
|
||||
|
||||
load_stack_and_jump(ptr, ptr, yield, NULL);
|
||||
page_init();
|
||||
kprintf(COL_CYAN_BLACK "Welcome to vbos!\n" COL_RESET);
|
||||
yield();
|
||||
}
|
||||
|
|
|
|||
19
init/msr.S
19
init/msr.S
|
|
@ -1,19 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
.global rdmsr
|
||||
rdmsr:
|
||||
movl %edi, %ecx
|
||||
rdmsr
|
||||
shlq $0x20, %rdx
|
||||
orq %rdx, %rax
|
||||
|
||||
ret
|
||||
|
||||
.global wrmsr
|
||||
wrmsr:
|
||||
movq %rsi, %rdx
|
||||
shrq $0x20, %rdx
|
||||
movl %esi, %eax
|
||||
movl %edi, %ecx
|
||||
wrmsr
|
||||
|
||||
ret
|
||||
23
init/pio.c
23
init/pio.c
|
|
@ -1,23 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/pio.h>
|
||||
|
||||
#define genio(suffix, type) \
|
||||
inline void out##suffix(uint16_t port, type value) \
|
||||
{ \
|
||||
__asm__ volatile("out" #suffix " %0, %1" : : "a"(value), "Nd"(port)); \
|
||||
} \
|
||||
inline type in##suffix(uint16_t port) \
|
||||
{ \
|
||||
type ret; \
|
||||
__asm__ volatile("in" #suffix " %1, %0" : "=a"(ret) : "Nd"(port)); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
inline void iowait()
|
||||
{
|
||||
outb(0x80, 0);
|
||||
}
|
||||
|
||||
genio(b, uint8_t);
|
||||
genio(w, uint16_t);
|
||||
genio(l, uint32_t);
|
||||
27
init/requests.c
Normal file
27
init/requests.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include "kernel/requests.h"
|
||||
#include "limine/limine.h"
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define REQUEST __attribute__((used, section(".limine_requests"))) static volatile
|
||||
|
||||
REQUEST struct limine_executable_address_request kern_addr_req = { .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, .revision = 0 };
|
||||
REQUEST struct limine_memmap_request memmap_req = { .id = LIMINE_MEMMAP_REQUEST_ID, .revision = 0 };
|
||||
REQUEST struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST_ID, .revision = 0 };
|
||||
|
||||
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
|
||||
|
||||
struct limine_executable_address_response *kern_addr_res = NULL;
|
||||
struct limine_memmap_response *kern_map_res = NULL;
|
||||
struct limine_hhdm_response *kern_hhdm_res = NULL;
|
||||
|
||||
uintptr_t hhdm_start = 0;
|
||||
|
||||
void requests_init()
|
||||
{
|
||||
kern_addr_res = kern_addr_req.response;
|
||||
kern_map_res = memmap_req.response;
|
||||
kern_hhdm_res = hhdm_req.response;
|
||||
|
||||
hhdm_start = kern_hhdm_res->offset;
|
||||
}
|
||||
72
init/trap.c
72
init/trap.c
|
|
@ -1,72 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/proc.h>
|
||||
|
||||
/* explicit handle list */
|
||||
void (*exception_handlers[32])(int error) = { NULL };
|
||||
|
||||
static const char *exception_names[] = {
|
||||
"Divide-by-zero Error",
|
||||
"Debug",
|
||||
"Non-maskable Interrupt",
|
||||
"Breakpoint",
|
||||
"Overflow",
|
||||
"Bound Range Exceeded",
|
||||
"Invalid Opcode",
|
||||
"Device Not Available",
|
||||
"Double Fault",
|
||||
"Coprocessor Segment Overrun",
|
||||
"Invalid TSS",
|
||||
"Segment Not Present",
|
||||
"Stack-Segment Fault",
|
||||
"General Protection Fault",
|
||||
"Page Fault",
|
||||
"Reserved",
|
||||
"x87 Floating-Point Exception",
|
||||
"Alignment Check",
|
||||
"Machine Check",
|
||||
"SIMD Floating-Point Exception",
|
||||
"Virtualization Exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Invalid exception",
|
||||
"Security Exception",
|
||||
"Reserved",
|
||||
"Triple Fault",
|
||||
};
|
||||
|
||||
static void exception_page_fault(int err)
|
||||
{
|
||||
uint64_t cr2 = cr2_read();
|
||||
|
||||
kprintf(LOG_ERROR "Page fault at %Xh, error code %xh\n", cr2, err);
|
||||
}
|
||||
|
||||
void exception(int vector, int error)
|
||||
{
|
||||
pid_t pid = getpid();
|
||||
|
||||
kprintf("Process %d terminated: %s(%xh)\n", pid, exception_names[vector], error);
|
||||
|
||||
struct proc *proc = proc_find(pid);
|
||||
|
||||
if (exception_handlers[vector])
|
||||
exception_handlers[vector](error);
|
||||
|
||||
if(proc->pid == 0)
|
||||
panic();
|
||||
|
||||
proc_set_current(0);
|
||||
proc_term(pid);
|
||||
schedule();
|
||||
}
|
||||
|
||||
void exception_init()
|
||||
{
|
||||
exception_handlers[0x0E] = exception_page_fault;
|
||||
}
|
||||
Loading…
Reference in a new issue