Skip to content

Commit

Permalink
before change pthread
Browse files Browse the repository at this point in the history
  • Loading branch information
lhw committed Nov 18, 2024
1 parent 8f328b4 commit 2517e13
Show file tree
Hide file tree
Showing 11,498 changed files with 1,161,815 additions and 25 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ justrun:
$(call run_qemu)

debug: build
$(call run_qemu_debug)
$(call run_qemu_debug) &
sleep 1
$(GDB) $(OUT_ELF) \
-ex 'target remote localhost:1234' \
-ex 'b rust_entry' \
-ex 'continue' \
-ex 'disp /16i $$pc'

debug_no_attach: build
$(call run_qemu_debug)
Expand Down
85 changes: 84 additions & 1 deletion api/ruxos_posix_api/src/imp/mmap/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use crate::ctypes;
use alloc::vec::Vec;
use axerrno::LinuxError;
use axsync::Mutex;
use core::{
ffi::{c_int, c_void},
ops::Bound,
};
use lazy_static::lazy_static;
use memory_addr::PAGE_SIZE_4K;
use ruxhal::mem::VirtAddr;
use ruxmm::paging::pte_update_page;
Expand All @@ -31,6 +33,86 @@ use {
alloc::sync::Arc,
};

lazy_static! {
static ref HEAP_TOP: Mutex<usize> = Mutex::new(0);
}

pub fn set_heap_top(addr: usize) {
let mut heap_top = HEAP_TOP.lock();
*heap_top = addr;
}

pub fn get_heap_top() -> usize {
let heap_top = HEAP_TOP.lock();
*heap_top
}

pub fn sys_brk(brk: *mut c_void) -> *mut c_void {
info!("sys_brk <= brk: {:p}",brk);
syscall_body!(sys_mmap, {
match brk as usize {
0 => {
if get_heap_top() == 0
{
//mmap 1M region as fake heap
let len: usize = 1024 * 1024;
//all the process share the fake heap
let mut new = Vma::new(-1, 0, ctypes::PROT_READ | ctypes::PROT_WRITE, ctypes::MAP_SHARED | ctypes::MAP_ANONYMOUS);
let binding_task = current();
let mut vma_map = binding_task.mm.vma_map.lock();

let try_addr = find_free_region(&vma_map, None, len);

match try_addr {
Some(vaddr) => {
info!("lhw debug create heap suc {:x} {:x}",vaddr, (vaddr + len));
new.start_addr = vaddr;
new.end_addr = vaddr + len;
vma_map.insert(vaddr, new);
let res = vaddr + len;
set_heap_top(res);
return Ok(res as *mut c_void);
}
_ => {
info!("lhw debug create heap err");
return Err(LinuxError::ENOMEM);
},
}
}
else
{
return Ok(get_heap_top() as *mut c_void);
}
}
_ => {
return Ok(get_heap_top() as *mut c_void);
/*let heap_top = get_heap_top();
if brk as usize <= heap_top {
//shrink heap, do nothing
return Ok(brk);
}
let len = brk as usize - heap_top;
let mut new = Vma::new(-1, 0, ctypes::PROT_READ | ctypes::PROT_WRITE, ctypes::MAP_SHARED | ctypes::MAP_ANONYMOUS);
let binding_task = current();
let mut vma_map = binding_task.mm.vma_map.lock();
//find mem after heap top to expand heap top
let try_addr = snatch_fixed_region(&mut vma_map, heap_top, len);
match try_addr {
Some(vaddr) => {
new.start_addr = vaddr;
new.end_addr = vaddr + len;
vma_map.insert(vaddr, new);
let res = vaddr;
set_heap_top(res);
return Ok(res as *mut c_void);
}
_ => { return Err(LinuxError::ENOMEM); },
}*/
}
}
})
}

