source: trunk/libs/newlib/src/libgloss/arc/nsim-syscalls.c @ 684

Last change on this file since 684 was 444, checked in by satin@…, 7 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 8.4 KB
Line 
1/*
2   Copyright (c) 2015, Synopsys, Inc. All rights reserved.
3
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6
7   1) Redistributions of source code must retain the above copyright notice,
8   this list of conditions and the following disclaimer.
9
10   2) Redistributions in binary form must reproduce the above copyright notice,
11   this list of conditions and the following disclaimer in the documentation
12   and/or other materials provided with the distribution.
13
14   3) Neither the name of the Synopsys, Inc., nor the names of its contributors
15   may be used to endorse or promote products derived from this software
16   without specific prior written permission.
17
18   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28   POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#include <_ansi.h>
32#include <_syslist.h>
33#include <errno.h>
34#include <sys/fcntl.h>
35#include <sys/stat.h>
36#include <sys/time.h>
37#include <sys/times.h>
38#include <sys/types.h>
39
40#include "glue.h"
41#include "nsim-syscall.h"
42
43/* Those system calls are implemented in both nSIM and CGEN.  */
44_syscall3 (_ssize_t, read, int, fd, void *, buf, size_t, count)
45_syscall3 (_ssize_t, write, int, fd, const void *, buf, size_t, count)
46_syscall1 (int, unlink, const char *, pathname)
47_syscall3 (off_t, lseek, int, fd, off_t, offset, int, whence)
48_syscall2 (int, gettimeofday, struct timeval *, tv, void *, tz)
49_syscall1 (_CLOCK_T_, time, _CLOCK_T_ *, t)
50_syscall1 (int, close, int, fd)
51_syscall1 (_CLOCK_T_, times, struct tms *, buf)
52/* stat requires custom implementation.  */
53/* _syscall2 (int, stat, const char *, path, struct stat *, st)
54   _syscall2 (int, fstat, int, file, struct stat *, st) */
55/* nSIM implements brk and sbrk, but instead sbrk.c is used.  */
56/* _syscall1 (int, brk, void *, addr) */
57/* open requires custom implementation.  */
58/* _syscall3 (int, open, const char *, pathname, int, flags, int, mode) */
59
60/* Those syscalls are not available in CGEN simulator.  */
61_syscall1 (int, rmdir, const char *, pathname)
62_syscall2 (char *, getcwd, char *, buf, size_t, size)
63/* stat requires custom implementation.  */
64/* _syscall2 (int, lstat, const char *, path, struct stat *, st) */
65
66/* Historically "open" flags defined by default in newlib and in Linux for ARC
67   are different - this is true for some other architectures as well, e.g.
68   ARM.  To provide compatibility ARC port of newlib had a custom fcntl.h file
69   that has "open" flags identical to Linux ones.  Some other architectures
70   (spart64, cris) override default fcntl.h as well, but I'm not sure this is
71   really a good idea.  Unlike system call numbers that can be unique to each
72   BSP in libgloss, "open" flags are not abstracted from the application code
73   itself, hence it is not possible to have fcntl.h in the libgloss.  To make
74   matters worse, existing simulators already has been built for the Linux-like
75   "open" flags.  To preserve compatibility with existing hostlink
76   implementations in simulators, but avoid custom fcntl.h in the future,
77   simulator BSP has to do dynamic rewriting of "open" flags from the newlib
78   default into old ARC Linux flags.  Simulators support only most basic flags,
79   therefore only those are translated in this implementation.  */
80int
81_open (const char * pathname, int flags, int mode)
82{
83  int nsim_flags = 0;
84
85  /* RDONLY, WRONLY, RDWR are same as newlib default.  */
86  nsim_flags |= flags & O_RDONLY;
87  nsim_flags |= flags & O_WRONLY;
88  nsim_flags |= flags & O_RDWR;
89  nsim_flags |= (flags & O_CREAT) ? ARC_LINUX_CREAT : 0;
90  nsim_flags |= (flags & O_APPEND) ? ARC_LINUX_APPEND : 0;
91  nsim_flags |= (flags & O_TRUNC) ? ARC_LINUX_TRUNC : 0;
92  nsim_flags |= (flags & O_EXCL) ? ARC_LINUX_EXCL : 0;
93  /* There are other fcntl flags that are different between newlib and ARC
94     uClibc, however they are not supported by nSIM hostlink, therefore there
95     is no need to translate them.  */
96
97  long __res;
98  _naked_syscall3 (__res, open, pathname, nsim_flags, mode)
99  return __res;
100}
101
102/* Should be provided by crt0.S.  */
103extern void __attribute__((noreturn)) _exit_halt ();
104
105void
106__attribute__((noreturn))
107_exit (int ret)
108{
109  /* Doing an "exit" system call would work on nSIM with hostlink, but call to
110     _exit_halt, which will do a CPU halt is more universal and will work in
111     many other cases as well, including an FPGA/SoC.  */
112  _exit_halt ();
113}
114
115/* This is a copy of newlib/libc/posix/_isatty.c.  It is needed because nSIM
116   hostlink doesn't implement isatty system call.  Hardware boards on the other
117   hand would want isatty implementation that always returns 1, since they are
118   connected to console and doesn't have file IO.  */
119int
120_isatty (int fd)
121{
122  struct stat buf;
123
124  if (fstat (fd, &buf) < 0)
125    {
126      errno = EBADF;
127      return 0;
128    }
129  if (S_ISCHR (buf.st_mode))
130    {
131      return 1;
132    }
133  errno = ENOTTY;
134  return 0;
135}
136
137/* System call "getpid" is implemented in nSIM hostlink, but it is better not
138   to expose it in libgloss.  */
139int
140_getpid (void)
141{
142  return __MYPID;
143}
144
145/* System call "kill" is implemented in nSIM hostlink on Linux hosts, but it
146   seems dangerous to expose it.  Instead, like most of the other "_kill"
147   implementations in libgloss, this will kill only self.  */
148int
149_kill (int pid, int sig)
150{
151  if (pid == __MYPID)
152    {
153      _exit (sig);
154    }
155  errno = ENOSYS;
156  return -1;
157}
158
159static void
160translate_stat (struct nsim_stat *nsim, struct stat *buf)
161{
162  #define TR(field, type) buf->st_ ## field = (type) nsim->field
163  TR (dev, dev_t);
164  TR (ino, ino_t);
165  TR (mode, mode_t);
166  TR (nlink, nlink_t);
167  TR (uid, uid_t);
168  TR (gid, gid_t);
169  TR (rdev, dev_t);
170  TR (size, off_t);
171  TR (atime, time_t);
172  TR (mtime, time_t);
173  TR (ctime, time_t);
174  TR (blksize, long);
175  TR (blocks, long);
176  #undef TR
177}
178
179/* stat/fstat implementation.  Situation is similiar to open and its flags -
180   structure is defined in libc, hence cannot be customized in libgloss, yet we
181   have a case where nSIM uses some definition which is not compatible with
182   neither old ARC-custom definition of "struct stat" in newlib, nor with
183   generic newlib implementation.  */
184int
185_stat (const char * path, struct stat *buf)
186{
187  struct nsim_stat nsim_stat;
188  long __res;
189  _naked_syscall2 (__res, stat, path, &nsim_stat)
190  translate_stat (&nsim_stat, buf);
191  return __res;
192}
193
194int
195_lstat (const char * path, struct stat *buf)
196{
197  struct nsim_stat nsim_stat;
198  long __res;
199  _naked_syscall2 (__res, stat, path, &nsim_stat)
200  translate_stat (&nsim_stat, buf);
201  return __res;
202}
203
204int
205_fstat (int fd, struct stat *buf)
206{
207  struct nsim_stat nsim_stat;
208  long __res;
209  _naked_syscall2 (__res, stat, fd, &nsim_stat)
210  translate_stat (&nsim_stat, buf);
211  return __res;
212}
213
214/* Some system calls are implemented in nSIM hostlink, but are available only
215   on Linux hosts.  To minimize potential compatibility issues they are by
216   default disabled in libgloss build.  */
217#ifdef ARC_NSIM_WIN32_HOST
218_syscall3 (int, ioctl, int, fd, int, request, char *, argp)
219_syscall3 (_ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
220_syscall3 (_ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
221_syscall5 (off_t, llseek, int, fd, unsigned long, offset_high,
222           unsigned long, offset_low, loff_t *, result,
223           unsigned int, whence)
224_syscall2 (int, getrusage, int, who, struct rusage *, usage)
225_syscall2 (int, setrlimit, int, resource, const struct rlimit *, rlim)
226_syscall2 (int, getrlimit, int, resource, struct rlimit *, rlim)
227_syscall3 (int, sigaction, int signum, const struct sigaction *, act,
228           struct sigaction *, oldact)
229_syscall0 (uid_t, getuid)
230_syscall0 (gid_t, getgid)
231_syscall0 (uid_t, geteuid)
232_syscall0 (gid_t, getegid)
233_syscall2 (int, kill, pid_t, pid, int, sig)
234_syscall3 (_ssize_t, readlink, const char *, path, char *, buf, size_t, bufsize)
235#endif
Note: See TracBrowser for help on using the repository browser.