Skip to content

Commit 02e3858

Browse files
committed
[mem/{layout,memory_region,mgr}] removed kernel stack and boot stack
These memory regions were unused Signed-off-by: danbugs <[email protected]>
1 parent b581c8d commit 02e3858

File tree

3 files changed

+6
-210
lines changed

3 files changed

+6
-210
lines changed

src/hyperlight_host/src/mem/layout.rs

Lines changed: 5 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use rand::{rng, RngCore};
2222
use tracing::{instrument, Span};
2323

2424
use super::memory_region::MemoryRegionType::{
25-
BootStack, Code, GuardPage, GuestErrorData, Heap, HostExceptionData, HostFunctionDefinitions,
26-
InputData, KernelStack, OutputData, PageTables, PanicContext, Peb, Stack,
25+
Code, GuardPage, GuestErrorData, Heap, HostExceptionData, HostFunctionDefinitions, InputData,
26+
OutputData, PageTables, PanicContext, Peb, Stack,
2727
};
2828
use super::memory_region::{MemoryRegion, MemoryRegionFlags, MemoryRegionVecBuilder};
2929
use super::mgr::AMOUNT_OF_MEMORY_PER_PT;
@@ -32,14 +32,6 @@ use crate::error::HyperlightError::{GuestOffsetIsInvalid, MemoryRequestTooBig};
3232
use crate::sandbox::SandboxConfiguration;
3333
use crate::{log_then_return, new_error, Result};
3434

35-
// +-------------------------------------------+
36-
// | Boot Stack (4KiB) |
37-
// +-------------------------------------------+
38-
// | Kernel Stack Guard Page (4KiB) |
39-
// +-------------------------------------------+
40-
// | Kernel Stack |
41-
// +-------------------------------------------+
42-
// | Guest Stack Guard Page (4KiB) |
4335
// +-------------------------------------------+
4436
// | Guest (User) Stack |
4537
// +-------------------------------------------+
@@ -71,10 +63,6 @@ use crate::{log_then_return, new_error, Result};
7163
// +-------------------------------------------+ 0x1_000
7264
// | PML4 |
7365
// +-------------------------------------------+ 0x0_000
74-
// | ⋮ |
75-
// | Unmapped |
76-
// | ⋮ |
77-
// +-------------------------------------------+ 0x0
7866

