^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Test cases for using floating point operations inside a kernel module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This tests kernel_fpu_begin() and kernel_fpu_end() functions, especially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * when userland has modified the floating point control registers. The kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * state might depend on the state set by the userland thread that was active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * before a syscall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * To facilitate the test, this module registers file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * /sys/kernel/debug/selftest_helpers/test_fpu, which when read causes a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * sequence of floating point operations. If the operations fail, either the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * read returns error status or the kernel crashes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * If the operations succeed, the read returns "1\n".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/fpu/api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int test_fpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * This sequence of operations tests that rounding mode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * to nearest and that denormal numbers are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Volatile variables are used to avoid compiler optimizing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * the calculations away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) volatile double a, b, c, d, e, f, g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) a = 4.0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) b = 1e-15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) c = 1e-310;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Sets precision flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) d = a + b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Result depends on rounding mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) e = a + b / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Denormal and very large values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) f = b / c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Depends on denormal support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) g = a + c * f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (d > a && e > a && g > a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int test_fpu_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kernel_fpu_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) status = test_fpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) kernel_fpu_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) DEFINE_SIMPLE_ATTRIBUTE(test_fpu_fops, test_fpu_get, NULL, "%lld\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct dentry *selftest_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int __init test_fpu_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) selftest_dir = debugfs_create_dir("selftest_helpers", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!selftest_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) debugfs_create_file("test_fpu", 0444, selftest_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) &test_fpu_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void __exit test_fpu_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) debugfs_remove(selftest_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) module_init(test_fpu_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) module_exit(test_fpu_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) MODULE_LICENSE("GPL");