1/*
2 * Copyright (c) 2000-2012 Apple Computer, 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 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
49 *  School of Computer Science
50 *  Carnegie Mellon University
51 *  Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58
59/*
60 *      kern/ast.h: Definitions for Asynchronous System Traps.
61 */
62
63#ifndef _KERN_AST_H_
64#define _KERN_AST_H_
65
66
67#include <kern/assert.h>
68#include <kern/macro_help.h>
69#include <kern/spl.h>
70#include <machine/ast.h>
71
72/*
73 * A processor takes an AST when it is about to return from an
74 * interrupt context, and calls ast_taken.
75 *
76 * Machine-dependent code is responsible for maintaining
77 * a set of reasons for an AST, and passing this set to ast_taken.
78 */
79typedef uint32_t		ast_t;
80
81/*
82 * When returning from interrupt/trap context to kernel mode,
83 * the pending ASTs are masked with AST_URGENT to determine if
84 * ast_taken(AST_PREEMPTION) should be called, for instance to
85 * effect preemption of a kernel thread by a realtime thread.
86 * This is also done when re-enabling preemption or re-enabling
87 * interrupts, since an AST may have been set while preemption
88 * was disabled, and it should take effect as soon as possible.
89 *
90 * When returning from interrupt/trap/syscall context to user
91 * mode, any and all ASTs that are pending should be handled.
92 *
93 * If a thread context switches, only ASTs not in AST_PER_THREAD
94 * remain active. The per-thread ASTs are stored in the thread_t
95 * and re-enabled when the thread context switches back.
96 *
97 * Typically the preemption ASTs are set as a result of threads
98 * becoming runnable, threads changing priority, or quantum
99 * expiration. If a thread becomes runnable and is chosen
100 * to run on another processor, cause_ast_check() may be called
101 * to IPI that processor and request csw_check() be run there.
102 */
103
104/*
105 *      Bits for reasons
106 */
107#define AST_PREEMPT		0x01
108#define AST_QUANTUM		0x02
109#define AST_URGENT		0x04
110#define AST_HANDOFF		0x08
111#define AST_YIELD		0x10
112#define AST_APC			0x20	/* migration APC hook */
113#define AST_LEDGER		0x40
114
115/*
116 * JMM - This is here temporarily. AST_BSD is used to simulate a
117 * general purpose mechanism for setting asynchronous procedure calls
118 * from the outside.
119 */
120#define AST_BSD			0x80
121#define AST_KPERF		0x100   /* kernel profiling */
122#define	AST_MACF		0x200	/* MACF user ret pending */
123#define AST_CHUD		0x400
124#define AST_CHUD_URGENT		0x800
125#define AST_GUARD		0x1000
126#define AST_TELEMETRY_USER	0x2000	/* telemetry sample requested on interrupt from userspace */
127#define AST_TELEMETRY_KERNEL	0x4000	/* telemetry sample requested on interrupt from kernel */
128#define AST_TELEMETRY_WINDOWED	0x8000	/* telemetry sample meant for the window buffer */
129
130#define AST_SFI			0x10000	/* Evaluate if SFI wait is needed before return to userspace */
131
132#define AST_NONE		0x00
133#define AST_ALL			(~AST_NONE)
134
135#define AST_SCHEDULING	(AST_PREEMPTION | AST_YIELD | AST_HANDOFF)
136#define AST_PREEMPTION	(AST_PREEMPT | AST_QUANTUM | AST_URGENT)
137
138#define AST_CHUD_ALL	(AST_CHUD_URGENT|AST_CHUD)
139#define AST_TELEMETRY_ALL	(AST_TELEMETRY_USER | AST_TELEMETRY_KERNEL | AST_TELEMETRY_WINDOWED)
140
141#ifdef  MACHINE_AST
142/*
143 *      machine/ast.h is responsible for defining aston and astoff.
144 */
145#else   /* MACHINE_AST */
146
147#define aston(mycpu)
148#define astoff(mycpu)
149
150#endif  /* MACHINE_AST */
151
152/* Initialize module */
153extern void		ast_init(void);
154
155/* Handle ASTs */
156extern void		ast_taken(
157					ast_t		mask,
158					boolean_t	enable);
159
160/* Check for pending ASTs */
161extern void    	ast_check(
162					processor_t		processor);
163
164/* Pending ast mask for the current processor */
165extern ast_t 	*ast_pending(void);
166
167/*
168 * Per-thread ASTs are reset at context-switch time.
169 */
170#ifndef MACHINE_AST_PER_THREAD
171#define MACHINE_AST_PER_THREAD  0
172#endif
173
174#define AST_PER_THREAD	(AST_APC | AST_BSD | AST_MACF | MACHINE_AST_PER_THREAD | AST_LEDGER | AST_GUARD | AST_TELEMETRY_USER | AST_TELEMETRY_KERNEL | AST_TELEMETRY_WINDOWED)
175/*
176 *	ast_pending(), ast_on(), ast_off(), ast_context(), and ast_propagate()
177 *	assume splsched.
178 */
179
180#define ast_on_fast(reasons)					\
181MACRO_BEGIN										\
182	ast_t	*_ast_myast = ast_pending();		\
183												\
184	if ((*_ast_myast |= (reasons)) != AST_NONE)	\
185		{ aston(_ast_myast); }					\
186MACRO_END
187
188#define ast_off_fast(reasons)					\
189MACRO_BEGIN										\
190	ast_t	*_ast_myast = ast_pending();		\
191												\
192	if ((*_ast_myast &= ~(reasons)) == AST_NONE) \
193		{ astoff(_ast_myast); }					\
194MACRO_END
195
196#define ast_propagate(reasons)		ast_on(reasons)
197
198#define ast_context(act)													\
199MACRO_BEGIN																	\
200	ast_t	*myast = ast_pending();											\
201																			\
202	if ((*myast = ((*myast &~ AST_PER_THREAD) | (act)->ast)) != AST_NONE)	\
203		{ aston(myast);	}													\
204	else																	\
205		{ astoff(myast); }													\
206MACRO_END
207
208#define ast_on(reason)			     ast_on_fast(reason)
209#define ast_off(reason)			     ast_off_fast(reason)
210
211/*
212 *	NOTE: if thread is the current thread, thread_ast_set() should
213 *  be followed by ast_propagate().
214 */
215#define thread_ast_set(act, reason)		\
216						(hw_atomic_or_noret(&(act)->ast, (reason)))
217#define thread_ast_clear(act, reason)	\
218						(hw_atomic_and_noret(&(act)->ast, ~(reason)))
219#define thread_ast_clear_all(act)		\
220						(hw_atomic_and_noret(&(act)->ast, AST_NONE))
221
222#ifdef MACH_BSD
223
224extern void astbsd_on(void);
225extern void act_set_astbsd(thread_t);
226extern void bsd_ast(thread_t);
227
228#endif /* MACH_BSD */
229
230#endif  /* _KERN_AST_H_ */
231