1/*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Gabe Black
38 */
39
40#ifndef __ARCH_X86_LINUX_LINUX_HH__
41#define __ARCH_X86_LINUX_LINUX_HH__
42
43#include "arch/x86/utility.hh"
44#include "kern/linux/linux.hh"
45
46class X86Linux : public Linux
47{
48  public:
49    static void
50    archClone(uint64_t flags,
51                          Process *pp, Process *cp,
52                          ThreadContext *ptc, ThreadContext *ctc,
53                          uint64_t stack, uint64_t tls)
54    {
55        X86ISA::copyRegs(ptc, ctc);
56
57        if (flags & TGT_CLONE_SETTLS) {
58            ctc->setMiscRegNoEffect(X86ISA::MISCREG_FS_BASE, tls);
59            ctc->setMiscRegNoEffect(X86ISA::MISCREG_FS_EFF_BASE, tls);
60        }
61
62        if (stack)
63            ctc->setIntReg(X86ISA::StackPointerReg, stack);
64    }
65};
66
67class X86Linux64 : public X86Linux
68{
69  public:
70
71    typedef struct {
72        uint64_t st_dev;
73        uint64_t st_ino;
74        uint64_t st_nlink;
75        uint32_t st_mode;
76        uint32_t st_uid;
77        uint32_t st_gid;
78        uint32_t __pad0;
79        uint64_t st_rdev;
80        int64_t st_size;
81        int64_t st_blksize;
82        int64_t st_blocks;
83        uint64_t st_atimeX;
84        uint64_t st_atime_nsec;
85        uint64_t st_mtimeX;
86        uint64_t st_mtime_nsec;
87        uint64_t st_ctimeX;
88        uint64_t st_ctime_nsec;
89        int64_t unused0[3];
90    } tgt_stat64;
91
92    typedef struct {
93        long val[2];
94    } tgt_fsid;
95
96    typedef struct {
97        long f_type;
98        long f_bsize;
99        long f_blocks;
100        long f_bfree;
101        long f_bavail;
102        long f_files;
103        long f_ffree;
104        tgt_fsid f_fsid;
105        long f_namelen;
106        long f_frsize;
107        long f_spare[5];
108    } tgt_statfs;
109
110    static const int TGT_SIGHUP         = 0x000001;
111    static const int TGT_SIGINT         = 0x000002;
112    static const int TGT_SIGQUIT        = 0x000003;
113    static const int TGT_SIGILL         = 0x000004;
114    static const int TGT_SIGTRAP        = 0x000005;
115    static const int TGT_SIGABRT        = 0x000006;
116    static const int TGT_SIGIOT         = 0x000006;
117    static const int TGT_SIGBUS         = 0x000007;
118    static const int TGT_SIGFPE         = 0x000008;
119    static const int TGT_SIGKILL        = 0x000009;
120    static const int TGT_SIGUSR1        = 0x00000a;
121    static const int TGT_SIGSEGV        = 0x00000b;
122    static const int TGT_SIGUSR2        = 0x00000c;
123    static const int TGT_SIGPIPE        = 0x00000d;
124    static const int TGT_SIGALRM        = 0x00000e;
125    static const int TGT_SIGTERM        = 0x00000f;
126    static const int TGT_SIGSTKFLT      = 0x000010;
127    static const int TGT_SIGCHLD        = 0x000011;
128    static const int TGT_SIGCONT        = 0x000012;
129    static const int TGT_SIGSTOP        = 0x000013;
130    static const int TGT_SIGTSTP        = 0x000014;
131    static const int TGT_SIGTTIN        = 0x000015;
132    static const int TGT_SIGTTOU        = 0x000016;
133    static const int TGT_SIGURG         = 0x000017;
134    static const int TGT_SIGXCPU        = 0x000018;
135    static const int TGT_SIGXFSZ        = 0x000019;
136    static const int TGT_SIGVTALRM      = 0x00001a;
137    static const int TGT_SIGPROF        = 0x00001b;
138    static const int TGT_SIGWINCH       = 0x00001c;
139    static const int TGT_SIGIO          = 0x00001d;
140    static const int TGT_SIGPOLL        = 0x00001d;
141    static const int TGT_SIGPWR         = 0x00001e;
142    static const int TGT_SIGSYS         = 0x00001f;
143    static const int TGT_SIGUNUSED      = 0x00001f;
144
145    static SyscallFlagTransTable openFlagTable[];
146
147    static const int TGT_O_RDONLY       = 000000000;     //!< O_RDONLY
148    static const int TGT_O_WRONLY       = 000000001;     //!< O_WRONLY
149    static const int TGT_O_RDWR         = 000000002;     //!< O_RDWR
150    static const int TGT_O_CREAT        = 000000100;     //!< O_CREAT
151    static const int TGT_O_EXCL         = 000000200;     //!< O_EXCL
152    static const int TGT_O_NOCTTY       = 000000400;     //!< O_NOCTTY
153    static const int TGT_O_TRUNC        = 000001000;     //!< O_TRUNC
154    static const int TGT_O_APPEND       = 000002000;     //!< O_APPEND
155    static const int TGT_O_NONBLOCK     = 000004000;     //!< O_NONBLOCK
156    static const int TGT_O_DSYNC        = 000010000;
157    static const int TGT_FASYNC         = 000020000;
158    static const int TGT_O_DIRECT       = 000040000;     //!< O_DIRECTIO
159    static const int TGT_O_LARGEFILE    = 000100000;
160    static const int TGT_O_DIRECTORY    = 000200000;
161    static const int TGT_O_NOFOLLOW     = 000400000;
162    static const int TGT_O_NOATIME      = 001000000;
163    static const int TGT_O_CLOEXEC      = 002000000;
164    static const int TGT_O_SYNC         = 004010000;     //!< O_SYNC
165    static const int TGT_O_PATH         = 010000000;
166
167    static const int NUM_OPEN_FLAGS;
168
169    static const unsigned TGT_MAP_SHARED        = 0x00001;
170    static const unsigned TGT_MAP_PRIVATE       = 0x00002;
171    static const unsigned TGT_MAP_32BIT         = 0x00040;
172    static const unsigned TGT_MAP_ANON          = 0x00020;
173    static const unsigned TGT_MAP_DENYWRITE     = 0x00800;
174    static const unsigned TGT_MAP_EXECUTABLE    = 0x01000;
175    static const unsigned TGT_MAP_FILE          = 0x00000;
176    static const unsigned TGT_MAP_GROWSDOWN     = 0x00100;
177    static const unsigned TGT_MAP_HUGETLB       = 0x40000;
178    static const unsigned TGT_MAP_LOCKED        = 0x02000;
179    static const unsigned TGT_MAP_NONBLOCK      = 0x10000;
180    static const unsigned TGT_MAP_NORESERVE     = 0x04000;
181    static const unsigned TGT_MAP_POPULATE      = 0x08000;
182    static const unsigned TGT_MAP_STACK         = 0x20000;
183    static const unsigned TGT_MAP_ANONYMOUS     = 0x00020;
184    static const unsigned TGT_MAP_FIXED         = 0x00010;
185
186    static const unsigned NUM_MMAP_FLAGS;
187
188    typedef struct {
189        uint64_t iov_base; // void *
190        uint64_t iov_len;  // size_t
191    } tgt_iovec;
192
193    typedef struct {
194        int64_t  uptime;    /* Seconds since boot */
195        uint64_t loads[3];  /* 1, 5, and 15 minute load averages */
196        uint64_t totalram;  /* Total usable main memory size */
197        uint64_t freeram;   /* Available memory size */
198        uint64_t sharedram; /* Amount of shared memory */
199        uint64_t bufferram; /* Memory used by buffers */
200        uint64_t totalswap; /* Total swap space size */
201        uint64_t freeswap;  /* swap space still available */
202        uint16_t procs;     /* Number of current processes */
203        uint64_t totalhigh; /* Total high memory size */
204        uint64_t freehigh;  /* Available high memory size */
205        uint64_t mem_unit;  /* Memory unit size in bytes */
206    } tgt_sysinfo;
207
208};
209
210class X86Linux32 : public X86Linux
211{
212  public:
213
214    typedef struct {
215        uint64_t st_dev;
216        uint8_t __pad0[4];
217        uint32_t __st_ino;
218        uint32_t st_mode;
219        uint32_t st_nlink;
220        uint32_t st_uid;
221        uint32_t st_gid;
222        uint64_t st_rdev;
223        uint8_t __pad3[4];
224        int64_t st_size;
225        uint32_t st_blksize;
226        uint64_t st_blocks;
227        uint32_t st_atimeX;
228        uint32_t st_atime_nsec;
229        uint32_t st_mtimeX;
230        uint32_t st_mtime_nsec;
231        uint32_t st_ctimeX;
232        uint32_t st_ctime_nsec;
233        uint64_t st_ino;
234    } __attribute__((__packed__)) tgt_stat64;
235
236    static const int TGT_SIGHUP         = 0x000001;
237    static const int TGT_SIGINT         = 0x000002;
238    static const int TGT_SIGQUIT        = 0x000003;
239    static const int TGT_SIGILL         = 0x000004;
240    static const int TGT_SIGTRAP        = 0x000005;
241    static const int TGT_SIGABRT        = 0x000006;
242    static const int TGT_SIGIOT         = 0x000006;
243    static const int TGT_SIGBUS         = 0x000007;
244    static const int TGT_SIGFPE         = 0x000008;
245    static const int TGT_SIGKILL        = 0x000009;
246    static const int TGT_SIGUSR1        = 0x00000a;
247    static const int TGT_SIGSEGV        = 0x00000b;
248    static const int TGT_SIGUSR2        = 0x00000c;
249    static const int TGT_SIGPIPE        = 0x00000d;
250    static const int TGT_SIGALRM        = 0x00000e;
251    static const int TGT_SIGTERM        = 0x00000f;
252    static const int TGT_SIGSTKFLT      = 0x000010;
253    static const int TGT_SIGCHLD        = 0x000011;
254    static const int TGT_SIGCONT        = 0x000012;
255    static const int TGT_SIGSTOP        = 0x000013;
256    static const int TGT_SIGTSTP        = 0x000014;
257    static const int TGT_SIGTTIN        = 0x000015;
258    static const int TGT_SIGTTOU        = 0x000016;
259    static const int TGT_SIGURG         = 0x000017;
260    static const int TGT_SIGXCPU        = 0x000018;
261    static const int TGT_SIGXFSZ        = 0x000019;
262    static const int TGT_SIGVTALRM      = 0x00001a;
263    static const int TGT_SIGPROF        = 0x00001b;
264    static const int TGT_SIGWINCH       = 0x00001c;
265    static const int TGT_SIGIO          = 0x00001d;
266    static const int TGT_SIGPOLL        = 0x00001d;
267    static const int TGT_SIGPWR         = 0x00001e;
268    static const int TGT_SIGSYS         = 0x00001f;
269    static const int TGT_SIGUNUSED      = 0x00001f;
270
271    static SyscallFlagTransTable openFlagTable[];
272
273    static const int TGT_O_RDONLY       = 000000000;     //!< O_RDONLY
274    static const int TGT_O_WRONLY       = 000000001;     //!< O_WRONLY
275    static const int TGT_O_RDWR         = 000000002;     //!< O_RDWR
276    static const int TGT_O_CREAT        = 000000100;     //!< O_CREAT
277    static const int TGT_O_EXCL         = 000000200;     //!< O_EXCL
278    static const int TGT_O_NOCTTY       = 000000400;     //!< O_NOCTTY
279    static const int TGT_O_TRUNC        = 000001000;     //!< O_TRUNC
280    static const int TGT_O_APPEND       = 000002000;     //!< O_APPEND
281    static const int TGT_O_NONBLOCK     = 000004000;     //!< O_NONBLOCK
282    static const int TGT_O_DSYNC        = 000010000;     //!< O_DSYNC
283    static const int TGT_FASYNC         = 000020000;
284    static const int TGT_O_DIRECT       = 000040000;     //!< O_DIRECTIO
285    static const int TGT_O_LARGEFILE    = 000100000;
286    static const int TGT_O_DIRECTORY    = 000200000;
287    static const int TGT_O_NOFOLLOW     = 000400000;
288    static const int TGT_O_NOATIME      = 001000000;
289    static const int TGT_O_CLOEXEC      = 002000000;
290    static const int TGT_O_SYNC         = 004010000;     //!< O_SYNC
291    static const int TGT_O_PATH         = 010000000;
292
293    static const int NUM_OPEN_FLAGS;
294
295    static SyscallFlagTransTable mmapFlagTable[];
296
297    static const unsigned TGT_MAP_SHARED        = 0x00001;
298    static const unsigned TGT_MAP_PRIVATE       = 0x00002;
299    static const unsigned TGT_MAP_32BIT         = 0x00040;
300    static const unsigned TGT_MAP_ANON          = 0x00020;
301    static const unsigned TGT_MAP_DENYWRITE     = 0x00800;
302    static const unsigned TGT_MAP_EXECUTABLE    = 0x01000;
303    static const unsigned TGT_MAP_FILE          = 0x00000;
304    static const unsigned TGT_MAP_GROWSDOWN     = 0x00100;
305    static const unsigned TGT_MAP_HUGETLB       = 0x40000;
306    static const unsigned TGT_MAP_LOCKED        = 0x02000;
307    static const unsigned TGT_MAP_NONBLOCK      = 0x10000;
308    static const unsigned TGT_MAP_NORESERVE     = 0x04000;
309    static const unsigned TGT_MAP_POPULATE      = 0x08000;
310    static const unsigned TGT_MAP_STACK         = 0x20000;
311    static const unsigned TGT_MAP_ANONYMOUS     = 0x00020;
312    static const unsigned TGT_MAP_FIXED         = 0x00010;
313
314    static const unsigned NUM_MMAP_FLAGS;
315
316    typedef struct {
317       int32_t  uptime;    /* Seconds since boot */
318       uint32_t loads[3];  /* 1, 5, and 15 minute load averages */
319       uint32_t totalram;  /* Total usable main memory size */
320       uint32_t freeram;   /* Available memory size */
321       uint32_t sharedram; /* Amount of shared memory */
322       uint32_t bufferram; /* Memory used by buffers */
323       uint32_t totalswap; /* Total swap space size */
324       uint32_t freeswap;  /* swap space still available */
325       uint16_t procs;     /* Number of current processes */
326       uint32_t totalhigh; /* Total high memory size */
327       uint32_t freehigh;  /* Available high memory size */
328       uint32_t mem_unit;  /* Memory unit size in bytes */
329    } tgt_sysinfo;
330};
331
332#endif
333