1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-11-10     RT-Thread    The first version
9  * 2023-03-13     WangXiaoyao  syscall metadata as structure
10  */
11 #ifndef __SYSCALL_DATA_H__
12 #define __SYSCALL_DATA_H__
13 
14 #include <rtthread.h>
15 
16 #include <errno.h>
17 #include <stdlib.h>
18 
19 /* signed ARCH related types */
20 typedef rt_base_t sysret_t;
21 
22 struct rt_syscall_def
23 {
24     void *func;
25     char *name;
26 };
27 
28 /**
29  * @brief signature for syscall, used to locate syscall metadata.
30  *
31  * We don't allocate an exclusive section in ELF like Linux do
32  * to avoid initializing necessary data by iterating that section,
33  * which increases system booting time. We signature a pointer
34  * just below each syscall entry in syscall table to make it
35  * easy to locate every syscall's metadata by using syscall id.
36  */
37 #define SYSCALL_SIGN(func) {    \
38     (void *)(func),             \
39     &RT_STRINGIFY(func)[4],     \
40 }
41 
42 #define SET_ERRNO(no) rt_set_errno(-(no))
43 #define GET_ERRNO() ({int _errno = rt_get_errno(); _errno > 0 ? -_errno : _errno;})
44 
45 #define _SYS_WRAP(func) ({int _ret = func; _ret < 0 ? GET_ERRNO() : _ret;})
46 
lwp_errno_to_posix(rt_err_t error)47 rt_inline sysret_t lwp_errno_to_posix(rt_err_t error)
48 {
49     sysret_t posix_rc;
50 
51     switch (labs(error))
52     {
53         case RT_EOK:
54             posix_rc = 0;
55             break;
56         case RT_ETIMEOUT:
57             posix_rc = -ETIMEDOUT;
58             break;
59         case RT_EINVAL:
60             posix_rc = -EINVAL;
61             break;
62         case RT_ENOENT:
63             posix_rc = -ENOENT;
64             break;
65         case RT_ENOSPC:
66             posix_rc = -ENOSPC;
67             break;
68         case RT_EPERM:
69             posix_rc = -EPERM;
70             break;
71         default:
72             posix_rc = -1;
73             break;
74     }
75 
76     return posix_rc;
77 }
78 
79 #endif /* __SYSCALL_DATA_H__ */
80