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 [email protected] 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 #include <sys/cdefs.h> 67 68 #include <kern/assert.h> 69 #include <kern/macro_help.h> 70 #include <kern/spl.h> 71 72 /* 73 * A processor detects an AST when it is about to return from an 74 * interrupt context, and calls ast_taken_kernel or ast_taken_user 75 * depending on whether it was returning from userspace or kernelspace. 76 * 77 * Machine-dependent code is responsible for maintaining 78 * a set of reasons for an AST. 79 * 80 * When returning from interrupt/trap context to kernel mode, 81 * if AST_URGENT is set, then ast_taken_kernel is called, for 82 * instance to effect preemption of a kernel thread by a realtime 83 * thread. 84 * 85 * This is also done when re-enabling preemption or re-enabling 86 * interrupts, since an AST may have been set while preemption 87 * was disabled, and it should take effect as soon as possible. 88 * 89 * When returning from interrupt/trap/syscall context to user 90 * mode, any and all ASTs that are pending should be handled by 91 * calling ast_taken_user. 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 * TODO: Split the context switch and return-to-user AST namespaces 107 * NOTE: Some of these are exported as the 'reason' code in scheduler tracepoints 108 */ 109 __options_decl(ast_t, uint32_t, { 110 AST_PREEMPT = 0x01, 111 AST_QUANTUM = 0x02, 112 AST_URGENT = 0x04, 113 AST_HANDOFF = 0x08, 114 AST_YIELD = 0x10, 115 AST_APC = 0x20, /* migration APC hook */ 116 AST_LEDGER = 0x40, 117 AST_BSD = 0x80, 118 AST_KPERF = 0x100, /* kernel profiling */ 119 AST_MACF = 0x200, /* MACF user ret pending */ 120 AST_RESET_PCS = 0x400, /* restartable ranges */ 121 AST_ARCADE = 0x800, /* arcade subsciption support */ 122 AST_MACH_EXCEPTION = 0x1000, 123 AST_TELEMETRY_USER = 0x2000, /* telemetry sample requested on interrupt from userspace */ 124 AST_TELEMETRY_KERNEL = 0x4000, /* telemetry sample requested on interrupt from kernel */ 125 AST_TELEMETRY_PMI = 0x8000, /* telemetry sample requested on PMI */ 126 AST_SFI = 0x10000, /* Evaluate if SFI wait is needed before return to userspace */ 127 AST_DTRACE = 0x20000, 128 AST_TELEMETRY_IO = 0x40000, /* telemetry sample requested for I/O */ 129 AST_KEVENT = 0x80000, 130 AST_REBALANCE = 0x100000, /* thread context switched due to rebalancing */ 131 // was AST_UNQUIESCE 0x200000 132 AST_PROC_RESOURCE = 0x400000, /* port space and/or file descriptor table has reached its limits */ 133 AST_DEBUG_ASSERT = 0x800000, /* check debug assertion */ 134 AST_TELEMETRY_MACF = 0x1000000, /* telemetry sample requested by MAC framework */ 135 AST_SYNTHESIZE_MACH = 0x2000000, 136 AST_LARGE_ENTER_TELEMETRY = 0x4000000, /* guard objects telemetry */ 137 }); 138 139 #define AST_NONE 0x00 140 #define AST_ALL (~AST_NONE) 141 142 #define AST_SCHEDULING (AST_PREEMPTION | AST_YIELD | AST_HANDOFF) 143 #define AST_PREEMPTION (AST_PREEMPT | AST_QUANTUM | AST_URGENT) 144 145 #define AST_TELEMETRY_ALL (AST_TELEMETRY_USER | AST_TELEMETRY_KERNEL | \ 146 AST_TELEMETRY_PMI | AST_TELEMETRY_IO | AST_TELEMETRY_MACF) 147 148 /* Per-thread ASTs follow the thread at context-switch time. */ 149 #define AST_PER_THREAD (AST_APC | AST_BSD | AST_MACF | AST_RESET_PCS | \ 150 AST_ARCADE | AST_LEDGER | AST_MACH_EXCEPTION | AST_SYNTHESIZE_MACH | \ 151 AST_LARGE_ENTER_TELEMETRY | AST_TELEMETRY_ALL | AST_KEVENT | \ 152 AST_PROC_RESOURCE | AST_DEBUG_ASSERT) 153 154 /* Handle AST_URGENT detected while in the kernel */ 155 extern void ast_taken_kernel(void); 156 157 /* Handle an AST flag set while returning to user mode (may continue via thread_exception_return) */ 158 extern void ast_taken_user(void); 159 160 /* Check for ASTs for per-process aio threads in the kernel */ 161 extern void ast_check_async_thread(void); 162 163 /* Check for pending ASTs */ 164 extern void ast_check(processor_t processor); 165 166 /* Pending ast mask for the current processor */ 167 extern ast_t *ast_pending(void); 168 169 /* Set AST flags on current processor */ 170 extern void ast_on(ast_t reasons); 171 172 /* Clear AST flags on current processor */ 173 extern void ast_off(ast_t reasons); 174 175 /* Consume specified AST flags from current processor */ 176 extern ast_t ast_consume(ast_t reasons); 177 178 /* Read specified AST flags from current processor */ 179 extern ast_t ast_peek(ast_t reasons); 180 181 /* Re-set current processor's per-thread AST flags to those set on thread */ 182 extern void ast_context(thread_t thread); 183 184 /* Propagate ASTs set on a thread to the current processor */ 185 extern void ast_propagate(thread_t thread); 186 187 /* 188 * Set an AST on a thread with thread_ast_set. 189 * 190 * You can then propagate it to the current processor with ast_propagate(), 191 * or tell another processor to act on it with cause_ast_check(). 192 * 193 * See act_set_ast() for an example. 194 */ 195 #define thread_ast_set(act, reason) ((void)os_atomic_or(&(act)->ast, (reason), relaxed)) 196 #define thread_ast_clear(act, reason) ((void)os_atomic_andnot(&(act)->ast, (reason), relaxed)) 197 #define thread_ast_peek(act, reason) (os_atomic_load(&(act)->ast, relaxed) & (reason)) 198 #define thread_ast_get(act) os_atomic_load(&(act)->ast, relaxed) 199 200 #ifdef MACH_BSD 201 202 extern void act_set_astbsd(thread_t); 203 extern void bsd_ast(thread_t); 204 extern void proc_filedesc_ast(task_t task); 205 206 #endif /* MACH_BSD */ 207 208 #ifdef CONFIG_DTRACE 209 extern void ast_dtrace_on(void); 210 extern void dtrace_ast(void); 211 #endif /* CONFIG_DTRACE */ 212 213 /* These are kept in sync with bsd/kern/ast.h */ 214 #define AST_KEVENT_RETURN_TO_KERNEL 0x0001 215 #define AST_KEVENT_REDRIVE_THREADREQ 0x0002 216 #define AST_KEVENT_WORKQ_QUANTUM_EXPIRED 0x0004 217 218 extern void kevent_ast(thread_t thread, uint16_t bits); 219 extern void act_set_astkevent(thread_t thread, uint16_t bits); 220 extern uint16_t act_clear_astkevent(thread_t thread, uint16_t bits); 221 extern bool act_set_ast_reset_pcs(task_t task, thread_t thread); 222 #if CONFIG_PROC_RESOURCE_LIMITS 223 extern void task_filedesc_ast(task_t task, int current_size, int soft_limit, int hard_limit); 224 extern void task_kqworkloop_ast(task_t task, int current_size, int soft_limit, int hard_limit); 225 #endif 226 extern void act_set_debug_assert(void); 227 228 extern void thread_debug_return_to_user_ast(thread_t thread); 229 #endif /* _KERN_AST_H_ */ 230