4185b3b927
Add a selftest for the usage of FPU code in kernel mode. Currently only implemented for x86. In the future, kernel FPU testing could be unified between the different architectures supporting it. [ bp: - Split out from a conglomerate patch, put comments over statements. - run the test only on debugfs write. - Add bare-minimum run_test_fpu.sh, run 1000 iterations on all CPUs by default. - Add conditionally -msse2 so that clang doesn't generate library calls. - Use cc-option to detect gcc 7.1 not supporting -mpreferred-stack-boundary=3 (amluto). - Document stuff so that we don't forget. - Fix: ld: lib/test_fpu.o: in function `test_fpu_get': >> test_fpu.c:(.text+0x16e): undefined reference to `__sanitizer_cov_trace_cmpd' >> ld: test_fpu.c:(.text+0x1a7): undefined reference to `__sanitizer_cov_trace_cmpd' ld: test_fpu.c:(.text+0x1e0): undefined reference to `__sanitizer_cov_trace_cmpd' ] Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Petteri Aimonen <jpa@git.mail.kapsi.fi> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Link: https://lkml.kernel.org/r/20200624114646.28953-3-bp@alien8.de
62 lines
1.5 KiB
C
62 lines
1.5 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/* This testcase operates with the test_fpu kernel driver.
|
|
* It modifies the FPU control register in user mode and calls the kernel
|
|
* module to perform floating point operations in the kernel. The control
|
|
* register value should be independent between kernel and user mode.
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <fenv.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
const char *test_fpu_path = "/sys/kernel/debug/selftest_helpers/test_fpu";
|
|
|
|
int main(void)
|
|
{
|
|
char dummy[1];
|
|
int fd = open(test_fpu_path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
printf("[SKIP]\tcan't access %s: %s\n",
|
|
test_fpu_path, strerror(errno));
|
|
return 0;
|
|
}
|
|
|
|
if (read(fd, dummy, 1) < 0) {
|
|
printf("[FAIL]\taccess with default rounding mode failed\n");
|
|
return 1;
|
|
}
|
|
|
|
fesetround(FE_DOWNWARD);
|
|
if (read(fd, dummy, 1) < 0) {
|
|
printf("[FAIL]\taccess with downward rounding mode failed\n");
|
|
return 2;
|
|
}
|
|
if (fegetround() != FE_DOWNWARD) {
|
|
printf("[FAIL]\tusermode rounding mode clobbered\n");
|
|
return 3;
|
|
}
|
|
|
|
/* Note: the tests up to this point are quite safe and will only return
|
|
* an error. But the exception mask setting can cause misbehaving kernel
|
|
* to crash.
|
|
*/
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
feenableexcept(FE_ALL_EXCEPT);
|
|
if (read(fd, dummy, 1) < 0) {
|
|
printf("[FAIL]\taccess with fpu exceptions unmasked failed\n");
|
|
return 4;
|
|
}
|
|
if (fegetexcept() != FE_ALL_EXCEPT) {
|
|
printf("[FAIL]\tusermode fpu exception mask clobbered\n");
|
|
return 5;
|
|
}
|
|
|
|
printf("[OK]\ttest_fpu\n");
|
|
return 0;
|
|
}
|