syscall_subr.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University.
  4. * Copyright (c) 1993,1994 The University of Utah and
  5. * the Computer Systems Laboratory (CSL).
  6. * All rights reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
  15. * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
  16. * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
  17. * THIS SOFTWARE.
  18. *
  19. * Carnegie Mellon requests users of this software to return to
  20. *
  21. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  22. * School of Computer Science
  23. * Carnegie Mellon University
  24. * Pittsburgh PA 15213-3890
  25. *
  26. * any improvements or extensions that they make and grant Carnegie Mellon
  27. * the rights to redistribute these changes.
  28. */
  29. #include <mach/boolean.h>
  30. #include <mach/thread_switch.h>
  31. #include <ipc/ipc_port.h>
  32. #include <ipc/ipc_space.h>
  33. #include <kern/counters.h>
  34. #include <kern/ipc_kobject.h>
  35. #include <kern/mach_clock.h>
  36. #include <kern/printf.h>
  37. #include <kern/processor.h>
  38. #include <kern/sched.h>
  39. #include <kern/sched_prim.h>
  40. #include <kern/syscall_subr.h>
  41. #include <kern/ipc_sched.h>
  42. #include <kern/task.h>
  43. #include <kern/thread.h>
  44. #include <machine/machspl.h> /* for splsched */
  45. #if MACH_FIXPRI
  46. #include <mach/policy.h>
  47. #endif /* MACH_FIXPRI */
  48. /*
  49. * swtch and swtch_pri both attempt to context switch (logic in
  50. * thread_block no-ops the context switch if nothing would happen).
  51. * A boolean is returned that indicates whether there is anything
  52. * else runnable.
  53. *
  54. * This boolean can be used by a thread waiting on a
  55. * lock or condition: If FALSE is returned, the thread is justified
  56. * in becoming a resource hog by continuing to spin because there's
  57. * nothing else useful that the processor could do. If TRUE is
  58. * returned, the thread should make one more check on the
  59. * lock and then be a good citizen and really suspend.
  60. */
  61. void swtch_continue(void)
  62. {
  63. processor_t myprocessor;
  64. myprocessor = current_processor();
  65. thread_syscall_return(myprocessor->runq.count > 0 ||
  66. myprocessor->processor_set->runq.count > 0);
  67. /*NOTREACHED*/
  68. }
  69. boolean_t swtch(void)
  70. {
  71. processor_t myprocessor;
  72. #if NCPUS > 1
  73. myprocessor = current_processor();
  74. if (myprocessor->runq.count == 0 &&
  75. myprocessor->processor_set->runq.count == 0)
  76. return(FALSE);
  77. #endif /* NCPUS > 1 */
  78. counter(c_swtch_block++);
  79. thread_block(swtch_continue);
  80. myprocessor = current_processor();
  81. return(myprocessor->runq.count > 0 ||
  82. myprocessor->processor_set->runq.count > 0);
  83. }
  84. void swtch_pri_continue(void)
  85. {
  86. thread_t thread = current_thread();
  87. processor_t myprocessor;
  88. if (thread->depress_priority >= 0)
  89. (void) thread_depress_abort(thread);
  90. myprocessor = current_processor();
  91. thread_syscall_return(myprocessor->runq.count > 0 ||
  92. myprocessor->processor_set->runq.count > 0);
  93. /*NOTREACHED*/
  94. }
  95. boolean_t swtch_pri(int pri)
  96. {
  97. thread_t thread = current_thread();
  98. processor_t myprocessor;
  99. #if NCPUS > 1
  100. myprocessor = current_processor();
  101. if (myprocessor->runq.count == 0 &&
  102. myprocessor->processor_set->runq.count == 0)
  103. return(FALSE);
  104. #endif /* NCPUS > 1 */
  105. /*
  106. * XXX need to think about depression duration.
  107. * XXX currently using min quantum.
  108. */
  109. thread_depress_priority(thread, min_quantum);
  110. counter(c_swtch_pri_block++);
  111. thread_block(swtch_pri_continue);
  112. if (thread->depress_priority >= 0)
  113. (void) thread_depress_abort(thread);
  114. myprocessor = current_processor();
  115. return(myprocessor->runq.count > 0 ||
  116. myprocessor->processor_set->runq.count > 0);
  117. }
  118. void thread_switch_continue(void)
  119. {
  120. thread_t cur_thread = current_thread();
  121. /*
  122. * Restore depressed priority
  123. */
  124. if (cur_thread->depress_priority >= 0)
  125. (void) thread_depress_abort(cur_thread);
  126. thread_syscall_return(KERN_SUCCESS);
  127. /*NOTREACHED*/
  128. }
  129. /*
  130. * thread_switch:
  131. *
  132. * Context switch. User may supply thread hint.
  133. *
  134. * Fixed priority threads that call this get what they asked for
  135. * even if that violates priority order.
  136. */
  137. kern_return_t thread_switch(
  138. mach_port_t thread_name,
  139. int option,
  140. mach_msg_timeout_t option_time)
  141. {
  142. thread_t cur_thread = current_thread();
  143. processor_t myprocessor;
  144. ipc_port_t port;
  145. /*
  146. * Process option.
  147. */
  148. switch (option) {
  149. case SWITCH_OPTION_NONE:
  150. /*
  151. * Nothing to do.
  152. */
  153. break;
  154. case SWITCH_OPTION_DEPRESS:
  155. /*
  156. * Depress priority for given time.
  157. */
  158. thread_depress_priority(cur_thread, option_time);
  159. break;
  160. case SWITCH_OPTION_WAIT:
  161. thread_will_wait_with_timeout(cur_thread, option_time);
  162. break;
  163. default:
  164. return(KERN_INVALID_ARGUMENT);
  165. }
  166. #ifndef MIGRATING_THREADS /* XXX thread_run defunct */
  167. /*
  168. * Check and act on thread hint if appropriate.
  169. */
  170. if ((thread_name != 0) &&
  171. (ipc_port_translate_send(cur_thread->task->itk_space,
  172. thread_name, &port) == KERN_SUCCESS)) {
  173. /* port is locked, but it might not be active */
  174. /*
  175. * Get corresponding thread.
  176. */
  177. if (ip_active(port) && (ip_kotype(port) == IKOT_THREAD)) {
  178. thread_t thread;
  179. spl_t s;
  180. thread = (thread_t) port->ip_kobject;
  181. /*
  182. * Check if the thread is in the right pset. Then
  183. * pull it off its run queue. If it
  184. * doesn't come, then it's not eligible.
  185. */
  186. s = splsched();
  187. thread_lock(thread);
  188. if ((thread->processor_set == cur_thread->processor_set)
  189. && (rem_runq(thread) != RUN_QUEUE_NULL)) {
  190. /*
  191. * Hah, got it!!
  192. */
  193. thread_unlock(thread);
  194. (void) splx(s);
  195. ip_unlock(port);
  196. /* XXX thread might disappear on us now? */
  197. #if MACH_FIXPRI
  198. if (thread->policy == POLICY_FIXEDPRI) {
  199. myprocessor = current_processor();
  200. myprocessor->quantum = thread->sched_data;
  201. myprocessor->first_quantum = TRUE;
  202. }
  203. #endif /* MACH_FIXPRI */
  204. counter(c_thread_switch_handoff++);
  205. thread_run(thread_switch_continue, thread);
  206. /*
  207. * Restore depressed priority
  208. */
  209. if (cur_thread->depress_priority >= 0)
  210. (void) thread_depress_abort(cur_thread);
  211. return(KERN_SUCCESS);
  212. }
  213. thread_unlock(thread);
  214. (void) splx(s);
  215. }
  216. ip_unlock(port);
  217. }
  218. #endif /* not MIGRATING_THREADS */
  219. /*
  220. * No handoff hint supplied, or hint was wrong. Call thread_block() in
  221. * hopes of running something else. If nothing else is runnable,
  222. * thread_block will detect this. WARNING: thread_switch with no
  223. * option will not do anything useful if the thread calling it is the
  224. * highest priority thread (can easily happen with a collection
  225. * of timesharing threads).
  226. */
  227. #if NCPUS > 1
  228. myprocessor = current_processor();
  229. if (myprocessor->processor_set->runq.count > 0 ||
  230. myprocessor->runq.count > 0)
  231. #endif /* NCPUS > 1 */
  232. {
  233. counter(c_thread_switch_block++);
  234. thread_block(thread_switch_continue);
  235. }
  236. /*
  237. * Restore depressed priority
  238. */
  239. if (cur_thread->depress_priority >= 0)
  240. (void) thread_depress_abort(cur_thread);
  241. return(KERN_SUCCESS);
  242. }
  243. /*
  244. * thread_depress_priority
  245. *
  246. * Depress thread's priority to lowest possible for specified period.
  247. * Intended for use when thread wants a lock but doesn't know which
  248. * other thread is holding it. As with thread_switch, fixed
  249. * priority threads get exactly what they asked for. Users access
  250. * this by the SWITCH_OPTION_DEPRESS option to thread_switch. A Time
  251. * of zero will result in no timeout being scheduled.
  252. */
  253. void
  254. thread_depress_priority(
  255. thread_t thread,
  256. mach_msg_timeout_t depress_time)
  257. {
  258. unsigned int ticks;
  259. spl_t s;
  260. /* convert from milliseconds to ticks */
  261. ticks = convert_ipc_timeout_to_ticks(depress_time);
  262. s = splsched();
  263. thread_lock(thread);
  264. /*
  265. * If thread is already depressed, override previous depression.
  266. */
  267. reset_timeout_check(&thread->depress_timer);
  268. /*
  269. * Save current priority, then set priority and
  270. * sched_pri to their lowest possible values.
  271. */
  272. thread->depress_priority = thread->priority;
  273. thread->priority = NRQS-1;
  274. thread->sched_pri = NRQS-1;
  275. if (ticks != 0)
  276. set_timeout(&thread->depress_timer, ticks);
  277. thread_unlock(thread);
  278. (void) splx(s);
  279. }
  280. /*
  281. * thread_depress_timeout:
  282. *
  283. * Timeout routine for priority depression.
  284. */
  285. void
  286. thread_depress_timeout(thread_t thread)
  287. {
  288. spl_t s;
  289. s = splsched();
  290. thread_lock(thread);
  291. /*
  292. * If we lose a race with thread_depress_abort,
  293. * then depress_priority might be -1.
  294. */
  295. if (thread->depress_priority >= 0) {
  296. thread->priority = thread->depress_priority;
  297. thread->depress_priority = -1;
  298. compute_priority(thread, FALSE);
  299. }
  300. thread_unlock(thread);
  301. (void) splx(s);
  302. }
  303. /*
  304. * thread_depress_abort:
  305. *
  306. * Prematurely abort priority depression if there is one.
  307. */
  308. kern_return_t
  309. thread_depress_abort(thread_t thread)
  310. {
  311. spl_t s;
  312. if (thread == THREAD_NULL)
  313. return(KERN_INVALID_ARGUMENT);
  314. s = splsched();
  315. thread_lock(thread);
  316. /*
  317. * Only restore priority if thread is depressed.
  318. */
  319. if (thread->depress_priority >= 0) {
  320. reset_timeout_check(&thread->depress_timer);
  321. thread->priority = thread->depress_priority;
  322. thread->depress_priority = -1;
  323. compute_priority(thread, FALSE);
  324. }
  325. thread_unlock(thread);
  326. (void) splx(s);
  327. return(KERN_SUCCESS);
  328. }
  329. /*
  330. * mach_print
  331. *
  332. * Display a null-terminated character string on the Mach console.
  333. * This system call is meant as a debugging tool useful to circumvent
  334. * messaging altogether.
  335. */
  336. #ifdef MACH_KDB
  337. void
  338. mach_print(const char *s)
  339. {
  340. printf("%s", s);
  341. }
  342. #endif /* MACH_KDB */