1 /*
2 * Copyright (C) 2016 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 "ProcessPipe.h"
18 #include "HostConnection.h"
19 #include "renderControl_enc.h"
20
21 #include <qemu_pipe_bp.h>
22
23 #if PLATFORM_SDK_VERSION < 26
24 #include <cutils/log.h>
25 #else
26 #include <log/log.h>
27 #endif
28 #include <pthread.h>
29 #include <errno.h>
30
31 #ifdef __Fuchsia__
32 #include <fidl/fuchsia.hardware.goldfish/cpp/wire.h>
33 #include <lib/zx/vmo.h>
34
35 #include "services/service_connector.h"
36
37 #define GET_STATUS_SAFE(result, member) \
38 ((result).ok() ? ((result).Unwrap()->member) : ZX_OK)
39
40 static QEMU_PIPE_HANDLE sProcDevice = 0;
41 #else // __Fuchsia__
42
43 #include "VirtioGpuPipeStream.h"
44 static VirtioGpuPipeStream* sVirtioGpuPipeStream = 0;
45 static int sStreamHandle = -1;
46
47 #endif // !__Fuchsia__
48
49 static QEMU_PIPE_HANDLE sProcPipe = 0;
50 static pthread_once_t sProcPipeOnce = PTHREAD_ONCE_INIT;
51 // sProcUID is a unique ID per process assigned by the host.
52 // It is different from getpid().
53 static uint64_t sProcUID = 0;
54 static volatile HostConnectionType sConnType = HOST_CONNECTION_VIRTIO_GPU_PIPE;
55
56 static uint32_t* sSeqnoPtr = 0;
57
58 // Meant to be called only once per process.
initSeqno()59 static void initSeqno() {
60 // So why do we reinitialize here? It's for testing purposes only;
61 // we have a unit test that exercise the case where this sequence
62 // number is reset as a result of guest process kill.
63 if (sSeqnoPtr) delete sSeqnoPtr;
64 sSeqnoPtr = new uint32_t;
65 *sSeqnoPtr = 0;
66 }
67
68 // processPipeInitOnce is used to generate a process unique ID (puid).
69 // processPipeInitOnce will only be called at most once per process.
70 // Use it with pthread_once for thread safety.
71 // The host associates resources with process unique ID (puid) for memory cleanup.
72 // It will fallback to the default path if the host does not support it.
73 // Processes are identified by acquiring a per-process 64bit unique ID from the
74 // host.
75 #ifdef __Fuchsia__
processPipeInitOnce()76 static void processPipeInitOnce() {
77 initSeqno();
78
79 fidl::ClientEnd<fuchsia_hardware_goldfish::PipeDevice> channel{
80 zx::channel(GetConnectToServiceFunction()(QEMU_PIPE_PATH))};
81 if (!channel) {
82 ALOGE("%s: failed to open " QEMU_PIPE_PATH,
83 __FUNCTION__);
84 return;
85 }
86
87 fidl::WireSyncClient<fuchsia_hardware_goldfish::PipeDevice> device(
88 std::move(channel));
89
90 auto pipe_ends =
91 fidl::CreateEndpoints<::fuchsia_hardware_goldfish::Pipe>();
92 if (!pipe_ends.is_ok()) {
93 ALOGE("%s: zx_channel_create failed: %d", __FUNCTION__, pipe_ends.status_value());
94 return;
95 }
96
97 fidl::WireSyncClient<fuchsia_hardware_goldfish::Pipe> pipe(
98 std::move(pipe_ends->client));
99 device->OpenPipe(std::move(pipe_ends->server));
100
101 zx::vmo vmo;
102 {
103 auto result = pipe->GetBuffer();
104 if (!result.ok() || result.Unwrap()->res != ZX_OK) {
105 ALOGE("%s: failed to get buffer: %d:%d", __FUNCTION__,
106 result.status(), GET_STATUS_SAFE(result, res));
107 return;
108 }
109 vmo = std::move(result.Unwrap()->vmo);
110 }
111
112 size_t len = strlen("pipe:GLProcessPipe");
113 zx_status_t status = vmo.write("pipe:GLProcessPipe", 0, len + 1);
114 if (status != ZX_OK) {
115 ALOGE("%s: failed write pipe name", __FUNCTION__);
116 return;
117 }
118
119 {
120 auto result = pipe->Write(len + 1, 0);
121 if (!result.ok() || result.Unwrap()->res != ZX_OK) {
122 ALOGD("%s: connecting to pipe service failed: %d:%d", __FUNCTION__,
123 result.status(), GET_STATUS_SAFE(result, res));
124 return;
125 }
126 }
127
128 // Send a confirmation int to the host and get per-process unique ID back
129 int32_t confirmInt = 100;
130 status = vmo.write(&confirmInt, 0, sizeof(confirmInt));
131 if (status != ZX_OK) {
132 ALOGE("%s: failed write confirm int", __FUNCTION__);
133 return;
134 }
135
136 {
137 auto result = pipe->DoCall(sizeof(confirmInt), 0, sizeof(sProcUID), 0);
138 if (!result.ok() || result.Unwrap()->res != ZX_OK) {
139 ALOGD("%s: failed to get per-process ID: %d:%d", __FUNCTION__,
140 result.status(), GET_STATUS_SAFE(result, res));
141 return;
142 }
143 }
144
145 status = vmo.read(&sProcUID, 0, sizeof(sProcUID));
146 if (status != ZX_OK) {
147 ALOGE("%s: failed read per-process ID: %d", __FUNCTION__, status);
148 return;
149 }
150 sProcDevice = device.TakeClientEnd().TakeChannel().release();
151 sProcPipe = pipe.TakeClientEnd().TakeChannel().release();
152 }
153 #else // __Fuchsia__
154
sQemuPipeInit()155 static void sQemuPipeInit() {
156 sProcPipe = qemu_pipe_open("GLProcessPipe");
157 if (!qemu_pipe_valid(sProcPipe)) {
158 sProcPipe = 0;
159 ALOGW("Process pipe failed");
160 return;
161 }
162 // Send a confirmation int to the host
163 int32_t confirmInt = 100;
164 if (qemu_pipe_write_fully(sProcPipe, &confirmInt, sizeof(confirmInt))) { // failed
165 qemu_pipe_close(sProcPipe);
166 sProcPipe = 0;
167 ALOGW("Process pipe failed");
168 return;
169 }
170
171 // Ask the host for per-process unique ID
172 if (qemu_pipe_read_fully(sProcPipe, &sProcUID, sizeof(sProcUID))) {
173 qemu_pipe_close(sProcPipe);
174 sProcPipe = 0;
175 sProcUID = 0;
176 ALOGW("Process pipe failed");
177 return;
178 }
179 }
180
processPipeInitOnce()181 static void processPipeInitOnce() {
182 initSeqno();
183
184 #if defined(HOST_BUILD) || !defined(GFXSTREAM)
185 sQemuPipeInit();
186 #else // HOST_BUILD
187 switch (sConnType) {
188 // TODO: Move those over too
189 case HOST_CONNECTION_QEMU_PIPE:
190 case HOST_CONNECTION_ADDRESS_SPACE:
191 case HOST_CONNECTION_TCP:
192 case HOST_CONNECTION_VIRTIO_GPU:
193 sQemuPipeInit();
194 break;
195 case HOST_CONNECTION_VIRTIO_GPU_PIPE:
196 case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
197 sVirtioGpuPipeStream = new VirtioGpuPipeStream(4096, sStreamHandle);
198 sProcUID = sVirtioGpuPipeStream->initProcessPipe();
199 break;
200 }
201 }
202 #endif // !HOST_BUILD
203 }
204 #endif // !__Fuchsia__
205
processPipeInit(int streamHandle,HostConnectionType connType,renderControl_encoder_context_t * rcEnc)206 bool processPipeInit(int streamHandle, HostConnectionType connType, renderControl_encoder_context_t *rcEnc) {
207 sConnType = connType;
208 #ifndef __Fuchsia__
209 sStreamHandle = streamHandle;
210 #endif // !__Fuchsia
211 pthread_once(&sProcPipeOnce, processPipeInitOnce);
212 bool pipeHandleInvalid = !sProcPipe;
213 #ifndef __Fuchsia__
214 pipeHandleInvalid = pipeHandleInvalid && !sVirtioGpuPipeStream;
215 #endif // !__Fuchsia__
216 if (pipeHandleInvalid) return false;
217 rcEnc->rcSetPuid(rcEnc, sProcUID);
218 return true;
219 }
220
getPuid()221 uint64_t getPuid() {
222 return sProcUID;
223 }
224
processPipeRestart()225 void processPipeRestart() {
226 ALOGW("%s: restarting process pipe\n", __func__);
227 bool isPipe = false;
228
229 switch (sConnType) {
230 // TODO: Move those over too
231 case HOST_CONNECTION_QEMU_PIPE:
232 case HOST_CONNECTION_ADDRESS_SPACE:
233 case HOST_CONNECTION_TCP:
234 case HOST_CONNECTION_VIRTIO_GPU:
235 isPipe = true;
236 break;
237 case HOST_CONNECTION_VIRTIO_GPU_PIPE:
238 case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
239 isPipe = false;
240 break;
241 }
242 }
243
244 sProcUID = 0;
245
246 #ifdef __Fuchsia__
247 zx_handle_close(sProcPipe);
248 sProcPipe = ZX_HANDLE_INVALID;
249 #else
250 if (isPipe) {
251 if (qemu_pipe_valid(sProcPipe)) {
252 qemu_pipe_close(sProcPipe);
253 sProcPipe = 0;
254 }
255 } else {
256 delete sVirtioGpuPipeStream;
257 sVirtioGpuPipeStream = nullptr;
258 }
259 #endif // __Fuchsia__
260
261 processPipeInitOnce();
262 }
263
refreshHostConnection()264 void refreshHostConnection() {
265 HostConnection* hostConn = HostConnection::get();
266 ExtendedRCEncoderContext* rcEnc = hostConn->rcEncoder();
267 rcEnc->rcSetPuid(rcEnc, sProcUID);
268 }
269
getSeqnoPtrForProcess()270 uint32_t* getSeqnoPtrForProcess() {
271 // It's assumed process pipe state has already been initialized.
272 return sSeqnoPtr;
273 }
274