support TLS for musl. partial impl sys_exit_group.

master
WangRunji 6 years ago
parent f76374000e
commit c7f9921e06

@ -6,7 +6,7 @@ use core::ptr::null;
pub struct ProcInitInfo {
pub args: Vec<String>,
pub envs: BTreeMap<String, String>,
// pub auxv: Vec<String>,
pub auxv: BTreeMap<u8, usize>,
}
impl ProcInitInfo {
@ -27,8 +27,11 @@ impl ProcInitInfo {
writer.push_str(arg.as_str());
writer.sp
}).collect();
// TODO: auxiliary vector entries
writer.push_slice(&[null::<u8>()]);
// auxiliary vector entries
writer.push_slice(&[null::<u8>(), null::<u8>()]);
for (&type_, &value) in self.auxv.iter() {
writer.push_slice(&[type_ as usize, value]);
}
// envionment pointers
writer.push_slice(&[null::<u8>()]);
writer.push_slice(envs.as_slice());
@ -59,4 +62,6 @@ impl StackWriter {
}
}
pub const AT_PHDR: u8 = 3;
pub const AT_PHENT: u8 = 4;
pub const AT_PHNUM: u8 = 5;

@ -9,7 +9,7 @@ use crate::arch::interrupt::{Context, TrapFrame};
use crate::memory::{ByFrame, GlobalFrameAlloc, KernelStack, MemoryAttr, MemorySet};
use crate::fs::{FileHandle, OpenOptions};
use super::abi::ProcInitInfo;
use super::abi::{self, ProcInitInfo};
// TODO: avoid pub
pub struct Thread {
@ -102,6 +102,17 @@ impl Thread {
let init_info = ProcInitInfo {
args: args.map(|s| String::from(s)).collect(),
envs: BTreeMap::new(),
auxv: {
let mut map = BTreeMap::new();
let phdr = elf.program_iter()
.find(|ph| ph.get_type() == Ok(Type::Phdr))
.expect("PHDR section not found")
.virtual_addr();
map.insert(abi::AT_PHDR, phdr as usize);
map.insert(abi::AT_PHENT, elf.header.pt2.ph_entry_size() as usize);
map.insert(abi::AT_PHNUM, elf.header.pt2.ph_count() as usize);
map
},
};
unsafe {
memory_set.with(|| { ustack_top = init_info.push_at(ustack_top) });

@ -184,6 +184,26 @@ fn get_file<'a>(proc: &'a mut MutexGuard<'static, Process>, fd: usize) -> Result
proc.files.get_mut(&fd).ok_or(SysError::Inval)
}
impl From<FsError> for SysError {
fn from(error: FsError) -> Self {
match error {
FsError::NotSupported => SysError::Unimp,
FsError::NotFile => SysError::Isdir,
FsError::IsDir => SysError::Isdir,
FsError::NotDir => SysError::Notdir,
FsError::EntryNotFound => SysError::Noent,
FsError::EntryExist => SysError::Exists,
FsError::NotSameFs => SysError::Xdev,
FsError::InvalidParam => SysError::Inval,
FsError::NoDeviceSpace => SysError::Nomem,
FsError::DirRemoved => SysError::Noent,
FsError::DirNotEmpty => SysError::Notempty,
FsError::WrongFs => SysError::Inval,
FsError::DeviceError => SysError::Io,
}
}
}
bitflags! {
struct OpenFlags: usize {
/// read only

@ -5,8 +5,11 @@ use crate::memory::GlobalFrameAlloc;
use super::*;
pub fn sys_mmap(mut addr: usize, len: usize, prot: MmapProt, flags: MmapFlags, fd: i32, offset: usize) -> SysResult {
pub fn sys_mmap(mut addr: usize, len: usize, prot: usize, flags: usize, fd: i32, offset: usize) -> SysResult {
let prot = MmapProt::from_bits_truncate(prot);
let flags = MmapFlags::from_bits_truncate(flags);
info!("mmap addr={:#x}, size={:#x}, prot={:?}, flags={:?}, fd={}, offset={:#x}", addr, len, prot, flags, fd, offset);
let mut proc = process();
addr = proc.memory_set.find_free_area(addr, len);

@ -37,7 +37,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
005 => sys_fstat(args[0], args[1] as *mut Stat),
// 007 => sys_poll(),
008 => sys_lseek(args[0], args[1] as i64, args[2] as u8),
009 => sys_mmap(args[0], args[1], MmapProt::from_bits_truncate(args[2]), MmapFlags::from_bits_truncate(args[3]), args[4] as i32, args[5]),
009 => sys_mmap(args[0], args[1], args[2], args[3], args[4] as i32, args[5]),
011 => sys_munmap(args[0], args[1]),
019 => sys_readv(args[0], args[1] as *const IoVec, args[2]),
020 => sys_writev(args[0], args[1] as *const IoVec, args[2]),
@ -109,6 +109,10 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
warn!("sys_set_tid_address is unimplemented");
Ok(thread::current().id() as isize)
}
231 => {
warn!("sys_exit_group is unimplemented");
sys_exit(args[0] as isize);
}
_ => {
error!("unknown syscall id: {:#x?}, args: {:x?}", id, args);
crate::trap::error(tf);
@ -145,23 +149,3 @@ pub enum SysError {
#[allow(dead_code)]
Unspcified = 1,// A really really unknown error.
}
impl From<FsError> for SysError {
fn from(error: FsError) -> Self {
match error {
FsError::NotSupported => SysError::Unimp,
FsError::NotFile => SysError::Isdir,
FsError::IsDir => SysError::Isdir,
FsError::NotDir => SysError::Notdir,
FsError::EntryNotFound => SysError::Noent,
FsError::EntryExist => SysError::Exists,
FsError::NotSameFs => SysError::Xdev,
FsError::InvalidParam => SysError::Inval,
FsError::NoDeviceSpace => SysError::Nomem,
FsError::DirRemoved => SysError::Noent,
FsError::DirNotEmpty => SysError::Notempty,
FsError::WrongFs => SysError::Inval,
FsError::DeviceError => SysError::Io,
}
}
}

@ -108,7 +108,7 @@ pub fn sys_getpid() -> SysResult {
}
/// Exit the current process
pub fn sys_exit(exit_code: isize) -> SysResult {
pub fn sys_exit(exit_code: isize) -> ! {
let pid = thread::current().id();
info!("exit: {}, code: {}", pid, exit_code);
processor().manager().exit(pid, exit_code as usize);

Loading…
Cancel
Save