Syscall.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. namespace Syscall {
  190. enum Function {
  191. #undef __ENUMERATE_SYSCALL
  192. #define __ENUMERATE_SYSCALL(x) SC_##x,
  193. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  194. #undef __ENUMERATE_SYSCALL
  195. __Count
  196. };
  197. constexpr const char* to_string(Function function)
  198. {
  199. switch (function) {
  200. #undef __ENUMERATE_SYSCALL
  201. #define __ENUMERATE_SYSCALL(x) \
  202. case SC_##x: \
  203. return #x;
  204. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  205. #undef __ENUMERATE_SYSCALL
  206. default:
  207. break;
  208. }
  209. return "Unknown";
  210. }
  211. #ifdef __serenity__
  212. struct StringArgument {
  213. const char* characters;
  214. size_t length { 0 };
  215. };
  216. template<typename DataType, typename SizeType>
  217. struct MutableBufferArgument {
  218. DataType* data { nullptr };
  219. SizeType size { 0 };
  220. };
  221. struct StringListArgument {
  222. StringArgument* strings {};
  223. size_t length { 0 };
  224. };
  225. struct SC_mmap_params {
  226. uintptr_t addr;
  227. size_t size;
  228. size_t alignment;
  229. int32_t prot;
  230. int32_t flags;
  231. int32_t fd;
  232. ssize_t offset;
  233. StringArgument name;
  234. };
  235. struct SC_mremap_params {
  236. uintptr_t old_address;
  237. size_t old_size;
  238. size_t new_size;
  239. int32_t flags;
  240. };
  241. struct SC_open_params {
  242. int dirfd;
  243. StringArgument path;
  244. int options;
  245. u16 mode;
  246. };
  247. struct SC_select_params {
  248. int nfds;
  249. fd_set* readfds;
  250. fd_set* writefds;
  251. fd_set* exceptfds;
  252. const struct timespec* timeout;
  253. const u32* sigmask;
  254. };
  255. struct SC_poll_params {
  256. struct pollfd* fds;
  257. unsigned nfds;
  258. const struct timespec* timeout;
  259. const u32* sigmask;
  260. };
  261. struct SC_clock_nanosleep_params {
  262. int clock_id;
  263. int flags;
  264. const struct timespec* requested_sleep;
  265. struct timespec* remaining_sleep;
  266. };
  267. struct SC_getsockopt_params {
  268. int sockfd;
  269. int level;
  270. int option;
  271. void* value;
  272. socklen_t* value_size;
  273. };
  274. struct SC_setsockopt_params {
  275. int sockfd;
  276. int level;
  277. int option;
  278. const void* value;
  279. socklen_t value_size;
  280. };
  281. struct SC_getsockname_params {
  282. int sockfd;
  283. sockaddr* addr;
  284. socklen_t* addrlen;
  285. };
  286. struct SC_getpeername_params {
  287. int sockfd;
  288. sockaddr* addr;
  289. socklen_t* addrlen;
  290. };
  291. struct SC_futex_params {
  292. u32* userspace_address;
  293. int futex_op;
  294. u32 val;
  295. union {
  296. const timespec* timeout;
  297. uintptr_t val2;
  298. };
  299. u32* userspace_address2;
  300. u32 val3;
  301. };
  302. struct SC_setkeymap_params {
  303. const u32* map;
  304. const u32* shift_map;
  305. const u32* alt_map;
  306. const u32* altgr_map;
  307. const u32* shift_altgr_map;
  308. StringArgument map_name;
  309. };
  310. struct SC_getkeymap_params {
  311. u32* map;
  312. u32* shift_map;
  313. u32* alt_map;
  314. u32* altgr_map;
  315. u32* shift_altgr_map;
  316. MutableBufferArgument<char, size_t> map_name;
  317. };
  318. struct SC_create_thread_params {
  319. unsigned int m_detach_state = 0; // JOINABLE or DETACHED
  320. int m_schedule_priority = 30; // THREAD_PRIORITY_NORMAL
  321. // FIXME: Implement guard pages in create_thread (unreadable pages at "overflow" end of stack)
  322. // "If an implementation rounds up the value of guardsize to a multiple of {PAGESIZE},
  323. // a call to pthread_attr_getguardsize() specifying attr shall store in the guardsize
  324. // parameter the guard size specified by the previous pthread_attr_setguardsize() function call"
  325. // ... ok, if you say so posix. Guess we get to lie to people about guard page size
  326. unsigned int m_guard_page_size = 0; // Rounded up to PAGE_SIZE
  327. unsigned int m_reported_guard_page_size = 0; // The lie we tell callers
  328. unsigned int m_stack_size = 4 * MiB; // Default PTHREAD_STACK_MIN
  329. void* m_stack_location; // nullptr means any, o.w. process virtual address
  330. };
  331. struct SC_realpath_params {
  332. StringArgument path;
  333. MutableBufferArgument<char, size_t> buffer;
  334. };
  335. struct SC_set_mmap_name_params {
  336. void* addr;
  337. size_t size;
  338. StringArgument name;
  339. };
  340. struct SC_execve_params {
  341. StringArgument path;
  342. StringListArgument arguments;
  343. StringListArgument environment;
  344. };
  345. struct SC_readlink_params {
  346. StringArgument path;
  347. MutableBufferArgument<char, size_t> buffer;
  348. };
  349. struct SC_link_params {
  350. StringArgument old_path;
  351. StringArgument new_path;
  352. };
  353. struct SC_chown_params {
  354. StringArgument path;
  355. u32 uid;
  356. u32 gid;
  357. };
  358. struct SC_mknod_params {
  359. StringArgument path;
  360. u16 mode;
  361. u32 dev;
  362. };
  363. struct SC_symlink_params {
  364. StringArgument target;
  365. StringArgument linkpath;
  366. };
  367. struct SC_rename_params {
  368. StringArgument old_path;
  369. StringArgument new_path;
  370. };
  371. struct SC_mount_params {
  372. int source_fd;
  373. StringArgument target;
  374. StringArgument fs_type;
  375. int flags;
  376. };
  377. struct SC_pledge_params {
  378. StringArgument promises;
  379. StringArgument execpromises;
  380. };
  381. struct SC_unveil_params {
  382. StringArgument path;
  383. StringArgument permissions;
  384. };
  385. struct SC_waitid_params {
  386. int idtype;
  387. int id;
  388. struct siginfo* infop;
  389. int options;
  390. };
  391. struct SC_stat_params {
  392. StringArgument path;
  393. struct stat* statbuf;
  394. bool follow_symlinks;
  395. };
  396. struct SC_ptrace_params {
  397. int request;
  398. pid_t tid;
  399. u8* addr;
  400. int data;
  401. };
  402. struct SC_ptrace_peek_params {
  403. const u32* address;
  404. u32* out_data;
  405. };
  406. struct SC_set_coredump_metadata_params {
  407. StringArgument key;
  408. StringArgument value;
  409. };
  410. void initialize();
  411. int sync();
  412. inline uintptr_t invoke(Function function)
  413. {
  414. uintptr_t result;
  415. asm volatile("int $0x82"
  416. : "=a"(result)
  417. : "a"(function)
  418. : "memory");
  419. return result;
  420. }
  421. template<typename T1>
  422. inline uintptr_t invoke(Function function, T1 arg1)
  423. {
  424. uintptr_t result;
  425. asm volatile("int $0x82"
  426. : "=a"(result)
  427. : "a"(function), "d"((uintptr_t)arg1)
  428. : "memory");
  429. return result;
  430. }
  431. template<typename T1, typename T2>
  432. inline uintptr_t invoke(Function function, T1 arg1, T2 arg2)
  433. {
  434. uintptr_t result;
  435. asm volatile("int $0x82"
  436. : "=a"(result)
  437. : "a"(function), "d"((uintptr_t)arg1), "c"((uintptr_t)arg2)
  438. : "memory");
  439. return result;
  440. }
  441. template<typename T1, typename T2, typename T3>
  442. inline uintptr_t invoke(Function function, T1 arg1, T2 arg2, T3 arg3)
  443. {
  444. uintptr_t result;
  445. asm volatile("int $0x82"
  446. : "=a"(result)
  447. : "a"(function), "d"((uintptr_t)arg1), "c"((uintptr_t)arg2), "b"((uintptr_t)arg3)
  448. : "memory");
  449. return result;
  450. }
  451. #endif
  452. }
  453. #undef __ENUMERATE_SYSCALL
  454. #define __ENUMERATE_SYSCALL(x) using Syscall::SC_##x;
  455. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  456. #undef __ENUMERATE_SYSCALL
  457. }
  458. using namespace Kernel;