blob: a5152b6e689c13a00e2f952386b0ba721942fc10 [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
Derek Sollenberger9ca0c522019-08-29 13:08:02 -040019#include <android/graphics/jni_runtime.h>
Jesse Hall42cf26e2017-07-06 15:30:39 -070020#include <ui/GraphicBufferMapper.h>
21
22#include "core_jni_helpers.h"
23
24namespace {
25
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070026// Shadow call stack (SCS) is a security mitigation that uses a separate stack
27// (the SCS) for return addresses. In versions of Android newer than P, the
28// compiler cooperates with the system to ensure that the SCS address is always
29// stored in register x18, as long as the app was compiled with a new enough
30// compiler and does not use features that rely on SP-HALs (this restriction is
31// because the SP-HALs might not preserve x18 due to potentially having been
32// compiled with an old compiler as a consequence of Treble; it generally means
33// that the app must be a system app without a UI). This struct is used to
34// temporarily store the address on the stack while preloading the SP-HALs, so
35// that such apps can use the same zygote as everything else.
36struct ScopedSCSExit {
37#ifdef __aarch64__
38 void* scs;
39
40 ScopedSCSExit() {
41 __asm__ __volatile__("str x18, [%0]" ::"r"(&scs));
42 }
43
44 ~ScopedSCSExit() {
45 __asm__ __volatile__("ldr x18, [%0]; str xzr, [%0]" ::"r"(&scs));
46 }
47#else
48 // Silence unused variable warnings in non-SCS builds.
49 ScopedSCSExit() {}
50 ~ScopedSCSExit() {}
51#endif
52};
53
Jesse Hall42cf26e2017-07-06 15:30:39 -070054void android_internal_os_ZygoteInit_nativePreloadAppProcessHALs(JNIEnv* env, jclass) {
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070055 ScopedSCSExit x;
Jesse Hall42cf26e2017-07-06 15:30:39 -070056 android::GraphicBufferMapper::preloadHal();
57 // Add preloading here for other HALs that are (a) always passthrough, and
58 // (b) loaded by most app processes.
59}
60
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070061void android_internal_os_ZygoteInit_nativePreloadGraphicsDriver(JNIEnv* env, jclass) {
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070062 ScopedSCSExit x;
Derek Sollenberger9ca0c522019-08-29 13:08:02 -040063 zygote_preload_graphics();
Peter Collingbourne6f4986b2018-10-26 14:52:14 -070064}
65
Jesse Hall42cf26e2017-07-06 15:30:39 -070066const JNINativeMethod gMethods[] = {
67 { "nativePreloadAppProcessHALs", "()V",
68 (void*)android_internal_os_ZygoteInit_nativePreloadAppProcessHALs },
Yiwei Zhang4bf3d9e2019-04-15 16:24:32 -070069 { "nativePreloadGraphicsDriver", "()V",
70 (void*)android_internal_os_ZygoteInit_nativePreloadGraphicsDriver },
Jesse Hall42cf26e2017-07-06 15:30:39 -070071};
72
73} // anonymous namespace
74
75namespace android {
76
77int register_com_android_internal_os_ZygoteInit(JNIEnv* env) {
78 return RegisterMethodsOrDie(env, "com/android/internal/os/ZygoteInit",
79 gMethods, NELEM(gMethods));
80}
81
82} // namespace android