1 /*
2  *  Copyright(c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Test the VLIW semantics of exceptions with mem_noshuf
20  *
21  * When a packet has the :mem_noshuf attribute, the semantics dictate
22  * That the load will get the data from the store if the addresses overlap.
23  * To accomplish this, we perform the store first.  However, we have to
24  * handle the case where the store raises an exception.  In that case, the
25  * store should not alter the machine state.
26  *
27  * We test this with a mem_noshuf packet with a store to a global variable,
28  * "should_not_change" and a load from NULL.  After the SIGSEGV is caught,
29  * we check * that the "should_not_change" value is the same.
30  *
31  * We also check that a predicated load where the predicate is false doesn't
32  * raise an exception and allows the store to happen.
33  */
34 
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <setjmp.h>
43 #include <signal.h>
44 
45 int err;
46 
47 #include "hex_test.h"
48 
49 bool segv_caught;
50 
51 #define SHOULD_NOT_CHANGE_VAL        5
52 int32_t should_not_change = SHOULD_NOT_CHANGE_VAL;
53 
54 #define OK_TO_CHANGE_VAL        13
55 int32_t ok_to_change = OK_TO_CHANGE_VAL;
56 
57 jmp_buf jmp_env;
58 
59 static void sig_segv(int sig, siginfo_t *info, void *puc)
60 {
61     check32(sig, SIGSEGV);
62     segv_caught = true;
63     longjmp(jmp_env, 1);
64 }
65 
66 int main()
67 {
68     struct sigaction act;
69     int32_t dummy32;
70     int64_t dummy64;
71     void *p;
72 
73     /* SIGSEGV test */
74     act.sa_sigaction = sig_segv;
75     sigemptyset(&act.sa_mask);
76     act.sa_flags = SA_SIGINFO;
77     chk_error(sigaction(SIGSEGV, &act, NULL));
78     if (setjmp(jmp_env) == 0) {
79         asm volatile("r18 = ##should_not_change\n\t"
80                      "r19 = #0\n\t"
81                      "{\n\t"
82                      "    memw(r18) = #7\n\t"
83                      "    %0 = memw(r19)\n\t"
84                      "}:mem_noshuf\n\t"
85                       : "=r"(dummy32) : : "r18", "r19", "memory");
86     }
87 
88     act.sa_handler = SIG_DFL;
89     sigemptyset(&act.sa_mask);
90     act.sa_flags = 0;
91     chk_error(sigaction(SIGSEGV, &act, NULL));
92 
93     check32(segv_caught, true);
94     check32(should_not_change, SHOULD_NOT_CHANGE_VAL);
95 
96     /*
97      * Check that a predicated load where the predicate is false doesn't
98      * raise an exception and allows the store to happen.
99      */
100     asm volatile("r18 = ##ok_to_change\n\t"
101                  "r19 = #0\n\t"
102                  "p0 = cmp.gt(r0, r0)\n\t"
103                  "{\n\t"
104                  "    memw(r18) = #7\n\t"
105                  "    if (p0) %0 = memw(r19)\n\t"
106                  "}:mem_noshuf\n\t"
107                   : "=r"(dummy32) : : "r18", "r19", "p0", "memory");
108 
109     check32(ok_to_change, 7);
110 
111     /*
112      * Also check that the post-increment doesn't happen when the
113      * predicate is false.
114      */
115     ok_to_change = OK_TO_CHANGE_VAL;
116     p = NULL;
117     asm volatile("r18 = ##ok_to_change\n\t"
118                  "p0 = cmp.gt(r0, r0)\n\t"
119                  "{\n\t"
120                  "    memw(r18) = #9\n\t"
121                  "    if (p0) %1 = memd(%0 ++ #8)\n\t"
122                  "}:mem_noshuf\n\t"
123                   : "+r"(p), "=r"(dummy64) : : "r18", "p0", "memory");
124 
125     check32(ok_to_change, 9);
126     check32((int)p, (int)NULL);
127 
128     puts(err ? "FAIL" : "PASS");
129     return err ? EXIT_FAILURE : EXIT_SUCCESS;
130 }
131