xref: /openbmc/qemu/tests/tcg/hexagon/hex_sigsegv.c (revision 2e1cacfb)
1 /*
2  *  Copyright(c) 2021-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 two stores in a packet
20  *
21  * When a packet has 2 stores, either both commit or neither commit.
22  * We test this with a packet that does stores to both NULL and a global
23  * variable, "should_not_change".  After the SIGSEGV is caught, we check
24  * that the "should_not_change" value is the same.
25  */
26 
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <setjmp.h>
35 #include <signal.h>
36 
37 int err;
38 
39 #include "hex_test.h"
40 
41 bool segv_caught;
42 
43 #define SHOULD_NOT_CHANGE_VAL        5
44 int32_t should_not_change = SHOULD_NOT_CHANGE_VAL;
45 
46 #define BUF_SIZE        300
47 uint8_t buf[BUF_SIZE];
48 jmp_buf jmp_env;
49 
50 static void sig_segv(int sig, siginfo_t *info, void *puc)
51 {
52     check32(sig, SIGSEGV);
53     segv_caught = true;
54     longjmp(jmp_env, 1);
55 }
56 
57 int main()
58 {
59     struct sigaction act;
60 
61     /* SIGSEGV test */
62     act.sa_sigaction = sig_segv;
63     sigemptyset(&act.sa_mask);
64     act.sa_flags = SA_SIGINFO;
65     chk_error(sigaction(SIGSEGV, &act, NULL));
66     if (setjmp(jmp_env) == 0) {
67         asm volatile("r18 = ##should_not_change\n\t"
68                      "r19 = #0\n\t"
69                      "{\n\t"
70                      "    memw(r18) = #7\n\t"
71                      "    memw(r19) = #0\n\t"
72                      "}\n\t"
73                       : : : "r18", "r19", "memory");
74     }
75 
76     act.sa_handler = SIG_DFL;
77     sigemptyset(&act.sa_mask);
78     act.sa_flags = 0;
79     chk_error(sigaction(SIGSEGV, &act, NULL));
80 
81     check32(segv_caught, true);
82     check32(should_not_change, SHOULD_NOT_CHANGE_VAL);
83 
84     puts(err ? "FAIL" : "PASS");
85     return err ? EXIT_FAILURE : EXIT_SUCCESS;
86 }
87