lwp_syscall.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * File : lwp_syscall.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. */
  23. /* RT-Thread System call */
  24. #include <lwp.h>
  25. #include <lwp_mem.h>
  26. #include <lwp_syscall.h>
  27. #define DBG_ENABLE
  28. #define DBG_SECTION_NAME "[LWP_CALL]"
  29. #define DBG_COLOR
  30. #define DBG_LEVEL DBG_WARNING
  31. #include <rtdbg.h>
  32. /* thread/process */
  33. void sys_exit(int value)
  34. {
  35. /* TODO: handle the return_value */
  36. dbg_log(DBG_LOG, "enter sys_exit\n");
  37. rt_thread_delete(rt_thread_self());
  38. rt_schedule();
  39. return;
  40. }
  41. /* syscall: "read" ret: "ssize_t" args: "int" "void *" "size_t" */
  42. ssize_t sys_read(int fd, void *buf, size_t nbyte)
  43. {
  44. return read(fd, buf, nbyte);
  45. }
  46. /* syscall: "write" ret: "ssize_t" args: "int" "const void *" "size_t" */
  47. ssize_t sys_write(int fd, const void *buf, size_t nbyte)
  48. {
  49. return write(fd, buf, nbyte);
  50. }
  51. /* syscall: "lseek" ret: "off_t" args: "int" "off_t" "int" */
  52. off_t sys_lseek(int fd, off_t offset, int whence)
  53. {
  54. return lseek(fd, offset, whence);
  55. }
  56. /* syscall: "open" ret: "int" args: "const char *" "int" "..." */
  57. int sys_open(const char *name, int mode, ...)
  58. {
  59. return open(name, mode, 0);
  60. }
  61. /* syscall: "close" ret: "int" args: "int" */
  62. int sys_close(int fd)
  63. {
  64. return close(fd);
  65. }
  66. /* syscall: "ioctl" ret: "int" args: "int" "u_long" "..." */
  67. int sys_ioctl(int fd, unsigned long cmd, void* data)
  68. {
  69. return ioctl(fd, cmd, data);
  70. }
  71. /* syscall: "nanosleep" ret: "int" args: "const struct timespec *" "struct timespec *" */
  72. int sys_nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  73. {
  74. rt_tick_t tick;
  75. dbg_log(DBG_LOG, "sys_nanosleep\n");
  76. tick = rqtp->tv_sec * RT_TICK_PER_SECOND + (rqtp->tv_nsec * RT_TICK_PER_SECOND)/ 1000000000;
  77. rt_thread_delay(tick);
  78. if (rmtp)
  79. {
  80. tick = rt_tick_get() - tick;
  81. /* get the passed time */
  82. rmtp->tv_sec = tick/RT_TICK_PER_SECOND;
  83. rmtp->tv_nsec = (tick%RT_TICK_PER_SECOND) * (1000000000/RT_TICK_PER_SECOND);
  84. }
  85. return 0;
  86. }
  87. /* syscall: "getpriority" ret: "int" args: "int" "id_t" */
  88. int sys_getpriority(int which, id_t who)
  89. {
  90. if (which == PRIO_PROCESS)
  91. {
  92. rt_thread_t tid;
  93. tid = rt_thread_self();
  94. if (who == (id_t)tid || who == 0xff)
  95. {
  96. return tid->current_priority;
  97. }
  98. }
  99. return 0xff;
  100. }
  101. /* syscall: "setpriority" ret: "int" args: "int" "id_t" "int" */
  102. int sys_setpriority(int which, id_t who, int prio)
  103. {
  104. if (which == PRIO_PROCESS)
  105. {
  106. rt_thread_t tid;
  107. tid = rt_thread_self();
  108. if ((who == (id_t)tid || who == 0xff) && (prio >= 0 && prio < RT_THREAD_PRIORITY_MAX))
  109. {
  110. rt_thread_control(tid, RT_THREAD_CTRL_CHANGE_PRIORITY, &prio);
  111. return 0;
  112. }
  113. }
  114. return -1;
  115. }
  116. /* syscall: "gettimeofday" ret: "int" args: "struct timeval *" "struct timezone *" */
  117. int sys_gettimeofday(struct timeval *tp, struct timezone *tzp)
  118. {
  119. if (tp)
  120. {
  121. tp->tv_sec = rt_tick_get() / RT_TICK_PER_SECOND;
  122. tp->tv_usec = (rt_tick_get() % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND);
  123. }
  124. return 0;
  125. }
  126. /* syscall: "settimeofday" ret: "int" args: "const struct timeval *" "const struct timezone *" */
  127. int sys_settimeofday(const struct timeval *tv, const struct timezone *tzp)
  128. {
  129. return 0;
  130. }
  131. /* syscall: "msgget" ret: "int" args: "key_t" "int" */
  132. int sys_msgget(key_t key, int msgflg)
  133. {
  134. return -1;
  135. }
  136. /* syscall: "msgsnd" ret: "int" args: "int" "const void *" "size_t" "int" */
  137. int sys_msgsend(int msqid, const void *msgp, size_t msgsz, int msgflg)
  138. {
  139. return -1;
  140. }
  141. /* syscall: "msgrcv" ret: "int" args: "int" "void *" "size_t" "long" "int" */
  142. int sys_msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
  143. {
  144. return -1;
  145. }
  146. /* syscall: "sys_log" ret: "int" args: "const char*" "size" */
  147. int sys_log(const char* log, int size)
  148. {
  149. rt_device_t console = rt_console_get_device();
  150. if (console) rt_device_write(console, -1, log, size);
  151. return 0;
  152. }
  153. void *sys_malloc(size_t size)
  154. {
  155. return rt_lwp_mem_malloc(size);
  156. }
  157. void sys_free(void *addr)
  158. {
  159. rt_lwp_mem_free(addr);
  160. }
  161. void *sys_realloc(void *rmem, size_t newsize)
  162. {
  163. return rt_lwp_mem_realloc(rmem, newsize);
  164. }
  165. const static void* func_table[] =
  166. {
  167. (void *)sys_exit, // 0x01
  168. (void *)sys_read, // 0x02
  169. (void *)sys_write, // 0x03
  170. (void *)sys_lseek, // 0x04
  171. (void *)sys_open, // 0x05
  172. (void *)sys_close, // 0x06
  173. (void *)sys_ioctl, // 0x07
  174. (void *)sys_nanosleep, // 0x08
  175. (void *)sys_getpriority, // 0x09
  176. (void *)sys_setpriority, // 0x0a
  177. (void *)sys_gettimeofday, // 0x0b
  178. (void *)sys_settimeofday, // 0x0c
  179. (void *)sys_malloc, // 0x0d
  180. (void *)sys_free, // 0x0e
  181. };
  182. const void *lwp_get_sys_api(rt_uint32_t number)
  183. {
  184. const void *func = RT_NULL;
  185. if (number == 0xff)
  186. {
  187. func = (void *)sys_log;
  188. }
  189. else
  190. {
  191. number -= 1;
  192. if (number < sizeof(func_table)/sizeof(func_table[0]))
  193. {
  194. func = func_table[number];
  195. }
  196. }
  197. return func;
  198. }