blob: 5cca0fdc735bc7d76e62349957b43cdf063384da [file] [log] [blame]
Jesse Hall42cf26e2017-07-06 15:30:39 -07001/*
2 * Copyright (C) 2017 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
17#define LOG_TAG "Zygote"
18
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070019#include <EGL/egl.h>
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070020#include <Properties.h>
Jesse Hall42cf26e2017-07-06 15:30:39 -070021#include <ui/GraphicBufferMapper.h>
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070022#include <vulkan/vulkan.h>
Jesse Hall42cf26e2017-07-06 15:30:39 -070023
24#include "core_jni_helpers.h"
25
26namespace {
27
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070028using android::uirenderer::Properties;
29using android::uirenderer::RenderPipelineType;
30
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070031// Shadow call stack (SCS) is a security mitigation that uses a separate stack
32// (the SCS) for return addresses. In versions of Android newer than P, the
33// compiler cooperates with the system to ensure that the SCS address is always
34// stored in register x18, as long as the app was compiled with a new enough
35// compiler and does not use features that rely on SP-HALs (this restriction is
36// because the SP-HALs might not preserve x18 due to potentially having been
37// compiled with an old compiler as a consequence of Treble; it generally means
38// that the app must be a system app without a UI). This struct is used to
39// temporarily store the address on the stack while preloading the SP-HALs, so
40// that such apps can use the same zygote as everything else.
41struct ScopedSCSExit {
42#ifdef __aarch64__
43 void* scs;
44
45 ScopedSCSExit() {
46 __asm__ __volatile__("str x18, [%0]" ::"r"(&scs));
47 }
48
49 ~ScopedSCSExit() {
50 __asm__ __volatile__("ldr x18, [%0]; str xzr, [%0]" ::"r"(&scs));
51 }
52#else
53 // Silence unused variable warnings in non-SCS builds.
54 ScopedSCSExit() {}
55 ~ScopedSCSExit() {}
56#endif
57};
58
Jesse Hall42cf26e2017-07-06 15:30:39 -070059void android_internal_os_ZygoteInit_nativePreloadAppProcessHALs(JNIEnv* env, jclass) {
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070060 ScopedSCSExit x;
Jesse Hall42cf26e2017-07-06 15:30:39 -070061 android::GraphicBufferMapper::preloadHal();
62 // Add preloading here for other HALs that are (a) always passthrough, and
63 // (b) loaded by most app processes.
64}
65
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070066void android_internal_os_ZygoteInit_nativePreloadGraphicsDriver(JNIEnv* env, jclass) {
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070067 ScopedSCSExit x;
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070068 if (Properties::peekRenderPipelineType() == RenderPipelineType::SkiaGL) {
69 eglGetDisplay(EGL_DEFAULT_DISPLAY);
70 } else {
71 uint32_t count = 0;
72 vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
73 }
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070074}
75
Jesse Hall42cf26e2017-07-06 15:30:39 -070076const JNINativeMethod gMethods[] = {
77 { "nativePreloadAppProcessHALs", "()V",
78 (void*)android_internal_os_ZygoteInit_nativePreloadAppProcessHALs },
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070079 { "nativePreloadGraphicsDriver", "()V",
80 (void*)android_internal_os_ZygoteInit_nativePreloadGraphicsDriver },
Jesse Hall42cf26e2017-07-06 15:30:39 -070081};
82
83} // anonymous namespace
84
85namespace android {
86
87int register_com_android_internal_os_ZygoteInit(JNIEnv* env) {
88 return RegisterMethodsOrDie(env, "com/android/internal/os/ZygoteInit",
89 gMethods, NELEM(gMethods));
90}
91
92} // namespace android