1/*
2 * Copyright (c) 2012 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#ifdef KERNEL_PRIVATE
30
31#ifndef _PTHREAD_SHIMS_H_
32#define _PTHREAD_SHIMS_H_
33
34#ifndef ASSEMBLER
35
36#include <kern/clock.h>
37#include <kern/kern_types.h>
38#include <kern/locks.h>
39#include <sys/_types.h>
40#include <sys/_types/_sigset_t.h>
41#include <sys/kernel_types.h>
42#include <sys/proc_info.h>
43
44#ifndef PTHREAD_INTERNAL
45struct uthread;
46#define M_PROC 41
47#endif
48
49#ifdef NEEDS_SCHED_CALL_T
50typedef void (*sched_call_t)(int type, thread_t thread);
51#endif
52
53typedef struct pthread_functions_s {
54	int version;
55
56	/* internal calls, kernel core -> kext */
57	void (*pthread_init)(void);
58	int (*fill_procworkqueue)(proc_t p, struct proc_workqueueinfo * pwqinfo);
59	void (*workqueue_init_lock)(proc_t p);
60	void (*workqueue_destroy_lock)(proc_t p);
61	void (*workqueue_exit)(struct proc *p);
62	void (*workqueue_mark_exiting)(struct proc *p);
63	void (*workqueue_thread_yielded)(void);
64	void (*pth_proc_hashinit)(proc_t p);
65	void (*pth_proc_hashdelete)(proc_t p);
66
67	/* syscall stubs */
68	int (*bsdthread_create)(struct proc *p, user_addr_t user_func, user_addr_t user_funcarg, user_addr_t user_stack, user_addr_t user_pthread, uint32_t flags, user_addr_t *retval);
69	int (*bsdthread_register)(struct proc *p, user_addr_t threadstart, user_addr_t wqthread, int pthsize, user_addr_t dummy_value, user_addr_t targetconc_ptr, uint64_t dispatchqueue_offset, int32_t *retval);
70	int (*bsdthread_terminate)(struct proc *p, user_addr_t stackaddr, size_t size, uint32_t kthport, uint32_t sem, int32_t *retval);
71	int (*thread_selfid)(struct proc *p, uint64_t *retval);
72	int (*workq_kernreturn)(struct proc *p, int options, user_addr_t item, int affinity, int prio, int32_t *retval);
73	int (*workq_open)(struct proc *p, int32_t *retval);
74
75	/* psynch syscalls */
76	int (*psynch_mutexwait)(proc_t p, user_addr_t mutex,  uint32_t mgen, uint32_t  ugen, uint64_t tid, uint32_t flags, uint32_t *retval);
77	int (*psynch_mutexdrop)(proc_t p, user_addr_t mutex,  uint32_t mgen, uint32_t  ugen, uint64_t tid, uint32_t flags, uint32_t *retval);
78	int (*psynch_cvbroad)(proc_t p, user_addr_t cv, uint64_t cvlsgen, uint64_t cvudgen, uint32_t flags, user_addr_t mutex, uint64_t mugen, uint64_t tid, uint32_t *retval);
79	int (*psynch_cvsignal)(proc_t p, user_addr_t cv, uint64_t cvlsgen, uint32_t cvugen, int thread_port, user_addr_t mutex, uint64_t mugen, uint64_t tid, uint32_t flags, uint32_t *retval);
80	int (*psynch_cvwait)(proc_t p, user_addr_t cv, uint64_t cvlsgen, uint32_t cvugen, user_addr_t mutex,  uint64_t mugen, uint32_t flags, int64_t sec, uint32_t nsec, uint32_t * retval);
81	int (*psynch_cvclrprepost)(proc_t p, user_addr_t cv, uint32_t cvgen, uint32_t cvugen, uint32_t cvsgen, uint32_t prepocnt, uint32_t preposeq, uint32_t flags, int *retval);
82	int (*psynch_rw_longrdlock)(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
83	int (*psynch_rw_rdlock)(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
84	int (*psynch_rw_unlock)(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
85	int (*psynch_rw_wrlock)(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
86	int (*psynch_rw_yieldwrlock)(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
87
88	sched_call_t (*workqueue_get_sched_callback)(void);
89
90	/* padding for future */
91	void* _pad[99];
92} *pthread_functions_t;
93
94typedef struct pthread_callbacks_s {
95	int version;
96
97	/* config information */
98	uint32_t config_thread_max;
99	uint32_t (*get_task_threadmax)(void);
100
101	/* proc.h accessors */
102	uint64_t (*proc_get_register)(struct proc *t);
103	void (*proc_set_register)(struct proc *t);
104
105	user_addr_t (*proc_get_threadstart)(struct proc *t);
106	void (*proc_set_threadstart)(struct proc *t, user_addr_t addr);
107	user_addr_t (*proc_get_wqthread)(struct proc *t);
108	void (*proc_set_wqthread)(struct proc *t, user_addr_t addr);
109	int (*proc_get_pthsize)(struct proc *t);
110	void (*proc_set_pthsize)(struct proc *t, int size);
111	user_addr_t (*proc_get_targconc)(struct proc *t);
112	void (*proc_set_targconc)(struct proc *t, user_addr_t addr);
113	uint64_t (*proc_get_dispatchqueue_offset)(struct proc *t);
114	void (*proc_set_dispatchqueue_offset)(struct proc *t, uint64_t offset);
115	lck_spin_t* (*proc_get_wqlockptr)(struct proc *t);
116	boolean_t* (*proc_get_wqinitingptr)(struct proc *t);
117	void* (*proc_get_wqptr)(struct proc *t);
118	void (*proc_set_wqptr)(struct proc *t, void* ptr);
119	int (*proc_get_wqsize)(struct proc *t);
120	void (*proc_set_wqsize)(struct proc *t, int sz);
121	void (*proc_lock)(struct proc *t);
122	void (*proc_unlock)(struct proc *t);
123	task_t (*proc_get_task)(struct proc *t);
124	void* (*proc_get_pthhash)(struct proc *t);
125	void (*proc_set_pthhash)(struct proc *t, void* ptr);
126
127	/* bsd/sys/user.h */
128	void* (*uthread_get_threadlist)(struct uthread *t);
129	void (*uthread_set_threadlist)(struct uthread *t, void* threadlist);
130	sigset_t (*uthread_get_sigmask)(struct uthread *t);
131	void (*uthread_set_sigmask)(struct uthread *t, sigset_t s);
132	void* (*uthread_get_uukwe)(struct uthread *t);
133	int (*uthread_get_returnval)(struct uthread *t);
134	void (*uthread_set_returnval)(struct uthread *t, int val);
135	int (*uthread_is_cancelled)(struct uthread *t);
136
137	/* vm_protos.h calls */
138	ipc_space_t (*task_get_ipcspace)(task_t t);
139	mach_port_name_t (*ipc_port_copyout_send)(ipc_port_t sright, ipc_space_t space);
140
141	/* osfmk/vm/vm_map.h */
142	kern_return_t (*vm_map_page_info)(vm_map_t map, vm_map_offset_t offset, vm_page_info_flavor_t flavor, vm_page_info_t info, mach_msg_type_number_t *count);
143	vm_map_t (*vm_map_switch)(vm_map_t map);
144
145	/* wq functions */
146	kern_return_t (*thread_set_wq_state32)(thread_t thread, thread_state_t state);
147	kern_return_t (*thread_set_wq_state64)(thread_t thread, thread_state_t state);
148
149	/* sched_prim.h */
150	void (*thread_exception_return)();
151	void (*thread_bootstrap_return)();
152
153	/* kern/clock.h */
154	void (*absolutetime_to_microtime)(uint64_t abstime, clock_sec_t *secs, clock_usec_t *microsecs);
155
156	/* osfmk/kern/task.h */
157	int (*proc_restore_workq_bgthreadpolicy)(thread_t t);
158	int (*proc_apply_workq_bgthreadpolicy)(thread_t t);
159
160	/* osfmk/kern/thread.h */
161	struct uthread* (*get_bsdthread_info)(thread_t th);
162	void (*thread_sched_call)(thread_t t, sched_call_t call);
163	void (*thread_static_param)(thread_t t, boolean_t state);
164	kern_return_t (*thread_create_workq)(task_t t, thread_continue_t c, thread_t *new_t);
165	kern_return_t (*thread_policy_set_internal)(thread_t t, thread_policy_flavor_t flavour, thread_policy_t info, mach_msg_type_number_t count);
166
167	/* osfmk/kern/affinity.h */
168	kern_return_t (*thread_affinity_set)(thread_t thread, uint32_t tag);
169
170	/* bsd/sys/systm.h */
171	void (*unix_syscall_return)(int error);
172
173	/* osfmk/kern/zalloc.h */
174	void* (*zalloc)(zone_t zone);
175	void (*zfree)(zone_t zone, void* ptr);
176	zone_t (*zinit)(vm_size_t, vm_size_t maxmem, vm_size_t alloc, const char *name);
177
178	/* bsd/kerb/kern_sig.c */
179	void (*__pthread_testcancel)(int);
180
181	/* calls without portfolio */
182	kern_return_t (*mach_port_deallocate)(ipc_space_t space, mach_port_name_t name);
183	kern_return_t (*semaphore_signal_internal_trap)(mach_port_name_t sema_name);
184	vm_map_t (*current_map)(void);
185
186	/* osfmk/kern/thread.h */
187	ipc_port_t (*convert_thread_to_port)(thread_t th);
188
189	/* mach/task.h */
190	kern_return_t (*thread_create)(task_t parent_task, thread_act_t *child_act);
191
192	/* mach/thread_act.h */
193	kern_return_t (*thread_resume)(thread_act_t target_act);
194
195	/* osfmk/<arch>/machine_routines.h */
196	int (*ml_get_max_cpus)(void);
197
198
199	/* <rdar://problem/12809089> xnu: struct proc p_dispatchqueue_serialno_offset additions */
200	uint64_t (*proc_get_dispatchqueue_serialno_offset)(struct proc *p);
201	void (*proc_set_dispatchqueue_serialno_offset)(struct proc *p, uint64_t offset);
202
203	/* padding for future */
204	void* _pad[98];
205
206} *pthread_callbacks_t;
207
208void
209pthread_kext_register(pthread_functions_t fns, pthread_callbacks_t *callbacks);
210
211#ifdef BSD_KERNEL_PRIVATE
212void workqueue_mark_exiting(struct proc *);
213void workqueue_exit(struct proc *);
214void workqueue_thread_yielded(void);
215sched_call_t workqueue_get_sched_callback(void);
216void pthread_init(void);
217
218extern pthread_callbacks_t pthread_kern;
219extern pthread_functions_t pthread_functions;
220#endif
221
222#endif /* ASSEMBLER */
223#endif /* _PTHREAD_SHIMS_H_ */
224#endif /* KERNEL_PRIVATE */
225