Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions header/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub mod syscalls {
ExitThread,
AtomicWait,
AtomicWake,
ClockGetTime,
AllocMem,
FreeMem,
Write,
Expand Down Expand Up @@ -76,14 +75,21 @@ pub mod syscalls {
Recvmsg,
GetAddrinfo,
FreeAddrinfo,
NanoSleep,
MqOpen,
MqClose,
MqUnlink,
MqTimedSend,
MqTimedReceive,
MqGetSetAttr,
Ioctl,
ClockGetTime,
ClockSetTime,
ClockNanoSleep,
TimerCreate,
TimerDelete,
TimerGetTime,
TimerSetTime,
TimerGetOverrun,
LastNR,
}
}
Expand Down
96 changes: 54 additions & 42 deletions kernel/src/syscall_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ use core::{
sync::atomic::AtomicUsize,
};
use libc::{
addrinfo, c_char, c_int, c_long, c_uint, c_ulong, c_void, clockid_t, mode_t, msghdr, off_t,
sigset_t, size_t, sockaddr, socklen_t, timespec, EBUSY, EINVAL, ESRCH,
addrinfo, c_char, c_int, c_long, c_uint, c_ulong, c_void, clockid_t, itimerspec, mode_t,
msghdr, off_t, sigevent, sigset_t, size_t, sockaddr, socklen_t, time_t, timer_t, timespec,
EBUSY, EINVAL, ESRCH,
};

#[cfg(not(enable_vfs))]
Expand Down Expand Up @@ -261,6 +262,7 @@ pub struct mq_attr {
pub mq_curmsgs: c_long,
pub pad: [c_long; 4],
}

// For every syscall number in NR, we have to define a module to
// handle the syscall request. `handle_context` serves as the
// dispatcher if syscall is invoked via software interrupt.
Expand Down Expand Up @@ -431,21 +433,44 @@ atomic_wake(addr: usize, count: *mut usize) -> c_long {
})
});

// Only for posix testsuite, we need to implement a stub for clock_gettime
define_syscall_handler!(
clock_gettime(_clk_id: clockid_t, tp: *mut timespec) -> c_long {
if tp.is_null() {
return -1;
}
// Only support CLOCK_MONOTONIC.
let now = time::now();
unsafe {
tp.write(timespec {
tv_sec: now.as_secs() as c_int,
tv_nsec: now.subsec_nanos() as c_int,
});
}
0
clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_long {
posix_timers::clock_gettime(clk_id, tp) as c_long
});

define_syscall_handler!(
clock_settime(clk_id: clockid_t, tp: *const timespec) -> c_long {
posix_timers::clock_settime(clk_id, tp) as c_long
});

define_syscall_handler!(
timer_create(clock_id: clockid_t, evp: *const sigevent, timerid: *mut timer_t) -> c_long {
posix_timers::timer_create(clock_id, evp, timerid) as c_long
});

define_syscall_handler!(
timer_delete(timerid: timer_t) -> c_long {
posix_timers::timer_delete(timerid) as c_long
});

define_syscall_handler!(
timer_gettime(timerid: timer_t, value: *mut itimerspec) -> c_long {
posix_timers::timer_gettime(timerid, value) as c_long
});

define_syscall_handler!(
timer_settime(
timerid: timer_t,
flags: c_int,
value: *const itimerspec,
ovalue: *mut itimerspec
) -> c_long {
posix_timers::timer_settime(timerid, flags, value, ovalue) as c_long
});

define_syscall_handler!(
timer_getoverrun(timerid: timer_t) -> c_long {
posix_timers::timer_getoverrun(timerid) as c_long
});

define_syscall_handler!(
Expand Down Expand Up @@ -754,33 +779,13 @@ define_syscall_handler!(
// Netdb syscall end

define_syscall_handler!(
sys_clock_nanosleep(
clock_id: i32,
flags: i32,
clock_nanosleep(
clock_id: clockid_t,
flags: c_int,
rqtp: *const timespec,
rmtp: *mut timespec
) -> c_int {
// TODO: Valid Clock Id

// TODO: Valid rqtp

// TODO: Implement absolute time sleep
let duration = timespec {
tv_sec: unsafe { rqtp.read().tv_sec },
tv_nsec: unsafe { rqtp.read().tv_nsec },
};

// TODO: Implement tv_nsec
let ticks = Tick(blueos_kconfig::CONFIG_TICKS_PER_SECOND as usize *
duration.tv_sec as usize +
duration.tv_nsec as usize *
blueos_kconfig::CONFIG_TICKS_PER_SECOND as usize / 1_000_000_000);
if ticks.0 == 0 {
scheduler::yield_me();
} else {
scheduler::suspend_me_for::<()>(ticks,None);
}
0
) -> c_long {
posix_timers::clock_nanosleep(clock_id, flags, rqtp, rmtp) as c_long
}
);

Expand Down Expand Up @@ -845,6 +850,13 @@ syscall_table! {
(AtomicWait, atomic_wait),
// For test only
(ClockGetTime, clock_gettime),
(ClockSetTime, clock_settime),
(ClockNanoSleep, clock_nanosleep),
(TimerCreate, timer_create),
(TimerDelete, timer_delete),
(TimerGetTime, timer_gettime),
(TimerSetTime, timer_settime),
(TimerGetOverrun, timer_getoverrun),
(AllocMem, alloc_mem),
(FreeMem, free_mem),
(Write, write),
Expand Down Expand Up @@ -891,7 +903,6 @@ syscall_table! {
(Recvmsg,recvmsg),
(GetAddrinfo,getaddrinfo),
(FreeAddrinfo,freeaddrinfo),
(NanoSleep,sys_clock_nanosleep),
(MqOpen, mq_open),
(MqClose, mq_close),
(MqUnlink, mq_unlink),
Expand All @@ -908,4 +919,5 @@ pub fn dispatch_syscall(ctx: &Context) -> usize {

// Begin syscall modules.
pub mod echo;
pub mod posix_timers;
// End syscall modules.
Loading