xref: /xnu-11417.140.69/osfmk/kern/thread_group.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Thread group support routines.
31  */
32 #ifndef _KERN_THREAD_GROUP_H_
33 #define _KERN_THREAD_GROUP_H_
34 
35 struct thread_group;
36 
37 #include <mach/thread_status.h> /* for proc_reg.h / CONFIG_THREAD_GROUPS */
38 
39 #ifndef CONFIG_THREAD_GROUPS
40 #error "The platform must define CONFIG_THREAD_GROUPS to 0 or 1"
41 #endif
42 
43 #if CONFIG_THREAD_GROUPS
44 
45 #if XNU_KERNEL_PRIVATE
46 /*
47  * In order to include this header file into test files such as
48  * tests/sched/thread_group_flags.c, use the KERNEL guard to remove includes
49  * which we do not want to compile as part of the userspace tests.
50  */
51 #endif /* XNU_KERNEL_PRIVATE */
52 
53 #if KERNEL
54 #include <kern/assert.h>
55 #include <kern/queue.h>
56 #include <machine/machine_routines.h>
57 #endif /* KERNEL */
58 
59 #define THREAD_GROUP_MAX                (CONFIG_TASK_MAX + 10)
60 #define THREAD_GROUP_MAXNAME            (32)
61 
62 #define THREAD_GROUP_SYSTEM          0       // kernel (-VM) + launchd
63 #define THREAD_GROUP_BACKGROUND      1       // background daemons
64 #define THREAD_GROUP_VM              3       // kernel VM threads
65 #define THREAD_GROUP_IO_STORAGE      4       // kernel io storage threads
66 #define THREAD_GROUP_PERF_CONTROLLER 5       // kernel CLPC threads
67 
68 #define THREAD_GROUP_INVALID    UINT64_MAX
69 
70 /* Thread group flags */
71 
72 /* Flags in the bottom 8 bits are exclusive. */
73 #define THREAD_GROUP_EXCLUSIVE_FLAGS_MASK 0xff
74 
75 /* Exclusive flags */
76 #define THREAD_GROUP_FLAGS_DEFAULT      0x0
77 #define THREAD_GROUP_FLAGS_EFFICIENT    0x1
78 #define THREAD_GROUP_FLAGS_APPLICATION  0x2
79 #define THREAD_GROUP_FLAGS_CRITICAL     0x4
80 #define THREAD_GROUP_FLAGS_BEST_EFFORT  0x8
81 
82 /* Shared flags */
83 #define THREAD_GROUP_FLAGS_UI_APP               0x100
84 #define THREAD_GROUP_FLAGS_MANAGED              0x200
85 #define THREAD_GROUP_FLAGS_STRICT_TIMERS        0x400
86 #define THREAD_GROUP_FLAGS_GAME_MODE            0x800
87 #define THREAD_GROUP_FLAGS_CARPLAY_MODE         0x1000
88 
89 #if defined(XNU_TARGET_OS_XR)
90 /* Keep around for backwards compatibility. */
91 #define THREAD_GROUP_FLAGS_UXM          0x200
92 #endif /* XNU_TARGET_OS_XR */
93 
94 #define THREAD_GROUP_FLAGS_EXCLUSIVE ( \
95     THREAD_GROUP_FLAGS_EFFICIENT |     \
96     THREAD_GROUP_FLAGS_APPLICATION |   \
97     THREAD_GROUP_FLAGS_CRITICAL |      \
98     THREAD_GROUP_FLAGS_BEST_EFFORT)
99 
100 #define THREAD_GROUP_FLAGS_ABSENT 0xffffffff
101 
102 static_assert(
103 	(THREAD_GROUP_FLAGS_EXCLUSIVE & ~THREAD_GROUP_EXCLUSIVE_FLAGS_MASK) == 0,
104 	"valid exclusive flags");
105 
106 /* Shared flags */
107 #define THREAD_GROUP_FLAGS_SHARED ( \
108     THREAD_GROUP_FLAGS_UI_APP |     \
109     THREAD_GROUP_FLAGS_MANAGED |    \
110     THREAD_GROUP_FLAGS_STRICT_TIMERS | \
111     THREAD_GROUP_FLAGS_GAME_MODE | \
112     THREAD_GROUP_FLAGS_CARPLAY_MODE)
113 
114 static_assert(
115 	(THREAD_GROUP_FLAGS_SHARED & THREAD_GROUP_EXCLUSIVE_FLAGS_MASK) == 0,
116 	"valid shared flags");
117 
118 __BEGIN_DECLS
119 
120 void            thread_group_init(void);
121 void            thread_group_resync(boolean_t create);
122 struct thread_group *thread_group_create_and_retain(uint32_t flags);
123 void            thread_group_init_thread(thread_t t, task_t task);
124 void            thread_group_set_name(struct thread_group *tg, const char *name);
125 void            thread_group_flags_update_lock(void);
126 void            thread_group_flags_update_unlock(void);
127 void            thread_group_set_flags(struct thread_group *tg, uint32_t flags);
128 uint32_t        thread_group_get_flags(struct thread_group *);
129 void            thread_group_clear_flags(struct thread_group *tg, uint32_t flags);
130 void            thread_group_set_flags_locked(struct thread_group *tg, uint32_t flags);
131 void            thread_group_clear_flags_locked(struct thread_group *tg, uint32_t flags);
132 boolean_t       thread_group_valid_flags(uint32_t flags);
133 struct thread_group *thread_group_find_by_name_and_retain(char *name);
134 struct thread_group *thread_group_find_by_id_and_retain(uint64_t id);
135 struct thread_group *thread_group_retain(struct thread_group *tg);
136 void            thread_group_release(struct thread_group *tg);
137 void            thread_group_release_live(struct thread_group *tg);
138 void            thread_group_deallocate_safe(struct thread_group *tg);
139 struct thread_group *thread_group_get(thread_t t);
140 struct thread_group *thread_group_get_home_group(thread_t t);
141 void            thread_group_set_bank(thread_t t, struct thread_group *tg);
142 uint64_t        thread_group_get_id(struct thread_group *tg);
143 uint32_t        thread_group_count(void);
144 const char *    thread_group_get_name(struct thread_group *tg);
145 void *          thread_group_get_machine_data(struct thread_group *tg);
146 uint32_t        thread_group_machine_data_size(void);
147 boolean_t       thread_group_uses_immediate_ipi(struct thread_group *tg);
148 cluster_type_t  thread_group_recommendation(struct thread_group *tg);
149 
150 typedef         void (*thread_group_iterate_fn_t)(void*, int, struct thread_group *);
151 kern_return_t   thread_group_iterate_stackshot(thread_group_iterate_fn_t callout, void *arg);
152 void            thread_group_update_recommendation(struct thread_group *tg, cluster_type_t new_recommendation);
153 uint64_t        thread_group_id(struct thread_group *tg);
154 
155 void            thread_set_work_interval_thread_group(thread_t t, struct thread_group *tg);
156 #if CONFIG_SCHED_AUTO_JOIN
157 void            thread_set_autojoin_thread_group_locked(thread_t t, struct thread_group *tg);
158 #endif
159 #if CONFIG_PREADOPT_TG
160 void thread_set_preadopt_thread_group(thread_t t, struct thread_group *tg);
161 #endif
162 
163 void             thread_resolve_and_enforce_thread_group_hierarchy_if_needed(thread_t t);
164 
165 #if XNU_KERNEL_PRIVATE
166 void            thread_group_vm_add(void);
167 #endif
168 
169 __END_DECLS
170 
171 #endif /* CONFIG_THREAD_GROUPS */
172 
173 #endif // _KERN_THREAD_GROUP_H_
174