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.
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/events/platform/platform_event_source.h"
36 #include "ui/gl/gl_implementation.h"
37 #include "ui/gl/gl_surface.h"
38 #include "ui/gl/gl_switches.h"
39 #include "ui/gl/gpu_switching_manager.h"
42 #include "base/win/windows_version.h"
43 #include "base/win/scoped_com_initializer.h"
44 #include "sandbox/win/src/sandbox.h"
48 #include "ui/base/x/x11_util.h"
52 #include "content/public/common/sandbox_init.h"
55 #if defined(OS_MACOSX)
56 #include "base/message_loop/message_pump_mac.h"
59 #if defined(ADDRESS_SANITIZER)
60 #include <sanitizer/asan_interface.h>
63 const int kGpuTimeout
= 10000;
69 void GetGpuInfoFromCommandLine(gpu::GPUInfo
& gpu_info
,
70 const CommandLine
& command_line
);
71 bool WarmUpSandbox(const CommandLine
& command_line
);
73 #if !defined(OS_MACOSX)
74 bool CollectGraphicsInfo(gpu::GPUInfo
& gpu_info
);
78 #if !defined(OS_CHROMEOS)
79 bool CanAccessNvidiaDeviceFile();
81 bool StartSandboxLinux(const gpu::GPUInfo
&, GpuWatchdogThread
*, bool);
83 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo
*);
86 base::LazyInstance
<GpuChildThread::DeferredMessages
> deferred_messages
=
87 LAZY_INSTANCE_INITIALIZER
;
89 bool GpuProcessLogMessageHandler(int severity
,
90 const char* file
, int line
,
92 const std::string
& str
) {
93 std::string header
= str
.substr(0, message_start
);
94 std::string message
= str
.substr(message_start
);
95 deferred_messages
.Get().push(new GpuHostMsg_OnLogMessage(
96 severity
, header
, message
));
100 } // namespace anonymous
102 // Main function for starting the Gpu process.
103 int GpuMain(const MainFunctionParams
& parameters
) {
104 TRACE_EVENT0("gpu", "GpuMain");
105 base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
106 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
107 kTraceEventGpuProcessSortIndex
);
109 const CommandLine
& command_line
= parameters
.command_line
;
110 if (command_line
.HasSwitch(switches::kGpuStartupDialog
)) {
111 ChildProcess::WaitForDebugger("Gpu");
114 base::Time start_time
= base::Time::Now();
117 // Prevent Windows from displaying a modal dialog on failures like not being
118 // able to load a DLL.
120 SEM_FAILCRITICALERRORS
|
121 SEM_NOGPFAULTERRORBOX
|
122 SEM_NOOPENFILEERRORBOX
);
123 #elif defined(USE_X11)
124 ui::SetDefaultX11ErrorHandlers();
127 logging::SetLogMessageHandler(GpuProcessLogMessageHandler
);
129 if (command_line
.HasSwitch(switches::kSupportsDualGpus
)) {
130 std::string types
= command_line
.GetSwitchValueASCII(
131 switches::kGpuDriverBugWorkarounds
);
132 std::set
<int> workarounds
;
133 gpu::StringToFeatureSet(types
, &workarounds
);
134 if (workarounds
.count(gpu::FORCE_DISCRETE_GPU
) == 1)
135 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
136 else if (workarounds
.count(gpu::FORCE_INTEGRATED_GPU
) == 1)
137 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
140 // Initialization of the OpenGL bindings may fail, in which case we
141 // will need to tear down this process. However, we can not do so
142 // safely until the IPC channel is set up, because the detection of
143 // early return of a child process is implemented using an IPC
144 // channel error. If the IPC channel is not fully set up between the
145 // browser and GPU process, and the GPU process crashes or exits
146 // early, the browser process will never detect it. For this reason
147 // we defer tearing down the GPU process until receiving the
148 // GpuMsg_Initialize message from the browser.
149 bool dead_on_arrival
= false;
152 base::MessageLoop::Type message_loop_type
= base::MessageLoop::TYPE_IO
;
153 // Unless we're running on desktop GL, we don't need a UI message
154 // loop, so avoid its use to work around apparent problems with some
155 // third-party software.
156 if (command_line
.HasSwitch(switches::kUseGL
) &&
157 command_line
.GetSwitchValueASCII(switches::kUseGL
) ==
158 gfx::kGLImplementationDesktopName
) {
159 message_loop_type
= base::MessageLoop::TYPE_UI
;
161 base::MessageLoop
main_message_loop(message_loop_type
);
162 #elif defined(OS_LINUX) && defined(USE_X11)
163 // We need a UI loop so that we can grab the Expose events. See GLSurfaceGLX
164 // and https://crbug.com/326995.
165 base::MessageLoop
main_message_loop(base::MessageLoop::TYPE_UI
);
166 scoped_ptr
<ui::PlatformEventSource
> event_source
=
167 ui::PlatformEventSource::CreateDefault();
168 #elif defined(OS_LINUX)
169 base::MessageLoop
main_message_loop(base::MessageLoop::TYPE_DEFAULT
);
170 #elif defined(OS_MACOSX)
171 // This is necessary for CoreAnimation layers hosted in the GPU process to be
172 // drawn. See http://crbug.com/312462.
173 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpCFRunLoop());
174 base::MessageLoop
main_message_loop(pump
.Pass());
176 base::MessageLoop
main_message_loop(base::MessageLoop::TYPE_IO
);
179 base::PlatformThread::SetName("CrGpuMain");
181 // In addition to disabling the watchdog if the command line switch is
182 // present, disable the watchdog on valgrind because the code is expected
183 // to run slowly in that case.
184 bool enable_watchdog
=
185 !command_line
.HasSwitch(switches::kDisableGpuWatchdog
) &&
186 !RunningOnValgrind();
188 // Disable the watchdog in debug builds because they tend to only be run by
189 // developers who will not appreciate the watchdog killing the GPU process.
191 enable_watchdog
= false;
194 bool delayed_watchdog_enable
= false;
196 #if defined(OS_CHROMEOS)
197 // Don't start watchdog immediately, to allow developers to switch to VT2 on
199 delayed_watchdog_enable
= true;
202 scoped_refptr
<GpuWatchdogThread
> watchdog_thread
;
204 // Start the GPU watchdog only after anything that is expected to be time
205 // consuming has completed, otherwise the process is liable to be aborted.
206 if (enable_watchdog
&& !delayed_watchdog_enable
) {
207 watchdog_thread
= new GpuWatchdogThread(kGpuTimeout
);
208 watchdog_thread
->Start();
211 gpu::GPUInfo gpu_info
;
212 // Get vendor_id, device_id, driver_version from browser process through
213 // commandline switches.
214 GetGpuInfoFromCommandLine(gpu_info
, command_line
);
216 base::TimeDelta collect_context_time
;
217 base::TimeDelta initialize_one_off_time
;
219 // Warm up resources that don't need access to GPUInfo.
220 if (WarmUpSandbox(command_line
)) {
221 #if defined(OS_LINUX)
222 bool initialized_sandbox
= false;
223 bool initialized_gl_context
= false;
224 bool should_initialize_gl_context
= false;
225 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
226 // On Chrome OS ARM Mali, GPU driver userspace creates threads when
227 // initializing a GL context, so start the sandbox early.
228 if (!command_line
.HasSwitch(
229 switches::kGpuSandboxStartAfterInitialization
)) {
230 gpu_info
.sandboxed
= StartSandboxLinux(gpu_info
, watchdog_thread
.get(),
231 should_initialize_gl_context
);
232 initialized_sandbox
= true;
235 #endif // defined(OS_LINUX)
237 base::TimeTicks before_initialize_one_off
= base::TimeTicks::Now();
239 // Determine if we need to initialize GL here or it has already been done.
240 bool gl_already_initialized
= false;
241 #if defined(OS_MACOSX)
242 if (!command_line
.HasSwitch(switches::kNoSandbox
)) {
243 // On Mac, if the sandbox is enabled, then GLSurface::InitializeOneOff()
244 // is called from the sandbox warmup code before getting here.
245 gl_already_initialized
= true;
248 if (command_line
.HasSwitch(switches::kInProcessGPU
)) {
249 // With in-process GPU, GLSurface::InitializeOneOff() is called from
250 // GpuChildThread before getting here.
251 gl_already_initialized
= true;
254 // Load and initialize the GL implementation and locate the GL entry points.
255 bool gl_initialized
=
256 gl_already_initialized
257 ? gfx::GetGLImplementation() != gfx::kGLImplementationNone
258 : gfx::GLSurface::InitializeOneOff();
259 if (gl_initialized
) {
260 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
261 // purposes. However, on Mac we don't actually use them. As documented in
262 // crbug.com/222934, due to some driver issues, glGetString could take
263 // multiple seconds to finish, which in turn cause the GPU process to
265 // By skipping the following code on Mac, we don't really lose anything,
266 // because the basic GPU information is passed down from browser process
267 // and we already registered them through SetGpuInfo() above.
268 base::TimeTicks before_collect_context_graphics_info
=
269 base::TimeTicks::Now();
270 #if !defined(OS_MACOSX)
271 if (!CollectGraphicsInfo(gpu_info
))
272 dead_on_arrival
= true;
274 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
275 // Recompute gpu driver bug workarounds - this is specifically useful
276 // on systems where vendor_id/device_id aren't available.
277 if (!command_line
.HasSwitch(switches::kDisableGpuDriverBugWorkarounds
)) {
278 gpu::ApplyGpuDriverBugWorkarounds(
279 gpu_info
, const_cast<CommandLine
*>(&command_line
));
283 #if defined(OS_LINUX)
284 initialized_gl_context
= true;
285 #if !defined(OS_CHROMEOS)
286 if (gpu_info
.gpu
.vendor_id
== 0x10de && // NVIDIA
287 gpu_info
.driver_vendor
== "NVIDIA" &&
288 !CanAccessNvidiaDeviceFile())
289 dead_on_arrival
= true;
290 #endif // !defined(OS_CHROMEOS)
291 #endif // defined(OS_LINUX)
292 #endif // !defined(OS_MACOSX)
293 collect_context_time
=
294 base::TimeTicks::Now() - before_collect_context_graphics_info
;
295 } else { // gl_initialized
296 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
297 dead_on_arrival
= true;
300 initialize_one_off_time
=
301 base::TimeTicks::Now() - before_initialize_one_off
;
303 if (enable_watchdog
&& delayed_watchdog_enable
) {
304 watchdog_thread
= new GpuWatchdogThread(kGpuTimeout
);
305 watchdog_thread
->Start();
308 // OSMesa is expected to run very slowly, so disable the watchdog in that
310 if (enable_watchdog
&&
311 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL
) {
312 watchdog_thread
->Stop();
313 watchdog_thread
= NULL
;
316 #if defined(OS_LINUX)
317 should_initialize_gl_context
= !initialized_gl_context
&&
320 if (!initialized_sandbox
) {
321 gpu_info
.sandboxed
= StartSandboxLinux(gpu_info
, watchdog_thread
.get(),
322 should_initialize_gl_context
);
324 #elif defined(OS_WIN)
325 gpu_info
.sandboxed
= StartSandboxWindows(parameters
.sandbox_info
);
328 dead_on_arrival
= true;
331 logging::SetLogMessageHandler(NULL
);
333 GpuProcess gpu_process
;
335 // These UMA must be stored after GpuProcess is constructed as it
336 // initializes StatisticsRecorder which tracks the histograms.
337 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time
);
338 UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time
);
340 GpuChildThread
* child_thread
= new GpuChildThread(watchdog_thread
.get(),
343 deferred_messages
.Get());
344 while (!deferred_messages
.Get().empty())
345 deferred_messages
.Get().pop();
347 child_thread
->Init(start_time
);
349 gpu_process
.set_main_thread(child_thread
);
352 watchdog_thread
->AddPowerObserver();
355 TRACE_EVENT0("gpu", "Run Message Loop");
356 main_message_loop
.Run();
359 child_thread
->StopWatchdog();
366 void GetGpuInfoFromCommandLine(gpu::GPUInfo
& gpu_info
,
367 const CommandLine
& command_line
) {
368 DCHECK(command_line
.HasSwitch(switches::kGpuVendorID
) &&
369 command_line
.HasSwitch(switches::kGpuDeviceID
) &&
370 command_line
.HasSwitch(switches::kGpuDriverVersion
));
371 bool success
= base::HexStringToUInt(
372 command_line
.GetSwitchValueASCII(switches::kGpuVendorID
),
373 &gpu_info
.gpu
.vendor_id
);
375 success
= base::HexStringToUInt(
376 command_line
.GetSwitchValueASCII(switches::kGpuDeviceID
),
377 &gpu_info
.gpu
.device_id
);
379 gpu_info
.driver_vendor
=
380 command_line
.GetSwitchValueASCII(switches::kGpuDriverVendor
);
381 gpu_info
.driver_version
=
382 command_line
.GetSwitchValueASCII(switches::kGpuDriverVersion
);
383 GetContentClient()->SetGpuInfo(gpu_info
);
386 bool WarmUpSandbox(const CommandLine
& command_line
) {
388 TRACE_EVENT0("gpu", "Warm up rand");
389 // Warm up the random subsystem, which needs to be done pre-sandbox on all
391 (void) base::RandUint64();
396 #if !defined(OS_MACOSX)
397 bool CollectGraphicsInfo(gpu::GPUInfo
& gpu_info
) {
399 gpu::CollectInfoResult result
= gpu::CollectContextGraphicsInfo(&gpu_info
);
401 case gpu::kCollectInfoFatalFailure
:
402 LOG(ERROR
) << "gpu::CollectGraphicsInfo failed (fatal).";
405 case gpu::kCollectInfoNonFatalFailure
:
406 VLOG(1) << "gpu::CollectGraphicsInfo failed (non-fatal).";
408 case gpu::kCollectInfoSuccess
:
411 GetContentClient()->SetGpuInfo(gpu_info
);
416 #if defined(OS_LINUX)
417 #if !defined(OS_CHROMEOS)
418 bool CanAccessNvidiaDeviceFile() {
420 base::ThreadRestrictions::AssertIOAllowed();
421 if (access("/dev/nvidiactl", R_OK
) != 0) {
422 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
429 void CreateDummyGlContext() {
430 scoped_refptr
<gfx::GLSurface
> surface(
431 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
432 if (!surface
.get()) {
433 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
437 // On Linux, this is needed to make sure /dev/nvidiactl has
438 // been opened and its descriptor cached.
439 scoped_refptr
<gfx::GLContext
> context(gfx::GLContext::CreateGLContext(
440 NULL
, surface
.get(), gfx::PreferDiscreteGpu
));
441 if (!context
.get()) {
442 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
446 // Similarly, this is needed for /dev/nvidia0.
447 if (context
->MakeCurrent(surface
.get())) {
448 context
->ReleaseCurrent(surface
.get());
450 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
454 void WarmUpSandboxNvidia(const gpu::GPUInfo
& gpu_info
,
455 bool should_initialize_gl_context
) {
456 // We special case Optimus since the vendor_id we see may not be Nvidia.
457 bool uses_nvidia_driver
= (gpu_info
.gpu
.vendor_id
== 0x10de && // NVIDIA.
458 gpu_info
.driver_vendor
== "NVIDIA") ||
460 if (uses_nvidia_driver
&& should_initialize_gl_context
) {
461 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
462 CreateDummyGlContext();
466 bool StartSandboxLinux(const gpu::GPUInfo
& gpu_info
,
467 GpuWatchdogThread
* watchdog_thread
,
468 bool should_initialize_gl_context
) {
469 TRACE_EVENT0("gpu", "Initialize sandbox");
473 WarmUpSandboxNvidia(gpu_info
, should_initialize_gl_context
);
475 if (watchdog_thread
) {
476 // LinuxSandbox needs to be able to ensure that the thread
477 // has really been stopped.
478 LinuxSandbox::StopThread(watchdog_thread
);
481 #if defined(ADDRESS_SANITIZER)
482 const std::string sancov_file_name
=
483 "gpu." + base::Uint64ToString(base::RandUint64());
484 LinuxSandbox
* linux_sandbox
= LinuxSandbox::GetInstance();
485 linux_sandbox
->sanitizer_args()->coverage_sandboxed
= 1;
486 linux_sandbox
->sanitizer_args()->coverage_fd
=
487 __sanitizer_maybe_open_cov_file(sancov_file_name
.c_str());
488 linux_sandbox
->sanitizer_args()->coverage_max_block_size
= 0;
491 // LinuxSandbox::InitializeSandbox() must always be called
492 // with only one thread.
493 res
= LinuxSandbox::InitializeSandbox();
494 if (watchdog_thread
) {
495 watchdog_thread
->Start();
500 #endif // defined(OS_LINUX)
503 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo
* sandbox_info
) {
504 TRACE_EVENT0("gpu", "Lower token");
506 // For Windows, if the target_services interface is not zero, the process
507 // is sandboxed and we must call LowerToken() before rendering untrusted
509 sandbox::TargetServices
* target_services
= sandbox_info
->target_services
;
510 if (target_services
) {
511 target_services
->LowerToken();
517 #endif // defined(OS_WIN)
521 } // namespace content