linux.hh revision 2450
1/* 2 * Copyright (c) 2004-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#ifndef __LINUX_HH__ 30#define __LINUX_HH__ 31#include "config/full_system.hh" 32 33#if FULL_SYSTEM 34 35class Linux {}; 36 37#else //!FULL_SYSTEM 38 39#include <dirent.h> 40#include <errno.h> 41#include <fcntl.h> // for host open() flags 42#include <string.h> // for memset() 43#include <sys/stat.h> 44#include <sys/types.h> 45#include <unistd.h> 46 47#include "sim/syscall_emul.hh" 48 49class TranslatingPort; 50 51/// 52/// This class encapsulates the types, structures, constants, 53/// functions, and syscall-number mappings specific to the Alpha Linux 54/// syscall interface. 55/// 56class Linux { 57 58 public: 59 60 //@{ 61 /// Basic Linux types. 62 typedef uint64_t size_t; 63 typedef uint64_t off_t; 64 typedef int64_t time_t; 65 typedef uint32_t uid_t; 66 typedef uint32_t gid_t; 67 //@} 68 69#if BSD_HOST 70 typedef struct stat hst_stat; 71 typedef struct stat hst_stat64; 72#else 73 typedef struct stat hst_stat ; 74 typedef struct stat64 hst_stat64; 75#endif 76 77 78 //@{ 79 /// open(2) flag values. 80 static const int TGT_O_RDONLY = 00000000; //!< O_RDONLY 81 static const int TGT_O_WRONLY = 00000001; //!< O_WRONLY 82 static const int TGT_O_RDWR = 00000002; //!< O_RDWR 83 static const int TGT_O_NONBLOCK = 00000004; //!< O_NONBLOCK 84 static const int TGT_O_APPEND = 00000010; //!< O_APPEND 85 static const int TGT_O_CREAT = 00001000; //!< O_CREAT 86 static const int TGT_O_TRUNC = 00002000; //!< O_TRUNC 87 static const int TGT_O_EXCL = 00004000; //!< O_EXCL 88 static const int TGT_O_NOCTTY = 00010000; //!< O_NOCTTY 89 static const int TGT_O_SYNC = 00040000; //!< O_SYNC 90 static const int TGT_O_DRD = 00100000; //!< O_DRD 91 static const int TGT_O_DIRECTIO = 00200000; //!< O_DIRECTIO 92 static const int TGT_O_CACHE = 00400000; //!< O_CACHE 93 static const int TGT_O_DSYNC = 02000000; //!< O_DSYNC 94 static const int TGT_O_RSYNC = 04000000; //!< O_RSYNC 95 //@} 96 97 /// This table maps the target open() flags to the corresponding 98 /// host open() flags. 99 static OpenFlagTransTable openFlagTable[]; 100 101 /// Number of entries in openFlagTable[]. 102 static const int NUM_OPEN_FLAGS; 103 104 /// Stat buffer. Note that we can't call it 'stat' since that 105 /// gets #defined to something else on some systems. 106 struct tgt_stat { 107 uint32_t st_dev; //!< device 108 uint32_t st_ino; //!< inode 109 uint32_t st_mode; //!< mode 110 uint32_t st_nlink; //!< link count 111 uint32_t st_uid; //!< owner's user ID 112 uint32_t st_gid; //!< owner's group ID 113 uint32_t st_rdev; //!< device number 114 int32_t _pad1; //!< for alignment 115 int64_t st_size; //!< file size in bytes 116 uint64_t st_atimeX; //!< time of last access 117 uint64_t st_mtimeX; //!< time of last modification 118 uint64_t st_ctimeX; //!< time of last status change 119 uint32_t st_blksize; //!< optimal I/O block size 120 int32_t st_blocks; //!< number of blocks allocated 121 uint32_t st_flags; //!< flags 122 uint32_t st_gen; //!< unknown 123 }; 124 125 // same for stat64 126 struct tgt_stat64 { 127 uint64_t st_dev; 128 uint64_t st_ino; 129 uint64_t st_rdev; 130 int64_t st_size; 131 uint64_t st_blocks; 132 133 uint32_t st_mode; 134 uint32_t st_uid; 135 uint32_t st_gid; 136 uint32_t st_blksize; 137 uint32_t st_nlink; 138 uint32_t __pad0; 139 140 uint64_t tgt_st_atime; 141 uint64_t st_atime_nsec; 142 uint64_t tgt_st_mtime; 143 uint64_t st_mtime_nsec; 144 uint64_t tgt_st_ctime; 145 uint64_t st_ctime_nsec; 146 int64_t ___unused[3]; 147 }; 148 149 /// Length of strings in struct utsname (plus 1 for null char). 150 static const int _SYS_NMLN = 65; 151 152 /// Interface struct for uname(). 153 struct utsname { 154 char sysname[_SYS_NMLN]; //!< System name. 155 char nodename[_SYS_NMLN]; //!< Node name. 156 char release[_SYS_NMLN]; //!< OS release. 157 char version[_SYS_NMLN]; //!< OS version. 158 char machine[_SYS_NMLN]; //!< Machine type. 159 }; 160 161 162 //@{ 163 /// ioctl() command codes. 164 static const unsigned TIOCGETP = 0x40067408; 165 static const unsigned TIOCSETP = 0x80067409; 166 static const unsigned TIOCSETN = 0x8006740a; 167 static const unsigned TIOCSETC = 0x80067411; 168 static const unsigned TIOCGETC = 0x40067412; 169 static const unsigned FIONREAD = 0x4004667f; 170 static const unsigned TIOCISATTY = 0x2000745e; 171 static const unsigned TIOCGETS = 0x402c7413; 172 static const unsigned TIOCGETA = 0x40127417; 173 //@} 174 175 /// Resource enumeration for getrlimit(). 176 enum rlimit_resources { 177 TGT_RLIMIT_CPU = 0, 178 TGT_RLIMIT_FSIZE = 1, 179 TGT_RLIMIT_DATA = 2, 180 TGT_RLIMIT_STACK = 3, 181 TGT_RLIMIT_CORE = 4, 182 TGT_RLIMIT_RSS = 5, 183 TGT_RLIMIT_NOFILE = 6, 184 TGT_RLIMIT_AS = 7, 185 TGT_RLIMIT_VMEM = 7, 186 TGT_RLIMIT_NPROC = 8, 187 TGT_RLIMIT_MEMLOCK = 9, 188 TGT_RLIMIT_LOCKS = 10 189 }; 190 191 /// Limit struct for getrlimit/setrlimit. 192 struct rlimit { 193 uint64_t rlim_cur; //!< soft limit 194 uint64_t rlim_max; //!< hard limit 195 }; 196 197 198 /// For mmap(). 199 static const unsigned TGT_MAP_ANONYMOUS = 0x10; 200 201 /// For gettimeofday(). 202 struct timeval { 203 int64_t tv_sec; //!< seconds 204 int64_t tv_usec; //!< microseconds 205 }; 206 207 // For writev/readv 208 struct tgt_iovec { 209 uint64_t iov_base; // void * 210 uint64_t iov_len; 211 }; 212 213 //@{ 214 /// For getrusage(). 215 static const int TGT_RUSAGE_SELF = 0; 216 static const int TGT_RUSAGE_CHILDREN = -1; 217 static const int TGT_RUSAGE_BOTH = -2; 218 //@} 219 220 /// For getrusage(). 221 struct rusage { 222 struct timeval ru_utime; //!< user time used 223 struct timeval ru_stime; //!< system time used 224 int64_t ru_maxrss; //!< max rss 225 int64_t ru_ixrss; //!< integral shared memory size 226 int64_t ru_idrss; //!< integral unshared data " 227 int64_t ru_isrss; //!< integral unshared stack " 228 int64_t ru_minflt; //!< page reclaims - total vmfaults 229 int64_t ru_majflt; //!< page faults 230 int64_t ru_nswap; //!< swaps 231 int64_t ru_inblock; //!< block input operations 232 int64_t ru_oublock; //!< block output operations 233 int64_t ru_msgsnd; //!< messages sent 234 int64_t ru_msgrcv; //!< messages received 235 int64_t ru_nsignals; //!< signals received 236 int64_t ru_nvcsw; //!< voluntary context switches 237 int64_t ru_nivcsw; //!< involuntary " 238 }; 239 240 /// Helper function to convert a host stat buffer to a target stat 241 /// buffer. Also copies the target buffer out to the simulated 242 /// memory space. Used by stat(), fstat(), and lstat(). 243#if !BSD_HOST 244 static void 245 copyOutStatBuf(TranslatingPort *mem, Addr addr, hst_stat *host) 246 { 247 using namespace TheISA; 248 249 TypedBufferArg<Linux::tgt_stat> tgt(addr); 250 251 tgt->st_dev = htog(host->st_dev); 252 tgt->st_ino = htog(host->st_ino); 253 tgt->st_mode = htog(host->st_mode); 254 tgt->st_nlink = htog(host->st_nlink); 255 tgt->st_uid = htog(host->st_uid); 256 tgt->st_gid = htog(host->st_gid); 257 tgt->st_rdev = htog(host->st_rdev); 258 tgt->st_size = htog(host->st_size); 259 tgt->st_atimeX = htog(host->st_atime); 260 tgt->st_mtimeX = htog(host->st_mtime); 261 tgt->st_ctimeX = htog(host->st_ctime); 262 tgt->st_blksize = htog(host->st_blksize); 263 tgt->st_blocks = htog(host->st_blocks); 264 265 tgt.copyOut(mem); 266 } 267#else 268 // Third version for bsd systems which no longer have any support for 269 // the old stat() call and stat() is actually a stat64() 270 static void 271 copyOutStatBuf(TranslatingPort *mem, Addr addr, hst_stat64 *host) 272 { 273 using namespace TheISA; 274 275 TypedBufferArg<Linux::tgt_stat> tgt(addr); 276 277 tgt->st_dev = htog(host->st_dev); 278 tgt->st_ino = htog(host->st_ino); 279 tgt->st_mode = htog(host->st_mode); 280 tgt->st_nlink = htog(host->st_nlink); 281 tgt->st_uid = htog(host->st_uid); 282 tgt->st_gid = htog(host->st_gid); 283 tgt->st_rdev = htog(host->st_rdev); 284 tgt->st_size = htog(host->st_size); 285 tgt->st_atimeX = htog(host->st_atime); 286 tgt->st_mtimeX = htog(host->st_mtime); 287 tgt->st_ctimeX = htog(host->st_ctime); 288 tgt->st_blksize = htog(host->st_blksize); 289 tgt->st_blocks = htog(host->st_blocks); 290 291 tgt.copyOut(mem); 292 } 293#endif 294 295 296 // Same for stat64 297 static void 298 copyOutStat64Buf(TranslatingPort *mem, int fd, Addr addr, hst_stat64 *host) 299 { 300 using namespace TheISA; 301 302 TypedBufferArg<Linux::tgt_stat64> tgt(addr); 303 304 // fd == 1 checks are because libc does some checks 305 // that the stdout is interactive vs. a file 306 // this makes it work on non-linux systems 307 if (fd == 1) 308 tgt->st_dev = htog((uint64_t)0xA); 309 else 310 tgt->st_dev = htog((uint64_t)host->st_dev); 311 // XXX What about STAT64_HAS_BROKEN_ST_INO ??? 312 tgt->st_ino = htog((uint64_t)host->st_ino); 313 if (fd == 1) 314 tgt->st_rdev = htog((uint64_t)0x880d); 315 else 316 tgt->st_rdev = htog((uint64_t)host->st_rdev); 317 tgt->st_size = htog((int64_t)host->st_size); 318 tgt->st_blocks = htog((uint64_t)host->st_blocks); 319 320 if (fd == 1) 321 tgt->st_mode = htog((uint32_t)0x2190); 322 else 323 tgt->st_mode = htog((uint32_t)host->st_mode); 324 tgt->st_uid = htog((uint32_t)host->st_uid); 325 tgt->st_gid = htog((uint32_t)host->st_gid); 326 tgt->st_blksize = htog((uint32_t)host->st_blksize); 327 tgt->st_nlink = htog((uint32_t)host->st_nlink); 328 tgt->tgt_st_atime = htog((uint64_t)host->st_atime); 329 tgt->tgt_st_mtime = htog((uint64_t)host->st_mtime); 330 tgt->tgt_st_ctime = htog((uint64_t)host->st_ctime); 331#if defined(STAT_HAVE_NSEC) 332 tgt->st_atime_nsec = htog(host->st_atime_nsec); 333 tgt->st_mtime_nsec = htog(host->st_mtime_nsec); 334 tgt->st_ctime_nsec = htog(host->st_ctime_nsec); 335#else 336 tgt->st_atime_nsec = 0; 337 tgt->st_mtime_nsec = 0; 338 tgt->st_ctime_nsec = 0; 339#endif 340 341 tgt.copyOut(mem); 342 } 343 344}; // class Linux 345 346 347#endif // FULL_SYSTEM 348 349#endif // __LINUX_HH__ 350