]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/syscall_subr.c
caca3545acee49a33952f66733fd10c2bc973492
[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 <cpus.h>
54
55 #include <mach/boolean.h>
56 #include <mach/thread_switch.h>
57 #include <ipc/ipc_port.h>
58 #include <ipc/ipc_space.h>
59 #include <kern/counters.h>
60 #include <kern/etap_macros.h>
61 #include <kern/ipc_kobject.h>
62 #include <kern/processor.h>
63 #include <kern/sched.h>
64 #include <kern/sched_prim.h>
65 #include <kern/spl.h>
66 #include <kern/task.h>
67 #include <kern/thread.h>
68 #include <kern/ast.h>
69 #include <mach/policy.h>
70
71 #include <kern/syscall_subr.h>
72 #include <mach/mach_host_server.h>
73 #include <mach/mach_syscalls.h>
74
75 #include <kern/sf.h>
76
77 /*
78 * swtch and swtch_pri both attempt to context switch (logic in
79 * thread_block no-ops the context switch if nothing would happen).
80 * A boolean is returned that indicates whether there is anything
81 * else runnable.
82 *
83 * This boolean can be used by a thread waiting on a
84 * lock or condition: If FALSE is returned, the thread is justified
85 * in becoming a resource hog by continuing to spin because there's
86 * nothing else useful that the processor could do. If TRUE is
87 * returned, the thread should make one more check on the
88 * lock and then be a good citizen and really suspend.
89 */
90
91 #if 0
92
93 /* broken..do not enable */
94
95 swtch_continue()
96 {
97 boolean_t retval;
98 register processor_t myprocessor;
99
100 mp_disable_preemption();
101 myprocessor = current_processor();
102 retval = (
103 #if NCPUS > 1
104 myprocessor->runq.count > 0 ||
105 #endif /*NCPUS > 1*/
106 myprocessor->processor_set->runq.count > 0);
107 mp_enable_preemption();
108 return retval;
109 }
110
111 #endif
112
113 boolean_t
114 swtch(void)
115 {
116 register processor_t myprocessor;
117 boolean_t result;
118
119 mp_disable_preemption();
120 myprocessor = current_processor();
121 if (
122 #if NCPUS > 1
123 myprocessor->runq.count == 0 &&
124 #endif /* NCPUS > 1 */
125 myprocessor->processor_set->runq.count == 0 ) {
126 mp_enable_preemption();
127
128 return (FALSE);
129 }
130 mp_enable_preemption();
131
132 counter(c_swtch_block++);
133
134 thread_block((void (*)(void)) 0);
135
136 mp_disable_preemption();
137 myprocessor = current_processor();
138 result =
139 #if NCPUS > 1
140 myprocessor->runq.count > 0 ||
141 #endif /*NCPUS > 1*/
142 myprocessor->processor_set->runq.count > 0;
143 mp_enable_preemption();
144
145 return (result);
146 }
147
148 boolean_t
149 swtch_pri(
150 int pri)
151 {
152 thread_t self = current_thread();
153 register processor_t myprocessor;
154 boolean_t result;
155 sched_policy_t *policy;
156 spl_t s;
157
158 s = splsched();
159 thread_lock(self);
160 myprocessor = current_processor();
161 if (
162 #if NCPUS > 1
163 myprocessor->runq.count == 0 &&
164 #endif /* NCPUS > 1 */
165 myprocessor->processor_set->runq.count == 0 ) {
166 thread_unlock(self);
167 splx(s);
168
169 return (FALSE);
170 }
171
172 policy = &sched_policy[self->policy];
173 thread_unlock(self);
174 splx(s);
175
176 policy->sp_ops.sp_swtch_pri(policy, pri);
177
178 mp_disable_preemption();
179 myprocessor = current_processor();
180 result =
181 #if NCPUS > 1
182 myprocessor->runq.count > 0 ||
183 #endif /*NCPUS > 1*/
184 myprocessor->processor_set->runq.count > 0;
185 mp_enable_preemption();
186
187 return (result);
188 }
189
190 /*
191 * thread_switch:
192 *
193 * Context switch. User may supply thread hint.
194 */
195 kern_return_t
196 thread_switch(
197 mach_port_name_t thread_name,
198 int option,
199 mach_msg_timeout_t option_time)
200 {
201 register thread_t self = current_thread();
202 register thread_act_t hint_act = THR_ACT_NULL;
203 sched_policy_t *policy;
204 spl_t s;
205
206 /*
207 * Process option.
208 */
209 switch (option) {
210
211 case SWITCH_OPTION_NONE:
212 case SWITCH_OPTION_DEPRESS:
213 case SWITCH_OPTION_WAIT:
214 break;
215
216 default:
217 return (KERN_INVALID_ARGUMENT);
218 }
219
220 if (thread_name != MACH_PORT_NULL) {
221 ipc_port_t port;
222
223 if (ipc_port_translate_send(self->top_act->task->itk_space,
224 thread_name, &port) == KERN_SUCCESS) {
225 ip_reference(port);
226 ip_unlock(port);
227
228 hint_act = convert_port_to_act(port);
229 ipc_port_release(port);
230 }
231 }
232
233 s = splsched();
234 thread_lock(self);
235 policy = &sched_policy[self->policy];
236 thread_unlock(self);
237 splx(s);
238
239 /*
240 * This is a scheduling policy-dependent operation.
241 * Call the routine associated with the thread's
242 * scheduling policy.
243 */
244 return (policy->sp_ops.
245 sp_thread_switch(policy, hint_act, option, option_time));
246 }