Syscall.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. struct stat;
  40. typedef u32 socklen_t;
  41. }
  42. namespace Kernel {
  43. #define ENUMERATE_SYSCALLS(S) \
  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(dup2) \
  82. S(sigaction) \
  83. S(umask) \
  84. S(getgroups) \
  85. S(setgroups) \
  86. S(sigreturn) \
  87. S(sigprocmask) \
  88. S(sigpending) \
  89. S(pipe) \
  90. S(killpg) \
  91. S(seteuid) \
  92. S(setegid) \
  93. S(setuid) \
  94. S(setgid) \
  95. S(setresuid) \
  96. S(setresgid) \
  97. S(alarm) \
  98. S(fstat) \
  99. S(access) \
  100. S(fcntl) \
  101. S(ioctl) \
  102. S(mkdir) \
  103. S(times) \
  104. S(utime) \
  105. S(sync) \
  106. S(ptsname) \
  107. S(select) \
  108. S(unlink) \
  109. S(poll) \
  110. S(rmdir) \
  111. S(chmod) \
  112. S(socket) \
  113. S(bind) \
  114. S(accept) \
  115. S(listen) \
  116. S(connect) \
  117. S(link) \
  118. S(chown) \
  119. S(fchmod) \
  120. S(symlink) \
  121. S(sendmsg) \
  122. S(recvmsg) \
  123. S(getsockopt) \
  124. S(setsockopt) \
  125. S(create_thread) \
  126. S(gettid) \
  127. S(donate) \
  128. S(rename) \
  129. S(ftruncate) \
  130. S(exit_thread) \
  131. S(mknod) \
  132. S(writev) \
  133. S(beep) \
  134. S(getsockname) \
  135. S(getpeername) \
  136. S(sched_setparam) \
  137. S(sched_getparam) \
  138. S(fchown) \
  139. S(halt) \
  140. S(reboot) \
  141. S(mount) \
  142. S(umount) \
  143. S(dump_backtrace) \
  144. S(dbgputch) \
  145. S(dbgputstr) \
  146. S(watch_file) \
  147. S(mprotect) \
  148. S(realpath) \
  149. S(get_process_name) \
  150. S(fchdir) \
  151. S(getrandom) \
  152. S(getkeymap) \
  153. S(setkeymap) \
  154. S(clock_gettime) \
  155. S(clock_settime) \
  156. S(clock_nanosleep) \
  157. S(join_thread) \
  158. S(module_load) \
  159. S(module_unload) \
  160. S(detach_thread) \
  161. S(set_thread_name) \
  162. S(get_thread_name) \
  163. S(madvise) \
  164. S(purge) \
  165. S(profiling_enable) \
  166. S(profiling_disable) \
  167. S(futex) \
  168. S(chroot) \
  169. S(pledge) \
  170. S(unveil) \
  171. S(perf_event) \
  172. S(shutdown) \
  173. S(get_stack_bounds) \
  174. S(ptrace) \
  175. S(sendfd) \
  176. S(recvfd) \
  177. S(sysconf) \
  178. S(set_process_name) \
  179. S(disown) \
  180. S(adjtime) \
  181. S(allocate_tls) \
  182. S(prctl) \
  183. S(mremap) \
  184. S(set_coredump_metadata) \
  185. S(abort) \
  186. S(anon_create) \
  187. S(msyscall) \
  188. S(readv) \
  189. S(emuctl)
  190. namespace Syscall {
  191. enum Function {
  192. #undef __ENUMERATE_SYSCALL
  193. #define __ENUMERATE_SYSCALL(x) SC_##x,
  194. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  195. #undef __ENUMERATE_SYSCALL
  196. __Count
  197. };
  198. constexpr const char* to_string(Function function)
  199. {
  200. switch (function) {
  201. #undef __ENUMERATE_SYSCALL
  202. #define __ENUMERATE_SYSCALL(x) \
  203. case SC_##x: \
  204. return #x;
  205. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  206. #undef __ENUMERATE_SYSCALL
  207. default:
  208. break;
  209. }
  210. return "Unknown";
  211. }
  212. #ifdef __serenity__
  213. struct StringArgument {
  214. const char* characters;
  215. size_t length { 0 };
  216. };
  217. template<typename DataType, typename SizeType>
  218. struct MutableBufferArgument {
  219. DataType* data { nullptr };
  220. SizeType size { 0 };
  221. };
  222. struct StringListArgument {
  223. StringArgument* strings {};
  224. size_t length { 0 };
  225. };
  226. struct SC_mmap_params {
  227. uintptr_t addr;
  228. size_t size;
  229. size_t alignment;
  230. int32_t prot;
  231. int32_t flags;
  232. int32_t fd;
  233. int64_t offset;
  234. StringArgument name;
  235. };
  236. struct SC_mremap_params {
  237. uintptr_t old_address;
  238. size_t old_size;
  239. size_t new_size;
  240. int32_t flags;
  241. };
  242. struct SC_open_params {
  243. int dirfd;
  244. StringArgument path;
  245. int options;
  246. u16 mode;
  247. };
  248. struct SC_select_params {
  249. int nfds;
  250. fd_set* readfds;
  251. fd_set* writefds;
  252. fd_set* exceptfds;
  253. const struct timespec* timeout;
  254. const u32* sigmask;
  255. };
  256. struct SC_poll_params {
  257. struct pollfd* fds;
  258. unsigned nfds;
  259. const struct timespec* timeout;
  260. const u32* sigmask;
  261. };
  262. struct SC_clock_nanosleep_params {
  263. int clock_id;
  264. int flags;
  265. const struct timespec* requested_sleep;
  266. struct timespec* remaining_sleep;
  267. };
  268. struct SC_getsockopt_params {
  269. int sockfd;
  270. int level;
  271. int option;
  272. void* value;
  273. socklen_t* value_size;
  274. };
  275. struct SC_setsockopt_params {
  276. int sockfd;
  277. int level;
  278. int option;
  279. const void* value;
  280. socklen_t value_size;
  281. };
  282. struct SC_getsockname_params {
  283. int sockfd;
  284. sockaddr* addr;
  285. socklen_t* addrlen;
  286. };
  287. struct SC_getpeername_params {
  288. int sockfd;
  289. sockaddr* addr;
  290. socklen_t* addrlen;
  291. };
  292. struct SC_futex_params {
  293. u32* userspace_address;
  294. int futex_op;
  295. u32 val;
  296. union {
  297. const timespec* timeout;
  298. uintptr_t val2;
  299. };
  300. u32* userspace_address2;
  301. u32 val3;
  302. };
  303. struct SC_setkeymap_params {
  304. const u32* map;
  305. const u32* shift_map;
  306. const u32* alt_map;
  307. const u32* altgr_map;
  308. const u32* shift_altgr_map;
  309. StringArgument map_name;
  310. };
  311. struct SC_getkeymap_params {
  312. u32* map;
  313. u32* shift_map;
  314. u32* alt_map;
  315. u32* altgr_map;
  316. u32* shift_altgr_map;
  317. MutableBufferArgument<char, size_t> map_name;
  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: Implement 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 * MiB; // Default PTHREAD_STACK_MIN
  330. 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. struct siginfo* infop;
  390. int options;
  391. };
  392. struct SC_stat_params {
  393. StringArgument path;
  394. struct stat* statbuf;
  395. int follow_symlinks;
  396. };
  397. struct SC_ptrace_params {
  398. int request;
  399. pid_t tid;
  400. u8* addr;
  401. int data;
  402. };
  403. struct SC_ptrace_peek_params {
  404. const u32* address;
  405. u32* out_data;
  406. };
  407. struct SC_set_coredump_metadata_params {
  408. StringArgument key;
  409. StringArgument value;
  410. };
  411. void initialize();
  412. int sync();
  413. inline uintptr_t invoke(Function function)
  414. {
  415. uintptr_t result;
  416. asm volatile("int $0x82"
  417. : "=a"(result)
  418. : "a"(function)
  419. : "memory");
  420. return result;
  421. }
  422. template<typename T1>
  423. inline uintptr_t invoke(Function function, T1 arg1)
  424. {
  425. uintptr_t result;
  426. asm volatile("int $0x82"
  427. : "=a"(result)
  428. : "a"(function), "d"((uintptr_t)arg1)
  429. : "memory");
  430. return result;
  431. }
  432. template<typename T1, typename T2>
  433. inline uintptr_t invoke(Function function, T1 arg1, T2 arg2)
  434. {
  435. uintptr_t result;
  436. asm volatile("int $0x82"
  437. : "=a"(result)
  438. : "a"(function), "d"((uintptr_t)arg1), "c"((uintptr_t)arg2)
  439. : "memory");
  440. return result;
  441. }
  442. template<typename T1, typename T2, typename T3>
  443. inline uintptr_t invoke(Function function, T1 arg1, T2 arg2, T3 arg3)
  444. {
  445. uintptr_t result;
  446. asm volatile("int $0x82"
  447. : "=a"(result)
  448. : "a"(function), "d"((uintptr_t)arg1), "c"((uintptr_t)arg2), "b"((uintptr_t)arg3)
  449. : "memory");
  450. return result;
  451. }
  452. #endif
  453. }
  454. #undef __ENUMERATE_SYSCALL
  455. #define __ENUMERATE_SYSCALL(x) using Syscall::SC_##x;
  456. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  457. #undef __ENUMERATE_SYSCALL
  458. }
  459. using namespace Kernel;