Merge tag 'qemu-macppc-20230206' of https://github.com/mcayland/qemu into staging
[qemu.git] / tests / tcg / s390x / trap.c
blobd4c61c7f520b6f00cd8c55847d7694372b402b95
1 /*
2 * Copyright 2021 IBM Corp.
4 * This work is licensed under the terms of the GNU GPL, version 2 or (at
5 * your option) any later version. See the COPYING file in the top-level
6 * directory.
7 */
9 #include <stdarg.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <signal.h>
18 static void error1(const char *filename, int line, const char *fmt, ...)
20 va_list ap;
21 va_start(ap, fmt);
22 fprintf(stderr, "%s:%d: ", filename, line);
23 vfprintf(stderr, fmt, ap);
24 fprintf(stderr, "\n");
25 va_end(ap);
26 exit(1);
29 static int __chk_error(const char *filename, int line, int ret)
31 if (ret < 0) {
32 error1(filename, line, "%m (ret=%d, errno=%d/%s)",
33 ret, errno, strerror(errno));
35 return ret;
38 #define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
40 #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
42 int sigfpe_count;
43 int sigill_count;
45 static void sig_handler(int sig, siginfo_t *si, void *puc)
47 if (sig == SIGFPE) {
48 if (si->si_code != 0) {
49 error("unexpected si_code: 0x%x != 0", si->si_code);
51 ++sigfpe_count;
52 return;
55 if (sig == SIGILL) {
56 ++sigill_count;
57 return;
60 error("unexpected signal 0x%x\n", sig);
63 int main(int argc, char **argv)
65 sigfpe_count = sigill_count = 0;
67 struct sigaction act;
69 /* Set up SIG handler */
70 act.sa_sigaction = sig_handler;
71 sigemptyset(&act.sa_mask);
72 act.sa_flags = SA_SIGINFO;
73 chk_error(sigaction(SIGFPE, &act, NULL));
74 chk_error(sigaction(SIGILL, &act, NULL));
76 uint64_t z = 0x0ull;
77 uint64_t lz = 0xffffffffffffffffull;
78 asm volatile (
79 "lg %%r13,%[lz]\n"
80 "cgitne %%r13,0\n" /* SIGFPE */
81 "lg %%r13,%[z]\n"
82 "cgitne %%r13,0\n" /* no trap */
83 "nopr\n"
84 "lg %%r13,%[lz]\n"
85 "citne %%r13,0\n" /* SIGFPE */
86 "lg %%r13,%[z]\n"
87 "citne %%r13,0\n" /* no trap */
88 "nopr\n"
90 : [z] "m" (z), [lz] "m" (lz)
91 : "memory", "r13");
93 if (sigfpe_count != 2) {
94 error("unexpected SIGFPE count: %d != 2", sigfpe_count);
96 if (sigill_count != 0) {
97 error("unexpected SIGILL count: %d != 0", sigill_count);
100 printf("PASS\n");
101 return 0;