Skip to content

Commit

Permalink
Add memory channels with fixed address ranges to ProcessContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabanic-P committed Dec 6, 2024
1 parent a6b58aa commit 92f2938
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
63 changes: 54 additions & 9 deletions kernel/src/process_manager/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,80 @@ use crate::cpu::control_regs::read_cr3;
use crate::{paddr_as_slice, map_paddr, vaddr_as_slice};
use crate::mm::PerCPUPageMappingGuard;

const ALLOCATION_VADDR_START: u64 = 0x30000000000u64;
pub const DEFAULT_ALLOCATION_RANGE_MOUNT: usize = 6;
const PGD_SHIFT: u64 = 39;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub struct AllocationRange(pub u64, pub u64);

impl AllocationRange {
impl AllocationRange {

pub fn allocate(&mut self, pages: u64){
// 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);
self.allocate_(&mut page_table_ref, pages, ALLOCATION_VADDR_START, true);
}

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);
}

fn allocate_(&mut self, page_table_ref: &mut ProcessPageTableRef, pages: u64, start_addr: u64, mount: 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 |
ProcessPageFlags::DIRTY | ProcessPageFlags::ACCESSED;
ProcessPageFlags::DIRTY | ProcessPageFlags::ACCESSED;

let start_address = VirtAddr::from(0x30000000000u64);
let start_address = VirtAddr::from(start_addr);

for i in 0..(pages as usize) {
let current_page = allocate_page();
page_table_ref.map_4k_page(start_address + i * PAGE_SIZE, current_page, table_flags);
};
if mount {
let (_mapping, pgd) = paddr_as_slice!(read_cr3());
self.0 = pgd[DEFAULT_ALLOCATION_RANGE_MOUNT];
self.1 = pages;
} else {
let offset = start_addr >> PGD_SHIFT;
let (_mapping, pgd) = paddr_as_slice!(page_table_ref.process_page_table);
self.0 = pgd[offset as usize];
self.1 = pages;
}
}

let (_mapping, pgd) = paddr_as_slice!(read_cr3());
self.0 = pgd[6];
pub fn inflate(&mut self, page_table_ref: &mut ProcessPageTableRef, pages: u64, start_addr: u64) {
if self.1 >= pages {
return;
}
let table_flags = ProcessPageFlags::PRESENT | ProcessPageFlags::WRITABLE |
ProcessPageFlags::DIRTY | ProcessPageFlags::ACCESSED;
let start_address = VirtAddr::from(start_addr);
let begin = self.1;
for i in 0..(pages as usize) {
let current_page = allocate_page();
page_table_ref.map_4k_page(start_address + i * PAGE_SIZE, current_page, table_flags);
}
self.1 = pages;

}

pub fn mount(&self) {
let (_mapping, pgd) = paddr_as_slice!(read_cr3());
pgd[6] = self.0;
pgd[DEFAULT_ALLOCATION_RANGE_MOUNT] = self.0;
}

pub fn mount_at(&self, loc: usize) -> u64 {
let (_mapping, pgd) = paddr_as_slice!(read_cr3());
let old_table = pgd[loc];
pgd[loc] = self.0;
return old_table;
}

pub fn reset_mount(&self, loc: usize, t: u64) {
let (_mapping, pgd) = paddr_as_slice!(read_cr3());
pgd[loc] = t;
}

pub fn delete(&self) {
Expand Down
1 change: 1 addition & 0 deletions kernel/src/process_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod process_memory;
pub mod process_paging;
pub mod memory_helper;
pub mod allocation;
pub mod memory_channels;

static MONITOR_INIT_STATE: ImmutAfterInitCell<bool> = ImmutAfterInitCell::new(false);
const MONITOR_INIT_STATE_TRUE: bool = true;
Expand Down
21 changes: 17 additions & 4 deletions kernel/src/process_manager/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ extern crate alloc;

use core::cell::UnsafeCell;
use alloc::vec::Vec;
use igvm_defs::PAGE_SIZE_4K;
use crate::address::PhysAddr;
use crate::cpu::percpu::this_cpu_shared;
use crate::cpu::percpu::this_cpu_unsafe;
use crate::mm::PAGE_SIZE;
use crate::mm::SVSM_PERCPU_VMSA_BASE;
use crate::process_manager::process_memory::allocate_page;
use crate::process_manager::allocation::AllocationRange;
Expand All @@ -25,6 +27,8 @@ use crate::vaddr_as_u64_slice;
use cpuarch::vmsa::VMSA;
use core::mem::replace;

use super::memory_channels::MemoryChannel;

trait FromVAddr {
fn from_virt_addr(v: VirtAddr) -> &'static mut VMSA;
}
Expand Down Expand Up @@ -103,7 +107,7 @@ impl ProcessData {
}
}

#[derive(Clone,Copy,Debug)]
#[derive(Clone,Copy,Debug, Default)]
pub struct ProcessID(pub usize);

#[derive(Clone,Copy,Debug)]
Expand All @@ -113,9 +117,7 @@ pub struct TrustedProcess {
pub base: ProcessBaseContext,
#[allow(dead_code)]
pub context: ProcessContext,
/*input: VirtAddr,
output: VirtAddr,
pub hash: [u8; 32]i,*/
//pub channel: MemoryChannel,
}

impl TrustedProcess {
Expand Down Expand Up @@ -346,6 +348,7 @@ impl ProcessBaseContext {
pub struct ProcessContext {
pub base: ProcessBaseContext,
pub vmsa: PhysAddr,
pub channel: MemoryChannel,
pub sev_features: u64,
}

Expand All @@ -354,6 +357,7 @@ impl Default for ProcessContext {
return ProcessContext {
base: ProcessBaseContext::default(),
vmsa: PhysAddr::null(),
channel: MemoryChannel::default(),
sev_features: 0,
}
}
Expand Down Expand Up @@ -402,6 +406,15 @@ impl ProcessContext {
panic!("Failed to create new VMSA");
}


//Memory Channel setup -- No chain setup here
let page_table_addr = vmsa.cr3;
let mut pptr = ProcessPageTableRef::default();
pptr.set_external_table(page_table_addr);
self.channel.allocate_input(&mut pptr, PAGE_SIZE);
self.channel.allocate_output(&mut pptr, PAGE_SIZE);


self.vmsa = new_vmsa_page;
self.sev_features = vmsa.sev_features;
self.base = base;
Expand Down

0 comments on commit 92f2938

Please sign in to comment.