kernel - various changes

+ vmm: higher half should not be identify-mapped
+ panic: now displays current process
+ kernel: fix physical addresses (ḑ̷̩̜̦̥̰͔̻͔̖͎̳̗̫̓̓͊́̒͜ơ̸͙͎̠͎̩̤̭̬̙͚̬̣͇̤̼̑̐̿̿͆͠͝ ̷̗̟̘͎̥̤̭̂͛͆́͂͝͠ͅN̴̨̛̥̩̺͚̺̠͙̼̙̯̱͚̫̊̐̅̀̌̏͆̋̕͜͝͠ͅö̵̪͚̞̞̜͎͉̦́́̍̀̃̋̇̄̓͊̎͝t̶̡͍̩̤̹̤̂̑̓͌͂̾̑̈́͒̾̾́͐͆͊͂ ̴̨̫̺̦̊̍̒͛͌̌͂͘̚̚͘͘͝I̶̥͚̯̖̙̩͂́͜d̸̢̡̗͉̠̹̒͠e̸̡̪̺͎͖͚̗̟̟̥͍͑̈̋̉̋̓̐̊̚͘͜n̸̡̍͐͗̈͌̀̓̃́́͠t̶̢͈͈̦̻̰͎̪̰̒̄͒̃̐͜ĩ̴͕̼̻͓͚͕̲̬̤͈̜̣̐̍́̾̀̏̏̑͒̚ͅf̷̡̨̼̻̠̠͔̪͍͛y̴͉͓̓͒̆̎̚ ̶̟͙͖̙̟͍̟͕̞̥̹͇̌̉́̑͗͋̀̕ͅͅM̷͙̬̲̓a̶̫̰̞̺̖̍̀p̶̡̨̡̗̖̹̩̫̯̞̬͋͂̏̍̾̽͜ͅ ̶̧̨̧̫͉̝̮̳͎͍̱̟̪̝̀̽͑̂̿̄̈̇̓͘Ḫ̶̨̨̗̣̪͓̺͙͈͙̀ḭ̶̧̡͇̹͙̩͍͎̮̤̦̜̻͎̞̔̐̇̉̓͒͛̅̿͊̍͆͘̕g̷͖͙͍͓̯̪̩̑̑͋̈́͌͐̊̀͝͠͝ĥ̴̢̡̫̪̟̞̭̟͕̖͎͊͑͛̆͝e̴̡̨̗̱̱͙͔̻̤͎͆̒̾̾̓̈͊̓ͅr̵̛͈̩͍̔̌̃̇͊̽̀̉̽̊͌̿́ ̷̨̡̛̩̹̹̇̇̈́̑̍̊͒̄́͛H̷̨̪̜̤͍̻͎̲̜͋́̆͋̂̚͘͘͜͠ą̷̠͓̫̲́́̽̉̒͌́̓ͅl̴̢̛͈̤̺̱̙̬̆̎̄̊̈́̐̾̏̿̕f̸̢̰͓̦̺̰̯͚̣̙͔̺̂͜͜)
This commit is contained in:
RaphProductions 2025-05-16 08:01:37 +02:00
parent a8e919b033
commit b2ec036055
32 changed files with 265 additions and 265 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@
*.iso
*.hdd
/compile_commands.json
/.idea

View file

