1 /*
2  *  Copyright(c) 2022 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 <unistd.h>
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <setjmp.h>
41 #include <signal.h>
42 
43 int err;
44 int segv_caught;
45 
46 #define SHOULD_NOT_CHANGE_VAL        5
47 int should_not_change = SHOULD_NOT_CHANGE_VAL;
48 
49 #define OK_TO_CHANGE_VAL        13
50 int ok_to_change = OK_TO_CHANGE_VAL;
51 
52 static void __check(const char *filename, int line, int x, int expect)
53 {
54     if (x != expect) {
55         printf("ERROR %s:%d - %d != %d\n",
56                filename, line, x, expect);
57         err++;
58     }
59 }
60 
61 #define check(x, expect) __check(__FILE__, __LINE__, (x), (expect))
62 
63 static void __chk_error(const char *filename, int line, int ret)
64 {
65     if (ret < 0) {
66         printf("ERROR %s:%d - %d\n", filename, line, ret);
67         err++;
68     }
69 }
70 
71 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
72 
73 jmp_buf jmp_env;
74 
75 static void sig_segv(int sig, siginfo_t *info, void *puc)
76 {
77     check(sig, SIGSEGV);
78     segv_caught = 1;
79     longjmp(jmp_env, 1);
80 }
81 
82 int main()
83 {
84     struct sigaction act;
85     int dummy32;
86     long long dummy64;
87     void *p;
88 
89     /* SIGSEGV test */
90     act.sa_sigaction = sig_segv;
91     sigemptyset(&act.sa_mask);
92     act.sa_flags = SA_SIGINFO;
93     chk_error(sigaction(SIGSEGV, &act, NULL));
94     if (setjmp(jmp_env) == 0) {
95         asm volatile("r18 = ##should_not_change\n\t"
96                      "r19 = #0\n\t"
97                      "{\n\t"
98                      "    memw(r18) = #7\n\t"
99                      "    %0 = memw(r19)\n\t"
100                      "}:mem_noshuf\n\t"
101                       : "=r"(dummy32) : : "r18", "r19", "memory");
102     }
103 
104     act.sa_handler = SIG_DFL;
105     sigemptyset(&act.sa_mask);
106     act.sa_flags = 0;
107     chk_error(sigaction(SIGSEGV, &act, NULL));
108 
109     check(segv_caught, 1);
110     check(should_not_change, SHOULD_NOT_CHANGE_VAL);
111 
112     /*
113      * Check that a predicated load where the predicate is false doesn't
114      * raise an exception and allows the store to happen.
115      */
116     asm volatile("r18 = ##ok_to_change\n\t"
117                  "r19 = #0\n\t"
118                  "p0 = cmp.gt(r0, r0)\n\t"
119                  "{\n\t"
120                  "    memw(r18) = #7\n\t"
121                  "    if (p0) %0 = memw(r19)\n\t"
122                  "}:mem_noshuf\n\t"
123                   : "=r"(dummy32) : : "r18", "r19", "p0", "memory");
124 
125     check(ok_to_change, 7);
126 
127     /*
128      * Also check that the post-increment doesn't happen when the
129      * predicate is false.
130      */
131     ok_to_change = OK_TO_CHANGE_VAL;
132     p = NULL;
133     asm volatile("r18 = ##ok_to_change\n\t"
134                  "p0 = cmp.gt(r0, r0)\n\t"
135                  "{\n\t"
136                  "    memw(r18) = #9\n\t"
137                  "    if (p0) %1 = memd(%0 ++ #8)\n\t"
138                  "}:mem_noshuf\n\t"
139                   : "+r"(p), "=r"(dummy64) : : "r18", "p0", "memory");
140 
141     check(ok_to_change, 9);
142     check((int)p, (int)NULL);
143 
144     puts(err ? "FAIL" : "PASS");
145     return err ? EXIT_FAILURE : EXIT_SUCCESS;
146 }
147