Skip to content

Commit cf5a6b6

Browse files
committed
Enhanced uhyve memory detection
1 parent 0245c53 commit cf5a6b6

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/arch/x86_64/mm/paging.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use x86_64::structures::paging::{
1515

1616
use crate::arch::x86_64::kernel::processor;
1717
use crate::arch::x86_64::mm::{physicalmem, PhysAddr, VirtAddr};
18+
use crate::kernel::get_limit;
1819
use crate::{env, mm, scheduler};
1920

2021
pub trait PageTableEntryFlagsExt {
@@ -305,11 +306,9 @@ pub fn init_page_tables() {
305306
// See https://github.com/hermit-os/uhyve/issues/426
306307
let kernel_end_addr = x86_64::VirtAddr::new(mm::kernel_end_address().as_u64());
307308
let start_page = Page::<Size2MiB>::from_start_address(kernel_end_addr).unwrap();
308-
let end_page = Page::from_page_table_indices_2mib(
309-
start_page.p4_index(),
310-
start_page.p3_index(),
311-
PageTableIndex::new(511),
312-
);
309+
let end_page =
310+
Page::<Size2MiB>::from_start_address(x86_64::VirtAddr::new(get_limit() as u64))
311+
.unwrap();
313312
let page_range = Page::range_inclusive(start_page, end_page);
314313

315314
let mut page_table = unsafe { recursive_page_table() };

src/arch/x86_64/mm/physicalmem.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use free_list::{AllocError, FreeList, PageLayout, PageRange};
44
use hermit_sync::InterruptTicketMutex;
55
use multiboot::information::{MemoryType, Multiboot};
66

7-
use crate::arch::x86_64::kernel::{get_fdt, get_limit, get_mbinfo};
7+
use crate::arch::x86_64::kernel::{get_fdt, get_limit, get_mbinfo, boot_info};
88
use crate::arch::x86_64::mm::paging::{BasePageSize, PageSize};
99
use crate::arch::x86_64::mm::{MultibootMemory, PhysAddr, VirtAddr};
10+
use crate::mm::kernel_start_address;
1011
use crate::{env, mm};
1112

1213
pub static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList<16>> =
@@ -110,36 +111,38 @@ fn detect_from_uhyve() -> Result<(), ()> {
110111
return Err(());
111112
}
112113

113-
let limit = get_limit();
114-
assert_ne!(limit, 0);
114+
let physmem_end = get_limit();
115+
assert_ne!(physmem_end, 0);
115116
let mut free_list = PHYSICAL_FREE_LIST.lock();
116117
let total_memory;
117118

119+
let kernel_end = mm::kernel_end_address().as_usize();
118120
// add gap for the APIC
119-
if limit > KVM_32BIT_GAP_START {
120-
let range =
121-
PageRange::new(mm::kernel_end_address().as_usize(), KVM_32BIT_GAP_START).unwrap();
121+
assert!(kernel_end < KVM_32BIT_GAP_START || kernel_end > KVM_32BIT_GAP_START + KVM_32BIT_GAP_SIZE, "Kernel was loaded into the KVM 32BIT GAP");
122+
if physmem_end > KVM_32BIT_GAP_START && kernel_end < KVM_32BIT_GAP_START {
123+
let range = PageRange::new(kernel_end, KVM_32BIT_GAP_START).unwrap();
122124
unsafe {
123125
free_list.deallocate(range).unwrap();
124126
}
125-
if limit > KVM_32BIT_GAP_START + KVM_32BIT_GAP_SIZE {
126-
let range = PageRange::new(KVM_32BIT_GAP_START + KVM_32BIT_GAP_SIZE, limit).unwrap();
127+
if physmem_end > KVM_32BIT_GAP_START + KVM_32BIT_GAP_SIZE {
128+
let range =
129+
PageRange::new(KVM_32BIT_GAP_START + KVM_32BIT_GAP_SIZE, physmem_end).unwrap();
127130
unsafe {
128131
free_list.deallocate(range).unwrap();
129132
}
130-
total_memory = limit - KVM_32BIT_GAP_SIZE;
133+
total_memory = boot_info().hardware_info.phys_addr_range.end - boot_info().hardware_info.phys_addr_range.start - KVM_32BIT_GAP_SIZE as u64;
131134
} else {
132-
total_memory = KVM_32BIT_GAP_START;
135+
total_memory = KVM_32BIT_GAP_START as u64 - boot_info().hardware_info.phys_addr_range.start;
133136
}
134137
} else {
135-
let range = PageRange::new(mm::kernel_end_address().as_usize(), limit).unwrap();
138+
let range = PageRange::new(kernel_end, physmem_end).unwrap();
136139
unsafe {
137140
free_list.deallocate(range).unwrap();
138141
}
139-
total_memory = limit;
142+
total_memory = boot_info().hardware_info.phys_addr_range.end - boot_info().hardware_info.phys_addr_range.start;
140143
}
141144

142-
TOTAL_MEMORY.store(total_memory, Ordering::Relaxed);
145+
TOTAL_MEMORY.store(total_memory as usize, Ordering::Relaxed);
143146

144147
Ok(())
145148
}

0 commit comments

Comments
 (0)