forked from syswonder/ruxos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bugs of SYS_execve that uses SYS_mmap not correctly, and add exam…
…ple app for ELF loader.
- Loading branch information
thesayol
committed
Apr 17, 2024
1 parent
b52dad1
commit f0dc625
Showing
11 changed files
with
209 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,56 @@ | ||
use core::{mem::size_of, ptr::null_mut}; | ||
use alloc::{vec, vec::Vec}; | ||
|
||
use crate::*; | ||
use ruxconfig::TASK_STACK_SIZE; | ||
|
||
const STACK_SIZE: usize = TASK_STACK_SIZE; | ||
|
||
#[derive(Debug)] | ||
pub struct Stack { | ||
sp: usize, | ||
start: usize, | ||
end: usize, | ||
/// stack | ||
data: Vec<u8>, | ||
/// index of top byte of stack | ||
top: usize, | ||
} | ||
|
||
impl Stack { | ||
// alloc a stack | ||
/// alloc a stack | ||
pub fn new() -> Self { | ||
let size = 0xa00000; // 10M | ||
let p = sys_mmap(null_mut(), size, 0, 0, 0, 0); | ||
|
||
let start = p as usize; | ||
let sp = start + size; | ||
let end = sp; | ||
Self { | ||
data: vec![0u8; STACK_SIZE], | ||
top: STACK_SIZE, | ||
} | ||
} | ||
|
||
Self { sp, start, end } | ||
/// panic if overflow | ||
fn panic_if_of(&self) { | ||
assert!(self.top <= self.data.len(), "sys_execve: stack overflow."); | ||
} | ||
|
||
/// move sp to align | ||
pub fn align(&mut self, align: usize) -> usize { | ||
self.sp -= self.sp % align; | ||
self.sp | ||
self.top -= self.top % align; | ||
self.top | ||
} | ||
|
||
/// addr of top of stack | ||
pub fn sp(&self) -> usize { | ||
self.data.as_ptr() as usize + self.top | ||
} | ||
|
||
pub fn push<T: Copy>(&mut self, thing: alloc::vec::Vec<T>, align: usize) -> usize { | ||
let size = thing.len() * size_of::<T>(); | ||
self.sp -= size; | ||
self.sp = self.align(align); // align 16B | ||
/// push data to stack and return the addr of sp | ||
pub fn push<T>(&mut self, data: &[T], align: usize) -> usize { | ||
// move sp to right place | ||
self.top -= core::mem::size_of_val(data); | ||
self.top = self.align(align); | ||
|
||
if self.sp < self.start { | ||
panic!("stack overflow"); | ||
} | ||
self.panic_if_of(); | ||
|
||
let mut pt = self.sp as *mut T; | ||
// write data into stack | ||
let sp = self.sp() as *mut T; | ||
unsafe { | ||
for t in thing { | ||
*pt = t; | ||
pt = pt.add(1); | ||
} | ||
sp.copy_from_nonoverlapping(data.as_ptr(), data.len()); | ||
} | ||
|
||
self.sp | ||
} | ||
} | ||
|
||
impl Drop for Stack { | ||
fn drop(&mut self) { | ||
sys_munmap(self.start as *mut _, self.end - self.start); | ||
sp as usize | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
ruxgo_bld | ||
compile_commands.json | ||
.cache | ||
/rootfs/bin/* | ||
/rootfs/lib/* | ||
/rootfs/dev | ||
/rootfs/etc | ||
/rootfs/proc | ||
/rootfs/sys | ||
/rootfs/tmp |
Oops, something went wrong.