/// Creates a new mapping in the virtual address space of the calling process.
///
/// Note: support flags `MAP_PRIVATE`, `MAP_SHARED`, `MAP_ANONYMOUS`, `MAP_FILE`, `MAP_FIXED`.
Expand All @@ -42,7 +124,7 @@ pub fn sys_mmap(
fd: c_int,
off: ctypes::off_t,
) -> *mut c_void {
debug!(
info!(
"sys_mmap <= start: {:p}, len: 0x{:x}, prot:0x{:x?}, flags:0x{:x?}, fd: {}",
start, len, prot, flags, fd
);
Expand Down Expand Up @@ -94,6 +176,7 @@ pub fn sys_mmap(
new.start_addr = vaddr;
new.end_addr = vaddr + len;
vma_map.insert(vaddr, new);
info!("lhw debug in sys_mmap normal return {:x}",vaddr);
Ok(vaddr as *mut c_void)
}
_ => Err(LinuxError::ENOMEM),
Expand Down
2 changes: 1 addition & 1 deletion api/ruxos_posix_api/src/imp/mmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cfg_if::cfg_if! {
mod utils;
mod api;
mod trap;
pub use self::api::{sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap};
pub use self::api::{sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap, sys_brk};
}else {
mod legacy;
pub use self::legacy::{sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap};
Expand Down
1 change: 1 addition & 0 deletions api/ruxos_posix_api/src/imp/mmap/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub(crate) fn snatch_fixed_region(

// Return None if the specified address can't be used
if start < VMA_START || end > VMA_END {
info!("lhw debug in snatch_fixed_region mem out of range {} {}", start, end);
return None;
}

Expand Down
3 changes: 2 additions & 1 deletion api/ruxos_posix_api/src/imp/pthread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub unsafe fn sys_clone(
tls: *mut c_void,
ctid: *mut ctypes::pid_t,
) -> c_int {
debug!(
info!(
"sys_clone <= flags: {:x}, stack: {:p}, ctid: {:x}",
flags, stack, ctid as usize
);
Expand All @@ -268,6 +268,7 @@ pub unsafe fn sys_clone(
(*(stack as *mut usize)) as *const (),
)
};
unsafe { info!("lhw debug in sys_clone func {:?} stack {}",func, *(stack as *mut usize)); }
let args = unsafe { *((stack as usize + 8) as *mut usize) } as *mut c_void;

let set_tid = if (flags as u32 & ctypes::CLONE_CHILD_SETTID) != 0 {
Expand Down
39 changes: 31 additions & 8 deletions api/ruxos_posix_api/src/imp/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use crate::ctypes;

use axerrno::LinuxError;

// nanoseconds per a second
const NANO_PER_SECOND: i64 = 1000000000;

impl From<ctypes::timespec> for Duration {
fn from(ts: ctypes::timespec) -> Self {
Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32)
Expand Down Expand Up @@ -112,16 +115,36 @@ pub unsafe fn sys_nanosleep(req: *const ctypes::timespec, rem: *mut ctypes::time

/// Sleep some nanoseconds
///
/// TODO: clockid is not used, should be woken by signals, and set errno
///
/// TODO: should be woken by signals, and set errno
/// TODO: deal with flags
pub unsafe fn sys_clock_nanosleep(
_clockid: *const ctypes::clockid_t,
_flags: c_int,
_t: *const ctypes::timespec,
_remain: *mut ctypes::timespec,
which_clock: *const ctypes::clockid_t,
flags: c_int,
req: *const ctypes::timespec,
rem: *mut ctypes::timespec,
) -> c_int {
syscall_body!(sys_nanosleep, {
warn!("sys_clock_nanosleep is not implemented yet, but returns 0 for compatibility");
info!("sys_clock_nanosleep flags: {:x}, req: {:?}, rem: {:?}",flags, *req, *rem);
syscall_body!(sys_clock_nanosleep, {
unsafe {
if req.is_null() || (*req).tv_nsec < 0 || (*req).tv_nsec >= NANO_PER_SECOND {
return Err(LinuxError::EINVAL);
}
}
let now = ruxhal::time::current_time();
let deadline = unsafe { now + Duration::from(*req) };
#[cfg(feature = "multitask")]
ruxtask::sleep_until(deadline);
#[cfg(not(feature = "multitask"))]
ruxhal::time::busy_wait_until(deadline);
let after = ruxhal::time::current_time();
let actual = after - now;
let due = deadline - now;
if let Some(diff) = due.checked_sub(actual) {
if !rem.is_null() {
unsafe { (*rem) = diff.into() };
}
return Err(LinuxError::EINTR);
}
Ok(0)
})
}
Expand Down
2 changes: 1 addition & 1 deletion api/ruxos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub use imp::io_mpx::{sys_pselect6, sys_select};
#[cfg(feature = "fd")]
pub use imp::ioctl::sys_ioctl;
#[cfg(feature = "alloc")]
pub use imp::mmap::{sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap};
pub use imp::mmap::{sys_brk, sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap};
#[cfg(feature = "net")]
pub use imp::net::{
sys_accept, sys_bind, sys_connect, sys_freeaddrinfo, sys_getaddrinfo, sys_getpeername,
Expand Down
Binary file added apps/c/busybox/busybox-1.36.0.tar.bz2
Binary file not shown.
1 change: 1 addition & 0 deletions apps/c/busybox/busybox-1.36.0/.busybox_unstripped.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd_busybox_unstripped := /home/lhw/rux/ruxos/apps/c/busybox/busybox-1.36.0/scripts/trylink "busybox_unstripped" "aarch64-linux-gnu-gcc" "-Wall -Wshadow -Wwrite-strings -Wundef -Wstrict-prototypes -Wunused -Wunused-parameter -Wunused-function -Wunused-value -Wmissing-prototypes -Wmissing-declarations -Wno-format-security -Wdeclaration-after-statement -Wold-style-definition -finline-limit=0 -fno-builtin-strlen -fomit-frame-pointer -ffunction-sections -fdata-sections -fno-guess-branch-probability -funsigned-char -static-libgcc -falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-builtin-printf -Os " " " " applets/built-in.o" " archival/lib.a archival/libarchive/lib.a console-tools/lib.a coreutils/lib.a coreutils/libcoreutils/lib.a debianutils/lib.a klibc-utils/lib.a e2fsprogs/lib.a editors/lib.a findutils/lib.a init/lib.a libbb/lib.a libpwdgrp/lib.a loginutils/lib.a mailutils/lib.a miscutils/lib.a modutils/lib.a networking/lib.a networking/libiproute/lib.a networking/udhcp/lib.a printutils/lib.a procps/lib.a runit/lib.a selinux/lib.a shell/lib.a sysklogd/lib.a util-linux/lib.a util-linux/volume_id/lib.a archival/built-in.o archival/libarchive/built-in.o console-tools/built-in.o coreutils/built-in.o coreutils/libcoreutils/built-in.o debianutils/built-in.o klibc-utils/built-in.o e2fsprogs/built-in.o editors/built-in.o findutils/built-in.o init/built-in.o libbb/built-in.o libpwdgrp/built-in.o loginutils/built-in.o mailutils/built-in.o miscutils/built-in.o modutils/built-in.o networking/built-in.o networking/libiproute/built-in.o networking/udhcp/built-in.o printutils/built-in.o procps/built-in.o runit/built-in.o selinux/built-in.o shell/built-in.o sysklogd/built-in.o util-linux/built-in.o util-linux/volume_id/built-in.o" "m rt resolv resolv" """" && /home/lhw/rux/ruxos/apps/c/busybox/busybox-1.36.0/scripts/generate_BUFSIZ.sh --post include/common_bufsiz.h
Loading

0 comments on commit 2517e13

Please sign in to comment.