Merge branch 'master' of github.com:betrusted-io/xous-core

This commit is contained in:
Sean Cross 2020-03-13 08:30:55 +08:00
commit 4bb3f6db68
12 changed files with 106 additions and 94 deletions

View file

@ -6,7 +6,7 @@ members = [
"xous-rs", "xous-rs",
"examples/shell", "examples/shell",
] ]
default-members = ["kernel", "loader", "xous-rs", "examples/shell"] default-members = ["xous-rs", "examples/shell"]
[profile.release] [profile.release]
codegen-units = 1 # 1 better optimizations codegen-units = 1 # 1 better optimizations

View file

@ -22,16 +22,21 @@ $ cargo build --target riscv32imac-unknown-none-elf
$ cd kernel $ cd kernel
$ cargo build -p kernel --target riscv32imac-unknown-none-elf $ cargo build -p kernel --target riscv32imac-unknown-none-elf
$ cd .. $ cd ..
$ cd loader
$ cargo build -p loader --target riscv32imac-unknown-none-elf
$ cd ..
``` ```
This will compile all runtime packages for `riscv32imac-unknown-none-elf`. This will compile all runtime packages for `riscv32imac-unknown-none-elf`.
Note that the kernel and loader must currently be built separately, due
to the fact that they have custom linker scripts.
These need to be packaged into a loadable binary. You can generate such These need to be packaged into a loadable binary. You can generate such
a binary by running the following: a binary by running the following:
```sh ```sh
$ cargo run -p tools --bin create-image -- \ $ cargo run -p tools --bin create-image -- \
--csv emulation/csr.csv --csv emulation/csr.csv \
--kernel target/riscv32imac-unknown-none-elf/debug/kernel \ --kernel target/riscv32imac-unknown-none-elf/debug/kernel \
--init target/riscv32imac-unknown-none-elf/debug/shell \ --init target/riscv32imac-unknown-none-elf/debug/shell \
target/args.bin target/args.bin

View file

@ -86,8 +86,8 @@ following values:
The fields `size`, `flags`, and `offset` occupy 64 bits (8 bytes). The The fields `size`, `flags`, and `offset` occupy 64 bits (8 bytes). The
`OFFSET` is a full 32-bit address. The `SIZE` field is in units of `OFFSET` is a full 32-bit address. The `SIZE` field is in units of
words, however as it is only 24 bits, meaning the largest section size bytes, however as it is only 24 bits, meaning the largest section size
is `2^26` bytes. is `2^24` bytes.
The `FLAGS` field contains the following four bits. Any region may be The `FLAGS` field contains the following four bits. Any region may be
marked NOCOPY, however RISC-V does not allow regions to be marked marked NOCOPY, however RISC-V does not allow regions to be marked

View file

@ -1,5 +1,9 @@
# Boot Renode Script (boot.resc) # Boot Renode Script (boot.resc)
# Add this script's path to the global path, so
# we can include files relative to ourselves.
path add $ORIGIN
using sysbus using sysbus
mach create mach create

View file

