100d38802SLucas Mateus Castro (alqotel) #include <stdlib.h>
2d21939caSMatheus Ferst #include <stdint.h>
300d38802SLucas Mateus Castro (alqotel) #include <assert.h>
400d38802SLucas Mateus Castro (alqotel) #include <signal.h>
500d38802SLucas Mateus Castro (alqotel) #include <sys/prctl.h>
600d38802SLucas Mateus Castro (alqotel)
7d21939caSMatheus Ferst #define MTFSF(FLM, FRB) asm volatile ("mtfsf %0, %1" :: "i" (FLM), "f" (FRB))
8d21939caSMatheus Ferst #define MFFS(FRT) asm("mffs %0" : "=f" (FRT))
9d21939caSMatheus Ferst
1000d38802SLucas Mateus Castro (alqotel) #define FPSCR_VE 7 /* Floating-point invalid operation exception enable */
1100d38802SLucas Mateus Castro (alqotel) #define FPSCR_VXSOFT 10 /* Floating-point invalid operation exception (soft) */
1200d38802SLucas Mateus Castro (alqotel) #define FPSCR_FI 17 /* Floating-point fraction inexact */
1300d38802SLucas Mateus Castro (alqotel)
1400d38802SLucas Mateus Castro (alqotel) #define FP_VE (1ull << FPSCR_VE)
1500d38802SLucas Mateus Castro (alqotel) #define FP_VXSOFT (1ull << FPSCR_VXSOFT)
1600d38802SLucas Mateus Castro (alqotel) #define FP_FI (1ull << FPSCR_FI)
1700d38802SLucas Mateus Castro (alqotel)
sigfpe_handler(int sig,siginfo_t * si,void * ucontext)1800d38802SLucas Mateus Castro (alqotel) void sigfpe_handler(int sig, siginfo_t *si, void *ucontext)
1900d38802SLucas Mateus Castro (alqotel) {
2000d38802SLucas Mateus Castro (alqotel) if (si->si_code == FPE_FLTINV) {
2100d38802SLucas Mateus Castro (alqotel) exit(0);
2200d38802SLucas Mateus Castro (alqotel) }
2300d38802SLucas Mateus Castro (alqotel) exit(1);
2400d38802SLucas Mateus Castro (alqotel) }
2500d38802SLucas Mateus Castro (alqotel)
main(void)2600d38802SLucas Mateus Castro (alqotel) int main(void)
2700d38802SLucas Mateus Castro (alqotel) {
28d21939caSMatheus Ferst uint64_t fpscr;
2900d38802SLucas Mateus Castro (alqotel)
3000d38802SLucas Mateus Castro (alqotel) struct sigaction sa = {
3100d38802SLucas Mateus Castro (alqotel) .sa_sigaction = sigfpe_handler,
3200d38802SLucas Mateus Castro (alqotel) .sa_flags = SA_SIGINFO
3300d38802SLucas Mateus Castro (alqotel) };
3400d38802SLucas Mateus Castro (alqotel)
3500d38802SLucas Mateus Castro (alqotel) /*
3600d38802SLucas Mateus Castro (alqotel) * Enable the MSR bits F0 and F1 to enable exceptions.
3700d38802SLucas Mateus Castro (alqotel) * This shouldn't be needed in linux-user as these bits are enabled by
3800d38802SLucas Mateus Castro (alqotel) * default, but this allows to execute either in a VM or a real machine
3900d38802SLucas Mateus Castro (alqotel) * to compare the behaviors.
4000d38802SLucas Mateus Castro (alqotel) */
4100d38802SLucas Mateus Castro (alqotel) prctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE);
4200d38802SLucas Mateus Castro (alqotel)
4300d38802SLucas Mateus Castro (alqotel) /* First test if the FI bit is being set correctly */
44d21939caSMatheus Ferst MTFSF(0b11111111, FP_FI);
45d21939caSMatheus Ferst MFFS(fpscr);
46d21939caSMatheus Ferst assert((fpscr & FP_FI) != 0);
4700d38802SLucas Mateus Castro (alqotel)
4800d38802SLucas Mateus Castro (alqotel) /* Then test if the deferred exception is being called correctly */
4900d38802SLucas Mateus Castro (alqotel) sigaction(SIGFPE, &sa, NULL);
5000d38802SLucas Mateus Castro (alqotel)
5100d38802SLucas Mateus Castro (alqotel) /*
5200d38802SLucas Mateus Castro (alqotel) * Although the VXSOFT exception has been chosen, based on test in a Power9
5300d38802SLucas Mateus Castro (alqotel) * any combination of exception bit + its enabling bit should work.
5400d38802SLucas Mateus Castro (alqotel) * But if a different exception is chosen si_code check should
5500d38802SLucas Mateus Castro (alqotel) * change accordingly.
5600d38802SLucas Mateus Castro (alqotel) */
57d21939caSMatheus Ferst MTFSF(0b11111111, FP_VE | FP_VXSOFT);
5800d38802SLucas Mateus Castro (alqotel)
5900d38802SLucas Mateus Castro (alqotel) return 1;
6000d38802SLucas Mateus Castro (alqotel) }
61