]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/syscall_subr.c
b5ff64c69babbdb0bf60e2dcb8c73e078a4bd150
[apple/xnu.git] / osfmk / kern / syscall_subr.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
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
73 #include <kern/mk_sp.h>
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
89 void
90 swtch_continue(void)
91 {
92 register processor_t myprocessor;
93 boolean_t result;
94
95 mp_disable_preemption();
96 myprocessor = current_processor();
97 result = myprocessor->runq.count > 0 ||
98 myprocessor->processor_set->runq.count > 0;
99 mp_enable_preemption();
100
101 thread_syscall_return(result);
102 /*NOTREACHED*/
103 }
104
105 boolean_t
106 swtch(void)
107 {
108 register processor_t myprocessor;
109 boolean_t result;
110
111 mp_disable_preemption();
112 myprocessor = current_processor();
113 if ( myprocessor->runq.count == 0 &&
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
123 thread_block(swtch_continue);
124
125 mp_disable_preemption();
126 myprocessor = current_processor();
127 result = myprocessor->runq.count > 0 ||
128 myprocessor->processor_set->runq.count > 0;
129 mp_enable_preemption();
130
131 return (result);
132 }
133
134 void
135 swtch_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
152 boolean_t
153 swtch_pri(
154 int pri)
155 {
156 register processor_t myprocessor;
157 boolean_t result;
158
159 mp_disable_preemption();
160 myprocessor = current_processor();
161 if ( myprocessor->runq.count == 0 &&
162 myprocessor->processor_set->runq.count == 0 ) {
163 mp_enable_preemption();
164
165 return (FALSE);
166 }
167 mp_enable_preemption();
168
169 counter(c_swtch_pri_block++);
170
171 _mk_sp_thread_depress_abstime(std_quantum);
172
173 thread_block(swtch_pri_continue);
174
175 _mk_sp_thread_depress_abort(current_thread(), FALSE);
176
177 mp_disable_preemption();
178 myprocessor = current_processor();
179 result = myprocessor->runq.count > 0 ||
180 myprocessor->processor_set->runq.count > 0;
181 mp_enable_preemption();
182
183 return (result);
184 }
185
186 /*
187 * thread_switch:
188 *
189 * Context switch. User may supply thread hint.
190 */
191 kern_return_t
192 thread_switch(
193 mach_port_name_t thread_name,
194 int option,
195 mach_msg_timeout_t option_time)
196 {
197 register thread_t self = current_thread();
198 register thread_act_t hint_act = THR_ACT_NULL;
199
200 /*
201 * Process option.
202 */
203 switch (option) {
204
205 case SWITCH_OPTION_NONE:
206 case SWITCH_OPTION_DEPRESS:
207 case SWITCH_OPTION_WAIT:
208 break;
209
210 default:
211 return (KERN_INVALID_ARGUMENT);
212 }
213
214 if (thread_name != MACH_PORT_NULL) {
215 ipc_port_t port;
216
217 if (ipc_port_translate_send(self->top_act->task->itk_space,
218 thread_name, &port) == KERN_SUCCESS) {
219 ip_reference(port);
220 ip_unlock(port);
221
222 hint_act = convert_port_to_act(port);
223 ipc_port_release(port);
224 }
225 }
226
227 return _mk_sp_thread_switch(hint_act, option, option_time);
228 }