kv2: initialize memory manager, early pci devices

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2026-05-12 02:59:37 -04:00
commit a252a3f8e9
58 changed files with 1270 additions and 505 deletions

View file

@ -1,5 +1,10 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += arch/
obj-y += drivers/
obj-y += init/
obj-y += kernel/
obj-y += amd64_init.o
obj-y += gdt.o
obj-y += idt.o
obj-y += trap.o
obj-y += exception.o

36
amd64/amd64_init.c Normal file
View file

@ -0,0 +1,36 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <drivers/acpi.h>
#include <drivers/tty.h>
#include <drivers/serial.h>
#include <lib/stdio.h>
#include "descriptor.h"
static struct tty tty_early = {0};
static struct tty_serial_ops tty_early_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write};
void yield()
{
for (;;) {
asm("hlt");
}
}
void _panic()
{
yield();
}
void arch_early_init()
{
tty_early.serial_ops = &tty_early_ops;
tty_early.priv = (void *)COM1;
tty_printf = &tty_early;
}
void arch_init()
{
gdt_init();
idt_init();
acpi_init();
}

View file

@ -45,3 +45,9 @@ lidt:
ltr:
ltr rdi
ret
.global tlb_flush
tlb_flush:
mov rax, cr3
mov cr3, rax
ret

View file

@ -9,15 +9,9 @@
#include <lib/stdio.h>
struct serial_port {
dev_t dev;
int port;
};
static int num_serial_ports = 0;
static struct serial_port serial_ports[32];
static dev_t init_serial_port(int port)
void init_serial_port(int port)
{
outb(port | 1, 0x00);
outb(port | 3, 0x80);
@ -31,49 +25,36 @@ static dev_t init_serial_port(int port)
outb(port | 0, 0xAE); /* Enable loopback mode */
if (inb(port | 0) != 0xAE) { /* Loopback test */
kwarn("Serial port 0x%04x loopback test failed\n", port);
return 1;
return;
}
outb(port | 1, 0x01); /* Enable interrupts */
outb(port | 4, 0x0F); /* Enter normal operation */
char name[256];
snprintf(name, sizeof(name) - 1, "tty%04x", port);
name[sizeof(name) - 1] = 0;
dev_t serial_device = device_create(&serial_ports[num_serial_ports], DEV_CLASS_NATIVE_SERIAL, name);
if (num_serial_ports + 1 < ARRAY_SIZE(serial_ports)) {
serial_ports[num_serial_ports].dev = serial_device;
serial_ports[num_serial_ports].port = port;
num_serial_ports += 1;
}
return serial_device;
}
dev_t arch_serial_init()
void *arch_serial_init()
{
dev_t com1 = init_serial_port(COM1);
return com1;
return (void *)COM1;
}
void arch_serial_write(struct tty *tty, char c)
{
/* Wait for line to be clear */
struct serial_port *serial_port = tty->serial_device_private;
while ((inb(serial_port->port + 5) & 0x20) == 0)
size_t port = (size_t)tty->priv;
while ((inb(port + 5) & 0x20) == 0)
;
outb(serial_port->port, c);
outb(port, c);
}
char arch_serial_read(struct tty *tty)
{
struct serial_port *serial_port = tty->serial_device_private;
size_t port = (size_t)tty->priv;
while ((inb(serial_port->port + 5) & 1) == 0)
while ((inb(port + 5) & 1) == 0)
;
return inb(serial_port->port);
return inb(port);
}

View file

@ -2,7 +2,7 @@
#include <kernel/common.h>
#include "descriptor.h"
#include "../arch/reg.h"
#include "arch/reg.h"
static size_t gdt_index = 0;

View file

@ -1,7 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include "descriptor.h"
#include "../arch/reg.h"
#include "arch/reg.h"
#define exception(_i) _exception_##_i
#define exception_decl(_j) void exception(_j)()

View file

@ -5,6 +5,7 @@
#include <kernel/types.h>
void arch_stack_init(void *new_stack_addr, size_t stack_size);
void arch_early_init();
void arch_init();
#endif /* _INIT_H_ */

View file

@ -7,8 +7,7 @@
#define PAGE_SIZE 0x1000ull
#define PAGE_MASK (~(PAGE_SIZE - 1))
#define PAGE_ALIGN (~(PAGE_SIZE - 1))
#define PAGE_ALIGN (PAGE_SIZE - 1)
#define PAGE_PRESENT 0x001
#define PAGE_RW 0x002
@ -17,7 +16,7 @@
#define PAGE_CACHE_DISABLE 0x010
#define PAGE_ACCESSED 0x020
#define PAGE_DIRTY 0x040
#define PAGE_4MB_ENABLE 0x080
#define PAGE_PS 0x080
#define pgd_index(address) (((address) >> 39) & 0x1FF)
#define pud_index(address) (((address) >> 30) & 0x1FF)

View file

@ -5,10 +5,8 @@
#define COM1 0x3F8
#define COM2 0x2F8
#define DEV_CLASS_NATIVE_SERIAL 0x9000
#include <kernel/device.h>
dev_t arch_serial_init();
void *arch_serial_init();
#endif /* _SERIAL_H_ */

View file

@ -1,6 +0,0 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += amd64_init.o
obj-y += gdt.o
obj-y += idt.o
obj-y += trap.o
obj-y += exception.o

View file

@ -1,20 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "descriptor.h"
void yield()
{
for (;;) {
asm("hlt");
}
}
void _panic()
{
yield();
}
void arch_init()
{
gdt_init();
idt_init();
}