Commit 23693bb7 authored by hidehiko@chromium.org's avatar hidehiko@chromium.org

Implement nacl_irt_futex for non-sfi mode.

This CL implements nacl_irt_futex for non-sfi mode.

BUG=https://code.google.com/p/nativeclient/issues/detail?id=3734
TEST=Tried to call a newly added function from plugin via AT_SYSINFO.

Review URL: https://codereview.chromium.org/139453002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245248 0039d316-1c4b-4281-b951-d872f2087c98
parent 2ac57b79
...@@ -196,6 +196,7 @@ ...@@ -196,6 +196,7 @@
'nacl/loader/nonsfi/elf_loader.h', 'nacl/loader/nonsfi/elf_loader.h',
'nacl/loader/nonsfi/irt_basic.cc', 'nacl/loader/nonsfi/irt_basic.cc',
'nacl/loader/nonsfi/irt_fdio.cc', 'nacl/loader/nonsfi/irt_fdio.cc',
'nacl/loader/nonsfi/irt_futex.cc',
'nacl/loader/nonsfi/irt_interfaces.cc', 'nacl/loader/nonsfi/irt_interfaces.cc',
'nacl/loader/nonsfi/irt_interfaces.h', 'nacl/loader/nonsfi/irt_interfaces.h',
'nacl/loader/nonsfi/irt_thread.cc', 'nacl/loader/nonsfi/irt_thread.cc',
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <errno.h>
#include <linux/futex.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>
#include "components/nacl/loader/nonsfi/irt_interfaces.h"
#include "native_client/src/trusted/service_runtime/include/sys/time.h"
namespace nacl {
namespace nonsfi {
namespace {
// Converts a pair of NaCl's timespec of absolute time and host's timespec of
// the current time to host's timespec of the relative time between them.
// Note that assuming tv_nsec for the both input structs are non-negative,
// the returned reltime's tv_nsec is also always non-negative.
// (I.e., for -0.1 sec, {tv_sec: -1, tv_nsec: 900000000} will be returned).
void NaClAbsTimeToRelTime(const struct nacl_abi_timespec& nacl_abstime,
const struct timespec& now,
struct timespec* reltime) {
reltime->tv_sec = nacl_abstime.tv_sec - now.tv_sec;
reltime->tv_nsec = nacl_abstime.tv_nsec - now.tv_nsec;
if (reltime->tv_nsec < 0) {
reltime->tv_sec -= 1;
reltime->tv_nsec += 1000000000;
}
}
int IrtFutexWaitAbs(volatile int* addr, int value,
const struct nacl_abi_timespec* abstime) {
struct timespec timeout;
struct timespec* timeout_ptr = NULL;
if (abstime) {
// futex syscall takes relative timeout, but the ABI for IRT's
// futex_wait_abs is absolute timeout. So, here we convert it.
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now))
return errno;
NaClAbsTimeToRelTime(*abstime, now, &timeout);
// Linux's FUTEX_WAIT returns EINVAL for negative timeout, but an absolute
// time that is in the past is a valid argument to irt_futex_wait_abs(),
// and a caller expects ETIMEDOUT.
// Here check only tv_sec since we assume time_t is signed. See also
// the comment for NaClAbsTimeToRelTime.
if (timeout.tv_sec < 0)
return ETIMEDOUT;
timeout_ptr = &timeout;
}
if (syscall(SYS_futex, addr, FUTEX_WAIT_PRIVATE, value, timeout_ptr, 0, 0))
return errno;
return 0;
}
int IrtFutexWake(volatile int* addr, int nwake, int* count) {
int result = syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, nwake, 0, 0, 0);
if (result < 0)
return errno;
*count = result;
return 0;
}
} // namespace
// For futex_wait_abs, the argument type should be nacl_abi_timespec. However,
// the definition uses its host type, struct timespec. So, here we need to cast
// it.
const nacl_irt_futex kIrtFutex = {
reinterpret_cast<int(*)(volatile int*, int, const struct timespec*)>(
IrtFutexWaitAbs),
IrtFutexWake,
};
} // namespace nonsfi
} // namespace nacl
...@@ -25,6 +25,7 @@ const NaClInterfaceTable kIrtInterfaces[] = { ...@@ -25,6 +25,7 @@ const NaClInterfaceTable kIrtInterfaces[] = {
NACL_INTERFACE_TABLE(NACL_IRT_BASIC_v0_1, kIrtBasic), NACL_INTERFACE_TABLE(NACL_IRT_BASIC_v0_1, kIrtBasic),
NACL_INTERFACE_TABLE(NACL_IRT_DEV_FDIO_v0_1, kIrtFdIO), NACL_INTERFACE_TABLE(NACL_IRT_DEV_FDIO_v0_1, kIrtFdIO),
NACL_INTERFACE_TABLE(NACL_IRT_THREAD_v0_1, kIrtThread), NACL_INTERFACE_TABLE(NACL_IRT_THREAD_v0_1, kIrtThread),
NACL_INTERFACE_TABLE(NACL_IRT_FUTEX_v0_1, kIrtFutex),
NACL_INTERFACE_TABLE(NACL_IRT_TLS_v0_1, kIrtTls), NACL_INTERFACE_TABLE(NACL_IRT_TLS_v0_1, kIrtTls),
}; };
#undef NACL_INTERFACE_TABLE #undef NACL_INTERFACE_TABLE
......
...@@ -17,6 +17,7 @@ size_t NaClIrtInterface(const char* interface_ident, ...@@ -17,6 +17,7 @@ size_t NaClIrtInterface(const char* interface_ident,
extern const struct nacl_irt_basic kIrtBasic; extern const struct nacl_irt_basic kIrtBasic;
extern const struct nacl_irt_fdio kIrtFdIO; extern const struct nacl_irt_fdio kIrtFdIO;
extern const struct nacl_irt_thread kIrtThread; extern const struct nacl_irt_thread kIrtThread;
extern const struct nacl_irt_futex kIrtFutex;
extern const struct nacl_irt_tls kIrtTls; extern const struct nacl_irt_tls kIrtTls;
} // namespace nonsfi } // namespace nonsfi
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment