linux.hh revision 10027
14158SN/A/*
24158SN/A * Copyright (c) 2004-2009 The Regents of The University of Michigan
34158SN/A * All rights reserved.
44158SN/A *
54158SN/A * Redistribution and use in source and binary forms, with or without
64158SN/A * modification, are permitted provided that the following conditions are
74158SN/A * met: redistributions of source code must retain the above copyright
84158SN/A * notice, this list of conditions and the following disclaimer;
94158SN/A * redistributions in binary form must reproduce the above copyright
104158SN/A * notice, this list of conditions and the following disclaimer in the
114158SN/A * documentation and/or other materials provided with the distribution;
124158SN/A * neither the name of the copyright holders nor the names of its
134158SN/A * contributors may be used to endorse or promote products derived from
144158SN/A * this software without specific prior written permission.
154158SN/A *
164158SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174158SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184158SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194158SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204158SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214158SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224158SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234158SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244158SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254158SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264158SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274158SN/A *
284158SN/A * Authors: Ali Saidi
294158SN/A */
304158SN/A
314158SN/A#ifndef __LINUX_HH__
324158SN/A#define __LINUX_HH__
334158SN/A
344158SN/A#include "base/types.hh"
354158SN/A
364158SN/A#include <string>
374158SN/A
384158SN/A#include "kern/operatingsystem.hh"
394158SN/A
404158SN/Aclass ThreadContext;
414158SN/Aclass LiveProcess;
424158SN/A
434158SN/A///
444158SN/A/// This class encapsulates the types, structures, constants,
454158SN/A/// functions, and syscall-number mappings specific to the Alpha Linux
464158SN/A/// syscall interface.
474158SN/A///
484158SN/Aclass Linux : public OperatingSystem
494158SN/A{
504158SN/A
514158SN/A  public:
524158SN/A
534158SN/A    //@{
544158SN/A    /// Basic Linux types.
554158SN/A    typedef uint64_t size_t;
564158SN/A    typedef uint64_t off_t;
574158SN/A    typedef int64_t time_t;
584158SN/A    typedef int64_t clock_t;
594158SN/A    typedef uint32_t uid_t;
604158SN/A    typedef uint32_t gid_t;
614276Sgblack@eecs.umich.edu    //@}
624158SN/A
634276Sgblack@eecs.umich.edu    /// Stat buffer.  Note that we can't call it 'stat' since that
644276Sgblack@eecs.umich.edu    /// gets #defined to something else on some systems. This type
654276Sgblack@eecs.umich.edu    /// can be specialized by architecture specific "Linux" classes
664276Sgblack@eecs.umich.edu    typedef struct {
674276Sgblack@eecs.umich.edu        uint32_t        st_dev;         //!< device
684276Sgblack@eecs.umich.edu        uint32_t        st_ino;         //!< inode
694276Sgblack@eecs.umich.edu        uint32_t        st_mode;        //!< mode
704276Sgblack@eecs.umich.edu        uint32_t        st_nlink;       //!< link count
714276Sgblack@eecs.umich.edu        uint32_t        st_uid;         //!< owner's user ID
724276Sgblack@eecs.umich.edu        uint32_t        st_gid;         //!< owner's group ID
734276Sgblack@eecs.umich.edu        uint32_t        st_rdev;        //!< device number
744276Sgblack@eecs.umich.edu        int32_t         _pad1;          //!< for alignment
754276Sgblack@eecs.umich.edu        int64_t         st_size;        //!< file size in bytes
764276Sgblack@eecs.umich.edu        uint64_t        st_atimeX;      //!< time of last access
774276Sgblack@eecs.umich.edu        uint64_t        st_mtimeX;      //!< time of last modification
784276Sgblack@eecs.umich.edu        uint64_t        st_ctimeX;      //!< time of last status change
794276Sgblack@eecs.umich.edu        uint32_t        st_blksize;     //!< optimal I/O block size
804276Sgblack@eecs.umich.edu        int32_t         st_blocks;      //!< number of blocks allocated
814276Sgblack@eecs.umich.edu        uint32_t        st_flags;       //!< flags
824276Sgblack@eecs.umich.edu        uint32_t        st_gen;         //!< unknown
834276Sgblack@eecs.umich.edu    } tgt_stat;
844276Sgblack@eecs.umich.edu
854276Sgblack@eecs.umich.edu    // same for stat64
864276Sgblack@eecs.umich.edu    typedef struct {
874276Sgblack@eecs.umich.edu        uint64_t        st_dev;
884276Sgblack@eecs.umich.edu        uint64_t        st_ino;
894158SN/A        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    /// Clock ticks per second, for times().
134    static const int M5_SC_CLK_TCK = 100;
135
136    /// For times().
137    struct tms {
138        int64_t tms_utime;      //!< user time
139        int64_t tms_stime;      //!< system time
140        int64_t tms_cutime;     //!< user time of children
141        int64_t tms_cstime;     //!< system time of children
142    };
143
144    // For writev/readv
145    struct tgt_iovec {
146        uint64_t iov_base; // void *
147        uint64_t iov_len;
148    };
149
150    //@{
151    /// ioctl() command codes.
152    static const unsigned TGT_TCGETS     = 0x5401;
153    static const unsigned TGT_TCGETA     = 0x5405;
154    static const unsigned TGT_TCSETAW    = 0x5407;
155    static const unsigned TGT_FIONREAD   = 0x541B;
156    //@}
157
158    /// Return true for the ioctl codes for which we return ENOTTY
159    /// *without* printing a warning, since we know that ENOTTY is the
160    /// correct thing to return (and not just a sign that we don't
161    /// recognize the ioctl code.
162    static bool
163    isTtyReq(unsigned req)
164    {
165        switch (req) {
166          case TGT_FIONREAD:
167          case TGT_TCSETAW:
168          case TGT_TCGETS:
169          case TGT_TCGETA:
170            return true;
171          default:
172            return false;
173        }
174    }
175
176
177    /// Resource constants for getrlimit().
178    static const unsigned TGT_RLIMIT_CPU = 0;
179    static const unsigned TGT_RLIMIT_FSIZE = 1;
180    static const unsigned TGT_RLIMIT_DATA = 2;
181    static const unsigned TGT_RLIMIT_STACK = 3;
182    static const unsigned TGT_RLIMIT_CORE = 4;
183    static const unsigned TGT_RLIMIT_RSS = 5;
184    static const unsigned TGT_RLIMIT_NPROC = 6;
185    static const unsigned TGT_RLIMIT_NOFILE = 7;
186    static const unsigned TGT_RLIMIT_MEMLOCK = 8;
187    static const unsigned TGT_RLIMIT_AS = 9;
188    static const unsigned TGT_RLIMIT_LOCKS = 10;
189    static const unsigned TGT_RLIMIT_SIGPENDING = 11;
190    static const unsigned TGT_RLIMIT_MSGQUEUE = 12;
191    static const unsigned TGT_RLIMIT_NICE = 13;
192    static const unsigned TGT_RLIMIT_RTPRIO = 14;
193    static const unsigned TGT_RLIMIT_RTTIME = 15;
194    static const unsigned TGT_RLIM_NLIMITS = 16;
195
196    /// For getrusage().
197    static const int TGT_RUSAGE_SELF     = 0;
198    static const int TGT_RUSAGE_CHILDREN = -1;
199    static const int TGT_RUSAGE_BOTH     = -2;
200
201    struct rusage {
202        struct timeval ru_utime;        //!< user time used
203        struct timeval ru_stime;        //!< system time used
204        int64_t ru_maxrss;              //!< max rss
205        int64_t ru_ixrss;               //!< integral shared memory size
206        int64_t ru_idrss;               //!< integral unshared data "
207        int64_t ru_isrss;               //!< integral unshared stack "
208        int64_t ru_minflt;              //!< page reclaims - total vmfaults
209        int64_t ru_majflt;              //!< page faults
210        int64_t ru_nswap;               //!< swaps
211        int64_t ru_inblock;             //!< block input operations
212        int64_t ru_oublock;             //!< block output operations
213        int64_t ru_msgsnd;              //!< messages sent
214        int64_t ru_msgrcv;              //!< messages received
215        int64_t ru_nsignals;            //!< signals received
216        int64_t ru_nvcsw;               //!< voluntary context switches
217        int64_t ru_nivcsw;              //!< involuntary "
218    };
219
220    static int openSpecialFile(std::string path, LiveProcess *process, ThreadContext *tc);
221    static std::string procMeminfo(LiveProcess *process, ThreadContext *tc);
222
223    // For futex system call
224    static const unsigned TGT_FUTEX_WAIT  = 0;
225    static const unsigned TGT_FUTEX_WAKE  = 1;
226    static const unsigned TGT_EAGAIN      = 11;
227    static const unsigned TGT_EWOULDBLOCK = TGT_EAGAIN;
228    static const unsigned TGT_FUTEX_PRIVATE_FLAG = 128;
229
230    // for *at syscalls
231    static const int TGT_AT_FDCWD   = -100;
232
233    // for MREMAP
234    static const unsigned TGT_MREMAP_MAYMOVE    = 0x1;
235    static const unsigned TGT_MREMAP_FIXED      = 0x2;
236};  // class Linux
237
238#endif // __LINUX_HH__
239