@@ -4,9 +4,10 @@ use free_list::{AllocError, FreeList, PageLayout, PageRange};
4
4
use hermit_sync:: InterruptTicketMutex ;
5
5
use multiboot:: information:: { MemoryType , Multiboot } ;
6
6
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 } ;
8
8
use crate :: arch:: x86_64:: mm:: paging:: { BasePageSize , PageSize } ;
9
9
use crate :: arch:: x86_64:: mm:: { MultibootMemory , PhysAddr , VirtAddr } ;
10
+ use crate :: mm:: kernel_start_address;
10
11
use crate :: { env, mm} ;
11
12
12
13
pub static PHYSICAL_FREE_LIST : InterruptTicketMutex < FreeList < 16 > > =
@@ -110,36 +111,38 @@ fn detect_from_uhyve() -> Result<(), ()> {
110
111
return Err ( ( ) ) ;
111
112
}
112
113
113
- let limit = get_limit ( ) ;
114
- assert_ne ! ( limit , 0 ) ;
114
+ let physmem_end = get_limit ( ) ;
115
+ assert_ne ! ( physmem_end , 0 ) ;
115
116
let mut free_list = PHYSICAL_FREE_LIST . lock ( ) ;
116
117
let total_memory;
117
118
119
+ let kernel_end = mm:: kernel_end_address ( ) . as_usize ( ) ;
118
120
// 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 ( ) ;
122
124
unsafe {
123
125
free_list. deallocate ( range) . unwrap ( ) ;
124
126
}
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 ( ) ;
127
130
unsafe {
128
131
free_list. deallocate ( range) . unwrap ( ) ;
129
132
}
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 ;
131
134
} 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 ;
133
136
}
134
137
} else {
135
- let range = PageRange :: new ( mm :: kernel_end_address ( ) . as_usize ( ) , limit ) . unwrap ( ) ;
138
+ let range = PageRange :: new ( kernel_end , physmem_end ) . unwrap ( ) ;
136
139
unsafe {
137
140
free_list. deallocate ( range) . unwrap ( ) ;
138
141
}
139
- total_memory = limit ;
142
+ total_memory = boot_info ( ) . hardware_info . phys_addr_range . end - boot_info ( ) . hardware_info . phys_addr_range . start ;
140
143
}
141
144
142
- TOTAL_MEMORY . store ( total_memory, Ordering :: Relaxed ) ;
145
+ TOTAL_MEMORY . store ( total_memory as usize , Ordering :: Relaxed ) ;
143
146
144
147
Ok ( ( ) )
145
148
}
0 commit comments