Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 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 | |
Yilong Li | 4666908 | 2020-01-14 16:22:57 -0800 | [diff] [blame] | 17 | #include "ProcessPipe.h" |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <log/log.h> |
| 21 | #include <pthread.h> |
| 22 | #include <qemu_pipe_bp.h> |
| 23 | |
Yilong Li | 2b3e178 | 2020-10-22 12:21:45 -0700 | [diff] [blame] | 24 | #include "HostConnection.h" |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 25 | #include "renderControl_enc.h" |
Roman Kiryanov | 04cdd7b | 2020-04-28 13:22:29 -0700 | [diff] [blame] | 26 | |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 27 | #ifndef __Fuchsia__ |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 28 | |
| 29 | #include "VirtioGpuPipeStream.h" |
| 30 | static VirtioGpuPipeStream* sVirtioGpuPipeStream = 0; |
Gurchetan Singh | 6adffbd | 2022-03-16 19:22:19 -0700 | [diff] [blame] | 31 | static int sStreamHandle = -1; |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 32 | |
| 33 | #endif // !__Fuchsia__ |
David Reveman | 5e2e842 | 2019-05-02 15:48:15 -0400 | [diff] [blame] | 34 | |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 35 | static QEMU_PIPE_HANDLE sProcPipe = 0; |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 36 | // sProcUID is a unique ID per process assigned by the host. |
| 37 | // It is different from getpid(). |
| 38 | static uint64_t sProcUID = 0; |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 39 | static HostConnectionType sConnType = HOST_CONNECTION_VIRTIO_GPU_PIPE; |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 40 | |
Lingfeng Yang | 77fdf25 | 2021-01-22 17:48:00 -0800 | [diff] [blame] | 41 | static uint32_t* sSeqnoPtr = 0; |
| 42 | |
| 43 | // Meant to be called only once per process. |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 44 | static void initSeqno(void) { |
Lingfeng Yang | 77fdf25 | 2021-01-22 17:48:00 -0800 | [diff] [blame] | 45 | // So why do we reinitialize here? It's for testing purposes only; |
| 46 | // we have a unit test that exercise the case where this sequence |
| 47 | // number is reset as a result of guest process kill. |
| 48 | if (sSeqnoPtr) delete sSeqnoPtr; |
| 49 | sSeqnoPtr = new uint32_t; |
| 50 | *sSeqnoPtr = 0; |
| 51 | } |
| 52 | |
C Stout | 5a3a422 | 2023-11-14 16:31:56 -0800 | [diff] [blame] | 53 | namespace { |
| 54 | |
| 55 | static std::mutex sNeedInitMutex; |
| 56 | static bool sNeedInit = true; |
| 57 | |
| 58 | } // namespace |
| 59 | |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 60 | #ifndef __Fuchsia__ |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 61 | |
| 62 | static void sQemuPipeInit() { |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 63 | sProcPipe = qemu_pipe_open("GLProcessPipe"); |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 64 | if (!qemu_pipe_valid(sProcPipe)) { |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 65 | sProcPipe = 0; |
| 66 | ALOGW("Process pipe failed"); |
| 67 | return; |
| 68 | } |
| 69 | // Send a confirmation int to the host |
| 70 | int32_t confirmInt = 100; |
Roman Kiryanov | 6e05b7f | 2020-10-16 14:25:52 -0700 | [diff] [blame] | 71 | if (qemu_pipe_write_fully(sProcPipe, &confirmInt, sizeof(confirmInt))) { // failed |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 72 | qemu_pipe_close(sProcPipe); |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 73 | sProcPipe = 0; |
| 74 | ALOGW("Process pipe failed"); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Ask the host for per-process unique ID |
Roman Kiryanov | 6e05b7f | 2020-10-16 14:25:52 -0700 | [diff] [blame] | 79 | if (qemu_pipe_read_fully(sProcPipe, &sProcUID, sizeof(sProcUID))) { |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 80 | qemu_pipe_close(sProcPipe); |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 81 | sProcPipe = 0; |
| 82 | sProcUID = 0; |
| 83 | ALOGW("Process pipe failed"); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
C Stout | 5a3a422 | 2023-11-14 16:31:56 -0800 | [diff] [blame] | 88 | #endif // !__Fuchsia__ |
Jason Macnak | aca4cdb | 2023-06-14 12:57:10 -0700 | [diff] [blame] | 89 | |
Gurchetan Singh | 9928883 | 2023-09-07 09:40:10 -0700 | [diff] [blame] | 90 | static void processPipeDoInit(uint32_t noRenderControlEnc) { |
Lingfeng Yang | 77fdf25 | 2021-01-22 17:48:00 -0800 | [diff] [blame] | 91 | initSeqno(); |
| 92 | |
Gurchetan Singh | 9928883 | 2023-09-07 09:40:10 -0700 | [diff] [blame] | 93 | // No need to setup auxiliary pipe stream in this case |
| 94 | if (noRenderControlEnc) return; |
| 95 | |
C Stout | 5a3a422 | 2023-11-14 16:31:56 -0800 | [diff] [blame] | 96 | #if defined(__Fuchsia__) |
| 97 | // Note: sProcUID is not initialized. |
| 98 | ALOGE("Fuchsia: requires noRenderControlEnc"); |
| 99 | abort(); |
| 100 | #else |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 101 | switch (sConnType) { |
| 102 | // TODO: Move those over too |
| 103 | case HOST_CONNECTION_QEMU_PIPE: |
| 104 | case HOST_CONNECTION_ADDRESS_SPACE: |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 105 | sQemuPipeInit(); |
| 106 | break; |
Lingfeng Yang | 667e8ee | 2020-05-27 14:10:11 -0700 | [diff] [blame] | 107 | case HOST_CONNECTION_VIRTIO_GPU_PIPE: |
| 108 | case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: { |
Gurchetan Singh | 6adffbd | 2022-03-16 19:22:19 -0700 | [diff] [blame] | 109 | sVirtioGpuPipeStream = new VirtioGpuPipeStream(4096, sStreamHandle); |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 110 | sProcUID = sVirtioGpuPipeStream->initProcessPipe(); |
| 111 | break; |
| 112 | } |
| 113 | } |
C Stout | 5a3a422 | 2023-11-14 16:31:56 -0800 | [diff] [blame] | 114 | #endif |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 115 | } |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 116 | |
Gurchetan Singh | 9928883 | 2023-09-07 09:40:10 -0700 | [diff] [blame] | 117 | bool processPipeInit(int streamHandle, HostConnectionType connType, uint32_t noRenderControlEnc) { |
Lingfeng Yang | 9c26ebf | 2019-12-30 09:14:49 -0800 | [diff] [blame] | 118 | sConnType = connType; |
Yilong Li | 7907feb | 2022-04-01 15:54:46 -0700 | [diff] [blame] | 119 | #ifndef __Fuchsia__ |
Gurchetan Singh | 6adffbd | 2022-03-16 19:22:19 -0700 | [diff] [blame] | 120 | sStreamHandle = streamHandle; |
Yilong Li | 7907feb | 2022-04-01 15:54:46 -0700 | [diff] [blame] | 121 | #endif // !__Fuchsia |
Jason Macnak | aca4cdb | 2023-06-14 12:57:10 -0700 | [diff] [blame] | 122 | |
| 123 | { |
| 124 | std::lock_guard<std::mutex> lock(sNeedInitMutex); |
| 125 | |
| 126 | if (sNeedInit) { |
| 127 | sNeedInit = false; |
Gurchetan Singh | 9928883 | 2023-09-07 09:40:10 -0700 | [diff] [blame] | 128 | processPipeDoInit(noRenderControlEnc); |
| 129 | |
| 130 | if (noRenderControlEnc) { |
| 131 | return true; |
| 132 | } |
Jason Macnak | aca4cdb | 2023-06-14 12:57:10 -0700 | [diff] [blame] | 133 | |
Gurchetan Singh | 8ed8556 | 2023-09-06 16:56:28 -0700 | [diff] [blame] | 134 | #ifndef __Fuchsia__ |
Jason Macnak | aca4cdb | 2023-06-14 12:57:10 -0700 | [diff] [blame] | 135 | if (!sProcPipe && !sVirtioGpuPipeStream) { |
| 136 | return false; |
| 137 | } |
| 138 | #endif |
| 139 | } |
| 140 | } |
| 141 | |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 142 | return true; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 143 | } |
Lingfeng Yang | c49d89d | 2020-08-20 12:33:04 -0700 | [diff] [blame] | 144 | |
| 145 | uint64_t getPuid() { |
| 146 | return sProcUID; |
| 147 | } |
Lingfeng Yang | 25bca5a | 2020-10-21 09:52:10 -0700 | [diff] [blame] | 148 | |
| 149 | void processPipeRestart() { |
Jason Macnak | aca4cdb | 2023-06-14 12:57:10 -0700 | [diff] [blame] | 150 | std::lock_guard<std::mutex> lock(sNeedInitMutex); |
| 151 | |
Lingfeng Yang | 25bca5a | 2020-10-21 09:52:10 -0700 | [diff] [blame] | 152 | ALOGW("%s: restarting process pipe\n", __func__); |
| 153 | bool isPipe = false; |
| 154 | |
| 155 | switch (sConnType) { |
| 156 | // TODO: Move those over too |
| 157 | case HOST_CONNECTION_QEMU_PIPE: |
| 158 | case HOST_CONNECTION_ADDRESS_SPACE: |
Lingfeng Yang | 25bca5a | 2020-10-21 09:52:10 -0700 | [diff] [blame] | 159 | isPipe = true; |
| 160 | break; |
| 161 | case HOST_CONNECTION_VIRTIO_GPU_PIPE: |
| 162 | case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: { |
| 163 | isPipe = false; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | sProcUID = 0; |
| 169 | |
| 170 | if (isPipe) { |
| 171 | if (qemu_pipe_valid(sProcPipe)) { |
| 172 | qemu_pipe_close(sProcPipe); |
| 173 | sProcPipe = 0; |
| 174 | } |
| 175 | } else { |
C Stout | 5a3a422 | 2023-11-14 16:31:56 -0800 | [diff] [blame] | 176 | #ifndef __Fuchsia__ |
Jason Macnak | 843b84b | 2023-05-19 14:19:25 -0700 | [diff] [blame] | 177 | if (sVirtioGpuPipeStream) { |
| 178 | delete sVirtioGpuPipeStream; |
| 179 | sVirtioGpuPipeStream = nullptr; |
| 180 | } |
C Stout | 5a3a422 | 2023-11-14 16:31:56 -0800 | [diff] [blame] | 181 | #endif |
Lingfeng Yang | 25bca5a | 2020-10-21 09:52:10 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jason Macnak | 459dd87 | 2023-09-13 08:27:41 -0700 | [diff] [blame] | 184 | if (sConnType == HOST_CONNECTION_VIRTIO_GPU_PIPE || |
| 185 | sConnType == HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE) { |
| 186 | VirtGpuDevice::resetInstance(); |
| 187 | } |
Lingfeng Yang | 25bca5a | 2020-10-21 09:52:10 -0700 | [diff] [blame] | 188 | |
Jason Macnak | 459dd87 | 2023-09-13 08:27:41 -0700 | [diff] [blame] | 189 | sNeedInit = true; |
Jason Macnak | 843b84b | 2023-05-19 14:19:25 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Lingfeng Yang | 25bca5a | 2020-10-21 09:52:10 -0700 | [diff] [blame] | 192 | void refreshHostConnection() { |
| 193 | HostConnection* hostConn = HostConnection::get(); |
| 194 | ExtendedRCEncoderContext* rcEnc = hostConn->rcEncoder(); |
| 195 | rcEnc->rcSetPuid(rcEnc, sProcUID); |
| 196 | } |
Lingfeng Yang | 77fdf25 | 2021-01-22 17:48:00 -0800 | [diff] [blame] | 197 | |
| 198 | uint32_t* getSeqnoPtrForProcess() { |
| 199 | // It's assumed process pipe state has already been initialized. |
| 200 | return sSeqnoPtr; |
| 201 | } |