Skip to content

Commit cf57761

Browse files
committed
APIC: Deduplicate IO-APIC address initialization
1 parent 60fc624 commit cf57761

File tree

1 file changed

+23
-63
lines changed

1 file changed

+23
-63
lines changed

src/arch/x86_64/kernel/apic.rs

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ pub fn local_apic_id_count() -> u32 {
251251
unsafe { CPU_LOCAL_APIC_IDS.as_ref().unwrap().len() as u32 }
252252
}
253253

254+
unsafe fn init_ioapic_address(phys_addr: PhysAddr) {
255+
unsafe {
256+
IOAPIC_ADDRESS = virtualmem::allocate(BasePageSize::SIZE as usize).unwrap();
257+
debug!("Mapping IOAPIC at {phys_addr:p} to virtual address {IOAPIC_ADDRESS:p}",);
258+
259+
let mut flags = PageTableEntryFlags::empty();
260+
flags.device().writable().execute_disable();
261+
paging::map::<BasePageSize>(IOAPIC_ADDRESS, phys_addr, 1, flags);
262+
}
263+
}
264+
254265
#[cfg(not(feature = "acpi"))]
255266
fn detect_from_acpi() -> Result<PhysAddr, ()> {
256267
// dummy implementation if acpi support is disabled
@@ -291,21 +302,7 @@ fn detect_from_acpi() -> Result<PhysAddr, ()> {
291302
debug!("Found I/O APIC record: {}", ioapic_record);
292303

293304
unsafe {
294-
IOAPIC_ADDRESS = virtualmem::allocate(BasePageSize::SIZE as usize).unwrap();
295-
let record_addr = ioapic_record.address;
296-
debug!(
297-
"Mapping IOAPIC at {:#X} to virtual address {:#X}",
298-
record_addr, IOAPIC_ADDRESS
299-
);
300-
301-
let mut flags = PageTableEntryFlags::empty();
302-
flags.device().writable().execute_disable();
303-
paging::map::<BasePageSize>(
304-
IOAPIC_ADDRESS,
305-
PhysAddr(record_addr.into()),
306-
1,
307-
flags,
308-
);
305+
init_ioapic_address(PhysAddr(ioapic_record.address.into()));
309306
}
310307
}
311308
_ => {
@@ -401,15 +398,7 @@ fn detect_from_mp() -> Result<PhysAddr, ()> {
401398
let default_address = PhysAddr(0xFEC0_0000);
402399

403400
unsafe {
404-
IOAPIC_ADDRESS = virtualmem::allocate(BasePageSize::SIZE as usize).unwrap();
405-
debug!(
406-
"Mapping IOAPIC at {:#X} to virtual address {:#X}",
407-
default_address, IOAPIC_ADDRESS
408-
);
409-
410-
let mut flags = PageTableEntryFlags::empty();
411-
flags.device().writable().execute_disable();
412-
paging::map::<BasePageSize>(IOAPIC_ADDRESS, default_address, 1, flags);
401+
init_ioapic_address(default_address);
413402
}
414403
} else {
415404
// entries starts directly after the config table
@@ -428,24 +417,11 @@ fn detect_from_mp() -> Result<PhysAddr, ()> {
428417
// IO-APIC entry
429418
2 => {
430419
let io_entry: &ApicIoEntry = unsafe { &*(addr as *const ApicIoEntry) };
431-
let ioapic = io_entry.addr;
420+
let ioapic = PhysAddr(io_entry.addr.into());
432421
info!("Found IOAPIC at 0x{:x}", ioapic);
433422

434423
unsafe {
435-
IOAPIC_ADDRESS = virtualmem::allocate(BasePageSize::SIZE as usize).unwrap();
436-
debug!(
437-
"Mapping IOAPIC at {:#X} to virtual address {:#X}",
438-
ioapic, IOAPIC_ADDRESS
439-
);
440-
441-
let mut flags = PageTableEntryFlags::empty();
442-
flags.device().writable().execute_disable();
443-
paging::map::<BasePageSize>(
444-
IOAPIC_ADDRESS,
445-
PhysAddr(ioapic as u64),
446-
1,
447-
flags,
448-
);
424+
init_ioapic_address(ioapic);
449425
}
450426

451427
addr += mem::size_of::<ApicIoEntry>();
@@ -463,43 +439,27 @@ fn detect_from_mp() -> Result<PhysAddr, ()> {
463439
fn default_apic() -> PhysAddr {
464440
warn!("Try to use default APIC address");
465441

466-
let default_address = PhysAddr(0xFEC0_0000);
442+
let default_address = PhysAddr(0xFEE0_0000);
467443

468444
unsafe {
469-
IOAPIC_ADDRESS = virtualmem::allocate(BasePageSize::SIZE as usize).unwrap();
470-
debug!(
471-
"Mapping IOAPIC at {:#X} to virtual address {:#X}",
472-
default_address, IOAPIC_ADDRESS
473-
);
474-
475-
let mut flags = PageTableEntryFlags::empty();
476-
flags.device().writable().execute_disable();
477-
paging::map::<BasePageSize>(IOAPIC_ADDRESS, default_address, 1, flags);
445+
init_ioapic_address(default_address);
478446
}
479447

480-
PhysAddr(0xFEE0_0000)
448+
default_address
481449
}
482450

483451
fn detect_from_uhyve() -> Result<PhysAddr, ()> {
484452
if env::is_uhyve() {
485-
let default_address = PhysAddr(0xFEC0_0000);
453+
let default_address = PhysAddr(0xFEE0_0000);
486454

487455
unsafe {
488-
IOAPIC_ADDRESS = virtualmem::allocate(BasePageSize::SIZE as usize).unwrap();
489-
debug!(
490-
"Mapping IOAPIC at {:#X} to virtual address {:#X}",
491-
default_address, IOAPIC_ADDRESS
492-
);
493-
494-
let mut flags = PageTableEntryFlags::empty();
495-
flags.device().writable().execute_disable();
496-
paging::map::<BasePageSize>(IOAPIC_ADDRESS, default_address, 1, flags);
456+
init_ioapic_address(default_address);
497457
}
498458

499-
return Ok(PhysAddr(0xFEE0_0000));
459+
Ok(default_address)
460+
} else {
461+
Err(())
500462
}
501-
502-
Err(())
503463
}
504464

505465
#[no_mangle]

0 commit comments

Comments
 (0)