1/*
2 * Copyright (c) 2004-2009 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 * Authors: Ali Saidi
29 */
30
31#ifndef __LINUX_HH__
32#define __LINUX_HH__
33
34#include "base/types.hh"
35
36#include <string>
37
38#include "kern/operatingsystem.hh"
39#include "sim/process.hh"
40
41class ThreadContext;
42
43///
44/// This class encapsulates the types, structures, constants,
45/// functions, and syscall-number mappings specific to the Alpha Linux
46/// syscall interface.
47///
48class Linux : public OperatingSystem
49{
50
51  public:
52
53    //@{
54    /// Basic Linux types.
55    typedef uint64_t size_t;
56    typedef uint64_t off_t;
57    typedef int64_t time_t;
58    typedef int64_t clock_t;
59    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    // For select().
157    // linux-3.14-src/include/uapi/linux/posix_types.h
158    struct fd_set{
159#ifndef LINUX__FD_SETSIZE
160#define LINUX__FD_SETSIZE 1024
161        unsigned long fds_bits[LINUX__FD_SETSIZE / (8 * sizeof(long))];
162#endif
163    };
164
165    //@{
166    /// ioctl() command codes.
167    static const unsigned TGT_TCGETS     = 0x5401;
168    static const unsigned TGT_TCGETA     = 0x5405;
169    static const unsigned TGT_TCSETAW    = 0x5407;
170    static const unsigned TGT_FIONREAD   = 0x541B;
171    //@}
172
173    /// Return true for the ioctl codes for which we return ENOTTY
174    /// *without* printing a warning, since we know that ENOTTY is the
175    /// correct thing to return (and not just a sign that we don't
176    /// recognize the ioctl code.
177    static bool
178    isTtyReq(unsigned req)
179    {
180        switch (req) {
181          case TGT_FIONREAD:
182          case TGT_TCSETAW:
183          case TGT_TCGETS:
184          case TGT_TCGETA:
185            return true;
186          default:
187            return false;
188        }
189    }
190
191
192    /// Resource constants for getrlimit().
193    static const unsigned TGT_RLIMIT_CPU = 0;
194    static const unsigned TGT_RLIMIT_FSIZE = 1;
195    static const unsigned TGT_RLIMIT_DATA = 2;
196    static const unsigned TGT_RLIMIT_STACK = 3;
197    static const unsigned TGT_RLIMIT_CORE = 4;
198    static const unsigned TGT_RLIMIT_RSS = 5;
199    static const unsigned TGT_RLIMIT_NPROC = 6;
200    static const unsigned TGT_RLIMIT_NOFILE = 7;
201    static const unsigned TGT_RLIMIT_MEMLOCK = 8;
202    static const unsigned TGT_RLIMIT_AS = 9;
203    static const unsigned TGT_RLIMIT_LOCKS = 10;
204    static const unsigned TGT_RLIMIT_SIGPENDING = 11;
205    static const unsigned TGT_RLIMIT_MSGQUEUE = 12;
206    static const unsigned TGT_RLIMIT_NICE = 13;
207    static const unsigned TGT_RLIMIT_RTPRIO = 14;
208    static const unsigned TGT_RLIMIT_RTTIME = 15;
209    static const unsigned TGT_RLIM_NLIMITS = 16;
210
211    /// For getrusage().
212    static const int TGT_RUSAGE_SELF     = 0;
213    static const int TGT_RUSAGE_CHILDREN = -1;
214    static const int TGT_RUSAGE_BOTH     = -2;
215
216    struct rusage {
217        struct timeval ru_utime;        //!< user time used
218        struct timeval ru_stime;        //!< system time used
219        int64_t ru_maxrss;              //!< max rss
220        int64_t ru_ixrss;               //!< integral shared memory size
221        int64_t ru_idrss;               //!< integral unshared data "
222        int64_t ru_isrss;               //!< integral unshared stack "
223        int64_t ru_minflt;              //!< page reclaims - total vmfaults
224        int64_t ru_majflt;              //!< page faults
225        int64_t ru_nswap;               //!< swaps
226        int64_t ru_inblock;             //!< block input operations
227        int64_t ru_oublock;             //!< block output operations
228        int64_t ru_msgsnd;              //!< messages sent
229        int64_t ru_msgrcv;              //!< messages received
230        int64_t ru_nsignals;            //!< signals received
231        int64_t ru_nvcsw;               //!< voluntary context switches
232        int64_t ru_nivcsw;              //!< involuntary "
233    };
234
235    static int openSpecialFile(std::string path, Process *process,
236                               ThreadContext *tc);
237    static std::string procMeminfo(Process *process, ThreadContext *tc);
238    static std::string etcPasswd(Process *process, ThreadContext *tc);
239    static std::string cpuOnline(Process *process, ThreadContext *tc);
240
241    // For futex system call
242    static const unsigned TGT_FUTEX_WAIT                = 0;
243    static const unsigned TGT_FUTEX_WAKE                = 1;
244    static const unsigned TGT_FUTEX_REQUEUE             = 3;
245    static const unsigned TGT_FUTEX_CMP_REQUEUE         = 4;
246    static const unsigned TGT_FUTEX_WAKE_OP             = 5;
247    static const unsigned TGT_FUTEX_WAIT_BITSET         = 9;
248    static const unsigned TGT_FUTEX_WAKE_BITSET         = 10;
249    static const unsigned TGT_EAGAIN                    = 11;
250    static const unsigned TGT_EWOULDBLOCK               = TGT_EAGAIN;
251    static const unsigned TGT_FUTEX_PRIVATE_FLAG        = 128;
252    static const unsigned TGT_FUTEX_CLOCK_REALTIME_FLAG = 256;
253    // op field of futex_wake_op operation
254    static const unsigned TGT_FUTEX_OP_SET  = 0; // uaddr2 = oparg;
255    static const unsigned TGT_FUTEX_OP_ADD  = 1; // uaddr2 += oparg;
256    static const unsigned TGT_FUTEX_OP_OR   = 2; // uaddr2 |= oparg;
257    static const unsigned TGT_FUTEX_OP_ANDN = 3; // uaddr2 &= ~oparg;
258    static const unsigned TGT_FUTEX_OP_XOR  = 4; // uaddr2 ^= oparg;
259    // Use (1 << oparg) as operand
260    static const unsigned TGT_FUTEX_OP_ARG_SHIFT = 8;
261    // cmp field of futex_wake_op operation
262    static const unsigned TGT_FUTEX_OP_CMP_EQ = 0;
263    static const unsigned TGT_FUTEX_OP_CMP_NE = 1;
264    static const unsigned TGT_FUTEX_OP_CMP_LT = 2;
265    static const unsigned TGT_FUTEX_OP_CMP_LE = 3;
266    static const unsigned TGT_FUTEX_OP_CMP_GT = 4;
267    static const unsigned TGT_FUTEX_OP_CMP_GE = 5;
268
269    // for *at syscalls
270    static const int TGT_AT_FDCWD   = -100;
271
272    // for MREMAP
273    static const unsigned TGT_MREMAP_MAYMOVE    = 0x1;
274    static const unsigned TGT_MREMAP_FIXED      = 0x2;
275
276    static const unsigned TGT_CLONE_VM              = 0x00000100;
277    static const unsigned TGT_CLONE_FS              = 0x00000200;
278    static const unsigned TGT_CLONE_FILES           = 0x00000400;
279    static const unsigned TGT_CLONE_SIGHAND         = 0x00000800;
280    static const unsigned TGT_CLONE_PTRACE          = 0x00002000;
281    static const unsigned TGT_CLONE_VFORK           = 0x00004000;
282    static const unsigned TGT_CLONE_PARENT          = 0x00008000;
283    static const unsigned TGT_CLONE_THREAD          = 0x00010000;
284    static const unsigned TGT_CLONE_NEWNS           = 0x00020000;
285    static const unsigned TGT_CLONE_SYSVSEM         = 0x00040000;
286    static const unsigned TGT_CLONE_SETTLS          = 0x00080000;
287    static const unsigned TGT_CLONE_PARENT_SETTID   = 0x00100000;
288    static const unsigned TGT_CLONE_CHILD_CLEARTID  = 0x00200000;
289    static const unsigned TGT_CLONE_DETACHED        = 0x00400000;
290    static const unsigned TGT_CLONE_UNTRACED        = 0x00800000;
291    static const unsigned TGT_CLONE_CHILD_SETTID    = 0x01000000;
292    static const unsigned TGT_CLONE_NEWUTS          = 0x04000000;
293    static const unsigned TGT_CLONE_NEWIPC          = 0x08000000;
294    static const unsigned TGT_CLONE_NEWUSER         = 0x10000000;
295    static const unsigned TGT_CLONE_NEWPID          = 0x20000000;
296    static const unsigned TGT_CLONE_NEWNET          = 0x40000000;
297    static const unsigned TGT_CLONE_IO              = 0x80000000;
298
299    // linux-3.13-src/include/uapi/linux/wait.h
300    static const unsigned TGT_WNOHANG               = 0x00000001;
301    static const unsigned TGT_WUNTRACED             = 0x00000002;
302    static const unsigned TGT_WSTOPPED              = TGT_WUNTRACED;
303    static const unsigned TGT_WEXITED               = 0x00000004;
304    static const unsigned TGT_WCONTINUED            = 0x00000008;
305    static const unsigned TGT_WNOWAIT               = 0x01000000;
306};  // class Linux
307
308#endif // __LINUX_HH__
309