1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef BERBERIS_KERNEL_API_SYSCALL_EMULATION_COMMON_H_
18 #define BERBERIS_KERNEL_API_SYSCALL_EMULATION_COMMON_H_
19
20 #include <sys/syscall.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <cerrno>
25
26 #include "berberis/base/bit_util.h"
27 #include "berberis/base/macros.h"
28 #include "berberis/kernel_api/exec_emulation.h"
29 #include "berberis/kernel_api/fcntl_emulation.h"
30 #include "berberis/kernel_api/open_emulation.h"
31 #include "berberis/kernel_api/sys_prctl_emulation.h"
32 #include "berberis/kernel_api/sys_ptrace_emulation.h"
33 #include "berberis/kernel_api/tracing.h"
34 #include "berberis/kernel_api/unistd_emulation.h"
35
36 namespace berberis {
37
RunGuestSyscall___NR_clone3(long arg_1,long arg_2)38 inline long RunGuestSyscall___NR_clone3(long arg_1, long arg_2) {
39 UNUSED(arg_1, arg_2);
40 KAPI_TRACE("unimplemented syscall __NR_clone3");
41 errno = ENOSYS;
42 return -1;
43 }
44
RunGuestSyscall___NR_execve(long arg_1,long arg_2,long arg_3)45 inline long RunGuestSyscall___NR_execve(long arg_1, long arg_2, long arg_3) {
46 return static_cast<long>(ExecveForGuest(bit_cast<const char*>(arg_1), // filename
47 bit_cast<char* const*>(arg_2), // argv
48 bit_cast<char* const*>(arg_3))); // envp
49 }
50
RunGuestSyscall___NR_faccessat(long arg_1,long arg_2,long arg_3)51 inline long RunGuestSyscall___NR_faccessat(long arg_1, long arg_2, long arg_3) {
52 // TODO(b/128614662): translate!
53 KAPI_TRACE("unimplemented syscall __NR_faccessat, running host syscall as is");
54 return syscall(__NR_faccessat, arg_1, arg_2, arg_3);
55 }
56
RunGuestSyscall___NR_fcntl(long arg_1,long arg_2,long arg_3)57 inline long RunGuestSyscall___NR_fcntl(long arg_1, long arg_2, long arg_3) {
58 return GuestFcntl(arg_1, arg_2, arg_3);
59 }
60
RunGuestSyscall___NR_openat(long arg_1,long arg_2,long arg_3,long arg_4)61 inline long RunGuestSyscall___NR_openat(long arg_1, long arg_2, long arg_3, long arg_4) {
62 return static_cast<long>(OpenatForGuest(static_cast<int>(arg_1), // dirfd
63 bit_cast<const char*>(arg_2), // path
64 static_cast<int>(arg_3), // flags
65 static_cast<mode_t>(arg_4))); // mode
66 }
67
RunGuestSyscall___NR_prctl(long arg_1,long arg_2,long arg_3,long arg_4,long arg_5)68 inline long RunGuestSyscall___NR_prctl(long arg_1, long arg_2, long arg_3, long arg_4, long arg_5) {
69 return PrctlForGuest(arg_1, arg_2, arg_3, arg_4, arg_5);
70 }
71
RunGuestSyscall___NR_ptrace(long arg_1,long arg_2,long arg_3,long arg_4)72 inline long RunGuestSyscall___NR_ptrace(long arg_1, long arg_2, long arg_3, long arg_4) {
73 return static_cast<long>(PtraceForGuest(static_cast<int>(arg_1), // request
74 static_cast<pid_t>(arg_2), // pid
75 bit_cast<void*>(arg_3), // addr
76 bit_cast<void*>(arg_4))); // data
77 }
78
RunGuestSyscall___NR_readlinkat(long arg_1,long arg_2,long arg_3,long arg_4)79 inline long RunGuestSyscall___NR_readlinkat(long arg_1, long arg_2, long arg_3, long arg_4) {
80 return static_cast<long>(ReadLinkAtForGuest(static_cast<int>(arg_1), // dirfd
81 bit_cast<const char*>(arg_2), // path
82 bit_cast<char*>(arg_3), // buf
83 bit_cast<size_t>(arg_4))); // buf_size
84 }
85
RunGuestSyscall___NR_rt_sigreturn(long)86 inline long RunGuestSyscall___NR_rt_sigreturn(long) {
87 KAPI_TRACE("unsupported syscall __NR_rt_sigaction");
88 errno = ENOSYS;
89 return -1;
90 }
91
RunGuestSyscall___NR_statx(long arg_1,long arg_2,long arg_3,long arg_4,long arg_5)92 inline long RunGuestSyscall___NR_statx(long arg_1, long arg_2, long arg_3, long arg_4, long arg_5) {
93 #if defined(__NR_statx)
94 // TODO(b/128614662): add struct statx layout checkers.
95 return syscall(__NR_statx, arg_1, arg_2, arg_3, arg_4, arg_5);
96 #else
97 UNUSED(arg_1, arg_2, arg_3, arg_4, arg_5);
98 errno = ENOSYS;
99 return -1;
100 #endif
101 }
102
RunUnknownGuestSyscall(long guest_nr,long arg_1,long arg_2,long arg_3,long arg_4,long arg_5,long arg_6)103 long RunUnknownGuestSyscall(long guest_nr,
104 long arg_1,
105 long arg_2,
106 long arg_3,
107 long arg_4,
108 long arg_5,
109 long arg_6) {
110 UNUSED(arg_1, arg_2, arg_3, arg_4, arg_5, arg_6);
111 KAPI_TRACE("unknown syscall %ld", guest_nr);
112 errno = ENOSYS;
113 return -1;
114 }
115
116 } // namespace berberis
117
118 #endif // BERBERIS_KERNEL_API_SYSCALL_EMULATION_COMMON_H_
119