sim-se: add eventfd system call
authorBrandon Potter <brandon.potter@amd.com>
Wed, 18 Apr 2018 21:39:26 +0000 (17:39 -0400)
committerBrandon Potter <Brandon.Potter@amd.com>
Fri, 3 May 2019 15:55:16 +0000 (15:55 +0000)
Change-Id: I7aeb4fe808d0c8f2fb8041e3662d330d8458f09c
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/12125
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Ciro Santilli <ciro.santilli@arm.com>
Maintainer: Brandon Potter <Brandon.Potter@amd.com>

src/arch/x86/linux/process.cc
src/sim/fd_entry.hh
src/sim/syscall_emul.cc
src/sim/syscall_emul.hh

index 9e8997afc07db1316c55697b6787bee589ec50d5..f19dce358942c33b29e92283687ae7ff20f02a4b 100644 (file)
@@ -510,13 +510,13 @@ static SyscallDesc syscallDescs64[] = {
     /* 281 */ SyscallDesc("epoll_pwait", unimplementedFunc),
     /* 282 */ SyscallDesc("signalfd", unimplementedFunc),
     /* 283 */ SyscallDesc("timerfd_create", unimplementedFunc),
-    /* 284 */ SyscallDesc("eventfd", unimplementedFunc),
+    /* 284 */ SyscallDesc("eventfd", eventfdFunc<X86Linux64>),
     /* 285 */ SyscallDesc("fallocate", fallocateFunc),
     /* 286 */ SyscallDesc("timerfd_settime", unimplementedFunc),
     /* 287 */ SyscallDesc("timerfd_gettime", unimplementedFunc),
     /* 288 */ SyscallDesc("accept4", unimplementedFunc),
     /* 289 */ SyscallDesc("signalfd4", unimplementedFunc),
-    /* 290 */ SyscallDesc("eventfd2", unimplementedFunc),
+    /* 290 */ SyscallDesc("eventfd2", eventfdFunc<X86Linux64>),
     /* 291 */ SyscallDesc("epoll_create1", unimplementedFunc),
     /* 292 */ SyscallDesc("dup3", unimplementedFunc),
     /* 293 */ SyscallDesc("pipe2", unimplementedFunc),
@@ -882,7 +882,7 @@ static SyscallDesc syscallDescs32[] = {
     /* 320 */ SyscallDesc("utimensat", unimplementedFunc),
     /* 321 */ SyscallDesc("signalfd", unimplementedFunc),
     /* 322 */ SyscallDesc("timerfd", unimplementedFunc),
-    /* 323 */ SyscallDesc("eventfd", unimplementedFunc)
+    /* 323 */ SyscallDesc("eventfd", eventfdFunc<X86Linux32>)
 };
 
 I386LinuxProcess::I386LinuxProcess(ProcessParams * params, ObjectFile *objFile)
index 15e174ae62fbe1cab2a9cfa88c716b3d1b6b7ab9..94c874af9b27d2d31ecaee957d1c927fffa4bcd1 100644 (file)
@@ -80,6 +80,16 @@ class HBFDEntry: public FDEntry
         : FDEntry(close_on_exec), _flags(flags), _simFD(sim_fd)
     { }
 
+    HBFDEntry(HBFDEntry const& reg, bool close_on_exec = false)
+        : FDEntry(close_on_exec), _flags(reg._flags), _simFD(reg._simFD)
+    { }
+
+    std::shared_ptr<FDEntry>
+    clone() const override
+    {
+        return std::make_shared<HBFDEntry>(*this);
+    }
+
     int getFlags() const { return _flags; }
     int getSimFD() const { return _simFD; }
 
index a9490fa57565e39e166c099ffe955a1c0b761025..bfec1f5a581c0622ed7a9258985c4dedf2ccaf9e 100644 (file)
 using namespace std;
 using namespace TheISA;
 
+void
+warnUnsupportedOS(std::string syscall_name)
+{
+    warn("Cannot invoke %s on host operating system.", syscall_name);
+}
+
 SyscallReturn
 unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
                   ThreadContext *tc)
@@ -1036,9 +1042,7 @@ getegidFunc(SyscallDesc *desc, int callnum, Process *process,
 SyscallReturn
 fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
 {
-#if NO_FALLOCATE
-    warn("Host OS cannot support calls to fallocate. Ignoring syscall");
-#else
+#if __linux__
     int index = 0;
     int tgt_fd = p->getSyscallArg(tc, index);
     int mode = p->getSyscallArg(tc, index);
@@ -1053,8 +1057,11 @@ fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
     int result = fallocate(sim_fd, mode, offset, len);
     if (result < 0)
         return -errno;
-#endif
     return 0;
+#else
+    warnUnsupportedOS("fallocate");
+    return -1;
+#endif
 }
 
 SyscallReturn
index 79960278055726b554063757600e98ee7e10c8ee..91db9ae49a91b0f055cf2309f60bb808f2447cf7 100644 (file)
 #define NO_STAT64 0
 #endif
 
-#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
-     defined(__FreeBSD__) || defined(__NetBSD__))
-#define NO_STATFS 1
-#else
-#define NO_STATFS 0
-#endif
-
-#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
-     defined(__FreeBSD__) || defined(__NetBSD__))
-#define NO_FALLOCATE 1
-#else
-#define NO_FALLOCATE 0
-#endif
-
 ///
 /// @file syscall_emul.hh
 ///
 /// This file defines objects used to emulate syscalls from the target
 /// application on the host machine.
 
+#ifdef __linux__
+#include <sys/eventfd.h>
+#include <sys/statfs.h>
+
+#endif
+
 #ifdef __CYGWIN32__
 #include <sys/fcntl.h>
 
 #include <sys/mman.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
-
-#if (NO_STATFS == 0)
-#include <sys/statfs.h>
-
-#else
-#include <sys/mount.h>
-
-#endif
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/uio.h>
 //
 //////////////////////////////////////////////////////////////////////
 
+void warnUnsupportedOS(std::string syscall_name);
 
 /// Handler for unimplemented syscalls that we haven't thought about.
 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
@@ -1530,9 +1515,7 @@ SyscallReturn
 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
            ThreadContext *tc)
 {
-#if NO_STATFS
-    warn("Host OS cannot support calls to statfs. Ignoring syscall");
-#else
+#ifdef __linux__
     std::string path;
 
     int index = 0;
@@ -1552,8 +1535,11 @@ statfsFunc(SyscallDesc *desc, int callnum, Process *process,
         return -errno;
 
     copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
-#endif
     return 0;
+#else
+    warnUnsupportedOS("statfs");
+    return -1;
+#endif
 }
 
 template <class OS>
@@ -2860,4 +2846,32 @@ acceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
     return p->fds->allocFD(afdp);
 }
 
+/// Target eventfd() function.
+template <class OS>
+SyscallReturn
+eventfdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
+{
+#ifdef __linux__
+    int index = 0;
+    unsigned initval = p->getSyscallArg(tc, index);
+    int in_flags = p->getSyscallArg(tc, index);
+
+    int sim_fd = eventfd(initval, in_flags);
+    if (sim_fd == -1)
+        return -errno;
+
+    bool cloexec = in_flags & OS::TGT_O_CLOEXEC;
+
+    int flags = cloexec ? OS::TGT_O_CLOEXEC : 0;
+    flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0;
+
+    auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec);
+    int tgt_fd = p->fds->allocFD(hbfdp);
+    return tgt_fd;
+#else
+    warnUnsupportedOS("eventfd");
+    return -1;
+#endif
+}
+
 #endif // __SIM_SYSCALL_EMUL_HH__