linux.hh revision 11851
14202Sbinkertn@umich.edu/*
24202Sbinkertn@umich.edu * Copyright (c) 2004-2009 The Regents of The University of Michigan
34202Sbinkertn@umich.edu * All rights reserved.
44202Sbinkertn@umich.edu *
54202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144202Sbinkertn@umich.edu * this software without specific prior written permission.
154202Sbinkertn@umich.edu *
164202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214202Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224202Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234202Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244202Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254202Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264202Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274202Sbinkertn@umich.edu *
284202Sbinkertn@umich.edu * Authors: Ali Saidi
294202Sbinkertn@umich.edu */
304202Sbinkertn@umich.edu
314202Sbinkertn@umich.edu#ifndef __LINUX_HH__
324202Sbinkertn@umich.edu#define __LINUX_HH__
334486Sbinkertn@umich.edu
344486Sbinkertn@umich.edu#include "base/types.hh"
354776Sgblack@eecs.umich.edu
364486Sbinkertn@umich.edu#include <string>
374202Sbinkertn@umich.edu
384202Sbinkertn@umich.edu#include "kern/operatingsystem.hh"
394202Sbinkertn@umich.edu#include "sim/process.hh"
404202Sbinkertn@umich.edu
414202Sbinkertn@umich.educlass ThreadContext;
424202Sbinkertn@umich.edu
434202Sbinkertn@umich.edu///
444202Sbinkertn@umich.edu/// This class encapsulates the types, structures, constants,
454202Sbinkertn@umich.edu/// functions, and syscall-number mappings specific to the Alpha Linux
464202Sbinkertn@umich.edu/// syscall interface.
474202Sbinkertn@umich.edu///
484202Sbinkertn@umich.educlass Linux : public OperatingSystem
494202Sbinkertn@umich.edu{
504202Sbinkertn@umich.edu
514202Sbinkertn@umich.edu  public:
524202Sbinkertn@umich.edu
534826Ssaidi@eecs.umich.edu    //@{
544202Sbinkertn@umich.edu    /// Basic Linux types.
554202Sbinkertn@umich.edu    typedef uint64_t size_t;
564486Sbinkertn@umich.edu    typedef uint64_t off_t;
574486Sbinkertn@umich.edu    typedef int64_t time_t;
584202Sbinkertn@umich.edu    typedef int64_t clock_t;
594202Sbinkertn@umich.edu    typedef uint32_t uid_t;
60    typedef uint32_t gid_t;
61    //@}
62
63    /// Stat buffer.  Note that we can't call it 'stat' since that
64    /// gets #defined to something else on some systems. This type
65    /// can be specialized by architecture specific "Linux" classes
66    typedef struct {
67        uint32_t        st_dev;         //!< device
68        uint32_t        st_ino;         //!< inode
69        uint32_t        st_mode;        //!< mode
70        uint32_t        st_nlink;       //!< link count
71        uint32_t        st_uid;         //!< owner's user ID
72        uint32_t        st_gid;         //!< owner's group ID
73        uint32_t        st_rdev;        //!< device number
74        int32_t         _pad1;          //!< for alignment
75        int64_t         st_size;        //!< file size in bytes
76        uint64_t        st_atimeX;      //!< time of last access
77        uint64_t        st_mtimeX;      //!< time of last modification
78        uint64_t        st_ctimeX;      //!< time of last status change
79        uint32_t        st_blksize;     //!< optimal I/O block size
80        int32_t         st_blocks;      //!< number of blocks allocated
81        uint32_t        st_flags;       //!< flags
82        uint32_t        st_gen;         //!< unknown
83    } tgt_stat;
84
85    // same for stat64
86    typedef struct {
87        uint64_t        st_dev;
88        uint64_t        st_ino;
89        uint64_t        st_rdev;
90        int64_t         st_size;
91        uint64_t        st_blocks;
92
93        uint32_t        st_mode;
94        uint32_t        st_uid;
95        uint32_t        st_gid;
96        uint32_t        st_blksize;
97        uint32_t        st_nlink;
98        uint32_t        __pad0;
99
100        uint64_t        st_atimeX;
101        uint64_t        st_atime_nsec;
102        uint64_t        st_mtimeX;
103        uint64_t        st_mtime_nsec;
104        uint64_t        st_ctimeX;
105        uint64_t        st_ctime_nsec;
106        int64_t         ___unused[3];
107    } tgt_stat64;
108
109    /// Length of strings in struct utsname (plus 1 for null char).
110    static const int _SYS_NMLN = 65;
111
112    /// Interface struct for uname().
113    struct utsname {
114        char sysname[_SYS_NMLN];        //!< System name.
115        char nodename[_SYS_NMLN];       //!< Node name.
116        char release[_SYS_NMLN];        //!< OS release.
117        char version[_SYS_NMLN];        //!< OS version.
118        char machine[_SYS_NMLN];        //!< Machine type.
119    };
120
121    /// Limit struct for getrlimit/setrlimit.
122    struct rlimit {
123        uint64_t  rlim_cur;     //!< soft limit
124        uint64_t  rlim_max;     //!< hard limit
125    };
126
127    /// For gettimeofday().
128    struct timeval {
129        int64_t tv_sec;         //!< seconds
130        int64_t tv_usec;        //!< microseconds
131    };
132
133    /// For clock_gettime().
134    struct timespec {
135        time_t tv_sec;         //!< seconds
136        int64_t tv_nsec;        //!< nanoseconds
137    };
138
139    /// Clock ticks per second, for times().
140    static const int M5_SC_CLK_TCK = 100;
141
142    /// For times().
143    struct tms {
144        int64_t tms_utime;      //!< user time
145        int64_t tms_stime;      //!< system time
146        int64_t tms_cutime;     //!< user time of children
147        int64_t tms_cstime;     //!< system time of children
148    };
149
150    // For writev/readv
151    struct tgt_iovec {
152        uint64_t iov_base; // void *
153        uint64_t iov_len;
154    };
155
156    //@{
157    /// ioctl() command codes.
158    static const unsigned TGT_TCGETS     = 0x5401;
159    static const unsigned TGT_TCGETA     = 0x5405;
160    static const unsigned TGT_TCSETAW    = 0x5407;
161    static const unsigned TGT_FIONREAD   = 0x541B;
162    //@}
163
164    /// Return true for the ioctl codes for which we return ENOTTY
165    /// *without* printing a warning, since we know that ENOTTY is the
166    /// correct thing to return (and not just a sign that we don't
167    /// recognize the ioctl code.
168    static bool
169    isTtyReq(unsigned req)
170    {
171        switch (req) {
172          case TGT_FIONREAD:
173          case TGT_TCSETAW:
174          case TGT_TCGETS:
175          case TGT_TCGETA:
176            return true;
177          default:
178            return false;
179        }
180    }
181
182
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