Use PHYS_OFFSET instead of KERNEL_OFFSET to refer to the physmap

This commit is contained in:
Jeremy Soller
2021-04-13 20:38:54 -06:00
parent afca4da382
commit f90033e0e1
8 changed files with 29 additions and 14 deletions

View File

@@ -79,7 +79,7 @@ impl Madt {
CPU_COUNT.fetch_add(1, Ordering::SeqCst);
// Allocate a stack
let stack_start = allocate_frames(64).expect("no more frames in acpi stack_start").start_address().data() + crate::KERNEL_OFFSET;
let stack_start = allocate_frames(64).expect("no more frames in acpi stack_start").start_address().data() + crate::PHYS_OFFSET;
let stack_end = stack_start + 64 * 4096;
let ap_ready = (TRAMPOLINE + 8) as *mut u64;

View File

@@ -27,6 +27,11 @@
/// Size of kernel percpu variables
pub const KERNEL_PERCPU_SIZE: usize = 64 * 1024; // 64 KB
/// Offset of physmap
// This needs to match RMM's PHYS_OFFSET
pub const PHYS_OFFSET: usize = 0xFFFF_8000_0000_0000;
pub const PHYS_PML4: usize = (PHYS_OFFSET & PML4_MASK)/PML4_SIZE;
/// Offset to user image
pub const USER_OFFSET: usize = 0;
pub const USER_PML4: usize = (USER_OFFSET & PML4_MASK)/PML4_SIZE;

View File

@@ -233,8 +233,9 @@ pub fn src_overrides() -> &'static [Override] {
#[cfg(feature = "acpi")]
pub unsafe fn handle_ioapic(active_table: &mut ActivePageTable, madt_ioapic: &'static MadtIoApic) {
// map the I/O APIC registers
let frame = Frame::containing_address(PhysicalAddress::new(madt_ioapic.address as usize));
let page = Page::containing_address(VirtualAddress::new(madt_ioapic.address as usize + crate::KERNEL_OFFSET));
let page = Page::containing_address(VirtualAddress::new(madt_ioapic.address as usize + crate::PHYS_OFFSET));
assert_eq!(active_table.translate_page(page), None);
@@ -370,13 +371,13 @@ pub unsafe fn init(active_table: &mut ActivePageTable) {
}
}
fn get_override(irq: u8) -> Option<&'static Override> {
src_overrides().iter().find(|over| over.bus_irq == irq)
src_overrides().iter().find(|over| over.bus_irq == irq)
}
fn resolve(irq: u8) -> u32 {
get_override(irq).map_or(u32::from(irq), |over| over.gsi)
}
fn find_ioapic(gsi: u32) -> Option<&'static IoApic> {
ioapics().iter().find(|apic| gsi >= apic.gsi_start && gsi < apic.gsi_start + u32::from(apic.count))
ioapics().iter().find(|apic| gsi >= apic.gsi_start && gsi < apic.gsi_start + u32::from(apic.count))
}
pub unsafe fn mask(irq: u8) {

View File

@@ -43,12 +43,12 @@ pub fn bsp_apic_id() -> Option<u32> {
impl LocalApic {
unsafe fn init(&mut self, active_table: &mut ActivePageTable) {
self.address = (rdmsr(IA32_APIC_BASE) as usize & 0xFFFF_0000) + crate::KERNEL_OFFSET;
self.address = (rdmsr(IA32_APIC_BASE) as usize & 0xFFFF_0000) + crate::PHYS_OFFSET;
self.x2 = CpuId::new().get_feature_info().unwrap().has_x2apic();
if ! self.x2 {
let page = Page::containing_address(VirtualAddress::new(self.address));
let frame = Frame::containing_address(PhysicalAddress::new(self.address - crate::KERNEL_OFFSET));
let frame = Frame::containing_address(PhysicalAddress::new(self.address - crate::PHYS_OFFSET));
let result = active_table.map_to(page, frame, EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE);
result.flush(active_table);
}

View File

@@ -19,7 +19,7 @@ pub unsafe fn init() {
#[cfg(feature = "lpss_debug")]
{
// TODO: Make this configurable
let address = crate::KERNEL_OFFSET + 0xFE032000;
let address = crate::PHYS_OFFSET + 0xFE032000;
{
use crate::paging::{ActivePageTable, Page, VirtualAddress, entry::EntryFlags};
@@ -27,13 +27,13 @@ pub unsafe fn init() {
let mut active_table = ActivePageTable::new();
let page = Page::containing_address(VirtualAddress::new(address));
let frame = Frame::containing_address(PhysicalAddress::new(address - crate::KERNEL_OFFSET));
let frame = Frame::containing_address(PhysicalAddress::new(address - crate::PHYS_OFFSET));
let result = active_table.map_to(page, frame, EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE);
result.flush(&mut active_table);
}
let lpss = SerialPort::<Mmio<u32>>::new(
crate::KERNEL_OFFSET + 0xFE032000
crate::PHYS_OFFSET + 0xFE032000
);
lpss.init();

View File

@@ -27,7 +27,7 @@ pub fn init(active_table: &mut ActivePageTable) {
let physbaseptr;
{
let mode_info_addr = 0x5200 + crate::KERNEL_OFFSET;
let mode_info_addr = 0x5200 + crate::PHYS_OFFSET;
let mode_info = unsafe { &*(mode_info_addr as *const VBEModeInfo) };
width = mode_info.xresolution as usize;
height = mode_info.yresolution as usize;
@@ -39,13 +39,13 @@ pub fn init(active_table: &mut ActivePageTable) {
{
let size = width * height;
let onscreen = physbaseptr + crate::KERNEL_OFFSET;
let onscreen = physbaseptr + crate::PHYS_OFFSET;
{
let mut flush_all = MapperFlushAll::new();
let start_page = Page::containing_address(VirtualAddress::new(onscreen));
let end_page = Page::containing_address(VirtualAddress::new(onscreen + size * 4));
for page in Page::range_inclusive(start_page, end_page) {
let frame = Frame::containing_address(PhysicalAddress::new(page.start_address().data() - crate::KERNEL_OFFSET));
let frame = Frame::containing_address(PhysicalAddress::new(page.start_address().data() - crate::PHYS_OFFSET));
let flags = EntryFlags::PRESENT | EntryFlags::NO_EXECUTE | EntryFlags::WRITABLE | EntryFlags::HUGE_PAGE;
let result = active_table.map_to(page, frame, flags);
flush_all.consume(result);

View File

@@ -171,14 +171,14 @@ pub unsafe fn init_generic(is_bsp: bool, idt: &mut Idt) {
let frames = crate::memory::allocate_frames(page_count)
.expect("failed to allocate pages for backup interrupt stack");
// Map them linearly, i.e. KERNEL_OFFSET + physaddr.
// Map them linearly, i.e. PHYS_OFFSET + physaddr.
let base_address = {
use crate::memory::{Frame, PhysicalAddress};
use crate::paging::{ActivePageTable, Page, VirtualAddress};
use crate::paging::entry::EntryFlags;
let mut active_table = ActivePageTable::new();
let base_virtual_address = VirtualAddress::new(frames.start_address().data() + crate::KERNEL_OFFSET);
let base_virtual_address = VirtualAddress::new(frames.start_address().data() + crate::PHYS_OFFSET);
for i in 0..page_count {
let virtual_address = VirtualAddress::new(base_virtual_address.data() + i * crate::memory::PAGE_SIZE);

View File

@@ -369,6 +369,15 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
});
}
// Copy physmap mapping
{
let frame = active_table.p4()[crate::PHYS_PML4].pointed_frame().expect("physmap not mapped");
let flags = active_table.p4()[crate::PHYS_PML4].flags();
active_table.with(&mut new_table, &mut temporary_page, |mapper| {
mapper.p4_mut()[crate::PHYS_PML4].set(frame, flags);
});
}
if let Some(fx) = kfx_opt.take() {
context.arch.set_fx(fx.as_ptr() as usize);
context.kfx = Some(fx);