187e9bf23SRichard Henderson /* 287e9bf23SRichard Henderson * AArch64 specific prctl functions for linux-user 387e9bf23SRichard Henderson * 487e9bf23SRichard Henderson * SPDX-License-Identifier: GPL-2.0-or-later 587e9bf23SRichard Henderson */ 687e9bf23SRichard Henderson #ifndef AARCH64_TARGET_PRCTL_H 787e9bf23SRichard Henderson #define AARCH64_TARGET_PRCTL_H 887e9bf23SRichard Henderson 9fd72f5d0SRichard Henderson static abi_long do_prctl_sve_get_vl(CPUArchState *env) 1087e9bf23SRichard Henderson { 1187e9bf23SRichard Henderson ARMCPU *cpu = env_archcpu(env); 1287e9bf23SRichard Henderson if (cpu_isar_feature(aa64_sve, cpu)) { 13*24d87c18SRichard Henderson /* PSTATE.SM is always unset on syscall entry. */ 148b599e5cSRichard Henderson return sve_vq(env) * 16; 1587e9bf23SRichard Henderson } 1687e9bf23SRichard Henderson return -TARGET_EINVAL; 1787e9bf23SRichard Henderson } 18fd72f5d0SRichard Henderson #define do_prctl_sve_get_vl do_prctl_sve_get_vl 1987e9bf23SRichard Henderson 20fd72f5d0SRichard Henderson static abi_long do_prctl_sve_set_vl(CPUArchState *env, abi_long arg2) 2187e9bf23SRichard Henderson { 2287e9bf23SRichard Henderson /* 2387e9bf23SRichard Henderson * We cannot support either PR_SVE_SET_VL_ONEXEC or PR_SVE_VL_INHERIT. 2487e9bf23SRichard Henderson * Note the kernel definition of sve_vl_valid allows for VQ=512, 2587e9bf23SRichard Henderson * i.e. VL=8192, even though the current architectural maximum is VQ=16. 2687e9bf23SRichard Henderson */ 2787e9bf23SRichard Henderson if (cpu_isar_feature(aa64_sve, env_archcpu(env)) 2887e9bf23SRichard Henderson && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) { 2987e9bf23SRichard Henderson uint32_t vq, old_vq; 3087e9bf23SRichard Henderson 31*24d87c18SRichard Henderson /* PSTATE.SM is always unset on syscall entry. */ 328b599e5cSRichard Henderson old_vq = sve_vq(env); 3387e9bf23SRichard Henderson 348b599e5cSRichard Henderson /* 358b599e5cSRichard Henderson * Bound the value of arg2, so that we know that it fits into 368b599e5cSRichard Henderson * the 4-bit field in ZCR_EL1. Rely on the hflags rebuild to 378b599e5cSRichard Henderson * sort out the length supported by the cpu. 388b599e5cSRichard Henderson */ 398b599e5cSRichard Henderson vq = MAX(arg2 / 16, 1); 408b599e5cSRichard Henderson vq = MIN(vq, ARM_MAX_VQ); 418b599e5cSRichard Henderson env->vfp.zcr_el[1] = vq - 1; 428b599e5cSRichard Henderson arm_rebuild_hflags(env); 438b599e5cSRichard Henderson 448b599e5cSRichard Henderson vq = sve_vq(env); 4587e9bf23SRichard Henderson if (vq < old_vq) { 4687e9bf23SRichard Henderson aarch64_sve_narrow_vq(env, vq); 4787e9bf23SRichard Henderson } 4887e9bf23SRichard Henderson return vq * 16; 4987e9bf23SRichard Henderson } 5087e9bf23SRichard Henderson return -TARGET_EINVAL; 5187e9bf23SRichard Henderson } 52fd72f5d0SRichard Henderson #define do_prctl_sve_set_vl do_prctl_sve_set_vl 5387e9bf23SRichard Henderson 54*24d87c18SRichard Henderson static abi_long do_prctl_sme_get_vl(CPUArchState *env) 55*24d87c18SRichard Henderson { 56*24d87c18SRichard Henderson ARMCPU *cpu = env_archcpu(env); 57*24d87c18SRichard Henderson if (cpu_isar_feature(aa64_sme, cpu)) { 58*24d87c18SRichard Henderson return sme_vq(env) * 16; 59*24d87c18SRichard Henderson } 60*24d87c18SRichard Henderson return -TARGET_EINVAL; 61*24d87c18SRichard Henderson } 62*24d87c18SRichard Henderson #define do_prctl_sme_get_vl do_prctl_sme_get_vl 63*24d87c18SRichard Henderson 64*24d87c18SRichard Henderson static abi_long do_prctl_sme_set_vl(CPUArchState *env, abi_long arg2) 65*24d87c18SRichard Henderson { 66*24d87c18SRichard Henderson /* 67*24d87c18SRichard Henderson * We cannot support either PR_SME_SET_VL_ONEXEC or PR_SME_VL_INHERIT. 68*24d87c18SRichard Henderson * Note the kernel definition of sve_vl_valid allows for VQ=512, 69*24d87c18SRichard Henderson * i.e. VL=8192, even though the architectural maximum is VQ=16. 70*24d87c18SRichard Henderson */ 71*24d87c18SRichard Henderson if (cpu_isar_feature(aa64_sme, env_archcpu(env)) 72*24d87c18SRichard Henderson && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) { 73*24d87c18SRichard Henderson int vq, old_vq; 74*24d87c18SRichard Henderson 75*24d87c18SRichard Henderson old_vq = sme_vq(env); 76*24d87c18SRichard Henderson 77*24d87c18SRichard Henderson /* 78*24d87c18SRichard Henderson * Bound the value of vq, so that we know that it fits into 79*24d87c18SRichard Henderson * the 4-bit field in SMCR_EL1. Because PSTATE.SM is cleared 80*24d87c18SRichard Henderson * on syscall entry, we are not modifying the current SVE 81*24d87c18SRichard Henderson * vector length. 82*24d87c18SRichard Henderson */ 83*24d87c18SRichard Henderson vq = MAX(arg2 / 16, 1); 84*24d87c18SRichard Henderson vq = MIN(vq, 16); 85*24d87c18SRichard Henderson env->vfp.smcr_el[1] = 86*24d87c18SRichard Henderson FIELD_DP64(env->vfp.smcr_el[1], SMCR, LEN, vq - 1); 87*24d87c18SRichard Henderson 88*24d87c18SRichard Henderson /* Delay rebuilding hflags until we know if ZA must change. */ 89*24d87c18SRichard Henderson vq = sve_vqm1_for_el_sm(env, 0, true) + 1; 90*24d87c18SRichard Henderson 91*24d87c18SRichard Henderson if (vq != old_vq) { 92*24d87c18SRichard Henderson /* 93*24d87c18SRichard Henderson * PSTATE.ZA state is cleared on any change to SVL. 94*24d87c18SRichard Henderson * We need not call arm_rebuild_hflags because PSTATE.SM was 95*24d87c18SRichard Henderson * cleared on syscall entry, so this hasn't changed VL. 96*24d87c18SRichard Henderson */ 97*24d87c18SRichard Henderson env->svcr = FIELD_DP64(env->svcr, SVCR, ZA, 0); 98*24d87c18SRichard Henderson arm_rebuild_hflags(env); 99*24d87c18SRichard Henderson } 100*24d87c18SRichard Henderson return vq * 16; 101*24d87c18SRichard Henderson } 102*24d87c18SRichard Henderson return -TARGET_EINVAL; 103*24d87c18SRichard Henderson } 104*24d87c18SRichard Henderson #define do_prctl_sme_set_vl do_prctl_sme_set_vl 105*24d87c18SRichard Henderson 10687e9bf23SRichard Henderson static abi_long do_prctl_reset_keys(CPUArchState *env, abi_long arg2) 10787e9bf23SRichard Henderson { 10887e9bf23SRichard Henderson ARMCPU *cpu = env_archcpu(env); 10987e9bf23SRichard Henderson 11087e9bf23SRichard Henderson if (cpu_isar_feature(aa64_pauth, cpu)) { 11187e9bf23SRichard Henderson int all = (PR_PAC_APIAKEY | PR_PAC_APIBKEY | 11287e9bf23SRichard Henderson PR_PAC_APDAKEY | PR_PAC_APDBKEY | PR_PAC_APGAKEY); 11387e9bf23SRichard Henderson int ret = 0; 11487e9bf23SRichard Henderson Error *err = NULL; 11587e9bf23SRichard Henderson 11687e9bf23SRichard Henderson if (arg2 == 0) { 11787e9bf23SRichard Henderson arg2 = all; 11887e9bf23SRichard Henderson } else if (arg2 & ~all) { 11987e9bf23SRichard Henderson return -TARGET_EINVAL; 12087e9bf23SRichard Henderson } 12187e9bf23SRichard Henderson if (arg2 & PR_PAC_APIAKEY) { 12287e9bf23SRichard Henderson ret |= qemu_guest_getrandom(&env->keys.apia, 12387e9bf23SRichard Henderson sizeof(ARMPACKey), &err); 12487e9bf23SRichard Henderson } 12587e9bf23SRichard Henderson if (arg2 & PR_PAC_APIBKEY) { 12687e9bf23SRichard Henderson ret |= qemu_guest_getrandom(&env->keys.apib, 12787e9bf23SRichard Henderson sizeof(ARMPACKey), &err); 12887e9bf23SRichard Henderson } 12987e9bf23SRichard Henderson if (arg2 & PR_PAC_APDAKEY) { 13087e9bf23SRichard Henderson ret |= qemu_guest_getrandom(&env->keys.apda, 13187e9bf23SRichard Henderson sizeof(ARMPACKey), &err); 13287e9bf23SRichard Henderson } 13387e9bf23SRichard Henderson if (arg2 & PR_PAC_APDBKEY) { 13487e9bf23SRichard Henderson ret |= qemu_guest_getrandom(&env->keys.apdb, 13587e9bf23SRichard Henderson sizeof(ARMPACKey), &err); 13687e9bf23SRichard Henderson } 13787e9bf23SRichard Henderson if (arg2 & PR_PAC_APGAKEY) { 13887e9bf23SRichard Henderson ret |= qemu_guest_getrandom(&env->keys.apga, 13987e9bf23SRichard Henderson sizeof(ARMPACKey), &err); 14087e9bf23SRichard Henderson } 14187e9bf23SRichard Henderson if (ret != 0) { 14287e9bf23SRichard Henderson /* 14387e9bf23SRichard Henderson * Some unknown failure in the crypto. The best 14487e9bf23SRichard Henderson * we can do is log it and fail the syscall. 14587e9bf23SRichard Henderson * The real syscall cannot fail this way. 14687e9bf23SRichard Henderson */ 14787e9bf23SRichard Henderson qemu_log_mask(LOG_UNIMP, "PR_PAC_RESET_KEYS: Crypto failure: %s", 14887e9bf23SRichard Henderson error_get_pretty(err)); 14987e9bf23SRichard Henderson error_free(err); 15087e9bf23SRichard Henderson return -TARGET_EIO; 15187e9bf23SRichard Henderson } 15287e9bf23SRichard Henderson return 0; 15387e9bf23SRichard Henderson } 15487e9bf23SRichard Henderson return -TARGET_EINVAL; 15587e9bf23SRichard Henderson } 15687e9bf23SRichard Henderson #define do_prctl_reset_keys do_prctl_reset_keys 15787e9bf23SRichard Henderson 15887e9bf23SRichard Henderson static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2) 15987e9bf23SRichard Henderson { 16087e9bf23SRichard Henderson abi_ulong valid_mask = PR_TAGGED_ADDR_ENABLE; 16187e9bf23SRichard Henderson ARMCPU *cpu = env_archcpu(env); 16287e9bf23SRichard Henderson 16387e9bf23SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 16487e9bf23SRichard Henderson valid_mask |= PR_MTE_TCF_MASK; 16587e9bf23SRichard Henderson valid_mask |= PR_MTE_TAG_MASK; 16687e9bf23SRichard Henderson } 16787e9bf23SRichard Henderson 16887e9bf23SRichard Henderson if (arg2 & ~valid_mask) { 16987e9bf23SRichard Henderson return -TARGET_EINVAL; 17087e9bf23SRichard Henderson } 17187e9bf23SRichard Henderson env->tagged_addr_enable = arg2 & PR_TAGGED_ADDR_ENABLE; 17287e9bf23SRichard Henderson 17387e9bf23SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 17487e9bf23SRichard Henderson switch (arg2 & PR_MTE_TCF_MASK) { 17587e9bf23SRichard Henderson case PR_MTE_TCF_NONE: 17687e9bf23SRichard Henderson case PR_MTE_TCF_SYNC: 17787e9bf23SRichard Henderson case PR_MTE_TCF_ASYNC: 17887e9bf23SRichard Henderson break; 17987e9bf23SRichard Henderson default: 18087e9bf23SRichard Henderson return -EINVAL; 18187e9bf23SRichard Henderson } 18287e9bf23SRichard Henderson 18387e9bf23SRichard Henderson /* 18487e9bf23SRichard Henderson * Write PR_MTE_TCF to SCTLR_EL1[TCF0]. 18587e9bf23SRichard Henderson * Note that the syscall values are consistent with hw. 18687e9bf23SRichard Henderson */ 18787e9bf23SRichard Henderson env->cp15.sctlr_el[1] = 18887e9bf23SRichard Henderson deposit64(env->cp15.sctlr_el[1], 38, 2, arg2 >> PR_MTE_TCF_SHIFT); 18987e9bf23SRichard Henderson 19087e9bf23SRichard Henderson /* 19187e9bf23SRichard Henderson * Write PR_MTE_TAG to GCR_EL1[Exclude]. 19287e9bf23SRichard Henderson * Note that the syscall uses an include mask, 19387e9bf23SRichard Henderson * and hardware uses an exclude mask -- invert. 19487e9bf23SRichard Henderson */ 19587e9bf23SRichard Henderson env->cp15.gcr_el1 = 19687e9bf23SRichard Henderson deposit64(env->cp15.gcr_el1, 0, 16, ~arg2 >> PR_MTE_TAG_SHIFT); 19787e9bf23SRichard Henderson arm_rebuild_hflags(env); 19887e9bf23SRichard Henderson } 19987e9bf23SRichard Henderson return 0; 20087e9bf23SRichard Henderson } 20187e9bf23SRichard Henderson #define do_prctl_set_tagged_addr_ctrl do_prctl_set_tagged_addr_ctrl 20287e9bf23SRichard Henderson 20387e9bf23SRichard Henderson static abi_long do_prctl_get_tagged_addr_ctrl(CPUArchState *env) 20487e9bf23SRichard Henderson { 20587e9bf23SRichard Henderson ARMCPU *cpu = env_archcpu(env); 20687e9bf23SRichard Henderson abi_long ret = 0; 20787e9bf23SRichard Henderson 20887e9bf23SRichard Henderson if (env->tagged_addr_enable) { 20987e9bf23SRichard Henderson ret |= PR_TAGGED_ADDR_ENABLE; 21087e9bf23SRichard Henderson } 21187e9bf23SRichard Henderson if (cpu_isar_feature(aa64_mte, cpu)) { 21287e9bf23SRichard Henderson /* See do_prctl_set_tagged_addr_ctrl. */ 21387e9bf23SRichard Henderson ret |= extract64(env->cp15.sctlr_el[1], 38, 2) << PR_MTE_TCF_SHIFT; 21487e9bf23SRichard Henderson ret = deposit64(ret, PR_MTE_TAG_SHIFT, 16, ~env->cp15.gcr_el1); 21587e9bf23SRichard Henderson } 21687e9bf23SRichard Henderson return ret; 21787e9bf23SRichard Henderson } 21887e9bf23SRichard Henderson #define do_prctl_get_tagged_addr_ctrl do_prctl_get_tagged_addr_ctrl 21987e9bf23SRichard Henderson 22087e9bf23SRichard Henderson #endif /* AARCH64_TARGET_PRCTL_H */ 221