Syscall.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Types.h>
  28. #include <AK/Userspace.h>
  29. #ifdef __serenity__
  30. # include <LibC/fd_set.h>
  31. #endif
  32. constexpr int syscall_vector = 0x82;
  33. extern "C" {
  34. struct pollfd;
  35. struct timeval;
  36. struct timespec;
  37. struct sockaddr;
  38. struct siginfo;
  39. typedef u32 socklen_t;
  40. }
  41. namespace Kernel {
  42. #define ENUMERATE_SYSCALLS(S) \
  43. S(sleep) \
  44. S(yield) \
  45. S(open) \
  46. S(close) \
  47. S(read) \
  48. S(lseek) \
  49. S(kill) \
  50. S(getuid) \
  51. S(exit) \
  52. S(geteuid) \
  53. S(getegid) \
  54. S(getgid) \
  55. S(getpid) \
  56. S(getppid) \
  57. S(getresuid) \
  58. S(getresgid) \
  59. S(waitid) \
  60. S(mmap) \
  61. S(munmap) \
  62. S(get_dir_entries) \
  63. S(getcwd) \
  64. S(gettimeofday) \
  65. S(gethostname) \
  66. S(sethostname) \
  67. S(chdir) \
  68. S(uname) \
  69. S(set_mmap_name) \
  70. S(readlink) \
  71. S(write) \
  72. S(ttyname) \
  73. S(stat) \
  74. S(getsid) \
  75. S(setsid) \
  76. S(getpgid) \
  77. S(setpgid) \
  78. S(getpgrp) \
  79. S(fork) \
  80. S(execve) \
  81. S(dup) \
  82. S(dup2) \
  83. S(sigaction) \
  84. S(umask) \
  85. S(getgroups) \
  86. S(setgroups) \
  87. S(sigreturn) \
  88. S(sigprocmask) \
  89. S(sigpending) \
  90. S(pipe) \
  91. S(killpg) \
  92. S(seteuid) \
  93. S(setegid) \
  94. S(setuid) \
  95. S(setgid) \
  96. S(setresuid) \
  97. S(setresgid) \
  98. S(alarm) \
  99. S(fstat) \
  100. S(access) \
  101. S(fcntl) \
  102. S(ioctl) \
  103. S(mkdir) \
  104. S(times) \
  105. S(utime) \
  106. S(sync) \
  107. S(ptsname) \
  108. S(select) \
  109. S(unlink) \
  110. S(poll) \
  111. S(rmdir) \
  112. S(chmod) \
  113. S(usleep) \
  114. S(socket) \
  115. S(bind) \
  116. S(accept) \
  117. S(listen) \
  118. S(connect) \
  119. S(shbuf_create) \
  120. S(shbuf_allow_pid) \
  121. S(shbuf_get) \
  122. S(shbuf_release) \
  123. S(link) \
  124. S(chown) \
  125. S(fchmod) \
  126. S(symlink) \
  127. S(shbuf_seal) \
  128. S(sendto) \
  129. S(recvfrom) \
  130. S(getsockopt) \
  131. S(setsockopt) \
  132. S(create_thread) \
  133. S(gettid) \
  134. S(donate) \
  135. S(rename) \
  136. S(ftruncate) \
  137. S(exit_thread) \
  138. S(mknod) \
  139. S(writev) \
  140. S(beep) \
  141. S(getsockname) \
  142. S(getpeername) \
  143. S(sched_setparam) \
  144. S(sched_getparam) \
  145. S(fchown) \
  146. S(halt) \
  147. S(reboot) \
  148. S(mount) \
  149. S(umount) \
  150. S(dump_backtrace) \
  151. S(dbgputch) \
  152. S(dbgputstr) \
  153. S(watch_file) \
  154. S(shbuf_allow_all) \
  155. S(set_process_icon) \
  156. S(mprotect) \
  157. S(realpath) \
  158. S(get_process_name) \
  159. S(fchdir) \
  160. S(getrandom) \
  161. S(setkeymap) \
  162. S(clock_gettime) \
  163. S(clock_settime) \
  164. S(clock_nanosleep) \
  165. S(join_thread) \
  166. S(module_load) \
  167. S(module_unload) \
  168. S(detach_thread) \
  169. S(set_thread_name) \
  170. S(get_thread_name) \
  171. S(madvise) \
  172. S(purge) \
  173. S(shbuf_set_volatile) \
  174. S(profiling_enable) \
  175. S(profiling_disable) \
  176. S(futex) \
  177. S(set_thread_boost) \
  178. S(set_process_boost) \
  179. S(chroot) \
  180. S(pledge) \
  181. S(unveil) \
  182. S(perf_event) \
  183. S(shutdown) \
  184. S(get_stack_bounds) \
  185. S(ptrace) \
  186. S(minherit) \
  187. S(sendfd) \
  188. S(recvfd) \
  189. S(sysconf) \
  190. S(set_process_name) \
  191. S(disown)
  192. namespace Syscall {
  193. enum Function {
  194. #undef __ENUMERATE_SYSCALL
  195. #define __ENUMERATE_SYSCALL(x) SC_##x,
  196. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  197. #undef __ENUMERATE_SYSCALL
  198. __Count
  199. };
  200. inline constexpr const char* to_string(Function function)
  201. {
  202. switch (function) {
  203. #undef __ENUMERATE_SYSCALL
  204. #define __ENUMERATE_SYSCALL(x) \
  205. case SC_##x: \
  206. return #x;
  207. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  208. #undef __ENUMERATE_SYSCALL
  209. default:
  210. break;
  211. }
  212. return "Unknown";
  213. }
  214. #ifdef __serenity__
  215. struct StringArgument {
  216. Userspace<const char*> characters;
  217. size_t length { 0 };
  218. };
  219. template<typename DataType, typename SizeType>
  220. struct MutableBufferArgument {
  221. DataType* data { nullptr };
  222. SizeType size { 0 };
  223. };
  224. template<typename DataType, typename SizeType>
  225. struct ImmutableBufferArgument {
  226. const DataType* data { nullptr };
  227. SizeType size { 0 };
  228. };
  229. struct StringListArgument {
  230. StringArgument* strings { nullptr };
  231. size_t length { 0 };
  232. };
  233. struct SC_mmap_params {
  234. uint32_t addr;
  235. uint32_t size;
  236. uint32_t alignment;
  237. int32_t prot;
  238. int32_t flags;
  239. int32_t fd;
  240. int32_t offset; // FIXME: 64-bit off_t?
  241. StringArgument name;
  242. };
  243. struct SC_open_params {
  244. int dirfd;
  245. StringArgument path;
  246. int options;
  247. u16 mode;
  248. };
  249. struct SC_select_params {
  250. int nfds;
  251. fd_set* readfds;
  252. fd_set* writefds;
  253. fd_set* exceptfds;
  254. const struct timespec* timeout;
  255. const u32* sigmask;
  256. };
  257. struct SC_poll_params {
  258. struct pollfd* fds;
  259. unsigned nfds;
  260. const struct timespec* timeout;
  261. const u32* sigmask;
  262. };
  263. struct SC_clock_nanosleep_params {
  264. int clock_id;
  265. int flags;
  266. Userspace<const struct timespec*> requested_sleep;
  267. Userspace<struct timespec*> remaining_sleep;
  268. };
  269. struct SC_sendto_params {
  270. int sockfd;
  271. ImmutableBufferArgument<void, size_t> data;
  272. int flags;
  273. const sockaddr* addr;
  274. socklen_t addr_length;
  275. };
  276. struct SC_recvfrom_params {
  277. int sockfd;
  278. MutableBufferArgument<void, size_t> buffer;
  279. int flags;
  280. sockaddr* addr;
  281. socklen_t* addr_length;
  282. };
  283. struct SC_getsockopt_params {
  284. int sockfd;
  285. int level;
  286. int option;
  287. void* value;
  288. socklen_t* value_size;
  289. };
  290. struct SC_setsockopt_params {
  291. int sockfd;
  292. int level;
  293. int option;
  294. const void* value;
  295. socklen_t value_size;
  296. };
  297. struct SC_getsockname_params {
  298. int sockfd;
  299. sockaddr* addr;
  300. socklen_t* addrlen;
  301. };
  302. struct SC_getpeername_params {
  303. int sockfd;
  304. sockaddr* addr;
  305. socklen_t* addrlen;
  306. };
  307. struct SC_futex_params {
  308. i32* userspace_address;
  309. int futex_op;
  310. i32 val;
  311. const timespec* timeout;
  312. };
  313. struct SC_setkeymap_params {
  314. Userspace<const u32*> map;
  315. Userspace<const u32*> shift_map;
  316. Userspace<const u32*> alt_map;
  317. Userspace<const u32*> altgr_map;
  318. };
  319. struct SC_create_thread_params {
  320. unsigned int m_detach_state = 0; // JOINABLE or DETACHED
  321. int m_schedule_priority = 30; // THREAD_PRIORITY_NORMAL
  322. // FIXME: Implment guard pages in create_thread (unreadable pages at "overflow" end of stack)
  323. // "If an implementation rounds up the value of guardsize to a multiple of {PAGESIZE},
  324. // a call to pthread_attr_getguardsize() specifying attr shall store in the guardsize
  325. // parameter the guard size specified by the previous pthread_attr_setguardsize() function call"
  326. // ... ok, if you say so posix. Guess we get to lie to people about guard page size
  327. unsigned int m_guard_page_size = 0; // Rounded up to PAGE_SIZE
  328. unsigned int m_reported_guard_page_size = 0; // The lie we tell callers
  329. unsigned int m_stack_size = 4 * MB; // Default PTHREAD_STACK_MIN
  330. Userspace<void*> m_stack_location; // nullptr means any, o.w. process virtual address
  331. };
  332. struct SC_realpath_params {
  333. StringArgument path;
  334. MutableBufferArgument<char, size_t> buffer;
  335. };
  336. struct SC_set_mmap_name_params {
  337. void* addr;
  338. size_t size;
  339. StringArgument name;
  340. };
  341. struct SC_execve_params {
  342. StringArgument path;
  343. StringListArgument arguments;
  344. StringListArgument environment;
  345. };
  346. struct SC_readlink_params {
  347. StringArgument path;
  348. MutableBufferArgument<char, size_t> buffer;
  349. };
  350. struct SC_link_params {
  351. StringArgument old_path;
  352. StringArgument new_path;
  353. };
  354. struct SC_chown_params {
  355. StringArgument path;
  356. u32 uid;
  357. u32 gid;
  358. };
  359. struct SC_mknod_params {
  360. StringArgument path;
  361. u16 mode;
  362. u32 dev;
  363. };
  364. struct SC_symlink_params {
  365. StringArgument target;
  366. StringArgument linkpath;
  367. };
  368. struct SC_rename_params {
  369. StringArgument old_path;
  370. StringArgument new_path;
  371. };
  372. struct SC_mount_params {
  373. int source_fd;
  374. StringArgument target;
  375. StringArgument fs_type;
  376. int flags;
  377. };
  378. struct SC_pledge_params {
  379. StringArgument promises;
  380. StringArgument execpromises;
  381. };
  382. struct SC_unveil_params {
  383. StringArgument path;
  384. StringArgument permissions;
  385. };
  386. struct SC_waitid_params {
  387. int idtype;
  388. int id;
  389. Userspace<struct siginfo*> infop;
  390. int options;
  391. };
  392. struct SC_stat_params {
  393. StringArgument path;
  394. Userspace<struct stat*> statbuf;
  395. bool follow_symlinks;
  396. };
  397. struct SC_ptrace_params {
  398. int request;
  399. pid_t pid;
  400. Userspace<u8*> addr;
  401. int data;
  402. };
  403. struct SC_ptrace_peek_params {
  404. Userspace<const u32*> address;
  405. Userspace<u32*> out_data;
  406. };
  407. void initialize();
  408. int sync();
  409. inline u32 invoke(Function function)
  410. {
  411. u32 result;
  412. asm volatile("int $0x82"
  413. : "=a"(result)
  414. : "a"(function)
  415. : "memory");
  416. return result;
  417. }
  418. template<typename T1>
  419. inline u32 invoke(Function function, T1 arg1)
  420. {
  421. u32 result;
  422. asm volatile("int $0x82"
  423. : "=a"(result)
  424. : "a"(function), "d"((u32)arg1)
  425. : "memory");
  426. return result;
  427. }
  428. template<typename T1, typename T2>
  429. inline u32 invoke(Function function, T1 arg1, T2 arg2)
  430. {
  431. u32 result;
  432. asm volatile("int $0x82"
  433. : "=a"(result)
  434. : "a"(function), "d"((u32)arg1), "c"((u32)arg2)
  435. : "memory");
  436. return result;
  437. }
  438. template<typename T1, typename T2, typename T3>
  439. inline u32 invoke(Function function, T1 arg1, T2 arg2, T3 arg3)
  440. {
  441. u32 result;
  442. asm volatile("int $0x82"
  443. : "=a"(result)
  444. : "a"(function), "d"((u32)arg1), "c"((u32)arg2), "b"((u32)arg3)
  445. : "memory");
  446. return result;
  447. }
  448. #endif
  449. }
  450. #undef __ENUMERATE_SYSCALL
  451. #define __ENUMERATE_SYSCALL(x) using Syscall::SC_##x;
  452. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  453. #undef __ENUMERATE_SYSCALL
  454. #define syscall Syscall::invoke
  455. }
  456. using namespace Kernel;