xref: /xnu-12377.61.12/osfmk/kern/timeout.h (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _KERN_TIMEOUT_H_
30 #define _KERN_TIMEOUT_H_
31 
32 #include <kern/kern_types.h>
33 #include <kern/timeout_decl.h>
34 
35 __options_closed_decl(timeout_flags_t, uint32_t, {
36 	TF_NONSPEC_TIMEBASE       = 0x01,
37 	TF_BACKTRACE              = 0x02,
38 #if XNU_KERNEL_PRIVATE
39 	TF_SAMPLE_INTERRUPT_TIME  = 0x04,
40 	TF_SAMPLE_PMC             = 0x08,
41 #endif /* XNU_KERNEL_PRIVATE */
42 });
43 
44 __enum_decl(kern_timeout_type_t, uint32_t, {
45 	KERN_TIMEOUT_PREEMPTION   = 1,
46 	KERN_TIMEOUT_INTERRUPT    = 2,
47 	KERN_TIMEOUT_MMIO         = 3,
48 	KERN_TIMEOUT_LOCK         = 4,
49 });
50 
51 extern void kern_timeout_start(kern_timeout_t *to, timeout_flags_t flags);
52 extern void kern_timeout_restart(kern_timeout_t *to, timeout_flags_t flags);
53 extern void kern_timeout_end(kern_timeout_t *to, timeout_flags_t flags);
54 extern void kern_timeout_override(kern_timeout_t *to);
55 extern void kern_timeout_try_panic(kern_timeout_type_t type, uint64_t payload, kern_timeout_t *to,
56     const char *prefix, uint64_t threshold);
57 
58 #if XNU_KERNEL_PRIVATE
59 extern void kern_timeout_cycles_instrs(kern_timeout_t *to, uint64_t *cycles, uint64_t *instrs);
60 extern void kern_timeout_cpi(kern_timeout_t *to, uint64_t *cpi_whole, uint64_t *cpi_fractional);
61 #endif /* XNU_KERNEL_PRIVATE */
62 
63 static inline void
kern_timeout_stretch(kern_timeout_t * to,uint64_t mt_ticks)64 kern_timeout_stretch(kern_timeout_t *to, uint64_t mt_ticks)
65 {
66 	to->start_mt -= mt_ticks;
67 }
68 
69 static inline uint64_t
kern_timeout_start_time(kern_timeout_t * to)70 kern_timeout_start_time(kern_timeout_t *to)
71 {
72 	return to->start_mt;
73 }
74 
75 /*
76  * Return the mach time elapsed beteween calls to kern_timeout_start() and kern_timeout_end().
77  */
78 static inline uint64_t
kern_timeout_gross_duration(kern_timeout_t * to)79 kern_timeout_gross_duration(kern_timeout_t *to)
80 {
81 	if (__improbable(to->start_mt == 0 || to->end_mt < to->start_mt)) {
82 		return 0;
83 	}
84 	return to->end_mt - to->start_mt;
85 }
86 
87 #if XNU_KERNEL_PRIVATE
88 /*
89  * Return the mach time elapsed beteween calls to kern_timeout_start() and kern_timeout_end()
90  * subtracting the mach time that elapsed handling interrupts.
91  */
92 static inline uint64_t
kern_timeout_net_duration(kern_timeout_t * to)93 kern_timeout_net_duration(kern_timeout_t *to)
94 {
95 	uint64_t gross_duration = kern_timeout_gross_duration(to);
96 	uint64_t int_duration = to->int_mt;
97 
98 	if (__improbable(to->start_mt == 0 || gross_duration < int_duration)) {
99 		return 0;
100 	}
101 	return gross_duration - int_duration;
102 }
103 #endif /* XNU_KERNEL_PRIVATE */
104 
105 static inline void
kern_timeout_mach_times(kern_timeout_t * to,uint64_t * start_mt,uint64_t * end_mt)106 kern_timeout_mach_times(kern_timeout_t *to, uint64_t *start_mt, uint64_t *end_mt)
107 {
108 	*start_mt = to->start_mt;
109 	*end_mt = to->end_mt;
110 }
111 
112 #endif /* _KERN_TIMEOUT_H_ */
113