From 2d77b89476a34bff605f4b1ca578e7361d748ed3 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Thu, 7 Mar 2019 22:06:26 +0800 Subject: [PATCH] Infer phdr from elf loading address, skip the first page in sys_mmap and update user to musl-enabled biscuit programs --- kernel/src/process/structs.rs | 6 ++++++ kernel/src/syscall/mem.rs | 7 +++++++ user | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/kernel/src/process/structs.rs b/kernel/src/process/structs.rs index 78d3aa4..59ccc9e 100644 --- a/kernel/src/process/structs.rs +++ b/kernel/src/process/structs.rs @@ -141,7 +141,13 @@ impl Thread { let mut map = BTreeMap::new(); if let Some(phdr) = elf.program_iter() .find(|ph| ph.get_type() == Ok(Type::Phdr)) { + // if phdr exists in program header, use it map.insert(abi::AT_PHDR, phdr.virtual_addr() as usize); + } else if let Some(elf_addr) = elf.program_iter().find(|ph| ph.get_type() == Ok(Type::Load) && ph.offset() == 0) { + // otherwise, check if elf is loaded from the beginning, then phdr can be inferred. + map.insert(abi::AT_PHDR, elf_addr.virtual_addr() as usize + elf.header.pt2.ph_offset() as usize); + } else { + debug!("new_user: no phdr found, tls might not work"); } 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); diff --git a/kernel/src/syscall/mem.rs b/kernel/src/syscall/mem.rs index 2f05578..6a3bb59 100644 --- a/kernel/src/syscall/mem.rs +++ b/kernel/src/syscall/mem.rs @@ -1,5 +1,6 @@ use rcore_memory::memory_set::handler::Delay; use rcore_memory::memory_set::MemoryAttr; +use rcore_memory::PAGE_SIZE; use crate::memory::GlobalFrameAlloc; @@ -11,6 +12,12 @@ pub fn sys_mmap(mut addr: usize, len: usize, prot: usize, flags: usize, fd: i32, info!("mmap addr={:#x}, size={:#x}, prot={:?}, flags={:?}, fd={}, offset={:#x}", addr, len, prot, flags, fd, offset); let mut proc = process(); + if addr == 0 { + // although NULL can be a valid address + // but in C, NULL is regarded as allocation failure + // so just skip it + addr = PAGE_SIZE; + } addr = proc.memory_set.find_free_area(addr, len); if flags.contains(MmapFlags::ANONYMOUS) { diff --git a/user b/user index fb9a1d5..93bac1c 160000 --- a/user +++ b/user @@ -1 +1 @@ -Subproject commit fb9a1d5f7014b3be4f4a234bcaecd069335a216c +Subproject commit 93bac1ce10eae9aa541b64e955316b325f8dc9cb