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