XHCI works on real hardware!

This commit is contained in:
2026-03-24 22:12:36 +01:00
parent b2e4a3802d
commit 0478570b2b
11 changed files with 215 additions and 102 deletions

View File

@@ -58,7 +58,7 @@ static struct pg_index mm_page_index (uint64_t vaddr) {
/* Walk paging tables and allocate necessary structures along the way */
static uint64_t* mm_next_table (uint64_t* table, uint64_t entry_idx, bool alloc) {
uint64_t entry = table[entry_idx];
physaddr_t paddr;
uintptr_t paddr;
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
@@ -194,7 +194,7 @@ void mm_unmap_kernel_page (uintptr_t vaddr) {
uintptr_t mm_alloc_user_pd_phys (void) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
physaddr_t cr3 = pmm_alloc (1);
uintptr_t cr3 = pmm_alloc (1);
if (cr3 == PMM_ALLOC_ERR)
return 0;

View File

@@ -1,7 +1,7 @@
#include <sys/spin_lock.h>
/// Relax the spinlock using AMD64 pause instruction
void spin_lock_relax (void) { __asm__ volatile ("pause"); }
void spin_lock_relax (void) { __asm__ volatile ("pause" ::: "memory"); }
void spin_lock_save_flags (uint64_t* flags) {
__asm__ volatile ("pushfq; cli; popq %0" : "=rm"(*flags)::"memory");

View File

@@ -12,7 +12,8 @@ c += amd64/bootmain.c \
amd64/sched1.c \
amd64/proc.c \
amd64/syscall.c \
amd64/gdt.c
amd64/gdt.c \
amd64/stall.c
S += amd64/intr_stub.S \
amd64/spin.S \
@@ -39,4 +40,5 @@ o += amd64/bootmain.o \
amd64/syscall.o \
amd64/syscallentry.o \
amd64/gdt.o \
amd64/sse.o
amd64/sse.o \
amd64/stall.o

31
kernel/amd64/stall.c Normal file
View File

@@ -0,0 +1,31 @@
#include <libk/std.h>
#include <sys/spin_lock.h>
#include <sys/stall.h>
static uint64_t stall_read_tsc (void) {
uint32_t lo, hi;
__asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)hi << 32) | lo;
}
static uint64_t stall_get_tsc_freq_hz (void) {
uint32_t eax, ebx, ecx, edx;
__asm__ volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(0x15));
if (eax == 0 || ebx == 0 || ecx == 0)
return 2500000000ULL;
return (uint64_t)ecx * ebx / eax;
}
void stall_ms (uint64_t ms) {
uint64_t freq_hz = stall_get_tsc_freq_hz ();
uint64_t cycles = freq_hz / 1000;
uint64_t wait_cycles = ms * cycles;
uint64_t now = stall_read_tsc ();
while ((stall_read_tsc () - now) < wait_cycles)
spin_lock_relax ();
}