@ -119,11 +119,11 @@ impl MemoryMapping {
if *l1_entry == 0 { if *l1_entry == 0 {
continue; continue;
} }
let superpage_addr = i as u32 * (1 << 22); let _superpage_addr = i as u32 * (1 << 22);
println!( println!(
" {:4} Superpage for {:08x} @ {:08x} (flags: {:?})", " {:4} Superpage for {:08x} @ {:08x} (flags: {:?})",
i, i,
superpage_addr, _superpage_addr,
(*l1_entry >> 10) << 12, (*l1_entry >> 10) << 12,
MMUFlags::from_bits(l1_entry & 0xff).unwrap() MMUFlags::from_bits(l1_entry & 0xff).unwrap()
); );
@ -141,11 +141,11 @@ impl MemoryMapping {
if *l0_entry & 1 == 0 { if *l0_entry & 1 == 0 {
continue; continue;
} }
let page_addr = j as u32 * (1 << 12); let _page_addr = j as u32 * (1 << 12);
println!( println!(
" {:4} {:08x} -> {:08x} (flags: {:?})", " {:4} {:08x} -> {:08x} (flags: {:?})",
j, j,
superpage_addr + page_addr, _superpage_addr + _page_addr,
(*l0_entry >> 10) << 12, (*l0_entry >> 10) << 12,
MMUFlags::from_bits(l0_entry & 0xff).unwrap() MMUFlags::from_bits(l0_entry & 0xff).unwrap()
); );

View file

@ -6,7 +6,7 @@ use core::slice;
use core::str; use core::str;
pub use crate::arch::mem::{MemoryMapping, PAGE_SIZE}; pub use crate::arch::mem::{MemoryMapping, PAGE_SIZE};
use xous::{MemoryAddress, MemoryFlags, PID}; use xous::{MemoryFlags, PID};
#[derive(Debug)] #[derive(Debug)]
enum ClaimOrRelease { enum ClaimOrRelease {
@ -167,10 +167,10 @@ impl MemoryManager {
// First, we build a &[u8]... // First, we build a &[u8]...
let name_bytes = self.ram_name.to_le_bytes(); let name_bytes = self.ram_name.to_le_bytes();
// ... and then convert that slice into a string slice // ... and then convert that slice into a string slice
let ram_name = str::from_utf8_unchecked(&name_bytes); let _ram_name = str::from_utf8_unchecked(&name_bytes);
println!( println!(
" Region {} ({:08x}) {:08x} - {:08x} {} bytes:", " Region {} ({:08x}) {:08x} - {:08x} {} bytes:",
ram_name, _ram_name,
self.ram_name, self.ram_name,
self.ram_start, self.ram_start,
self.ram_start + self.ram_size, self.ram_start + self.ram_size,
@ -232,6 +232,18 @@ impl MemoryManager {
Err(xous::Error::OutOfMemory) Err(xous::Error::OutOfMemory)
} }
// Find a virtual address in the current process that is big enough
// to fit `size` bytes.
fn find_virtual_address(&mut self, virt_ptr: *mut usize, size: usize) -> Result<usize, xous::Error> {
// If we were supplied a perfectly good address, return that.
if virt_ptr as usize != 0 {
return Ok(virt_ptr as usize);
}
// let mm = SystemServicesHandle::get().current_process();
Err(xous::Error::BadAddress)
}
/// Reserve the given range without actually allocating memory. /// Reserve the given range without actually allocating memory.
/// That way we can overpromise on stack size and heap size without /// That way we can overpromise on stack size and heap size without
/// needing to actually have pages to back it. /// needing to actually have pages to back it.
@ -241,7 +253,10 @@ impl MemoryManager {
size: usize, size: usize,
flags: MemoryFlags, flags: MemoryFlags,
) -> Result<xous::Result, xous::Error> { ) -> Result<xous::Result, xous::Error> {
let virt = virt_ptr as usize;
// If no address was specified, pick the next address that fits
// in the "default" range
let virt = self.find_virtual_address(virt_ptr, size)?;
if virt & 0xfff != 0 { if virt & 0xfff != 0 {
return Err(xous::Error::BadAlignment); return Err(xous::Error::BadAlignment);
@ -253,6 +268,7 @@ impl MemoryManager {
let mut mm = MemoryMapping::current(); let mut mm = MemoryMapping::current();
for virt in (virt..(virt + size)).step_by(PAGE_SIZE) { for virt in (virt..(virt + size)).step_by(PAGE_SIZE) {
// FIXME: Un-reserve addresses if we encounter an error here
mm.reserve_address(self, virt, flags)?; mm.reserve_address(self, virt, flags)?;
} }
Ok(xous::Result::MemoryRange(virt_ptr as *mut u8, size)) Ok(xous::Result::MemoryRange(virt_ptr as *mut u8, size))
@ -270,48 +286,47 @@ impl MemoryManager {
virt_ptr: *mut usize, virt_ptr: *mut usize,
size: usize, size: usize,
flags: MemoryFlags, flags: MemoryFlags,
) -> Result<MemoryAddress, xous::Error> { ) -> Result<xous::Result, xous::Error> {
let ss = SystemServicesHandle::get(); let ss = SystemServicesHandle::get();
let pid = ss.current_pid(); let pid = ss.current_pid();
let phys = phys_ptr as usize; let phys = phys_ptr as usize;
let virt = virt_ptr as usize; let virt = self.find_virtual_address(virt_ptr, size)?;
if phys == 0 || virt == 0 { // If no physical address is specified, give the user the next available pages
println!("Attempted to map a range without specifying phys or virt"); if phys == 0 {
return Err(xous::Error::BadAddress); return self.reserve_range(virt as *mut usize, size, flags);
} }
let mut error = None; // 1. Attempt to claim all physical pages in the range
for phys in (phys..(phys + size)).step_by(PAGE_SIZE) { for claim_phys in (phys..(phys + size)).step_by(PAGE_SIZE) {
if let Err(err) = self.claim_page(phys as *mut usize, pid) { if let Err(err) = self.claim_page(claim_phys as *mut usize, pid) {
error = Some(err); // If we were unable to claim one or more pages, release everything and return
break; for rel_phys in (phys..claim_phys).step_by(PAGE_SIZE) {
} self.release_page(rel_phys as *mut usize, pid).ok();
} }
if let Some(err) = error { return Err(err);
for phys in (phys..(phys + size)).step_by(PAGE_SIZE) {
self.release_page(phys as *mut usize, pid).ok();
}
return Err(err);
}
for phys in (phys..(phys + size)).step_by(PAGE_SIZE) {
if let Err(e) =
crate::arch::mem::map_page_inner(self, pid, phys as usize, virt as usize, flags)
{
error = Some(e);
break;
} }
} }
if let Some(err) = error { // Actually perform the map. At this stage, every physical page should be owned by us.
for phys in (phys..(phys + size)).step_by(PAGE_SIZE) { for offset in (0..size).step_by(PAGE_SIZE) {
self.release_page(phys as *mut usize, pid).ok(); if let Err(e) = crate::arch::mem::map_page_inner(
self,
pid,
offset + phys as usize,
offset + virt as usize,
flags,
) {
for unmap_offset in (0..offset).step_by(PAGE_SIZE) {
crate::arch::mem::unmap_page_inner(self, unmap_offset + virt).ok();
self.release_page((unmap_offset + phys) as *mut usize, pid)
.ok();
}
return Err(e);
} }
return Err(err);
} }
Ok(MemoryAddress::new(virt).unwrap()) Ok(xous::Result::MemoryRange(virt as *mut u8, size))
} }
// /// Map a range of physical addresses into the current memory space. // /// Map a range of physical addresses into the current memory space.

View file

@ -59,6 +59,9 @@ pub struct Process {
/// Default virtual address when MapMemory is called with no `virt` /// Default virtual address when MapMemory is called with no `virt`
pub mem_default_base: usize, pub mem_default_base: usize,
/// The last address allocated from
pub mem_default_last: usize,
/// Address where messages are passed into /// Address where messages are passed into
pub mem_message_base: usize, pub mem_message_base: usize,
@ -110,6 +113,7 @@ static mut SYSTEM_SERVICES: SystemServices = SystemServices {
ppid: 0, ppid: 0,
mapping: arch::mem::DEFAULT_MEMORY_MAPPING, mapping: arch::mem::DEFAULT_MEMORY_MAPPING,
mem_default_base: arch::mem::DEFAULT_BASE, mem_default_base: arch::mem::DEFAULT_BASE,
mem_default_last: arch::mem::DEFAULT_BASE,
mem_message_base: arch::mem::DEFAULT_MESSAGE_BASE, mem_message_base: arch::mem::DEFAULT_MESSAGE_BASE,
mem_heap_base: arch::mem::DEFAULT_HEAP_BASE, mem_heap_base: arch::mem::DEFAULT_HEAP_BASE,
mem_heap_size: 0, mem_heap_size: 0,

View file

@ -155,9 +155,7 @@ pub struct MiniElfSection {
impl MiniElfSection { impl MiniElfSection {
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
// Strip off the top four bits, which contain the flags. // Strip off the top four bits, which contain the flags.
// Also shift everything up by two bits, since it's in let len = self.size_and_flags & !0xff00_0000;
// units of 32-bit words.
let len = (self.size_and_flags << 2) & !0xf000_0000;
len as usize len as usize
} }
@ -577,13 +575,13 @@ fn copy_processes(cfg: &mut BootConfig) {
if tag.name == make_type!("IniE") { if tag.name == make_type!("IniE") {
let mut page_addr: usize = 0; let mut page_addr: usize = 0;
let mut previous_addr: usize = 0; let mut previous_addr: usize = 0;
let mut top = 0 as *mut usize; let mut top = 0 as *mut u8;
let inie = MiniElf::new(&tag); let inie = MiniElf::new(&tag);
let mut src_addr = unsafe { let mut src_addr = unsafe {
cfg.base_addr cfg.base_addr
.add(inie.load_offset as usize / mem::size_of::<usize>()) .add(inie.load_offset as usize / mem::size_of::<usize>())
}; } as *const u8;
// Example: Page starts at 0xf0c0 and is 8192 bytes long. // Example: Page starts at 0xf0c0 and is 8192 bytes long.
// 1. Copy 3094 bytes to page 1 // 1. Copy 3094 bytes to page 1
@ -622,28 +620,18 @@ fn copy_processes(cfg: &mut BootConfig) {
); );
unsafe { unsafe {
bzero( bzero(
top.add( top.add(previous_addr as usize & (PAGE_SIZE - 1)),
(previous_addr as usize & (PAGE_SIZE - 1)) top.add(PAGE_SIZE as usize),
/ mem::size_of::<usize>(),
),
top.add(PAGE_SIZE as usize / mem::size_of::<usize>()),
) )
}; };
} }
// Allocate a new page. // Allocate a new page.
cfg.extra_pages += 1; cfg.extra_pages += 1;
top = cfg.get_top(); top = cfg.get_top() as *mut u8;
// Zero out the page, if necessary. // Zero out the page, if necessary.
unsafe { unsafe { bzero(top, top.add(section.virt as usize & (PAGE_SIZE - 1))) };
bzero(
top,
top.add(
(section.virt as usize & (PAGE_SIZE - 1)) / mem::size_of::<usize>(),
),
)
};
} }
// Part 1: Copy the first chunk over. // Part 1: Copy the first chunk over.
@ -656,29 +644,23 @@ fn copy_processes(cfg: &mut BootConfig) {
"First chunk is {} bytes, copying from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})", "First chunk is {} bytes, copying from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})",
first_chunk_size, first_chunk_size,
src_addr as usize, src_addr as usize,
unsafe { src_addr.add(first_chunk_size / 4) as usize }, unsafe { src_addr.add(first_chunk_size) as usize },
unsafe { top.add(first_chunk_offset / mem::size_of::<usize>()) as usize }, unsafe { top.add(first_chunk_offset) as usize },
unsafe { top.add((first_chunk_size + first_chunk_offset) / mem::size_of::<usize>()) unsafe { top.add(first_chunk_size + first_chunk_offset)
as usize }, as usize },
this_page + first_chunk_offset, this_page + first_chunk_offset,
); );
// Perform the copy, if NOCOPY is not set // Perform the copy, if NOCOPY is not set
if !section.no_copy() { if !section.no_copy() {
unsafe { unsafe {
memcpy( memcpy(top.add(first_chunk_offset), src_addr, first_chunk_size);
top.add(first_chunk_offset / mem::size_of::<usize>()), src_addr = src_addr.add(first_chunk_size);
src_addr,
first_chunk_size,
);
src_addr = src_addr.add(first_chunk_size / mem::size_of::<usize>());
} }
} else { } else {
unsafe { unsafe {
bzero( bzero(
top.add(first_chunk_offset / mem::size_of::<usize>()), top.add(first_chunk_offset),
top.add( top.add(first_chunk_offset + first_chunk_size),
(first_chunk_offset + first_chunk_size) / mem::size_of::<usize>(),
),
); );
} }
} }
@ -687,7 +669,7 @@ fn copy_processes(cfg: &mut BootConfig) {
// Part 2: Copy any full pages. // Part 2: Copy any full pages.
while bytes_to_copy > PAGE_SIZE { while bytes_to_copy > PAGE_SIZE {
cfg.extra_pages += 1; cfg.extra_pages += 1;
top = cfg.get_top(); top = cfg.get_top() as *mut u8;
// println!( // println!(
// "Copying next page from {:08x} {:08x}", // "Copying next page from {:08x} {:08x}",
// src_addr as usize, top as usize // src_addr as usize, top as usize
@ -695,10 +677,10 @@ fn copy_processes(cfg: &mut BootConfig) {
if !section.no_copy() { if !section.no_copy() {
unsafe { unsafe {
memcpy(top, src_addr, PAGE_SIZE); memcpy(top, src_addr, PAGE_SIZE);
src_addr = src_addr.add(PAGE_SIZE / mem::size_of::<usize>()); src_addr = src_addr.add(PAGE_SIZE);
} }
} else { } else {
unsafe { bzero(top, top.add(PAGE_SIZE / 4)) }; unsafe { bzero(top, top.add(PAGE_SIZE)) };
} }
bytes_to_copy -= PAGE_SIZE; bytes_to_copy -= PAGE_SIZE;
} }
@ -707,14 +689,14 @@ fn copy_processes(cfg: &mut BootConfig) {
if bytes_to_copy > 0 { if bytes_to_copy > 0 {
println!("Copying final section -- {} bytes", bytes_to_copy); println!("Copying final section -- {} bytes", bytes_to_copy);
cfg.extra_pages += 1; cfg.extra_pages += 1;
top = cfg.get_top(); top = cfg.get_top() as *mut u8;
if !section.no_copy() { if !section.no_copy() {
unsafe { unsafe {
memcpy(top, src_addr, bytes_to_copy); memcpy(top, src_addr, bytes_to_copy);
src_addr = src_addr.add(bytes_to_copy / mem::size_of::<usize>()); src_addr = src_addr.add(bytes_to_copy);
} }
} else { } else {
unsafe { bzero(top, top.add(bytes_to_copy / 4)) }; unsafe { bzero(top, top.add(bytes_to_copy)) };
} }
} }
@ -727,8 +709,8 @@ fn copy_processes(cfg: &mut BootConfig) {
// Zero-out the trailing bytes // Zero-out the trailing bytes
unsafe { unsafe {
bzero( bzero(
top.add((previous_addr as usize & (PAGE_SIZE - 1)) / mem::size_of::<usize>()), top.add(previous_addr as usize & (PAGE_SIZE - 1)),
top.add(PAGE_SIZE as usize / mem::size_of::<usize>()), top.add(PAGE_SIZE as usize),
) )
}; };
} else if tag.name == make_type!("XKrn") { } else if tag.name == make_type!("XKrn") {

View file

@ -191,6 +191,11 @@ fn read_word(satp: usize, virt: usize) -> Result<u32, &'static str> {
Ok(unsafe { (page_base as *mut u32).read() }) Ok(unsafe { (page_base as *mut u32).read() })
} }
fn read_byte(satp: usize, virt: usize) -> Result<u8, &'static str> {
let word = read_word(satp, virt & 0xfffffffc)?;
Ok(word.to_le_bytes()[virt & 3])
}
fn verify_kernel(cfg: &BootConfig, pid: usize, arg: &crate::args::KernelArgument) { fn verify_kernel(cfg: &BootConfig, pid: usize, arg: &crate::args::KernelArgument) {
let prog = unsafe { &*(arg.data.as_ptr() as *const crate::ProgramDescription) }; let prog = unsafe { &*(arg.data.as_ptr() as *const crate::ProgramDescription) };
let program_offset = prog.load_offset as usize; let program_offset = prog.load_offset as usize;
@ -273,21 +278,21 @@ fn verify_program(cfg: &BootConfig, pid: usize, arg: &crate::args::KernelArgumen
let mut program_offset = elf.load_offset as usize; let mut program_offset = elf.load_offset as usize;
for section in elf.sections.iter() { for section in elf.sections.iter() {
for addr in (section.virt..(section.virt + section.len() as u32)).step_by(4) { for addr in section.virt..(section.virt + section.len() as u32) {
let addr = addr as usize; let addr = addr as usize;
let word = read_word(cfg.processes[pid].satp as usize, addr).unwrap(); let word = read_byte(cfg.processes[pid].satp as usize, addr).unwrap();
if section.no_copy() { if section.no_copy() {
assert!(word == 0, "bss is {:08x}, not 0 @ {:08x}", word, addr); assert!(word == 0, "bss is {:08x}, not 0 @ {:08x}", word, addr);
} else { } else {
let check_word = unsafe { let check_word = unsafe {
(cfg.base_addr as *mut u32) (cfg.base_addr as *mut u8)
.add(program_offset / 4) .add(program_offset)
.read() .read()
}; };
program_offset += 4; program_offset += 1;
assert!( assert!(
word == check_word, word == check_word,
"program doesn't match @ {:08x} (expected: {:08x} found: {:08x})", "program doesn't match @ {:08x} (expected: {:02x} found: {:02x})",
addr, addr,
check_word, check_word,
word, word,

Binary file not shown.

View file

@ -308,7 +308,6 @@ pub fn read_minielf<P: AsRef<Path>>(filename: P) -> Result<MiniElf, ElfReadError
debug!(" size: {:?}", s.size()); debug!(" size: {:?}", s.size());
debug!(" link: {:?}", s.link()); debug!(" link: {:?}", s.link());
let size = s.size(); let size = s.size();
let padding = (4 - (size & 3)) & 3;
if s.flags() & SHF_ALLOC == 0 { if s.flags() & SHF_ALLOC == 0 {
debug!("section has no allocations -- skipping"); debug!("section has no allocations -- skipping");
@ -326,12 +325,11 @@ pub fn read_minielf<P: AsRef<Path>>(filename: P) -> Result<MiniElf, ElfReadError
debug!("Adding {} to the file", name); debug!("Adding {} to the file", name);
debug!( debug!(
"{} offset: {:08x} program_offset: {:08x} bytes: {} padding: {} seek: {}", "{} offset: {:08x} program_offset: {:08x} bytes: {} seek: {}",
name, name,
s.offset(), s.offset(),
program_offset, program_offset,
s.raw_data(&elf).len(), s.raw_data(&elf).len(),
padding,
program_offset program_offset
); );
let section_data = s.raw_data(&elf); let section_data = s.raw_data(&elf);
@ -349,11 +347,10 @@ pub fn read_minielf<P: AsRef<Path>>(filename: P) -> Result<MiniElf, ElfReadError
.write(section_data) .write(section_data)
.or_else(|x| Err(ElfReadError::WriteSectionError(x)))?; .or_else(|x| Err(ElfReadError::WriteSectionError(x)))?;
program_offset += section_data.len() as u64; program_offset += section_data.len() as u64;
program_offset += padding as u64;
} }
sections.push(MiniElfSection { sections.push(MiniElfSection {
virt: s.address() as u32, virt: s.address() as u32,
size: (size + padding) as u32, size: size as u32,
flags: flags, flags: flags,
name: name.to_string(), name: name.to_string(),
}); });

View file

@ -73,7 +73,7 @@ impl XousArgument for IniE {
written += output.write(&self.entrypoint.to_le_bytes())?; written += output.write(&self.entrypoint.to_le_bytes())?;
for section in &self.sections { for section in &self.sections {
written += output.write(&section.virt.to_le_bytes())?; written += output.write(&section.virt.to_le_bytes())?;
let mut word2 = (section.size >> 2).to_le_bytes(); let mut word2 = section.size.to_le_bytes();
word2[3] = section.flags.bits(); word2[3] = section.flags.bits();
written += output.write(&word2)?; written += output.write(&word2)?;
} }