Skip to main content

hyperlight_common/
layout.rs

1/*
2Copyright 2025  The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15 */
16
17#[cfg_attr(target_arch = "x86", path = "arch/i686/layout.rs")]
18#[cfg_attr(
19    all(target_arch = "x86_64", not(feature = "nanvix-unstable")),
20    path = "arch/amd64/layout.rs"
21)]
22#[cfg_attr(
23    all(target_arch = "x86_64", feature = "nanvix-unstable"),
24    path = "arch/i686/layout.rs"
25)]
26#[cfg_attr(target_arch = "aarch64", path = "arch/aarch64/layout.rs")]
27mod arch;
28
29pub use arch::{MAX_GPA, MAX_GVA};
30#[cfg(any(
31    all(target_arch = "x86_64", not(feature = "nanvix-unstable")),
32    target_arch = "aarch64"
33))]
34pub use arch::{SNAPSHOT_PT_GVA_MAX, SNAPSHOT_PT_GVA_MIN};
35
36// offsets down from the top of scratch memory for various things
37pub const SCRATCH_TOP_SIZE_OFFSET: u64 = 0x08;
38pub const SCRATCH_TOP_ALLOCATOR_OFFSET: u64 = 0x10;
39pub const SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET: u64 = 0x18;
40pub const SCRATCH_TOP_EXN_STACK_OFFSET: u64 = 0x20;
41
42/// Offset from the top of scratch memory for a shared host-guest u64 counter.
43///
44/// This is placed at 0x1008 (rather than the next sequential 0x28) so that the
45/// counter falls in scratch page 0xffffe000 instead of the very last page
46/// 0xfffff000, which on i686 guests would require frame 0xfffff — exceeding the
47/// maximum representable frame number.
48#[cfg(feature = "nanvix-unstable")]
49pub const SCRATCH_TOP_GUEST_COUNTER_OFFSET: u64 = 0x1008;
50
51pub fn scratch_base_gpa(size: usize) -> u64 {
52    (MAX_GPA - size + 1) as u64
53}
54pub fn scratch_base_gva(size: usize) -> u64 {
55    (MAX_GVA - size + 1) as u64
56}
57
58/// Compute the minimum scratch region size needed for a sandbox.
59pub use arch::min_scratch_size;