1 /*
2 * Copyright (C) 2010 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 #include <jni.h>
18 #include <string.h>
19 #include <sys/auxv.h>
20 #include <sys/system_properties.h>
21 #include <sys/utsname.h>
22
23 #include <string>
24
android_cts_CpuFeatures_isArmCpu(JNIEnv * env,jobject thiz)25 jboolean android_cts_CpuFeatures_isArmCpu(JNIEnv* env, jobject thiz)
26 {
27 #if defined(__arm__)
28 return JNI_TRUE;
29 #else
30 return JNI_FALSE;
31 #endif
32 }
33
android_cts_CpuFeatures_isX86Cpu(JNIEnv * env,jobject thiz)34 jboolean android_cts_CpuFeatures_isX86Cpu(JNIEnv* env, jobject thiz)
35 {
36 #if defined(__i386__)
37 return JNI_TRUE;
38 #else
39 return JNI_FALSE;
40 #endif
41 }
42
android_cts_CpuFeatures_isArm64Cpu(JNIEnv * env,jobject thiz)43 jboolean android_cts_CpuFeatures_isArm64Cpu(JNIEnv* env, jobject thiz)
44 {
45 #if defined(__aarch64__)
46 return JNI_TRUE;
47 #else
48 return JNI_FALSE;
49 #endif
50 }
51
android_cts_CpuFeatures_isRiscv64Cpu(JNIEnv * env,jobject thiz)52 jboolean android_cts_CpuFeatures_isRiscv64Cpu(JNIEnv* env, jobject thiz)
53 {
54 #if defined(__riscv)
55 return JNI_TRUE;
56 #else
57 return JNI_FALSE;
58 #endif
59 }
60
android_cts_CpuFeatures_isX86_64Cpu(JNIEnv * env,jobject thiz)61 jboolean android_cts_CpuFeatures_isX86_64Cpu(JNIEnv* env, jobject thiz)
62 {
63 #if defined(__x86_64__)
64 return JNI_TRUE;
65 #else
66 return JNI_FALSE;
67 #endif
68 }
69
android_cts_CpuFeatures_getHwCaps(JNIEnv *,jobject)70 jint android_cts_CpuFeatures_getHwCaps(JNIEnv*, jobject)
71 {
72 return (jint)getauxval(AT_HWCAP);
73 }
74
android_cts_CpuFeatures_isNativeBridgedCpu(JNIEnv * env,jobject thiz)75 jboolean android_cts_CpuFeatures_isNativeBridgedCpu(JNIEnv* env, jobject thiz)
76 {
77 #if defined(__arm__)
78 static const prop_info* pi = __system_property_find("ro.dalvik.vm.isa.arm");
79 return pi != nullptr;
80 #endif
81 #if defined(__arm__) || defined(__aarch64__)
82 // If the test is compiled for arm use uname() to check if host CPU is x86.
83 struct utsname uname_data;
84 uname(&uname_data);
85 std::string machine = uname_data.machine;
86 // Matches all of i386, i686 and x86_64.
87 return machine.find("86") != std::string::npos;
88 #else
89 return false;
90 #endif
91 }
92
93 static JNINativeMethod gMethods[] = {
94 { "isArmCpu", "()Z",
95 (void *) android_cts_CpuFeatures_isArmCpu },
96 { "isX86Cpu", "()Z",
97 (void *) android_cts_CpuFeatures_isX86Cpu },
98 { "isArm64Cpu", "()Z",
99 (void *) android_cts_CpuFeatures_isArm64Cpu },
100 { "isRiscv64Cpu", "()Z",
101 (void *) android_cts_CpuFeatures_isRiscv64Cpu },
102 { "isX86_64Cpu", "()Z",
103 (void *) android_cts_CpuFeatures_isX86_64Cpu },
104 { "getHwCaps", "()I",
105 (void *) android_cts_CpuFeatures_getHwCaps },
106 { "isNativeBridgedCpu", "()Z",
107 (void *) android_cts_CpuFeatures_isNativeBridgedCpu },
108 };
109
register_android_cts_CpuFeatures(JNIEnv * env)110 int register_android_cts_CpuFeatures(JNIEnv* env)
111 {
112 jclass clazz = env->FindClass("com/android/compatibility/common/util/CpuFeatures");
113
114 return env->RegisterNatives(clazz, gMethods,
115 sizeof(gMethods) / sizeof(JNINativeMethod));
116 }
117