From patchwork Fri Oct 18 12:55:52 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mle+hs@mega-nerd.com X-Patchwork-Id: 284688 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 896332C0082 for ; Sat, 19 Oct 2013 04:44:45 +1100 (EST) Received: from localhost ([::1]:58897 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VXE6N-0002LF-CX for incoming@patchwork.ozlabs.org; Fri, 18 Oct 2013 13:44:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VX9bC-0001FR-86 for qemu-devel@nongnu.org; Fri, 18 Oct 2013 08:56:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VX9b7-0007ZH-8Q for qemu-devel@nongnu.org; Fri, 18 Oct 2013 08:56:14 -0400 Received: from hendrix.mega-nerd.net ([203.206.230.162]:45882) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VX9b6-0007Z4-9U for qemu-devel@nongnu.org; Fri, 18 Oct 2013 08:56:09 -0400 Received: from rollins.mnn (pharoah-vpn [10.9.8.2]) by hendrix.mega-nerd.net (Postfix) with ESMTP id 232C9106E73; Fri, 18 Oct 2013 23:56:07 +1100 (EST) Received: by rollins.mnn (Postfix, from userid 1000) id 1C60E180046; Fri, 18 Oct 2013 23:56:07 +1100 (EST) From: mle+hs@mega-nerd.com To: qemu-devel@nongnu.org Date: Fri, 18 Oct 2013 23:55:52 +1100 Message-Id: <1382100952-2269-2-git-send-email-mle+hs@mega-nerd.com> X-Mailer: git-send-email 1.8.4.rc3 In-Reply-To: <1382100952-2269-1-git-send-email-mle+hs@mega-nerd.com> References: <1382100952-2269-1-git-send-email-mle+hs@mega-nerd.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 203.206.230.162 X-Mailman-Approved-At: Fri, 18 Oct 2013 13:42:36 -0400 Cc: Erik de Castro Lopo Subject: [Qemu-devel] [PATCH 2/2] linux-user: Implement handling of 5 POSIX timer syscalls. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Erik de Castro Lopo Implement timer_create, timer_settime, timer_gettime, timer_getoverrun and timer_delete. --- linux-user/syscall.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 4a14a43..5be400d 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -428,6 +428,38 @@ _syscall4(int, sys_prlimit64, pid_t, pid, int, resource, struct host_rlimit64 *, old_limit) #endif +#if defined(TARGET_NR_timer_create) +#ifndef __NR_timer_create +# define __NR_timer_create -1 +# define __NR_timer_settime -1 +# define __NR_timer_gettime -1 +# define __NR_timer_getoverrun -1 +# define __NR_timer_delete -1 +#endif + +#define __NR_sys_timer_create __NR_timer_create +#define __NR_sys_timer_settime __NR_timer_settime +#define __NR_sys_timer_gettime __NR_timer_gettime +#define __NR_sys_timer_getoverrun __NR_timer_getoverrun +#define __NR_sys_timer_delete __NR_timer_delete + + +/* Maxiumum of 32 active timers allowed at any one time. */ +static timer_t g_posix_timers[32] = { 0, } ; + +static inline int next_free_host_timer(void) +{ + int k ; + /* FIXME: Does finding the next free slot require a lock? */ + for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) + if (g_posix_timers[k] == 0) { + g_posix_timers[k] = (timer_t) 1; + return k; + } + return -1; +} +#endif + /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */ #ifdef TARGET_ARM static inline int regpairs_aligned(void *cpu_env) { @@ -4838,6 +4870,45 @@ static inline abi_long host_to_target_timespec(abi_ulong target_addr, return 0; } +static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec, + abi_ulong target_addr) +{ + struct target_itimerspec *target_itspec; + + if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) { + return -TARGET_EFAULT; + } + + host_itspec->it_interval.tv_sec = + tswapal(target_itspec->it_interval.tv_sec); + host_itspec->it_interval.tv_nsec = + tswapal(target_itspec->it_interval.tv_nsec); + host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec); + host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec); + + unlock_user_struct(target_itspec, target_addr, 1); + return 0; +} + +static inline abi_long host_to_target_itimerspec(abi_ulong target_addr, + struct itimerspec *host_its) +{ + struct target_itimerspec *target_itspec; + + if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) { + return -TARGET_EFAULT; + } + + target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec); + target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec); + + target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec); + target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec); + + unlock_user_struct(target_itspec, target_addr, 1); + return 0; +} + #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat) static inline abi_long host_to_target_stat64(void *cpu_env, abi_ulong target_addr, @@ -9195,6 +9266,123 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, break; } #endif + +#ifdef TARGET_NR_timer_create + case TARGET_NR_timer_create: + { + /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */ + + struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL; + struct target_sigevent *ptarget_sevp; + struct target_timer_t *ptarget_timer; + + int clkid = arg1; + int timer_index = next_free_host_timer(); + + if (timer_index < 0) { + ret = -TARGET_EAGAIN; + } else { + timer_t *phtimer = g_posix_timers + timer_index; + + if (arg2) { + if (!lock_user_struct(VERIFY_READ, ptarget_sevp, arg2, 1)) { + goto efault; + } + + host_sevp.sigev_signo = tswap32(ptarget_sevp->sigev_signo); + host_sevp.sigev_notify = tswap32(ptarget_sevp->sigev_notify); + + phost_sevp = &host_sevp; + } + + ret = get_errno(timer_create(clkid, phost_sevp, phtimer)); + if (ret) { + phtimer = NULL; + } else { + if (!lock_user_struct(VERIFY_WRITE, ptarget_timer, arg3, 1)) { + goto efault; + } + ptarget_timer->ptr = tswap32(0xcafe0000 | timer_index); + unlock_user_struct(ptarget_timer, arg3, 1); + } + } + break; + } +#endif + +#ifdef TARGET_NR_timer_settime + case TARGET_NR_timer_settime: + { + /* args: timer_t timerid, int flags, const struct itimerspec *new_value, + * struct itimerspec * old_value */ + arg1 &= 0xffff; + if (arg3 == 0 || arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) { + ret = -TARGET_EINVAL; + } else { + timer_t htimer = g_posix_timers[arg1]; + struct itimerspec hspec_new = {{0},}, hspec_old = {{0},}; + + target_to_host_itimerspec(&hspec_new, arg3); + ret = get_errno(timer_settime(htimer, arg2, &hspec_new, &hspec_old)); + host_to_target_itimerspec(arg2, &hspec_old); + } + break; + } +#endif + +#ifdef TARGET_NR_timer_gettime + case TARGET_NR_timer_gettime: + { + /* args: timer_t timerid, struct itimerspec *curr_value */ + arg1 &= 0xffff; + if (!arg2) { + return -TARGET_EFAULT; + } else if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) { + ret = -TARGET_EINVAL; + } else { + timer_t htimer = g_posix_timers[arg1]; + struct itimerspec hspec; + ret = get_errno(timer_gettime(htimer, &hspec)); + + if (host_to_target_itimerspec(arg2, &hspec)) { + ret = -TARGET_EFAULT; + } + } + break; + } +#endif + +#ifdef TARGET_NR_timer_getoverrun + case TARGET_NR_timer_getoverrun: + { + /* args: timer_t timerid */ + arg1 &= 0xffff; + if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) { + ret = -TARGET_EINVAL; + } else { + timer_t htimer = g_posix_timers[arg1]; + ret = get_errno(timer_getoverrun(htimer)); + } + break; + } +#endif + +#ifdef TARGET_NR_timer_delete + case TARGET_NR_timer_delete: + { + /* args: timer_t timerid */ + arg1 &= 0xffff; + if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) { + ret = -TARGET_EINVAL; + } else { + timer_t *phtimer = g_posix_timers + arg1; + ret = get_errno(timer_delete(phtimer)); + g_posix_timers[arg1] = 0; + } + break; + } +#endif + default: unimplemented: gemu_log("qemu: Unsupported syscall: %d\n", num);