• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)->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::Controller> controller_channel{
80         zx::channel(GetConnectToServiceFunction()(QEMU_PIPE_PATH))};
81     if (!controller_channel) {
82         ALOGE("%s: failed to open " QEMU_PIPE_PATH,
83               __FUNCTION__);
84         return;
85     }
86     fidl::WireSyncClient controller(std::move(controller_channel));
87     zx::result pipe_device_ends =
88         fidl::CreateEndpoints<fuchsia_hardware_goldfish::PipeDevice>();
89     if (pipe_device_ends.is_error()) {
90         ALOGE("%s: zx_channel_create failed: %s", __FUNCTION__, pipe_device_ends.status_string());
91         return;
92     }
93 
94     if (fidl::Status result = controller->OpenSession(std::move(pipe_device_ends->server));
95         !result.ok()) {
96         ALOGE("%s: failed to open session: %s", __FUNCTION__, result.status_string());
97         return;
98     }
99 
100     fidl::WireSyncClient device(std::move(pipe_device_ends->client));
101 
102     auto pipe_ends =
103         fidl::CreateEndpoints<::fuchsia_hardware_goldfish::Pipe>();
104     if (!pipe_ends.is_ok()) {
105         ALOGE("%s: zx_channel_create failed: %d", __FUNCTION__, pipe_ends.status_value());
106         return;
107     }
108 
109     fidl::WireSyncClient pipe(std::move(pipe_ends->client));
110     device->OpenPipe(std::move(pipe_ends->server));
111 
112     zx::vmo vmo;
113     {
114         auto result = pipe->GetBuffer();
115         if (!result.ok() || result->res != ZX_OK) {
116             ALOGE("%s: failed to get buffer: %d:%d", __FUNCTION__,
117                   result.status(), GET_STATUS_SAFE(result, res));
118             return;
119         }
120         vmo = std::move(result->vmo);
121     }
122 
123     size_t len = strlen("pipe:GLProcessPipe");
124     zx_status_t status = vmo.write("pipe:GLProcessPipe", 0, len + 1);
125     if (status != ZX_OK) {
126         ALOGE("%s: failed write pipe name", __FUNCTION__);
127         return;
128     }
129 
130     {
131         auto result = pipe->Write(len + 1, 0);
132         if (!result.ok() || result->res != ZX_OK) {
133             ALOGD("%s: connecting to pipe service failed: %d:%d", __FUNCTION__,
134                   result.status(), GET_STATUS_SAFE(result, res));
135             return;
136         }
137     }
138 
139     // Send a confirmation int to the host and get per-process unique ID back
140     int32_t confirmInt = 100;
141     status = vmo.write(&confirmInt, 0, sizeof(confirmInt));
142     if (status != ZX_OK) {
143         ALOGE("%s: failed write confirm int", __FUNCTION__);
144         return;
145     }
146 
147     {
148         auto result = pipe->DoCall(sizeof(confirmInt), 0, sizeof(sProcUID), 0);
149         if (!result.ok() || result->res != ZX_OK) {
150             ALOGD("%s: failed to get per-process ID: %d:%d", __FUNCTION__,
151                   result.status(), GET_STATUS_SAFE(result, res));
152             return;
153         }
154     }
155 
156     status = vmo.read(&sProcUID, 0, sizeof(sProcUID));
157     if (status != ZX_OK) {
158         ALOGE("%s: failed read per-process ID: %d", __FUNCTION__, status);
159         return;
160     }
161     sProcDevice = device.TakeClientEnd().TakeChannel().release();
162     sProcPipe = pipe.TakeClientEnd().TakeChannel().release();
163 }
164 #else // __Fuchsia__
165 
sQemuPipeInit()166 static void sQemuPipeInit() {
167     sProcPipe = qemu_pipe_open("GLProcessPipe");
168     if (!qemu_pipe_valid(sProcPipe)) {
169         sProcPipe = 0;
170         ALOGW("Process pipe failed");
171         return;
172     }
173     // Send a confirmation int to the host
174     int32_t confirmInt = 100;
175     if (qemu_pipe_write_fully(sProcPipe, &confirmInt, sizeof(confirmInt))) { // failed
176         qemu_pipe_close(sProcPipe);
177         sProcPipe = 0;
178         ALOGW("Process pipe failed");
179         return;
180     }
181 
182     // Ask the host for per-process unique ID
183     if (qemu_pipe_read_fully(sProcPipe, &sProcUID, sizeof(sProcUID))) {
184         qemu_pipe_close(sProcPipe);
185         sProcPipe = 0;
186         sProcUID = 0;
187         ALOGW("Process pipe failed");
188         return;
189     }
190 }
191 
processPipeInitOnce()192 static void processPipeInitOnce() {
193     initSeqno();
194 
195 #if defined(HOST_BUILD) || !defined(GFXSTREAM)
196     sQemuPipeInit();
197 #else // HOST_BUILD
198     switch (sConnType) {
199         // TODO: Move those over too
200         case HOST_CONNECTION_QEMU_PIPE:
201         case HOST_CONNECTION_ADDRESS_SPACE:
202         case HOST_CONNECTION_TCP:
203             sQemuPipeInit();
204             break;
205         case HOST_CONNECTION_VIRTIO_GPU_PIPE:
206         case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
207             sVirtioGpuPipeStream = new VirtioGpuPipeStream(4096, sStreamHandle);
208             sProcUID = sVirtioGpuPipeStream->initProcessPipe();
209             break;
210         }
211     }
212 #endif // !HOST_BUILD
213 }
214 #endif // !__Fuchsia__
215 
processPipeInit(int streamHandle,HostConnectionType connType,renderControl_encoder_context_t * rcEnc)216 bool processPipeInit(int streamHandle, HostConnectionType connType, renderControl_encoder_context_t *rcEnc) {
217     sConnType = connType;
218 #ifndef __Fuchsia__
219     sStreamHandle = streamHandle;
220 #endif // !__Fuchsia
221     pthread_once(&sProcPipeOnce, processPipeInitOnce);
222     bool pipeHandleInvalid = !sProcPipe;
223 #ifndef __Fuchsia__
224     pipeHandleInvalid = pipeHandleInvalid && !sVirtioGpuPipeStream;
225 #endif // !__Fuchsia__
226     if (pipeHandleInvalid) return false;
227     rcEnc->rcSetPuid(rcEnc, sProcUID);
228     return true;
229 }
230 
getPuid()231 uint64_t getPuid() {
232     return sProcUID;
233 }
234 
processPipeRestart()235 void processPipeRestart() {
236     ALOGW("%s: restarting process pipe\n", __func__);
237     bool isPipe = false;
238 
239     switch (sConnType) {
240         // TODO: Move those over too
241         case HOST_CONNECTION_QEMU_PIPE:
242         case HOST_CONNECTION_ADDRESS_SPACE:
243         case HOST_CONNECTION_TCP:
244             isPipe = true;
245             break;
246         case HOST_CONNECTION_VIRTIO_GPU_PIPE:
247         case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
248             isPipe = false;
249             break;
250         }
251     }
252 
253     sProcUID = 0;
254 
255 #ifdef __Fuchsia__
256     zx_handle_close(sProcPipe);
257     sProcPipe = ZX_HANDLE_INVALID;
258 #else
259     if (isPipe) {
260         if (qemu_pipe_valid(sProcPipe)) {
261             qemu_pipe_close(sProcPipe);
262             sProcPipe = 0;
263         }
264     } else {
265         delete sVirtioGpuPipeStream;
266         sVirtioGpuPipeStream = nullptr;
267     }
268 #endif // __Fuchsia__
269 
270     processPipeInitOnce();
271 }
272 
refreshHostConnection()273 void refreshHostConnection() {
274     HostConnection* hostConn = HostConnection::get();
275     ExtendedRCEncoderContext* rcEnc = hostConn->rcEncoder();
276     rcEnc->rcSetPuid(rcEnc, sProcUID);
277 }
278 
getSeqnoPtrForProcess()279 uint32_t* getSeqnoPtrForProcess() {
280     // It's assumed process pipe state has already been initialized.
281     return sSeqnoPtr;
282 }
283