openvm_platform/
memory.rsuse super::WORD_SIZE;
pub const MEM_BITS: usize = 28;
pub const MEM_SIZE: usize = 1 << MEM_BITS;
pub const GUEST_MIN_MEM: usize = 0x0000_0400;
pub const GUEST_MAX_MEM: usize = SYSTEM.start;
pub const STACK_TOP: u32 = 0x0020_0400;
pub const TEXT_START: u32 = 0x0020_0800;
pub const SYSTEM: Region = Region::new(0x0C00_0000, mb(16));
pub const PAGE_TABLE: Region = Region::new(0x0D00_0000, mb(16));
pub const PRE_LOAD: Region = Region::new(0x0D70_0000, mb(9));
pub struct Region {
start: usize,
len_bytes: usize,
}
impl Region {
pub const fn new(start: usize, len_bytes: usize) -> Self {
Self { start, len_bytes }
}
pub const fn start(&self) -> usize {
self.start
}
pub const fn len_bytes(&self) -> usize {
self.len_bytes
}
pub const fn len_words(&self) -> usize {
assert!((self.len_bytes % WORD_SIZE) == 0);
self.len_bytes / WORD_SIZE
}
pub const fn end(&self) -> usize {
self.start + self.len_bytes
}
}
const fn kb(kb: usize) -> usize {
kb * 1024
}
const fn mb(mb: usize) -> usize {
kb(mb * 1024)
}
pub fn is_guest_memory(addr: u32) -> bool {
GUEST_MIN_MEM <= (addr as usize) && (addr as usize) < GUEST_MAX_MEM
}
#[cfg(feature = "rust-runtime")]
#[no_mangle]
pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8 {
#[cfg(target_os = "zkvm")]
extern "C" {
static _end: u8;
}
static mut HEAP_POS: usize = 0;
let mut heap_pos = unsafe { HEAP_POS };
#[cfg(target_os = "zkvm")]
if heap_pos == 0 {
heap_pos = unsafe { (&_end) as *const u8 as usize };
}
let align = usize::max(align, WORD_SIZE);
let offset = heap_pos & (align - 1);
if offset != 0 {
heap_pos += align - offset;
}
let ptr = heap_pos as *mut u8;
heap_pos += bytes;
if crate::memory::SYSTEM.start() < heap_pos {
super::rust_rt::terminate::<1>();
}
unsafe { HEAP_POS = heap_pos };
ptr
}