7967
///
8068
/// - `HostDefinitions` - the length of this is the `HostFunctionDefinitionSize`
@@ -106,11 +94,6 @@ use crate::{log_then_return, new_error, Result};
10694
/// - `GuestPanicContext` - contains a buffer for context associated with any guest
10795
/// panic that occurred.
10896
/// the length of this field is returned by the `guest_panic_context_size()` fn of this struct.
109-
///
110-
/// Boot Stack - this is the stack that is used before the TSS is set up. It is fixed to 4K
111-
/// Kernel Stack Guard Page is to Guard against boot stack overflow so we dont corrupt the kernel stack
112-
/// Kernel Stack - this is the stack that is used for kernel mode operations we switch to this early in the initialization function
113-
/// Guest Stack Guard Page is to Guard against kernel stack overflow so we dont corrupt the user stack
11497
11598
#[derive(Copy, Clone)]
11699
pub(crate) struct SandboxMemoryLayout {
@@ -147,12 +130,6 @@ pub(crate) struct SandboxMemoryLayout {
147130
guest_heap_buffer_offset: usize,
148131
guard_page_offset: usize,
149132
guest_user_stack_buffer_offset: usize, // the lowest address of the user stack
150-
user_stack_guard_page_offset: usize,
151-
kernel_stack_buffer_offset: usize,
152-
kernel_stack_guard_page_offset: usize,
153-
#[allow(dead_code)]
154-
pub(super) kernel_stack_size_rounded: usize,
155-
boot_stack_buffer_offset: usize,
156133

157134
// other
158135
pub(crate) peb_address: usize,
@@ -263,22 +240,6 @@ impl Debug for SandboxMemoryLayout {
263240
"Guest Code Offset",
264241
&format_args!("{:#x}", self.guest_code_offset),
265242
)
266-
.field(
267-
"User Stack Guard Page Offset",
268-
&format_args!("{:#x}", self.user_stack_guard_page_offset),
269-
)
270-
.field(
271-
"Kernel Stack Buffer Offset",
272-
&format_args!("{:#x}", self.kernel_stack_buffer_offset),
273-
)
274-
.field(
275-
"Kernel Stack Guard Page Offset",
276-
&format_args!("{:#x}", self.kernel_stack_guard_page_offset),
277-
)
278-
.field(
279-
"Boot Stack Buffer Offset",
280-
&format_args!("{:#x}", self.boot_stack_buffer_offset),
281-
)
282243
.finish()
283244
}
284245
}
@@ -384,12 +345,6 @@ impl SandboxMemoryLayout {
384345
// round up stack size to page size. This is needed for MemoryRegion
385346
let stack_size_rounded = round_up_to(stack_size, PAGE_SIZE_USIZE);
386347

387-
let user_stack_guard_page_offset = guest_user_stack_buffer_offset + stack_size_rounded;
388-
let kernel_stack_buffer_offset = user_stack_guard_page_offset + PAGE_SIZE_USIZE;
389-
let kernel_stack_size_rounded = round_up_to(cfg.get_kernel_stack_size(), PAGE_SIZE_USIZE);
390-
let kernel_stack_guard_page_offset = kernel_stack_buffer_offset + kernel_stack_size_rounded;
391-
let boot_stack_buffer_offset = kernel_stack_guard_page_offset + PAGE_SIZE_USIZE;
392-
393348
Ok(Self {
394349
peb_offset,
395350
stack_size: stack_size_rounded,
@@ -420,11 +375,6 @@ impl SandboxMemoryLayout {
420375
guard_page_offset,
421376
total_page_table_size,
422377
guest_code_offset,
423-
user_stack_guard_page_offset,
424-
kernel_stack_buffer_offset,
425-
kernel_stack_guard_page_offset,
426-
kernel_stack_size_rounded,
427-
boot_stack_buffer_offset,
428378
})
429379
}
430380

@@ -591,22 +541,6 @@ impl SandboxMemoryLayout {
591541
self.get_min_guest_stack_address_offset() + size_of::<u64>()
592542
}
593543

594-
/// Get the offset of the kernel stack pointer in guest memory,
595-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
596-
fn get_kernel_stack_pointer_offset(&self) -> usize {
597-
// The kernelStackAddress is immediately after the
598-
// userStackAddress in the `GuestStackData` struct which is a `u64`.
599-
self.get_user_stack_pointer_offset() + size_of::<u64>()
600-
}
601-
602-
/// Get the offset of the boot stack pointer in guest memory,
603-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
604-
fn get_boot_stack_pointer_offset(&self) -> usize {
605-
// The bootStackAddress is immediately after the
606-
// kernelStackAddress in the `GuestStackData` struct which is a `u64`.
607-
self.get_kernel_stack_pointer_offset() + size_of::<u64>()
608-
}
609-
610544
// Get the offset in guest memory to the start of the guest panic context data
611545
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
612546
pub(crate) fn get_guest_panic_context_offset(&self) -> usize {
@@ -644,7 +578,7 @@ impl SandboxMemoryLayout {
644578
/// layout.
645579
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
646580
fn get_unaligned_memory_size(&self) -> usize {
647-
self.get_boot_stack_buffer_offset() + PAGE_SIZE_USIZE
581+
self.get_top_of_user_stack_offset() + self.stack_size
648582
}
649583

650584
/// get the code offset
@@ -660,34 +594,6 @@ impl SandboxMemoryLayout {
660594
Self::BASE_ADDRESS + self.guest_code_offset
661595
}
662596

663-
/// Get the offset in guest memory to the user stack guard page
664-
/// This is the offset in the sandbox memory where the user stack guard page starts
665-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
666-
pub(super) fn get_user_stack_guard_page_offset(&self) -> usize {
667-
self.user_stack_guard_page_offset
668-
}
669-
670-
/// Get the offset in guest memory to the kernel stack buffer
671-
/// This is the offset in the sandbox memory where the kernel stack starts
672-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
673-
pub(super) fn get_kernel_stack_buffer_offset(&self) -> usize {
674-
self.kernel_stack_buffer_offset
675-
}
676-
677-
/// Get the offset in guest memory to the kernel stack guard page
678-
/// This is the offset in the sandbox memory where the kernel stack guard page starts
679-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
680-
pub(super) fn get_kernel_stack_guard_page_offset(&self) -> usize {
681-
self.kernel_stack_guard_page_offset
682-
}
683-
684-
/// Get the offset in guest memory to the boot stack buffer
685-
/// This is the offset in the sandbox memory where the boot stack starts
686-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
687-
pub(super) fn get_boot_stack_buffer_offset(&self) -> usize {
688-
self.boot_stack_buffer_offset
689-
}
690-
691597
#[cfg(test)]
692598
/// Get the page table size
693599
fn get_page_table_size(&self) -> usize {
@@ -971,80 +877,12 @@ impl SandboxMemoryLayout {
971877
}
972878

973879
// stack
974-
let user_stack_guard_page_offset = builder.push_page_aligned(
880+
let final_offset = builder.push_page_aligned(
975881
self.get_guest_stack_size(),
976882
MemoryRegionFlags::READ | MemoryRegionFlags::WRITE,
977883
Stack,
978884
);
979885

980-
let expected_user_stack_guard_page_offset =
981-
TryInto::<usize>::try_into(self.get_top_of_user_stack_offset())?
982-
+ self.get_guest_stack_size();
983-
984-
if user_stack_guard_page_offset != expected_user_stack_guard_page_offset {
985-
return Err(new_error!(
986-
"User Guard Page offset does not match expected User Guard Page offset expected: {}, actual: {}",
987-
expected_user_stack_guard_page_offset,
988-
user_stack_guard_page_offset
989-
));
990-
}
991-
992-
let kernel_stack_offset = builder.push_page_aligned(
993-
PAGE_SIZE_USIZE,
994-
MemoryRegionFlags::READ | MemoryRegionFlags::STACK_GUARD,
995-
GuardPage,
996-
);
997-
998-
let expected_kernel_stack_offset =
999-
TryInto::<usize>::try_into(self.kernel_stack_buffer_offset)?;
1000-
1001-
if kernel_stack_offset != expected_kernel_stack_offset {
1002-
return Err(new_error!(
1003-
"Kernel Stack offset does not match expected Kernel Stack offset expected: {}, actual: {}",
1004-
expected_kernel_stack_offset,
1005-
kernel_stack_offset
1006-
));
1007-
}
1008-
1009-
let kernel_stack_guard_page_offset = builder.push_page_aligned(
1010-
self.kernel_stack_size_rounded,
1011-
MemoryRegionFlags::READ | MemoryRegionFlags::WRITE,
1012-
KernelStack,
1013-
);
1014-
1015-
let expected_kernel_stack_guard_page_offset =
1016-
TryInto::<usize>::try_into(self.kernel_stack_guard_page_offset)?;
1017-
1018-
if kernel_stack_guard_page_offset != expected_kernel_stack_guard_page_offset {
1019-
return Err(new_error!(
1020-
"Kernel Guard Page offset does not match expected Kernel Guard Page offset expected: {}, actual: {}",
1021-
expected_kernel_stack_guard_page_offset,
1022-
kernel_stack_guard_page_offset
1023-
));
1024-
}
1025-
1026-
let boot_stack_offset = builder.push_page_aligned(
1027-
PAGE_SIZE_USIZE,
1028-
MemoryRegionFlags::READ | MemoryRegionFlags::STACK_GUARD,
1029-
GuardPage,
1030-
);
1031-
1032-
let expected_boot_stack_offset = TryInto::<usize>::try_into(self.boot_stack_buffer_offset)?;
1033-
1034-
if boot_stack_offset != expected_boot_stack_offset {
1035-
return Err(new_error!(
1036-
"Boot Stack offset does not match expected Boot Stack offset expected: {}, actual: {}",
1037-
expected_boot_stack_offset,
1038-
boot_stack_offset
1039-
));
1040-
}
1041-
1042-
let final_offset = builder.push_page_aligned(
1043-
PAGE_SIZE_USIZE,
1044-
MemoryRegionFlags::READ | MemoryRegionFlags::WRITE,
1045-
BootStack,
1046-
);
1047-
1048886
let expected_final_offset = TryInto::<usize>::try_into(self.get_memory_size()?)?;
1049887

1050888
if final_offset != expected_final_offset {
@@ -1187,17 +1025,9 @@ impl SandboxMemoryLayout {
11871025

11881026
// The top of the user stack is calculated as the size of the guest memory + the guest offset which gives us the
11891027
// address at the bottom of the guest memory.
1190-
// we then subtract the size of the stack, the size of the kernel stack,
1191-
// the size of the boot stack, the size of the user stack guard page and the size of the kernel stack guard page
1192-
// which are all 4K
11931028

11941029
let bottom = guest_offset + size;
1195-
let min_user_stack_address = bottom
1196-
- self.stack_size
1197-
- self.kernel_stack_size_rounded
1198-
- PAGE_SIZE_USIZE
1199-
- PAGE_SIZE_USIZE
1200-
- PAGE_SIZE_USIZE;
1030+
let min_user_stack_address = bottom - self.stack_size;
12011031

12021032
// Top of user stack
12031033

@@ -1212,26 +1042,6 @@ impl SandboxMemoryLayout {
12121042

12131043
shared_mem.write_u64(self.get_user_stack_pointer_offset(), start_of_user_stack)?;
12141044

1215-
// Start of kernel stack
1216-
1217-
// There is a guard page between the user stack and the kernel stack and then we need to add the size of the kernel stack
1218-
1219-
let start_of_kernel_stack: u64 =
1220-
start_of_user_stack + (PAGE_SIZE_USIZE + self.kernel_stack_size_rounded) as u64;
1221-
1222-
shared_mem.write_u64(
1223-
self.get_kernel_stack_pointer_offset(),
1224-
start_of_kernel_stack,
1225-
)?;
1226-
1227-
// Start of boot stack
1228-
1229-
// There is a guard page between the kernel stack and the boot stack and then we need to add the size of the boot stack
1230-
1231-
let start_of_boot_stack: u64 = start_of_kernel_stack + (PAGE_SIZE_USIZE * 2) as u64;
1232-
1233-
shared_mem.write_u64(self.get_boot_stack_pointer_offset(), start_of_boot_stack)?;
1234-
12351045
// End of setting up the PEB
12361046

12371047
// Initialize the stack pointers of input data and output data
@@ -1306,14 +1116,6 @@ mod tests {
13061116

13071117
expected_size += round_up_to(layout.stack_size, PAGE_SIZE_USIZE);
13081118

1309-
expected_size += PAGE_SIZE_USIZE; // user stack guard page
1310-
1311-
expected_size += round_up_to(layout.kernel_stack_size_rounded, PAGE_SIZE_USIZE);
1312-
1313-
expected_size += PAGE_SIZE_USIZE; // kernel stack guard page
1314-
1315-
expected_size += PAGE_SIZE_USIZE; // boot stack
1316-
13171119
expected_size
13181120
}
13191121

src/hyperlight_host/src/mem/memory_region.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ pub enum MemoryRegionType {
149149
GuardPage,
150150
/// The region contains the Stack
151151
Stack,
152-
/// The region contains the Kernel Stack
153-
KernelStack,
154-
/// The region contains the Boot Stack
155-
BootStack,
156152
}
157153

158154
/// represents a single memory region inside the guest. All memory within a region has

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const PAGE_USER: u64 = 1 << 2; // User/Supervisor (if this bit is set then the p
5858
const PAGE_NX: u64 = 1 << 63; // Execute Disable (if this bit is set then data in the page cannot be executed)
5959

6060
// The amount of memory that can be mapped per page table
61-
pub(super) const AMOUNT_OF_MEMORY_PER_PT: usize = 0x200000;
61+
pub(super) const AMOUNT_OF_MEMORY_PER_PT: usize = 0x200_000;
6262
/// Read/write permissions flag for the 64-bit PDE
6363
/// The page size for the 64-bit PDE
6464
/// The size of stack guard cookies
@@ -213,8 +213,6 @@ where
213213
// Host Exception Data are readonly in the guest
214214
MemoryRegionType::HostExceptionData => PAGE_PRESENT | PAGE_NX,
215215
MemoryRegionType::PageTables => PAGE_PRESENT | PAGE_RW | PAGE_NX,
216-
MemoryRegionType::KernelStack => PAGE_PRESENT | PAGE_RW | PAGE_NX,
217-
MemoryRegionType::BootStack => PAGE_PRESENT | PAGE_RW | PAGE_NX,
218216
},
219217
// If there is an error then the address isn't mapped so mark it as not present
220218
Err(_) => 0,

0 commit comments

Comments
 (0)