syscalls.c
12.1 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
* 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup sys_newlib
* @{
*
* @file
* @brief Newlib system call implementations
*
* @author Michael Baar <michael.baar@fu-berlin.de>
* @author Stefan Pfeiffer <pfeiffer@inf.fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include <unistd.h>
#include <reent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <stdint.h>
#include "cpu.h"
#include "board.h"
#include "sched.h"
#include "thread.h"
#include "irq.h"
#include "log.h"
#include "periph/pm.h"
#if MODULE_VFS
#include "vfs.h"
#endif
#include "uart_stdio.h"
#ifdef MODULE_XTIMER
#include <sys/time.h>
#include "div.h"
#include "xtimer.h"
#endif
/**
* @brief manage the heap
*/
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
char *heap_top = &_sheap + 4;
/* MIPS newlib crt implements _init,_fini and _exit and manages the heap */
#ifndef __mips__
/**
* @brief Initialize NewLib, called by __libc_init_array() from the startup script
*/
void _init(void)
{
uart_stdio_init();
}
/**
* @brief Free resources on NewLib de-initialization, not used for RIOT
*/
/* __attribute__((used)) fixes linker errors when building with LTO, but without nano.specs */
__attribute__((used)) void _fini(void)
{
/* nothing to do here */
}
/**
* @brief Exit a program without cleaning up files
*
* If your system doesn't provide this, it is best to avoid linking with subroutines that
* require it (exit, system).
*
* @param n the exit code, 0 for all OK, >0 for not OK
*/
void _exit(int n)
{
LOG_INFO("#! exit %i: powering off\n", n);
pm_off();
while(1);
}
/**
* @brief Allocate memory from the heap.
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not have any means to free memory again
*
* @return pointer to the newly allocated memory on success
* @return pointer set to address `-1` on failure
*/
void *_sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = irq_disable();
void *res = heap_top;
if ((heap_top + incr > &_eheap) || (heap_top + incr < &_sheap)) {
r->_errno = ENOMEM;
res = (void *)-1;
}
else {
heap_top += incr;
}
irq_restore(state);
return res;
}
#endif /*__mips__*/
/**
* @brief Get the process-ID of the current thread
*
* @return the process ID of the current thread
*/
pid_t _getpid(void)
{
return sched_active_pid;
}
/**
* @brief Get the process-ID of the current thread
*
* @return the process ID of the current thread
*/
pid_t _getpid_r(struct _reent *ptr)
{
(void) ptr;
return sched_active_pid;
}
/**
* @brief Send a signal to a given thread
*
* @param r TODO
* @param pid TODO
* @param sig TODO
*
* @return TODO
*/
__attribute__ ((weak))
int _kill_r(struct _reent *r, pid_t pid, int sig)
{
(void) pid;
(void) sig;
r->_errno = ESRCH; /* not implemented yet */
return -1;
}
#if MODULE_VFS
/**
* @brief Open a file
*
* This is a wrapper around @c vfs_open
*
* @param r pointer to reent structure
* @param name file name to open
* @param flags flags, see man 3p open
* @param mode mode, file creation mode if the file is created when opening
*
* @return fd number (>= 0) on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
int _open_r(struct _reent *r, const char *name, int flags, int mode)
{
int fd = vfs_open(name, flags, mode);
if (fd < 0) {
/* vfs returns negative error codes */
r->_errno = -fd;
return -1;
}
return fd;
}
/**
* @brief Read bytes from an open file
*
* This is a wrapper around @c vfs_read
*
* @param[in] r pointer to reent structure
* @param[in] fd open file descriptor obtained from @c open()
* @param[out] dest destination buffer
* @param[in] count maximum number of bytes to read
*
* @return number of bytes read on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
_ssize_t _read_r(struct _reent *r, int fd, void *dest, size_t count)
{
int res = vfs_read(fd, dest, count);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return res;
}
/**
* @brief Write bytes to an open file
*
* This is a wrapper around @c vfs_write
*
* @param[in] r pointer to reent structure
* @param[in] fd open file descriptor obtained from @c open()
* @param[in] src source data buffer
* @param[in] count maximum number of bytes to write
*
* @return number of bytes written on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
_ssize_t _write_r(struct _reent *r, int fd, const void *src, size_t count)
{
int res = vfs_write(fd, src, count);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return res;
}
/**
* @brief Close an open file
*
* This is a wrapper around @c vfs_close
*
* If this call returns an error, the fd should still be considered invalid and
* no further attempt to use it shall be made, not even to retry @c close()
*
* @param[in] r pointer to reent structure
* @param[in] fd open file descriptor obtained from @c open()
*
* @return 0 on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
int _close_r(struct _reent *r, int fd)
{
int res = vfs_close(fd);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return res;
}
/**
* @brief Query or set options on an open file
*
* This is a wrapper around @c vfs_fcntl
*
* @param[in] r pointer to reent structure
* @param[in] fd open file descriptor obtained from @c open()
* @param[in] cmd fcntl command, see man 3p fcntl
* @param[in] arg argument to fcntl command, see man 3p fcntl
*
* @return 0 on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
int _fcntl_r (struct _reent *r, int fd, int cmd, int arg)
{
int res = vfs_fcntl(fd, cmd, arg);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return res;
}
/**
* @brief Seek to position in file
*
* This is a wrapper around @c vfs_lseek
*
* @p whence determines the function of the seek and should be set to one of
* the following values:
*
* - @c SEEK_SET: Seek to absolute offset @p off
* - @c SEEK_CUR: Seek to current location + @p off
* - @c SEEK_END: Seek to end of file + @p off
*
* @param[in] r pointer to reent structure
* @param[in] fd open file descriptor obtained from @c open()
* @param[in] off seek offset
* @param[in] whence determines the seek method, see detailed description
*
* @return the new seek location in the file on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
_off_t _lseek_r(struct _reent *r, int fd, _off_t off, int whence)
{
int res = vfs_lseek(fd, off, whence);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return res;
}
/**
* @brief Get status of an open file
*
* This is a wrapper around @c vfs_fstat
*
* @param[in] r pointer to reent structure
* @param[in] fd open file descriptor obtained from @c open()
* @param[out] buf pointer to stat struct to fill
*
* @return 0 on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
int _fstat_r(struct _reent *r, int fd, struct stat *buf)
{
int res = vfs_fstat(fd, buf);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return 0;
}
/**
* @brief Status of a file (by name)
*
* This is a wrapper around @c vfs_fstat
*
* @param[in] r pointer to reent structure
* @param[in] name path to file
* @param[out] buf pointer to stat struct to fill
*
* @return 0 on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
int _stat_r(struct _reent *r, const char *name, struct stat *st)
{
int res = vfs_stat(name, st);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return 0;
}
/**
* @brief Unlink (delete) a file
*
* @param[in] r pointer to reent structure
* @param[in] path path to file to be deleted
*
* @return 0 on success
* @return -1 on error, @c r->_errno set to a constant from errno.h to indicate the error
*/
int _unlink_r(struct _reent *r, const char *path)
{
int res = vfs_unlink(path);
if (res < 0) {
/* vfs returns negative error codes */
r->_errno = -res;
return -1;
}
return 0;
}
#else /* MODULE_VFS */
/* Fallback stdio_uart wrappers for when VFS is not used, does not allow any
* other file access */
/*
* Fallback read function
*
* All input is read from uart_stdio regardless of fd number. The function will
* block until a byte is actually read.
*
* Note: the read function does not buffer - data will be lost if the function is not
* called fast enough.
*/
_ssize_t _read_r(struct _reent *r, int fd, void *buffer, size_t count)
{
(void)r;
(void)fd;
return uart_stdio_read(buffer, count);
}
/*
* Fallback write function
*
* All output is directed to uart_stdio, independent of the given file descriptor.
* The write call will further block until the byte is actually written to the UART.
*/
_ssize_t _write_r(struct _reent *r, int fd, const void *data, size_t count)
{
(void) r;
(void) fd;
return uart_stdio_write(data, count);
}
/* Stubs to avoid linking errors, these functions do not have any effect */
int _open_r(struct _reent *r, const char *name, int flags, int mode)
{
(void) name;
(void) flags;
(void) mode;
r->_errno = ENODEV;
return -1;
}
int _close_r(struct _reent *r, int fd)
{
(void) fd;
r->_errno = ENODEV;
return -1;
}
_off_t _lseek_r(struct _reent *r, int fd, _off_t pos, int dir)
{
(void) fd;
(void) pos;
(void) dir;
r->_errno = ENODEV;
return -1;
}
int _fstat_r(struct _reent *r, int fd, struct stat *st)
{
(void) fd;
(void) st;
r->_errno = ENODEV;
return -1;
}
int _stat_r(struct _reent *r, const char *name, struct stat *st)
{
(void) name;
(void) st;
r->_errno = ENODEV;
return -1;
}
int _unlink_r(struct _reent *r, const char *path)
{
(void) path;
r->_errno = ENODEV;
return -1;
}
#endif /* MODULE_VFS */
/**
* @brief Query whether output stream is a terminal
*
* @param r TODO
* @param fd TODO
*
* @return TODO
*/
int _isatty_r(struct _reent *r, int fd)
{
r->_errno = 0;
if(fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO) {
return 1;
}
return 0;
}
/**
* @brief Send a signal to a thread
*
* @param[in] pid the pid to send to
* @param[in] sig the signal to send
*
* @return TODO
*/
__attribute__ ((weak))
int _kill(pid_t pid, int sig)
{
(void) pid;
(void) sig;
errno = ESRCH; /* not implemented yet */
return -1;
}
#ifdef MODULE_XTIMER
int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp)
{
(void)tzp;
(void) r;
uint64_t now = xtimer_now_usec64();
tp->tv_sec = div_u64_by_1000000(now);
tp->tv_usec = now - (tp->tv_sec * US_PER_SEC);
return 0;
}
#endif