Jesse Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 19 | #include <EGL/egl.h> |
Yiwei Zhang | 4bf3d9e | 2019-04-15 16:24:32 -0700 | [diff] [blame] | 20 | #include <Properties.h> |
Jesse Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 21 | #include <ui/GraphicBufferMapper.h> |
Yiwei Zhang | 4bf3d9e | 2019-04-15 16:24:32 -0700 | [diff] [blame] | 22 | #include <vulkan/vulkan.h> |
Jesse Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 23 | |
| 24 | #include "core_jni_helpers.h" |
| 25 | |
| 26 | namespace { |
| 27 | |
Yiwei Zhang | 4bf3d9e | 2019-04-15 16:24:32 -0700 | [diff] [blame] | 28 | using android::uirenderer::Properties; |
| 29 | using android::uirenderer::RenderPipelineType; |
| 30 | |
Peter Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 31 | // 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. |
| 41 | struct 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 Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 59 | void android_internal_os_ZygoteInit_nativePreloadAppProcessHALs(JNIEnv* env, jclass) { |
Peter Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 60 | ScopedSCSExit x; |
Jesse Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 61 | 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 Zhang | 4bf3d9e | 2019-04-15 16:24:32 -0700 | [diff] [blame] | 66 | void android_internal_os_ZygoteInit_nativePreloadGraphicsDriver(JNIEnv* env, jclass) { |
Peter Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 67 | ScopedSCSExit x; |
Yiwei Zhang | 4bf3d9e | 2019-04-15 16:24:32 -0700 | [diff] [blame] | 68 | if (Properties::peekRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 69 | eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 70 | } else { |
| 71 | uint32_t count = 0; |
| 72 | vkEnumerateInstanceExtensionProperties(nullptr, &count, nullptr); |
| 73 | } |
Peter Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jesse Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 76 | const JNINativeMethod gMethods[] = { |
| 77 | { "nativePreloadAppProcessHALs", "()V", |
| 78 | (void*)android_internal_os_ZygoteInit_nativePreloadAppProcessHALs }, |
Yiwei Zhang | 4bf3d9e | 2019-04-15 16:24:32 -0700 | [diff] [blame] | 79 | { "nativePreloadGraphicsDriver", "()V", |
| 80 | (void*)android_internal_os_ZygoteInit_nativePreloadGraphicsDriver }, |
Jesse Hall | 42cf26e | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // anonymous namespace |
| 84 | |
| 85 | namespace android { |
| 86 | |
| 87 | int 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 |