vbos/amd64/amd64_init.c
Michael Shaw 33d6f72cfe
amd64: insert idle threads
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-12 22:11:11 -04:00

79 lines
1.6 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/tty.h>
#include <kernel/atomic.h>
#include <arch/proc.h>
#include <drivers/acpi.h>
#include <drivers/serial.h>
#include <lib/stdio.h>
#include "arch/msr.h"
#include "arch/reg.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};
static struct cpu_local cpu_local_arr[128];
static u64 cpu_ticker = 0;
void yield()
{
for (;;) {
asm("hlt");
}
}
void _panic()
{
yield();
}
void arch_early_init()
{
/* enable SSE: CR4.OSFXSR (bit 9) + CR4.OSXMMEXCPT (bit 10) */
u64 cr4 = cr4_read();
cr4 |= (1 << 9) | (1 << 10);
cr4_write(cr4);
tty_early.serial_ops = tty_early_ops;
tty_early.priv = (void *)COM1;
tty_printf = &tty_early;
tty_clear_screen(tty_printf);
tty_set_cursor_pos(tty_printf, 0, 0);
}
void arch_init()
{
void _syscall_enter();
gdt_init();
idt_init();
wrmsr(IA32_EFER, rdmsr(IA32_EFER) | 1); /* SCE */
wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
wrmsr(IA32_FMASK, 0x3F7FD5);
}
void arch_ap_startup()
{
u64 id = atomic_xadd(&cpu_ticker, 1);
struct cpu_local *this_local = &cpu_local_arr[id];
this_local->cpu_id = id;
wrmsr(IA32_GS_BASE, (u64)this_local);
tss_init(id, this_local);
char buf[64];
snprintf(buf, 64, "kidle_cpu%lld", id);
struct thread *idle = kthread_create(yield, NULL, buf);
if (idle)
kdebug("idle thread inserted for cpu %d\n", id);
else
panic("failed to create idle thread for cpu %d\n", id);
this_local->idle_thread = idle;
}