lwp_syscall.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include <dfs_poll.h>
  28. #if (defined(RT_USING_SAL) && defined(SAL_USING_POSIX))
  29. #include <sys/socket.h>
  30. #define SYSCALL_NET(f) (f)
  31. #else
  32. #define SYSCALL_NET(f) (sys_notimpl)
  33. #endif
  34. #define DBG_ENABLE
  35. #define DBG_SECTION_NAME "LWP_CALL"
  36. #define DBG_COLOR
  37. #define DBG_LEVEL DBG_WARNING
  38. #include <rtdbg.h>
  39. /* thread/process */
  40. void sys_exit(int value)
  41. {
  42. /* TODO: handle the return_value */
  43. dbg_log(DBG_LOG, "enter sys_exit\n");
  44. rt_thread_delete(rt_thread_self());
  45. rt_schedule();
  46. return;
  47. }
  48. /* syscall: "read" ret: "ssize_t" args: "int" "void *" "size_t" */
  49. ssize_t sys_read(int fd, void *buf, size_t nbyte)
  50. {
  51. return read(fd, buf, nbyte);
  52. }
  53. /* syscall: "write" ret: "ssize_t" args: "int" "const void *" "size_t" */
  54. ssize_t sys_write(int fd, const void *buf, size_t nbyte)
  55. {
  56. return write(fd, buf, nbyte);
  57. }
  58. /* syscall: "lseek" ret: "off_t" args: "int" "off_t" "int" */
  59. off_t sys_lseek(int fd, off_t offset, int whence)
  60. {
  61. return lseek(fd, offset, whence);
  62. }
  63. /* syscall: "open" ret: "int" args: "const char *" "int" "..." */
  64. int sys_open(const char *name, int mode, ...)
  65. {
  66. return open(name, mode, 0);
  67. }
  68. /* syscall: "close" ret: "int" args: "int" */
  69. int sys_close(int fd)
  70. {
  71. return close(fd);
  72. }
  73. /* syscall: "ioctl" ret: "int" args: "int" "u_long" "..." */
  74. int sys_ioctl(int fd, unsigned long cmd, void* data)
  75. {
  76. return ioctl(fd, cmd, data);
  77. }
  78. /* syscall: "nanosleep" ret: "int" args: "const struct timespec *" "struct timespec *" */
  79. int sys_nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
  80. {
  81. rt_tick_t tick;
  82. dbg_log(DBG_LOG, "sys_nanosleep\n");
  83. tick = rqtp->tv_sec * RT_TICK_PER_SECOND + (rqtp->tv_nsec * RT_TICK_PER_SECOND)/ 1000000000;
  84. rt_thread_delay(tick);
  85. if (rmtp)
  86. {
  87. tick = rt_tick_get() - tick;
  88. /* get the passed time */
  89. rmtp->tv_sec = tick/RT_TICK_PER_SECOND;
  90. rmtp->tv_nsec = (tick%RT_TICK_PER_SECOND) * (1000000000/RT_TICK_PER_SECOND);
  91. }
  92. return 0;
  93. }
  94. /* syscall: "getpriority" ret: "int" args: "int" "id_t" */
  95. int sys_getpriority(int which, id_t who)
  96. {
  97. if (which == PRIO_PROCESS)
  98. {
  99. rt_thread_t tid;
  100. tid = rt_thread_self();
  101. if (who == (id_t)tid || who == 0xff)
  102. {
  103. return tid->current_priority;
  104. }
  105. }
  106. return 0xff;
  107. }
  108. /* syscall: "setpriority" ret: "int" args: "int" "id_t" "int" */
  109. int sys_setpriority(int which, id_t who, int prio)
  110. {
  111. if (which == PRIO_PROCESS)
  112. {
  113. rt_thread_t tid;
  114. tid = rt_thread_self();
  115. if ((who == (id_t)tid || who == 0xff) && (prio >= 0 && prio < RT_THREAD_PRIORITY_MAX))
  116. {
  117. rt_thread_control(tid, RT_THREAD_CTRL_CHANGE_PRIORITY, &prio);
  118. return 0;
  119. }
  120. }
  121. return -1;
  122. }
  123. /* syscall: "gettimeofday" ret: "int" args: "struct timeval *" "struct timezone *" */
  124. int sys_gettimeofday(struct timeval *tp, struct timezone *tzp)
  125. {
  126. if (tp)
  127. {
  128. tp->tv_sec = rt_tick_get() / RT_TICK_PER_SECOND;
  129. tp->tv_usec = (rt_tick_get() % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND);
  130. }
  131. return 0;
  132. }
  133. /* syscall: "settimeofday" ret: "int" args: "const struct timeval *" "const struct timezone *" */
  134. int sys_settimeofday(const struct timeval *tv, const struct timezone *tzp)
  135. {
  136. return 0;
  137. }
  138. /* syscall: "msgget" ret: "int" args: "key_t" "int" */
  139. int sys_msgget(key_t key, int msgflg)
  140. {
  141. return -1;
  142. }
  143. /* syscall: "msgsnd" ret: "int" args: "int" "const void *" "size_t" "int" */
  144. int sys_msgsend(int msqid, const void *msgp, size_t msgsz, int msgflg)
  145. {
  146. return -1;
  147. }
  148. /* syscall: "msgrcv" ret: "int" args: "int" "void *" "size_t" "long" "int" */
  149. int sys_msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
  150. {
  151. return -1;
  152. }
  153. /* syscall: "sys_log" ret: "int" args: "const char*" "size" */
  154. int sys_log(const char* log, int size)
  155. {
  156. rt_device_t console = rt_console_get_device();
  157. if (console) rt_device_write(console, -1, log, size);
  158. return 0;
  159. }
  160. void *sys_malloc(size_t size)
  161. {
  162. return rt_lwp_mem_malloc(size);
  163. }
  164. void sys_free(void *addr)
  165. {
  166. rt_lwp_mem_free(addr);
  167. }
  168. void *sys_realloc(void *rmem, size_t newsize)
  169. {
  170. return rt_lwp_mem_realloc(rmem, newsize);
  171. }
  172. int sys_fstat(int file, struct stat *buf)
  173. {
  174. return fstat(file, buf);
  175. }
  176. int sys_notimpl(void)
  177. {
  178. return -ENOSYS;
  179. }
  180. const static void* func_table[] =
  181. {
  182. (void *)sys_exit, // 0x01
  183. (void *)sys_read, // 0x02
  184. (void *)sys_write, // 0x03
  185. (void *)sys_lseek, // 0x04
  186. (void *)sys_open, // 0x05
  187. (void *)sys_close, // 0x06
  188. (void *)sys_ioctl, // 0x07
  189. (void *)sys_nanosleep, // 0x08
  190. (void *)sys_getpriority, // 0x09
  191. (void *)sys_setpriority, // 0x0a
  192. (void *)sys_gettimeofday, // 0x0b
  193. (void *)sys_settimeofday, // 0x0c
  194. (void *)sys_malloc, // 0x0d
  195. (void *)sys_free, // 0x0e
  196. (void *)sys_realloc, //0x0f
  197. (void *)sys_fstat, // 0x10
  198. poll, // 0x11
  199. SYSCALL_NET(accept), // 0x12
  200. SYSCALL_NET(bind), // 0x13
  201. SYSCALL_NET(shutdown), // 0x14
  202. SYSCALL_NET(getpeername),// 0x15
  203. SYSCALL_NET(getsockname),// 0x16
  204. SYSCALL_NET(getsockopt), // 0x17
  205. SYSCALL_NET(setsockopt), // 0x18
  206. SYSCALL_NET(connect), // 0x19
  207. SYSCALL_NET(listen), // 0x1a
  208. SYSCALL_NET(recv), // 0x1b
  209. SYSCALL_NET(recvfrom), // 0x1c
  210. SYSCALL_NET(send), // 0x1d
  211. SYSCALL_NET(sendto), // 0x1e
  212. SYSCALL_NET(socket), // 0x1f
  213. };
  214. const void *lwp_get_sys_api(rt_uint32_t number)
  215. {
  216. const void *func = sys_notimpl;
  217. if (number == 0xff)
  218. {
  219. func = (void *)sys_log;
  220. }
  221. else
  222. {
  223. number -= 1;
  224. if (number < sizeof(func_table)/sizeof(func_table[0]))
  225. {
  226. func = func_table[number];
  227. }
  228. }
  229. return func;
  230. }