xref: /openbmc/qemu/linux-user/aarch64/target_prctl.h (revision f2cb4026fccfe073f84a4b440e41d3ed0c3134f6)
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 
95a534314SPeter Maydell #include "target/arm/cpu-features.h"
10*bef6a77fSGustavo Romero #include "mte_user_helper.h"
115a534314SPeter Maydell 
do_prctl_sve_get_vl(CPUArchState * env)12fd72f5d0SRichard Henderson static abi_long do_prctl_sve_get_vl(CPUArchState *env)
1387e9bf23SRichard Henderson {
1487e9bf23SRichard Henderson     ARMCPU *cpu = env_archcpu(env);
1587e9bf23SRichard Henderson     if (cpu_isar_feature(aa64_sve, cpu)) {
1624d87c18SRichard Henderson         /* PSTATE.SM is always unset on syscall entry. */
178b599e5cSRichard Henderson         return sve_vq(env) * 16;
1887e9bf23SRichard Henderson     }
1987e9bf23SRichard Henderson     return -TARGET_EINVAL;
2087e9bf23SRichard Henderson }
21fd72f5d0SRichard Henderson #define do_prctl_sve_get_vl do_prctl_sve_get_vl
2287e9bf23SRichard Henderson 
do_prctl_sve_set_vl(CPUArchState * env,abi_long arg2)23fd72f5d0SRichard Henderson static abi_long do_prctl_sve_set_vl(CPUArchState *env, abi_long arg2)
2487e9bf23SRichard Henderson {
2587e9bf23SRichard Henderson     /*
2687e9bf23SRichard Henderson      * We cannot support either PR_SVE_SET_VL_ONEXEC or PR_SVE_VL_INHERIT.
2787e9bf23SRichard Henderson      * Note the kernel definition of sve_vl_valid allows for VQ=512,
2887e9bf23SRichard Henderson      * i.e. VL=8192, even though the current architectural maximum is VQ=16.
2987e9bf23SRichard Henderson      */
3087e9bf23SRichard Henderson     if (cpu_isar_feature(aa64_sve, env_archcpu(env))
3187e9bf23SRichard Henderson         && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) {
3287e9bf23SRichard Henderson         uint32_t vq, old_vq;
3387e9bf23SRichard Henderson 
3424d87c18SRichard Henderson         /* PSTATE.SM is always unset on syscall entry. */
358b599e5cSRichard Henderson         old_vq = sve_vq(env);
3687e9bf23SRichard Henderson 
378b599e5cSRichard Henderson         /*
388b599e5cSRichard Henderson          * Bound the value of arg2, so that we know that it fits into
398b599e5cSRichard Henderson          * the 4-bit field in ZCR_EL1.  Rely on the hflags rebuild to
408b599e5cSRichard Henderson          * sort out the length supported by the cpu.
418b599e5cSRichard Henderson          */
428b599e5cSRichard Henderson         vq = MAX(arg2 / 16, 1);
438b599e5cSRichard Henderson         vq = MIN(vq, ARM_MAX_VQ);
448b599e5cSRichard Henderson         env->vfp.zcr_el[1] = vq - 1;
458b599e5cSRichard Henderson         arm_rebuild_hflags(env);
468b599e5cSRichard Henderson 
478b599e5cSRichard Henderson         vq = sve_vq(env);
4887e9bf23SRichard Henderson         if (vq < old_vq) {
4987e9bf23SRichard Henderson             aarch64_sve_narrow_vq(env, vq);
5087e9bf23SRichard Henderson         }
5187e9bf23SRichard Henderson         return vq * 16;
5287e9bf23SRichard Henderson     }
5387e9bf23SRichard Henderson     return -TARGET_EINVAL;
5487e9bf23SRichard Henderson }
55fd72f5d0SRichard Henderson #define do_prctl_sve_set_vl do_prctl_sve_set_vl
5687e9bf23SRichard Henderson 
do_prctl_sme_get_vl(CPUArchState * env)5724d87c18SRichard Henderson static abi_long do_prctl_sme_get_vl(CPUArchState *env)
5824d87c18SRichard Henderson {
5924d87c18SRichard Henderson     ARMCPU *cpu = env_archcpu(env);
6024d87c18SRichard Henderson     if (cpu_isar_feature(aa64_sme, cpu)) {
6124d87c18SRichard Henderson         return sme_vq(env) * 16;
6224d87c18SRichard Henderson     }
6324d87c18SRichard Henderson     return -TARGET_EINVAL;
6424d87c18SRichard Henderson }
6524d87c18SRichard Henderson #define do_prctl_sme_get_vl do_prctl_sme_get_vl
6624d87c18SRichard Henderson 
do_prctl_sme_set_vl(CPUArchState * env,abi_long arg2)6724d87c18SRichard Henderson static abi_long do_prctl_sme_set_vl(CPUArchState *env, abi_long arg2)
6824d87c18SRichard Henderson {
6924d87c18SRichard Henderson     /*
7024d87c18SRichard Henderson      * We cannot support either PR_SME_SET_VL_ONEXEC or PR_SME_VL_INHERIT.
7124d87c18SRichard Henderson      * Note the kernel definition of sve_vl_valid allows for VQ=512,
7224d87c18SRichard Henderson      * i.e. VL=8192, even though the architectural maximum is VQ=16.
7324d87c18SRichard Henderson      */
7424d87c18SRichard Henderson     if (cpu_isar_feature(aa64_sme, env_archcpu(env))
7524d87c18SRichard Henderson         && arg2 >= 0 && arg2 <= 512 * 16 && !(arg2 & 15)) {
7624d87c18SRichard Henderson         int vq, old_vq;
7724d87c18SRichard Henderson 
7824d87c18SRichard Henderson         old_vq = sme_vq(env);
7924d87c18SRichard Henderson 
8024d87c18SRichard Henderson         /*
8124d87c18SRichard Henderson          * Bound the value of vq, so that we know that it fits into
8224d87c18SRichard Henderson          * the 4-bit field in SMCR_EL1.  Because PSTATE.SM is cleared
8324d87c18SRichard Henderson          * on syscall entry, we are not modifying the current SVE
8424d87c18SRichard Henderson          * vector length.
8524d87c18SRichard Henderson          */
8624d87c18SRichard Henderson         vq = MAX(arg2 / 16, 1);
8724d87c18SRichard Henderson         vq = MIN(vq, 16);
8824d87c18SRichard Henderson         env->vfp.smcr_el[1] =
8924d87c18SRichard Henderson             FIELD_DP64(env->vfp.smcr_el[1], SMCR, LEN, vq - 1);
9024d87c18SRichard Henderson 
9124d87c18SRichard Henderson         /* Delay rebuilding hflags until we know if ZA must change. */
9224d87c18SRichard Henderson         vq = sve_vqm1_for_el_sm(env, 0, true) + 1;
9324d87c18SRichard Henderson 
9424d87c18SRichard Henderson         if (vq != old_vq) {
9524d87c18SRichard Henderson             /*
9624d87c18SRichard Henderson              * PSTATE.ZA state is cleared on any change to SVL.
9724d87c18SRichard Henderson              * We need not call arm_rebuild_hflags because PSTATE.SM was
9824d87c18SRichard Henderson              * cleared on syscall entry, so this hasn't changed VL.
9924d87c18SRichard Henderson              */
10024d87c18SRichard Henderson             env->svcr = FIELD_DP64(env->svcr, SVCR, ZA, 0);
10124d87c18SRichard Henderson             arm_rebuild_hflags(env);
10224d87c18SRichard Henderson         }
10324d87c18SRichard Henderson         return vq * 16;
10424d87c18SRichard Henderson     }
10524d87c18SRichard Henderson     return -TARGET_EINVAL;
10624d87c18SRichard Henderson }
10724d87c18SRichard Henderson #define do_prctl_sme_set_vl do_prctl_sme_set_vl
10824d87c18SRichard Henderson 
do_prctl_reset_keys(CPUArchState * env,abi_long arg2)10987e9bf23SRichard Henderson static abi_long do_prctl_reset_keys(CPUArchState *env, abi_long arg2)
11087e9bf23SRichard Henderson {
11187e9bf23SRichard Henderson     ARMCPU *cpu = env_archcpu(env);
11287e9bf23SRichard Henderson 
11387e9bf23SRichard Henderson     if (cpu_isar_feature(aa64_pauth, cpu)) {
11487e9bf23SRichard Henderson         int all = (PR_PAC_APIAKEY | PR_PAC_APIBKEY |
11587e9bf23SRichard Henderson                    PR_PAC_APDAKEY | PR_PAC_APDBKEY | PR_PAC_APGAKEY);
11687e9bf23SRichard Henderson         int ret = 0;
11787e9bf23SRichard Henderson         Error *err = NULL;
11887e9bf23SRichard Henderson 
11987e9bf23SRichard Henderson         if (arg2 == 0) {
12087e9bf23SRichard Henderson             arg2 = all;
12187e9bf23SRichard Henderson         } else if (arg2 & ~all) {
12287e9bf23SRichard Henderson             return -TARGET_EINVAL;
12387e9bf23SRichard Henderson         }
12487e9bf23SRichard Henderson         if (arg2 & PR_PAC_APIAKEY) {
12587e9bf23SRichard Henderson             ret |= qemu_guest_getrandom(&env->keys.apia,
12687e9bf23SRichard Henderson                                         sizeof(ARMPACKey), &err);
12787e9bf23SRichard Henderson         }
12887e9bf23SRichard Henderson         if (arg2 & PR_PAC_APIBKEY) {
12987e9bf23SRichard Henderson             ret |= qemu_guest_getrandom(&env->keys.apib,
13087e9bf23SRichard Henderson                                         sizeof(ARMPACKey), &err);
13187e9bf23SRichard Henderson         }
13287e9bf23SRichard Henderson         if (arg2 & PR_PAC_APDAKEY) {
13387e9bf23SRichard Henderson             ret |= qemu_guest_getrandom(&env->keys.apda,
13487e9bf23SRichard Henderson                                         sizeof(ARMPACKey), &err);
13587e9bf23SRichard Henderson         }
13687e9bf23SRichard Henderson         if (arg2 & PR_PAC_APDBKEY) {
13787e9bf23SRichard Henderson             ret |= qemu_guest_getrandom(&env->keys.apdb,
13887e9bf23SRichard Henderson                                         sizeof(ARMPACKey), &err);
13987e9bf23SRichard Henderson         }
14087e9bf23SRichard Henderson         if (arg2 & PR_PAC_APGAKEY) {
14187e9bf23SRichard Henderson             ret |= qemu_guest_getrandom(&env->keys.apga,
14287e9bf23SRichard Henderson                                         sizeof(ARMPACKey), &err);
14387e9bf23SRichard Henderson         }
14487e9bf23SRichard Henderson         if (ret != 0) {
14587e9bf23SRichard Henderson             /*
14687e9bf23SRichard Henderson              * Some unknown failure in the crypto.  The best
14787e9bf23SRichard Henderson              * we can do is log it and fail the syscall.
14887e9bf23SRichard Henderson              * The real syscall cannot fail this way.
14987e9bf23SRichard Henderson              */
15087e9bf23SRichard Henderson             qemu_log_mask(LOG_UNIMP, "PR_PAC_RESET_KEYS: Crypto failure: %s",
15187e9bf23SRichard Henderson                           error_get_pretty(err));
15287e9bf23SRichard Henderson             error_free(err);
15387e9bf23SRichard Henderson             return -TARGET_EIO;
15487e9bf23SRichard Henderson         }
15587e9bf23SRichard Henderson         return 0;
15687e9bf23SRichard Henderson     }
15787e9bf23SRichard Henderson     return -TARGET_EINVAL;
15887e9bf23SRichard Henderson }
15987e9bf23SRichard Henderson #define do_prctl_reset_keys do_prctl_reset_keys
16087e9bf23SRichard Henderson 
do_prctl_set_tagged_addr_ctrl(CPUArchState * env,abi_long arg2)16187e9bf23SRichard Henderson static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2)
16287e9bf23SRichard Henderson {
16387e9bf23SRichard Henderson     abi_ulong valid_mask = PR_TAGGED_ADDR_ENABLE;
16487e9bf23SRichard Henderson     ARMCPU *cpu = env_archcpu(env);
16587e9bf23SRichard Henderson 
16687e9bf23SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
16787e9bf23SRichard Henderson         valid_mask |= PR_MTE_TCF_MASK;
16887e9bf23SRichard Henderson         valid_mask |= PR_MTE_TAG_MASK;
16987e9bf23SRichard Henderson     }
17087e9bf23SRichard Henderson 
17187e9bf23SRichard Henderson     if (arg2 & ~valid_mask) {
17287e9bf23SRichard Henderson         return -TARGET_EINVAL;
17387e9bf23SRichard Henderson     }
17487e9bf23SRichard Henderson     env->tagged_addr_enable = arg2 & PR_TAGGED_ADDR_ENABLE;
17587e9bf23SRichard Henderson 
17687e9bf23SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
177*bef6a77fSGustavo Romero         arm_set_mte_tcf0(env, arg2);
17887e9bf23SRichard Henderson 
17987e9bf23SRichard Henderson         /*
18087e9bf23SRichard Henderson          * Write PR_MTE_TAG to GCR_EL1[Exclude].
18187e9bf23SRichard Henderson          * Note that the syscall uses an include mask,
18287e9bf23SRichard Henderson          * and hardware uses an exclude mask -- invert.
18387e9bf23SRichard Henderson          */
18487e9bf23SRichard Henderson         env->cp15.gcr_el1 =
18587e9bf23SRichard Henderson             deposit64(env->cp15.gcr_el1, 0, 16, ~arg2 >> PR_MTE_TAG_SHIFT);
18687e9bf23SRichard Henderson         arm_rebuild_hflags(env);
18787e9bf23SRichard Henderson     }
18887e9bf23SRichard Henderson     return 0;
18987e9bf23SRichard Henderson }
19087e9bf23SRichard Henderson #define do_prctl_set_tagged_addr_ctrl do_prctl_set_tagged_addr_ctrl
19187e9bf23SRichard Henderson 
do_prctl_get_tagged_addr_ctrl(CPUArchState * env)19287e9bf23SRichard Henderson static abi_long do_prctl_get_tagged_addr_ctrl(CPUArchState *env)
19387e9bf23SRichard Henderson {
19487e9bf23SRichard Henderson     ARMCPU *cpu = env_archcpu(env);
19587e9bf23SRichard Henderson     abi_long ret = 0;
19687e9bf23SRichard Henderson 
19787e9bf23SRichard Henderson     if (env->tagged_addr_enable) {
19887e9bf23SRichard Henderson         ret |= PR_TAGGED_ADDR_ENABLE;
19987e9bf23SRichard Henderson     }
20087e9bf23SRichard Henderson     if (cpu_isar_feature(aa64_mte, cpu)) {
20187e9bf23SRichard Henderson         /* See do_prctl_set_tagged_addr_ctrl. */
20287e9bf23SRichard Henderson         ret |= extract64(env->cp15.sctlr_el[1], 38, 2) << PR_MTE_TCF_SHIFT;
20387e9bf23SRichard Henderson         ret = deposit64(ret, PR_MTE_TAG_SHIFT, 16, ~env->cp15.gcr_el1);
20487e9bf23SRichard Henderson     }
20587e9bf23SRichard Henderson     return ret;
20687e9bf23SRichard Henderson }
20787e9bf23SRichard Henderson #define do_prctl_get_tagged_addr_ctrl do_prctl_get_tagged_addr_ctrl
20887e9bf23SRichard Henderson 
20987e9bf23SRichard Henderson #endif /* AARCH64_TARGET_PRCTL_H */
210