1 /*
   2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3 * Copyright (c) 2020, Arm Ltd. All rights reserved.
   4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 *
   6 * This code is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU General Public License version 2 only, as
   8 * published by the Free Software Foundation.
   9 *
  10 * This code is distributed in the hope that it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13 * version 2 for more details (a copy is included in the LICENSE file that
  14 * accompanied this code).
  15 *
  16 * You should have received a copy of the GNU General Public License version
  17 * 2 along with this work; if not, write to the Free Software Foundation,
  18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19 *
  20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21 * or visit www.oracle.com if you need additional information or have any
  22 * questions.
  23 *
  24 */
  25 
  26 #ifdef __aarch64__
  27 
  28 #include <jni.h>
  29 #include <pthread.h>
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <sys/prctl.h>
  33 #include <unistd.h>
  34 
  35 #ifndef PR_SVE_GET_VL
  36 // For old toolchains which do not have SVE related macros defined.
  37 #define PR_SVE_SET_VL   50
  38 #define PR_SVE_GET_VL   51
  39 #endif
  40 
  41 int get_current_thread_vl() {
  42   return prctl(PR_SVE_GET_VL);
  43 }
  44 
  45 int set_current_thread_vl(unsigned long arg) {
  46   return prctl(PR_SVE_SET_VL, arg);
  47 }
  48 
  49 #ifdef __cplusplus
  50 extern "C" {
  51 #endif
  52 
  53 JNIEXPORT jint JNICALL Java_compiler_c2_aarch64_TestSVEWithJNI_setVectorLength
  54 (JNIEnv * env, jclass clz, jint length) {
  55   return set_current_thread_vl(length);
  56 }
  57 
  58 JNIEXPORT jint JNICALL Java_compiler_c2_aarch64_TestSVEWithJNI_getVectorLength
  59 (JNIEnv *env, jclass clz) {
  60   return get_current_thread_vl();
  61 }
  62 
  63 
  64 #ifdef __cplusplus
  65 }
  66 #endif
  67 
  68 #endif