1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/* Copyright (c) 2017 SiFive Inc. All rights reserved.
This copyrighted material is made available to anyone wishing to use,
modify, copy, or redistribute it subject to the terms and conditions
of the FreeBSD License. This program is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
including the implied warranties of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. A copy of this license is available at
http://www.opensource.org/licenses.
========================================================================
syscalls.c : Newlib operating system interface
========================================================================
This is the maven implementation of the narrow newlib operating
system interface. It is based on the minimum stubs in the newlib
documentation, the error stubs in libnosys, and the previous scale
implementation. Please do not include any additional system calls or
other functions in this file. Additional header and source files
should be in the machine subdirectory.
Here is a list of the functions which make up the operating system
interface. The file management instructions execute syscall assembly
instructions so that a proxy kernel (or the simulator) can marshal up
the request to the host machine. The process management functions are
mainly just stubs since for now maven only supports a single process.
- File management functions
+ open : (v) open file
+ lseek : (v) set position in file
+ read : (v) read from file
+ write : (v) write to file
+ fstat : (z) status of an open file
+ stat : (z) status of a file by name
+ close : (z) close a file
+ link : (z) rename a file
+ unlink : (z) remote file's directory entry
- Process management functions
+ execve : (z) transfer control to new proc
+ fork : (z) create a new process
+ getpid : (v) get process id
+ kill : (z) send signal to child process
+ wait : (z) wait for a child process
- Misc functions
+ isatty : (v) query whether output stream is a terminal
+ times : (z) timing information for current process
+ sbrk : (v) increase program data space
+ _exit : (-) exit program without cleaning up files
There are two types of system calls. Those which return a value when
everything is okay (marked with (v) in above list) and those which
return a zero when everything is okay (marked with (z) in above
list). On an error (ie. when the error flag is 1) the return value is
always an errno which should correspond to the numbers in
newlib/libc/include/sys/errno.h
See the newlib documentation for more information
http://sourceware.org/newlib/libc.html#Syscalls
*/
#include <machine/syscall.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <utime.h>
#define syscall_errno(n, a, b, c, d) \
__internal_syscall(n, (long)(a), (long)(b), (long)(c), (long)(d))
long
__syscall_error(long a0)
{
errno = -a0;
return -1;
}
/* Open a file. */
int
_open(const char *name, int flags, int mode)
{
return syscall_errno (SYS_open, name, flags, mode, 0);
}
/* Open file relative to given directory. */
int
_openat(int dirfd, const char *name, int flags, int mode)
{
return syscall_errno (SYS_openat, dirfd, name, flags, mode);
}
/* Set position in a file. */
off_t
_lseek(int file, off_t ptr, int dir)
{
return syscall_errno (SYS_lseek, file, ptr, dir, 0);
}
/* Read from a file. */
ssize_t
_read(int file, void *ptr, size_t len)
{
return syscall_errno (SYS_read, file, ptr, len, 0);
}
/* Write to a file. */
ssize_t
_write(int file, const void *ptr, size_t len)
{
return syscall_errno (SYS_write, file, ptr, len, 0);
}
struct kernel_stat
{
unsigned long long st_dev;
unsigned long long st_ino;
unsigned int st_mode;
unsigned int st_nlink;
unsigned int st_uid;
unsigned int st_gid;
unsigned long long st_rdev;
unsigned long long __pad1;
long long st_size;
int st_blksize;
int __pad2;
long long st_blocks;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
int __glibc_reserved[2];
};
/* Convert linux's stat64 sturct to newlib's stat. */
static void
conv_stat (struct stat *st, struct kernel_stat *kst)
{
st->st_dev = kst->st_dev;
st->st_ino = kst->st_ino;
st->st_mode = kst->st_mode;
st->st_nlink = kst->st_nlink;
st->st_uid = kst->st_uid;
st->st_gid = kst->st_gid;
st->st_rdev = kst->st_rdev;
st->st_size = kst->st_size;
st->st_blocks = kst->st_blocks;
st->st_blksize = kst->st_blksize;
st->st_atime = kst->st_atim.tv_sec;
st->st_mtime = kst->st_mtim.tv_sec;
st->st_ctime = kst->st_ctim.tv_sec;
}
/* Status of an open file. The sys/stat.h header file required is
distributed in the include subdirectory for this C library. */
int
_fstat(int file, struct stat *st)
{
struct kernel_stat kst;
int rv = syscall_errno (SYS_fstat, file, &kst, 0, 0);
conv_stat (st, &kst);
return rv;
}
/* Status of a file (by name). */
int
_stat(const char *file, struct stat *st)
{
struct kernel_stat kst;
int rv = syscall_errno (SYS_stat, file, &kst, 0, 0);
conv_stat (st, &kst);
return rv;
}
/* Status of a link (by name). */
int
_lstat(const char *file, struct stat *st)
{
struct kernel_stat kst;
int rv = syscall_errno (SYS_lstat, file, &kst, 0, 0);
conv_stat (st, &kst);
return rv;
}
/* Status of a file (by name) in a given directory. */
int
_fstatat(int dirfd, const char *file, struct stat *st, int flags)
{
struct kernel_stat kst;
int rv = syscall_errno (SYS_fstatat, dirfd, file, &kst, flags);
conv_stat (st, &kst);
return rv;
}
/* Permissions of a file (by name). */
int
_access(const char *file, int mode)
{
return syscall_errno (SYS_access, file, mode, 0, 0);
}
/* Permissions of a file (by name) in a given directory. */
int
_faccessat(int dirfd, const char *file, int mode, int flags)
{
return syscall_errno (SYS_faccessat, dirfd, file, mode, flags);
}
/* Close a file. */
int
_close(int file)
{
return syscall_errno (SYS_close, file, 0, 0, 0);
}
/* Establish a new name for an existing file. */
int
_link(const char *old_name, const char *new_name)
{
return syscall_errno (SYS_link, old_name, new_name, 0, 0);
}
/* Remove a file's directory entry. */
int
_unlink(const char *name)
{
return syscall_errno (SYS_unlink, name, 0, 0, 0);
}
/* Transfer control to a new process. Minimal implementation for a
system without processes from newlib documentation. */
int
_execve(const char *name, char *const argv[], char *const env[])
{
errno = ENOMEM;
return -1;
}
/* Create a new process. Minimal implementation for a system without
processes from newlib documentation. */
int
_fork()
{
errno = EAGAIN;
return -1;
}
/* Get process id. This is sometimes used to generate strings unlikely
to conflict with other processes. Minimal implementation for a
system without processes just returns 1. */
int
_getpid()
{
return 1;
}
/* Send a signal. Minimal implementation for a system without processes
just causes an error. */
int
_kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
/* Wait for a child process. Minimal implementation for a system without
processes just causes an error. */
int
_wait(int *status)
{
errno = ECHILD;
return -1;
}
/* Query whether output stream is a terminal. For consistency with the
other minimal implementations, which only support output to stdout,
this minimal implementation is suggested by the newlib docs. */
int
_isatty(int file)
{
struct stat s;
int ret = _fstat (file, &s);
return ret == -1 ? -1 : !!(s.st_mode & S_IFCHR);
}
/* Get the current time. Only relatively correct. */
int
_gettimeofday(struct timeval *tp, void *tz)
{
return syscall_errno (SYS_gettimeofday, tp, 0, 0, 0);
}
/* Timing information for current process. From
newlib/libc/include/sys/times.h the tms struct fields are as follows:
- clock_t tms_utime : user clock ticks
- clock_t tms_stime : system clock ticks
- clock_t tms_cutime : children's user clock ticks
- clock_t tms_cstime : children's system clock ticks
Since maven does not currently support processes we set both of the
children's times to zero. Eventually we might want to separately
account for user vs system time, but for now we just return the total
number of cycles since starting the program. */
clock_t
_times(struct tms *buf)
{
// when called for the first time, initialize t0
static struct timeval t0;
if(t0.tv_sec == 0)
_gettimeofday (&t0,0);
struct timeval t;
_gettimeofday (&t, 0);
long long utime = (t.tv_sec - t0.tv_sec) * 1000000 + (t.tv_usec - t0.tv_usec);
buf->tms_utime = utime * CLOCKS_PER_SEC / 1000000;
buf->tms_stime = buf->tms_cstime = buf->tms_cutime = 0;
return -1;
}
/* Get the current time. Only relatively correct. */
int
_ftime(struct timeb *tp)
{
tp->time = tp->millitm = 0;
return 0;
}
/* Stub. */
int
_utime(const char *path, const struct utimbuf *times)
{
return -1;
}
/* Stub. */
int
_chown(const char *path, uid_t owner, gid_t group)
{
return -1;
}
/* Stub. */
int
_chmod(const char *path, mode_t mode)
{
return -1;
}
/* Stub. */
int
_chdir(const char *path)
{
return -1;
}
/* Stub. */
char *
_getcwd(char *buf, size_t size)
{
return NULL;
}
/* Get configurable system variables. */
long
_sysconf(int name)
{
switch (name)
{
case _SC_CLK_TCK:
return CLOCKS_PER_SEC;
}
return -1;
}
/* Increase program data space. As malloc and related functions depend
on this, it is useful to have a working implementation. The following
is suggested by the newlib docs and suffices for a standalone
system. */
void *
_sbrk(ptrdiff_t incr)
{
static unsigned long heap_end;
if (heap_end == 0)
{
long brk = syscall_errno (SYS_brk, 0, 0, 0, 0);
if (brk == -1)
return (void *)-1;
heap_end = brk;
}
if (syscall_errno (SYS_brk, heap_end + incr, 0, 0, 0) != heap_end + incr)
return (void *)-1;
heap_end += incr;
return (void *)(heap_end - incr);
}
/* Exit a program without cleaning up files. */
void
_exit(int exit_status)
{
syscall_errno (SYS_exit, exit_status, 0, 0, 0);
while (1);
}
|