mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
split memory allocs into RAM and peripheral tables
RAM allocs can be augmented with more tracking data in the case of swap.
This commit is contained in:
parent
f657caa176
commit
903c903b2f
4 changed files with 94 additions and 44 deletions
|
|
@ -104,14 +104,12 @@ pub extern "C" fn start_kernel(
|
|||
args: usize,
|
||||
ss: usize,
|
||||
rpt: usize,
|
||||
xpt: usize,
|
||||
satp: usize,
|
||||
entrypoint: usize,
|
||||
stack: usize,
|
||||
debug_: bool,
|
||||
resume_: bool,
|
||||
debug_resume: usize,
|
||||
) -> ! {
|
||||
let debug: usize = if debug_ { 1 } else { 0 };
|
||||
let resume: usize = if resume_ { 1 } else { 0 };
|
||||
unsafe {
|
||||
#[rustfmt::skip]
|
||||
asm! (
|
||||
|
|
@ -119,7 +117,7 @@ pub extern "C" fn start_kernel(
|
|||
"mv a0, {args}",
|
||||
"mv a1, {ss}",
|
||||
"mv a2, {rpt}",
|
||||
"mv a7, {resume}",
|
||||
"mv a3, {xpt}",
|
||||
// Delegate as much as we can supervisor mode
|
||||
"li t0, 0xffffffff",
|
||||
"csrw mideleg, t0",
|
||||
|
|
@ -128,13 +126,14 @@ pub extern "C" fn start_kernel(
|
|||
// Return to Supervisor mode (1 << 11) when we call `reti`.
|
||||
// Disable interrupts (0 << 5)
|
||||
"li t0, (1 << 11) | (0 << 5)",
|
||||
// If arg6 is "true", also set mstatus.SUM to allow the kernel
|
||||
// If debug_resume is "true", also set mstatus.SUM to allow the kernel
|
||||
// to access userspace memory.
|
||||
"mv a6, {debug}",
|
||||
"andi a6, a6, 1",
|
||||
"slli a6, a6, 18",
|
||||
"or t0, t0, a6",
|
||||
"mv t3, {debug_resume}",
|
||||
"andi t3, t3, 1",
|
||||
"slli t3, t3, 18",
|
||||
"or t0, t0, t3",
|
||||
"csrw mstatus, t0",
|
||||
"mv a7, {debug_resume}",
|
||||
|
||||
// Enable the MMU (once we issue `mret`) and flush the cache
|
||||
"csrw satp, {satp}",
|
||||
|
|
@ -151,11 +150,11 @@ pub extern "C" fn start_kernel(
|
|||
args = in(reg) args,
|
||||
ss = in(reg) ss,
|
||||
rpt = in(reg) rpt,
|
||||
xpt = in(reg) xpt,
|
||||
satp = in(reg) satp,
|
||||
entrypoint = in(reg) entrypoint,
|
||||
stack = in(reg) stack,
|
||||
debug = in(reg) debug,
|
||||
resume = in(reg) resume,
|
||||
debug_resume = in(reg) debug_resume,
|
||||
options(noreturn)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,9 +44,16 @@ pub struct BootConfig {
|
|||
pub extra_pages: usize,
|
||||
|
||||
/// This structure keeps track of which pages are owned
|
||||
/// and which are free. A PID of `0` indicates it's free.
|
||||
/// and which are free in main RAM. A PID of `0` indicates it's free.
|
||||
/// Because this area is swappable, a type alias of XousAlloc allows
|
||||
/// us to expand the tracking for the RAM area.
|
||||
pub runtime_page_tracker: &'static mut [XousAlloc],
|
||||
|
||||
/// This structure keeps track of which pages are owned
|
||||
/// and which are free in non-RAM areas. A PID of `0` indicates it's free.
|
||||
/// These areas are non-swappable, so the tracking is a simple XousPid
|
||||
pub extra_page_tracker: &'static mut [XousPid],
|
||||
|
||||
/// A list of processes that were set up. The first element
|
||||
/// is the kernel, and any subsequent elements are init processes.
|
||||
pub processes: &'static mut [InitialProcess],
|
||||
|
|
@ -96,6 +103,7 @@ impl Default for BootConfig {
|
|||
init_size: 0,
|
||||
extra_pages: 0,
|
||||
runtime_page_tracker: Default::default(),
|
||||
extra_page_tracker: Default::default(),
|
||||
init_process_count: 0,
|
||||
processes: Default::default(),
|
||||
swap_offset: 0,
|
||||
|
|
@ -162,16 +170,16 @@ impl BootConfig {
|
|||
return;
|
||||
}
|
||||
// The region isn't in RAM, so check the other memory regions.
|
||||
let mut rpt_offset = self.sram_size / PAGE_SIZE;
|
||||
let mut xpt_offset = 0;
|
||||
|
||||
for region in self.regions.iter() {
|
||||
let rstart = region.start as usize;
|
||||
let rlen = region.length as usize;
|
||||
if addr >= rstart && addr < rstart + rlen {
|
||||
self.runtime_page_tracker[rpt_offset + (addr - rstart) / PAGE_SIZE] = XousAlloc::from(pid);
|
||||
self.extra_page_tracker[xpt_offset + (addr - rstart) / PAGE_SIZE] = XousPid::from(pid);
|
||||
return;
|
||||
}
|
||||
rpt_offset += rlen / PAGE_SIZE;
|
||||
xpt_offset += rlen / PAGE_SIZE;
|
||||
}
|
||||
panic!("Tried to change region {:08x} that isn't in defined memory!", addr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -212,6 +212,8 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
resume_csr.wfo(utra::susres::INTERRUPT_INTERRUPT, 1);
|
||||
}
|
||||
|
||||
// condense debug and resume arguments into a single register, so we have space for XPT
|
||||
let debug_resume = if cfg.debug { 0x1 } else { 0x0 } | if clean { 0x2 } else { 0x0 };
|
||||
if !clean {
|
||||
// The MMU should be set up now, and memory pages assigned to their
|
||||
// respective processes.
|
||||
|
|
@ -241,13 +243,20 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
let ip_offset = cfg.processes.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
|
||||
let rpt_offset =
|
||||
cfg.runtime_page_tracker.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
|
||||
let xpt_offset = cfg.extra_page_tracker.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
|
||||
#[cfg(not(feature = "atsama5d27"))]
|
||||
let _tt_addr = { cfg.processes[0].satp };
|
||||
#[cfg(feature = "atsama5d27")]
|
||||
let _tt_addr = { cfg.processes[0].ttbr0 };
|
||||
println!(
|
||||
"Jumping to kernel @ {:08x} with map @ {:08x} and stack @ {:08x} (kargs: {:08x}, ip: {:08x}, rpt: {:08x})",
|
||||
cfg.processes[0].entrypoint, _tt_addr, cfg.processes[0].sp, arg_offset, ip_offset, rpt_offset,
|
||||
"Jumping to kernel @ {:08x} with map @ {:08x} and stack @ {:08x} (kargs: {:08x}, ip: {:08x}, rpt: {:08x}, xpt: {:08x})",
|
||||
cfg.processes[0].entrypoint,
|
||||
_tt_addr,
|
||||
cfg.processes[0].sp,
|
||||
arg_offset,
|
||||
ip_offset,
|
||||
rpt_offset,
|
||||
xpt_offset,
|
||||
);
|
||||
|
||||
// save a copy of the computed kernel registers at the bottom of the page reserved
|
||||
|
|
@ -260,7 +269,7 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
// critical parameters like these kernel arguments.
|
||||
#[cfg(not(feature = "atsama5d27"))]
|
||||
unsafe {
|
||||
let backup_args: *mut [u32; 7] = BACKUP_ARGS_ADDR as *mut [u32; 7];
|
||||
let backup_args: *mut [u32; 8] = BACKUP_ARGS_ADDR as *mut [u32; 8];
|
||||
(*backup_args)[0] = arg_offset as u32;
|
||||
(*backup_args)[1] = ip_offset as u32;
|
||||
(*backup_args)[2] = rpt_offset as u32;
|
||||
|
|
@ -268,12 +277,13 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
(*backup_args)[4] = cfg.processes[0].entrypoint as u32;
|
||||
(*backup_args)[5] = cfg.processes[0].sp as u32;
|
||||
(*backup_args)[6] = if cfg.debug { 1 } else { 0 };
|
||||
(*backup_args)[7] = xpt_offset as u32;
|
||||
#[cfg(feature = "debug-print")]
|
||||
{
|
||||
if VDBG {
|
||||
println!("Backup kernel args:");
|
||||
for i in 0..7 {
|
||||
println!("0x{:08x}", (*backup_args)[i]);
|
||||
for &arg in (*backup_args).iter() {
|
||||
println!("0x{:08x}", arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -293,11 +303,11 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
arg_offset,
|
||||
ip_offset,
|
||||
rpt_offset,
|
||||
xpt_offset,
|
||||
cfg.processes[0].satp,
|
||||
cfg.processes[0].entrypoint,
|
||||
cfg.processes[0].sp,
|
||||
cfg.debug,
|
||||
clean,
|
||||
debug_resume,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -310,19 +320,19 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
arg_offset,
|
||||
ip_offset,
|
||||
rpt_offset,
|
||||
cfg.debug,
|
||||
clean,
|
||||
xpt_offset,
|
||||
debug_resume,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
#[cfg(feature = "resume")]
|
||||
unsafe {
|
||||
let backup_args: *mut [u32; 7] = BACKUP_ARGS_ADDR as *mut [u32; 7];
|
||||
let backup_args: *mut [u32; 8] = BACKUP_ARGS_ADDR as *mut [u32; 8];
|
||||
#[cfg(feature = "debug-print")]
|
||||
{
|
||||
println!("Using backed up kernel args:");
|
||||
for i in 0..7 {
|
||||
println!("0x{:08x}", (*backup_args)[i]);
|
||||
for &arg in (*backup_args).iter() {
|
||||
println!("0x{:08x}", arg);
|
||||
}
|
||||
}
|
||||
let satp = ((*backup_args)[3] as usize) & 0x803F_FFFF | (((susres_pid as usize) & 0x1FF) << 22);
|
||||
|
|
@ -344,11 +354,11 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
|
|||
(*backup_args)[0] as usize,
|
||||
(*backup_args)[1] as usize,
|
||||
(*backup_args)[2] as usize,
|
||||
(*backup_args)[7] as usize,
|
||||
satp as usize,
|
||||
(*backup_args)[4] as usize,
|
||||
(*backup_args)[5] as usize,
|
||||
if (*backup_args)[6] == 0 { false } else { true },
|
||||
clean,
|
||||
debug_resume,
|
||||
);
|
||||
}
|
||||
#[cfg(not(feature = "resume"))]
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ pub fn phase_1(cfg: &mut BootConfig) {
|
|||
// needs to be claimed by the susres server before the kernel allocates it.
|
||||
// Lower numbered indices corresponding to higher address pages.
|
||||
println!("Marking pages as in-use");
|
||||
for i in 4..(cfg.init_size / PAGE_SIZE) {
|
||||
for i in ((GUARD_MEMORY_BYTES / PAGE_SIZE) + 1)..(cfg.init_size / PAGE_SIZE) {
|
||||
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - i] = XousAlloc::from(1);
|
||||
}
|
||||
}
|
||||
|
|
@ -111,18 +111,7 @@ pub fn allocate_regions(cfg: &mut BootConfig) {
|
|||
// Number of individual pages in the system
|
||||
let mut rpt_pages = cfg.sram_size / PAGE_SIZE;
|
||||
|
||||
for region in cfg.regions.iter() {
|
||||
println!(
|
||||
"Discovered memory region {:08x} ({:08x} - {:08x}) -- {} bytes",
|
||||
region.name,
|
||||
region.start,
|
||||
region.start + region.length,
|
||||
region.length
|
||||
);
|
||||
let region_length_rounded = (region.length as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||
rpt_pages += region_length_rounded / PAGE_SIZE;
|
||||
}
|
||||
|
||||
// allocate the RPT
|
||||
#[cfg(not(feature = "swap"))]
|
||||
{
|
||||
// Round the tracker to a multiple of the pointer size, so as to keep memory
|
||||
|
|
@ -135,7 +124,7 @@ pub fn allocate_regions(cfg: &mut BootConfig) {
|
|||
rpt_pages = (rpt_pages + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||
}
|
||||
println!(
|
||||
"init_size bef: {:x}, ext pages: {:x}, rpt alloc: {:x}",
|
||||
"RPT ALLOC init_size before: {:x}, ext pages: {:x}, rpt alloc: {:x}",
|
||||
cfg.init_size,
|
||||
cfg.extra_pages,
|
||||
rpt_pages * mem::size_of::<XousAlloc>()
|
||||
|
|
@ -165,6 +154,50 @@ pub fn allocate_regions(cfg: &mut BootConfig) {
|
|||
runtime_page_tracker as usize + rpt_pages * core::mem::size_of::<XousAlloc>()
|
||||
);
|
||||
}
|
||||
|
||||
// allocate the XPT
|
||||
let mut xpt_pages = 0;
|
||||
for region in cfg.regions.iter() {
|
||||
println!(
|
||||
"Discovered memory region {:08x} ({:08x} - {:08x}) -- {} bytes",
|
||||
region.name,
|
||||
region.start,
|
||||
region.start + region.length,
|
||||
region.length
|
||||
);
|
||||
let region_length_rounded = (region.length as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||
xpt_pages += region_length_rounded / PAGE_SIZE;
|
||||
}
|
||||
// Round the tracker to a multiple of the pointer size, so as to keep memory
|
||||
// operations fast.
|
||||
xpt_pages = (xpt_pages + mem::size_of::<usize>() - 1) & !(mem::size_of::<usize>() - 1);
|
||||
println!(
|
||||
"XPT ALLOC init_size bef: {:x}, ext pages: {:x}, rpt alloc: {:x}",
|
||||
cfg.init_size,
|
||||
cfg.extra_pages,
|
||||
xpt_pages * mem::size_of::<XousPid>()
|
||||
);
|
||||
|
||||
cfg.init_size += xpt_pages * mem::size_of::<XousPid>();
|
||||
|
||||
// Clear all memory pages such that they're not owned by anyone
|
||||
let extra_page_tracker = cfg.get_top();
|
||||
println!("rpt value: {:x}", extra_page_tracker as usize);
|
||||
assert!((extra_page_tracker as usize) < (cfg.sram_start as usize) + cfg.sram_size);
|
||||
unsafe {
|
||||
bzero(extra_page_tracker, extra_page_tracker.add(xpt_pages / mem::size_of::<usize>()));
|
||||
}
|
||||
|
||||
cfg.extra_page_tracker =
|
||||
unsafe { slice::from_raw_parts_mut(extra_page_tracker as *mut XousPid, xpt_pages) };
|
||||
#[cfg(feature = "swap")]
|
||||
if SDBG {
|
||||
println!(
|
||||
" -> XPT range: {:x} - {:x}",
|
||||
extra_page_tracker as usize,
|
||||
extra_page_tracker as usize + xpt_pages * core::mem::size_of::<XousPid>()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allocate_processes(cfg: &mut BootConfig) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue