VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/display-drm.cpp@ 86318

Last change on this file since 86318 was 86318, checked in by vboxsync, 4 years ago

GA:Linux: bugref:9671 Non-security issue with our Linux GA binaries

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: display-drm.cpp 86318 2020-09-28 12:33:34Z vboxsync $ */
2/** @file
3 * X11 guest client - VMSVGA emulation resize event pass-through to drm guest
4 * driver.
5 */
6
7/*
8 * Copyright (C) 2016-2020 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/*
20 * Known things to test when changing this code. All assume a guest with VMSVGA
21 * active and controlled by X11 or Wayland, and Guest Additions installed and
22 * running, unless otherwise stated.
23 * - On Linux 4.6 and later guests, VBoxClient --vmsvga should be running as
24 * root and not as the logged-in user. Dynamic resizing should work for all
25 * screens in any environment which handles kernel resize notifications,
26 * including at log-in screens. Test GNOME Shell Wayland and GNOME Shell
27 * under X.Org or Unity or KDE at the log-in screen and after log-in.
28 * - Linux 4.10 changed the user-kernel-ABI introduced in 4.6: test both.
29 * - On other guests (than Linux 4.6 or later) running X.Org Server 1.3 or
30 * later, VBoxClient --vmsvga should never be running as root, and should run
31 * (and dynamic resizing and screen enable/disable should work for all
32 * screens) whenever a user is logged in to a supported desktop environment.
33 * - On guests running X.Org Server 1.2 or older, VBoxClient --vmsvga should
34 * never run as root and should run whenever a user is logged in to a
35 * supported desktop environment. Dynamic resizing should work for the first
36 * screen, and enabling others should not be possible.
37 * - When VMSVGA is not enabled, VBoxClient --vmsvga should never stay running.
38 */
39
40#include "VBoxClient.h"
41
42#include <VBox/VBoxGuestLib.h>
43
44#include <iprt/assert.h>
45#include <iprt/file.h>
46#include <iprt/err.h>
47#include <iprt/string.h>
48#include <iprt/initterm.h>
49#include <iprt/message.h>
50#include <unistd.h>
51#include <stdio.h>
52
53
54/** Maximum number of supported screens. DRM and X11 both limit this to 32. */
55/** @todo if this ever changes, dynamically allocate resizeable arrays in the
56 * context structure. */
57#define VMW_MAX_HEADS 32
58
59/* VMWare kernel driver control parts definitions. */
60
61#ifdef RT_OS_LINUX
62# include <sys/ioctl.h>
63#else /* Solaris and BSDs, in case they ever adopt the DRM driver. */
64# include <sys/ioccom.h>
65#endif
66
67#define DRM_DRIVER_NAME "vmwgfx"
68
69/** Counter of how often our daemon has been respawned. */
70unsigned g_cRespawn = 0;
71/** Logging verbosity level. */
72unsigned g_cVerbosity = 0;
73
74/** DRM version structure. */
75struct DRMVERSION
76{
77 int cMajor;
78 int cMinor;
79 int cPatchLevel;
80 size_t cbName;
81 char *pszName;
82 size_t cbDate;
83 char *pszDate;
84 size_t cbDescription;
85 char *pszDescription;
86};
87AssertCompileSize(struct DRMVERSION, 8 + 7 * sizeof(void *));
88
89/** Rectangle structure for geometry of a single screen. */
90struct DRMVMWRECT
91{
92 int32_t x;
93 int32_t y;
94 uint32_t w;
95 uint32_t h;
96};
97AssertCompileSize(struct DRMVMWRECT, 16);
98
99#define DRM_IOCTL_VERSION _IOWR('d', 0x00, struct DRMVERSION)
100
101struct DRMCONTEXT
102{
103 RTFILE hDevice;
104};
105
106static void drmConnect(struct DRMCONTEXT *pContext)
107{
108 unsigned i;
109 RTFILE hDevice;
110
111 if (pContext->hDevice != NIL_RTFILE)
112 VBClLogFatalError("%s called with bad argument\n", __func__);
113 /* Try to open the SVGA DRM device. */
114 for (i = 0; i < 128; ++i)
115 {
116 char szPath[64];
117 struct DRMVERSION version;
118 char szName[sizeof(DRM_DRIVER_NAME)];
119 int rc;
120
121 /* Control devices for drm graphics driver control devices go from
122 * controlD64 to controlD127. Render node devices go from renderD128
123 * to renderD192. The driver takes resize hints via the control device
124 * on pre-4.10 kernels and on the render device on newer ones. Try
125 * both types. */
126 if (i % 2 == 0)
127 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/renderD%u", i / 2 + 128);
128 else
129 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/controlD%u", i / 2 + 64);
130 if (RT_FAILURE(rc))
131 VBClLogFatalError("RTStrPrintf of device path failed, rc=%Rrc\n", rc);
132 rc = RTFileOpen(&hDevice, szPath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
133 if (RT_FAILURE(rc))
134 continue;
135 RT_ZERO(version);
136 version.cbName = sizeof(szName);
137 version.pszName = szName;
138 rc = RTFileIoCtl(hDevice, DRM_IOCTL_VERSION, &version, sizeof(version), NULL);
139 if ( RT_SUCCESS(rc)
140 && !strncmp(szName, DRM_DRIVER_NAME, sizeof(DRM_DRIVER_NAME) - 1)
141 && ( version.cMajor > 2
142 || (version.cMajor == 2 && version.cMinor > 9)))
143 break;
144 hDevice = NIL_RTFILE;
145 }
146 pContext->hDevice = hDevice;
147}
148
149/** Preferred screen layout information for DRM_VMW_UPDATE_LAYOUT IoCtl. The
150 * rects argument is a cast pointer to an array of drm_vmw_rect. */
151struct DRMVMWUPDATELAYOUT {
152 uint32_t cOutputs;
153 uint32_t u32Pad;
154 uint64_t ptrRects;
155};
156AssertCompileSize(struct DRMVMWUPDATELAYOUT, 16);
157
158#define DRM_IOCTL_VMW_UPDATE_LAYOUT \
159 _IOW('d', 0x40 + 20, struct DRMVMWUPDATELAYOUT)
160
161static void drmSendHints(struct DRMCONTEXT *pContext, struct DRMVMWRECT *paRects,
162 unsigned cHeads)
163{
164 uid_t curuid = getuid();
165 if (setreuid(0, 0) != 0)
166 perror("setreuid failed during drm ioctl.");
167 int rc;
168 struct DRMVMWUPDATELAYOUT ioctlLayout;
169
170 if (pContext->hDevice == NIL_RTFILE)
171 VBClLogFatalError("%s bad device argument\n", __func__);
172 ioctlLayout.cOutputs = cHeads;
173 ioctlLayout.ptrRects = (uint64_t)paRects;
174 rc = RTFileIoCtl(pContext->hDevice, DRM_IOCTL_VMW_UPDATE_LAYOUT,
175 &ioctlLayout, sizeof(ioctlLayout), NULL);
176 if (RT_FAILURE(rc) && rc != VERR_INVALID_PARAMETER)
177 VBClLogFatalError("Failure updating layout, rc=%Rrc\n", rc);
178 if (setreuid(curuid, 0) != 0)
179 perror("reset of setreuid failed after drm ioctl.");
180}
181
182int main(int argc, char *argv[])
183{
184 int rc = RTR3InitExe(argc, &argv, 0);
185 if (RT_FAILURE(rc))
186 return RTMsgInitFailure(rc);
187 rc = VbglR3InitUser();
188 if (RT_FAILURE(rc))
189 VBClLogFatalError("VbglR3InitUser failed: %Rrc", rc);
190
191 struct DRMCONTEXT drmContext = { NIL_RTFILE };
192 static struct VMMDevDisplayDef aMonitors[VMW_MAX_HEADS];
193
194 unsigned cEnabledMonitors;
195 /* Do not acknowledge the first event we query for to pick up old events,
196 * e.g. from before a guest reboot. */
197 bool fAck = false;
198 drmConnect(&drmContext);
199 if (drmContext.hDevice == NIL_RTFILE)
200 return VERR_OPEN_FAILED;
201 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
202 if (RT_FAILURE(rc))
203 {
204 VBClLogFatalError("Failed to request display change events, rc=%Rrc\n", rc);
205 return VERR_INVALID_HANDLE;
206 }
207 rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
208 if (rc == VERR_RESOURCE_BUSY) /* Someone else has already acquired it. */
209 {
210 return VERR_RESOURCE_BUSY;
211 }
212 if (RT_FAILURE(rc))
213 {
214 VBClLogFatalError("Failed to register resizing support, rc=%Rrc\n", rc);
215 return VERR_INVALID_HANDLE;
216 }
217
218 for (;;)
219 {
220 uint32_t events;
221 struct VMMDevDisplayDef aDisplays[VMW_MAX_HEADS];
222 uint32_t cDisplaysOut;
223 /* Query the first size without waiting. This lets us e.g. pick up
224 * the last event before a guest reboot when we start again after. */
225 rc = VbglR3GetDisplayChangeRequestMulti(VMW_MAX_HEADS, &cDisplaysOut, aDisplays, fAck);
226 fAck = true;
227 if (RT_FAILURE(rc))
228 VBClLogFatalError("Failed to get display change request, rc=%Rrc\n", rc);
229 if (cDisplaysOut > VMW_MAX_HEADS)
230 VBClLogFatalError("Display change request contained, rc=%Rrc\n", rc);
231 if (cDisplaysOut > 0)
232 {
233 for (unsigned i = 0; i < cDisplaysOut && i < VMW_MAX_HEADS; ++i)
234 {
235 uint32_t idDisplay = aDisplays[i].idDisplay;
236 if (idDisplay >= VMW_MAX_HEADS)
237 continue;
238 aMonitors[idDisplay].fDisplayFlags = aDisplays[i].fDisplayFlags;
239 if (!(aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
240 {
241 if ((idDisplay == 0) || (aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_ORIGIN))
242 {
243 aMonitors[idDisplay].xOrigin = aDisplays[i].xOrigin;
244 aMonitors[idDisplay].yOrigin = aDisplays[i].yOrigin;
245 } else {
246 aMonitors[idDisplay].xOrigin = aMonitors[idDisplay - 1].xOrigin + aMonitors[idDisplay - 1].cx;
247 aMonitors[idDisplay].yOrigin = aMonitors[idDisplay - 1].yOrigin;
248 }
249 aMonitors[idDisplay].cx = aDisplays[i].cx;
250 aMonitors[idDisplay].cy = aDisplays[i].cy;
251 }
252 }
253 /* Create an dense (consisting of enabled monitors only) array to pass to DRM. */
254 cEnabledMonitors = 0;
255 struct DRMVMWRECT aEnabledMonitors[VMW_MAX_HEADS];
256 for (int j = 0; j < VMW_MAX_HEADS; ++j)
257 {
258 if (!(aMonitors[j].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
259 {
260 aEnabledMonitors[cEnabledMonitors].x = aMonitors[j].xOrigin;
261 aEnabledMonitors[cEnabledMonitors].y = aMonitors[j].yOrigin;
262 aEnabledMonitors[cEnabledMonitors].w = aMonitors[j].cx;
263 aEnabledMonitors[cEnabledMonitors].h = aMonitors[j].cy;
264 if (cEnabledMonitors > 0)
265 aEnabledMonitors[cEnabledMonitors].x = aEnabledMonitors[cEnabledMonitors - 1].x + aEnabledMonitors[cEnabledMonitors - 1].w;
266 ++cEnabledMonitors;
267 }
268 }
269 for (unsigned i = 0; i < cEnabledMonitors; ++i)
270 printf("Monitor %u: %dx%d, (%d, %d)\n", i, (int)aEnabledMonitors[i].w, (int)aEnabledMonitors[i].h,
271 (int)aEnabledMonitors[i].x, (int)aEnabledMonitors[i].y);
272 drmSendHints(&drmContext, aEnabledMonitors, cEnabledMonitors);
273 }
274 do
275 {
276 rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, RT_INDEFINITE_WAIT, &events);
277 } while (rc == VERR_INTERRUPTED);
278 if (RT_FAILURE(rc))
279 VBClLogFatalError("Failure waiting for event, rc=%Rrc\n", rc);
280 }
281 return 0;
282}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette