major refactorings
Signed-off-by: kaguya3311 <kaguya3311@national.shitposting.agency>
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
#include <uacpi/platform/arch_helpers.h>
|
||||
#include "mm/memory.h"
|
||||
#include "arch/x86_64/cpu/io.h"
|
||||
#include "arch/x86_64/sys/irq.h"
|
||||
#include "arch/x86_64/sys/pit.h"
|
||||
#include "arch/x86_64/sys/tsc.h"
|
||||
#include <stdatomic.h>
|
||||
#include <stdarg.h>
|
||||
#include "libk/debug.h"
|
||||
#include "mm/vmm.h"
|
||||
#include "arch/x86_64/sys/timer.h"
|
||||
#include "arch/x86_64/boot/isr.h"
|
||||
|
||||
|
||||
extern uint64_t g_rsdp_phys;
|
||||
@@ -70,19 +72,19 @@ void uacpi_kernel_unmap(void *addr, uacpi_size len)
|
||||
static const char *uacpi_log_prefix(uacpi_log_level level)
|
||||
{
|
||||
switch (level) {
|
||||
case UACPI_LOG_DEBUG: return "[uACPI DBG ] ";
|
||||
case UACPI_LOG_TRACE: return "[uACPI TRC ] ";
|
||||
case UACPI_LOG_INFO: return "[uACPI INFO] ";
|
||||
case UACPI_LOG_WARN: return "[uACPI WARN] ";
|
||||
case UACPI_LOG_ERROR: return "[uACPI ERR ] ";
|
||||
default: return "[uACPI ] ";
|
||||
case UACPI_LOG_DEBUG: return "[acpi] DBG: ";
|
||||
case UACPI_LOG_TRACE: return "[acpi] TRC: ";
|
||||
case UACPI_LOG_INFO: return "[acpi] INFO: ";
|
||||
case UACPI_LOG_WARN: return "[acpi] WARN: ";
|
||||
case UACPI_LOG_ERROR: return "[acpi] ERR: ";
|
||||
default: return "[acpi]: ";
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef UACPI_FORMATTED_LOGGING
|
||||
void uacpi_kernel_log(uacpi_log_level level, const uacpi_char *msg)
|
||||
{
|
||||
printf("%s%s", uacpi_log_prefix(level), msg);
|
||||
kprintf("%s%s", uacpi_log_prefix(level), msg);
|
||||
}
|
||||
#else
|
||||
UACPI_PRINTF_DECL(2, 3)
|
||||
@@ -97,7 +99,7 @@ void uacpi_kernel_log(uacpi_log_level level, const uacpi_char *fmt, ...)
|
||||
void uacpi_kernel_vlog(uacpi_log_level level, const uacpi_char *fmt,
|
||||
uacpi_va_list va)
|
||||
{
|
||||
printf("%s", uacpi_log_prefix(level));
|
||||
kprintf("%s", uacpi_log_prefix(level));
|
||||
vfprintf(fmt, va);
|
||||
}
|
||||
#endif
|
||||
@@ -348,7 +350,7 @@ void uacpi_kernel_free(void *mem, uacpi_size size_hint)
|
||||
*/
|
||||
uacpi_u64 uacpi_kernel_get_nanoseconds_since_boot(void)
|
||||
{
|
||||
return rdtsc() / (tsc_cycles_per_us / 1000); // rough ns
|
||||
return (rdtsc() * 1000ULL) / tsc_cycles_per_us;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -368,13 +370,7 @@ void uacpi_kernel_stall(uacpi_u8 usec)
|
||||
*/
|
||||
void uacpi_kernel_sleep(uacpi_u64 msec)
|
||||
{
|
||||
uint64_t start = g_Ticks;
|
||||
uint64_t target = start + msec;
|
||||
|
||||
while (g_Ticks < target)
|
||||
{
|
||||
__asm__ volatile("hlt");
|
||||
}
|
||||
timer_sleep(msec);
|
||||
}
|
||||
|
||||
typedef struct { atomic_flag flag; } uacpi_mutex_impl_t;
|
||||
@@ -426,13 +422,26 @@ void uacpi_kernel_free_event(uacpi_handle handle)
|
||||
uacpi_interrupt_state uacpi_kernel_disable_interrupts(void)
|
||||
{
|
||||
uint64_t flags;
|
||||
asm volatile("pushfq; pop %0; cli" : "=r"(flags) :: "memory");
|
||||
asm volatile(
|
||||
"pushfq\n\t"
|
||||
"pop %0\n\t"
|
||||
"cli"
|
||||
: "=r"(flags)
|
||||
:
|
||||
: "memory"
|
||||
);
|
||||
return (uacpi_interrupt_state)flags;
|
||||
}
|
||||
|
||||
void uacpi_kernel_restore_interrupts(uacpi_interrupt_state state)
|
||||
{
|
||||
asm volatile("push %0; popfq" :: "r"((uint64_t)state) : "memory", "cc");
|
||||
asm volatile(
|
||||
"push %0\n\t"
|
||||
"popfq"
|
||||
:
|
||||
: "r"((uint64_t)state)
|
||||
: "memory", "cc"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -542,10 +551,10 @@ uacpi_status uacpi_kernel_handle_firmware_request(uacpi_firmware_request *req)
|
||||
{
|
||||
switch (req->type) {
|
||||
case UACPI_FIRMWARE_REQUEST_TYPE_BREAKPOINT:
|
||||
printf("[uACPI] AML Breakpoint\n");
|
||||
kprintf("[uACPI] AML Breakpoint\n");
|
||||
return UACPI_STATUS_OK;
|
||||
case UACPI_FIRMWARE_REQUEST_TYPE_FATAL:
|
||||
printf("[uACPI] AML Fatal! type=0x%x code=0x%x arg=0x%lx\n",
|
||||
kprintf("[uACPI] AML Fatal! type=0x%x code=0x%x arg=0x%lx\n",
|
||||
req->fatal.type, req->fatal.code,
|
||||
(unsigned long)req->fatal.arg);
|
||||
for (;;) asm volatile("hlt");
|
||||
@@ -573,9 +582,9 @@ typedef struct {
|
||||
|
||||
static uacpi_irq_entry_t uacpi_irq_table[UACPI_MAX_IRQS];
|
||||
|
||||
static void uacpi_irq_shim(Registers *regs)
|
||||
static void uacpi_irq_shim(registers_t *regs)
|
||||
{
|
||||
int irq = (int)regs->interrupt - 0x20; /* PIC_REMAP_OFFSET */
|
||||
int irq = (int)regs->isrNumber - 0x20; /* PIC_REMAP_OFFSET */
|
||||
if ((unsigned)irq < UACPI_MAX_IRQS && uacpi_irq_table[irq].fn)
|
||||
uacpi_irq_table[irq].fn(uacpi_irq_table[irq].ctx);
|
||||
}
|
||||
@@ -589,7 +598,7 @@ uacpi_status uacpi_kernel_install_interrupt_handler(
|
||||
|
||||
uacpi_irq_table[irq].fn = handler;
|
||||
uacpi_irq_table[irq].ctx = ctx;
|
||||
x86_64_IRQ_RegisterHandler((int)irq, uacpi_irq_shim);
|
||||
isr_register_handler((int)irq, uacpi_irq_shim);
|
||||
|
||||
/* Return a non-NULL token the caller can pass to uninstall */
|
||||
*out_irq_handle = (uacpi_handle)(uintptr_t)(irq + 1);
|
||||
@@ -610,7 +619,7 @@ uacpi_status uacpi_kernel_uninstall_interrupt_handler(
|
||||
|
||||
uacpi_irq_table[irq].fn = NULL;
|
||||
uacpi_irq_table[irq].ctx = NULL;
|
||||
x86_64_IRQ_RegisterHandler((int)irq, NULL);
|
||||
isr_register_handler((int)irq, NULL);
|
||||
return UACPI_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -646,18 +655,42 @@ void uacpi_kernel_free_spinlock(uacpi_handle handle)
|
||||
uacpi_cpu_flags uacpi_kernel_lock_spinlock(uacpi_handle handle)
|
||||
{
|
||||
uint64_t flags;
|
||||
asm volatile("pushfq; pop %0; cli" : "=r"(flags) :: "memory");
|
||||
|
||||
asm volatile(
|
||||
"pushfq\n\t"
|
||||
"pop %0\n\t"
|
||||
"cli"
|
||||
: "=r"(flags)
|
||||
:
|
||||
: "memory"
|
||||
);
|
||||
|
||||
uacpi_spinlock_impl_t *sl = handle;
|
||||
|
||||
while (atomic_flag_test_and_set_explicit(&sl->flag, memory_order_acquire))
|
||||
asm volatile("pause" ::: "memory");
|
||||
asm volatile(
|
||||
"pause"
|
||||
:
|
||||
:
|
||||
: "memory"
|
||||
);
|
||||
|
||||
return (uacpi_cpu_flags)flags;
|
||||
}
|
||||
|
||||
void uacpi_kernel_unlock_spinlock(uacpi_handle handle, uacpi_cpu_flags saved)
|
||||
{
|
||||
uacpi_spinlock_impl_t *sl = handle;
|
||||
|
||||
atomic_flag_clear_explicit(&sl->flag, memory_order_release);
|
||||
asm volatile("push %0; popfq" :: "r"((uint64_t)saved) : "memory", "cc");
|
||||
|
||||
asm volatile(
|
||||
"push %0\n\t"
|
||||
"popfq"
|
||||
:
|
||||
: "r"((uint64_t)saved)
|
||||
: "memory", "cc"
|
||||
);
|
||||
}
|
||||
|
||||
typedef enum uacpi_work_type {
|
||||
|
||||
Reference in New Issue
Block a user