OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [tools/] [riscv-gnu-toolchain-master/] [newlib/] [libgloss/] [riscv/] [syscalls.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
//========================================================================
2
// syscalls.c : Newlib operating system interface                       
3
//========================================================================
4
// This is the maven implementation of the narrow newlib operating
5
// system interface. It is based on the minimum stubs in the newlib
6
// documentation, the error stubs in libnosys, and the previous scale
7
// implementation. Please do not include any additional system calls or
8
// other functions in this file. Additional header and source files
9
// should be in the machine subdirectory.
10
//
11
// Here is a list of the functions which make up the operating system
12
// interface. The file management instructions execute syscall assembly
13
// instructions so that a proxy kernel (or the simulator) can marshal up
14
// the request to the host machine. The process management functions are
15
// mainly just stubs since for now maven only supports a single process.
16
//
17
//  - File management functions
18
//     + open   : (v) open file
19
//     + lseek  : (v) set position in file
20
//     + read   : (v) read from file
21
//     + write  : (v) write to file
22
//     + fstat  : (z) status of an open file
23
//     + stat   : (z) status of a file by name
24
//     + close  : (z) close a file
25
//     + link   : (z) rename a file
26
//     + unlink : (z) remote file's directory entry
27
//
28
//  - Process management functions
29
//     + execve : (z) transfer control to new proc
30
//     + fork   : (z) create a new process 
31
//     + getpid : (v) get process id 
32
//     + kill   : (z) send signal to child process
33
//     + wait   : (z) wait for a child process
34
//     
35
//  - Misc functions
36
//     + isatty : (v) query whether output stream is a terminal
37
//     + times  : (z) timing information for current process
38
//     + sbrk   : (v) increase program data space
39
//     + _exit  : (-) exit program without cleaning up files
40
//
41
// There are two types of system calls. Those which return a value when
42
// everything is okay (marked with (v) in above list) and those which
43
// return a zero when everything is okay (marked with (z) in above
44
// list). On an error (ie. when the error flag is 1) the return value is
45
// always an errno which should correspond to the numbers in
46
// newlib/libc/include/sys/errno.h 
47
//
48
// Note that really I think we are supposed to define versions of these
49
// functions with an underscore prefix (eg. _open). This is what some of
50
// the newlib documentation says, and all the newlib code calls the
51
// underscore version. This is because technically I don't think we are
52
// supposed to pollute the namespace with these function names. If you
53
// define MISSING_SYSCALL_NAMES in xcc/src/newlib/configure.host
54
// then xcc/src/newlib/libc/include/_syslist.h will essentially define
55
// all of the underscore versions to be equal to the non-underscore
56
// versions. I tried not defining MISSING_SYSCALL_NAMES, and newlib
57
// compiled fine but libstdc++ complained about not being able to fine
58
// write, read, etc. So for now we do not use underscores (and we do
59
// define MISSING_SYSCALL_NAMES).
60
//
61
// See the newlib documentation for more information 
62
// http://sourceware.org/newlib/libc.html#Syscalls
63
 
64
#include <machine/syscall.h>
65
#include <sys/stat.h>
66
#include <sys/times.h>
67
#include <sys/time.h>
68
#include <sys/timeb.h>
69
#include <errno.h>
70
#include <string.h>
71
#include <unistd.h>
72
#include <utime.h>
73
 
74
//------------------------------------------------------------------------
75
// environment                                                          
76
//------------------------------------------------------------------------
77
// A pointer to a list of environment variables and their values. For a
78
// minimal environment, this empty list is adequate. We used to define
79
// environ here but it is already defined in
80
// xcc/src/newlib/libc/stdlib/environ.c so to avoid multiple definition
81
// errors we have commented this out for now.
82
//
83
// char* __env[1] = { 0 };
84
// char** environ = __env;
85
 
86
//------------------------------------------------------------------------
87
// open                                                                 
88
//------------------------------------------------------------------------
89
// Open a file.
90
 
91
#define syscall_errno(n, a, b, c, d) \
92
        __internal_syscall(n, (long)(a), (long)(b), (long)(c), (long)(d))
93
 
94
long __syscall_error(long a0)
95
{
96
  errno = -a0;
97
  return -1;
98
}
99
 
100
int open(const char* name, int flags, int mode)
101
{
102
  return syscall_errno(SYS_open, name, flags, mode, 0);
103
}
104
 
105
//------------------------------------------------------------------------
106
// openat                                                                
107
//------------------------------------------------------------------------
108
// Open file relative to given directory
109
int openat(int dirfd, const char* name, int flags, int mode)
110
{
111
  return syscall_errno(SYS_openat, dirfd, name, flags, mode);
112
}
113
 
114
//------------------------------------------------------------------------
115
// lseek                                                                
116
//------------------------------------------------------------------------
117
// Set position in a file.
118
 
119
off_t lseek(int file, off_t ptr, int dir)
120
{
121
  return syscall_errno(SYS_lseek, file, ptr, dir, 0);
122
}
123
 
124
//----------------------------------------------------------------------
125
// read                                                                 
126
//----------------------------------------------------------------------
127
// Read from a file.
128
 
129
ssize_t read(int file, void* ptr, size_t len)
130
{
131
  return syscall_errno(SYS_read, file, ptr, len, 0);
132
}
133
 
134
//------------------------------------------------------------------------
135
// write                                                                
136
//------------------------------------------------------------------------
137
// Write to a file.
138
 
139
ssize_t write(int file, const void* ptr, size_t len)
140
{
141
  return syscall_errno(SYS_write, file, ptr, len, 0);
142
}
143
 
144
//------------------------------------------------------------------------
145
// fstat                                                                
146
//------------------------------------------------------------------------
147
// Status of an open file. The sys/stat.h header file required is
148
// distributed in the include subdirectory for this C library.
149
 
150
int fstat(int file, struct stat* st)
151
{
152
  return syscall_errno(SYS_fstat, file, st, 0, 0);
153
}
154
 
155
//------------------------------------------------------------------------
156
// stat                                                                 
157
//------------------------------------------------------------------------
158
// Status of a file (by name).
159
 
160
int stat(const char* file, struct stat* st)
161
{
162
  return syscall_errno(SYS_stat, file, st, 0, 0);
163
}
164
 
165
//------------------------------------------------------------------------
166
// lstat                                                                 
167
//------------------------------------------------------------------------
168
// Status of a link (by name).
169
 
170
int lstat(const char* file, struct stat* st)
171
{
172
  return syscall_errno(SYS_lstat, file, st, 0, 0);
173
}
174
 
175
//------------------------------------------------------------------------
176
// fstatat                                                                 
177
//------------------------------------------------------------------------
178
// Status of a file (by name) in a given directory.
179
 
180
int fstatat(int dirfd, const char* file, struct stat* st, int flags)
181
{
182
  return syscall_errno(SYS_fstatat, dirfd, file, st, flags);
183
}
184
 
185
//------------------------------------------------------------------------
186
// access                                                                 
187
//------------------------------------------------------------------------
188
// Permissions of a file (by name).
189
 
190
int access(const char* file, int mode)
191
{
192
  return syscall_errno(SYS_access, file, mode, 0, 0);
193
}
194
 
195
//------------------------------------------------------------------------
196
// faccessat                                                                 
197
//------------------------------------------------------------------------
198
// Permissions of a file (by name) in a given directory.
199
 
200
int faccessat(int dirfd, const char* file, int mode, int flags)
201
{
202
  return syscall_errno(SYS_faccessat, dirfd, file, mode, flags);
203
}
204
 
205
//------------------------------------------------------------------------
206
// close                                                                
207
//------------------------------------------------------------------------
208
// Close a file.
209
 
210
int close(int file)
211
{
212
  return syscall_errno(SYS_close, file, 0, 0, 0);
213
}
214
 
215
//------------------------------------------------------------------------
216
// link                                                                 
217
//------------------------------------------------------------------------
218
// Establish a new name for an existing file.
219
 
220
int link(const char* old_name, const char* new_name)
221
{
222
  return syscall_errno(SYS_link, old_name, new_name, 0, 0);
223
}
224
 
225
//------------------------------------------------------------------------
226
// unlink                                                               
227
//------------------------------------------------------------------------
228
// Remove a file's directory entry.
229
 
230
int unlink(const char* name)
231
{
232
  return syscall_errno(SYS_unlink, name, 0, 0, 0);
233
}
234
 
235
//------------------------------------------------------------------------
236
// execve                                                               
237
//------------------------------------------------------------------------
238
// Transfer control to a new process. Minimal implementation for a
239
// system without processes from newlib documentation.
240
 
241
int execve(const char* name, char* const argv[], char* const env[])
242
{
243
  errno = ENOMEM;
244
  return -1;
245
}
246
 
247
//------------------------------------------------------------------------
248
// fork                                                                 
249
//------------------------------------------------------------------------
250
// Create a new process. Minimal implementation for a system without
251
// processes from newlib documentation.
252
 
253
int fork()
254
{
255
  errno = EAGAIN;
256
  return -1;
257
}
258
 
259
//------------------------------------------------------------------------
260
// getpid                                                               
261
//------------------------------------------------------------------------
262
// Get process id. This is sometimes used to generate strings unlikely
263
// to conflict with other processes. Minimal implementation for a
264
// system without processes just returns 1.
265
 
266
int getpid()
267
{
268
  return 1;
269
}
270
 
271
//------------------------------------------------------------------------
272
// kill                                                                 
273
//------------------------------------------------------------------------
274
// Send a signal. Minimal implementation for a system without processes
275
// just causes an error.
276
 
277
int kill(int pid, int sig)
278
{
279
  errno = EINVAL;
280
  return -1;
281
}
282
 
283
//------------------------------------------------------------------------
284
// wait                                                                 
285
//------------------------------------------------------------------------
286
// Wait for a child process. Minimal implementation for a system without
287
// processes just causes an error.
288
 
289
int wait(int* status)
290
{
291
  errno = ECHILD;
292
  return -1;
293
}
294
 
295
//------------------------------------------------------------------------
296
// isatty                                                               
297
//------------------------------------------------------------------------
298
// Query whether output stream is a terminal. For consistency with the
299
// other minimal implementations, which only support output to stdout,
300
// this minimal implementation is suggested by the newlib docs.
301
 
302
int isatty(int file)
303
{
304
  struct stat s;
305
  int ret = fstat(file,&s);
306
  return ret == -1 ? -1 : !!(s.st_mode & S_IFCHR);
307
}
308
 
309
//------------------------------------------------------------------------
310
// times                                                                
311
//------------------------------------------------------------------------
312
// Timing information for current process. From
313
// newlib/libc/include/sys/times.h the tms struct fields are as follows:
314
//
315
//  - clock_t tms_utime  : user clock ticks
316
//  - clock_t tms_stime  : system clock ticks
317
//  - clock_t tms_cutime : children's user clock ticks
318
//  - clock_t tms_cstime : children's system clock ticks
319
//
320
// Since maven does not currently support processes we set both of the
321
// children's times to zero. Eventually we might want to separately
322
// account for user vs system time, but for now we just return the total
323
// number of cycles since starting the program.
324
 
325
clock_t times(struct tms* buf)
326
{
327
  // when called for the first time, initialize t0
328
  static struct timeval t0;
329
  if(t0.tv_sec == 0)
330
    gettimeofday(&t0,0);
331
 
332
  struct timeval t;
333
  gettimeofday(&t,0);
334
 
335
  long long utime = (t.tv_sec-t0.tv_sec)*1000000 + (t.tv_usec-t0.tv_usec);
336
  buf->tms_utime = utime*CLOCKS_PER_SEC/1000000;
337
  buf->tms_stime = buf->tms_cstime = buf->tms_cutime = 0;
338
 
339
  return -1;
340
}
341
 
342
//----------------------------------------------------------------------
343
// gettimeofday                                                                 
344
//----------------------------------------------------------------------
345
// Get the current time.  Only relatively correct.
346
 
347
int gettimeofday(struct timeval* tp, void* tzp)
348
{
349
  return syscall_errno(SYS_gettimeofday, tp, 0, 0, 0);
350
}
351
 
352
//----------------------------------------------------------------------
353
// ftime                                                                 
354
//----------------------------------------------------------------------
355
// Get the current time.  Only relatively correct.
356
 
357
int ftime(struct timeb* tp)
358
{
359
  tp->time = tp->millitm = 0;
360
  return 0;
361
}
362
 
363
//----------------------------------------------------------------------
364
// utime
365
//----------------------------------------------------------------------
366
// Stub.
367
 
368
int utime(const char* path, const struct utimbuf* times)
369
{
370
  return -1;
371
}
372
 
373
//----------------------------------------------------------------------
374
// chown
375
//----------------------------------------------------------------------
376
// Stub.
377
 
378
int chown(const char* path, uid_t owner, gid_t group)
379
{
380
  return -1;
381
}
382
 
383
//----------------------------------------------------------------------
384
// chmod
385
//----------------------------------------------------------------------
386
// Stub.
387
 
388
int chmod(const char* path, mode_t mode)
389
{
390
  return -1;
391
}
392
 
393
//----------------------------------------------------------------------
394
// chdir
395
//----------------------------------------------------------------------
396
// Stub.
397
 
398
int chdir(const char* path)
399
{
400
  return -1;
401
}
402
 
403
//----------------------------------------------------------------------
404
// getcwd
405
//----------------------------------------------------------------------
406
// Stub.
407
 
408
char* getcwd(char* buf, size_t size)
409
{
410
  return NULL;
411
}
412
 
413
//----------------------------------------------------------------------
414
// sysconf
415
//----------------------------------------------------------------------
416
// Get configurable system variables
417
 
418
long sysconf(int name)
419
{
420
  switch(name)
421
  {
422
    case _SC_CLK_TCK:
423
      return CLOCKS_PER_SEC;
424
  }
425
 
426
  return -1;
427
}
428
 
429
//----------------------------------------------------------------------
430
// sbrk                                                                 
431
//----------------------------------------------------------------------
432
// Increase program data space. As malloc and related functions depend
433
// on this, it is useful to have a working implementation. The following
434
// is suggested by the newlib docs and suffices for a standalone
435
// system.
436
 
437
void* sbrk(ptrdiff_t incr)
438
{
439
  extern unsigned char _end[]; // Defined by linker
440
  static unsigned long heap_end;
441
 
442
  if (heap_end == 0)
443
    heap_end = (long)_end;
444
  if (syscall_errno(SYS_brk, heap_end + incr, 0, 0, 0) != heap_end + incr)
445
    return (void*)-1;
446
 
447
  heap_end += incr;
448
  return heap_end - incr;
449
}
450
 
451
//------------------------------------------------------------------------
452
// _exit                                                                
453
//------------------------------------------------------------------------
454
// Exit a program without cleaning up files.
455
 
456
void _exit(int exit_status)
457
{
458
  syscall_errno(SYS_exit, exit_status, 0, 0, 0);
459
  while (1);
460
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.