linux.hh revision 11851
15222Sksewell@umich.edu/*
25268Sksewell@umich.edu * Copyright (c) 2004-2009 The Regents of The University of Michigan
35254Sksewell@umich.edu * All rights reserved.
45222Sksewell@umich.edu *
55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are
75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
145254Sksewell@umich.edu * this software without specific prior written permission.
155222Sksewell@umich.edu *
165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275222Sksewell@umich.edu *
285254Sksewell@umich.edu * Authors: Ali Saidi
295254Sksewell@umich.edu */
305254Sksewell@umich.edu
315222Sksewell@umich.edu#ifndef __LINUX_HH__
325222Sksewell@umich.edu#define __LINUX_HH__
335222Sksewell@umich.edu
345222Sksewell@umich.edu#include "base/types.hh"
355222Sksewell@umich.edu
365222Sksewell@umich.edu#include <string>
375222Sksewell@umich.edu
385222Sksewell@umich.edu#include "kern/operatingsystem.hh"
395222Sksewell@umich.edu#include "sim/process.hh"
405222Sksewell@umich.edu
415222Sksewell@umich.educlass ThreadContext;
425222Sksewell@umich.edu
435222Sksewell@umich.edu///
448229Snate@binkert.org/// This class encapsulates the types, structures, constants,
455222Sksewell@umich.edu/// functions, and syscall-number mappings specific to the Alpha Linux
468229Snate@binkert.org/// syscall interface.
475222Sksewell@umich.edu///
488229Snate@binkert.orgclass Linux : public OperatingSystem
495222Sksewell@umich.edu{
508775Sgblack@eecs.umich.edu
515222Sksewell@umich.edu  public:
528229Snate@binkert.org
535222Sksewell@umich.edu    //@{
545222Sksewell@umich.edu    /// Basic Linux types.
555222Sksewell@umich.edu    typedef uint64_t size_t;
565222Sksewell@umich.edu    typedef uint64_t off_t;
575222Sksewell@umich.edu    typedef int64_t time_t;
585222Sksewell@umich.edu    typedef int64_t clock_t;
595222Sksewell@umich.edu    typedef uint32_t uid_t;
605222Sksewell@umich.edu    typedef uint32_t gid_t;
615222Sksewell@umich.edu    //@}
625222Sksewell@umich.edu
635222Sksewell@umich.edu    /// Stat buffer.  Note that we can't call it 'stat' since that
645222Sksewell@umich.edu    /// gets #defined to something else on some systems. This type
655222Sksewell@umich.edu    /// can be specialized by architecture specific "Linux" classes
665222Sksewell@umich.edu    typedef struct {
675222Sksewell@umich.edu        uint32_t        st_dev;         //!< device
685222Sksewell@umich.edu        uint32_t        st_ino;         //!< inode
695222Sksewell@umich.edu        uint32_t        st_mode;        //!< mode
705222Sksewell@umich.edu        uint32_t        st_nlink;       //!< link count
715222Sksewell@umich.edu        uint32_t        st_uid;         //!< owner's user ID
725222Sksewell@umich.edu        uint32_t        st_gid;         //!< owner's group ID
735222Sksewell@umich.edu        uint32_t        st_rdev;        //!< device number
745222Sksewell@umich.edu        int32_t         _pad1;          //!< for alignment
755222Sksewell@umich.edu        int64_t         st_size;        //!< file size in bytes
765222Sksewell@umich.edu        uint64_t        st_atimeX;      //!< time of last access
775222Sksewell@umich.edu        uint64_t        st_mtimeX;      //!< time of last modification
785222Sksewell@umich.edu        uint64_t        st_ctimeX;      //!< time of last status change
795222Sksewell@umich.edu        uint32_t        st_blksize;     //!< optimal I/O block size
808775Sgblack@eecs.umich.edu        int32_t         st_blocks;      //!< number of blocks allocated
815222Sksewell@umich.edu        uint32_t        st_flags;       //!< flags
825222Sksewell@umich.edu        uint32_t        st_gen;         //!< unknown
835222Sksewell@umich.edu    } tgt_stat;
845222Sksewell@umich.edu
855222Sksewell@umich.edu    // same for stat64
865222Sksewell@umich.edu    typedef struct {
875222Sksewell@umich.edu        uint64_t        st_dev;
885222Sksewell@umich.edu        uint64_t        st_ino;
898775Sgblack@eecs.umich.edu        uint64_t        st_rdev;
905222Sksewell@umich.edu        int64_t         st_size;
915222Sksewell@umich.edu        uint64_t        st_blocks;
925222Sksewell@umich.edu
935222Sksewell@umich.edu        uint32_t        st_mode;
945222Sksewell@umich.edu        uint32_t        st_uid;
955222Sksewell@umich.edu        uint32_t        st_gid;
965222Sksewell@umich.edu        uint32_t        st_blksize;
975222Sksewell@umich.edu        uint32_t        st_nlink;
985222Sksewell@umich.edu        uint32_t        __pad0;
998775Sgblack@eecs.umich.edu
1005222Sksewell@umich.edu        uint64_t        st_atimeX;
1015222Sksewell@umich.edu        uint64_t        st_atime_nsec;
1025222Sksewell@umich.edu        uint64_t        st_mtimeX;
1035222Sksewell@umich.edu        uint64_t        st_mtime_nsec;
1045222Sksewell@umich.edu        uint64_t        st_ctimeX;
1055222Sksewell@umich.edu        uint64_t        st_ctime_nsec;
1065222Sksewell@umich.edu        int64_t         ___unused[3];
1075222Sksewell@umich.edu    } tgt_stat64;
1085222Sksewell@umich.edu
1095222Sksewell@umich.edu    /// Length of strings in struct utsname (plus 1 for null char).
1105222Sksewell@umich.edu    static const int _SYS_NMLN = 65;
1115222Sksewell@umich.edu
1125222Sksewell@umich.edu    /// Interface struct for uname().
1135222Sksewell@umich.edu    struct utsname {
1145222Sksewell@umich.edu        char sysname[_SYS_NMLN];        //!< System name.
1155222Sksewell@umich.edu        char nodename[_SYS_NMLN];       //!< Node name.
1165222Sksewell@umich.edu        char release[_SYS_NMLN];        //!< OS release.
1175222Sksewell@umich.edu        char version[_SYS_NMLN];        //!< OS version.
1185222Sksewell@umich.edu        char machine[_SYS_NMLN];        //!< Machine type.
1195222Sksewell@umich.edu    };
1205222Sksewell@umich.edu
1215222Sksewell@umich.edu    /// Limit struct for getrlimit/setrlimit.
1225222Sksewell@umich.edu    struct rlimit {
1235222Sksewell@umich.edu        uint64_t  rlim_cur;     //!< soft limit
1245222Sksewell@umich.edu        uint64_t  rlim_max;     //!< hard limit
1255222Sksewell@umich.edu    };
1265222Sksewell@umich.edu
1275222Sksewell@umich.edu    /// For gettimeofday().
1285222Sksewell@umich.edu    struct timeval {
1295222Sksewell@umich.edu        int64_t tv_sec;         //!< seconds
1305222Sksewell@umich.edu        int64_t tv_usec;        //!< microseconds
1315222Sksewell@umich.edu    };
1325222Sksewell@umich.edu
1335222Sksewell@umich.edu    /// For clock_gettime().
1345222Sksewell@umich.edu    struct timespec {
1355222Sksewell@umich.edu        time_t tv_sec;         //!< seconds
1365222Sksewell@umich.edu        int64_t tv_nsec;        //!< nanoseconds
1375222Sksewell@umich.edu    };
1385222Sksewell@umich.edu
1395222Sksewell@umich.edu    /// Clock ticks per second, for times().
1405222Sksewell@umich.edu    static const int M5_SC_CLK_TCK = 100;
1415222Sksewell@umich.edu
1425222Sksewell@umich.edu    /// For times().
1435222Sksewell@umich.edu    struct tms {
1445222Sksewell@umich.edu        int64_t tms_utime;      //!< user time
1455222Sksewell@umich.edu        int64_t tms_stime;      //!< system time
1465222Sksewell@umich.edu        int64_t tms_cutime;     //!< user time of children
1475222Sksewell@umich.edu        int64_t tms_cstime;     //!< system time of children
1485222Sksewell@umich.edu    };
1495222Sksewell@umich.edu
1505222Sksewell@umich.edu    // For writev/readv
1515222Sksewell@umich.edu    struct tgt_iovec {
1525222Sksewell@umich.edu        uint64_t iov_base; // void *
1535222Sksewell@umich.edu        uint64_t iov_len;
1545222Sksewell@umich.edu    };
1555222Sksewell@umich.edu
1565222Sksewell@umich.edu    //@{
1578741Sgblack@eecs.umich.edu    /// ioctl() command codes.
1585222Sksewell@umich.edu    static const unsigned TGT_TCGETS     = 0x5401;
1595222Sksewell@umich.edu    static const unsigned TGT_TCGETA     = 0x5405;
1605222Sksewell@umich.edu    static const unsigned TGT_TCSETAW    = 0x5407;
1615222Sksewell@umich.edu    static const unsigned TGT_FIONREAD   = 0x541B;
1625222Sksewell@umich.edu    //@}
1635222Sksewell@umich.edu
1645222Sksewell@umich.edu    /// Return true for the ioctl codes for which we return ENOTTY
1655222Sksewell@umich.edu    /// *without* printing a warning, since we know that ENOTTY is the
1665222Sksewell@umich.edu    /// correct thing to return (and not just a sign that we don't
1675222Sksewell@umich.edu    /// recognize the ioctl code.
1685222Sksewell@umich.edu    static bool
1695222Sksewell@umich.edu    isTtyReq(unsigned req)
1705222Sksewell@umich.edu    {
1715222Sksewell@umich.edu        switch (req) {
1725222Sksewell@umich.edu          case TGT_FIONREAD:
1735222Sksewell@umich.edu          case TGT_TCSETAW:
1745222Sksewell@umich.edu          case TGT_TCGETS:
1755222Sksewell@umich.edu          case TGT_TCGETA:
1765222Sksewell@umich.edu            return true;
1775222Sksewell@umich.edu          default:
1785222Sksewell@umich.edu            return false;
1795222Sksewell@umich.edu        }
1805222Sksewell@umich.edu    }
1815222Sksewell@umich.edu
1825222Sksewell@umich.edu
183    /// Resource constants for getrlimit().
184    static const unsigned TGT_RLIMIT_CPU = 0;
185    static const unsigned TGT_RLIMIT_FSIZE = 1;
186    static const unsigned TGT_RLIMIT_DATA = 2;
187    static const unsigned TGT_RLIMIT_STACK = 3;
188    static const unsigned TGT_RLIMIT_CORE = 4;
189    static const unsigned TGT_RLIMIT_RSS = 5;
190    static const unsigned TGT_RLIMIT_NPROC = 6;
191    static const unsigned TGT_RLIMIT_NOFILE = 7;
192    static const unsigned TGT_RLIMIT_MEMLOCK = 8;
193    static const unsigned TGT_RLIMIT_AS = 9;
194    static const unsigned TGT_RLIMIT_LOCKS = 10;
195    static const unsigned TGT_RLIMIT_SIGPENDING = 11;
196    static const unsigned TGT_RLIMIT_MSGQUEUE = 12;
197    static const unsigned TGT_RLIMIT_NICE = 13;
198    static const unsigned TGT_RLIMIT_RTPRIO = 14;
199    static const unsigned TGT_RLIMIT_RTTIME = 15;
200    static const unsigned TGT_RLIM_NLIMITS = 16;
201
202    /// For getrusage().
203    static const int TGT_RUSAGE_SELF     = 0;
204    static const int TGT_RUSAGE_CHILDREN = -1;
205    static const int TGT_RUSAGE_BOTH     = -2;
206
207    struct rusage {
208        struct timeval ru_utime;        //!< user time used
209        struct timeval ru_stime;        //!< system time used
210        int64_t ru_maxrss;              //!< max rss
211        int64_t ru_ixrss;               //!< integral shared memory size
212        int64_t ru_idrss;               //!< integral unshared data "
213        int64_t ru_isrss;               //!< integral unshared stack "
214        int64_t ru_minflt;              //!< page reclaims - total vmfaults
215        int64_t ru_majflt;              //!< page faults
216        int64_t ru_nswap;               //!< swaps
217        int64_t ru_inblock;             //!< block input operations
218        int64_t ru_oublock;             //!< block output operations
219        int64_t ru_msgsnd;              //!< messages sent
220        int64_t ru_msgrcv;              //!< messages received
221        int64_t ru_nsignals;            //!< signals received
222        int64_t ru_nvcsw;               //!< voluntary context switches
223        int64_t ru_nivcsw;              //!< involuntary "
224    };
225
226    static int openSpecialFile(std::string path, Process *process,
227                               ThreadContext *tc);
228    static std::string procMeminfo(Process *process, ThreadContext *tc);
229
230    // For futex system call
231    static const unsigned TGT_FUTEX_WAIT  = 0;
232    static const unsigned TGT_FUTEX_WAKE  = 1;
233    static const unsigned TGT_EAGAIN      = 11;
234    static const unsigned TGT_EWOULDBLOCK = TGT_EAGAIN;
235    static const unsigned TGT_FUTEX_PRIVATE_FLAG = 128;
236
237    // for *at syscalls
238    static const int TGT_AT_FDCWD   = -100;
239
240    // for MREMAP
241    static const unsigned TGT_MREMAP_MAYMOVE    = 0x1;
242    static const unsigned TGT_MREMAP_FIXED      = 0x2;
243
244    static const unsigned TGT_CLONE_VM              = 0x00000100;
245    static const unsigned TGT_CLONE_FS              = 0x00000200;
246    static const unsigned TGT_CLONE_FILES           = 0x00000400;
247    static const unsigned TGT_CLONE_SIGHAND         = 0x00000800;
248    static const unsigned TGT_CLONE_PTRACE          = 0x00002000;
249    static const unsigned TGT_CLONE_VFORK           = 0x00004000;
250    static const unsigned TGT_CLONE_PARENT          = 0x00008000;
251    static const unsigned TGT_CLONE_THREAD          = 0x00010000;
252    static const unsigned TGT_CLONE_NEWNS           = 0x00020000;
253    static const unsigned TGT_CLONE_SYSVSEM         = 0x00040000;
254    static const unsigned TGT_CLONE_SETTLS          = 0x00080000;
255    static const unsigned TGT_CLONE_PARENT_SETTID   = 0x00100000;
256    static const unsigned TGT_CLONE_CHILD_CLEARTID  = 0x00200000;
257    static const unsigned TGT_CLONE_DETACHED        = 0x00400000;
258    static const unsigned TGT_CLONE_UNTRACED        = 0x00800000;
259    static const unsigned TGT_CLONE_CHILD_SETTID    = 0x01000000;
260    static const unsigned TGT_CLONE_NEWUTS          = 0x04000000;
261    static const unsigned TGT_CLONE_NEWIPC          = 0x08000000;
262    static const unsigned TGT_CLONE_NEWUSER         = 0x10000000;
263    static const unsigned TGT_CLONE_NEWPID          = 0x20000000;
264    static const unsigned TGT_CLONE_NEWNET          = 0x40000000;
265    static const unsigned TGT_CLONE_IO              = 0x80000000;
266};  // class Linux
267
268#endif // __LINUX_HH__
269