syscall_generic.h 1.8 KB

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