-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathsyscalls.c
More file actions
633 lines (581 loc) · 13.5 KB
/
syscalls.c
File metadata and controls
633 lines (581 loc) · 13.5 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* Copyright (C) 2019 Koen Zandberg
*
* 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.
*/
/**
* @defgroup sys_picolibc PicoLibc system call
* @ingroup sys
* @brief PicoLibc system call
* @{
*
* @file
* @brief PicoLibc system call implementations
*
* @author Koen Zandberg <[email protected]>
*
* @}
*/
#include <errno.h>
#include <stdio.h>
#include <sys/times.h>
#include <unistd.h>
#include "irq.h"
#include "log.h"
#include "periph/pm.h"
#include "stdio_base.h"
#ifndef NUM_HEAPS
#define NUM_HEAPS 1
#endif
/**
* @brief manage the heap
*/
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
#define __eheap &_eheap
/**
* @brief Additional heap sections that may be defined in the linkerscript.
*
* The compiler should not generate references to those symbols if
* they are not used, so only provide them if additional memory sections
* that can be used as heap are available.
* @{
*/
extern char _sheap1;
extern char _eheap1;
extern char _sheap2;
extern char _eheap2;
extern char _sheap3;
extern char _eheap3;
/* @} */
struct heap {
char* start;
char* end;
};
static char *heap_top[NUM_HEAPS] = {
&_sheap,
#if NUM_HEAPS > 1
&_sheap1,
#endif
#if NUM_HEAPS > 2
&_sheap2,
#endif
#if NUM_HEAPS > 3
&_sheap3,
#endif
#if NUM_HEAPS > 4
#error "Unsupported NUM_HEAPS value, edit newlib_syscalls_default/syscalls.c to add more heaps."
#endif
};
static const struct heap heaps[NUM_HEAPS] = {
{
.start = &_sheap,
.end = __eheap
},
#if NUM_HEAPS > 1
{
.start = &_sheap1,
.end = &_eheap1
},
#endif
#if NUM_HEAPS > 2
{
.start = &_sheap2,
.end = &_eheap2
},
#endif
#if NUM_HEAPS > 3
{
.start = &_sheap3,
.end = &_eheap3
},
#endif
};
/**
* @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 __attribute__((__noreturn__))
_exit(int n)
{
LOG_INFO("#! exit %i: powering off\n", n);
#ifdef MODULE_PERIPH_PM
pm_off();
#endif /* MODULE_PERIPH_PM */
for (;;) {
}
}
/**
* @brief Allocate memory from the heap.
*
* @return pointer to the newly allocated memory on success
* @return pointer set to address `-1` on failure
*/
void *sbrk(ptrdiff_t incr)
{
void *res = (void*)UINTPTR_MAX;
unsigned int state = irq_disable();
for (unsigned i = 0; i < NUM_HEAPS; ++i) {
if ((heap_top[i] + incr > heaps[i].end) ||
(heap_top[i] + incr < heaps[i].start)) {
continue;
}
res = heap_top[i];
heap_top[i] += incr;
break;
}
irq_restore(state);
return res;
}
/**
* @brief Send a signal to a thread
*
* @param[in] pid the pid to send to
* @param[in] sig the signal to send
*
* @return always returns -1 to signal error
*/
__attribute__ ((weak))
int kill(pid_t pid, int sig)
{
(void)pid;
(void)sig;
errno = ESRCH; /* not implemented yet */
return -1;
}
#include "mutex.h"
#ifndef PICOLIBC_STDOUT_BUFSIZE
#define PICOLIBC_STDOUT_BUFSIZE 64
#endif
#ifdef MODULE_PICOLIBC_STDOUT_BUFFERED
static mutex_t picolibc_put_mutex = MUTEX_INIT;
static char picolibc_stdout[PICOLIBC_STDOUT_BUFSIZE];
static int picolibc_stdout_queued;
static void _picolibc_flush(void)
{
if (picolibc_stdout_queued) {
stdio_write(picolibc_stdout, picolibc_stdout_queued);
picolibc_stdout_queued = 0;
}
}
static int picolibc_put(char c, FILE *file)
{
(void)file;
mutex_lock(&picolibc_put_mutex);
picolibc_stdout[picolibc_stdout_queued++] = c;
if (picolibc_stdout_queued == PICOLIBC_STDOUT_BUFSIZE || c == '\n') {
_picolibc_flush();
}
mutex_unlock(&picolibc_put_mutex);
return 1;
}
static int picolibc_flush(FILE *file)
{
(void)file;
mutex_lock(&picolibc_put_mutex);
_picolibc_flush();
mutex_unlock(&picolibc_put_mutex);
return 0;
}
#else
int picolibc_put(char c, FILE *file)
{
(void)file;
stdio_write(&c, 1);
return 1;
}
static int picolibc_flush(FILE *file)
{
(void)file;
return 0;
}
#endif
static int picolibc_get(FILE *file)
{
(void)file;
picolibc_flush(NULL);
char c = 0;
stdio_read(&c, 1);
return c;
}
FILE picolibc_stdio =
FDEV_SETUP_STREAM(picolibc_put, picolibc_get, picolibc_flush, _FDEV_SETUP_RW);
/* Since picolibc 1.8.10, PICOLIBC_STDIO_GLOBALS is prefixed with two leading
* underscores. We just test for both to remain backwards compatible */
#if defined(PICOLIBC_STDIO_GLOBALS) || defined(__PICOLIBC_STDIO_GLOBALS)
#ifdef __strong_reference
/* This saves two const pointers.
* See https://github.com/RIOT-OS/RIOT/pull/17001#issuecomment-945936918
*/
#define STDIO_ALIAS(x) __strong_reference(stdin, x);
#else
#define STDIO_ALIAS(x) FILE *const x = &__picolibc_stdio;
#endif
FILE *const stdin = &picolibc_stdio;
STDIO_ALIAS(stdout);
STDIO_ALIAS(stderr);
#else
FILE *const __iob[] = {
&picolibc_stdio, /* stdin */
&picolibc_stdio, /* stdout */
&picolibc_stdio, /* stderr */
};
#endif
#include <thread.h>
/**
* @brief Get the process-ID of the current thread
*
* @return the process ID of the current thread
*/
pid_t getpid(void)
{
return thread_getpid();
}
#if MODULE_VFS
#include "vfs.h"
#else
#include <sys/stat.h>
#endif
/**
* @brief Open a file
*
* This is a wrapper around @c vfs_open
*
* @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 errno set to a constant from errno.h to indicate the error
*/
int open(const char *name, int flags, int mode)
{
#ifdef MODULE_VFS
int fd = vfs_open(name, flags, mode);
if (fd < 0) {
/* vfs returns negative error codes */
errno = -fd;
return -1;
}
return fd;
#else
(void)name;
(void)flags;
(void)mode;
errno = ENODEV;
return -1;
#endif
}
/*
* Picolibc newer than 1.8 uses standard posix types for read/write
* return values
*/
#if __PICOLIBC_MAJOR__ > 1 || __PICOLIBC_MINOR__ >= 8
#define _READ_WRITE_RETURN_TYPE ssize_t
#endif
/**
* @brief Read bytes from an open file
*
* This is a wrapper around @c vfs_read
*
* @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 errno set to a constant from errno.h to indicate the error
*/
_READ_WRITE_RETURN_TYPE read(int fd, void *dest, size_t count)
{
#ifdef MODULE_VFS
int res = vfs_read(fd, dest, count);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return res;
#else
if (fd != STDIN_FILENO) {
errno = ENOTSUP;
return -1;
}
return stdio_read(dest, count);
#endif
}
/**
* @brief Write bytes to an open file
*
* This is a wrapper around @c vfs_write
*
* @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 errno set to a constant from errno.h to indicate the error
*/
_READ_WRITE_RETURN_TYPE write(int fd, const void *src, size_t count)
{
#ifdef MODULE_VFS
int res = vfs_write(fd, src, count);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return res;
#else
if (fd != STDOUT_FILENO && fd != STDERR_FILENO) {
errno = ENOTSUP;
return -1;
}
return stdio_write(src, count);
#endif
}
/**
* @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] fd open file descriptor obtained from @c open()
*
* @return 0 on success
* @return -1 on error, @c errno set to a constant from errno.h to indicate the error
*/
int close(int fd)
{
#ifdef MODULE_VFS
int res = vfs_close(fd);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return res;
#else
(void)fd;
errno = ENOTSUP;
return -1;
#endif
}
/**
* Current process times (not implemented).
*
* @param[out] ptms Not modified.
*
* @return -1, this function always fails. errno is set to ENOSYS.
*/
clock_t times(struct tms *ptms)
{
(void)ptms;
errno = ENOSYS;
return (-1);
}
/**
* @brief Query or set options on an open file
*
* This is a wrapper around @c vfs_fcntl
*
* @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 errno set to a constant from errno.h to indicate the error
*/
int fcntl(int fd, int cmd, int arg)
{
#ifdef MODULE_VFS
int res = vfs_fcntl(fd, cmd, arg);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return res;
#else
(void)fd;
(void)cmd;
(void)arg;
errno = ENOTSUP;
return -1;
#endif
}
/**
* @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] 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 errno set to a constant from errno.h to indicate the error
*/
off_t lseek(int fd, _off_t off, int whence)
{
#ifdef MODULE_VFS
int res = vfs_lseek(fd, off, whence);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return res;
#else
(void)fd;
(void)off;
(void)whence;
errno = ENOTSUP;
return -1;
#endif
}
/**
* @brief Sets the file position indicator to the beginning of the file.
*
* @param[in] stream open file descriptor obtained from @c fopen()
*/
void rewind(FILE *stream)
{
fseek(stream, 0L, SEEK_SET);
}
/**
* @brief Get status of an open file
*
* This is a wrapper around @c vfs_fstat
*
* @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 errno set to a constant from errno.h to indicate the error
*/
int fstat(int fd, struct stat *buf)
{
#ifdef MODULE_VFS
int res = vfs_fstat(fd, buf);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return 0;
#else
(void)fd;
(void)buf;
errno = ENOTSUP;
return -1;
#endif
}
/**
* @brief Status of a file (by name)
*
* This is a wrapper around @c vfs_fstat
*
* @param[in] name path to file
* @param[out] buf pointer to stat struct to fill
*
* @return 0 on success
* @return -1 on error, @c errno set to a constant from errno.h to indicate the error
*/
int stat(const char *name, struct stat *st)
{
#ifdef MODULE_VFS
int res = vfs_stat(name, st);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return 0;
#else
(void)name;
(void)st;
errno = ENODEV;
return -1;
#endif
}
/**
* @brief Unlink (delete) a file
*
* @param[in] path path to file to be deleted
*
* @return 0 on success
* @return -1 on error, @c errno set to a constant from errno.h to indicate the error
*/
int unlink(const char *path)
{
#ifdef MODULE_VFS
int res = vfs_unlink(path);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return 0;
#else
(void)path;
errno = ENODEV;
return -1;
#endif
}
/**
* @brief Deletes a directory, which must be empty
*
* @param[in] path path to directory to be deleted
*
* @return 0 on success
* @return -1 on error, @c errno set to a constant from errno.h to indicate the error
*/
int rmdir(const char *path)
{
#ifdef MODULE_VFS
int res = vfs_rmdir(path);
if (res < 0) {
/* vfs returns negative error codes */
errno = -res;
return -1;
}
return 0;
#else
(void)path;
errno = ENODEV;
return -1;
#endif
}
/**
* @brief Remove (delete) a file or directory
*
* remove() deletes a name from the filesystem.
* It calls unlink(2) for files, and rmdir(2) for directories.
*
* @param[in] path path to file or directory to be deleted
*
* @return 0 on success
* @return -1 on error, @c errno set to a constant from errno.h to indicate the error
*/
int remove(const char *path)
{
if (unlink(path) == 0) {
return 0;
}
return rmdir(path);
}