Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / tests / tcg / aarch64 / pauth-2.c
blob89ffdbf1df74a46fce535805f5bc8b3a730806ef
1 #include <stdint.h>
2 #include <signal.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include "pauth.h"
8 static void sigill(int sig, siginfo_t *info, void *vuc)
10 ucontext_t *uc = vuc;
11 uint64_t test;
13 /* There is only one insn below that is allowed to fault. */
14 asm volatile("adr %0, auth2_insn" : "=r"(test));
15 assert(test == uc->uc_mcontext.pc);
16 exit(0);
19 static int pac_feature;
21 void do_test(uint64_t value)
23 uint64_t salt1, salt2;
24 uint64_t encode, decode;
27 * With TBI enabled and a 48-bit VA, there are 7 bits of auth,
28 * and so a 1/128 chance of encode = pac(value,key,salt) producing
29 * an auth for which leaves value unchanged.
30 * Iterate until we find a salt for which encode != value.
32 for (salt1 = 1; ; salt1++) {
33 asm volatile("pacda %0, %2" : "=r"(encode) : "0"(value), "r"(salt1));
34 if (encode != value) {
35 break;
39 /* A valid salt must produce a valid authorization. */
40 asm volatile("autda %0, %2" : "=r"(decode) : "0"(encode), "r"(salt1));
41 assert(decode == value);
44 * An invalid salt usually fails authorization, but again there
45 * is a chance of choosing another salt that works.
46 * Iterate until we find another salt which does fail.
48 * With FEAT_FPAC, this will SIGILL instead of producing a result.
50 for (salt2 = salt1 + 1; ; salt2++) {
51 asm volatile("auth2_insn: autda %0, %2"
52 : "=r"(decode) : "0"(encode), "r"(salt2));
53 if (decode != value) {
54 break;
58 assert(pac_feature < 4); /* No FEAT_FPAC */
60 /* The VA bits, bit 55, and the TBI bits, should be unchanged. */
61 assert(((decode ^ value) & 0xff80ffffffffffffull) == 0);
64 * Without FEAT_Pauth2, bits [54:53] are an error indicator based on
65 * the key used; the DA key above is keynumber 0, so error == 0b01.
66 * Otherwise, bit 55 of the original is sign-extended into the rest
67 * of the auth.
69 if (pac_feature < 3) {
70 if ((value >> 55) & 1) {
71 assert(((decode >> 48) & 0xff) == 0b10111111);
72 } else {
73 assert(((decode >> 48) & 0xff) == 0b00100000);
78 int main()
80 static const struct sigaction sa = {
81 .sa_sigaction = sigill,
82 .sa_flags = SA_SIGINFO
85 pac_feature = get_pac_feature();
86 assert(pac_feature != 0);
88 if (pac_feature >= 4) {
89 /* FEAT_FPAC */
90 sigaction(SIGILL, &sa, NULL);
93 do_test(0);
94 do_test(0xda004acedeadbeefull);
95 return 0;