thread.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1993-1987 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * File: thread.h
  28. * Author: Avadis Tevanian, Jr.
  29. *
  30. * This file contains the structure definitions for threads.
  31. *
  32. */
  33. #ifndef _KERN_THREAD_H_
  34. #define _KERN_THREAD_H_
  35. #include <mach/boolean.h>
  36. #include <mach/thread_info.h>
  37. #include <mach/thread_status.h>
  38. #include <mach/machine/vm_types.h>
  39. #include <mach/message.h>
  40. #include <mach/port.h>
  41. #include <mach/vm_prot.h>
  42. #include <kern/ast.h>
  43. #include <kern/cpu_number.h>
  44. #include <kern/mach_clock.h>
  45. #include <kern/queue.h>
  46. #include <kern/pc_sample.h>
  47. #include <kern/processor.h>
  48. #include <kern/sched_prim.h> /* event_t, continuation_t */
  49. #include <kern/timer.h>
  50. #include <kern/lock.h>
  51. #include <kern/sched.h>
  52. #include <kern/task.h> /* for current_space(), current_map() */
  53. #include <machine/thread.h>
  54. #include <ipc/ipc_kmsg_queue.h>
  55. //BEGIN LINUX INCLUDES
  56. #include <linux/wait.h>
  57. #undef current
  58. //END LINUX INCLUDES
  59. struct thread {
  60. /* Run queues */
  61. queue_chain_t links; /* current run queue links */
  62. run_queue_t runq; /* run queue p is on SEE BELOW */
  63. /*
  64. * NOTE: The runq field in the thread structure has an unusual
  65. * locking protocol. If its value is RUN_QUEUE_NULL, then it is
  66. * locked by the thread_lock, but if its value is something else
  67. * (i.e. a run_queue) then it is locked by that run_queue's lock.
  68. */
  69. /* Task information */
  70. struct task_struct * linux_task;
  71. task_t task; /* Task to which I belong */
  72. queue_chain_t thread_list; /* list of threads in task */
  73. /* Flags */
  74. /* The flags are grouped here, but documented at the original
  75. position. */
  76. union {
  77. struct {
  78. unsigned state:16;
  79. unsigned wake_active:1;
  80. unsigned active:1;
  81. };
  82. event_t event_key;
  83. /* These keys can be used with thread_wakeup and friends. */
  84. #define TH_EV_WAKE_ACTIVE(t) ((event_t) (&(t)->event_key + 0))
  85. #define TH_EV_STATE(t) ((event_t) (&(t)->event_key + 1))
  86. };
  87. /* Thread bookkeeping */
  88. queue_chain_t pset_threads; /* list of all threads in proc set*/
  89. /* Self-preservation */
  90. decl_simple_lock_data(,lock)
  91. int ref_count; /* number of references to me */
  92. /* Hardware state */
  93. pcb_t pcb; /* hardware pcb & machine state */
  94. vm_offset_t kernel_stack; /* accurate only if the thread is
  95. not swapped and not executing */
  96. vm_offset_t stack_privilege;/* reserved kernel stack */
  97. /* Swapping information */
  98. continuation_t swap_func; /* start here after swapin */
  99. /* Blocking information */
  100. event_t wait_event; /* event we are waiting on */
  101. int suspend_count; /* internal use only */
  102. kern_return_t wait_result; /* outcome of wait -
  103. may be examined by this thread
  104. WITHOUT locking */
  105. /* Defined above */
  106. /* boolean_t wake_active; someone is waiting for this
  107. thread to become suspended */
  108. /* int state; Thread state: */
  109. /*
  110. * Thread states [bits or'ed]
  111. */
  112. #define TH_WAIT 0x01 /* thread is queued for waiting */
  113. #define TH_SUSP 0x02 /* thread has been asked to stop */
  114. #define TH_RUN 0x04 /* thread is running or on runq */
  115. #define TH_UNINT 0x08 /* thread is waiting uninteruptibly */
  116. #define TH_HALTED 0x10 /* thread is halted at clean point ? */
  117. #define TH_SIGNALED 0x40 /* thread is signaled */
  118. #define TH_IDLE 0x80 /* thread is an idle thread */
  119. #define TH_SCHED_STATE (TH_WAIT|TH_SUSP|TH_RUN|TH_UNINT)
  120. #define TH_SWAPPED 0x0100 /* thread has no kernel stack */
  121. #define TH_SW_COMING_IN 0x0200 /* thread is waiting for kernel stack */
  122. #define TH_SWAP_STATE (TH_SWAPPED | TH_SW_COMING_IN)
  123. /* Scheduling information */
  124. int priority; /* thread's priority */
  125. int max_priority; /* maximum priority */
  126. int sched_pri; /* scheduled (computed) priority */
  127. #if MACH_FIXPRI
  128. int sched_data; /* for use by policy */
  129. int policy; /* scheduling policy */
  130. #endif /* MACH_FIXPRI */
  131. int depress_priority; /* depressed from this priority */
  132. unsigned int cpu_usage; /* exp. decaying cpu usage [%cpu] */
  133. unsigned int sched_usage; /* load-weighted cpu usage [sched] */
  134. unsigned int sched_stamp; /* last time priority was updated */
  135. /* VM global variables */
  136. vm_offset_t recover; /* page fault recovery (copyin/out) */
  137. unsigned int vm_privilege; /* Can use reserved memory?
  138. Implemented as a counter */
  139. /* User-visible scheduling state */
  140. int user_stop_count; /* outstanding stops */
  141. /* IPC data structures */
  142. struct thread *ith_next, *ith_prev;
  143. mach_msg_return_t ith_state;
  144. union {
  145. mach_msg_size_t msize; /* max size for recvd msg */
  146. struct ipc_kmsg *kmsg; /* received message */
  147. } data;
  148. mach_port_seqno_t ith_seqno; /* seqno of recvd message */
  149. /* This queue is used only when destroying messages:
  150. it prevents nasty recursion problems when destroying one message
  151. causes other messages to be destroyed.
  152. This queue should always be empty under normal circumstances.
  153. See ipc_kmsg_destroy() for more details. */
  154. struct ipc_kmsg_queue ith_messages;
  155. decl_simple_lock_data(, ith_lock_data)
  156. struct ipc_port *ith_self; /* not a right, doesn't hold ref */
  157. struct ipc_port *ith_sself; /* a send right */
  158. struct ipc_port *ith_exception; /* a send right */
  159. mach_port_t ith_mig_reply; /* reply port for mig */
  160. struct ipc_port *ith_rpc_reply; /* reply port for kernel RPCs */
  161. /* State saved when thread's stack is discarded */
  162. union {
  163. struct {
  164. mach_msg_header_t *msg;
  165. mach_msg_option_t option;
  166. mach_msg_size_t rcv_size;
  167. mach_msg_timeout_t timeout;
  168. mach_port_t notify;
  169. struct ipc_object *object;
  170. struct ipc_mqueue *mqueue;
  171. } receive;
  172. struct {
  173. struct ipc_port *port;
  174. int exc;
  175. int code;
  176. int subcode;
  177. } exception;
  178. void *other; /* catch-all for other state */
  179. } saved;
  180. /* Timing data structures */
  181. timer_data_t user_timer; /* user mode timer */
  182. timer_data_t system_timer; /* system mode timer */
  183. timer_save_data_t user_timer_save; /* saved user timer value */
  184. timer_save_data_t system_timer_save; /* saved sys timer val. */
  185. unsigned int cpu_delta; /* cpu usage since last update */
  186. unsigned int sched_delta; /* weighted cpu usage since update */
  187. /* Creation time stamp */
  188. time_value_t creation_time;
  189. /* Time-outs */
  190. timer_elt_data_t timer; /* timer for thread */
  191. timer_elt_data_t depress_timer; /* timer for priority depression */
  192. /* Ast/Halt data structures */
  193. /* Defined above */
  194. /* boolean_t active; how alive is the thread */
  195. int ast; /* ast's needed. See ast.h */
  196. /* Processor data structures */
  197. processor_set_t processor_set; /* assigned processor set */
  198. processor_t bound_processor; /* bound to processor ?*/
  199. sample_control_t pc_sample;
  200. #if MACH_HOST
  201. boolean_t may_assign; /* may assignment change? */
  202. boolean_t assign_active; /* someone waiting for may_assign */
  203. #endif /* MACH_HOST */
  204. #if NCPUS > 1
  205. processor_t last_processor; /* processor this last ran on */
  206. #endif /* NCPUS > 1 */
  207. };
  208. /* typedef of thread_t is in kern/kern_types.h */
  209. typedef struct thread_shuttle *thread_shuttle_t;
  210. #define THREAD_NULL ((thread_t) 0)
  211. #define THREAD_SHUTTLE_NULL ((thread_shuttle_t)0)
  212. #define ith_msize data.msize
  213. #define ith_kmsg data.kmsg
  214. #define ith_wait_result wait_result
  215. #define ith_msg saved.receive.msg
  216. #define ith_option saved.receive.option
  217. #define ith_rcv_size saved.receive.rcv_size
  218. #define ith_timeout saved.receive.timeout
  219. #define ith_notify saved.receive.notify
  220. #define ith_object saved.receive.object
  221. #define ith_mqueue saved.receive.mqueue
  222. #define ith_port saved.exception.port
  223. #define ith_exc saved.exception.exc
  224. #define ith_exc_code saved.exception.code
  225. #define ith_exc_subcode saved.exception.subcode
  226. #define ith_other saved.other
  227. #ifndef _KERN_KERN_TYPES_H_
  228. typedef struct thread *thread_t;
  229. #define THREAD_NULL ((thread_t) 0)
  230. typedef mach_port_t *thread_array_t;
  231. #endif /* _KERN_KERN_TYPES_H_ */
  232. //extern thread_t active_threads[NCPUS]; /* active threads */
  233. extern vm_offset_t active_stacks[NCPUS]; /* active kernel stacks */
  234. #ifdef KERNEL
  235. /*
  236. * User routines
  237. */
  238. extern kern_return_t thread_create(
  239. task_t parent_task,
  240. thread_t *child_thread);
  241. extern kern_return_t thread_terminate(
  242. thread_t thread);
  243. extern kern_return_t thread_terminate_release(
  244. thread_t thread,
  245. task_t task,
  246. mach_port_t thread_name,
  247. mach_port_t reply_port,
  248. vm_offset_t address,
  249. vm_size_t size);
  250. extern kern_return_t thread_suspend(
  251. thread_t thread);
  252. extern kern_return_t thread_resume(
  253. thread_t thread);
  254. extern kern_return_t thread_abort(
  255. thread_t thread);
  256. extern void thread_start(
  257. thread_t thread,
  258. continuation_t start);
  259. /* conflicts with linux 4.9.117
  260. extern thread_t (
  261. task_t task,
  262. continuation_t start,
  263. void *arg);*/
  264. extern kern_return_t thread_priority(
  265. thread_t thread,
  266. int priority,
  267. boolean_t set_max);
  268. extern void thread_set_own_priority(
  269. int priority);
  270. extern kern_return_t thread_max_priority(
  271. thread_t thread,
  272. processor_set_t pset,
  273. int max_priority);
  274. extern kern_return_t thread_policy(
  275. thread_t thread,
  276. int policy,
  277. int data);
  278. extern void consider_thread_collect(
  279. void);
  280. extern void stack_privilege(
  281. thread_t thread);
  282. extern kern_return_t thread_get_state(
  283. thread_t thread,
  284. int flavor,
  285. thread_state_t old_state,
  286. natural_t *old_state_count);
  287. extern kern_return_t thread_set_state(
  288. thread_t thread,
  289. int flavor,
  290. thread_state_t new_state,
  291. natural_t new_state_count);
  292. extern kern_return_t thread_get_special_port(
  293. thread_t thread,
  294. int which,
  295. struct ipc_port **portp);
  296. extern kern_return_t thread_set_special_port(
  297. thread_t thread,
  298. int which,
  299. struct ipc_port *port);
  300. extern kern_return_t thread_info(
  301. thread_t thread,
  302. int flavor,
  303. thread_info_t thread_info_out,
  304. natural_t *thread_info_count);
  305. extern kern_return_t thread_assign(
  306. thread_t thread,
  307. processor_set_t new_pset);
  308. extern kern_return_t thread_assign_default(
  309. thread_t thread);
  310. extern void stack_collect(void);
  311. #endif
  312. /*
  313. * Kernel-only routines
  314. */
  315. extern void thread_init(void);
  316. extern void thread_reference(thread_t);
  317. extern void thread_deallocate(thread_t);
  318. extern void thread_hold(thread_t);
  319. extern kern_return_t thread_dowait(
  320. thread_t thread,
  321. boolean_t must_halt);
  322. extern void thread_release(thread_t);
  323. extern kern_return_t thread_halt(
  324. thread_t thread,
  325. boolean_t must_halt);
  326. extern void thread_halt_self(continuation_t);
  327. extern void thread_force_terminate(thread_t);
  328. /* conflicts with linux 4.9.117
  329. extern thread_t kernel_thread(
  330. task_t task,
  331. void (*start)(void),
  332. void * arg);*/
  333. extern void reaper_thread(void) __attribute__((noreturn));
  334. #if MACH_HOST
  335. extern void thread_freeze(
  336. thread_t thread);
  337. extern void thread_doassign(
  338. thread_t thread,
  339. processor_set_t new_pset,
  340. boolean_t release_freeze);
  341. extern void thread_unfreeze(
  342. thread_t thread);
  343. #endif /* MACH_HOST */
  344. /*
  345. * Macro-defined routines
  346. */
  347. #define thread_pcb(th) ((th)->pcb)
  348. #define thread_lock(th) simple_lock(&(th)->lock)
  349. #define thread_unlock(th) simple_unlock(&(th)->lock)
  350. #define thread_should_halt(thread) \
  351. ((thread)->ast & (AST_HALT|AST_TERMINATE))
  352. /*
  353. * Machine specific implementations of the current thread macro
  354. * designate this by defining CURRENT_THREAD.
  355. */
  356. extern thread_t gnumach_current_thread(void);
  357. #define current_thread gnumach_current_thread
  358. void gnumach_set_current_thread(thread_t thread);
  359. #define current_stack() (__panic__)
  360. #define current_task() (current_thread()->task)
  361. #define current_space() (current_task()->itk_space)
  362. #define current_map() (current_task()->map)
  363. #if MACH_DEBUG
  364. void stack_init(vm_offset_t stack);
  365. void stack_finalize(vm_offset_t stack);
  366. #endif /* MACH_DEBUG */
  367. #endif /* _KERN_THREAD_H_ */