1 /*
2 * Copyright (C) 2014 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 #include "epoll_emulation.h"
18
19 #include <linux/unistd.h>
20 #include <sys/epoll.h>
21 #include <unistd.h>
22
23 #include <cerrno>
24 #include <cstring>
25
26 #include "berberis/base/bit_util.h"
27 #include "berberis/kernel_api/tracing.h"
28
29 #include "guest_types.h"
30
31 namespace berberis {
32
33 namespace {
34
35 // Note that we are doing somewhat dangerous operation here. We are converting array of epoll_event
36 // structs into array of Guest_epoll_event structs in place. This works because Guest_epoll_event
37 // is larger than epoll_event but contain the same data types, only alignment differs.
ConvertHostEPollEventArrayToGuestInPlace(Guest_epoll_event * guest_events,int count)38 void ConvertHostEPollEventArrayToGuestInPlace(Guest_epoll_event* guest_events, int count) {
39 auto host_events = reinterpret_cast<epoll_event*>(guest_events);
40
41 // Handle negative count safely!
42 while (count-- > 0) {
43 // Use memmove to guarantee that there wouldn't any aliasing issues.
44 //
45 // Copy "data" first, "event" second because this guarantees that we always copy
46 // data "down" and don't rely on padding.
47 //
48 // CHECK_FIELD_LAYOUT checks in epoll_emulation.h guarantee that offsetof "data"
49 // is larger than offsetof "event" and that offsetof fields on host are not
50 // larger than offsetof fields on guest.
51 memmove(&guest_events[count].data, &host_events[count].data, sizeof(guest_events[count].data));
52 memmove(&guest_events[count].events,
53 &host_events[count].events,
54 sizeof(guest_events[count].events));
55 }
56 }
57
58 } // namespace
59
RunGuestSyscall___NR_epoll_ctl(long arg_1,long arg_2,long arg_3,long arg_4)60 long RunGuestSyscall___NR_epoll_ctl(long arg_1, long arg_2, long arg_3, long arg_4) {
61 if (arg_4 == 0) {
62 return syscall(__NR_epoll_ctl, arg_1, arg_2, arg_3, nullptr);
63 }
64
65 Guest_epoll_event* guest_event = bit_cast<Guest_epoll_event*>(arg_4);
66 epoll_event host_event;
67 host_event.events = guest_event->events;
68 host_event.data.u64 = guest_event->data;
69 return syscall(__NR_epoll_ctl, arg_1, arg_2, arg_3, &host_event);
70 }
71
RunGuestSyscall___NR_epoll_pwait(long arg_1,long arg_2,long arg_3,long arg_4,long arg_5,long arg_6)72 long RunGuestSyscall___NR_epoll_pwait(long arg_1,
73 long arg_2,
74 long arg_3,
75 long arg_4,
76 long arg_5,
77 long arg_6) {
78 long res = syscall(__NR_epoll_pwait, arg_1, arg_2, arg_3, arg_4, arg_5, arg_6);
79 if (res != -1 && arg_2 != 0) {
80 ConvertHostEPollEventArrayToGuestInPlace(bit_cast<Guest_epoll_event*>(arg_2), arg_3);
81 }
82 return res;
83 }
84
RunGuestSyscall___NR_epoll_pwait2(long,long,long,long,long,long)85 long RunGuestSyscall___NR_epoll_pwait2(long, long, long, long, long, long) {
86 KAPI_TRACE("unsupported syscall __NR_epoll_pwait2");
87 errno = ENOSYS;
88 return -1;
89 }
90
91 } // namespace berberis
92