• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* x86 variant of the amd64-solaris/context_fpu.c test. */
2 
3 #include <assert.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <sys/syscall.h>
9 #include <sys/ucontext.h>
10 
11 static siginfo_t si;
12 static ucontext_t uc;
13 static float inhandler[8];
14 
sighandler(int sig,siginfo_t * sip,void * arg)15 static void sighandler(int sig, siginfo_t *sip, void *arg)
16 {
17    int i;
18    ucontext_t *ucp = (ucontext_t *) arg;
19 
20    si = *sip;
21    uc = *ucp;
22 
23    /* Reset the FP stack so it's possible to push other values onto it.  (It
24       is fully filled in main() before triggering the signal handler).  Note
25       that VEX also clears all FP values when the finit instruction is
26       executed.  This provides another level of validation that the restore
27       code is correct. */
28    __asm__ __volatile__(
29       "finit\n");
30 
31    /* Convert 80b values in mcontext to 32b values in the inhandler array. */
32    for (i = 0; i < 8; i++) {
33       __asm__ __volatile__(
34          "fldt   %[in]\n"
35          "fstps  %[out]\n"
36          : [out] "=m" (inhandler[i])
37          : [in] "m" (*((char*)&ucp->uc_mcontext.fpregs.fp_reg_set.fpchip_state
38                        + 28 + i * 10)));
39    }
40 }
41 
main(void)42 int main(void)
43 {
44    struct sigaction sa;
45    pid_t pid;
46    float out[8];
47    float x0;
48 
49    /* Uninitialised, but we know px[0] is 0x0. */
50    float *px = malloc(sizeof(*px));
51    x0 = px[0];
52 
53    sa.sa_sigaction = sighandler;
54    sa.sa_flags = SA_SIGINFO;
55    if (sigfillset(&sa.sa_mask)) {
56       perror("sigfillset");
57       return 1;
58    }
59    if (sigaction(SIGUSR1, &sa, NULL)) {
60       perror("sigaction");
61       return 1;
62    }
63 
64    pid = getpid();
65 
66    __asm__ __volatile__(
67       /* Set values in the FP stack. */
68       "flds   %[x0]\n"
69       "fld1\n"
70       "flds   %[x0]\n"
71       "fld1\n"
72       "flds   %[x0]\n"
73       "fld1\n"
74       "flds   %[x0]\n"
75       "fld1\n"
76 
77       /* Prepare syscall parameters. */
78       "pushl  %[sig]\n"
79       "pushl  %[pid]\n"
80       "pushl  $0xdeadbeef\n"
81       "movl   %[scall], %%eax\n"
82 
83       /* Trigger the signal handler. */
84       "int    $0x91\n"
85       "addl   $12, %%esp\n"
86       "fstps  0x00 + %[out]\n"
87       "fstps  0x04 + %[out]\n"
88       "fstps  0x08 + %[out]\n"
89       "fstps  0x0c + %[out]\n"
90       "fstps  0x10 + %[out]\n"
91       "fstps  0x14 + %[out]\n"
92       "fstps  0x18 + %[out]\n"
93       "fstps  0x1c + %[out]\n"
94       : [out] "=m" (out[0])
95       : [scall] "i" (SYS_kill), [pid] "a" (pid), [sig] "i" (SIGUSR1),
96         [x0] "m" (x0)
97       : "edx", "cc", "memory");
98 
99    printf("Values in the signal handler:\n");
100    printf("  fp[0]=%f, fp[2]=%f, fp[4]=%f, fp[6]=%f\n",
101           inhandler[0], inhandler[2], inhandler[4], inhandler[6]);
102    /* Check that inhandler[1], inhandler[3], inhandler[5] and inhandler[7]
103       contain uninitialised values (origin is px[0]). */
104    if (inhandler[1] || inhandler[3] || inhandler[5] || inhandler[7])
105       assert(0);
106 
107    printf("Values after the return from the signal handler:\n");
108    printf("  fp[0]=%f, fp[2]=%f, fp[4]=%f, fp[6]=%f\n",
109           out[0], out[2], out[4], out[6]);
110    /* Check that out[1], out[3], out[5] and out[7] contain uninitialised
111       values (origin is px[0]). */
112    if (out[1] || out[3] || out[5] || out[7])
113       assert(0);
114 
115    return 0;
116 }
117 
118