xref: /xnu-8796.141.3/tests/fp_exception.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /**
29*1b191cb5SApple OSS Distributions  * On devices that support it, this test ensures that a mach exception is
30*1b191cb5SApple OSS Distributions  * generated when an ARMv8 floating point exception is triggered.
31*1b191cb5SApple OSS Distributions  * Also verifies that the main thread's FPCR value matches its expected default.
32*1b191cb5SApple OSS Distributions  */
33*1b191cb5SApple OSS Distributions #include <darwintest.h>
34*1b191cb5SApple OSS Distributions #include <stdbool.h>
35*1b191cb5SApple OSS Distributions #include <stdint.h>
36*1b191cb5SApple OSS Distributions #include <stdio.h>
37*1b191cb5SApple OSS Distributions #include <stdlib.h>
38*1b191cb5SApple OSS Distributions #include <mach/mach.h>
39*1b191cb5SApple OSS Distributions #include <mach/thread_status.h>
40*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
41*1b191cb5SApple OSS Distributions #include <inttypes.h>
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions #include "exc_helpers.h"
44*1b191cb5SApple OSS Distributions 
45*1b191cb5SApple OSS Distributions T_GLOBAL_META(
46*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
47*1b191cb5SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
48*1b191cb5SApple OSS Distributions 	T_META_OWNER("devon_andrade"),
49*1b191cb5SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
50*1b191cb5SApple OSS Distributions 
51*1b191cb5SApple OSS Distributions /* The bit to set in FPCR to enable the divide-by-zero floating point exception. */
52*1b191cb5SApple OSS Distributions #define FPCR_DIV_EXC 0x200
53*1b191cb5SApple OSS Distributions #define FPCR_INIT (0x0)
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions /* Whether we caught the EXC_ARITHMETIC mach exception or not. */
56*1b191cb5SApple OSS Distributions static volatile bool mach_exc_caught = false;
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions #ifdef __arm64__
59*1b191cb5SApple OSS Distributions static size_t
exc_arithmetic_handler(__unused mach_port_t task,__unused mach_port_t thread,exception_type_t type,mach_exception_data_t codes_64)60*1b191cb5SApple OSS Distributions exc_arithmetic_handler(
61*1b191cb5SApple OSS Distributions 	__unused mach_port_t task,
62*1b191cb5SApple OSS Distributions 	__unused mach_port_t thread,
63*1b191cb5SApple OSS Distributions 	exception_type_t type,
64*1b191cb5SApple OSS Distributions 	mach_exception_data_t codes_64)
65*1b191cb5SApple OSS Distributions {
66*1b191cb5SApple OSS Distributions 	/* Floating point divide by zero should cause an EXC_ARITHMETIC exception. */
67*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(type, EXC_ARITHMETIC, "Caught an EXC_ARITHMETIC exception");
68*1b191cb5SApple OSS Distributions 
69*1b191cb5SApple OSS Distributions 	/* Verify the exception is a floating point divide-by-zero exception. */
70*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(codes_64[0], (mach_exception_data_type_t)EXC_ARM_FP_DZ, "The subcode is EXC_ARM_FP_DZ (floating point divide-by-zero)");
71*1b191cb5SApple OSS Distributions 
72*1b191cb5SApple OSS Distributions 	mach_exc_caught = true;
73*1b191cb5SApple OSS Distributions 	return 4;
74*1b191cb5SApple OSS Distributions }
75*1b191cb5SApple OSS Distributions #endif
76*1b191cb5SApple OSS Distributions 
77*1b191cb5SApple OSS Distributions #define KERNEL_BOOTARGS_MAX_SIZE 1024
78*1b191cb5SApple OSS Distributions static char kernel_bootargs[KERNEL_BOOTARGS_MAX_SIZE];
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions T_DECL(armv8_fp_exception,
81*1b191cb5SApple OSS Distributions     "Test that ARMv8 floating point exceptions generate Mach exceptions, verify default FPCR value.")
82*1b191cb5SApple OSS Distributions {
83*1b191cb5SApple OSS Distributions #ifndef __arm64__
84*1b191cb5SApple OSS Distributions 	T_SKIP("Running on non-arm64 target, skipping...");
85*1b191cb5SApple OSS Distributions #else
86*1b191cb5SApple OSS Distributions 	mach_port_t exc_port = MACH_PORT_NULL;
87*1b191cb5SApple OSS Distributions 	size_t kernel_bootargs_len;
88*1b191cb5SApple OSS Distributions 
89*1b191cb5SApple OSS Distributions 	uint64_t fpcr = __builtin_arm_rsr64("FPCR");
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions 	if (fpcr != FPCR_INIT) {
92*1b191cb5SApple OSS Distributions 		T_FAIL("The floating point control register has a non-default value" "%" PRIx64, fpcr);
93*1b191cb5SApple OSS Distributions 	}
94*1b191cb5SApple OSS Distributions 
95*1b191cb5SApple OSS Distributions 	/* Attempt to enable Divide-by-Zero floating point exceptions in hardware. */
96*1b191cb5SApple OSS Distributions 	uint64_t fpcr_divexc = fpcr | FPCR_DIV_EXC;
97*1b191cb5SApple OSS Distributions 	__builtin_arm_wsr64("FPCR", fpcr_divexc);
98*1b191cb5SApple OSS Distributions #define DSB_ISH 0xb
99*1b191cb5SApple OSS Distributions 	__builtin_arm_dsb(DSB_ISH);
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions 	/* Devices that don't support floating point exceptions have FPCR as RAZ/WI. */
102*1b191cb5SApple OSS Distributions 	if (__builtin_arm_rsr64("FPCR") != fpcr_divexc) {
103*1b191cb5SApple OSS Distributions 		T_SKIP("Running on a device that doesn't support floating point exceptions, skipping...");
104*1b191cb5SApple OSS Distributions 	}
105*1b191cb5SApple OSS Distributions 
106*1b191cb5SApple OSS Distributions 	/* Check if floating-point exceptions are enabled */
107*1b191cb5SApple OSS Distributions 	kernel_bootargs_len = sizeof(kernel_bootargs);
108*1b191cb5SApple OSS Distributions 	kern_return_t kr = sysctlbyname("kern.bootargs", kernel_bootargs, &kernel_bootargs_len, NULL, 0);
109*1b191cb5SApple OSS Distributions 	if (kr != 0) {
110*1b191cb5SApple OSS Distributions 		T_SKIP("Could not get kernel bootargs, skipping...");
111*1b191cb5SApple OSS Distributions 	}
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions 	if (NULL == strstr(kernel_bootargs, "-fp_exceptions")) {
114*1b191cb5SApple OSS Distributions 		T_SKIP("Floating-point exceptions are disabled, skipping...");
115*1b191cb5SApple OSS Distributions 	}
116*1b191cb5SApple OSS Distributions 
117*1b191cb5SApple OSS Distributions 	/* Create the mach port the exception messages will be sent to. */
118*1b191cb5SApple OSS Distributions 	exc_port = create_exception_port(EXC_MASK_ARITHMETIC);
119*1b191cb5SApple OSS Distributions 	/* Spawn the exception server's thread. */
120*1b191cb5SApple OSS Distributions 	run_exception_handler(exc_port, exc_arithmetic_handler);
121*1b191cb5SApple OSS Distributions 
122*1b191cb5SApple OSS Distributions 	/**
123*1b191cb5SApple OSS Distributions 	 * This should cause a floating point divide-by-zero exception to get triggered.
124*1b191cb5SApple OSS Distributions 	 *
125*1b191cb5SApple OSS Distributions 	 * The kernel shouldn't resume this thread until the mach exception is handled
126*1b191cb5SApple OSS Distributions 	 * by the exception server that was just spawned. The exception handler will
127*1b191cb5SApple OSS Distributions 	 * explicitly increment the PC += 4 to move to the next instruction.
128*1b191cb5SApple OSS Distributions 	 */
129*1b191cb5SApple OSS Distributions 	float a = 6.5f;
130*1b191cb5SApple OSS Distributions 	float b = 0.0f;
131*1b191cb5SApple OSS Distributions 	__asm volatile ("fdiv %s0, %s1, %s2" : "=w" (a) : "w" (a), "w" (b));
132*1b191cb5SApple OSS Distributions 
133*1b191cb5SApple OSS Distributions 	if (mach_exc_caught) {
134*1b191cb5SApple OSS Distributions 		T_PASS("The expected floating point divide-by-zero exception was caught!");
135*1b191cb5SApple OSS Distributions 	} else {
136*1b191cb5SApple OSS Distributions 		T_FAIL("The floating point divide-by-zero exception was not captured :(");
137*1b191cb5SApple OSS Distributions 	}
138*1b191cb5SApple OSS Distributions #endif /* __arm64__ */
139*1b191cb5SApple OSS Distributions }
140