lwp_syscall.c 7.1 KB

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