linux.hh revision 2093
1/*
2 * Copyright (c) 2004-2005 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
29#ifndef __LINUX_HH__
30#define __LINUX_HH__
31#include "config/full_system.hh"
32
33#if FULL_SYSTEM
34
35class Linux {};
36
37#else //!FULL_SYSTEM
38
39#include <dirent.h>
40#include <errno.h>
41#include <fcntl.h>	// for host open() flags
42#include <string.h>	// for memset()
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <unistd.h>
46
47#include "sim/syscall_emul.hh"
48
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