Skip to content

Commit fa61fc2

Browse files
committed
Update
1 parent fd0d9bf commit fa61fc2

5 files changed

Lines changed: 75 additions & 48 deletions

File tree

kernel/src/boot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ pub struct BootInfo {
2525
pub fn boot(bootinfo: BootInfo) -> ! {
2626
crate::memory::init(&bootinfo);
2727
crate::cpuvar::init(0);
28-
crate::loader::init(&bootinfo);
28+
crate::server::init(&bootinfo);
2929
crate::scheduler::return_to_user();
3030
}

kernel/src/loader.rs

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,22 @@ use ftl_utils::alignment::align_up;
1414

1515
use crate::arch;
1616
use crate::arch::MIN_PAGE_SIZE;
17-
use crate::boot::BootInfo;
18-
use crate::initfs;
1917
use crate::memory::PAGE_ALLOCATOR;
2018
use crate::memory::PageType;
2119

22-
const START_INFO: &StartInfo = &StartInfo {
23-
sys_print: |bytes| {
24-
println!("{}", core::str::from_utf8(bytes).unwrap());
25-
},
26-
sys_panic: || {
27-
error!("server panicked");
28-
crate::scheduler::return_to_user();
29-
},
30-
};
31-
32-
fn load_elf(elf_file: &[u8]) {
33-
let elf = match Elf::parse(elf_file, ftl_elf::ET_DYN) {
34-
Ok(elf) => elf,
35-
Err(e) => {
36-
panic!("failed to parse ELF file: {:?}", e);
37-
}
38-
};
20+
pub type EntryFn = extern "Rust" fn(start_info: *const StartInfo);
21+
22+
#[derive(Debug)]
23+
pub enum Error {
24+
ParseElf,
25+
OutOfMemory,
26+
BadRelocType,
27+
BadRelocOffset,
28+
BadRelocSize,
29+
}
30+
31+
pub fn load_elf(elf_file: &[u8]) -> Result<(*const u8, usize, EntryFn), Error> {
32+
let elf = Elf::parse(elf_file, ftl_elf::ET_DYN).map_err(|_| Error::ParseElf)?;
3933

4034
// Find the end of the image to calculate the size of the memory it needs.
4135
let mut image_size = 0;
@@ -46,12 +40,9 @@ fn load_elf(elf_file: &[u8]) {
4640
}
4741

4842
let image_size = align_up(image_size as usize, MIN_PAGE_SIZE);
49-
let image_paddr = match PAGE_ALLOCATOR.alloc(image_size, PageType::Zeroed) {
50-
Some(paddr) => paddr,
51-
None => {
52-
panic!("out of memory: {} bytes", image_size);
53-
}
54-
};
43+
let image_paddr = PAGE_ALLOCATOR
44+
.alloc(image_size, PageType::Zeroed)
45+
.ok_or(Error::OutOfMemory)?;
5546

5647
let image_ptr: *mut u8 = arch::paddr2vaddr(image_paddr).as_mut_ptr();
5748
let image = unsafe { slice::from_raw_parts_mut(image_ptr, image_size) };
@@ -107,7 +98,7 @@ fn load_elf(elf_file: &[u8]) {
10798
// Apply relocations.
10899
if rela_addr != 0 && rela_size > 0 {
109100
if rela_size % size_of::<Rela>() != 0 {
110-
panic!("invalid relocation table size: {} bytes", rela_size);
101+
return Err(Error::BadRelocSize);
111102
}
112103

113104
let relocations = unsafe {
@@ -120,12 +111,12 @@ fn load_elf(elf_file: &[u8]) {
120111
for rela in relocations {
121112
let target_off = rela.r_offset as usize;
122113
if rela.r_sym() != 0 || rela.r_type() != R_X86_64_RELATIVE {
123-
panic!("unexpected relocation type: {}", rela.r_type());
114+
return Err(Error::BadRelocType);
124115
}
125116

126117
let target_end = match target_off.checked_add(size_of::<u64>()) {
127118
Some(end) if end <= image.len() => end,
128-
_ => panic!("invalid relocation target offset: {}", target_off),
119+
_ => return Err(Error::BadRelocOffset),
129120
};
130121

131122
let base = image.as_ptr() as u64;
@@ -139,19 +130,5 @@ fn load_elf(elf_file: &[u8]) {
139130
core::mem::transmute::<*const u8, extern "Rust" fn(start_info: *const StartInfo)>(entry_ptr)
140131
};
141132

142-
trace!("Calling entry point: {:p}", entry_fn);
143-
entry_fn(START_INFO);
144-
trace!("Entry point returned");
145-
}
146-
147-
pub fn init(bootinfo: &BootInfo) {
148-
for module in &bootinfo.modules {
149-
let initfs = initfs::InitFsLoader::new(module);
150-
for file in initfs {
151-
if file.name.starts_with(b"servers/") && file.name.ends_with(b".elf") {
152-
trace!("loading {}...", core::str::from_utf8(file.name).unwrap());
153-
load_elf(file.data);
154-
}
155-
}
156-
}
133+
Ok((image.as_ptr(), image_size, entry_fn))
157134
}

kernel/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod loader;
2121
mod memory;
2222
mod panic;
2323
mod scheduler;
24+
mod server;
2425
mod shared_ref;
2526
mod spinlock;
2627
mod syscall;

kernel/src/server.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use alloc::vec::Vec;
2+
3+
use ftl_server::start::StartInfo;
4+
5+
use crate::boot::BootInfo;
6+
use crate::initfs;
7+
use crate::spinlock::SpinLock;
8+
9+
const START_INFO: &StartInfo = &StartInfo {
10+
sys_print: |bytes| {
11+
println!("{}", core::str::from_utf8(bytes).unwrap());
12+
},
13+
sys_panic: || {
14+
error!("server panicked");
15+
crate::scheduler::return_to_user();
16+
},
17+
};
18+
19+
static SERVERS: SpinLock<Vec<Server>> = SpinLock::new(Vec::new());
20+
21+
pub struct Server {
22+
image: *const u8,
23+
image_size: usize,
24+
}
25+
26+
impl Server {
27+
fn load(elf_file: &[u8]) -> Result<Self, crate::loader::Error> {
28+
let (image, image_size, entry_fn) = crate::loader::load_elf(elf_file)?;
29+
entry_fn(START_INFO);
30+
Ok(Self { image, image_size })
31+
}
32+
}
33+
34+
unsafe impl Send for Server {}
35+
36+
pub fn init(bootinfo: &BootInfo) {
37+
for module in &bootinfo.modules {
38+
let initfs = initfs::InitFsLoader::new(module);
39+
for file in initfs {
40+
if file.name.starts_with(b"servers/") && file.name.ends_with(b".elf") {
41+
trace!("loading {}...", core::str::from_utf8(file.name).unwrap());
42+
match Server::load(file.data) {
43+
Ok(server) => {
44+
SERVERS.lock().push(server);
45+
}
46+
Err(e) => {
47+
error!("failed to load server: {:?}", e);
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}

servers/lx/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![cfg_attr(target_os = "none", no_std)]
22

3-
use ftl_server::info;
4-
53
struct Server {}
64

75
impl Server {
@@ -18,7 +16,5 @@ impl ftl_server::Server for Server {
1816

1917
#[unsafe(no_mangle)]
2018
pub fn init() {
21-
info!("Hello from init!");
22-
panic!("test panic");
2319
ftl_server::register(Server::new());
2420
}

0 commit comments

Comments
 (0)