From 311cf104f9feb1d3ebc88e40e6027d1fd9241123 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 29 Mar 2019 13:14:08 +0800 Subject: [PATCH] Many minor fixes to allow gcc to run inside rCore. Add some syscalls, fix SEEK_* and enlarge the heap --- kernel/Cargo.lock | 4 ++-- kernel/Cargo.toml | 4 ++-- kernel/Makefile | 3 ++- kernel/src/arch/x86_64/memory.rs | 35 +++++++++++++++++++++++++++++++- kernel/src/arch/x86_64/paging.rs | 5 ++++- kernel/src/syscall/fs.rs | 6 +++--- kernel/src/syscall/misc.rs | 29 ++++++++++++++++++++++++++ kernel/src/syscall/mod.rs | 9 ++++++++ tools/addr2line.py | 3 ++- 9 files changed, 87 insertions(+), 11 deletions(-) diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock index acfcfd6..1d22c8d 100644 --- a/kernel/Cargo.lock +++ b/kernel/Cargo.lock @@ -328,12 +328,12 @@ dependencies = [ [[package]] name = "rcore-fs" version = "0.1.0" -source = "git+https://github.com/rcore-os/rcore-fs#ff3dd7d157bfdfaa05f8228ebb80456799fe6b4d" +source = "git+https://github.com/rcore-os/rcore-fs#f8d7b06727b0a66091419b2dd6a15f620a7152af" [[package]] name = "rcore-fs-sfs" version = "0.1.0" -source = "git+https://github.com/rcore-os/rcore-fs#ff3dd7d157bfdfaa05f8228ebb80456799fe6b4d" +source = "git+https://github.com/rcore-os/rcore-fs#f8d7b06727b0a66091419b2dd6a15f620a7152af" dependencies = [ "bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 853fda1..63f4969 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -30,8 +30,8 @@ link_user = [] run_cmdline = [] [profile.dev] -# MUST >= 1 : Enable RVO to avoid stack overflow -opt-level = 1 +# MUST >= 2 : Enable RVO to avoid stack overflow +opt-level = 2 [dependencies] log = "0.4" diff --git a/kernel/Makefile b/kernel/Makefile index 6a82f38..f1c3281 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -68,6 +68,7 @@ qemu_opts += \ -drive format=raw,file=$(bootimage) \ -drive format=qcow2,file=$(SFSIMG),media=disk,cache=writeback \ -serial mon:stdio \ + -m 4G \ -device isa-debug-exit ifeq ($(pci_passthru), ) qemu_net_opts += \ @@ -307,4 +308,4 @@ endif .PHONY: addr2line: - @python3 ../tools/addr2line.py $(prefix)addr2line $(arch) + @python3 ../tools/addr2line.py $(prefix)addr2line $(arch) $(mode) diff --git a/kernel/src/arch/x86_64/memory.rs b/kernel/src/arch/x86_64/memory.rs index 822624f..b2570a9 100644 --- a/kernel/src/arch/x86_64/memory.rs +++ b/kernel/src/arch/x86_64/memory.rs @@ -2,7 +2,10 @@ use crate::consts::KERNEL_OFFSET; use bit_allocator::BitAlloc; // Depends on kernel use super::{BootInfo, MemoryRegionType}; -use crate::memory::{active_table, init_heap, FRAME_ALLOCATOR}; +use crate::memory::{active_table, init_heap, FRAME_ALLOCATOR, alloc_frame}; +use crate::HEAP_ALLOCATOR; +use rcore_memory::PAGE_SIZE; +use alloc::vec::Vec; use log::*; use once::*; use rcore_memory::paging::*; @@ -12,6 +15,7 @@ pub fn init(boot_info: &BootInfo) { init_frame_allocator(boot_info); init_device_vm_map(); init_heap(); + enlarge_heap(); info!("memory: init end"); } @@ -38,3 +42,32 @@ fn init_device_vm_map() { .map(KERNEL_OFFSET + 0xfee00000, 0xfee00000) .update(); } + +fn enlarge_heap() { + let mut page_table = active_table(); + let mut addrs = Vec::new(); + let va_offset = KERNEL_OFFSET + 0xe0000000; + for i in 0..16384 { + let page = alloc_frame().unwrap(); + let va = KERNEL_OFFSET + 0xe0000000 + page; + if let Some((ref mut addr, ref mut len)) = addrs.last_mut() { + if *addr - PAGE_SIZE == va { + *len += PAGE_SIZE; + *addr -= PAGE_SIZE; + continue; + } + } + addrs.push((va, PAGE_SIZE)); + } + for (addr, len) in addrs.into_iter() { + for va in (addr..(addr+len)).step_by(PAGE_SIZE) { + page_table.map(va, va - va_offset).update(); + } + info!("Adding {:#X} {:#X} to heap", addr, len); + unsafe { + HEAP_ALLOCATOR + .lock() + .init(addr, len); + } + } +} diff --git a/kernel/src/arch/x86_64/paging.rs b/kernel/src/arch/x86_64/paging.rs index 47a4058..fb45522 100644 --- a/kernel/src/arch/x86_64/paging.rs +++ b/kernel/src/arch/x86_64/paging.rs @@ -78,7 +78,10 @@ impl PageTable for ActivePageTable { } impl PageTableExt for ActivePageTable { - const TEMP_PAGE_ADDR: usize = KERNEL_OFFSET | 0xcafeb000; + // FIXME: the default value 0xcafebe000 is so low that allocation might overwrite it sometimes. + // However, putting it to KERNEL_OFFSET | 0xcafeb000 has unintended effects. + // Someone needs to reconsider this and use an ultimate solution. + // const TEMP_PAGE_ADDR: usize = KERNEL_OFFSET | 0xcafeb000; } impl ActivePageTable { diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 37187c5..6f9ca29 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -1147,9 +1147,9 @@ impl From for Stat { } } -const SEEK_SET: u8 = 1; -const SEEK_CUR: u8 = 2; -const SEEK_END: u8 = 4; +const SEEK_SET: u8 = 0; +const SEEK_CUR: u8 = 1; +const SEEK_END: u8 = 2; #[derive(Debug, Copy, Clone)] #[repr(C)] diff --git a/kernel/src/syscall/misc.rs b/kernel/src/syscall/misc.rs index 110b041..fc6779f 100644 --- a/kernel/src/syscall/misc.rs +++ b/kernel/src/syscall/misc.rs @@ -2,6 +2,7 @@ use super::*; use crate::arch::cpu; use core::mem::size_of; use core::sync::atomic::{AtomicI32, Ordering}; +use crate::consts::USER_STACK_SIZE; pub fn sys_arch_prctl(code: i32, addr: usize, tf: &mut TrapFrame) -> SysResult { const ARCH_SET_FS: i32 = 0x1002; @@ -136,7 +137,10 @@ pub struct SysInfo { mem_unit: u32, } +const RLIMIT_STACK: usize = 3; +const RLIMIT_RSS: usize = 5; const RLIMIT_NOFILE: usize = 7; +const RLIMIT_AS: usize = 9; pub fn sys_prlimit64( pid: usize, @@ -150,6 +154,18 @@ pub fn sys_prlimit64( pid, resource, new_limit, old_limit ); match resource { + RLIMIT_STACK => { + if !old_limit.is_null() { + proc.vm.check_write_ptr(old_limit)?; + unsafe { + *old_limit = RLimit { + cur: USER_STACK_SIZE as u64, + max: USER_STACK_SIZE as u64, + }; + } + } + Ok(0) + } RLIMIT_NOFILE => { if !old_limit.is_null() { proc.vm.check_write_ptr(old_limit)?; @@ -161,6 +177,19 @@ pub fn sys_prlimit64( } } Ok(0) + }, + RLIMIT_RSS | RLIMIT_AS => { + if !old_limit.is_null() { + proc.vm.check_write_ptr(old_limit)?; + unsafe { + // 1GB + *old_limit = RLimit { + cur: 1024 * 1024 * 1024, + max: 1024 * 1024 * 1024, + }; + } + } + Ok(0) } _ => Err(SysError::ENOSYS), } diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index b818fc1..23e047d 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -161,6 +161,15 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { SYS_GETCWD => sys_getcwd(args[0] as *mut u8, args[1]), // 80 SYS_CHDIR => sys_chdir(args[0] as *const u8), + // 90 + SYS_CHMOD => { + warn!("sys_chmod is unimplemented"); + Ok(0) + } + SYS_FCHMOD => { + warn!("sys_fchmod is unimplemented"); + Ok(0) + } SYS_FCHOWN => { warn!("sys_fchown is unimplemented"); Ok(0) diff --git a/tools/addr2line.py b/tools/addr2line.py index fa84228..a7a1e79 100644 --- a/tools/addr2line.py +++ b/tools/addr2line.py @@ -7,12 +7,13 @@ print('Paste backtrace here, and then input EOF(Ctrl-D or Ctrl-Z) to get annotat lines = sys.stdin.readlines() addrline = sys.argv[1] arch = sys.argv[2] +mode = sys.argv[3] print('--------------------------------------') for line in lines: match = re.search('(#[0-9]+ )(0x[0-9A-F]+)( fp 0x[0-9A-F]+)', line) if match: addr = match.group(2) - process = subprocess.run([addrline, '-e', 'target/{0}/debug/rcore'.format(arch), '-f', '-C', addr], capture_output=True) + process = subprocess.run([addrline, '-e', 'target/{0}/{1}/rcore'.format(arch, mode), '-f', '-C', addr], capture_output=True) res = process.stdout.decode('utf-8') print('{0}{1}{3} {2}'.format(match.group(1), match.group(2), res.strip(), match.group(3))) else: