1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdlib.h>
6
7 #if defined(OS_WIN)
8 #include <dwmapi.h>
9 #include <windows.h>
10 #endif
11
12 #include "base/debug/trace_event.h"
13 #include "base/lazy_instance.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/threading/platform_thread.h"
20 #include "build/build_config.h"
21 #include "content/child/child_process.h"
22 #include "content/common/content_constants_internal.h"
23 #include "content/common/gpu/gpu_config.h"
24 #include "content/common/gpu/gpu_messages.h"
25 #include "content/common/sandbox_linux/sandbox_linux.h"
26 #include "content/gpu/gpu_child_thread.h"
27 #include "content/gpu/gpu_process.h"
28 #include "content/gpu/gpu_watchdog_thread.h"
29 #include "content/public/common/content_client.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/common/main_function_params.h"
32 #include "gpu/command_buffer/service/gpu_switches.h"
33 #include "gpu/config/gpu_info_collector.h"
34 #include "gpu/config/gpu_util.h"
35 #include "ui/gl/gl_implementation.h"
36 #include "ui/gl/gl_surface.h"
37 #include "ui/gl/gl_switches.h"
38 #include "ui/gl/gpu_switching_manager.h"
39
40 #if defined(OS_WIN)
41 #include "base/win/windows_version.h"
42 #include "base/win/scoped_com_initializer.h"
43 #include "sandbox/win/src/sandbox.h"
44 #endif
45
46 #if defined(USE_X11)
47 #include "ui/base/x/x11_util.h"
48 #endif
49
50 #if defined(OS_LINUX)
51 #include "content/public/common/sandbox_init.h"
52 #endif
53
54 const int kGpuTimeout = 10000;
55
56 namespace content {
57
58 namespace {
59
60 bool WarmUpSandbox(const CommandLine& command_line);
61 #if defined(OS_LINUX)
62 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
63 #elif defined(OS_WIN)
64 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
65 #endif
66
67 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
68 LAZY_INSTANCE_INITIALIZER;
69
GpuProcessLogMessageHandler(int severity,const char * file,int line,size_t message_start,const std::string & str)70 bool GpuProcessLogMessageHandler(int severity,
71 const char* file, int line,
72 size_t message_start,
73 const std::string& str) {
74 std::string header = str.substr(0, message_start);
75 std::string message = str.substr(message_start);
76 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
77 severity, header, message));
78 return false;
79 }
80
81 } // namespace anonymous
82
83 // Main function for starting the Gpu process.
GpuMain(const MainFunctionParams & parameters)84 int GpuMain(const MainFunctionParams& parameters) {
85 TRACE_EVENT0("gpu", "GpuMain");
86 base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
87 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
88 kTraceEventGpuProcessSortIndex);
89
90 const CommandLine& command_line = parameters.command_line;
91 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
92 ChildProcess::WaitForDebugger("Gpu");
93 }
94
95 base::Time start_time = base::Time::Now();
96
97 #if defined(OS_WIN)
98 // Prevent Windows from displaying a modal dialog on failures like not being
99 // able to load a DLL.
100 SetErrorMode(
101 SEM_FAILCRITICALERRORS |
102 SEM_NOGPFAULTERRORBOX |
103 SEM_NOOPENFILEERRORBOX);
104 #elif defined(USE_X11)
105 ui::SetDefaultX11ErrorHandlers();
106 #endif
107
108 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
109
110 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
111 std::string types = command_line.GetSwitchValueASCII(
112 switches::kGpuDriverBugWorkarounds);
113 std::set<int> workarounds;
114 gpu::StringToFeatureSet(types, &workarounds);
115 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
116 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
117 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
118 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
119 }
120
121 // Initialization of the OpenGL bindings may fail, in which case we
122 // will need to tear down this process. However, we can not do so
123 // safely until the IPC channel is set up, because the detection of
124 // early return of a child process is implemented using an IPC
125 // channel error. If the IPC channel is not fully set up between the
126 // browser and GPU process, and the GPU process crashes or exits
127 // early, the browser process will never detect it. For this reason
128 // we defer tearing down the GPU process until receiving the
129 // GpuMsg_Initialize message from the browser.
130 bool dead_on_arrival = false;
131
132 base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
133 #if defined(OS_WIN)
134 // Unless we're running on desktop GL, we don't need a UI message
135 // loop, so avoid its use to work around apparent problems with some
136 // third-party software.
137 if (command_line.HasSwitch(switches::kUseGL) &&
138 command_line.GetSwitchValueASCII(switches::kUseGL) ==
139 gfx::kGLImplementationDesktopName) {
140 message_loop_type = base::MessageLoop::TYPE_UI;
141 }
142 #elif defined(TOOLKIT_GTK)
143 message_loop_type = base::MessageLoop::TYPE_GPU;
144 #elif defined(OS_LINUX)
145 message_loop_type = base::MessageLoop::TYPE_DEFAULT;
146 #endif
147
148 base::MessageLoop main_message_loop(message_loop_type);
149 base::PlatformThread::SetName("CrGpuMain");
150
151 // In addition to disabling the watchdog if the command line switch is
152 // present, disable the watchdog on valgrind because the code is expected
153 // to run slowly in that case.
154 bool enable_watchdog =
155 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
156 !RunningOnValgrind();
157
158 // Disable the watchdog in debug builds because they tend to only be run by
159 // developers who will not appreciate the watchdog killing the GPU process.
160 #ifndef NDEBUG
161 enable_watchdog = false;
162 #endif
163
164 bool delayed_watchdog_enable = false;
165
166 #if defined(OS_CHROMEOS)
167 // Don't start watchdog immediately, to allow developers to switch to VT2 on
168 // startup.
169 delayed_watchdog_enable = true;
170 #endif
171
172 scoped_refptr<GpuWatchdogThread> watchdog_thread;
173
174 // Start the GPU watchdog only after anything that is expected to be time
175 // consuming has completed, otherwise the process is liable to be aborted.
176 if (enable_watchdog && !delayed_watchdog_enable) {
177 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
178 watchdog_thread->Start();
179 }
180
181 gpu::GPUInfo gpu_info;
182 // Get vendor_id, device_id, driver_version from browser process through
183 // commandline switches.
184 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
185 command_line.HasSwitch(switches::kGpuDeviceID) &&
186 command_line.HasSwitch(switches::kGpuDriverVersion));
187 bool success = base::HexStringToUInt(
188 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
189 &gpu_info.gpu.vendor_id);
190 DCHECK(success);
191 success = base::HexStringToUInt(
192 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
193 &gpu_info.gpu.device_id);
194 DCHECK(success);
195 gpu_info.driver_vendor =
196 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
197 gpu_info.driver_version =
198 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
199 GetContentClient()->SetGpuInfo(gpu_info);
200
201 base::TimeDelta collect_context_time;
202 base::TimeDelta initialize_one_off_time;
203
204 // Warm up resources that don't need access to GPUInfo.
205 if (WarmUpSandbox(command_line)) {
206 #if defined(OS_LINUX)
207 bool initialized_sandbox = false;
208 bool initialized_gl_context = false;
209 bool should_initialize_gl_context = false;
210 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
211 // On Chrome OS ARM, GPU driver userspace creates threads when initializing
212 // a GL context, so start the sandbox early.
213 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
214 should_initialize_gl_context);
215 initialized_sandbox = true;
216 #endif
217 #endif // defined(OS_LINUX)
218
219 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
220
221 // Load and initialize the GL implementation and locate the GL entry points.
222 if (gfx::GLSurface::InitializeOneOff()) {
223 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
224 // purposes. However, on Mac we don't actually use them. As documented in
225 // crbug.com/222934, due to some driver issues, glGetString could take
226 // multiple seconds to finish, which in turn cause the GPU process to
227 // crash.
228 // By skipping the following code on Mac, we don't really lose anything,
229 // because the basic GPU information is passed down from browser process
230 // and we already registered them through SetGpuInfo() above.
231 base::TimeTicks before_collect_context_graphics_info =
232 base::TimeTicks::Now();
233 #if !defined(OS_MACOSX)
234 if (!gpu::CollectContextGraphicsInfo(&gpu_info))
235 VLOG(1) << "gpu::CollectGraphicsInfo failed";
236 GetContentClient()->SetGpuInfo(gpu_info);
237
238 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
239 // Recompute gpu driver bug workarounds - this is specifically useful
240 // on systems where vendor_id/device_id aren't available.
241 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
242 gpu::ApplyGpuDriverBugWorkarounds(
243 gpu_info, const_cast<CommandLine*>(&command_line));
244 }
245 #endif
246
247 #if defined(OS_LINUX)
248 initialized_gl_context = true;
249 #if !defined(OS_CHROMEOS)
250 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
251 gpu_info.driver_vendor == "NVIDIA") {
252 base::ThreadRestrictions::AssertIOAllowed();
253 if (access("/dev/nvidiactl", R_OK) != 0) {
254 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
255 dead_on_arrival = true;
256 }
257 }
258 #endif // !defined(OS_CHROMEOS)
259 #endif // defined(OS_LINUX)
260 #endif // !defined(OS_MACOSX)
261 collect_context_time =
262 base::TimeTicks::Now() - before_collect_context_graphics_info;
263 } else {
264 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
265 dead_on_arrival = true;
266 }
267
268 initialize_one_off_time =
269 base::TimeTicks::Now() - before_initialize_one_off;
270
271 if (enable_watchdog && delayed_watchdog_enable) {
272 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
273 watchdog_thread->Start();
274 }
275
276 // OSMesa is expected to run very slowly, so disable the watchdog in that
277 // case.
278 if (enable_watchdog &&
279 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
280 watchdog_thread->Stop();
281 watchdog_thread = NULL;
282 }
283
284 #if defined(OS_LINUX)
285 should_initialize_gl_context = !initialized_gl_context &&
286 !dead_on_arrival;
287
288 if (!initialized_sandbox) {
289 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
290 should_initialize_gl_context);
291 }
292 #elif defined(OS_WIN)
293 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
294 #endif
295 } else {
296 dead_on_arrival = true;
297 }
298
299 logging::SetLogMessageHandler(NULL);
300
301 GpuProcess gpu_process;
302
303 // These UMA must be stored after GpuProcess is constructed as it
304 // initializes StatisticsRecorder which tracks the histograms.
305 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
306 UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
307
308 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
309 dead_on_arrival,
310 gpu_info,
311 deferred_messages.Get());
312 while (!deferred_messages.Get().empty())
313 deferred_messages.Get().pop();
314
315 child_thread->Init(start_time);
316
317 gpu_process.set_main_thread(child_thread);
318
319 if (watchdog_thread)
320 watchdog_thread->AddPowerObserver();
321
322 {
323 TRACE_EVENT0("gpu", "Run Message Loop");
324 main_message_loop.Run();
325 }
326
327 child_thread->StopWatchdog();
328
329 return 0;
330 }
331
332 namespace {
333
334 #if defined(OS_LINUX)
CreateDummyGlContext()335 void CreateDummyGlContext() {
336 scoped_refptr<gfx::GLSurface> surface(
337 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)));
338 if (!surface.get()) {
339 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
340 return;
341 }
342
343 // On Linux, this is needed to make sure /dev/nvidiactl has
344 // been opened and its descriptor cached.
345 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
346 NULL, surface.get(), gfx::PreferDiscreteGpu));
347 if (!context.get()) {
348 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
349 return;
350 }
351
352 // Similarly, this is needed for /dev/nvidia0.
353 if (context->MakeCurrent(surface.get())) {
354 context->ReleaseCurrent(surface.get());
355 } else {
356 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
357 }
358 }
359 #endif
360
WarmUpSandbox(const CommandLine & command_line)361 bool WarmUpSandbox(const CommandLine& command_line) {
362 {
363 TRACE_EVENT0("gpu", "Warm up rand");
364 // Warm up the random subsystem, which needs to be done pre-sandbox on all
365 // platforms.
366 (void) base::RandUint64();
367 }
368 return true;
369 }
370
371 #if defined(OS_LINUX)
WarmUpSandboxNvidia(const gpu::GPUInfo & gpu_info,bool should_initialize_gl_context)372 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
373 bool should_initialize_gl_context) {
374 // We special case Optimus since the vendor_id we see may not be Nvidia.
375 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
376 gpu_info.driver_vendor == "NVIDIA") ||
377 gpu_info.optimus;
378 if (uses_nvidia_driver && should_initialize_gl_context) {
379 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
380 CreateDummyGlContext();
381 }
382 }
383
StartSandboxLinux(const gpu::GPUInfo & gpu_info,GpuWatchdogThread * watchdog_thread,bool should_initialize_gl_context)384 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
385 GpuWatchdogThread* watchdog_thread,
386 bool should_initialize_gl_context) {
387 TRACE_EVENT0("gpu", "Initialize sandbox");
388
389 bool res = false;
390
391 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
392
393 if (watchdog_thread) {
394 // LinuxSandbox needs to be able to ensure that the thread
395 // has really been stopped.
396 LinuxSandbox::StopThread(watchdog_thread);
397 }
398 // LinuxSandbox::InitializeSandbox() must always be called
399 // with only one thread.
400 res = LinuxSandbox::InitializeSandbox();
401 if (watchdog_thread) {
402 watchdog_thread->Start();
403 }
404
405 return res;
406 }
407 #endif // defined(OS_LINUX)
408
409 #if defined(OS_WIN)
StartSandboxWindows(const sandbox::SandboxInterfaceInfo * sandbox_info)410 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
411 TRACE_EVENT0("gpu", "Lower token");
412
413 // For Windows, if the target_services interface is not zero, the process
414 // is sandboxed and we must call LowerToken() before rendering untrusted
415 // content.
416 sandbox::TargetServices* target_services = sandbox_info->target_services;
417 if (target_services) {
418 target_services->LowerToken();
419 return true;
420 }
421
422 return false;
423 }
424 #endif // defined(OS_WIN)
425
426 } // namespace.
427
428 } // namespace content
429