sim: Use the system and OS to get endianness.
authorGabe Black <gabeblack@google.com>
Tue, 29 Oct 2019 22:50:48 +0000 (15:50 -0700)
committerGabe Black <gabeblack@google.com>
Wed, 30 Oct 2019 22:29:23 +0000 (22:29 +0000)
This converts the syscall implementations to either use the
OS::byteOrder constant or, if that's not available, the system's
getGuestByteOrder() accessor, to determine the byte order, instead of
relying on TheISA to provide the correct accessor.

Change-Id: Idf7b02ee8d73990224ceac9a5efaec91a5ebf79f
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/22364
Reviewed-by: Brandon Potter <Brandon.Potter@amd.com>
Maintainer: Gabe Black <gabeblack@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
src/sim/syscall_emul.cc
src/sim/syscall_emul.hh

index f2f4f276f4d3091316926bc346252d584c1bd44d..49fee7e8fdb9fd910ff8df53657337b6de44b872 100644 (file)
@@ -347,7 +347,7 @@ _llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc)
     uint64_t offset = (offset_high << 32) | offset_low;
 
     uint64_t result = lseek(sim_fd, offset, whence);
-    result = TheISA::htog(result);
+    result = htog(result, tc->getSystemPtr()->getGuestByteOrder());
 
     if (result == (off_t)-1)
         return -errno;
@@ -1262,9 +1262,10 @@ getdentsImpl(SyscallDesc *desc, int callnum, ThreadContext *tc)
          * passing the data back into the target's address space to preserve
          * endianness.
          */
-        buffer->d_ino = htog(buffer->d_ino);
-        buffer->d_off = htog(buffer->d_off);
-        buffer->d_reclen = htog(buffer->d_reclen);
+        const ByteOrder bo = tc->getSystemPtr()->getGuestByteOrder();
+        buffer->d_ino = htog(buffer->d_ino, bo);
+        buffer->d_off = htog(buffer->d_off, bo);
+        buffer->d_reclen = htog(buffer->d_reclen, bo);
 
         traversed += host_reclen;
     }
index 66ca28f823dc0834278155b9ef19b0e7ef94eaad..2d4ef25fd39d97a7adef9548a7dae51e7dfac275 100644 (file)
@@ -538,67 +538,65 @@ getElapsedTimeNano(T1 &sec, T2 &nsec)
 
 template <typename target_stat, typename host_stat>
 void
-convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
+convertStatBuf(target_stat &tgt, host_stat *host,
+               ByteOrder bo, bool fakeTTY=false)
 {
-    using namespace TheISA;
-
     if (fakeTTY)
         tgt->st_dev = 0xA;
     else
         tgt->st_dev = host->st_dev;
-    tgt->st_dev = TheISA::htog(tgt->st_dev);
+    tgt->st_dev = htog(tgt->st_dev, bo);
     tgt->st_ino = host->st_ino;
-    tgt->st_ino = TheISA::htog(tgt->st_ino);
+    tgt->st_ino = htog(tgt->st_ino, bo);
     tgt->st_mode = host->st_mode;
     if (fakeTTY) {
         // Claim to be a character device
         tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
         tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
     }
-    tgt->st_mode = TheISA::htog(tgt->st_mode);
+    tgt->st_mode = htog(tgt->st_mode, bo);
     tgt->st_nlink = host->st_nlink;
-    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
+    tgt->st_nlink = htog(tgt->st_nlink, bo);
     tgt->st_uid = host->st_uid;
-    tgt->st_uid = TheISA::htog(tgt->st_uid);
+    tgt->st_uid = htog(tgt->st_uid, bo);
     tgt->st_gid = host->st_gid;
-    tgt->st_gid = TheISA::htog(tgt->st_gid);
+    tgt->st_gid = htog(tgt->st_gid, bo);
     if (fakeTTY)
         tgt->st_rdev = 0x880d;
     else
         tgt->st_rdev = host->st_rdev;
-    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
+    tgt->st_rdev = htog(tgt->st_rdev, bo);
     tgt->st_size = host->st_size;
-    tgt->st_size = TheISA::htog(tgt->st_size);
+    tgt->st_size = htog(tgt->st_size, bo);
     tgt->st_atimeX = host->st_atime;
-    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
+    tgt->st_atimeX = htog(tgt->st_atimeX, bo);
     tgt->st_mtimeX = host->st_mtime;
-    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
+    tgt->st_mtimeX = htog(tgt->st_mtimeX, bo);
     tgt->st_ctimeX = host->st_ctime;
-    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
+    tgt->st_ctimeX = htog(tgt->st_ctimeX, bo);
     // Force the block size to be 8KB. This helps to ensure buffered io works
     // consistently across different hosts.
     tgt->st_blksize = 0x2000;
-    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
+    tgt->st_blksize = htog(tgt->st_blksize, bo);
     tgt->st_blocks = host->st_blocks;
-    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
+    tgt->st_blocks = htog(tgt->st_blocks, bo);
 }
 
 // Same for stat64
 
 template <typename target_stat, typename host_stat64>
 void
-convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
+convertStat64Buf(target_stat &tgt, host_stat64 *host,
+                 ByteOrder bo, bool fakeTTY=false)
 {
-    using namespace TheISA;
-
-    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
+    convertStatBuf<target_stat, host_stat64>(tgt, host, bo, fakeTTY);
 #if defined(STAT_HAVE_NSEC)
     tgt->st_atime_nsec = host->st_atime_nsec;
-    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
+    tgt->st_atime_nsec = htog(tgt->st_atime_nsec, bo);
     tgt->st_mtime_nsec = host->st_mtime_nsec;
-    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
+    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec, bo);
     tgt->st_ctime_nsec = host->st_ctime_nsec;
-    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
+    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec, bo);
 #else
     tgt->st_atime_nsec = 0;
     tgt->st_mtime_nsec = 0;
@@ -614,7 +612,7 @@ copyOutStatBuf(PortProxy &mem, Addr addr,
 {
     typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
     tgt_stat_buf tgt(addr);
-    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
+    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, OS::byteOrder, fakeTTY);
     tgt.copyOut(mem);
 }
 
@@ -625,7 +623,8 @@ copyOutStat64Buf(PortProxy &mem, Addr addr,
 {
     typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
     tgt_stat_buf tgt(addr);
-    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
+    convertStat64Buf<tgt_stat_buf, hst_stat64>(
+            tgt, host, OS::byteOrder, fakeTTY);
     tgt.copyOut(mem);
 }
 
@@ -636,27 +635,29 @@ copyOutStatfsBuf(PortProxy &mem, Addr addr,
 {
     TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
 
-    tgt->f_type = TheISA::htog(host->f_type);
+    const ByteOrder bo = OS::byteOrder;
+
+    tgt->f_type = htog(host->f_type, bo);
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
-    tgt->f_bsize = TheISA::htog(host->f_iosize);
+    tgt->f_bsize = htog(host->f_iosize, bo);
 #else
-    tgt->f_bsize = TheISA::htog(host->f_bsize);
+    tgt->f_bsize = htog(host->f_bsize, bo);
 #endif
-    tgt->f_blocks = TheISA::htog(host->f_blocks);
-    tgt->f_bfree = TheISA::htog(host->f_bfree);
-    tgt->f_bavail = TheISA::htog(host->f_bavail);
-    tgt->f_files = TheISA::htog(host->f_files);
-    tgt->f_ffree = TheISA::htog(host->f_ffree);
+    tgt->f_blocks = htog(host->f_blocks, bo);
+    tgt->f_bfree = htog(host->f_bfree, bo);
+    tgt->f_bavail = htog(host->f_bavail, bo);
+    tgt->f_files = htog(host->f_files, bo);
+    tgt->f_ffree = htog(host->f_ffree, bo);
     memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
-    tgt->f_namelen = TheISA::htog(host->f_namemax);
-    tgt->f_frsize = TheISA::htog(host->f_bsize);
+    tgt->f_namelen = htog(host->f_namemax, bo);
+    tgt->f_frsize = htog(host->f_bsize, bo);
 #elif defined(__APPLE__)
     tgt->f_namelen = 0;
     tgt->f_frsize = 0;
 #else
-    tgt->f_namelen = TheISA::htog(host->f_namelen);
-    tgt->f_frsize = TheISA::htog(host->f_frsize);
+    tgt->f_namelen = htog(host->f_namelen, bo);
+    tgt->f_frsize = htog(host->f_frsize, bo);
 #endif
 #if defined(__linux__)
     memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
@@ -1679,7 +1680,7 @@ readvFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
     for (size_t i = 0; i < count; ++i) {
         prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)),
                       &tiov[i], sizeof(typename OS::tgt_iovec));
-        hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len);
+        hiov[i].iov_len = gtoh(tiov[i].iov_len, OS::byteOrder);
         hiov[i].iov_base = new char [hiov[i].iov_len];
     }
 
@@ -1688,7 +1689,7 @@ readvFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
 
     for (size_t i = 0; i < count; ++i) {
         if (result != -1) {
-            prox.writeBlob(TheISA::htog(tiov[i].iov_base),
+            prox.writeBlob(htog(tiov[i].iov_base, OS::byteOrder),
                            hiov[i].iov_base, hiov[i].iov_len);
         }
         delete [] (char *)hiov[i].iov_base;
@@ -1720,9 +1721,9 @@ writevFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
 
         prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
                       &tiov, sizeof(typename OS::tgt_iovec));
-        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
+        hiov[i].iov_len = gtoh(tiov.iov_len, OS::byteOrder);
         hiov[i].iov_base = new char [hiov[i].iov_len];
-        prox.readBlob(TheISA::gtoh(tiov.iov_base), hiov[i].iov_base,
+        prox.readBlob(gtoh(tiov.iov_base, OS::byteOrder), hiov[i].iov_base,
                       hiov[i].iov_len);
     }
 
@@ -1955,25 +1956,26 @@ getrlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
     unsigned resource = process->getSyscallArg(tc, index);
     TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
 
+    const ByteOrder bo = OS::byteOrder;
     switch (resource) {
       case OS::TGT_RLIMIT_STACK:
         // max stack size in bytes: make up a number (8MB for now)
         rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
-        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
-        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
+        rlp->rlim_cur = htog(rlp->rlim_cur, bo);
+        rlp->rlim_max = htog(rlp->rlim_max, bo);
         break;
 
       case OS::TGT_RLIMIT_DATA:
         // max data segment size in bytes: make up a number
         rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
-        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
-        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
+        rlp->rlim_cur = htog(rlp->rlim_cur, bo);
+        rlp->rlim_max = htog(rlp->rlim_max, bo);
         break;
 
       case OS::TGT_RLIMIT_NPROC:
         rlp->rlim_cur = rlp->rlim_max = tc->getSystemPtr()->numContexts();
-        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
-        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
+        rlp->rlim_cur = htog(rlp->rlim_cur, bo);
+        rlp->rlim_max = htog(rlp->rlim_max, bo);
         break;
 
       default:
@@ -2002,21 +2004,22 @@ prlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
     if (n != 0)
         warn("prlimit: ignoring new rlimit");
     Addr o = process->getSyscallArg(tc, index);
-    if (o != 0)
-    {
+    if (o != 0) {
+
+        const ByteOrder bo = OS::byteOrder;
         TypedBufferArg<typename OS::rlimit> rlp(o);
         switch (resource) {
           case OS::TGT_RLIMIT_STACK:
             // max stack size in bytes: make up a number (8MB for now)
             rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
-            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
-            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
+            rlp->rlim_cur = htog(rlp->rlim_cur, bo);
+            rlp->rlim_max = htog(rlp->rlim_max, bo);
             break;
           case OS::TGT_RLIMIT_DATA:
             // max data segment size in bytes: make up a number
             rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
-            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
-            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
+            rlp->rlim_cur = htog(rlp->rlim_cur, bo);
+            rlp->rlim_max = htog(rlp->rlim_max, bo);
             break;
           default:
             warn("prlimit: unimplemented resource %d", resource);
@@ -2040,8 +2043,8 @@ clock_gettimeFunc(SyscallDesc *desc, int num, ThreadContext *tc)
 
     getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
     tp->tv_sec += seconds_since_epoch;
-    tp->tv_sec = TheISA::htog(tp->tv_sec);
-    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
+    tp->tv_sec = htog(tp->tv_sec, OS::byteOrder);
+    tp->tv_nsec = htog(tp->tv_nsec, OS::byteOrder);
 
     tp.copyOut(tc->getVirtProxy());
 
@@ -2077,8 +2080,8 @@ gettimeofdayFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
 
     getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
     tp->tv_sec += seconds_since_epoch;
-    tp->tv_sec = TheISA::htog(tp->tv_sec);
-    tp->tv_usec = TheISA::htog(tp->tv_usec);
+    tp->tv_sec = htog(tp->tv_sec, OS::byteOrder);
+    tp->tv_usec = htog(tp->tv_usec, OS::byteOrder);
 
     tp.copyOut(tc->getVirtProxy());
 
@@ -2106,8 +2109,8 @@ utimesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
 
     struct timeval hostTimeval[2];
     for (int i = 0; i < 2; ++i) {
-        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
-        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
+        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec, OS::byteOrder);
+        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec, OS::byteOrder);
     }
 
     // Adjust path for cwd and redirection
@@ -2245,8 +2248,8 @@ getrusageFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
     switch (who) {
       case OS::TGT_RUSAGE_SELF:
         getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
-        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
-        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
+        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec, OS::byteOrder);
+        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec, OS::byteOrder);
         break;
 
       case OS::TGT_RUSAGE_CHILDREN:
@@ -2282,7 +2285,7 @@ timesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
     bufp->tms_cstime = 0;
 
     // Convert to host endianness
-    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
+    bufp->tms_utime = htog(bufp->tms_utime, OS::byteOrder);
 
     // Write back
     bufp.copyOut(tc->getVirtProxy());
@@ -2305,7 +2308,7 @@ timeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
     Addr taddr = (Addr)process->getSyscallArg(tc, index);
     if (taddr != 0) {
         typename OS::time_t t = sec;
-        t = TheISA::htog(t);
+        t = htog(t, OS::byteOrder);
         PortProxy &p = tc->getVirtProxy();
         p.writeBlob(taddr, &t, (int)sizeof(typename OS::time_t));
     }