1/*
2 * Copyright (c) 2006 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: Korey Sewell
29 */
30
31#ifndef __ARCH_RISCV_LINUX_LINUX_HH__
32#define __ARCH_RISCV_LINUX_LINUX_HH__
33
34#include "arch/riscv/utility.hh"
35#include "kern/linux/linux.hh"
36
37class RiscvLinux64 : public Linux
38{
39  public:
40    static const int TGT_SIGHUP         =  1;
41    static const int TGT_SIGINT         =  2;
42    static const int TGT_SIGQUIT        =  3;
43    static const int TGT_SIGILL         =  4;
44    static const int TGT_SIGTRAP        =  5;
45    static const int TGT_SIGABRT        =  6;
46    static const int TGT_SIGIOT         =  6;
47    static const int TGT_SIGEMT         =  7;
48    static const int TGT_SIGFPE         =  8;
49    static const int TGT_SIGKILL        =  9;
50    static const int TGT_SIGBUS         = 10;
51    static const int TGT_SIGSEGV        = 11;
52    static const int TGT_SIGSYS         = 12;
53    static const int TGT_SIGPIPE        = 13;
54    static const int TGT_SIGALRM        = 14;
55    static const int TGT_SIGTERM        = 15;
56    static const int TGT_SIGURG         = 16;
57    static const int TGT_SIGSTOP        = 17;
58    static const int TGT_SIGTSTP        = 18;
59    static const int TGT_SIGCONT        = 19;
60    static const int TGT_SIGCHLD        = 20;
61    static const int TGT_SIGCLD         = 20;
62    static const int TGT_SIGTTIN        = 21;
63    static const int TGT_SIGTTOU        = 22;
64    static const int TGT_SIGPOLL        = 23;
65    static const int TGT_SIGIO          = 23;
66    static const int TGT_SIGXCPU        = 24;
67    static const int TGT_SIGXFSZ        = 25;
68    static const int TGT_SIGVTALRM      = 26;
69    static const int TGT_SIGPROF        = 27;
70    static const int TGT_SIGWINCH       = 28;
71    static const int TGT_SIGLOST        = 29;
72    static const int TGT_SIGPWR         = 29;
73    static const int TGT_SIGUSR1        = 30;
74    static const int TGT_SIGUSR2        = 31;
75
76    /// This table maps the target open() flags to the corresponding
77    /// host open() flags.
78    static SyscallFlagTransTable openFlagTable[];
79
80    /// Number of entries in openFlagTable[].
81    static const int NUM_OPEN_FLAGS;
82
83    //@{
84    /// open(2) flag values.
85    static const int TGT_O_RDONLY       = 0x000000; //!< O_RDONLY
86    static const int TGT_O_WRONLY       = 0x000001; //!< O_WRONLY
87    static const int TGT_O_RDWR         = 0x000002; //!< O_RDWR
88    static const int TGT_O_CREAT        = 0x000040; //!< O_CREAT
89    static const int TGT_O_EXCL         = 0x000080; //!< O_EXCL
90    static const int TGT_O_NOCTTY       = 0x000100; //!< O_NOCTTY
91    static const int TGT_O_TRUNC        = 0x000200; //!< O_TRUNC
92    static const int TGT_O_APPEND       = 0x000400; //!< O_APPEND
93    static const int TGT_O_NONBLOCK     = 0x000800; //!< O_NONBLOCK
94    static const int TGT_O_SYNC         = 0x001000; //!< O_SYNC
95    static const int TGT_FSYNC          = 0x001000; //!< FSYNC
96    static const int TGT_FASYNC         = 0x008000; //!< FASYNC
97    // The following are not present in riscv64-unknown-elf <fcntl.h>
98    static const int TGT_O_DSYNC        = 0x010000; //!< O_DSYNC
99    static const int TGT_O_CLOEXEC      = 0x040000; //!< O_CLOEXEC
100    static const int TGT_O_NOINHERIT    = 0x040000; //!< O_NOINHERIT
101    static const int TGT_O_DIRECT       = 0x080000; //!< O_DIRECT
102    static const int TGT_O_NOFOLLOW     = 0x100000; //!< O_NOFOLLOW
103    static const int TGT_O_DIRECTORY    = 0x200000; //!< O_DIRECTORY
104    // The following are not defined by riscv64-unknown-elf
105    static const int TGT_O_LARGEFILE    = 0x020000; //!< O_LARGEFILE
106    static const int TGT_O_NOATIME      = 0x800000; //!< O_NOATIME
107    static const int TGT_O_PATH         = 0x400000; //!< O_PATH
108    //@}
109
110    // Only defined in riscv-unknown-elf for proxy kernel and not linux kernel
111    static const unsigned TGT_MAP_SHARED        = 0x0001;
112    static const unsigned TGT_MAP_PRIVATE       = 0x0002;
113    static const unsigned TGT_MAP_FIXED         = 0x0010;
114    static const unsigned TGT_MAP_ANONYMOUS     = 0x0020;
115    static const unsigned TGT_MAP_POPULATE      = 0x1000;
116    static const unsigned TGT_MREMAP_FIXED      = 0x0020;
117
118    static const unsigned NUM_MMAP_FLAGS;
119
120    typedef int64_t time_t;
121    typedef uint64_t dev_t;
122    typedef uint64_t ino_t;
123    typedef uint32_t mode_t;
124    typedef uint32_t nlink_t;
125    typedef uint32_t uid_t;
126    typedef uint32_t gid_t;
127    typedef int64_t off_t;
128    typedef uint64_t blksize_t;
129    typedef uint64_t blkcnt_t;
130
131    struct timespec {
132        time_t tv_sec;
133        int64_t tv_nsec;
134    };
135
136    typedef struct {
137        dev_t st_dev;
138        ino_t st_ino;
139        mode_t st_mode;
140        nlink_t st_nlink;
141        uid_t st_uid;
142        gid_t st_gid;
143        dev_t st_rdev;
144        dev_t __pad1;
145        off_t st_size;
146        blksize_t st_blksize;
147        blkcnt_t st_blocks;
148        uint64_t st_atimeX;
149        uint64_t st_atime_nsec;
150        uint64_t st_mtimeX;
151        uint64_t st_mtime_nsec;
152        uint64_t st_ctimeX;
153        uint64_t st_ctime_nsec;
154        int32_t ___glibc_reserved[2];
155    } tgt_stat64;
156
157    typedef struct {
158        int32_t val[2];
159    } tgt_fsid_t;
160
161    typedef struct {
162        uint64_t f_type;
163        uint64_t f_bsize;
164        uint64_t f_blocks;
165        uint64_t f_bfree;
166        uint64_t f_bavail;
167        uint64_t f_files;
168        uint64_t f_ffree;
169        tgt_fsid_t f_fsid;
170        uint64_t f_namelen;
171        uint64_t f_frsize;
172        uint64_t f_flags;
173        uint64_t f_spare[4];
174    } tgt_statfs;
175
176    typedef struct {
177        int64_t uptime;
178        uint64_t loads[3];
179        uint64_t totalram;
180        uint64_t freeram;
181        uint64_t sharedram;
182        uint64_t bufferram;
183        uint64_t totalswap;
184        uint64_t freeswap;
185        uint16_t procs;
186        uint16_t pad;
187        uint64_t totalhigh;
188        uint64_t freehigh;
189        uint32_t mem_unit;
190    } tgt_sysinfo;
191
192    static void
193    archClone(uint64_t flags,
194              Process *pp, Process *cp,
195              ThreadContext *ptc, ThreadContext *ctc,
196              uint64_t stack, uint64_t tls)
197    {
198        RiscvISA::copyRegs(ptc, ctc);
199        if (flags & TGT_CLONE_SETTLS)
200            ctc->setIntReg(RiscvISA::ThreadPointerReg, tls);
201        if (stack)
202            ctc->setIntReg(RiscvISA::StackPointerReg, stack);
203    }
204};
205
206class RiscvLinux32 : public Linux
207{
208  public:
209    static const int TGT_SIGHUP         =  1;
210    static const int TGT_SIGINT         =  2;
211    static const int TGT_SIGQUIT        =  3;
212    static const int TGT_SIGILL         =  4;
213    static const int TGT_SIGTRAP        =  5;
214    static const int TGT_SIGABRT        =  6;
215    static const int TGT_SIGIOT         =  6;
216    static const int TGT_SIGEMT         =  7;
217    static const int TGT_SIGFPE         =  8;
218    static const int TGT_SIGKILL        =  9;
219    static const int TGT_SIGBUS         = 10;
220    static const int TGT_SIGSEGV        = 11;
221    static const int TGT_SIGSYS         = 12;
222    static const int TGT_SIGPIPE        = 13;
223    static const int TGT_SIGALRM        = 14;
224    static const int TGT_SIGTERM        = 15;
225    static const int TGT_SIGURG         = 16;
226    static const int TGT_SIGSTOP        = 17;
227    static const int TGT_SIGTSTP        = 18;
228    static const int TGT_SIGCONT        = 19;
229    static const int TGT_SIGCHLD        = 20;
230    static const int TGT_SIGCLD         = 20;
231    static const int TGT_SIGTTIN        = 21;
232    static const int TGT_SIGTTOU        = 22;
233    static const int TGT_SIGPOLL        = 23;
234    static const int TGT_SIGIO          = 23;
235    static const int TGT_SIGXCPU        = 24;
236    static const int TGT_SIGXFSZ        = 25;
237    static const int TGT_SIGVTALRM      = 26;
238    static const int TGT_SIGPROF        = 27;
239    static const int TGT_SIGWINCH       = 28;
240    static const int TGT_SIGLOST        = 29;
241    static const int TGT_SIGPWR         = 29;
242    static const int TGT_SIGUSR1        = 30;
243    static const int TGT_SIGUSR2        = 31;
244
245    /// This table maps the target open() flags to the corresponding
246    /// host open() flags.
247    static SyscallFlagTransTable openFlagTable[];
248
249    /// Number of entries in openFlagTable[].
250    static const int NUM_OPEN_FLAGS;
251
252    //@{
253    /// open(2) flag values.
254    // The following values match newlib 3.0.0.
255    // Note that glibc has different values.
256    static const int TGT_O_RDONLY       = 0x000000; //!< O_RDONLY
257    static const int TGT_O_WRONLY       = 0x000001; //!< O_WRONLY
258    static const int TGT_O_RDWR         = 0x000002; //!< O_RDWR
259    static const int TGT_O_CREAT        = 0x000200; //!< O_CREAT
260    static const int TGT_O_EXCL         = 0x000800; //!< O_EXCL
261    static const int TGT_O_NOCTTY       = 0x008000; //!< O_NOCTTY
262    static const int TGT_O_TRUNC        = 0x000400; //!< O_TRUNC
263    static const int TGT_O_APPEND       = 0x000008; //!< O_APPEND
264    static const int TGT_O_NONBLOCK     = 0x004000; //!< O_NONBLOCK
265    static const int TGT_O_SYNC         = 0x002000; //!< O_SYNC
266    static const int TGT_FSYNC          = 0x002000; //!< FSYNC
267    static const int TGT_FASYNC         = 0x000040; //!< FASYNC
268    // The following are not present in riscv32-unknown-elf <fcntl.h>
269    static const int TGT_O_DSYNC        = 0x010000; //!< O_DSYNC
270    static const int TGT_O_CLOEXEC      = 0x040000; //!< O_CLOEXEC
271    static const int TGT_O_NOINHERIT    = 0x040000; //!< O_NOINHERIT
272    static const int TGT_O_DIRECT       = 0x080000; //!< O_DIRECT
273    static const int TGT_O_NOFOLLOW     = 0x100000; //!< O_NOFOLLOW
274    static const int TGT_O_DIRECTORY    = 0x200000; //!< O_DIRECTORY
275    // The following are not defined by riscv32-unknown-elf
276    static const int TGT_O_LARGEFILE    = 0x020000; //!< O_LARGEFILE
277    static const int TGT_O_NOATIME      = 0x800000; //!< O_NOATIME
278    static const int TGT_O_PATH         = 0x400000; //!< O_PATH
279    //@}
280
281    // Only defined in riscv-unknown-elf for proxy kernel and not linux kernel
282    static const unsigned TGT_MAP_SHARED        = 0x0001;
283    static const unsigned TGT_MAP_PRIVATE       = 0x0002;
284    static const unsigned TGT_MAP_FIXED         = 0x0010;
285    static const unsigned TGT_MAP_ANONYMOUS     = 0x0020;
286    static const unsigned TGT_MAP_POPULATE      = 0x1000;
287    static const unsigned TGT_MREMAP_FIXED      = 0x0020;
288
289    static const unsigned NUM_MMAP_FLAGS;
290
291    // Newlib 3.0.0 defaults to 64-bits for time_t.
292    // Currently time_t in glibc for riscv32 is 32-bits, but will be changed.
293    typedef int64_t time_t;
294
295    /// Limit struct for getrlimit/setrlimit.
296    struct rlimit {
297        uint32_t  rlim_cur;     //!< soft limit
298        uint32_t  rlim_max;     //!< hard limit
299    };
300
301    struct timespec {
302        time_t tv_sec;
303        int32_t tv_nsec;
304    };
305
306    typedef struct {
307        int32_t val[2];
308    } tgt_fsid_t;
309
310    typedef struct {
311        uint64_t st_dev;
312        uint64_t st_ino;
313        uint32_t st_mode;
314        uint32_t st_nlink;
315        uint32_t st_uid;
316        uint32_t st_gid;
317        uint64_t st_rdev;
318        uint64_t __pad1;
319        int64_t st_size;
320        int32_t st_blksize;
321        int32_t __pad2;
322        int64_t st_blocks;
323        time_t st_atimeX;
324        int32_t st_atime_nsec;
325        time_t st_mtimeX;
326        int32_t st_mtime_nsec;
327        time_t st_ctimeX;
328        int32_t st_ctime_nsec;
329        int32_t __unused4;
330        int32_t __unused5;
331    } tgt_stat;
332
333    typedef struct {
334        uint32_t f_type;
335        uint32_t f_bsize;
336        uint32_t f_blocks;
337        uint32_t f_bfree;
338        uint32_t f_bavail;
339        uint32_t f_files;
340        uint32_t f_ffree;
341        tgt_fsid_t f_fsid;
342        uint32_t f_namelen;
343        uint32_t f_frsize;
344        uint32_t f_flags;
345        uint32_t f_spare[4];
346    } tgt_statfs;
347
348    typedef struct {
349        int32_t uptime;
350        uint32_t loads[3];
351        uint32_t totalram;
352        uint32_t freeram;
353        uint32_t sharedram;
354        uint32_t bufferram;
355        uint32_t totalswap;
356        uint32_t freeswap;
357        uint16_t procs;
358        uint16_t pad;
359        uint32_t totalhigh;
360        uint32_t freehigh;
361        uint32_t mem_unit;
362    } tgt_sysinfo;
363
364    static void
365    archClone(uint64_t flags,
366              Process *pp, Process *cp,
367              ThreadContext *ptc, ThreadContext *ctc,
368              uint64_t stack, uint64_t tls)
369    {
370        RiscvISA::copyRegs(ptc, ctc);
371        if (stack)
372            ctc->setIntReg(RiscvISA::StackPointerReg, stack);
373    }
374};
375
376#endif
377