Refactor
All checks were successful
Build / build (push) Successful in 2m48s

This commit is contained in:
Mathieu Strypsteen 2024-10-30 20:55:28 +01:00
parent f58d15a197
commit 9b7e2c979e
6 changed files with 28 additions and 65 deletions

View file

@ -158,10 +158,7 @@ extern "C" fn AcpiOsUnmapMemory(address: *mut c_void, size: ACPI_SIZE) {
}
#[no_mangle]
extern "C" fn AcpiOsPrint(raw_str: *const c_char) {
let str;
unsafe {
str = CStr::from_ptr(raw_str);
}
let str = unsafe { CStr::from_ptr(raw_str) };
log_raw(str.to_str().unwrap());
}
#[no_mangle]

View file

@ -66,10 +66,7 @@ pub fn setup_hpet() {
address = map_physical(hpet_table.Address.Address, 0x200) as *mut u64;
}
ADDRESS.store(address, Ordering::Relaxed);
let period;
unsafe {
period = address.add(REGISTER_CAPABILITIES).read_volatile() >> 32;
}
let period = unsafe { address.add(REGISTER_CAPABILITIES).read_volatile() >> 32 };
PERIOD.store(period, Ordering::Relaxed);
unsafe {
let current_config = address.add(REGISTER_CONFIG).read_volatile();

View file

@ -96,10 +96,7 @@ pub fn register_isa_irq_handler(irq: usize, handler: fn()) {
}
pub fn setup_ioapic(apic_i: u8, phys: u64, gsi_base: u32) {
let apic_i = apic_i as usize;
let address;
unsafe {
address = map_physical(phys, 0x14) as *mut u32;
}
let address = unsafe { map_physical(phys, 0x14) as *mut u32 };
IOAPICS[apic_i].address.store(address, Ordering::Relaxed);
IOAPICS[apic_i].start_gsi.store(gsi_base, Ordering::Relaxed);
let max_ints = (read_register(apic_i, REGISTER_VERSION) >> 16) & 0xff;

View file

@ -19,10 +19,7 @@ pub fn send_eoi() {
}
}
pub fn setup_lapic(phys: u64) {
let address;
unsafe {
address = map_physical(phys, 0x400) as *mut u32;
}
let address = unsafe { map_physical(phys, 0x400) as *mut u32 };
ADDRESS.store(address, Ordering::Relaxed);
unsafe {
BSP_LAPIC_ID.store(address.add(REGISTER_ID).read_volatile() as u8, Ordering::Relaxed);

View file

@ -10,6 +10,17 @@ use super::{
lapic::setup_lapic,
};
fn process_subtables(subtable_address: u64, end_address: u64, target_type: u8, handler: &mut dyn FnMut(u64)) {
let mut subtable_address_i = subtable_address;
while subtable_address_i < end_address {
let subtable_header = unsafe { &*(subtable_address_i as *const acpi_subtable_header) };
if subtable_header.Type == target_type {
handler(subtable_address_i);
}
subtable_address_i += subtable_header.Length as u64;
}
}
pub fn parse_madt() {
let madt: &acpi_table_madt;
let signature = b"APIC" as *const u8;
@ -22,52 +33,19 @@ pub fn parse_madt() {
let mut lapic_address: u64 = madt.Address as u64;
let subtable_address = madt as *const acpi_table_madt as u64 + size_of::<acpi_table_madt>() as u64;
let end_address = madt as *const acpi_table_madt as u64 + madt.Header.Length as u64;
let mut subtable_address_i = subtable_address;
while subtable_address_i < end_address {
let subtable_header;
unsafe {
subtable_header = &*(subtable_address_i as *const acpi_subtable_header);
}
if subtable_header.Type == 5 {
let subtable;
unsafe {
subtable = &*(subtable_address_i as *const acpi_madt_local_apic_override);
}
lapic_address = subtable.Address;
}
subtable_address_i += subtable_header.Length as u64;
}
process_subtables(subtable_address, end_address, 5, &mut |addr| {
let subtable = unsafe { &*(addr as *const acpi_madt_local_apic_override) };
lapic_address = subtable.Address;
});
setup_lapic(lapic_address);
subtable_address_i = subtable_address;
while subtable_address_i < end_address {
let subtable_header;
unsafe {
subtable_header = &*(subtable_address_i as *const acpi_subtable_header);
}
if subtable_header.Type == 1 {
let subtable;
unsafe {
subtable = &*(subtable_address_i as *const acpi_madt_io_apic);
}
setup_ioapic(subtable.Id, subtable.Address as u64, subtable.GlobalIrqBase);
}
subtable_address_i += subtable_header.Length as u64;
}
subtable_address_i = subtable_address;
while subtable_address_i < end_address {
let subtable_header;
unsafe {
subtable_header = &*(subtable_address_i as *const acpi_subtable_header);
}
if subtable_header.Type == 2 {
let subtable;
unsafe {
subtable = &*(subtable_address_i as *const acpi_madt_interrupt_override);
}
set_irq_override(subtable.GlobalIrq as usize, subtable.SourceIrq as usize, subtable.IntiFlags as u8 & 3, subtable.IntiFlags as u8 & 0xc);
}
subtable_address_i += subtable_header.Length as u64;
}
process_subtables(subtable_address, end_address, 1, &mut |addr| {
let subtable = unsafe { &*(addr as *const acpi_madt_io_apic) };
setup_ioapic(subtable.Id, subtable.Address as u64, subtable.GlobalIrqBase);
});
process_subtables(subtable_address, end_address, 2, &mut |addr| {
let subtable = unsafe { &*(addr as *const acpi_madt_interrupt_override) };
set_irq_override(subtable.GlobalIrq as usize, subtable.SourceIrq as usize, subtable.IntiFlags as u8 & 3, subtable.IntiFlags as u8 & 0xc);
});
unsafe {
sti();
}

View file

@ -55,10 +55,7 @@ fn main() -> Status {
}
});
assert_ne!(rsdp.load(Ordering::Relaxed), 0, "RSDP not found");
let memory_map;
unsafe {
memory_map = exit_boot_services(MemoryType::LOADER_DATA);
}
let memory_map = unsafe { exit_boot_services(MemoryType::LOADER_DATA) };
let pml4 = setup_paging(&memory_map, heap_start);
map_kernel(KERNEL, pml4, kernel_start);
let loader_struct = generate_loader_struct(&memory_map, kernel_start, heap_start, rsdp.load(Ordering::Relaxed));