Path: blob/master/tools/testing/selftests/fpu/test_fpu.c
26288 views
// SPDX-License-Identifier: GPL-2.0+1/* This testcase operates with the test_fpu kernel driver.2* It modifies the FPU control register in user mode and calls the kernel3* module to perform floating point operations in the kernel. The control4* register value should be independent between kernel and user mode.5*/67#define _GNU_SOURCE8#include <stdio.h>9#include <errno.h>10#include <string.h>11#include <fenv.h>12#include <unistd.h>13#include <fcntl.h>1415const char *test_fpu_path = "/sys/kernel/debug/selftest_helpers/test_fpu";1617int main(void)18{19char dummy[1];20int fd = open(test_fpu_path, O_RDONLY);2122if (fd < 0) {23printf("[SKIP]\tcan't access %s: %s\n",24test_fpu_path, strerror(errno));25return 0;26}2728if (read(fd, dummy, 1) < 0) {29printf("[FAIL]\taccess with default rounding mode failed\n");30return 1;31}3233fesetround(FE_DOWNWARD);34if (read(fd, dummy, 1) < 0) {35printf("[FAIL]\taccess with downward rounding mode failed\n");36return 2;37}38if (fegetround() != FE_DOWNWARD) {39printf("[FAIL]\tusermode rounding mode clobbered\n");40return 3;41}4243/* Note: the tests up to this point are quite safe and will only return44* an error. But the exception mask setting can cause misbehaving kernel45* to crash.46*/47feclearexcept(FE_ALL_EXCEPT);48feenableexcept(FE_ALL_EXCEPT);49if (read(fd, dummy, 1) < 0) {50printf("[FAIL]\taccess with fpu exceptions unmasked failed\n");51return 4;52}53if (fegetexcept() != FE_ALL_EXCEPT) {54printf("[FAIL]\tusermode fpu exception mask clobbered\n");55return 5;56}5758printf("[OK]\ttest_fpu\n");59return 0;60}616263