Skip to content

Commit ef6ed2f

Browse files
committed
Add memory channel for single Trustlet use
1 parent 92f2938 commit ef6ed2f

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

kernel/src/process_manager/allocation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl AllocationRange {
8484
}
8585

8686
pub fn delete(&self) {
87-
todo!("Not implemented");
87+
8888
}
8989

9090
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::{address::VirtAddr, mm::PAGE_SIZE, process_manager::process_memory::ALLOCATION_RANGE_VIRT_START};
2+
3+
use super::{allocation::AllocationRange, process::ProcessID, process_paging::ProcessPageTableRef};
4+
5+
pub const INPUT_VADDR: u64 = 0xFF0000000000u64;
6+
pub const OUTPUT_VADDR: u64 = 0xFF8000000000u64;
7+
8+
#[derive(Debug, Clone, Copy, Default)]
9+
pub struct MemoryChannel {
10+
pub input: AllocationRange,
11+
pub output: AllocationRange,
12+
pub owner: ProcessID,
13+
pub last_in_channel: bool,
14+
pub next: ProcessID,
15+
}
16+
17+
impl MemoryChannel {
18+
19+
pub fn allocate_input(&mut self, page_table_ref: &mut ProcessPageTableRef, size: usize) {
20+
self.input = self.allocate_range(page_table_ref, size, INPUT_VADDR);
21+
}
22+
23+
pub fn allocate_output(&mut self, page_table_ref: &mut ProcessPageTableRef, size: usize) {
24+
self.output = self.allocate_range(page_table_ref, size, OUTPUT_VADDR);
25+
}
26+
27+
pub fn inflate_input(&mut self, page_table_ref: &mut ProcessPageTableRef, size: usize) {
28+
let page_count = (size + PAGE_SIZE - (size % PAGE_SIZE)) / PAGE_SIZE;
29+
self.input.inflate(page_table_ref, page_count as u64, INPUT_VADDR);
30+
}
31+
32+
pub fn inflate_output(&mut self, page_table_ref: &mut ProcessPageTableRef, size: usize) {
33+
let page_count = (size + PAGE_SIZE - (size % PAGE_SIZE)) / PAGE_SIZE;
34+
self.output.inflate(page_table_ref, page_count as u64, OUTPUT_VADDR);
35+
}
36+
37+
pub fn copy_into(&mut self, source_addr: u64, page_table: u64, size: usize) {
38+
let copy_size = size + PAGE_SIZE - (size % PAGE_SIZE);
39+
let copy_page_count = copy_size / PAGE_SIZE;
40+
let target = VirtAddr::from(ALLOCATION_RANGE_VIRT_START);
41+
42+
let mut page_table_ref = ProcessPageTableRef::default();
43+
page_table_ref.set_external_table(page_table);
44+
45+
self.input.mount();
46+
47+
page_table_ref.copy_address_range(VirtAddr::from(source_addr), copy_size as u64, target);
48+
49+
}
50+
51+
pub fn copy_out(&mut self, target_addr: u64, page_table: u64, size: usize) {
52+
let copy_size = size + PAGE_SIZE - (size % PAGE_SIZE);
53+
let copy_page_count = copy_size / PAGE_SIZE;
54+
let target = VirtAddr::from(ALLOCATION_RANGE_VIRT_START);
55+
let mut page_table_ref = ProcessPageTableRef::default();
56+
page_table_ref.set_external_table(page_table);
57+
58+
self.output.mount();
59+
60+
page_table_ref.copy_address_range(VirtAddr::from(target), copy_size as u64, VirtAddr::from(target_addr));
61+
}
62+
63+
fn allocate_range(&mut self, page_table_ref: &mut ProcessPageTableRef, size: usize, start: u64) -> AllocationRange{
64+
let mut r = AllocationRange::default();
65+
let page_count = (size + PAGE_SIZE - (size % PAGE_SIZE)) / PAGE_SIZE;
66+
r.allocate_with_start_addr(page_table_ref, page_count as u64, start);
67+
return r;
68+
}
69+
70+
}

kernel/src/process_manager/process.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ impl TrustedProcess {
173173

174174
}
175175

176-
pub fn trustlet(parent: ProcessID, _data: u64, _size: u64, _pgt: u64) -> Self{
176+
pub fn trustlet(parent: ProcessID, data: u64, size: u64, pgt: u64) -> Self{
177177
// Inherit the data from the Zygote
178178
let trustlet = TrustedProcess::dublicate(parent);
179+
if data != 0 {
180+
let (function_code, function_code_range) = ProcessPageTableRef::copy_data_from_guest(data, size, pgt);
181+
trustlet.base.page_table_ref.add_function(function_code, size);
182+
function_code_range.delete();
183+
}
179184
trustlet
180185
}
181186

@@ -421,6 +426,11 @@ impl ProcessContext {
421426

422427
}
423428

429+
pub fn add_function(&mut self, function: VirtAddr, size: u64) {
430+
let size = size + PAGE_SIZE_4K - (size % PAGE_SIZE_4K);
431+
self.base.page_table_ref.add_function(function, size);
432+
}
433+
424434
pub fn test_run(&self) {
425435
let apic_id = this_cpu().get_apic_id();
426436
log::info!("Trying to execute Context");

kernel/src/process_manager/process_paging.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ impl ProcessPageTableRef {
286286
self.add_region_vaddr(VirtAddr::from(0x18000000000u64), data);
287287
}
288288

289+
pub fn add_function(&self, data:VirtAddr, size: u64) {
290+
let data: *mut u8 = data.as_mut_ptr::<u8>();
291+
let data = unsafe { slice::from_raw_parts(data, size as usize) };
292+
self.add_region_vaddr(VirtAddr::from(0xFE8000000000u64), data);
293+
}
294+
289295
pub fn add_pages(&self, start: VirtAddr, size: u64, flags: ProcessPageFlags) {
290296
for i in 0..(size as usize) {
291297
let new_page = allocate_page();
@@ -394,7 +400,7 @@ impl ProcessPageTableRef {
394400
pub fn virt_to_phys(&self, vaddr: VirtAddr) -> PhysAddr {
395401
let (_pgd_mapping, pgd_table) = paddr_as_table!(self.process_page_table);
396402
let mut current_mapping = self.page_walk(&pgd_table, self.process_page_table, vaddr);
397-
log::info!("Current Mapping {:?}", current_mapping);
403+
//log::info!("Current Mapping {:?}", current_mapping);
398404
match current_mapping {
399405
ProcessTableLevelMapping::PTE(addr, index) => {
400406
let (_mapping, table) = paddr_as_u64_slice!(addr);

kernel/src/process_runtime/runtime.rs

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
3737
log::info!("Invoking Trustlet");
3838

3939
let id = params.rcx;
40+
let function_arg = params.r8;
41+
let function_arg_size = params.r9;
4042

4143
let trustlet = PROCESS_STORE.get(ProcessID(id.try_into().unwrap()));
4244

@@ -53,6 +55,8 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
5355
let mut string_pos: usize = 0;
5456
let sev_features = trustlet.context.sev_features;
5557

58+
trustlet.context.channel.copy_into(function_arg, vmsa.cr3, function_arg_size as usize);
59+
5660

5761
let mut rc = PALContext{
5862
process: trustlet,

0 commit comments

Comments
 (0)