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 <cpu-features.h>
18 #include <jni.h>
19 #include <string.h>
20 #include <sys/auxv.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     AndroidCpuFamily cpuFamily = android_getCpuFamily();
28     return cpuFamily == ANDROID_CPU_FAMILY_ARM;
29 }
30 
android_cts_CpuFeatures_isX86Cpu(JNIEnv * env,jobject thiz)31 jboolean android_cts_CpuFeatures_isX86Cpu(JNIEnv* env, jobject thiz)
32 {
33     AndroidCpuFamily cpuFamily = android_getCpuFamily();
34     return cpuFamily == ANDROID_CPU_FAMILY_X86;
35 }
36 
android_cts_CpuFeatures_isArm64Cpu(JNIEnv * env,jobject thiz)37 jboolean android_cts_CpuFeatures_isArm64Cpu(JNIEnv* env, jobject thiz)
38 {
39     AndroidCpuFamily cpuFamily = android_getCpuFamily();
40     return cpuFamily == ANDROID_CPU_FAMILY_ARM64;
41 }
42 
android_cts_CpuFeatures_isX86_64Cpu(JNIEnv * env,jobject thiz)43 jboolean android_cts_CpuFeatures_isX86_64Cpu(JNIEnv* env, jobject thiz)
44 {
45     AndroidCpuFamily cpuFamily = android_getCpuFamily();
46     return cpuFamily == ANDROID_CPU_FAMILY_X86_64;
47 }
48 
android_cts_CpuFeatures_getHwCaps(JNIEnv *,jobject)49 jint android_cts_CpuFeatures_getHwCaps(JNIEnv*, jobject)
50 {
51     return (jint)getauxval(AT_HWCAP);
52 }
53 
android_cts_CpuFeatures_isNativeBridgedCpu(JNIEnv * env,jobject thiz)54 jboolean android_cts_CpuFeatures_isNativeBridgedCpu(JNIEnv* env, jobject thiz)
55 {
56 #if defined(__arm__) || defined(__aarch64__)
57   // If the test is compiled for arm use uname() to check if host CPU is x86.
58   struct utsname uname_data;
59   uname(&uname_data);
60   std::string machine = uname_data.machine;
61   // Matches all of i386, i686 and x86_64.
62   return machine.find("86") != std::string::npos;
63 #else
64   return false;
65 #endif
66 }
67 
68 static JNINativeMethod gMethods[] = {
69     {  "isArmCpu", "()Z",
70             (void *) android_cts_CpuFeatures_isArmCpu  },
71     {  "isX86Cpu", "()Z",
72             (void *) android_cts_CpuFeatures_isX86Cpu  },
73     {  "isArm64Cpu", "()Z",
74             (void *) android_cts_CpuFeatures_isArm64Cpu  },
75     {  "isX86_64Cpu", "()Z",
76             (void *) android_cts_CpuFeatures_isX86_64Cpu  },
77     {  "getHwCaps", "()I",
78             (void *) android_cts_CpuFeatures_getHwCaps  },
79     {  "isNativeBridgedCpu", "()Z",
80             (void *) android_cts_CpuFeatures_isNativeBridgedCpu  },
81 };
82 
register_android_cts_CpuFeatures(JNIEnv * env)83 int register_android_cts_CpuFeatures(JNIEnv* env)
84 {
85     jclass clazz = env->FindClass("com/android/compatibility/common/util/CpuFeatures");
86 
87     return env->RegisterNatives(clazz, gMethods,
88             sizeof(gMethods) / sizeof(JNINativeMethod));
89 }
90