@ -1,11 +1,11 @@
// #include "sys/log.h"
#include "arch/x86_64/smp.h"
#include "dev/ioapic.h"
#include "dev/lapic.h"
#include "mm/vmm.h"
#include "arch/x86_64/smp.h"
#include "sys/errhnd/panic.h"
#include <arch/x86_64/idt.h>
#include <stdbool.h>
#include <arch//x86_64/idt.h>
#include <sys/log.h>
__attribute__((aligned(0x10))) static idt_entry_t idt[256];
@ -60,7 +60,6 @@ void idt_init() {
log("idt - initialized\n");
}
void idt_int_handler(registers_t *regs) {
vmm_load_pagemap(vmm_kernel_pm);
// log("kernel - Interrupt %d\n", regs->int_no);

View file

@ -5,24 +5,14 @@
#define IA32_GS_MSR 0xC0000101
#define IA32_GS_KERNEL_MSR 0xC0000102
static inline void wrmsr(uint64_t msr, uint64_t value)
{
static inline void wrmsr(uint64_t msr, uint64_t value) {
uint32_t low = value & 0xFFFFFFFF;
uint32_t high = value >> 32;
asm volatile (
"wrmsr"
:
: "c"(msr), "a"(low), "d"(high)
);
asm volatile("wrmsr" : : "c"(msr), "a"(low), "d"(high));
}
static inline uint64_t rdmsr(uint64_t msr)
{
static inline uint64_t rdmsr(uint64_t msr) {
uint32_t low, high;
asm volatile (
"rdmsr"
: "=a"(low), "=d"(high)
: "c"(msr)
);
asm volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr));
return ((uint64_t)high << 32) | low;
}

View file

@ -1,9 +1,9 @@
#include "arch//x86_64/idt.h"
#include "arch/x86_64/idt.h"
#include "sched/sched.h"
#include <arch/x86_64/idt.h>
#include <arch/x86_64/pit.h>
#include <stdint.h>
#include <sys/log.h>
#include <arch//x86_64/idt.h>
#include <arch//x86_64/pit.h>
uint32_t tick = 0;
@ -18,9 +18,10 @@ void pit_init() {
uint16_t div = (uint16_t)(1193180 / 1000);
outb(0x40, (uint8_t)div);
outb(0x40, (uint8_t)(div >> 8));
idt_register_irq(0, pit_handler);
}
void pit_enable() { idt_register_irq(0, pit_handler); }
void pit_sleep(uint32_t ms) {
uint64_t start = tick;
while (tick - start < ms) {

3
kernel/src/arch/x86_64/pit.h Executable file → Normal file
View file

@ -1,12 +1,13 @@
#ifndef PIT_H
#define PIT_H
#include <arch/x86_64/io.h>
#include <stdint.h>
#include <arch//x86_64/io.h>
extern uint32_t tick;
void pit_init();
void pit_sleep(uint32_t ms);
void pit_enable();
#endif

View file

@ -1,12 +1,11 @@
#include "arch//x86_64/smp.h"
#include "arch/x86_64/smp.h"
#include "lib/spinlock.h"
#include "sys/log.h"
#include "limine.h"
#include "sys/log.h"
#include <stdint.h>
__attribute__((
used,
section(".limine_requests"))) static volatile struct limine_mp_request
used, section(".limine_requests"))) static volatile struct limine_mp_request
smp_request = {.id = LIMINE_MP_REQUEST, .revision = 0};
uint32_t bootstrap_lapic_id = 0;
@ -18,11 +17,13 @@ uint32_t ctr = 0;
void smp_entry(struct limine_mp_info *smp_info) {
// spinlock_acquire(&smp_lock);
log("smp - CPU %d started (LAPIC ID: %d)\n", smp_info->processor_id, smp_info->lapic_id);
log("smp - CPU %d started (LAPIC ID: %d)\n", smp_info->processor_id,
smp_info->lapic_id);
__atomic_fetch_add(&ctr, 1, __ATOMIC_SEQ_CST);
while (1)
;;
;
;
// spinlock_release(&smp_lock);
}
@ -36,12 +37,14 @@ void smp_init() {
if (smp_request.response->cpus[i]->lapic_id != bootstrap_lapic_id) {
uint32_t old_ctr = __atomic_load_n(&ctr, __ATOMIC_SEQ_CST);
__atomic_store_n(&smp_request.response->cpus[i]->goto_address, smp_entry, __ATOMIC_SEQ_CST);
__atomic_store_n(&smp_request.response->cpus[i]->goto_address, smp_entry,
__ATOMIC_SEQ_CST);
while (__atomic_load_n(&ctr, __ATOMIC_SEQ_CST) == old_ctr)
;
} else {
log("smp - CPU %d is the bootstrap processor (LAPIC ID: %d)\n", i, smp_request.response->cpus[i]->lapic_id);
log("smp - CPU %d is the bootstrap processor (LAPIC ID: %d)\n", i,
smp_request.response->cpus[i]->lapic_id);
}
}

View file

@ -1,4 +1,4 @@
#include <arch//x86_64/sse.h>
#include <arch/x86_64/sse.h>
#include <sys/log.h>
#include <sys/printf.h>

View file

@ -10,7 +10,8 @@ void __x86_64_syscall_init() {
wrmsr(IA32_EFER, efer);
uint64_t star = 0;
star |= ((uint64_t)0x28 << 32); // kernel cs
star |= ((uint64_t)0x30 << 48); // user cs base (SYSCALL adds 16 for CS=0x38, 24 for SS=0x40)
star |= ((uint64_t)0x30
<< 48); // user cs base (SYSCALL adds 16 for CS=0x38, 24 for SS=0x40)
wrmsr(IA32_STAR, star);
wrmsr(IA32_LSTAR, (uint64_t)syscall_entry);
wrmsr(IA32_CSTAR, 0x0);

View file

@ -1,7 +1,7 @@
#include "sys/acpi/madt.h"
#include <dev/ioapic.h>
#include <sys/log.h>
#include <mm/pmm.h>
#include <sys/log.h>
void ioapic_init() {
madt_ioapic *ioapic = acpi_madt_ioapic_list[0];
@ -22,21 +22,24 @@ void ioapic_init() {
}
void ioapic_write(madt_ioapic *ioapic, uint8_t reg, uint32_t val) {
*((volatile uint32_t*)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_REGSEL)) = reg;
*((volatile uint32_t *)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_REGSEL)) =
reg;
*((volatile uint32_t *)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_IOWIN)) = val;
}
uint32_t ioapic_read(madt_ioapic *ioapic, uint8_t reg) {
*((volatile uint32_t*)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_REGSEL)) = reg;
return *((volatile uint32_t*)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_IOWIN));
*((volatile uint32_t *)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_REGSEL)) =
reg;
return *(
(volatile uint32_t *)(HIGHER_HALF(ioapic->apic_addr) + IOAPIC_IOWIN));
}
void ioapic_set_entry(madt_ioapic *ioapic, uint8_t idx, uint64_t data) {
ioapic_write(ioapic, (uint8_t)(IOAPIC_REDTBL + idx * 2), (uint32_t)data);
ioapic_write(ioapic, (uint8_t)(IOAPIC_REDTBL + idx * 2 + 1), (uint32_t)(data >> 32));
ioapic_write(ioapic, (uint8_t)(IOAPIC_REDTBL + idx * 2 + 1),
(uint32_t)(data >> 32));
}
uint64_t ioapic_gsi_count(madt_ioapic *ioapic) {
return (ioapic_read(ioapic, 1) & 0xff0000) >> 16;
}
@ -44,13 +47,15 @@ uint64_t ioapic_gsi_count(madt_ioapic* ioapic) {
madt_ioapic *ioapic_get_gsi(uint32_t gsi) {
for (uint64_t i = 0; i < acpi_madt_ioapic_length; i++) {
madt_ioapic *ioapic = acpi_madt_ioapic_list[i];
if (ioapic->gsi_base <= gsi && ioapic->gsi_base + ioapic_gsi_count(ioapic) > gsi)
if (ioapic->gsi_base <= gsi &&
ioapic->gsi_base + ioapic_gsi_count(ioapic) > gsi)
return ioapic;
}
return (madt_ioapic *)0;
}
void ioapic_redirect_gsi(uint32_t lapic_id, uint8_t vec, uint32_t gsi, uint16_t flags, bool mask) {
void ioapic_redirect_gsi(uint32_t lapic_id, uint8_t vec, uint32_t gsi,
uint16_t flags, bool mask) {
madt_ioapic *ioapic = ioapic_get_gsi(gsi);
uint64_t redirect = vec;
@ -63,8 +68,10 @@ void ioapic_redirect_gsi(uint32_t lapic_id, uint8_t vec, uint32_t gsi, uint16_t
redirect |= (1 << 15);
}
if (mask) redirect |= (1 << 16);
else redirect &= ~(1 << 16);
if (mask)
redirect |= (1 << 16);
else
redirect &= ~(1 << 16);
redirect |= (uint64_t)lapic_id << 56;
@ -73,7 +80,8 @@ void ioapic_redirect_gsi(uint32_t lapic_id, uint8_t vec, uint32_t gsi, uint16_t
ioapic_write(ioapic, redir_table + 1, (uint32_t)(redirect >> 32));
}
void ioapic_redirect_irq(uint32_t lapic_id, uint8_t vec, uint8_t irq, bool mask) {
void ioapic_redirect_irq(uint32_t lapic_id, uint8_t vec, uint8_t irq,
bool mask) {
uint8_t idx = 0;
madt_iso *iso = (madt_iso *)0;

View file

@ -12,11 +12,11 @@
#define IOAPIC_ARB 0x02
#define IOAPIC_REDTBL 0x10
void ioapic_write(madt_ioapic *ioapic, uint8_t reg, uint32_t val);
uint32_t ioapic_read(madt_ioapic *ioapic, uint8_t reg);
void ioapic_redirect_irq(uint32_t lapic_id, uint8_t vec, uint8_t irq, bool mask);
void ioapic_redirect_irq(uint32_t lapic_id, uint8_t vec, uint8_t irq,
bool mask);
uint32_t ioapic_get_redirect_irq(uint8_t irq);
void ioapic_set_entry(madt_ioapic *ioapic, uint8_t idx, uint64_t data);

View file

@ -42,9 +42,7 @@ uint32_t lapic_read(uint32_t reg) {
return *((volatile uint32_t *)(HIGHER_HALF(0xfee00000) + reg));
}
void lapic_eoi() {
lapic_write((uint8_t)0xb0, 0x0);
}
void lapic_eoi() { lapic_write((uint8_t)0xb0, 0x0); }
void lapic_ipi(uint32_t id, uint8_t dat) {
lapic_write(LAPIC_ICRHI, id << LAPIC_ICDESTSHIFT);
@ -59,6 +57,4 @@ void lapic_send_others_int(uint32_t id, uint32_t vec) {
lapic_ipi(id, vec | LAPIC_ICRAES);
}
uint32_t lapic_get_id() {
return lapic_read(0x0020) >> LAPIC_ICDESTSHIFT;
}
uint32_t lapic_get_id() { return lapic_read(0x0020) >> LAPIC_ICDESTSHIFT; }

View file

@ -1,7 +1,7 @@
#include "fs/vfs.h"
#include "lib/string.h"
#include "mm/liballoc/liballoc.h"
#include "mm/memop.h"
#include "lib/string.h"
vnode_t *vfs_create_node(char *name, vnode_type_t type) {
vnode_t *node = (vnode_t *)malloc(sizeof(vnode_t));

View file

@ -78,4 +78,3 @@ char *strncpy(char *dest, const char *src, size_t n) {
dest[i] = '\0';
return dest;
}

View file

@ -1,3 +1,6 @@
#include "arch/x86_64/pit.h"
#include "arch/x86_64/smp.h"
#include "arch/x86_64/sse.h"
#include "dev/ioapic.h"
#include "dev/lapic.h"
#include "exec/elf.h"
@ -9,25 +12,22 @@
#include "sched/sched.h"
#include "sys/acpi.h"
#include "sys/acpi/madt.h"
#include "arch//x86_64/pit.h"
#include "arch//x86_64/smp.h"
#include "arch//x86_64/sse.h"
#include "sys/syscall.h"
#include <arch/x86_64/fpu.h>
#include <arch/x86_64/gdt.h>
#include <arch/x86_64/idt.h>
#include <font.h>
#include <fs/vfs.h>
#include <limine.h>
#include <mm/memop.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <arch//x86_64/fpu.h>
#include <arch//x86_64/gdt.h>
#include <arch//x86_64/idt.h>
#include <sys/errhnd/panic.h>
#include <sys/gfx/flanterm/backends/fb.h>
#include <sys/gfx/flanterm/flanterm.h>
#include <sys/log.h>
#include <sys/printf.h>
#include <fs/vfs.h>
__attribute__((
used, section(".limine_requests"))) static volatile LIMINE_BASE_REVISION(3);
@ -100,18 +100,17 @@ void kmain(void) {
// vfs_init();
// panic("No working initialization program found. (This is normal due to "
// "Soaplin's current state, so please do not report this as a bug)");
program_t *p = elf_load(module_request.response->modules[0]->address, 1);
sched_process *proc = sched_create("Test", p->entry, p->pm,
SCHED_USER_PROCESS);
sched_process *proc =
sched_create("Test", p->entry, p->pm, SCHED_USER_PROCESS);
log("kernel - Soaplin initialized sucessfully.\n");
pit_enable();
while (1)
;
;

View file

@ -49,7 +49,7 @@ void vmm_release_pm(pagemap_t *pm) {
}
void vmm_sanity_check() {
uint64_t *my_memory = pmm_request_page();
uint64_t *my_memory = HIGHER_HALF(pmm_request_page());
*my_memory = 0x40;
pagemap_t *pm = vmm_alloc_pm();
@ -113,14 +113,14 @@ void vmm_init() {
log("vmm - mapping address from 0x0 to 0x100000000...\n");
for (uint64_t gb4 = 0; gb4 < 0x100000000; gb4 += PMM_PAGE_SIZE) {
vmm_map(vmm_kernel_pm, gb4, gb4, VMM_PRESENT | VMM_WRITABLE);
//vmm_map(vmm_kernel_pm, gb4, gb4, VMM_PRESENT | VMM_WRITABLE);
vmm_map(vmm_kernel_pm, (uint64_t)HIGHER_HALF(gb4), gb4,
VMM_PRESENT | VMM_WRITABLE);
}
vmm_load_pagemap(vmm_kernel_pm);
vmm_sanity_check();
//vmm_sanity_check();
log("vmm - initialized!\n");
}

View file

@ -1,13 +1,13 @@
#include "sched/sched.h"
#include "arch/x86_64/idt.h"
#include "arch/x86_64/msr.h"
#include "mm/memop.h"
#include "mm/pmm.h"
#include "mm/vmm.h"
#include "arch//x86_64/idt.h"
#include "sys/log.h"
#include <lib/string.h>
#include <stddef.h>
#include <mm/liballoc/liballoc.h>
#include <stddef.h>
sched_process *proc_list;
sched_process *curr_proc;
@ -114,7 +114,6 @@ sched_process *sched_create(char *name, uint64_t entry_point, pagemap_t *pm,
"disabled.\n");
}
log("sched - created process '%s' (pid: %d, rip: %p)\n", proc->name,
proc->pid, proc->regs.rip);
return proc;

View file

@ -1,7 +1,7 @@
#pragma once
#include "mm/vmm.h"
#include "arch/x86_64/idt.h"
#include "mm/vmm.h"
#define SCHED_KERNEL_PROCESS 0 // A process that runs in kernel mode.
#define SCHED_USER_PROCESS \

View file

@ -42,7 +42,7 @@ void *acpi_find_table(const char *name) {
}
void acpi_init() {
acpi_rsdp *rsdp = (acpi_rsdp *)rsdp_req.response->address;
acpi_rsdp *rsdp = (acpi_rsdp *)HIGHER_HALF(rsdp_req.response->address);
if (memcmp(rsdp->sign, "RSD PTR", 7))
panic("acpi: Invalid RSDP signature!");
@ -50,9 +50,9 @@ void acpi_init() {
if (rsdp->revision != 0) {
__acpi_uses_xsdt = 1;
acpi_xsdp *xsdp = (acpi_xsdp *)rsdp;
__acpi_rsdt_ptr = (acpi_xsdt *)HIGHER_HALF((uint64_t)xsdp->xsdt_addr);
__acpi_rsdt_ptr = (void *)HIGHER_HALF(xsdp->xsdt_addr);
return;
}
__acpi_rsdt_ptr = (acpi_rsdt *)HIGHER_HALF((uint64_t)rsdp->rsdt_addr);
__acpi_rsdt_ptr = (void *)HIGHER_HALF(rsdp->rsdt_addr);
}

View file

@ -1,8 +1,10 @@
#include "arch//x86_64/idt.h"
#include "arch/x86_64/idt.h"
#include "lib/spinlock.h"
#include <mm/memop.h>
#include <sys/log.h>
#include "sched/sched.h"
static registers_t __panic_regdump;
static void __panic_dump_regs() {
@ -85,17 +87,14 @@ static void __panic_display_page_fault(registers_t *regs) {
static void __panic_display_regs(registers_t *regs) {
log("-- REGISTER DUMP --\n");
log("RDI: %p, RSI: %p, RDX: %p, RCX: %p, R8: %p, R9: %p\n",
regs->rdi, regs->rsi, regs->rdx,
regs->rcx, regs->r8, regs->r9);
log("RAX: %p, RBP: %p, RBX: %p, R10: %p, R11: %p, R12: %p\n",
regs->rax, regs->rbp, regs->rbx,
regs->r10, regs->r11, regs->r12);
log("R13: %p, R14: %p, R15: %p\n", regs->r13, regs->r14,
regs->r15);
log("RDI: %p, RSI: %p, RDX: %p, RCX: %p, R8: %p, R9: %p\n", regs->rdi,
regs->rsi, regs->rdx, regs->rcx, regs->r8, regs->r9);
log("RAX: %p, RBP: %p, RBX: %p, R10: %p, R11: %p, R12: %p\n", regs->rax,
regs->rbp, regs->rbx, regs->r10, regs->r11, regs->r12);
log("R13: %p, R14: %p, R15: %p\n", regs->r13, regs->r14, regs->r15);
log("RIP: %p, CS: %x, SS: %x, RFLAGS: %d, INTERRUPT: %d, ERROR CODE: %d\n",
regs->rip, regs->cs, regs->ss,
regs->rflags, regs->int_no, regs->err_code);
regs->rip, regs->cs, regs->ss, regs->rflags, regs->int_no,
regs->err_code);
log("RSP: %p\n", regs->rsp);
if (regs->int_no == 14) // If it's a page fault
@ -136,13 +135,16 @@ void panic_ctx(char *msg, registers_t *regs) {
log("%s\n", msg);
log("\n");
if (curr_proc) {
log("Current process: %s (PID: %d)\n", curr_proc->name, curr_proc->pid);
log("\n");
}
if (regs)
__panic_display_regs(regs);
else
log("No register context provided.\n");
log("System halted: Please restart your computer manually.\n");
asm("cli");

View file

@ -1,4 +1,4 @@
#include "arch//x86_64/io.h"
#include "arch/x86_64/io.h"
#include "sys/gfx/flanterm/flanterm.h"
#include <lib/spinlock.h>
#include <lib/string.h>

View file

@ -1,6 +1,6 @@
#pragma once
#include "arch//x86_64/idt.h"
#include "arch/x86_64/idt.h"
#include <stdint.h>
/// A function that defines a system call.

Binary file not shown.

View file

@ -3,6 +3,7 @@ section .text
global _start
_start:
mov rax, 1025
syscall
.loop:
jmp .loop

Binary file not shown.