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 "crypto/hmac.h"
33 #include "gpu/command_buffer/service/gpu_switches.h"
34 #include "gpu/config/gpu_info_collector.h"
35 #include "gpu/config/gpu_util.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 const int kGpuTimeout = 10000;
61 bool WarmUpSandbox(const CommandLine& command_line);
63 bool StartSandboxLinux(const gpu::GPUInfo&, GpuWatchdogThread*, bool);
65 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo*);
68 base::LazyInstance<GpuChildThread::DeferredMessages> deferred_messages =
69 LAZY_INSTANCE_INITIALIZER;
71 bool GpuProcessLogMessageHandler(int severity,
72 const char* file, int line,
74 const std::string& str) {
75 std::string header = str.substr(0, message_start);
76 std::string message = str.substr(message_start);
77 deferred_messages.Get().push(new GpuHostMsg_OnLogMessage(
78 severity, header, message));
82 } // namespace anonymous
84 // Main function for starting the Gpu process.
85 int GpuMain(const MainFunctionParams& parameters) {
86 TRACE_EVENT0("gpu", "GpuMain");
87 base::debug::TraceLog::GetInstance()->SetProcessName("GPU Process");
88 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
89 kTraceEventGpuProcessSortIndex);
91 const CommandLine& command_line = parameters.command_line;
92 if (command_line.HasSwitch(switches::kGpuStartupDialog)) {
93 ChildProcess::WaitForDebugger("Gpu");
96 base::Time start_time = base::Time::Now();
99 // Prevent Windows from displaying a modal dialog on failures like not being
100 // able to load a DLL.
102 SEM_FAILCRITICALERRORS |
103 SEM_NOGPFAULTERRORBOX |
104 SEM_NOOPENFILEERRORBOX);
105 #elif defined(USE_X11)
106 ui::SetDefaultX11ErrorHandlers();
109 logging::SetLogMessageHandler(GpuProcessLogMessageHandler);
111 if (command_line.HasSwitch(switches::kSupportsDualGpus)) {
112 std::string types = command_line.GetSwitchValueASCII(
113 switches::kGpuDriverBugWorkarounds);
114 std::set<int> workarounds;
115 gpu::StringToFeatureSet(types, &workarounds);
116 if (workarounds.count(gpu::FORCE_DISCRETE_GPU) == 1)
117 ui::GpuSwitchingManager::GetInstance()->ForceUseOfDiscreteGpu();
118 else if (workarounds.count(gpu::FORCE_INTEGRATED_GPU) == 1)
119 ui::GpuSwitchingManager::GetInstance()->ForceUseOfIntegratedGpu();
122 // Initialization of the OpenGL bindings may fail, in which case we
123 // will need to tear down this process. However, we can not do so
124 // safely until the IPC channel is set up, because the detection of
125 // early return of a child process is implemented using an IPC
126 // channel error. If the IPC channel is not fully set up between the
127 // browser and GPU process, and the GPU process crashes or exits
128 // early, the browser process will never detect it. For this reason
129 // we defer tearing down the GPU process until receiving the
130 // GpuMsg_Initialize message from the browser.
131 bool dead_on_arrival = false;
133 base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_IO;
135 // Unless we're running on desktop GL, we don't need a UI message
136 // loop, so avoid its use to work around apparent problems with some
137 // third-party software.
138 if (command_line.HasSwitch(switches::kUseGL) &&
139 command_line.GetSwitchValueASCII(switches::kUseGL) ==
140 gfx::kGLImplementationDesktopName) {
141 message_loop_type = base::MessageLoop::TYPE_UI;
143 #elif defined(TOOLKIT_GTK)
144 message_loop_type = base::MessageLoop::TYPE_GPU;
145 #elif defined(OS_LINUX)
146 message_loop_type = base::MessageLoop::TYPE_DEFAULT;
149 base::MessageLoop main_message_loop(message_loop_type);
150 base::PlatformThread::SetName("CrGpuMain");
152 // In addition to disabling the watchdog if the command line switch is
153 // present, disable the watchdog on valgrind because the code is expected
154 // to run slowly in that case.
155 bool enable_watchdog =
156 !command_line.HasSwitch(switches::kDisableGpuWatchdog) &&
157 !RunningOnValgrind();
159 // Disable the watchdog in debug builds because they tend to only be run by
160 // developers who will not appreciate the watchdog killing the GPU process.
162 enable_watchdog = false;
165 bool delayed_watchdog_enable = false;
167 #if defined(OS_CHROMEOS)
168 // Don't start watchdog immediately, to allow developers to switch to VT2 on
170 delayed_watchdog_enable = true;
173 scoped_refptr<GpuWatchdogThread> watchdog_thread;
175 // Start the GPU watchdog only after anything that is expected to be time
176 // consuming has completed, otherwise the process is liable to be aborted.
177 if (enable_watchdog && !delayed_watchdog_enable) {
178 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
179 watchdog_thread->Start();
182 gpu::GPUInfo gpu_info;
183 // Get vendor_id, device_id, driver_version from browser process through
184 // commandline switches.
185 DCHECK(command_line.HasSwitch(switches::kGpuVendorID) &&
186 command_line.HasSwitch(switches::kGpuDeviceID) &&
187 command_line.HasSwitch(switches::kGpuDriverVersion));
188 bool success = base::HexStringToUInt(
189 command_line.GetSwitchValueASCII(switches::kGpuVendorID),
190 &gpu_info.gpu.vendor_id);
192 success = base::HexStringToUInt(
193 command_line.GetSwitchValueASCII(switches::kGpuDeviceID),
194 &gpu_info.gpu.device_id);
196 gpu_info.driver_vendor =
197 command_line.GetSwitchValueASCII(switches::kGpuDriverVendor);
198 gpu_info.driver_version =
199 command_line.GetSwitchValueASCII(switches::kGpuDriverVersion);
200 GetContentClient()->SetGpuInfo(gpu_info);
202 base::TimeDelta collect_context_time;
203 base::TimeDelta initialize_one_off_time;
205 // Warm up resources that don't need access to GPUInfo.
206 if (WarmUpSandbox(command_line)) {
207 #if defined(OS_LINUX)
208 bool initialized_sandbox = false;
209 bool initialized_gl_context = false;
210 bool should_initialize_gl_context = false;
211 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
212 // On Chrome OS ARM, GPU driver userspace creates threads when initializing
213 // a GL context, so start the sandbox early.
214 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
215 should_initialize_gl_context);
216 initialized_sandbox = true;
218 #endif // defined(OS_LINUX)
220 base::TimeTicks before_initialize_one_off = base::TimeTicks::Now();
222 // Load and initialize the GL implementation and locate the GL entry points.
223 if (gfx::GLSurface::InitializeOneOff()) {
224 // We need to collect GL strings (VENDOR, RENDERER) for blacklisting
225 // purposes. However, on Mac we don't actually use them. As documented in
226 // crbug.com/222934, due to some driver issues, glGetString could take
227 // multiple seconds to finish, which in turn cause the GPU process to
229 // By skipping the following code on Mac, we don't really lose anything,
230 // because the basic GPU information is passed down from browser process
231 // and we already registered them through SetGpuInfo() above.
232 base::TimeTicks before_collect_context_graphics_info =
233 base::TimeTicks::Now();
234 #if !defined(OS_MACOSX)
235 if (!gpu::CollectContextGraphicsInfo(&gpu_info))
236 VLOG(1) << "gpu::CollectGraphicsInfo failed";
237 GetContentClient()->SetGpuInfo(gpu_info);
239 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
240 // Recompute gpu driver bug workarounds - this is specifically useful
241 // on systems where vendor_id/device_id aren't available.
242 if (!command_line.HasSwitch(switches::kDisableGpuDriverBugWorkarounds)) {
243 gpu::ApplyGpuDriverBugWorkarounds(
244 gpu_info, const_cast<CommandLine*>(&command_line));
248 #if defined(OS_LINUX)
249 initialized_gl_context = true;
250 #if !defined(OS_CHROMEOS)
251 if (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA
252 gpu_info.driver_vendor == "NVIDIA") {
253 base::ThreadRestrictions::AssertIOAllowed();
254 if (access("/dev/nvidiactl", R_OK) != 0) {
255 VLOG(1) << "NVIDIA device file /dev/nvidiactl access denied";
256 dead_on_arrival = true;
259 #endif // !defined(OS_CHROMEOS)
260 #endif // defined(OS_LINUX)
261 #endif // !defined(OS_MACOSX)
262 collect_context_time =
263 base::TimeTicks::Now() - before_collect_context_graphics_info;
265 VLOG(1) << "gfx::GLSurface::InitializeOneOff failed";
266 dead_on_arrival = true;
269 initialize_one_off_time =
270 base::TimeTicks::Now() - before_initialize_one_off;
272 if (enable_watchdog && delayed_watchdog_enable) {
273 watchdog_thread = new GpuWatchdogThread(kGpuTimeout);
274 watchdog_thread->Start();
277 // OSMesa is expected to run very slowly, so disable the watchdog in that
279 if (enable_watchdog &&
280 gfx::GetGLImplementation() == gfx::kGLImplementationOSMesaGL) {
281 watchdog_thread->Stop();
282 watchdog_thread = NULL;
285 #if defined(OS_LINUX)
286 should_initialize_gl_context = !initialized_gl_context &&
289 if (!initialized_sandbox) {
290 gpu_info.sandboxed = StartSandboxLinux(gpu_info, watchdog_thread.get(),
291 should_initialize_gl_context);
293 #elif defined(OS_WIN)
294 gpu_info.sandboxed = StartSandboxWindows(parameters.sandbox_info);
297 dead_on_arrival = true;
300 logging::SetLogMessageHandler(NULL);
302 GpuProcess gpu_process;
304 // These UMA must be stored after GpuProcess is constructed as it
305 // initializes StatisticsRecorder which tracks the histograms.
306 UMA_HISTOGRAM_TIMES("GPU.CollectContextGraphicsInfo", collect_context_time);
307 UMA_HISTOGRAM_TIMES("GPU.InitializeOneOffTime", initialize_one_off_time);
309 GpuChildThread* child_thread = new GpuChildThread(watchdog_thread.get(),
312 deferred_messages.Get());
313 while (!deferred_messages.Get().empty())
314 deferred_messages.Get().pop();
316 child_thread->Init(start_time);
318 gpu_process.set_main_thread(child_thread);
321 watchdog_thread->AddPowerObserver();
324 TRACE_EVENT0("gpu", "Run Message Loop");
325 main_message_loop.Run();
328 child_thread->StopWatchdog();
335 #if defined(OS_LINUX)
336 void CreateDummyGlContext() {
337 scoped_refptr<gfx::GLSurface> surface(
338 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)));
339 if (!surface.get()) {
340 VLOG(1) << "gfx::GLSurface::CreateOffscreenGLSurface failed";
344 // On Linux, this is needed to make sure /dev/nvidiactl has
345 // been opened and its descriptor cached.
346 scoped_refptr<gfx::GLContext> context(gfx::GLContext::CreateGLContext(
347 NULL, surface.get(), gfx::PreferDiscreteGpu));
348 if (!context.get()) {
349 VLOG(1) << "gfx::GLContext::CreateGLContext failed";
353 // Similarly, this is needed for /dev/nvidia0.
354 if (context->MakeCurrent(surface.get())) {
355 context->ReleaseCurrent(surface.get());
357 VLOG(1) << "gfx::GLContext::MakeCurrent failed";
362 bool WarmUpSandbox(const CommandLine& command_line) {
364 TRACE_EVENT0("gpu", "Warm up rand");
365 // Warm up the random subsystem, which needs to be done pre-sandbox on all
367 (void) base::RandUint64();
370 TRACE_EVENT0("gpu", "Warm up HMAC");
371 // Warm up the crypto subsystem, which needs to done pre-sandbox on all
373 crypto::HMAC hmac(crypto::HMAC::SHA256);
374 unsigned char key = '\0';
375 if (!hmac.Init(&key, sizeof(key))) {
376 LOG(ERROR) << "WarmUpSandbox() failed with crypto::HMAC::Init()";
384 #if defined(OS_LINUX)
385 void WarmUpSandboxNvidia(const gpu::GPUInfo& gpu_info,
386 bool should_initialize_gl_context) {
387 // We special case Optimus since the vendor_id we see may not be Nvidia.
388 bool uses_nvidia_driver = (gpu_info.gpu.vendor_id == 0x10de && // NVIDIA.
389 gpu_info.driver_vendor == "NVIDIA") ||
391 if (uses_nvidia_driver && should_initialize_gl_context) {
392 // We need this on Nvidia to pre-open /dev/nvidiactl and /dev/nvidia0.
393 CreateDummyGlContext();
397 bool StartSandboxLinux(const gpu::GPUInfo& gpu_info,
398 GpuWatchdogThread* watchdog_thread,
399 bool should_initialize_gl_context) {
400 TRACE_EVENT0("gpu", "Initialize sandbox");
404 WarmUpSandboxNvidia(gpu_info, should_initialize_gl_context);
407 watchdog_thread->Stop();
408 // LinuxSandbox::InitializeSandbox() must always be called
409 // with only one thread.
410 res = LinuxSandbox::InitializeSandbox();
412 watchdog_thread->Start();
416 #endif // defined(OS_LINUX)
419 bool StartSandboxWindows(const sandbox::SandboxInterfaceInfo* sandbox_info) {
420 TRACE_EVENT0("gpu", "Lower token");
422 // For Windows, if the target_services interface is not zero, the process
423 // is sandboxed and we must call LowerToken() before rendering untrusted
425 sandbox::TargetServices* target_services = sandbox_info->target_services;
426 if (target_services) {
427 target_services->LowerToken();
433 #endif // defined(OS_WIN)
437 } // namespace content