1
1
use crate :: constants:: {
2
- PROT_NONE , PROT_READ , PROT_WRITE , PROT_EXEC ,
3
- MAP_SHARED , MAP_PRIVATE , MAP_FIXED , MAP_ANONYMOUS ,
4
- MAP_FAILED
2
+ MAP_ANONYMOUS , MAP_FAILED , MAP_FIXED , MAP_PRIVATE , MAP_SHARED , PAGESHIFT , PROT_EXEC , PROT_NONE , PROT_READ , PROT_WRITE
5
3
} ;
6
4
use std:: io;
7
5
use nodit:: NoditMap ;
8
6
use nodit:: { interval:: ie, Interval } ;
9
7
use crate :: fdtables;
10
8
use crate :: safeposix:: cage:: syscall_error;
11
9
use crate :: safeposix:: cage:: Errno ;
10
+
11
+ const DEFAULT_VMMAP_SIZE : u32 = 1 << ( 32 - PAGESHIFT ) ;
12
+
12
13
/// Used to identify whether the vmmap entry is backed anonymously,
13
14
/// by an fd, or by a shared memory segment
14
15
///
@@ -243,8 +244,11 @@ pub struct Vmmap {
243
244
pub entries : NoditMap < u32 , Interval < u32 > , VmmapEntry > , // Keyed by `page_num`
244
245
pub cached_entry : Option < VmmapEntry > , // TODO: is this still needed?
245
246
// Use Option for safety
246
- pub base_address : Option < i64 > , // wasm base address. None means uninitialized yet
247
+ pub base_address : Option < usize > , // wasm base address. None means uninitialized yet
247
248
249
+ pub start_address : u32 , // start address of valid vmmap address range
250
+ pub end_address : u32 , // end address of valid vmmap address range
251
+ pub program_break : u32 , // program break (i.e. heap bottom) of the memory
248
252
}
249
253
250
254
#[ allow( dead_code) ]
@@ -255,7 +259,10 @@ impl Vmmap {
255
259
Vmmap {
256
260
entries : NoditMap :: new ( ) ,
257
261
cached_entry : None ,
258
- base_address : None
262
+ base_address : None ,
263
+ start_address : 0 ,
264
+ end_address : DEFAULT_VMMAP_SIZE ,
265
+ program_break : 0 ,
259
266
}
260
267
}
261
268
@@ -287,20 +294,28 @@ impl Vmmap {
287
294
///
288
295
/// Arguments:
289
296
/// - base_address: The base address to set
290
- pub fn set_base_address ( & mut self , base_address : i64 ) {
297
+ pub fn set_base_address ( & mut self , base_address : usize ) {
291
298
// Store the provided base address
292
299
self . base_address = Some ( base_address) ;
293
300
}
294
301
302
+ /// Sets the program break for the memory
303
+ ///
304
+ /// Arguments:
305
+ /// - program_break: The program break to set
306
+ pub fn set_program_break ( & mut self , program_break : u32 ) {
307
+ self . program_break = program_break;
308
+ }
309
+
295
310
/// Converts a user address to a system address
296
311
///
297
312
/// Arguments:
298
313
/// - address: User space address to convert
299
314
///
300
315
/// Returns the corresponding system address
301
- pub fn user_to_sys ( & self , address : i32 ) -> i64 {
316
+ pub fn user_to_sys ( & self , address : u32 ) -> usize {
302
317
// Add base address to user address to get system address
303
- address as i64 + self . base_address . unwrap ( )
318
+ address as usize + self . base_address . unwrap ( )
304
319
}
305
320
306
321
/// Converts a system address to a user address
@@ -309,9 +324,9 @@ impl Vmmap {
309
324
/// - address: System address to convert
310
325
///
311
326
/// Returns the corresponding user space address
312
- pub fn sys_to_user ( & self , address : i64 ) -> i32 {
327
+ pub fn sys_to_user ( & self , address : usize ) -> u32 {
313
328
// Subtract base address from system address to get user address
314
- ( address as i64 - self . base_address . unwrap ( ) ) as i32
329
+ ( address as usize - self . base_address . unwrap ( ) ) as u32
315
330
}
316
331
317
332
// Visits each entry in the vmmap, applying a visitor function to each entry
@@ -814,24 +829,17 @@ impl VmmapOps for Vmmap {
814
829
/// - Some(Interval) containing the found space
815
830
/// - None if no suitable space found
816
831
fn find_space ( & self , npages : u32 ) -> Option < Interval < u32 > > {
817
- let start = self . first_entry ( ) ;
818
- let end = self . last_entry ( ) ;
832
+ let start = self . start_address ;
833
+ let end = self . end_address ;
819
834
820
- if start == None || end == None {
821
- return None ;
822
- } else {
823
- let start_unwrapped = start. unwrap ( ) . 0 . start ( ) ;
824
- let end_unwrapped = end. unwrap ( ) . 0 . end ( ) ;
825
-
826
- let desired_space = npages + 1 ; // TODO: check if this is correct
835
+ let desired_space = npages + 1 ; // TODO: check if this is correct
827
836
828
- for gap in self
829
- . entries
830
- . gaps_trimmed ( ie ( start_unwrapped, end_unwrapped) )
831
- {
832
- if gap. end ( ) - gap. start ( ) >= desired_space {
833
- return Some ( gap) ;
834
- }
837
+ for gap in self
838
+ . entries
839
+ . gaps_trimmed ( ie ( start, end) )
840
+ {
841
+ if gap. end ( ) - gap. start ( ) >= desired_space {
842
+ return Some ( gap) ;
835
843
}
836
844
}
837
845
@@ -852,19 +860,13 @@ impl VmmapOps for Vmmap {
852
860
/// - None if no suitable space found
853
861
fn find_space_above_hint ( & self , npages : u32 , hint : u32 ) -> Option < Interval < u32 > > {
854
862
let start = hint;
855
- let end = self . last_entry ( ) ;
863
+ let end = self . end_address ;
856
864
857
- if end == None {
858
- return None ;
859
- } else {
860
- let end_unwrapped = end. unwrap ( ) . 0 . end ( ) ;
861
-
862
- let desired_space = npages + 1 ; // TODO: check if this is correct
865
+ let desired_space = npages + 1 ; // TODO: check if this is correct
863
866
864
- for gap in self . entries . gaps_trimmed ( ie ( start, end_unwrapped) ) {
865
- if gap. end ( ) - gap. start ( ) >= desired_space {
866
- return Some ( gap) ;
867
- }
867
+ for gap in self . entries . gaps_trimmed ( ie ( start, end) ) {
868
+ if gap. end ( ) - gap. start ( ) >= desired_space {
869
+ return Some ( gap) ;
868
870
}
869
871
}
870
872
@@ -888,31 +890,24 @@ impl VmmapOps for Vmmap {
888
890
/// - Rounds page numbers up to alignment boundaries
889
891
/// - Handles alignment constraints for start and end addresses
890
892
fn find_map_space ( & self , num_pages : u32 , pages_per_map : u32 ) -> Option < Interval < u32 > > {
891
- let start = self . first_entry ( ) ;
892
- let end = self . last_entry ( ) ;
893
+ let start = self . start_address ;
894
+ let end = self . end_address ;
893
895
894
- if start == None || end == None {
895
- return None ;
896
- } else {
897
- let start_unwrapped = start. unwrap ( ) . 0 . start ( ) ;
898
- let end_unwrapped = end. unwrap ( ) . 0 . end ( ) ;
899
-
900
- let rounded_num_pages =
901
- self . round_page_num_up_to_map_multiple ( num_pages, pages_per_map) ;
896
+ let rounded_num_pages =
897
+ self . round_page_num_up_to_map_multiple ( num_pages, pages_per_map) ;
902
898
903
- for gap in self
904
- . entries
905
- . gaps_trimmed ( ie ( start_unwrapped, end_unwrapped) )
906
- {
907
- let aligned_start_page =
908
- self . trunc_page_num_down_to_map_multiple ( gap. start ( ) , pages_per_map) ;
909
- let aligned_end_page =
910
- self . round_page_num_up_to_map_multiple ( gap. end ( ) , pages_per_map) ;
911
-
912
- let gap_size = aligned_end_page - aligned_start_page;
913
- if gap_size >= rounded_num_pages {
914
- return Some ( ie ( aligned_end_page - rounded_num_pages, aligned_end_page) ) ;
915
- }
899
+ for gap in self
900
+ . entries
901
+ . gaps_trimmed ( ie ( start, end) )
902
+ {
903
+ let aligned_start_page =
904
+ self . trunc_page_num_down_to_map_multiple ( gap. start ( ) , pages_per_map) ;
905
+ let aligned_end_page =
906
+ self . round_page_num_up_to_map_multiple ( gap. end ( ) , pages_per_map) ;
907
+
908
+ let gap_size = aligned_end_page - aligned_start_page;
909
+ if gap_size >= rounded_num_pages {
910
+ return Some ( ie ( aligned_end_page - rounded_num_pages, aligned_end_page) ) ;
916
911
}
917
912
}
918
913
@@ -944,26 +939,20 @@ impl VmmapOps for Vmmap {
944
939
hint : u32 ,
945
940
) -> Option < Interval < u32 > > {
946
941
let start = hint;
947
- let end = self . last_entry ( ) ;
942
+ let end = self . end_address ;
948
943
949
- if end == None {
950
- return None ;
951
- } else {
952
- let end_unwrapped = end. unwrap ( ) . 0 . end ( ) ;
953
-
954
- let rounded_num_pages =
955
- self . round_page_num_up_to_map_multiple ( num_pages, pages_per_map) ;
944
+ let rounded_num_pages =
945
+ self . round_page_num_up_to_map_multiple ( num_pages, pages_per_map) ;
956
946
957
- for gap in self . entries . gaps_trimmed ( ie ( start, end_unwrapped ) ) {
958
- let aligned_start_page =
959
- self . trunc_page_num_down_to_map_multiple ( gap. start ( ) , pages_per_map) ;
960
- let aligned_end_page =
961
- self . round_page_num_up_to_map_multiple ( gap. end ( ) , pages_per_map) ;
947
+ for gap in self . entries . gaps_trimmed ( ie ( start, end ) ) {
948
+ let aligned_start_page =
949
+ self . trunc_page_num_down_to_map_multiple ( gap. start ( ) , pages_per_map) ;
950
+ let aligned_end_page =
951
+ self . round_page_num_up_to_map_multiple ( gap. end ( ) , pages_per_map) ;
962
952
963
- let gap_size = aligned_end_page - aligned_start_page;
964
- if gap_size >= rounded_num_pages {
965
- return Some ( ie ( aligned_end_page - rounded_num_pages, aligned_end_page) ) ;
966
- }
953
+ let gap_size = aligned_end_page - aligned_start_page;
954
+ if gap_size >= rounded_num_pages {
955
+ return Some ( ie ( aligned_end_page - rounded_num_pages, aligned_end_page) ) ;
967
956
}
968
957
}
969
958
0 commit comments