Skip to content

Commit

Permalink
Fix Problems with memory channels
Browse files Browse the repository at this point in the history
Set new ranges for input and output
Add rmp_adjust for new memory
Fix input copy
  • Loading branch information
Sabanic-P committed Dec 9, 2024
1 parent ef6ed2f commit b68ce76
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
18 changes: 14 additions & 4 deletions kernel/src/process_manager/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::address::{Address, VirtAddr};
use crate::process_manager::process_paging::ProcessPageTableRef;
use crate::process_manager::process_paging::ProcessPageFlags;
use crate::cpu::control_regs::read_cr3;
use crate::sev::{rmp_adjust, RMPFlags};
use crate::types::PageSize;
use crate::{paddr_as_slice, map_paddr, vaddr_as_slice};
use crate::mm::PerCPUPageMappingGuard;

Expand All @@ -19,24 +21,32 @@ impl AllocationRange {
pub fn allocate(&mut self, pages: u64){
let mut page_table_ref = ProcessPageTableRef::default();
page_table_ref.set_external_table(read_cr3().bits() as u64);
self.allocate_(&mut page_table_ref, pages, ALLOCATION_VADDR_START, true);
self.allocate_(&mut page_table_ref, pages, ALLOCATION_VADDR_START, true, false);
}

pub fn allocate_with_start_addr(&mut self, page_table_ref: &mut ProcessPageTableRef, pages: u64, start_addr: u64){
self.allocate_(page_table_ref, pages, start_addr, false);
self.allocate_(page_table_ref, pages, start_addr, false, true);
}

fn allocate_(&mut self, page_table_ref: &mut ProcessPageTableRef, pages: u64, start_addr: u64, mount: bool){
fn allocate_(&mut self, page_table_ref: &mut ProcessPageTableRef, pages: u64, start_addr: u64, mount: bool, user: bool){
// Reuses the Process page managment to add new memory to the Monitor
//let mut page_table_ref = ProcessPageTableRef::default();
//page_table_ref.set_external_table(read_cr3().bits() as u64);
let table_flags = ProcessPageFlags::PRESENT | ProcessPageFlags::WRITABLE |
let mut table_flags = ProcessPageFlags::PRESENT | ProcessPageFlags::WRITABLE |
ProcessPageFlags::DIRTY | ProcessPageFlags::ACCESSED;

if user {
table_flags = table_flags | ProcessPageFlags::USER_ACCESSIBLE;

This comment has been minimized.

Copy link
@mmisono

mmisono Dec 9, 2024

Member

@Sabanic-P
Why do we need that? All code runs in ring3? (just wondering)

}

let start_address = VirtAddr::from(start_addr);

for i in 0..(pages as usize) {
let current_page = allocate_page();
if !mount {
let (mapping, _page_mapped) = paddr_as_slice!(current_page);
rmp_adjust(mapping.virt_addr(), RMPFlags::VMPL1 | RMPFlags::RWX, PageSize::Regular);
}
page_table_ref.map_4k_page(start_address + i * PAGE_SIZE, current_page, table_flags);
};
if mount {
Expand Down
13 changes: 6 additions & 7 deletions kernel/src/process_manager/memory_channels.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{address::VirtAddr, mm::PAGE_SIZE, process_manager::process_memory::ALLOCATION_RANGE_VIRT_START};
use log::Metadata;

use crate::{address::{PhysAddr, VirtAddr}, map_paddr, mm::{PerCPUPageMappingGuard, PAGE_SIZE}, paddr_as_slice, process_manager::process_memory::ALLOCATION_RANGE_VIRT_START, vaddr_as_slice};

use super::{allocation::AllocationRange, process::ProcessID, process_paging::ProcessPageTableRef};

pub const INPUT_VADDR: u64 = 0xFF0000000000u64;
pub const OUTPUT_VADDR: u64 = 0xFF8000000000u64;
pub const INPUT_VADDR: u64 = 0x28000000000u64;
pub const OUTPUT_VADDR: u64 = 0x30000000000u64;

#[derive(Debug, Clone, Copy, Default)]
pub struct MemoryChannel {
Expand Down Expand Up @@ -38,14 +40,11 @@ impl MemoryChannel {
let copy_size = size + PAGE_SIZE - (size % PAGE_SIZE);
let copy_page_count = copy_size / PAGE_SIZE;
let target = VirtAddr::from(ALLOCATION_RANGE_VIRT_START);

let mut page_table_ref = ProcessPageTableRef::default();
page_table_ref.set_external_table(page_table);

self.input.mount();

page_table_ref.copy_address_range(VirtAddr::from(source_addr), copy_size as u64, target);

ProcessPageTableRef::copy_data_from_guest_to(source_addr, size as u64, page_table, ALLOCATION_RANGE_VIRT_START);
}

pub fn copy_out(&mut self, target_addr: u64, page_table: u64, size: usize) {
Expand Down
1 change: 1 addition & 0 deletions kernel/src/process_manager/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl TrustedProcess {
let trustlet = TrustedProcess::dublicate(parent);
if data != 0 {
let (function_code, function_code_range) = ProcessPageTableRef::copy_data_from_guest(data, size, pgt);
let size = (4096 - (size & 0xFFF)) + size;
trustlet.base.page_table_ref.add_function(function_code, size);
function_code_range.delete();
}
Expand Down
17 changes: 15 additions & 2 deletions kernel/src/process_manager/process_paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::mem::replace;
use crate::types::PageSize;
use super::process_memory::{ALLOCATION_RANGE_VIRT_START, PGD, PMD, PTE, PUD};
use crate::process_manager::allocation::AllocationRange;

use core::ffi::CStr;
use super::memory_helper::{ZERO_PAGE};

// Flags for the Page Table
Expand Down Expand Up @@ -289,7 +289,7 @@ impl ProcessPageTableRef {
pub fn add_function(&self, data:VirtAddr, size: u64) {
let data: *mut u8 = data.as_mut_ptr::<u8>();
let data = unsafe { slice::from_raw_parts(data, size as usize) };
self.add_region_vaddr(VirtAddr::from(0xFE8000000000u64), data);
self.add_region_vaddr(VirtAddr::from(0x140_0000_0000u64), data);
}

pub fn add_pages(&self, start: VirtAddr, size: u64, flags: ProcessPageFlags) {
Expand Down Expand Up @@ -346,6 +346,7 @@ impl ProcessPageTableRef {
let mut index = ProcessPageTable::index::<PGD>(addr);
let mut table_entry = table[index];


let mut _mapping: PerCPUPageMappingGuard;
//let mut prev_addr = table_entry.0;

Expand Down Expand Up @@ -424,13 +425,25 @@ impl ProcessPageTableRef {
let target = VirtAddr::from(ALLOCATION_RANGE_VIRT_START);

let mut page_table_ref = ProcessPageTableRef::default();

page_table_ref.set_external_table(page_table);

page_table_ref.copy_address_range(VirtAddr::from(addr), copy_size, target);

(target, alloc_range)
}

pub fn copy_data_from_guest_to(addr: u64, size: u64, page_table: u64, dst: u64) {
let copy_size = size + (PAGE_SIZE_4K - size % PAGE_SIZE_4K);
let copy_page_count = copy_size / PAGE_SIZE_4K;
let target = VirtAddr::from(dst);

let mut page_table_ref = ProcessPageTableRef::default();
page_table_ref.set_external_table(page_table);
page_table_ref.copy_address_range(VirtAddr::from(addr), copy_size, target);
}


pub fn map_4k_page(&self, target: VirtAddr, addr: PhysAddr, flags: ProcessPageFlags) {
let (_pgd_mapping, pgd_table) = paddr_as_table!(self.process_page_table);
let mut current_mapping = self.page_walk(&pgd_table, self.process_page_table, target);
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/process_runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
let id = params.rcx;
let function_arg = params.r8;
let function_arg_size = params.r9;
let guest_page_table = params.rdx;

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

Expand All @@ -55,8 +56,7 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
let mut string_pos: usize = 0;
let sev_features = trustlet.context.sev_features;

trustlet.context.channel.copy_into(function_arg, vmsa.cr3, function_arg_size as usize);

trustlet.context.channel.copy_into(function_arg, guest_page_table, function_arg_size as usize);

let mut rc = PALContext{
process: trustlet,
Expand Down

0 comments on commit b68ce76

Please sign in to comment.