xref: /xnu-10002.61.3/bsd/pthread/workqueue_syscalls.h (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions  * Copyright (c) 2017 Apple, Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions  *
4*0f4c859eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions  *
6*0f4c859eSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*0f4c859eSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*0f4c859eSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*0f4c859eSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*0f4c859eSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*0f4c859eSApple OSS Distributions  *
15*0f4c859eSApple OSS Distributions  * Please obtain a copy of the License at
16*0f4c859eSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*0f4c859eSApple OSS Distributions  *
18*0f4c859eSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*0f4c859eSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*0f4c859eSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*0f4c859eSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*0f4c859eSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*0f4c859eSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*0f4c859eSApple OSS Distributions  * limitations under the License.
25*0f4c859eSApple OSS Distributions  *
26*0f4c859eSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*0f4c859eSApple OSS Distributions  */
28*0f4c859eSApple OSS Distributions 
29*0f4c859eSApple OSS Distributions #ifndef _PTHREAD_WORKQUEUE_PRIVATE_H_
30*0f4c859eSApple OSS Distributions #define _PTHREAD_WORKQUEUE_PRIVATE_H_
31*0f4c859eSApple OSS Distributions 
32*0f4c859eSApple OSS Distributions #include <stdint.h>
33*0f4c859eSApple OSS Distributions 
34*0f4c859eSApple OSS Distributions #if XNU_KERNEL_PRIVATE && !defined(__PTHREAD_EXPOSE_INTERNALS__)
35*0f4c859eSApple OSS Distributions #define __PTHREAD_EXPOSE_INTERNALS__ 1
36*0f4c859eSApple OSS Distributions #endif // XNU_KERNEL_PRIVATE
37*0f4c859eSApple OSS Distributions 
38*0f4c859eSApple OSS Distributions #ifdef __PTHREAD_EXPOSE_INTERNALS__
39*0f4c859eSApple OSS Distributions /* workq_kernreturn commands */
40*0f4c859eSApple OSS Distributions #define WQOPS_THREAD_RETURN              0x004 /* parks the thread back into the kernel */
41*0f4c859eSApple OSS Distributions #define WQOPS_QUEUE_NEWSPISUPP           0x010 /* this is to check for newer SPI support */
42*0f4c859eSApple OSS Distributions #define WQOPS_QUEUE_REQTHREADS           0x020 /* request number of threads of a prio */
43*0f4c859eSApple OSS Distributions #define WQOPS_QUEUE_REQTHREADS2          0x030 /* request a number of threads in a given priority bucket */
44*0f4c859eSApple OSS Distributions #define WQOPS_THREAD_KEVENT_RETURN       0x040 /* parks the thread after delivering the passed kevent array */
45*0f4c859eSApple OSS Distributions #define WQOPS_SET_EVENT_MANAGER_PRIORITY 0x080 /* max() in the provided priority in the the priority of the event manager */
46*0f4c859eSApple OSS Distributions #define WQOPS_THREAD_WORKLOOP_RETURN     0x100 /* parks the thread after delivering the passed kevent array */
47*0f4c859eSApple OSS Distributions #define WQOPS_SHOULD_NARROW              0x200 /* checks whether we should narrow our concurrency */
48*0f4c859eSApple OSS Distributions #define WQOPS_SETUP_DISPATCH             0x400 /* setup pthread workqueue-related operations */
49*0f4c859eSApple OSS Distributions 
50*0f4c859eSApple OSS Distributions /* flag values for upcall flags field, only 8 bits per struct threadlist */
51*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_PRIO_SCHED               0x00008000
52*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_PRIO_QOS                 0x00004000
53*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_PRIO_MASK                0x00000fff
54*0f4c859eSApple OSS Distributions 
55*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_OVERCOMMIT               0x00010000  /* thread is with overcommit prio */
56*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_REUSE                    0x00020000  /* thread is being reused */
57*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_NEWSPI                   0x00040000  /* the call is with new SPIs */
58*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_KEVENT                   0x00080000  /* thread is response to kevent req */
59*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_EVENT_MANAGER            0x00100000  /* event manager thread */
60*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_TSD_BASE_SET             0x00200000  /* tsd base has already been set */
61*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_WORKLOOP                 0x00400000  /* workloop thread */
62*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_OUTSIDEQOS               0x00800000  /* thread qos changes should not be sent to kernel */
63*0f4c859eSApple OSS Distributions #define WQ_FLAG_THREAD_COOPERATIVE              0x01000000  /* thread is part of cooperative thread pool */
64*0f4c859eSApple OSS Distributions 
65*0f4c859eSApple OSS Distributions #define WQ_KEVENT_LIST_LEN  16 // WORKQ_KEVENT_EVENT_BUFFER_LEN
66*0f4c859eSApple OSS Distributions #define WQ_KEVENT_DATA_SIZE (32 * 1024)
67*0f4c859eSApple OSS Distributions 
68*0f4c859eSApple OSS Distributions /* kqueue_workloop_ctl commands */
69*0f4c859eSApple OSS Distributions #define KQ_WORKLOOP_CREATE                              0x01
70*0f4c859eSApple OSS Distributions #define KQ_WORKLOOP_DESTROY                             0x02
71*0f4c859eSApple OSS Distributions 
72*0f4c859eSApple OSS Distributions /* indicate which fields of kq_workloop_create params are valid */
73*0f4c859eSApple OSS Distributions #define KQ_WORKLOOP_CREATE_SCHED_PRI    0x01
74*0f4c859eSApple OSS Distributions #define KQ_WORKLOOP_CREATE_SCHED_POL    0x02
75*0f4c859eSApple OSS Distributions #define KQ_WORKLOOP_CREATE_CPU_PERCENT  0x04
76*0f4c859eSApple OSS Distributions 
77*0f4c859eSApple OSS Distributions struct kqueue_workloop_params {
78*0f4c859eSApple OSS Distributions 	int kqwlp_version;
79*0f4c859eSApple OSS Distributions 	int kqwlp_flags;
80*0f4c859eSApple OSS Distributions 	uint64_t kqwlp_id;
81*0f4c859eSApple OSS Distributions 	int kqwlp_sched_pri;
82*0f4c859eSApple OSS Distributions 	int kqwlp_sched_pol;
83*0f4c859eSApple OSS Distributions 	int kqwlp_cpu_percent;
84*0f4c859eSApple OSS Distributions 	int kqwlp_cpu_refillms;
85*0f4c859eSApple OSS Distributions } __attribute__((packed));
86*0f4c859eSApple OSS Distributions 
87*0f4c859eSApple OSS Distributions _Static_assert(offsetof(struct kqueue_workloop_params, kqwlp_version) == 0,
88*0f4c859eSApple OSS Distributions     "kqwlp_version should be first");
89*0f4c859eSApple OSS Distributions 
90*0f4c859eSApple OSS Distributions int
91*0f4c859eSApple OSS Distributions __workq_open(void);
92*0f4c859eSApple OSS Distributions 
93*0f4c859eSApple OSS Distributions int
94*0f4c859eSApple OSS Distributions __workq_kernreturn(int op, void *arg2, int arg3, int arg4);
95*0f4c859eSApple OSS Distributions 
96*0f4c859eSApple OSS Distributions int
97*0f4c859eSApple OSS Distributions __kqueue_workloop_ctl(uintptr_t cmd, uint64_t options, void *addr, size_t sz);
98*0f4c859eSApple OSS Distributions 
99*0f4c859eSApple OSS Distributions /* SPI flags between WQ and workq_setup_thread in pthread.kext */
100*0f4c859eSApple OSS Distributions #define WQ_SETUP_NONE           0
101*0f4c859eSApple OSS Distributions #define WQ_SETUP_FIRST_USE      1
102*0f4c859eSApple OSS Distributions #define WQ_SETUP_CLEAR_VOUCHER  2
103*0f4c859eSApple OSS Distributions // was  WQ_SETUP_SET_SCHED_CALL 4
104*0f4c859eSApple OSS Distributions #define WQ_SETUP_EXIT_THREAD    8
105*0f4c859eSApple OSS Distributions 
106*0f4c859eSApple OSS Distributions #endif // __PTHREAD_EXPOSE_INTERNALS__
107*0f4c859eSApple OSS Distributions 
108*0f4c859eSApple OSS Distributions #define WORKQ_DISPATCH_CONFIG_VERSION        2
109*0f4c859eSApple OSS Distributions #define WORKQ_DISPATCH_MIN_SUPPORTED_VERSION 1
110*0f4c859eSApple OSS Distributions #define WORKQ_DISPATCH_SUPPORTED_FLAGS       0
111*0f4c859eSApple OSS Distributions struct workq_dispatch_config {
112*0f4c859eSApple OSS Distributions 	uint32_t wdc_version;
113*0f4c859eSApple OSS Distributions 	uint32_t wdc_flags;
114*0f4c859eSApple OSS Distributions 	uint64_t wdc_queue_serialno_offs;
115*0f4c859eSApple OSS Distributions 	uint64_t wdc_queue_label_offs;
116*0f4c859eSApple OSS Distributions } __attribute__((packed, aligned(4)));
117*0f4c859eSApple OSS Distributions 
118*0f4c859eSApple OSS Distributions #endif // _PTHREAD_WORKQUEUE_PRIVATE_H_
119