linux.hh revision 2553
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 "arch/isa_traits.hh"
48#include "sim/syscall_emul.hh"
49
50class TranslatingPort;
51
52///
53/// This class encapsulates the types, structures, constants,
54/// functions, and syscall-number mappings specific to the Alpha Linux
55/// syscall interface.
56///
57class Linux {
58
59  public:
60
61    //@{
62    /// Basic Linux types.
63    typedef uint64_t size_t;
64    typedef uint64_t off_t;
65    typedef int64_t time_t;
66    typedef uint32_t uid_t;
67    typedef uint32_t gid_t;
68    //@}
69
70#if BSD_HOST
71    typedef struct stat hst_stat;
72    typedef struct stat hst_stat64;
73#else
74    typedef struct stat hst_stat ;
75    typedef struct stat64 hst_stat64;
76#endif
77
78    /// Stat buffer.  Note that we can't call it 'stat' since that
79    /// gets #defined to something else on some systems.
80    struct tgt_stat {
81        uint32_t	st_dev;		//!< device
82        uint32_t	st_ino;		//!< inode
83        uint32_t	st_mode;	//!< mode
84        uint32_t	st_nlink;	//!< link count
85        uint32_t	st_uid;		//!< owner's user ID
86        uint32_t	st_gid;		//!< owner's group ID
87        uint32_t	st_rdev;	//!< device number
88        int32_t		_pad1;		//!< for alignment
89        int64_t		st_size;	//!< file size in bytes
90        uint64_t	st_atimeX;	//!< time of last access
91        uint64_t	st_mtimeX;	//!< time of last modification
92        uint64_t	st_ctimeX;	//!< time of last status change
93        uint32_t	st_blksize;	//!< optimal I/O block size
94        int32_t		st_blocks;	//!< number of blocks allocated
95        uint32_t	st_flags;	//!< flags
96        uint32_t	st_gen;		//!< unknown
97    };
98
99    // same for stat64
100    struct tgt_stat64 {
101        uint64_t	st_dev;
102        uint64_t	st_ino;
103        uint64_t	st_rdev;
104        int64_t		st_size;
105        uint64_t	st_blocks;
106
107        uint32_t	st_mode;
108        uint32_t	st_uid;
109        uint32_t	st_gid;
110        uint32_t	st_blksize;
111        uint32_t	st_nlink;
112        uint32_t	__pad0;
113
114        uint64_t	tgt_st_atime;
115        uint64_t 	st_atime_nsec;
116        uint64_t	tgt_st_mtime;
117        uint64_t	st_mtime_nsec;
118        uint64_t	tgt_st_ctime;
119        uint64_t	st_ctime_nsec;
120        int64_t		___unused[3];
121    };
122
123    /// Length of strings in struct utsname (plus 1 for null char).
124    static const int _SYS_NMLN = 65;
125
126    /// Interface struct for uname().
127    struct utsname {
128        char sysname[_SYS_NMLN];	//!< System name.
129        char nodename[_SYS_NMLN];	//!< Node name.
130        char release[_SYS_NMLN];	//!< OS release.
131        char version[_SYS_NMLN];	//!< OS version.
132        char machine[_SYS_NMLN];	//!< Machine type.
133    };
134
135
136    /// Resource enumeration for getrlimit().
137    enum rlimit_resources {
138        TGT_RLIMIT_CPU = 0,
139        TGT_RLIMIT_FSIZE = 1,
140        TGT_RLIMIT_DATA = 2,
141        TGT_RLIMIT_STACK = 3,
142        TGT_RLIMIT_CORE = 4,
143        TGT_RLIMIT_RSS = 5,
144        TGT_RLIMIT_NOFILE = 6,
145        TGT_RLIMIT_AS = 7,
146        TGT_RLIMIT_VMEM = 7,
147        TGT_RLIMIT_NPROC = 8,
148        TGT_RLIMIT_MEMLOCK = 9,
149        TGT_RLIMIT_LOCKS = 10
150    };
151
152    /// Limit struct for getrlimit/setrlimit.
153    struct rlimit {
154        uint64_t  rlim_cur;	//!< soft limit
155        uint64_t  rlim_max;	//!< hard limit
156    };
157
158    /// For gettimeofday().
159    struct timeval {
160        int64_t tv_sec;		//!< seconds
161        int64_t tv_usec;	//!< microseconds
162    };
163
164    // For writev/readv
165    struct tgt_iovec {
166        uint64_t iov_base; // void *
167        uint64_t iov_len;
168    };
169
170
171    /// For getrusage().
172    struct rusage {
173        struct timeval ru_utime;	//!< user time used
174        struct timeval ru_stime;	//!< system time used
175        int64_t ru_maxrss;		//!< max rss
176        int64_t ru_ixrss;		//!< integral shared memory size
177        int64_t ru_idrss;		//!< integral unshared data "
178        int64_t ru_isrss;		//!< integral unshared stack "
179        int64_t ru_minflt;		//!< page reclaims - total vmfaults
180        int64_t ru_majflt;		//!< page faults
181        int64_t ru_nswap;		//!< swaps
182        int64_t ru_inblock;		//!< block input operations
183        int64_t ru_oublock;		//!< block output operations
184        int64_t ru_msgsnd;		//!< messages sent
185        int64_t ru_msgrcv;		//!< messages received
186        int64_t ru_nsignals;		//!< signals received
187        int64_t ru_nvcsw;		//!< voluntary context switches
188        int64_t ru_nivcsw;		//!< involuntary "
189    };
190
191    /// Helper function to convert a host stat buffer to a target stat
192    /// buffer.  Also copies the target buffer out to the simulated
193    /// memory space.  Used by stat(), fstat(), and lstat().
194#if !BSD_HOST
195    static void
196    copyOutStatBuf(TranslatingPort *mem, Addr addr, hst_stat *host)
197    {
198        using namespace TheISA;
199
200        TypedBufferArg<Linux::tgt_stat> tgt(addr);
201
202        tgt->st_dev = htog(host->st_dev);
203        tgt->st_ino = htog(host->st_ino);
204        tgt->st_mode = htog(host->st_mode);
205        tgt->st_nlink = htog(host->st_nlink);
206        tgt->st_uid = htog(host->st_uid);
207        tgt->st_gid = htog(host->st_gid);
208        tgt->st_rdev = htog(host->st_rdev);
209        tgt->st_size = htog(host->st_size);
210        tgt->st_atimeX = htog(host->st_atime);
211        tgt->st_mtimeX = htog(host->st_mtime);
212        tgt->st_ctimeX = htog(host->st_ctime);
213        tgt->st_blksize = htog(host->st_blksize);
214        tgt->st_blocks = htog(host->st_blocks);
215
216        tgt.copyOut(mem);
217    }
218#else
219    // Third version for bsd systems which no longer have any support for
220    // the old stat() call and stat() is actually a stat64()
221    static void
222    copyOutStatBuf(TranslatingPort *mem, Addr addr, hst_stat64 *host)
223    {
224        using namespace TheISA;
225
226        TypedBufferArg<Linux::tgt_stat> tgt(addr);
227
228        tgt->st_dev = htog(host->st_dev);
229        tgt->st_ino = htog(host->st_ino);
230        tgt->st_mode = htog(host->st_mode);
231        tgt->st_nlink = htog(host->st_nlink);
232        tgt->st_uid = htog(host->st_uid);
233        tgt->st_gid = htog(host->st_gid);
234        tgt->st_rdev = htog(host->st_rdev);
235        tgt->st_size = htog(host->st_size);
236        tgt->st_atimeX = htog(host->st_atime);
237        tgt->st_mtimeX = htog(host->st_mtime);
238        tgt->st_ctimeX = htog(host->st_ctime);
239        tgt->st_blksize = htog(host->st_blksize);
240        tgt->st_blocks = htog(host->st_blocks);
241
242        tgt.copyOut(mem);
243    }
244#endif
245
246
247    // Same for stat64
248    static void
249    copyOutStat64Buf(TranslatingPort *mem, int fd, Addr addr, hst_stat64 *host)
250    {
251        using namespace TheISA;
252
253        TypedBufferArg<Linux::tgt_stat64> tgt(addr);
254
255        // fd == 1 checks are because libc does some checks
256        // that the stdout is interactive vs. a file
257        // this makes it work on non-linux systems
258        if (fd == 1)
259            tgt->st_dev = htog((uint64_t)0xA);
260        else
261            tgt->st_dev = htog((uint64_t)host->st_dev);
262        // XXX What about STAT64_HAS_BROKEN_ST_INO ???
263        tgt->st_ino = htog((uint64_t)host->st_ino);
264        if (fd == 1)
265            tgt->st_rdev = htog((uint64_t)0x880d);
266        else
267            tgt->st_rdev = htog((uint64_t)host->st_rdev);
268        tgt->st_size = htog((int64_t)host->st_size);
269        tgt->st_blocks = htog((uint64_t)host->st_blocks);
270
271        if (fd == 1)
272            tgt->st_mode = htog((uint32_t)0x2190);
273        else
274            tgt->st_mode = htog((uint32_t)host->st_mode);
275        tgt->st_uid = htog((uint32_t)host->st_uid);
276        tgt->st_gid = htog((uint32_t)host->st_gid);
277        tgt->st_blksize = htog((uint32_t)host->st_blksize);
278        tgt->st_nlink = htog((uint32_t)host->st_nlink);
279        tgt->tgt_st_atime = htog((uint64_t)host->st_atime);
280        tgt->tgt_st_mtime = htog((uint64_t)host->st_mtime);
281        tgt->tgt_st_ctime = htog((uint64_t)host->st_ctime);
282#if defined(STAT_HAVE_NSEC)
283        tgt->st_atime_nsec = htog(host->st_atime_nsec);
284        tgt->st_mtime_nsec = htog(host->st_mtime_nsec);
285        tgt->st_ctime_nsec = htog(host->st_ctime_nsec);
286#else
287        tgt->st_atime_nsec = 0;
288        tgt->st_mtime_nsec = 0;
289        tgt->st_ctime_nsec = 0;
290#endif
291
292        tgt.copyOut(mem);
293    }
294
295};  // class Linux
296
297
298#endif // FULL_SYSTEM
299
300#endif // __LINUX_HH__
301