syscall_generic.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <rtthread.h>
  14. #include <errno.h>
  15. #include <stdlib.h>
  16. typedef long sysret_t;
  17. struct rt_syscall_def
  18. {
  19. void *func;
  20. char *name;
  21. };
  22. /**
  23. * @brief signature for syscall, used to locate syscall metadata.
  24. *
  25. * We don't allocate an exclusive section in ELF like Linux do
  26. * to avoid initializing necessary data by iterating that section,
  27. * which increases system booting time. We signature a pointer
  28. * just below each syscall entry in syscall table to make it
  29. * easy to locate every syscall's metadata by using syscall id.
  30. */
  31. #define SYSCALL_SIGN(func) { \
  32. (void *)(func), \
  33. &RT_STRINGIFY(func)[4], \
  34. }
  35. #define SET_ERRNO(no) rt_set_errno(-(no))
  36. #define GET_ERRNO() ({int _errno = rt_get_errno(); _errno > 0 ? -_errno : _errno;})
  37. #define _SYS_WRAP(func) ({int _ret = func; _ret < 0 ? GET_ERRNO() : _ret;})
  38. rt_inline sysret_t lwp_errno_to_posix(rt_err_t error)
  39. {
  40. sysret_t posix_rc;
  41. switch (labs(error))
  42. {
  43. case RT_EOK:
  44. posix_rc = 0;
  45. break;
  46. case RT_ETIMEOUT:
  47. posix_rc = -ETIMEDOUT;
  48. break;
  49. case RT_EINVAL:
  50. posix_rc = -EINVAL;
  51. break;
  52. case RT_ENOENT:
  53. posix_rc = -ENOENT;
  54. break;
  55. case RT_ENOSPC:
  56. posix_rc = -ENOSPC;
  57. break;
  58. case RT_EPERM:
  59. posix_rc = -EPERM;
  60. break;
  61. default:
  62. posix_rc = -1;
  63. break;
  64. }
  65. return posix_rc;
  66. }
  67. #endif /* __SYSCALL_DATA_H__ */