]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/syscall_subr.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / osfmk / kern / syscall_subr.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52
1c79356b
A
53#include <mach/boolean.h>
54#include <mach/thread_switch.h>
55#include <ipc/ipc_port.h>
56#include <ipc/ipc_space.h>
57#include <kern/counters.h>
58#include <kern/etap_macros.h>
59#include <kern/ipc_kobject.h>
60#include <kern/processor.h>
61#include <kern/sched.h>
62#include <kern/sched_prim.h>
63#include <kern/spl.h>
64#include <kern/task.h>
65#include <kern/thread.h>
66#include <kern/ast.h>
67#include <mach/policy.h>
68
69#include <kern/syscall_subr.h>
70#include <mach/mach_host_server.h>
71#include <mach/mach_syscalls.h>
72
0b4e3aa0 73#include <kern/mk_sp.h>
1c79356b
A
74
75/*
76 * swtch and swtch_pri both attempt to context switch (logic in
77 * thread_block no-ops the context switch if nothing would happen).
78 * A boolean is returned that indicates whether there is anything
79 * else runnable.
80 *
81 * This boolean can be used by a thread waiting on a
82 * lock or condition: If FALSE is returned, the thread is justified
83 * in becoming a resource hog by continuing to spin because there's
84 * nothing else useful that the processor could do. If TRUE is
85 * returned, the thread should make one more check on the
86 * lock and then be a good citizen and really suspend.
87 */
88
0b4e3aa0
A
89void
90swtch_continue(void)
1c79356b 91{
0b4e3aa0
A
92 register processor_t myprocessor;
93 boolean_t result;
1c79356b
A
94
95 mp_disable_preemption();
96 myprocessor = current_processor();
0b4e3aa0
A
97 result = myprocessor->runq.count > 0 ||
98 myprocessor->processor_set->runq.count > 0;
1c79356b 99 mp_enable_preemption();
1c79356b 100
0b4e3aa0
A
101 thread_syscall_return(result);
102 /*NOTREACHED*/
103}
1c79356b
A
104
105boolean_t
106swtch(void)
107{
108 register processor_t myprocessor;
109 boolean_t result;
110
111 mp_disable_preemption();
112 myprocessor = current_processor();
0b4e3aa0 113 if ( myprocessor->runq.count == 0 &&
1c79356b
A
114 myprocessor->processor_set->runq.count == 0 ) {
115 mp_enable_preemption();
116
117 return (FALSE);
118 }
119 mp_enable_preemption();
120
121 counter(c_swtch_block++);
122
9bccf70c 123 thread_block_reason(swtch_continue, AST_YIELD);
1c79356b
A
124
125 mp_disable_preemption();
126 myprocessor = current_processor();
0b4e3aa0
A
127 result = myprocessor->runq.count > 0 ||
128 myprocessor->processor_set->runq.count > 0;
1c79356b
A
129 mp_enable_preemption();
130
131 return (result);
132}
133
0b4e3aa0
A
134void
135swtch_pri_continue(void)
136{
137 register processor_t myprocessor;
138 boolean_t result;
139
140 _mk_sp_thread_depress_abort(current_thread(), FALSE);
141
142 mp_disable_preemption();
143 myprocessor = current_processor();
144 result = myprocessor->runq.count > 0 ||
145 myprocessor->processor_set->runq.count > 0;
146 mp_enable_preemption();
147
148 thread_syscall_return(result);
149 /*NOTREACHED*/
150}
151
1c79356b
A
152boolean_t
153swtch_pri(
154 int pri)
155{
1c79356b
A
156 register processor_t myprocessor;
157 boolean_t result;
1c79356b 158
0b4e3aa0 159 mp_disable_preemption();
1c79356b 160 myprocessor = current_processor();
0b4e3aa0 161 if ( myprocessor->runq.count == 0 &&
1c79356b 162 myprocessor->processor_set->runq.count == 0 ) {
0b4e3aa0 163 mp_enable_preemption();
1c79356b
A
164
165 return (FALSE);
166 }
0b4e3aa0
A
167 mp_enable_preemption();
168
169 counter(c_swtch_pri_block++);
170
171 _mk_sp_thread_depress_abstime(std_quantum);
1c79356b 172
9bccf70c 173 thread_block_reason(swtch_pri_continue, AST_YIELD);
1c79356b 174
0b4e3aa0 175 _mk_sp_thread_depress_abort(current_thread(), FALSE);
1c79356b
A
176
177 mp_disable_preemption();
178 myprocessor = current_processor();
0b4e3aa0
A
179 result = myprocessor->runq.count > 0 ||
180 myprocessor->processor_set->runq.count > 0;
1c79356b
A
181 mp_enable_preemption();
182
183 return (result);
184}
185
186/*
187 * thread_switch:
188 *
189 * Context switch. User may supply thread hint.
190 */
191kern_return_t
192thread_switch(
193 mach_port_name_t thread_name,
194 int option,
195 mach_msg_timeout_t option_time)
196{
1c79356b 197 register thread_act_t hint_act = THR_ACT_NULL;
1c79356b
A
198
199 /*
200 * Process option.
201 */
202 switch (option) {
203
204 case SWITCH_OPTION_NONE:
205 case SWITCH_OPTION_DEPRESS:
206 case SWITCH_OPTION_WAIT:
207 break;
208
209 default:
210 return (KERN_INVALID_ARGUMENT);
211 }
212
213 if (thread_name != MACH_PORT_NULL) {
214 ipc_port_t port;
215
55e303ae 216 if (ipc_port_translate_send(current_task()->itk_space,
1c79356b
A
217 thread_name, &port) == KERN_SUCCESS) {
218 ip_reference(port);
219 ip_unlock(port);
220
221 hint_act = convert_port_to_act(port);
222 ipc_port_release(port);
223 }
224 }
225
0b4e3aa0 226 return _mk_sp_thread_switch(hint_act, option, option_time);
1c79356b 227}