xref: /xnu-8796.121.2/bsd/dev/dtrace/dtrace_glue.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2005-2021 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions #include <kern/thread.h>
30*c54f35caSApple OSS Distributions 
31*c54f35caSApple OSS Distributions #include <sys/time.h>
32*c54f35caSApple OSS Distributions #include <sys/proc.h>
33*c54f35caSApple OSS Distributions #include <sys/kauth.h>
34*c54f35caSApple OSS Distributions #include <sys/user.h>
35*c54f35caSApple OSS Distributions #include <sys/systm.h>
36*c54f35caSApple OSS Distributions #include <sys/dtrace.h>
37*c54f35caSApple OSS Distributions #include <sys/dtrace_impl.h>
38*c54f35caSApple OSS Distributions #include <machine/atomic.h>
39*c54f35caSApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
40*c54f35caSApple OSS Distributions #include <kern/kern_types.h>
41*c54f35caSApple OSS Distributions #include <kern/timer_call.h>
42*c54f35caSApple OSS Distributions #include <kern/thread_call.h>
43*c54f35caSApple OSS Distributions #include <kern/task.h>
44*c54f35caSApple OSS Distributions #include <kern/sched_prim.h>
45*c54f35caSApple OSS Distributions #include <miscfs/devfs/devfs.h>
46*c54f35caSApple OSS Distributions #include <kern/kalloc.h>
47*c54f35caSApple OSS Distributions 
48*c54f35caSApple OSS Distributions #include <mach/vm_param.h>
49*c54f35caSApple OSS Distributions #include <mach/mach_vm.h>
50*c54f35caSApple OSS Distributions #include <mach/task.h>
51*c54f35caSApple OSS Distributions #include <vm/vm_map.h> /* All the bits we care about are guarded by MACH_KERNEL_PRIVATE :-( */
52*c54f35caSApple OSS Distributions 
53*c54f35caSApple OSS Distributions /*
54*c54f35caSApple OSS Distributions  * pid/proc
55*c54f35caSApple OSS Distributions  */
56*c54f35caSApple OSS Distributions /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
57*c54f35caSApple OSS Distributions #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
58*c54f35caSApple OSS Distributions 
59*c54f35caSApple OSS Distributions KALLOC_HEAP_DEFINE(KHEAP_DTRACE, "dtrace", KHEAP_ID_DEFAULT);
60*c54f35caSApple OSS Distributions 
61*c54f35caSApple OSS Distributions void
dtrace_sprlock(proc_t * p)62*c54f35caSApple OSS Distributions dtrace_sprlock(proc_t *p)
63*c54f35caSApple OSS Distributions {
64*c54f35caSApple OSS Distributions 	lck_mtx_lock(&p->p_dtrace_sprlock);
65*c54f35caSApple OSS Distributions }
66*c54f35caSApple OSS Distributions 
67*c54f35caSApple OSS Distributions void
dtrace_sprunlock(proc_t * p)68*c54f35caSApple OSS Distributions dtrace_sprunlock(proc_t *p)
69*c54f35caSApple OSS Distributions {
70*c54f35caSApple OSS Distributions 	lck_mtx_unlock(&p->p_dtrace_sprlock);
71*c54f35caSApple OSS Distributions }
72*c54f35caSApple OSS Distributions 
73*c54f35caSApple OSS Distributions /* Not called from probe context */
74*c54f35caSApple OSS Distributions proc_t *
sprlock(pid_t pid)75*c54f35caSApple OSS Distributions sprlock(pid_t pid)
76*c54f35caSApple OSS Distributions {
77*c54f35caSApple OSS Distributions 	proc_t* p;
78*c54f35caSApple OSS Distributions 
79*c54f35caSApple OSS Distributions 	if ((p = proc_find(pid)) == PROC_NULL) {
80*c54f35caSApple OSS Distributions 		return PROC_NULL;
81*c54f35caSApple OSS Distributions 	}
82*c54f35caSApple OSS Distributions 
83*c54f35caSApple OSS Distributions 	task_suspend_internal(proc_task(p));
84*c54f35caSApple OSS Distributions 
85*c54f35caSApple OSS Distributions 	dtrace_sprlock(p);
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions 	return p;
88*c54f35caSApple OSS Distributions }
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions /* Not called from probe context */
91*c54f35caSApple OSS Distributions void
sprunlock(proc_t * p)92*c54f35caSApple OSS Distributions sprunlock(proc_t *p)
93*c54f35caSApple OSS Distributions {
94*c54f35caSApple OSS Distributions 	if (p != PROC_NULL) {
95*c54f35caSApple OSS Distributions 		dtrace_sprunlock(p);
96*c54f35caSApple OSS Distributions 
97*c54f35caSApple OSS Distributions 		task_resume_internal(proc_task(p));
98*c54f35caSApple OSS Distributions 
99*c54f35caSApple OSS Distributions 		proc_rele(p);
100*c54f35caSApple OSS Distributions 	}
101*c54f35caSApple OSS Distributions }
102*c54f35caSApple OSS Distributions 
103*c54f35caSApple OSS Distributions /*
104*c54f35caSApple OSS Distributions  * uread/uwrite
105*c54f35caSApple OSS Distributions  */
106*c54f35caSApple OSS Distributions 
107*c54f35caSApple OSS Distributions // These are not exported from vm_map.h.
108*c54f35caSApple OSS Distributions extern kern_return_t vm_map_read_user(vm_map_t map, vm_map_address_t src_addr, void *dst_p, vm_size_t size);
109*c54f35caSApple OSS Distributions extern kern_return_t vm_map_write_user(vm_map_t map, void *src_p, vm_map_address_t dst_addr, vm_size_t size);
110*c54f35caSApple OSS Distributions 
111*c54f35caSApple OSS Distributions /* Not called from probe context */
112*c54f35caSApple OSS Distributions int
uread(proc_t * p,void * buf,user_size_t len,user_addr_t a)113*c54f35caSApple OSS Distributions uread(proc_t *p, void *buf, user_size_t len, user_addr_t a)
114*c54f35caSApple OSS Distributions {
115*c54f35caSApple OSS Distributions 	kern_return_t ret;
116*c54f35caSApple OSS Distributions 
117*c54f35caSApple OSS Distributions 	ASSERT(p != PROC_NULL);
118*c54f35caSApple OSS Distributions 	ASSERT(proc_task(p) != NULL);
119*c54f35caSApple OSS Distributions 
120*c54f35caSApple OSS Distributions 	task_t task = proc_task(p);
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions 	/*
123*c54f35caSApple OSS Distributions 	 * Grab a reference to the task vm_map_t to make sure
124*c54f35caSApple OSS Distributions 	 * the map isn't pulled out from under us.
125*c54f35caSApple OSS Distributions 	 *
126*c54f35caSApple OSS Distributions 	 * Because the proc_lock is not held at all times on all code
127*c54f35caSApple OSS Distributions 	 * paths leading here, it is possible for the proc to have
128*c54f35caSApple OSS Distributions 	 * exited. If the map is null, fail.
129*c54f35caSApple OSS Distributions 	 */
130*c54f35caSApple OSS Distributions 	vm_map_t map = get_task_map_reference(task);
131*c54f35caSApple OSS Distributions 	if (map) {
132*c54f35caSApple OSS Distributions 		ret = vm_map_read_user( map, (vm_map_address_t)a, buf, (vm_size_t)len);
133*c54f35caSApple OSS Distributions 		vm_map_deallocate(map);
134*c54f35caSApple OSS Distributions 	} else {
135*c54f35caSApple OSS Distributions 		ret = KERN_TERMINATED;
136*c54f35caSApple OSS Distributions 	}
137*c54f35caSApple OSS Distributions 
138*c54f35caSApple OSS Distributions 	return (int)ret;
139*c54f35caSApple OSS Distributions }
140*c54f35caSApple OSS Distributions 
141*c54f35caSApple OSS Distributions 
142*c54f35caSApple OSS Distributions /* Not called from probe context */
143*c54f35caSApple OSS Distributions int
uwrite(proc_t * p,void * buf,user_size_t len,user_addr_t a)144*c54f35caSApple OSS Distributions uwrite(proc_t *p, void *buf, user_size_t len, user_addr_t a)
145*c54f35caSApple OSS Distributions {
146*c54f35caSApple OSS Distributions 	kern_return_t ret;
147*c54f35caSApple OSS Distributions 
148*c54f35caSApple OSS Distributions 	ASSERT(p != NULL);
149*c54f35caSApple OSS Distributions 	ASSERT(proc_task(p) != NULL);
150*c54f35caSApple OSS Distributions 
151*c54f35caSApple OSS Distributions 	task_t task = proc_task(p);
152*c54f35caSApple OSS Distributions 
153*c54f35caSApple OSS Distributions 	/*
154*c54f35caSApple OSS Distributions 	 * Grab a reference to the task vm_map_t to make sure
155*c54f35caSApple OSS Distributions 	 * the map isn't pulled out from under us.
156*c54f35caSApple OSS Distributions 	 *
157*c54f35caSApple OSS Distributions 	 * Because the proc_lock is not held at all times on all code
158*c54f35caSApple OSS Distributions 	 * paths leading here, it is possible for the proc to have
159*c54f35caSApple OSS Distributions 	 * exited. If the map is null, fail.
160*c54f35caSApple OSS Distributions 	 */
161*c54f35caSApple OSS Distributions 	vm_map_t map = get_task_map_reference(task);
162*c54f35caSApple OSS Distributions 	if (map) {
163*c54f35caSApple OSS Distributions 		/* Find the memory permissions. */
164*c54f35caSApple OSS Distributions 		uint32_t nestingDepth = 999999;
165*c54f35caSApple OSS Distributions 		vm_region_submap_short_info_data_64_t info;
166*c54f35caSApple OSS Distributions 		mach_msg_type_number_t count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
167*c54f35caSApple OSS Distributions 		mach_vm_address_t address = (mach_vm_address_t)a;
168*c54f35caSApple OSS Distributions 		mach_vm_size_t sizeOfRegion = (mach_vm_size_t)len;
169*c54f35caSApple OSS Distributions 
170*c54f35caSApple OSS Distributions 		ret = mach_vm_region_recurse(map, &address, &sizeOfRegion, &nestingDepth, (vm_region_recurse_info_t)&info, &count);
171*c54f35caSApple OSS Distributions 		if (ret != KERN_SUCCESS) {
172*c54f35caSApple OSS Distributions 			goto done;
173*c54f35caSApple OSS Distributions 		}
174*c54f35caSApple OSS Distributions 
175*c54f35caSApple OSS Distributions 		vm_prot_t reprotect;
176*c54f35caSApple OSS Distributions 
177*c54f35caSApple OSS Distributions 		if (!(info.protection & VM_PROT_WRITE)) {
178*c54f35caSApple OSS Distributions 			/* Save the original protection values for restoration later */
179*c54f35caSApple OSS Distributions 			reprotect = info.protection;
180*c54f35caSApple OSS Distributions 
181*c54f35caSApple OSS Distributions 			if (info.max_protection & VM_PROT_WRITE) {
182*c54f35caSApple OSS Distributions 				/* The memory is not currently writable, but can be made writable. */
183*c54f35caSApple OSS Distributions 				ret = mach_vm_protect(map, (mach_vm_offset_t)a, (mach_vm_size_t)len, 0, (reprotect & ~VM_PROT_EXECUTE) | VM_PROT_WRITE);
184*c54f35caSApple OSS Distributions 			} else {
185*c54f35caSApple OSS Distributions 				/*
186*c54f35caSApple OSS Distributions 				 * The memory is not currently writable, and cannot be made writable. We need to COW this memory.
187*c54f35caSApple OSS Distributions 				 *
188*c54f35caSApple OSS Distributions 				 * Strange, we can't just say "reprotect | VM_PROT_COPY", that fails.
189*c54f35caSApple OSS Distributions 				 */
190*c54f35caSApple OSS Distributions 				ret = mach_vm_protect(map, (mach_vm_offset_t)a, (mach_vm_size_t)len, 0, VM_PROT_COPY | VM_PROT_READ | VM_PROT_WRITE);
191*c54f35caSApple OSS Distributions 			}
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions 			if (ret != KERN_SUCCESS) {
194*c54f35caSApple OSS Distributions 				goto done;
195*c54f35caSApple OSS Distributions 			}
196*c54f35caSApple OSS Distributions 		} else {
197*c54f35caSApple OSS Distributions 			/* The memory was already writable. */
198*c54f35caSApple OSS Distributions 			reprotect = VM_PROT_NONE;
199*c54f35caSApple OSS Distributions 		}
200*c54f35caSApple OSS Distributions 
201*c54f35caSApple OSS Distributions 		ret = vm_map_write_user( map,
202*c54f35caSApple OSS Distributions 		    buf,
203*c54f35caSApple OSS Distributions 		    (vm_map_address_t)a,
204*c54f35caSApple OSS Distributions 		    (vm_size_t)len);
205*c54f35caSApple OSS Distributions 
206*c54f35caSApple OSS Distributions 		dtrace_flush_caches();
207*c54f35caSApple OSS Distributions 
208*c54f35caSApple OSS Distributions 		if (ret != KERN_SUCCESS) {
209*c54f35caSApple OSS Distributions 			goto done;
210*c54f35caSApple OSS Distributions 		}
211*c54f35caSApple OSS Distributions 
212*c54f35caSApple OSS Distributions 		if (reprotect != VM_PROT_NONE) {
213*c54f35caSApple OSS Distributions 			ASSERT(reprotect & VM_PROT_EXECUTE);
214*c54f35caSApple OSS Distributions 			ret = mach_vm_protect(map, (mach_vm_offset_t)a, (mach_vm_size_t)len, 0, reprotect);
215*c54f35caSApple OSS Distributions 		}
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions done:
218*c54f35caSApple OSS Distributions 		vm_map_deallocate(map);
219*c54f35caSApple OSS Distributions 	} else {
220*c54f35caSApple OSS Distributions 		ret = KERN_TERMINATED;
221*c54f35caSApple OSS Distributions 	}
222*c54f35caSApple OSS Distributions 
223*c54f35caSApple OSS Distributions 	return (int)ret;
224*c54f35caSApple OSS Distributions }
225*c54f35caSApple OSS Distributions 
226*c54f35caSApple OSS Distributions /*
227*c54f35caSApple OSS Distributions  * cpuvar
228*c54f35caSApple OSS Distributions  */
229*c54f35caSApple OSS Distributions LCK_MTX_DECLARE_ATTR(cpu_lock, &dtrace_lck_grp, &dtrace_lck_attr);
230*c54f35caSApple OSS Distributions LCK_MTX_DECLARE_ATTR(cyc_lock, &dtrace_lck_grp, &dtrace_lck_attr);
231*c54f35caSApple OSS Distributions LCK_MTX_DECLARE_ATTR(mod_lock, &dtrace_lck_grp, &dtrace_lck_attr);
232*c54f35caSApple OSS Distributions 
233*c54f35caSApple OSS Distributions dtrace_cpu_t *cpu_list;
234*c54f35caSApple OSS Distributions cpu_core_t *cpu_core; /* XXX TLB lockdown? */
235*c54f35caSApple OSS Distributions 
236*c54f35caSApple OSS Distributions /*
237*c54f35caSApple OSS Distributions  * cred_t
238*c54f35caSApple OSS Distributions  */
239*c54f35caSApple OSS Distributions 
240*c54f35caSApple OSS Distributions /*
241*c54f35caSApple OSS Distributions  * dtrace_CRED() can be called from probe context. We cannot simply call kauth_cred_get() since
242*c54f35caSApple OSS Distributions  * that function may try to resolve a lazy credential binding, which entails taking the proc_lock.
243*c54f35caSApple OSS Distributions  */
244*c54f35caSApple OSS Distributions cred_t *
dtrace_CRED(void)245*c54f35caSApple OSS Distributions dtrace_CRED(void)
246*c54f35caSApple OSS Distributions {
247*c54f35caSApple OSS Distributions 	return current_thread_ro_unchecked()->tro_cred;
248*c54f35caSApple OSS Distributions }
249*c54f35caSApple OSS Distributions 
250*c54f35caSApple OSS Distributions int
PRIV_POLICY_CHOICE(void * cred,int priv,int all)251*c54f35caSApple OSS Distributions PRIV_POLICY_CHOICE(void* cred, int priv, int all)
252*c54f35caSApple OSS Distributions {
253*c54f35caSApple OSS Distributions #pragma unused(priv, all)
254*c54f35caSApple OSS Distributions 	return kauth_cred_issuser(cred); /* XXX TODO: How is this different from PRIV_POLICY_ONLY? */
255*c54f35caSApple OSS Distributions }
256*c54f35caSApple OSS Distributions 
257*c54f35caSApple OSS Distributions int
PRIV_POLICY_ONLY(void * cr,int priv,int boolean)258*c54f35caSApple OSS Distributions PRIV_POLICY_ONLY(void *cr, int priv, int boolean)
259*c54f35caSApple OSS Distributions {
260*c54f35caSApple OSS Distributions #pragma unused(priv, boolean)
261*c54f35caSApple OSS Distributions 	return kauth_cred_issuser(cr); /* XXX TODO: HAS_PRIVILEGE(cr, priv); */
262*c54f35caSApple OSS Distributions }
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions uid_t
crgetuid(const cred_t * cr)265*c54f35caSApple OSS Distributions crgetuid(const cred_t *cr)
266*c54f35caSApple OSS Distributions {
267*c54f35caSApple OSS Distributions 	cred_t copy_cr = *cr; return kauth_cred_getuid(&copy_cr);
268*c54f35caSApple OSS Distributions }
269*c54f35caSApple OSS Distributions 
270*c54f35caSApple OSS Distributions /*
271*c54f35caSApple OSS Distributions  * "cyclic"
272*c54f35caSApple OSS Distributions  */
273*c54f35caSApple OSS Distributions 
274*c54f35caSApple OSS Distributions typedef struct wrap_timer_call {
275*c54f35caSApple OSS Distributions 	/* node attributes */
276*c54f35caSApple OSS Distributions 	cyc_handler_t           hdlr;
277*c54f35caSApple OSS Distributions 	cyc_time_t              when;
278*c54f35caSApple OSS Distributions 	uint64_t                deadline;
279*c54f35caSApple OSS Distributions 	int                     cpuid;
280*c54f35caSApple OSS Distributions 	boolean_t               suspended;
281*c54f35caSApple OSS Distributions 	struct timer_call       call;
282*c54f35caSApple OSS Distributions 
283*c54f35caSApple OSS Distributions 	/* next item in the linked list */
284*c54f35caSApple OSS Distributions 	LIST_ENTRY(wrap_timer_call) entries;
285*c54f35caSApple OSS Distributions } wrap_timer_call_t;
286*c54f35caSApple OSS Distributions 
287*c54f35caSApple OSS Distributions #define WAKEUP_REAPER           0x7FFFFFFFFFFFFFFFLL
288*c54f35caSApple OSS Distributions #define NEARLY_FOREVER          0x7FFFFFFFFFFFFFFELL
289*c54f35caSApple OSS Distributions 
290*c54f35caSApple OSS Distributions 
291*c54f35caSApple OSS Distributions typedef struct cyc_list {
292*c54f35caSApple OSS Distributions 	cyc_omni_handler_t cyl_omni;
293*c54f35caSApple OSS Distributions 	wrap_timer_call_t cyl_wrap_by_cpus[];
294*c54f35caSApple OSS Distributions } cyc_list_t;
295*c54f35caSApple OSS Distributions 
296*c54f35caSApple OSS Distributions /* CPU going online/offline notifications */
297*c54f35caSApple OSS Distributions void (*dtrace_cpu_state_changed_hook)(int, boolean_t) = NULL;
298*c54f35caSApple OSS Distributions void dtrace_cpu_state_changed(int, boolean_t);
299*c54f35caSApple OSS Distributions 
300*c54f35caSApple OSS Distributions void
dtrace_install_cpu_hooks(void)301*c54f35caSApple OSS Distributions dtrace_install_cpu_hooks(void)
302*c54f35caSApple OSS Distributions {
303*c54f35caSApple OSS Distributions 	dtrace_cpu_state_changed_hook = dtrace_cpu_state_changed;
304*c54f35caSApple OSS Distributions }
305*c54f35caSApple OSS Distributions 
306*c54f35caSApple OSS Distributions void
dtrace_cpu_state_changed(int cpuid,boolean_t is_running)307*c54f35caSApple OSS Distributions dtrace_cpu_state_changed(int cpuid, boolean_t is_running)
308*c54f35caSApple OSS Distributions {
309*c54f35caSApple OSS Distributions 	wrap_timer_call_t       *wrapTC = NULL;
310*c54f35caSApple OSS Distributions 	boolean_t               suspend = (is_running ? FALSE : TRUE);
311*c54f35caSApple OSS Distributions 	dtrace_icookie_t        s;
312*c54f35caSApple OSS Distributions 
313*c54f35caSApple OSS Distributions 	/* Ensure that we're not going to leave the CPU */
314*c54f35caSApple OSS Distributions 	s = dtrace_interrupt_disable();
315*c54f35caSApple OSS Distributions 
316*c54f35caSApple OSS Distributions 	LIST_FOREACH(wrapTC, &(cpu_list[cpuid].cpu_cyc_list), entries) {
317*c54f35caSApple OSS Distributions 		assert3u(wrapTC->cpuid, ==, cpuid);
318*c54f35caSApple OSS Distributions 		if (suspend) {
319*c54f35caSApple OSS Distributions 			assert(!wrapTC->suspended);
320*c54f35caSApple OSS Distributions 			/* If this fails, we'll panic anyway, so let's do this now. */
321*c54f35caSApple OSS Distributions 			if (!timer_call_cancel(&wrapTC->call)) {
322*c54f35caSApple OSS Distributions 				panic("timer_call_cancel() failed to cancel a timer call: %p",
323*c54f35caSApple OSS Distributions 				    &wrapTC->call);
324*c54f35caSApple OSS Distributions 			}
325*c54f35caSApple OSS Distributions 			wrapTC->suspended = TRUE;
326*c54f35caSApple OSS Distributions 		} else {
327*c54f35caSApple OSS Distributions 			/* Rearm the timer, but ensure it was suspended first. */
328*c54f35caSApple OSS Distributions 			assert(wrapTC->suspended);
329*c54f35caSApple OSS Distributions 			clock_deadline_for_periodic_event(wrapTC->when.cyt_interval, mach_absolute_time(),
330*c54f35caSApple OSS Distributions 			    &wrapTC->deadline);
331*c54f35caSApple OSS Distributions 			timer_call_enter1(&wrapTC->call, (void*) wrapTC, wrapTC->deadline,
332*c54f35caSApple OSS Distributions 			    TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL);
333*c54f35caSApple OSS Distributions 			wrapTC->suspended = FALSE;
334*c54f35caSApple OSS Distributions 		}
335*c54f35caSApple OSS Distributions 	}
336*c54f35caSApple OSS Distributions 
337*c54f35caSApple OSS Distributions 	/* Restore the previous interrupt state. */
338*c54f35caSApple OSS Distributions 	dtrace_interrupt_enable(s);
339*c54f35caSApple OSS Distributions }
340*c54f35caSApple OSS Distributions 
341*c54f35caSApple OSS Distributions static void
_timer_call_apply_cyclic(void * ignore,void * vTChdl)342*c54f35caSApple OSS Distributions _timer_call_apply_cyclic( void *ignore, void *vTChdl )
343*c54f35caSApple OSS Distributions {
344*c54f35caSApple OSS Distributions #pragma unused(ignore)
345*c54f35caSApple OSS Distributions 	wrap_timer_call_t *wrapTC = (wrap_timer_call_t *)vTChdl;
346*c54f35caSApple OSS Distributions 
347*c54f35caSApple OSS Distributions 	(*(wrapTC->hdlr.cyh_func))( wrapTC->hdlr.cyh_arg );
348*c54f35caSApple OSS Distributions 
349*c54f35caSApple OSS Distributions 	clock_deadline_for_periodic_event( wrapTC->when.cyt_interval, mach_absolute_time(), &(wrapTC->deadline));
350*c54f35caSApple OSS Distributions 	timer_call_enter1( &(wrapTC->call), (void *)wrapTC, wrapTC->deadline, TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL );
351*c54f35caSApple OSS Distributions }
352*c54f35caSApple OSS Distributions 
353*c54f35caSApple OSS Distributions static cyclic_id_t
timer_call_add_cyclic(wrap_timer_call_t * wrapTC,cyc_handler_t * handler,cyc_time_t * when)354*c54f35caSApple OSS Distributions timer_call_add_cyclic(wrap_timer_call_t *wrapTC, cyc_handler_t *handler, cyc_time_t *when)
355*c54f35caSApple OSS Distributions {
356*c54f35caSApple OSS Distributions 	uint64_t now;
357*c54f35caSApple OSS Distributions 	dtrace_icookie_t s;
358*c54f35caSApple OSS Distributions 
359*c54f35caSApple OSS Distributions 	timer_call_setup( &(wrapTC->call), _timer_call_apply_cyclic, NULL );
360*c54f35caSApple OSS Distributions 	wrapTC->hdlr = *handler;
361*c54f35caSApple OSS Distributions 	wrapTC->when = *when;
362*c54f35caSApple OSS Distributions 
363*c54f35caSApple OSS Distributions 	nanoseconds_to_absolutetime( wrapTC->when.cyt_interval, (uint64_t *)&wrapTC->when.cyt_interval );
364*c54f35caSApple OSS Distributions 
365*c54f35caSApple OSS Distributions 	now = mach_absolute_time();
366*c54f35caSApple OSS Distributions 	wrapTC->deadline = now;
367*c54f35caSApple OSS Distributions 
368*c54f35caSApple OSS Distributions 	clock_deadline_for_periodic_event( wrapTC->when.cyt_interval, now, &(wrapTC->deadline));
369*c54f35caSApple OSS Distributions 
370*c54f35caSApple OSS Distributions 	/* Insert the timer to the list of the running timers on this CPU, and start it. */
371*c54f35caSApple OSS Distributions 	s = dtrace_interrupt_disable();
372*c54f35caSApple OSS Distributions 	wrapTC->cpuid = cpu_number();
373*c54f35caSApple OSS Distributions 	LIST_INSERT_HEAD(&cpu_list[wrapTC->cpuid].cpu_cyc_list, wrapTC, entries);
374*c54f35caSApple OSS Distributions 	timer_call_enter1(&wrapTC->call, (void*) wrapTC, wrapTC->deadline,
375*c54f35caSApple OSS Distributions 	    TIMER_CALL_SYS_CRITICAL | TIMER_CALL_LOCAL);
376*c54f35caSApple OSS Distributions 	wrapTC->suspended = FALSE;
377*c54f35caSApple OSS Distributions 	dtrace_interrupt_enable(s);
378*c54f35caSApple OSS Distributions 
379*c54f35caSApple OSS Distributions 	return (cyclic_id_t)wrapTC;
380*c54f35caSApple OSS Distributions }
381*c54f35caSApple OSS Distributions 
382*c54f35caSApple OSS Distributions /*
383*c54f35caSApple OSS Distributions  * Executed on the CPU the timer is running on.
384*c54f35caSApple OSS Distributions  */
385*c54f35caSApple OSS Distributions static void
timer_call_remove_cyclic(wrap_timer_call_t * wrapTC)386*c54f35caSApple OSS Distributions timer_call_remove_cyclic(wrap_timer_call_t *wrapTC)
387*c54f35caSApple OSS Distributions {
388*c54f35caSApple OSS Distributions 	assert(wrapTC);
389*c54f35caSApple OSS Distributions 	assert(cpu_number() == wrapTC->cpuid);
390*c54f35caSApple OSS Distributions 
391*c54f35caSApple OSS Distributions 	if (!timer_call_cancel(&wrapTC->call)) {
392*c54f35caSApple OSS Distributions 		panic("timer_call_remove_cyclic() failed to cancel a timer call");
393*c54f35caSApple OSS Distributions 	}
394*c54f35caSApple OSS Distributions 
395*c54f35caSApple OSS Distributions 	LIST_REMOVE(wrapTC, entries);
396*c54f35caSApple OSS Distributions }
397*c54f35caSApple OSS Distributions 
398*c54f35caSApple OSS Distributions static void *
timer_call_get_cyclic_arg(wrap_timer_call_t * wrapTC)399*c54f35caSApple OSS Distributions timer_call_get_cyclic_arg(wrap_timer_call_t *wrapTC)
400*c54f35caSApple OSS Distributions {
401*c54f35caSApple OSS Distributions 	return wrapTC ? wrapTC->hdlr.cyh_arg : NULL;
402*c54f35caSApple OSS Distributions }
403*c54f35caSApple OSS Distributions 
404*c54f35caSApple OSS Distributions cyclic_id_t
cyclic_timer_add(cyc_handler_t * handler,cyc_time_t * when)405*c54f35caSApple OSS Distributions cyclic_timer_add(cyc_handler_t *handler, cyc_time_t *when)
406*c54f35caSApple OSS Distributions {
407*c54f35caSApple OSS Distributions 	wrap_timer_call_t *wrapTC = kalloc_type(wrap_timer_call_t, Z_ZERO | Z_WAITOK);
408*c54f35caSApple OSS Distributions 	if (NULL == wrapTC) {
409*c54f35caSApple OSS Distributions 		return CYCLIC_NONE;
410*c54f35caSApple OSS Distributions 	} else {
411*c54f35caSApple OSS Distributions 		return timer_call_add_cyclic( wrapTC, handler, when );
412*c54f35caSApple OSS Distributions 	}
413*c54f35caSApple OSS Distributions }
414*c54f35caSApple OSS Distributions 
415*c54f35caSApple OSS Distributions void
cyclic_timer_remove(cyclic_id_t cyclic)416*c54f35caSApple OSS Distributions cyclic_timer_remove(cyclic_id_t cyclic)
417*c54f35caSApple OSS Distributions {
418*c54f35caSApple OSS Distributions 	ASSERT( cyclic != CYCLIC_NONE );
419*c54f35caSApple OSS Distributions 
420*c54f35caSApple OSS Distributions 	/* Removing a timer call must be done on the CPU the timer is running on. */
421*c54f35caSApple OSS Distributions 	wrap_timer_call_t *wrapTC = (wrap_timer_call_t *) cyclic;
422*c54f35caSApple OSS Distributions 	dtrace_xcall(wrapTC->cpuid, (dtrace_xcall_t) timer_call_remove_cyclic, (void*) cyclic);
423*c54f35caSApple OSS Distributions 
424*c54f35caSApple OSS Distributions 	kfree_type(wrap_timer_call_t, wrapTC);
425*c54f35caSApple OSS Distributions }
426*c54f35caSApple OSS Distributions 
427*c54f35caSApple OSS Distributions static void
_cyclic_add_omni(cyc_list_t * cyc_list)428*c54f35caSApple OSS Distributions _cyclic_add_omni(cyc_list_t *cyc_list)
429*c54f35caSApple OSS Distributions {
430*c54f35caSApple OSS Distributions 	cyc_time_t cT;
431*c54f35caSApple OSS Distributions 	cyc_handler_t cH;
432*c54f35caSApple OSS Distributions 	cyc_omni_handler_t *omni = &cyc_list->cyl_omni;
433*c54f35caSApple OSS Distributions 
434*c54f35caSApple OSS Distributions 	(omni->cyo_online)(omni->cyo_arg, CPU, &cH, &cT);
435*c54f35caSApple OSS Distributions 
436*c54f35caSApple OSS Distributions 	wrap_timer_call_t *wrapTC = &cyc_list->cyl_wrap_by_cpus[cpu_number()];
437*c54f35caSApple OSS Distributions 	timer_call_add_cyclic(wrapTC, &cH, &cT);
438*c54f35caSApple OSS Distributions }
439*c54f35caSApple OSS Distributions 
440*c54f35caSApple OSS Distributions cyclic_id_list_t
cyclic_add_omni(cyc_omni_handler_t * omni)441*c54f35caSApple OSS Distributions cyclic_add_omni(cyc_omni_handler_t *omni)
442*c54f35caSApple OSS Distributions {
443*c54f35caSApple OSS Distributions 	cyc_list_t *cyc_list = kalloc_type(cyc_list_t, wrap_timer_call_t, NCPU, Z_WAITOK | Z_ZERO);
444*c54f35caSApple OSS Distributions 
445*c54f35caSApple OSS Distributions 	if (NULL == cyc_list) {
446*c54f35caSApple OSS Distributions 		return NULL;
447*c54f35caSApple OSS Distributions 	}
448*c54f35caSApple OSS Distributions 
449*c54f35caSApple OSS Distributions 	cyc_list->cyl_omni = *omni;
450*c54f35caSApple OSS Distributions 
451*c54f35caSApple OSS Distributions 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)_cyclic_add_omni, (void *)cyc_list);
452*c54f35caSApple OSS Distributions 
453*c54f35caSApple OSS Distributions 	return (cyclic_id_list_t)cyc_list;
454*c54f35caSApple OSS Distributions }
455*c54f35caSApple OSS Distributions 
456*c54f35caSApple OSS Distributions static void
_cyclic_remove_omni(cyc_list_t * cyc_list)457*c54f35caSApple OSS Distributions _cyclic_remove_omni(cyc_list_t *cyc_list)
458*c54f35caSApple OSS Distributions {
459*c54f35caSApple OSS Distributions 	cyc_omni_handler_t *omni = &cyc_list->cyl_omni;
460*c54f35caSApple OSS Distributions 	void *oarg;
461*c54f35caSApple OSS Distributions 	wrap_timer_call_t *wrapTC;
462*c54f35caSApple OSS Distributions 
463*c54f35caSApple OSS Distributions 	/*
464*c54f35caSApple OSS Distributions 	 * If the processor was offline when dtrace started, we did not allocate
465*c54f35caSApple OSS Distributions 	 * a cyclic timer for this CPU.
466*c54f35caSApple OSS Distributions 	 */
467*c54f35caSApple OSS Distributions 	if ((wrapTC = &cyc_list->cyl_wrap_by_cpus[cpu_number()]) != NULL) {
468*c54f35caSApple OSS Distributions 		oarg = timer_call_get_cyclic_arg(wrapTC);
469*c54f35caSApple OSS Distributions 		timer_call_remove_cyclic(wrapTC);
470*c54f35caSApple OSS Distributions 		(omni->cyo_offline)(omni->cyo_arg, CPU, oarg);
471*c54f35caSApple OSS Distributions 	}
472*c54f35caSApple OSS Distributions }
473*c54f35caSApple OSS Distributions 
474*c54f35caSApple OSS Distributions void
cyclic_remove_omni(cyclic_id_list_t cyc_list)475*c54f35caSApple OSS Distributions cyclic_remove_omni(cyclic_id_list_t cyc_list)
476*c54f35caSApple OSS Distributions {
477*c54f35caSApple OSS Distributions 	ASSERT(cyc_list != NULL);
478*c54f35caSApple OSS Distributions 
479*c54f35caSApple OSS Distributions 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)_cyclic_remove_omni, (void *)cyc_list);
480*c54f35caSApple OSS Distributions 	void *cyc_list_p = (void *)cyc_list;
481*c54f35caSApple OSS Distributions 	kfree_type(cyc_list_t, wrap_timer_call_t, NCPU, cyc_list_p);
482*c54f35caSApple OSS Distributions }
483*c54f35caSApple OSS Distributions 
484*c54f35caSApple OSS Distributions typedef struct wrap_thread_call {
485*c54f35caSApple OSS Distributions 	thread_call_t TChdl;
486*c54f35caSApple OSS Distributions 	cyc_handler_t hdlr;
487*c54f35caSApple OSS Distributions 	cyc_time_t when;
488*c54f35caSApple OSS Distributions 	uint64_t deadline;
489*c54f35caSApple OSS Distributions } wrap_thread_call_t;
490*c54f35caSApple OSS Distributions 
491*c54f35caSApple OSS Distributions /*
492*c54f35caSApple OSS Distributions  * _cyclic_apply will run on some thread under kernel_task. That's OK for the
493*c54f35caSApple OSS Distributions  * cleaner and the deadman, but too distant in time and place for the profile provider.
494*c54f35caSApple OSS Distributions  */
495*c54f35caSApple OSS Distributions static void
_cyclic_apply(void * ignore,void * vTChdl)496*c54f35caSApple OSS Distributions _cyclic_apply( void *ignore, void *vTChdl )
497*c54f35caSApple OSS Distributions {
498*c54f35caSApple OSS Distributions #pragma unused(ignore)
499*c54f35caSApple OSS Distributions 	wrap_thread_call_t *wrapTC = (wrap_thread_call_t *)vTChdl;
500*c54f35caSApple OSS Distributions 
501*c54f35caSApple OSS Distributions 	(*(wrapTC->hdlr.cyh_func))( wrapTC->hdlr.cyh_arg );
502*c54f35caSApple OSS Distributions 
503*c54f35caSApple OSS Distributions 	clock_deadline_for_periodic_event( wrapTC->when.cyt_interval, mach_absolute_time(), &(wrapTC->deadline));
504*c54f35caSApple OSS Distributions 	(void)thread_call_enter1_delayed( wrapTC->TChdl, (void *)wrapTC, wrapTC->deadline );
505*c54f35caSApple OSS Distributions 
506*c54f35caSApple OSS Distributions 	/* Did cyclic_remove request a wakeup call when this thread call was re-armed? */
507*c54f35caSApple OSS Distributions 	if (wrapTC->when.cyt_interval == WAKEUP_REAPER) {
508*c54f35caSApple OSS Distributions 		thread_wakeup((event_t)wrapTC);
509*c54f35caSApple OSS Distributions 	}
510*c54f35caSApple OSS Distributions }
511*c54f35caSApple OSS Distributions 
512*c54f35caSApple OSS Distributions cyclic_id_t
cyclic_add(cyc_handler_t * handler,cyc_time_t * when)513*c54f35caSApple OSS Distributions cyclic_add(cyc_handler_t *handler, cyc_time_t *when)
514*c54f35caSApple OSS Distributions {
515*c54f35caSApple OSS Distributions 	uint64_t now;
516*c54f35caSApple OSS Distributions 
517*c54f35caSApple OSS Distributions 	wrap_thread_call_t *wrapTC = kalloc_type(wrap_thread_call_t, Z_ZERO | Z_WAITOK);
518*c54f35caSApple OSS Distributions 	if (NULL == wrapTC) {
519*c54f35caSApple OSS Distributions 		return CYCLIC_NONE;
520*c54f35caSApple OSS Distributions 	}
521*c54f35caSApple OSS Distributions 
522*c54f35caSApple OSS Distributions 	wrapTC->TChdl = thread_call_allocate( _cyclic_apply, NULL );
523*c54f35caSApple OSS Distributions 	wrapTC->hdlr = *handler;
524*c54f35caSApple OSS Distributions 	wrapTC->when = *when;
525*c54f35caSApple OSS Distributions 
526*c54f35caSApple OSS Distributions 	ASSERT(when->cyt_when == 0);
527*c54f35caSApple OSS Distributions 	ASSERT(when->cyt_interval < WAKEUP_REAPER);
528*c54f35caSApple OSS Distributions 
529*c54f35caSApple OSS Distributions 	nanoseconds_to_absolutetime(wrapTC->when.cyt_interval, (uint64_t *)&wrapTC->when.cyt_interval);
530*c54f35caSApple OSS Distributions 
531*c54f35caSApple OSS Distributions 	now = mach_absolute_time();
532*c54f35caSApple OSS Distributions 	wrapTC->deadline = now;
533*c54f35caSApple OSS Distributions 
534*c54f35caSApple OSS Distributions 	clock_deadline_for_periodic_event( wrapTC->when.cyt_interval, now, &(wrapTC->deadline));
535*c54f35caSApple OSS Distributions 	(void)thread_call_enter1_delayed( wrapTC->TChdl, (void *)wrapTC, wrapTC->deadline );
536*c54f35caSApple OSS Distributions 
537*c54f35caSApple OSS Distributions 	return (cyclic_id_t)wrapTC;
538*c54f35caSApple OSS Distributions }
539*c54f35caSApple OSS Distributions 
540*c54f35caSApple OSS Distributions static void
noop_cyh_func(void * ignore)541*c54f35caSApple OSS Distributions noop_cyh_func(void * ignore)
542*c54f35caSApple OSS Distributions {
543*c54f35caSApple OSS Distributions #pragma unused(ignore)
544*c54f35caSApple OSS Distributions }
545*c54f35caSApple OSS Distributions 
546*c54f35caSApple OSS Distributions void
cyclic_remove(cyclic_id_t cyclic)547*c54f35caSApple OSS Distributions cyclic_remove(cyclic_id_t cyclic)
548*c54f35caSApple OSS Distributions {
549*c54f35caSApple OSS Distributions 	wrap_thread_call_t *wrapTC = (wrap_thread_call_t *)cyclic;
550*c54f35caSApple OSS Distributions 
551*c54f35caSApple OSS Distributions 	ASSERT(cyclic != CYCLIC_NONE);
552*c54f35caSApple OSS Distributions 
553*c54f35caSApple OSS Distributions 	while (!thread_call_cancel(wrapTC->TChdl)) {
554*c54f35caSApple OSS Distributions 		int ret = assert_wait(wrapTC, THREAD_UNINT);
555*c54f35caSApple OSS Distributions 		ASSERT(ret == THREAD_WAITING);
556*c54f35caSApple OSS Distributions 
557*c54f35caSApple OSS Distributions 		wrapTC->when.cyt_interval = WAKEUP_REAPER;
558*c54f35caSApple OSS Distributions 
559*c54f35caSApple OSS Distributions 		ret = thread_block(THREAD_CONTINUE_NULL);
560*c54f35caSApple OSS Distributions 		ASSERT(ret == THREAD_AWAKENED);
561*c54f35caSApple OSS Distributions 	}
562*c54f35caSApple OSS Distributions 
563*c54f35caSApple OSS Distributions 	if (thread_call_free(wrapTC->TChdl)) {
564*c54f35caSApple OSS Distributions 		kfree_type(wrap_thread_call_t, wrapTC);
565*c54f35caSApple OSS Distributions 	} else {
566*c54f35caSApple OSS Distributions 		/* Gut this cyclic and move on ... */
567*c54f35caSApple OSS Distributions 		wrapTC->hdlr.cyh_func = noop_cyh_func;
568*c54f35caSApple OSS Distributions 		wrapTC->when.cyt_interval = NEARLY_FOREVER;
569*c54f35caSApple OSS Distributions 	}
570*c54f35caSApple OSS Distributions }
571*c54f35caSApple OSS Distributions 
572*c54f35caSApple OSS Distributions int
ddi_driver_major(dev_info_t * devi)573*c54f35caSApple OSS Distributions ddi_driver_major(dev_info_t     *devi)
574*c54f35caSApple OSS Distributions {
575*c54f35caSApple OSS Distributions 	return (int)major(CAST_DOWN_EXPLICIT(int, devi));
576*c54f35caSApple OSS Distributions }
577*c54f35caSApple OSS Distributions 
578*c54f35caSApple OSS Distributions int
ddi_create_minor_node(dev_info_t * dip,const char * name,int spec_type,minor_t minor_num,const char * node_type,int flag)579*c54f35caSApple OSS Distributions ddi_create_minor_node(dev_info_t *dip, const char *name, int spec_type,
580*c54f35caSApple OSS Distributions     minor_t minor_num, const char *node_type, int flag)
581*c54f35caSApple OSS Distributions {
582*c54f35caSApple OSS Distributions #pragma unused(spec_type,node_type,flag)
583*c54f35caSApple OSS Distributions 	dev_t dev = makedev( ddi_driver_major(dip), minor_num );
584*c54f35caSApple OSS Distributions 
585*c54f35caSApple OSS Distributions 	if (NULL == devfs_make_node( dev, DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, "%s", name )) {
586*c54f35caSApple OSS Distributions 		return DDI_FAILURE;
587*c54f35caSApple OSS Distributions 	} else {
588*c54f35caSApple OSS Distributions 		return DDI_SUCCESS;
589*c54f35caSApple OSS Distributions 	}
590*c54f35caSApple OSS Distributions }
591*c54f35caSApple OSS Distributions 
592*c54f35caSApple OSS Distributions void
ddi_remove_minor_node(dev_info_t * dip,char * name)593*c54f35caSApple OSS Distributions ddi_remove_minor_node(dev_info_t *dip, char *name)
594*c54f35caSApple OSS Distributions {
595*c54f35caSApple OSS Distributions #pragma unused(dip,name)
596*c54f35caSApple OSS Distributions /* XXX called from dtrace_detach, so NOTREACHED for now. */
597*c54f35caSApple OSS Distributions }
598*c54f35caSApple OSS Distributions 
599*c54f35caSApple OSS Distributions major_t
getemajor(dev_t d)600*c54f35caSApple OSS Distributions getemajor( dev_t d )
601*c54f35caSApple OSS Distributions {
602*c54f35caSApple OSS Distributions 	return (major_t) major(d);
603*c54f35caSApple OSS Distributions }
604*c54f35caSApple OSS Distributions 
605*c54f35caSApple OSS Distributions minor_t
getminor(dev_t d)606*c54f35caSApple OSS Distributions getminor( dev_t d )
607*c54f35caSApple OSS Distributions {
608*c54f35caSApple OSS Distributions 	return (minor_t) minor(d);
609*c54f35caSApple OSS Distributions }
610*c54f35caSApple OSS Distributions 
611*c54f35caSApple OSS Distributions extern void Debugger(const char*);
612*c54f35caSApple OSS Distributions 
613*c54f35caSApple OSS Distributions void
debug_enter(char * c)614*c54f35caSApple OSS Distributions debug_enter(char *c)
615*c54f35caSApple OSS Distributions {
616*c54f35caSApple OSS Distributions 	Debugger(c);
617*c54f35caSApple OSS Distributions }
618*c54f35caSApple OSS Distributions 
619*c54f35caSApple OSS Distributions /*
620*c54f35caSApple OSS Distributions  * kmem
621*c54f35caSApple OSS Distributions  */
622*c54f35caSApple OSS Distributions 
623*c54f35caSApple OSS Distributions // rdar://88962505
624*c54f35caSApple OSS Distributions __typed_allocators_ignore_push
625*c54f35caSApple OSS Distributions 
626*c54f35caSApple OSS Distributions void *
dt_kmem_alloc_tag(size_t size,int kmflag,vm_tag_t tag)627*c54f35caSApple OSS Distributions dt_kmem_alloc_tag(size_t size, int kmflag, vm_tag_t tag)
628*c54f35caSApple OSS Distributions {
629*c54f35caSApple OSS Distributions #pragma unused(kmflag)
630*c54f35caSApple OSS Distributions 
631*c54f35caSApple OSS Distributions /*
632*c54f35caSApple OSS Distributions  * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
633*c54f35caSApple OSS Distributions  * Requests larger than 8K with M_NOWAIT fail in kalloc_ext.
634*c54f35caSApple OSS Distributions  */
635*c54f35caSApple OSS Distributions 	return kheap_alloc_tag(KHEAP_DTRACE, size, Z_WAITOK, tag);
636*c54f35caSApple OSS Distributions }
637*c54f35caSApple OSS Distributions 
638*c54f35caSApple OSS Distributions void *
dt_kmem_zalloc_tag(size_t size,int kmflag,vm_tag_t tag)639*c54f35caSApple OSS Distributions dt_kmem_zalloc_tag(size_t size, int kmflag, vm_tag_t tag)
640*c54f35caSApple OSS Distributions {
641*c54f35caSApple OSS Distributions #pragma unused(kmflag)
642*c54f35caSApple OSS Distributions 
643*c54f35caSApple OSS Distributions /*
644*c54f35caSApple OSS Distributions  * We ignore the M_NOWAIT bit in kmflag (all of kmflag, in fact).
645*c54f35caSApple OSS Distributions  * Requests larger than 8K with M_NOWAIT fail in kalloc_ext.
646*c54f35caSApple OSS Distributions  */
647*c54f35caSApple OSS Distributions 	return kheap_alloc_tag(KHEAP_DTRACE, size, Z_WAITOK | Z_ZERO, tag);
648*c54f35caSApple OSS Distributions }
649*c54f35caSApple OSS Distributions 
650*c54f35caSApple OSS Distributions void
dt_kmem_free(void * buf,size_t size)651*c54f35caSApple OSS Distributions dt_kmem_free(void *buf, size_t size)
652*c54f35caSApple OSS Distributions {
653*c54f35caSApple OSS Distributions 	kheap_free(KHEAP_DTRACE, buf, size);
654*c54f35caSApple OSS Distributions }
655*c54f35caSApple OSS Distributions 
656*c54f35caSApple OSS Distributions __typed_allocators_ignore_pop
657*c54f35caSApple OSS Distributions 
658*c54f35caSApple OSS Distributions 
659*c54f35caSApple OSS Distributions /*
660*c54f35caSApple OSS Distributions  * aligned dt_kmem allocator
661*c54f35caSApple OSS Distributions  * align should be a power of two
662*c54f35caSApple OSS Distributions  */
663*c54f35caSApple OSS Distributions 
664*c54f35caSApple OSS Distributions void*
dt_kmem_alloc_aligned_tag(size_t size,size_t align,int kmflag,vm_tag_t tag)665*c54f35caSApple OSS Distributions dt_kmem_alloc_aligned_tag(size_t size, size_t align, int kmflag, vm_tag_t tag)
666*c54f35caSApple OSS Distributions {
667*c54f35caSApple OSS Distributions 	void *mem, **addr_to_free;
668*c54f35caSApple OSS Distributions 	intptr_t mem_aligned;
669*c54f35caSApple OSS Distributions 	size_t *size_to_free, hdr_size;
670*c54f35caSApple OSS Distributions 
671*c54f35caSApple OSS Distributions 	/* Must be a power of two. */
672*c54f35caSApple OSS Distributions 	assert(align != 0);
673*c54f35caSApple OSS Distributions 	assert((align & (align - 1)) == 0);
674*c54f35caSApple OSS Distributions 
675*c54f35caSApple OSS Distributions 	/*
676*c54f35caSApple OSS Distributions 	 * We are going to add a header to the allocation. It contains
677*c54f35caSApple OSS Distributions 	 * the address to free and the total size of the buffer.
678*c54f35caSApple OSS Distributions 	 */
679*c54f35caSApple OSS Distributions 	hdr_size = sizeof(size_t) + sizeof(void*);
680*c54f35caSApple OSS Distributions 	mem = dt_kmem_alloc_tag(size + align + hdr_size, kmflag, tag);
681*c54f35caSApple OSS Distributions 	if (mem == NULL) {
682*c54f35caSApple OSS Distributions 		return NULL;
683*c54f35caSApple OSS Distributions 	}
684*c54f35caSApple OSS Distributions 
685*c54f35caSApple OSS Distributions 	mem_aligned = (intptr_t) (((intptr_t) mem + align + hdr_size) & ~(align - 1));
686*c54f35caSApple OSS Distributions 
687*c54f35caSApple OSS Distributions 	/* Write the address to free in the header. */
688*c54f35caSApple OSS Distributions 	addr_to_free = (void**) (mem_aligned - sizeof(void*));
689*c54f35caSApple OSS Distributions 	*addr_to_free = mem;
690*c54f35caSApple OSS Distributions 
691*c54f35caSApple OSS Distributions 	/* Write the size to free in the header. */
692*c54f35caSApple OSS Distributions 	size_to_free = (size_t*) (mem_aligned - hdr_size);
693*c54f35caSApple OSS Distributions 	*size_to_free = size + align + hdr_size;
694*c54f35caSApple OSS Distributions 
695*c54f35caSApple OSS Distributions 	return (void*) mem_aligned;
696*c54f35caSApple OSS Distributions }
697*c54f35caSApple OSS Distributions 
698*c54f35caSApple OSS Distributions void*
dt_kmem_zalloc_aligned_tag(size_t size,size_t align,int kmflag,vm_tag_t tag)699*c54f35caSApple OSS Distributions dt_kmem_zalloc_aligned_tag(size_t size, size_t align, int kmflag, vm_tag_t tag)
700*c54f35caSApple OSS Distributions {
701*c54f35caSApple OSS Distributions 	void* buf;
702*c54f35caSApple OSS Distributions 
703*c54f35caSApple OSS Distributions 	buf = dt_kmem_alloc_aligned_tag(size, align, kmflag, tag);
704*c54f35caSApple OSS Distributions 
705*c54f35caSApple OSS Distributions 	if (!buf) {
706*c54f35caSApple OSS Distributions 		return NULL;
707*c54f35caSApple OSS Distributions 	}
708*c54f35caSApple OSS Distributions 
709*c54f35caSApple OSS Distributions 	bzero(buf, size);
710*c54f35caSApple OSS Distributions 
711*c54f35caSApple OSS Distributions 	return buf;
712*c54f35caSApple OSS Distributions }
713*c54f35caSApple OSS Distributions 
714*c54f35caSApple OSS Distributions void
dt_kmem_free_aligned(void * buf,size_t size)715*c54f35caSApple OSS Distributions dt_kmem_free_aligned(void* buf, size_t size)
716*c54f35caSApple OSS Distributions {
717*c54f35caSApple OSS Distributions #pragma unused(size)
718*c54f35caSApple OSS Distributions 	intptr_t ptr = (intptr_t) buf;
719*c54f35caSApple OSS Distributions 	void **addr_to_free = (void**) (ptr - sizeof(void*));
720*c54f35caSApple OSS Distributions 	size_t *size_to_free = (size_t*) (ptr - (sizeof(size_t) + sizeof(void*)));
721*c54f35caSApple OSS Distributions 
722*c54f35caSApple OSS Distributions 	if (buf == NULL) {
723*c54f35caSApple OSS Distributions 		return;
724*c54f35caSApple OSS Distributions 	}
725*c54f35caSApple OSS Distributions 
726*c54f35caSApple OSS Distributions 	dt_kmem_free(*addr_to_free, *size_to_free);
727*c54f35caSApple OSS Distributions }
728*c54f35caSApple OSS Distributions 
729*c54f35caSApple OSS Distributions /*
730*c54f35caSApple OSS Distributions  * vmem (Solaris "slab" allocator) used by DTrace solely to hand out resource ids
731*c54f35caSApple OSS Distributions  */
732*c54f35caSApple OSS Distributions typedef unsigned int u_daddr_t;
733*c54f35caSApple OSS Distributions #include "blist.h"
734*c54f35caSApple OSS Distributions 
735*c54f35caSApple OSS Distributions /* By passing around blist *handles*, the underlying blist can be resized as needed. */
736*c54f35caSApple OSS Distributions struct blist_hdl {
737*c54f35caSApple OSS Distributions 	blist_t blist;
738*c54f35caSApple OSS Distributions };
739*c54f35caSApple OSS Distributions 
740*c54f35caSApple OSS Distributions vmem_t *
vmem_create(const char * name,void * base,size_t size,size_t quantum,void * ignore5,void * ignore6,vmem_t * source,size_t qcache_max,int vmflag)741*c54f35caSApple OSS Distributions vmem_create(const char *name, void *base, size_t size, size_t quantum, void *ignore5,
742*c54f35caSApple OSS Distributions     void *ignore6, vmem_t *source, size_t qcache_max, int vmflag)
743*c54f35caSApple OSS Distributions {
744*c54f35caSApple OSS Distributions #pragma unused(name,quantum,ignore5,ignore6,source,qcache_max,vmflag)
745*c54f35caSApple OSS Distributions 	blist_t bl;
746*c54f35caSApple OSS Distributions 	struct blist_hdl *p = kalloc_type(struct blist_hdl, Z_WAITOK);
747*c54f35caSApple OSS Distributions 
748*c54f35caSApple OSS Distributions 	ASSERT(quantum == 1);
749*c54f35caSApple OSS Distributions 	ASSERT(NULL == ignore5);
750*c54f35caSApple OSS Distributions 	ASSERT(NULL == ignore6);
751*c54f35caSApple OSS Distributions 	ASSERT(NULL == source);
752*c54f35caSApple OSS Distributions 	ASSERT(0 == qcache_max);
753*c54f35caSApple OSS Distributions 	ASSERT(size <= INT32_MAX);
754*c54f35caSApple OSS Distributions 	ASSERT(vmflag & VMC_IDENTIFIER);
755*c54f35caSApple OSS Distributions 
756*c54f35caSApple OSS Distributions 	size = MIN(128, size); /* Clamp to 128 initially, since the underlying data structure is pre-allocated */
757*c54f35caSApple OSS Distributions 
758*c54f35caSApple OSS Distributions 	p->blist = bl = blist_create((daddr_t)size);
759*c54f35caSApple OSS Distributions 	blist_free(bl, 0, (daddr_t)size);
760*c54f35caSApple OSS Distributions 	if (base) {
761*c54f35caSApple OSS Distributions 		blist_alloc( bl, (daddr_t)(uintptr_t)base );   /* Chomp off initial ID(s) */
762*c54f35caSApple OSS Distributions 	}
763*c54f35caSApple OSS Distributions 	return (vmem_t *)p;
764*c54f35caSApple OSS Distributions }
765*c54f35caSApple OSS Distributions 
766*c54f35caSApple OSS Distributions void *
vmem_alloc(vmem_t * vmp,size_t size,int vmflag)767*c54f35caSApple OSS Distributions vmem_alloc(vmem_t *vmp, size_t size, int vmflag)
768*c54f35caSApple OSS Distributions {
769*c54f35caSApple OSS Distributions #pragma unused(vmflag)
770*c54f35caSApple OSS Distributions 	struct blist_hdl *q = (struct blist_hdl *)vmp;
771*c54f35caSApple OSS Distributions 	blist_t bl = q->blist;
772*c54f35caSApple OSS Distributions 	daddr_t p;
773*c54f35caSApple OSS Distributions 
774*c54f35caSApple OSS Distributions 	p = blist_alloc(bl, (daddr_t)size);
775*c54f35caSApple OSS Distributions 
776*c54f35caSApple OSS Distributions 	if (p == SWAPBLK_NONE) {
777*c54f35caSApple OSS Distributions 		blist_resize(&bl, (bl->bl_blocks) << 1, 1);
778*c54f35caSApple OSS Distributions 		q->blist = bl;
779*c54f35caSApple OSS Distributions 		p = blist_alloc(bl, (daddr_t)size);
780*c54f35caSApple OSS Distributions 		if (p == SWAPBLK_NONE) {
781*c54f35caSApple OSS Distributions 			panic("vmem_alloc: failure after blist_resize!");
782*c54f35caSApple OSS Distributions 		}
783*c54f35caSApple OSS Distributions 	}
784*c54f35caSApple OSS Distributions 
785*c54f35caSApple OSS Distributions 	return (void *)(uintptr_t)p;
786*c54f35caSApple OSS Distributions }
787*c54f35caSApple OSS Distributions 
788*c54f35caSApple OSS Distributions void
vmem_free(vmem_t * vmp,void * vaddr,size_t size)789*c54f35caSApple OSS Distributions vmem_free(vmem_t *vmp, void *vaddr, size_t size)
790*c54f35caSApple OSS Distributions {
791*c54f35caSApple OSS Distributions 	struct blist_hdl *p = (struct blist_hdl *)vmp;
792*c54f35caSApple OSS Distributions 
793*c54f35caSApple OSS Distributions 	blist_free( p->blist, (daddr_t)(uintptr_t)vaddr, (daddr_t)size );
794*c54f35caSApple OSS Distributions }
795*c54f35caSApple OSS Distributions 
796*c54f35caSApple OSS Distributions void
vmem_destroy(vmem_t * vmp)797*c54f35caSApple OSS Distributions vmem_destroy(vmem_t *vmp)
798*c54f35caSApple OSS Distributions {
799*c54f35caSApple OSS Distributions 	struct blist_hdl *p = (struct blist_hdl *)vmp;
800*c54f35caSApple OSS Distributions 
801*c54f35caSApple OSS Distributions 	blist_destroy( p->blist );
802*c54f35caSApple OSS Distributions 	kfree_type(struct blist_hdl, p);
803*c54f35caSApple OSS Distributions }
804*c54f35caSApple OSS Distributions 
805*c54f35caSApple OSS Distributions /*
806*c54f35caSApple OSS Distributions  * Timing
807*c54f35caSApple OSS Distributions  */
808*c54f35caSApple OSS Distributions 
809*c54f35caSApple OSS Distributions /*
810*c54f35caSApple OSS Distributions  * dtrace_gethrestime() provides the "walltimestamp", a value that is anchored at
811*c54f35caSApple OSS Distributions  * January 1, 1970. Because it can be called from probe context, it must take no locks.
812*c54f35caSApple OSS Distributions  */
813*c54f35caSApple OSS Distributions 
814*c54f35caSApple OSS Distributions hrtime_t
dtrace_gethrestime(void)815*c54f35caSApple OSS Distributions dtrace_gethrestime(void)
816*c54f35caSApple OSS Distributions {
817*c54f35caSApple OSS Distributions 	clock_sec_t             secs;
818*c54f35caSApple OSS Distributions 	clock_nsec_t    nanosecs;
819*c54f35caSApple OSS Distributions 	uint64_t                secs64, ns64;
820*c54f35caSApple OSS Distributions 
821*c54f35caSApple OSS Distributions 	clock_get_calendar_nanotime_nowait(&secs, &nanosecs);
822*c54f35caSApple OSS Distributions 	secs64 = (uint64_t)secs;
823*c54f35caSApple OSS Distributions 	ns64 = (uint64_t)nanosecs;
824*c54f35caSApple OSS Distributions 
825*c54f35caSApple OSS Distributions 	ns64 = ns64 + (secs64 * 1000000000LL);
826*c54f35caSApple OSS Distributions 	return ns64;
827*c54f35caSApple OSS Distributions }
828*c54f35caSApple OSS Distributions 
829*c54f35caSApple OSS Distributions /*
830*c54f35caSApple OSS Distributions  * dtrace_gethrtime() provides high-resolution timestamps with machine-dependent origin.
831*c54f35caSApple OSS Distributions  * Hence its primary use is to specify intervals.
832*c54f35caSApple OSS Distributions  */
833*c54f35caSApple OSS Distributions 
834*c54f35caSApple OSS Distributions hrtime_t
dtrace_abs_to_nano(uint64_t elapsed)835*c54f35caSApple OSS Distributions dtrace_abs_to_nano(uint64_t elapsed)
836*c54f35caSApple OSS Distributions {
837*c54f35caSApple OSS Distributions 	static mach_timebase_info_data_t    sTimebaseInfo = { 0, 0 };
838*c54f35caSApple OSS Distributions 
839*c54f35caSApple OSS Distributions 	/*
840*c54f35caSApple OSS Distributions 	 * If this is the first time we've run, get the timebase.
841*c54f35caSApple OSS Distributions 	 * We can use denom == 0 to indicate that sTimebaseInfo is
842*c54f35caSApple OSS Distributions 	 * uninitialised because it makes no sense to have a zero
843*c54f35caSApple OSS Distributions 	 * denominator in a fraction.
844*c54f35caSApple OSS Distributions 	 */
845*c54f35caSApple OSS Distributions 
846*c54f35caSApple OSS Distributions 	if (sTimebaseInfo.denom == 0) {
847*c54f35caSApple OSS Distributions 		(void) clock_timebase_info(&sTimebaseInfo);
848*c54f35caSApple OSS Distributions 	}
849*c54f35caSApple OSS Distributions 
850*c54f35caSApple OSS Distributions 	/*
851*c54f35caSApple OSS Distributions 	 * Convert to nanoseconds.
852*c54f35caSApple OSS Distributions 	 * return (elapsed * (uint64_t)sTimebaseInfo.numer)/(uint64_t)sTimebaseInfo.denom;
853*c54f35caSApple OSS Distributions 	 *
854*c54f35caSApple OSS Distributions 	 * Provided the final result is representable in 64 bits the following maneuver will
855*c54f35caSApple OSS Distributions 	 * deliver that result without intermediate overflow.
856*c54f35caSApple OSS Distributions 	 */
857*c54f35caSApple OSS Distributions 	if (sTimebaseInfo.denom == sTimebaseInfo.numer) {
858*c54f35caSApple OSS Distributions 		return elapsed;
859*c54f35caSApple OSS Distributions 	} else if (sTimebaseInfo.denom == 1) {
860*c54f35caSApple OSS Distributions 		return elapsed * (uint64_t)sTimebaseInfo.numer;
861*c54f35caSApple OSS Distributions 	} else {
862*c54f35caSApple OSS Distributions 		/* Decompose elapsed = eta32 * 2^32 + eps32: */
863*c54f35caSApple OSS Distributions 		uint64_t eta32 = elapsed >> 32;
864*c54f35caSApple OSS Distributions 		uint64_t eps32 = elapsed & 0x00000000ffffffffLL;
865*c54f35caSApple OSS Distributions 
866*c54f35caSApple OSS Distributions 		uint32_t numer = sTimebaseInfo.numer, denom = sTimebaseInfo.denom;
867*c54f35caSApple OSS Distributions 
868*c54f35caSApple OSS Distributions 		/* Form product of elapsed64 (decomposed) and numer: */
869*c54f35caSApple OSS Distributions 		uint64_t mu64 = numer * eta32;
870*c54f35caSApple OSS Distributions 		uint64_t lambda64 = numer * eps32;
871*c54f35caSApple OSS Distributions 
872*c54f35caSApple OSS Distributions 		/* Divide the constituents by denom: */
873*c54f35caSApple OSS Distributions 		uint64_t q32 = mu64 / denom;
874*c54f35caSApple OSS Distributions 		uint64_t r32 = mu64 - (q32 * denom); /* mu64 % denom */
875*c54f35caSApple OSS Distributions 
876*c54f35caSApple OSS Distributions 		return (q32 << 32) + ((r32 << 32) + lambda64) / denom;
877*c54f35caSApple OSS Distributions 	}
878*c54f35caSApple OSS Distributions }
879*c54f35caSApple OSS Distributions 
880*c54f35caSApple OSS Distributions hrtime_t
dtrace_gethrtime(void)881*c54f35caSApple OSS Distributions dtrace_gethrtime(void)
882*c54f35caSApple OSS Distributions {
883*c54f35caSApple OSS Distributions 	static uint64_t        start = 0;
884*c54f35caSApple OSS Distributions 
885*c54f35caSApple OSS Distributions 	if (start == 0) {
886*c54f35caSApple OSS Distributions 		start = mach_absolute_time();
887*c54f35caSApple OSS Distributions 	}
888*c54f35caSApple OSS Distributions 
889*c54f35caSApple OSS Distributions 	return dtrace_abs_to_nano(mach_absolute_time() - start);
890*c54f35caSApple OSS Distributions }
891*c54f35caSApple OSS Distributions 
892*c54f35caSApple OSS Distributions /*
893*c54f35caSApple OSS Distributions  * Atomicity and synchronization
894*c54f35caSApple OSS Distributions  */
895*c54f35caSApple OSS Distributions uint32_t
dtrace_cas32(uint32_t * target,uint32_t cmp,uint32_t new)896*c54f35caSApple OSS Distributions dtrace_cas32(uint32_t *target, uint32_t cmp, uint32_t new)
897*c54f35caSApple OSS Distributions {
898*c54f35caSApple OSS Distributions 	if (OSCompareAndSwap((UInt32)cmp, (UInt32)new, (volatile UInt32 *)target )) {
899*c54f35caSApple OSS Distributions 		return cmp;
900*c54f35caSApple OSS Distributions 	} else {
901*c54f35caSApple OSS Distributions 		return ~cmp; /* Must return something *other* than cmp */
902*c54f35caSApple OSS Distributions 	}
903*c54f35caSApple OSS Distributions }
904*c54f35caSApple OSS Distributions 
905*c54f35caSApple OSS Distributions void *
dtrace_casptr(void * target,void * cmp,void * new)906*c54f35caSApple OSS Distributions dtrace_casptr(void *target, void *cmp, void *new)
907*c54f35caSApple OSS Distributions {
908*c54f35caSApple OSS Distributions 	if (OSCompareAndSwapPtr( cmp, new, (void**)target )) {
909*c54f35caSApple OSS Distributions 		return cmp;
910*c54f35caSApple OSS Distributions 	} else {
911*c54f35caSApple OSS Distributions 		return (void *)(~(uintptr_t)cmp); /* Must return something *other* than cmp */
912*c54f35caSApple OSS Distributions 	}
913*c54f35caSApple OSS Distributions }
914*c54f35caSApple OSS Distributions 
915*c54f35caSApple OSS Distributions /*
916*c54f35caSApple OSS Distributions  * Interrupt manipulation
917*c54f35caSApple OSS Distributions  */
918*c54f35caSApple OSS Distributions dtrace_icookie_t
dtrace_interrupt_disable(void)919*c54f35caSApple OSS Distributions dtrace_interrupt_disable(void)
920*c54f35caSApple OSS Distributions {
921*c54f35caSApple OSS Distributions 	return (dtrace_icookie_t)ml_set_interrupts_enabled(FALSE);
922*c54f35caSApple OSS Distributions }
923*c54f35caSApple OSS Distributions 
924*c54f35caSApple OSS Distributions void
dtrace_interrupt_enable(dtrace_icookie_t reenable)925*c54f35caSApple OSS Distributions dtrace_interrupt_enable(dtrace_icookie_t reenable)
926*c54f35caSApple OSS Distributions {
927*c54f35caSApple OSS Distributions 	(void)ml_set_interrupts_enabled((boolean_t)reenable);
928*c54f35caSApple OSS Distributions }
929*c54f35caSApple OSS Distributions 
930*c54f35caSApple OSS Distributions /*
931*c54f35caSApple OSS Distributions  * MP coordination
932*c54f35caSApple OSS Distributions  */
933*c54f35caSApple OSS Distributions static void
dtrace_sync_func(void)934*c54f35caSApple OSS Distributions dtrace_sync_func(void)
935*c54f35caSApple OSS Distributions {
936*c54f35caSApple OSS Distributions }
937*c54f35caSApple OSS Distributions 
938*c54f35caSApple OSS Distributions /*
939*c54f35caSApple OSS Distributions  * dtrace_sync() is not called from probe context.
940*c54f35caSApple OSS Distributions  */
941*c54f35caSApple OSS Distributions void
dtrace_sync(void)942*c54f35caSApple OSS Distributions dtrace_sync(void)
943*c54f35caSApple OSS Distributions {
944*c54f35caSApple OSS Distributions 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
945*c54f35caSApple OSS Distributions }
946*c54f35caSApple OSS Distributions 
947*c54f35caSApple OSS Distributions /*
948*c54f35caSApple OSS Distributions  * The dtrace_copyin/out/instr and dtrace_fuword* routines can be called from probe context.
949*c54f35caSApple OSS Distributions  */
950*c54f35caSApple OSS Distributions 
951*c54f35caSApple OSS Distributions extern kern_return_t dtrace_copyio_preflight(addr64_t);
952*c54f35caSApple OSS Distributions extern kern_return_t dtrace_copyio_postflight(addr64_t);
953*c54f35caSApple OSS Distributions 
954*c54f35caSApple OSS Distributions static int
dtrace_copycheck(user_addr_t uaddr,uintptr_t kaddr,size_t size)955*c54f35caSApple OSS Distributions dtrace_copycheck(user_addr_t uaddr, uintptr_t kaddr, size_t size)
956*c54f35caSApple OSS Distributions {
957*c54f35caSApple OSS Distributions #pragma unused(kaddr)
958*c54f35caSApple OSS Distributions 
959*c54f35caSApple OSS Distributions 	ASSERT(kaddr + size >= kaddr);
960*c54f35caSApple OSS Distributions 
961*c54f35caSApple OSS Distributions 	if (uaddr + size < uaddr ||             /* Avoid address wrap. */
962*c54f35caSApple OSS Distributions 	    KERN_FAILURE == dtrace_copyio_preflight(uaddr)) {   /* Machine specific setup/constraints. */
963*c54f35caSApple OSS Distributions 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
964*c54f35caSApple OSS Distributions 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
965*c54f35caSApple OSS Distributions 		return 0;
966*c54f35caSApple OSS Distributions 	}
967*c54f35caSApple OSS Distributions 	return 1;
968*c54f35caSApple OSS Distributions }
969*c54f35caSApple OSS Distributions 
970*c54f35caSApple OSS Distributions void
dtrace_copyin(user_addr_t src,uintptr_t dst,size_t len,volatile uint16_t * flags)971*c54f35caSApple OSS Distributions dtrace_copyin(user_addr_t src, uintptr_t dst, size_t len, volatile uint16_t *flags)
972*c54f35caSApple OSS Distributions {
973*c54f35caSApple OSS Distributions #pragma unused(flags)
974*c54f35caSApple OSS Distributions 
975*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( src, dst, len )) {
976*c54f35caSApple OSS Distributions 		if (copyin((const user_addr_t)src, (char *)dst, (vm_size_t)len)) {
977*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
978*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = src;
979*c54f35caSApple OSS Distributions 		}
980*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(src);
981*c54f35caSApple OSS Distributions 	}
982*c54f35caSApple OSS Distributions }
983*c54f35caSApple OSS Distributions 
984*c54f35caSApple OSS Distributions void
dtrace_copyinstr(user_addr_t src,uintptr_t dst,size_t len,volatile uint16_t * flags)985*c54f35caSApple OSS Distributions dtrace_copyinstr(user_addr_t src, uintptr_t dst, size_t len, volatile uint16_t *flags)
986*c54f35caSApple OSS Distributions {
987*c54f35caSApple OSS Distributions #pragma unused(flags)
988*c54f35caSApple OSS Distributions 
989*c54f35caSApple OSS Distributions 	size_t actual;
990*c54f35caSApple OSS Distributions 
991*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( src, dst, len )) {
992*c54f35caSApple OSS Distributions 		/*  copyin as many as 'len' bytes. */
993*c54f35caSApple OSS Distributions 		int error = copyinstr((const user_addr_t)src, (char *)dst, (vm_size_t)len, &actual);
994*c54f35caSApple OSS Distributions 
995*c54f35caSApple OSS Distributions 		/*
996*c54f35caSApple OSS Distributions 		 * ENAMETOOLONG is returned when 'len' bytes have been copied in but the NUL terminator was
997*c54f35caSApple OSS Distributions 		 * not encountered. That does not require raising CPU_DTRACE_BADADDR, and we press on.
998*c54f35caSApple OSS Distributions 		 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
999*c54f35caSApple OSS Distributions 		 * to the caller.
1000*c54f35caSApple OSS Distributions 		 */
1001*c54f35caSApple OSS Distributions 		if (error && error != ENAMETOOLONG) {
1002*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1003*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = src;
1004*c54f35caSApple OSS Distributions 		}
1005*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(src);
1006*c54f35caSApple OSS Distributions 	}
1007*c54f35caSApple OSS Distributions }
1008*c54f35caSApple OSS Distributions 
1009*c54f35caSApple OSS Distributions void
dtrace_copyout(uintptr_t src,user_addr_t dst,size_t len,volatile uint16_t * flags)1010*c54f35caSApple OSS Distributions dtrace_copyout(uintptr_t src, user_addr_t dst, size_t len, volatile uint16_t *flags)
1011*c54f35caSApple OSS Distributions {
1012*c54f35caSApple OSS Distributions #pragma unused(flags)
1013*c54f35caSApple OSS Distributions 
1014*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( dst, src, len )) {
1015*c54f35caSApple OSS Distributions 		if (copyout((const void *)src, dst, (vm_size_t)len)) {
1016*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1017*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = dst;
1018*c54f35caSApple OSS Distributions 		}
1019*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(dst);
1020*c54f35caSApple OSS Distributions 	}
1021*c54f35caSApple OSS Distributions }
1022*c54f35caSApple OSS Distributions 
1023*c54f35caSApple OSS Distributions void
dtrace_copyoutstr(uintptr_t src,user_addr_t dst,size_t len,volatile uint16_t * flags)1024*c54f35caSApple OSS Distributions dtrace_copyoutstr(uintptr_t src, user_addr_t dst, size_t len, volatile uint16_t *flags)
1025*c54f35caSApple OSS Distributions {
1026*c54f35caSApple OSS Distributions #pragma unused(flags)
1027*c54f35caSApple OSS Distributions 
1028*c54f35caSApple OSS Distributions 	size_t actual;
1029*c54f35caSApple OSS Distributions 
1030*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( dst, src, len )) {
1031*c54f35caSApple OSS Distributions 		/*
1032*c54f35caSApple OSS Distributions 		 * ENAMETOOLONG is returned when 'len' bytes have been copied out but the NUL terminator was
1033*c54f35caSApple OSS Distributions 		 * not encountered. We raise CPU_DTRACE_BADADDR in that case.
1034*c54f35caSApple OSS Distributions 		 * Note that we do *not* stuff a NUL terminator when returning ENAMETOOLONG, that's left
1035*c54f35caSApple OSS Distributions 		 * to the caller.
1036*c54f35caSApple OSS Distributions 		 */
1037*c54f35caSApple OSS Distributions 		if (copyoutstr((const void *)src, dst, (size_t)len, &actual)) {
1038*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1039*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = dst;
1040*c54f35caSApple OSS Distributions 		}
1041*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(dst);
1042*c54f35caSApple OSS Distributions 	}
1043*c54f35caSApple OSS Distributions }
1044*c54f35caSApple OSS Distributions 
1045*c54f35caSApple OSS Distributions extern const int copysize_limit_panic;
1046*c54f35caSApple OSS Distributions 
1047*c54f35caSApple OSS Distributions int
dtrace_copy_maxsize(void)1048*c54f35caSApple OSS Distributions dtrace_copy_maxsize(void)
1049*c54f35caSApple OSS Distributions {
1050*c54f35caSApple OSS Distributions 	return copysize_limit_panic;
1051*c54f35caSApple OSS Distributions }
1052*c54f35caSApple OSS Distributions 
1053*c54f35caSApple OSS Distributions 
1054*c54f35caSApple OSS Distributions int
dtrace_buffer_copyout(const void * kaddr,user_addr_t uaddr,vm_size_t nbytes)1055*c54f35caSApple OSS Distributions dtrace_buffer_copyout(const void *kaddr, user_addr_t uaddr, vm_size_t nbytes)
1056*c54f35caSApple OSS Distributions {
1057*c54f35caSApple OSS Distributions 	int maxsize = dtrace_copy_maxsize();
1058*c54f35caSApple OSS Distributions 	/*
1059*c54f35caSApple OSS Distributions 	 * Partition the copyout in copysize_limit_panic-sized chunks
1060*c54f35caSApple OSS Distributions 	 */
1061*c54f35caSApple OSS Distributions 	while (nbytes >= (vm_size_t)maxsize) {
1062*c54f35caSApple OSS Distributions 		if (copyout(kaddr, uaddr, maxsize) != 0) {
1063*c54f35caSApple OSS Distributions 			return EFAULT;
1064*c54f35caSApple OSS Distributions 		}
1065*c54f35caSApple OSS Distributions 
1066*c54f35caSApple OSS Distributions 		nbytes -= maxsize;
1067*c54f35caSApple OSS Distributions 		uaddr += maxsize;
1068*c54f35caSApple OSS Distributions 		kaddr = (const void *)((uintptr_t)kaddr + maxsize);
1069*c54f35caSApple OSS Distributions 	}
1070*c54f35caSApple OSS Distributions 	if (nbytes > 0) {
1071*c54f35caSApple OSS Distributions 		if (copyout(kaddr, uaddr, nbytes) != 0) {
1072*c54f35caSApple OSS Distributions 			return EFAULT;
1073*c54f35caSApple OSS Distributions 		}
1074*c54f35caSApple OSS Distributions 	}
1075*c54f35caSApple OSS Distributions 
1076*c54f35caSApple OSS Distributions 	return 0;
1077*c54f35caSApple OSS Distributions }
1078*c54f35caSApple OSS Distributions 
1079*c54f35caSApple OSS Distributions uint8_t
dtrace_fuword8(user_addr_t uaddr)1080*c54f35caSApple OSS Distributions dtrace_fuword8(user_addr_t uaddr)
1081*c54f35caSApple OSS Distributions {
1082*c54f35caSApple OSS Distributions 	uint8_t ret = 0;
1083*c54f35caSApple OSS Distributions 
1084*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1085*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( uaddr, (uintptr_t)&ret, sizeof(ret))) {
1086*c54f35caSApple OSS Distributions 		if (copyin((const user_addr_t)uaddr, (char *)&ret, sizeof(ret))) {
1087*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1088*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
1089*c54f35caSApple OSS Distributions 		}
1090*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(uaddr);
1091*c54f35caSApple OSS Distributions 	}
1092*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
1093*c54f35caSApple OSS Distributions 
1094*c54f35caSApple OSS Distributions 	return ret;
1095*c54f35caSApple OSS Distributions }
1096*c54f35caSApple OSS Distributions 
1097*c54f35caSApple OSS Distributions uint16_t
dtrace_fuword16(user_addr_t uaddr)1098*c54f35caSApple OSS Distributions dtrace_fuword16(user_addr_t uaddr)
1099*c54f35caSApple OSS Distributions {
1100*c54f35caSApple OSS Distributions 	uint16_t ret = 0;
1101*c54f35caSApple OSS Distributions 
1102*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1103*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( uaddr, (uintptr_t)&ret, sizeof(ret))) {
1104*c54f35caSApple OSS Distributions 		if (copyin((const user_addr_t)uaddr, (char *)&ret, sizeof(ret))) {
1105*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1106*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
1107*c54f35caSApple OSS Distributions 		}
1108*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(uaddr);
1109*c54f35caSApple OSS Distributions 	}
1110*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
1111*c54f35caSApple OSS Distributions 
1112*c54f35caSApple OSS Distributions 	return ret;
1113*c54f35caSApple OSS Distributions }
1114*c54f35caSApple OSS Distributions 
1115*c54f35caSApple OSS Distributions uint32_t
dtrace_fuword32(user_addr_t uaddr)1116*c54f35caSApple OSS Distributions dtrace_fuword32(user_addr_t uaddr)
1117*c54f35caSApple OSS Distributions {
1118*c54f35caSApple OSS Distributions 	uint32_t ret = 0;
1119*c54f35caSApple OSS Distributions 
1120*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1121*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( uaddr, (uintptr_t)&ret, sizeof(ret))) {
1122*c54f35caSApple OSS Distributions 		if (copyin((const user_addr_t)uaddr, (char *)&ret, sizeof(ret))) {
1123*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1124*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
1125*c54f35caSApple OSS Distributions 		}
1126*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(uaddr);
1127*c54f35caSApple OSS Distributions 	}
1128*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
1129*c54f35caSApple OSS Distributions 
1130*c54f35caSApple OSS Distributions 	return ret;
1131*c54f35caSApple OSS Distributions }
1132*c54f35caSApple OSS Distributions 
1133*c54f35caSApple OSS Distributions uint64_t
dtrace_fuword64(user_addr_t uaddr)1134*c54f35caSApple OSS Distributions dtrace_fuword64(user_addr_t uaddr)
1135*c54f35caSApple OSS Distributions {
1136*c54f35caSApple OSS Distributions 	uint64_t ret = 0;
1137*c54f35caSApple OSS Distributions 
1138*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1139*c54f35caSApple OSS Distributions 	if (dtrace_copycheck( uaddr, (uintptr_t)&ret, sizeof(ret))) {
1140*c54f35caSApple OSS Distributions 		if (copyin((const user_addr_t)uaddr, (char *)&ret, sizeof(ret))) {
1141*c54f35caSApple OSS Distributions 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1142*c54f35caSApple OSS Distributions 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
1143*c54f35caSApple OSS Distributions 		}
1144*c54f35caSApple OSS Distributions 		dtrace_copyio_postflight(uaddr);
1145*c54f35caSApple OSS Distributions 	}
1146*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
1147*c54f35caSApple OSS Distributions 
1148*c54f35caSApple OSS Distributions 	return ret;
1149*c54f35caSApple OSS Distributions }
1150*c54f35caSApple OSS Distributions 
1151*c54f35caSApple OSS Distributions /*
1152*c54f35caSApple OSS Distributions  * Emulation of Solaris fuword / suword
1153*c54f35caSApple OSS Distributions  * Called from the fasttrap provider, so the use of copyin/out requires fewer safegaurds.
1154*c54f35caSApple OSS Distributions  */
1155*c54f35caSApple OSS Distributions 
1156*c54f35caSApple OSS Distributions int
fuword8(user_addr_t uaddr,uint8_t * value)1157*c54f35caSApple OSS Distributions fuword8(user_addr_t uaddr, uint8_t *value)
1158*c54f35caSApple OSS Distributions {
1159*c54f35caSApple OSS Distributions 	if (copyin((const user_addr_t)uaddr, (char *)value, sizeof(uint8_t)) != 0) {
1160*c54f35caSApple OSS Distributions 		return -1;
1161*c54f35caSApple OSS Distributions 	}
1162*c54f35caSApple OSS Distributions 
1163*c54f35caSApple OSS Distributions 	return 0;
1164*c54f35caSApple OSS Distributions }
1165*c54f35caSApple OSS Distributions 
1166*c54f35caSApple OSS Distributions int
fuword16(user_addr_t uaddr,uint16_t * value)1167*c54f35caSApple OSS Distributions fuword16(user_addr_t uaddr, uint16_t *value)
1168*c54f35caSApple OSS Distributions {
1169*c54f35caSApple OSS Distributions 	if (copyin((const user_addr_t)uaddr, (char *)value, sizeof(uint16_t)) != 0) {
1170*c54f35caSApple OSS Distributions 		return -1;
1171*c54f35caSApple OSS Distributions 	}
1172*c54f35caSApple OSS Distributions 
1173*c54f35caSApple OSS Distributions 	return 0;
1174*c54f35caSApple OSS Distributions }
1175*c54f35caSApple OSS Distributions 
1176*c54f35caSApple OSS Distributions int
fuword32(user_addr_t uaddr,uint32_t * value)1177*c54f35caSApple OSS Distributions fuword32(user_addr_t uaddr, uint32_t *value)
1178*c54f35caSApple OSS Distributions {
1179*c54f35caSApple OSS Distributions 	if (copyin((const user_addr_t)uaddr, (char *)value, sizeof(uint32_t)) != 0) {
1180*c54f35caSApple OSS Distributions 		return -1;
1181*c54f35caSApple OSS Distributions 	}
1182*c54f35caSApple OSS Distributions 
1183*c54f35caSApple OSS Distributions 	return 0;
1184*c54f35caSApple OSS Distributions }
1185*c54f35caSApple OSS Distributions 
1186*c54f35caSApple OSS Distributions int
fuword64(user_addr_t uaddr,uint64_t * value)1187*c54f35caSApple OSS Distributions fuword64(user_addr_t uaddr, uint64_t *value)
1188*c54f35caSApple OSS Distributions {
1189*c54f35caSApple OSS Distributions 	if (copyin((const user_addr_t)uaddr, (char *)value, sizeof(uint64_t)) != 0) {
1190*c54f35caSApple OSS Distributions 		return -1;
1191*c54f35caSApple OSS Distributions 	}
1192*c54f35caSApple OSS Distributions 
1193*c54f35caSApple OSS Distributions 	return 0;
1194*c54f35caSApple OSS Distributions }
1195*c54f35caSApple OSS Distributions 
1196*c54f35caSApple OSS Distributions void
fuword32_noerr(user_addr_t uaddr,uint32_t * value)1197*c54f35caSApple OSS Distributions fuword32_noerr(user_addr_t uaddr, uint32_t *value)
1198*c54f35caSApple OSS Distributions {
1199*c54f35caSApple OSS Distributions 	if (copyin((const user_addr_t)uaddr, (char *)value, sizeof(uint32_t))) {
1200*c54f35caSApple OSS Distributions 		*value = 0;
1201*c54f35caSApple OSS Distributions 	}
1202*c54f35caSApple OSS Distributions }
1203*c54f35caSApple OSS Distributions 
1204*c54f35caSApple OSS Distributions void
fuword64_noerr(user_addr_t uaddr,uint64_t * value)1205*c54f35caSApple OSS Distributions fuword64_noerr(user_addr_t uaddr, uint64_t *value)
1206*c54f35caSApple OSS Distributions {
1207*c54f35caSApple OSS Distributions 	if (copyin((const user_addr_t)uaddr, (char *)value, sizeof(uint64_t))) {
1208*c54f35caSApple OSS Distributions 		*value = 0;
1209*c54f35caSApple OSS Distributions 	}
1210*c54f35caSApple OSS Distributions }
1211*c54f35caSApple OSS Distributions 
1212*c54f35caSApple OSS Distributions int
suword64(user_addr_t addr,uint64_t value)1213*c54f35caSApple OSS Distributions suword64(user_addr_t addr, uint64_t value)
1214*c54f35caSApple OSS Distributions {
1215*c54f35caSApple OSS Distributions 	if (copyout((const void *)&value, addr, sizeof(value)) != 0) {
1216*c54f35caSApple OSS Distributions 		return -1;
1217*c54f35caSApple OSS Distributions 	}
1218*c54f35caSApple OSS Distributions 
1219*c54f35caSApple OSS Distributions 	return 0;
1220*c54f35caSApple OSS Distributions }
1221*c54f35caSApple OSS Distributions 
1222*c54f35caSApple OSS Distributions int
suword32(user_addr_t addr,uint32_t value)1223*c54f35caSApple OSS Distributions suword32(user_addr_t addr, uint32_t value)
1224*c54f35caSApple OSS Distributions {
1225*c54f35caSApple OSS Distributions 	if (copyout((const void *)&value, addr, sizeof(value)) != 0) {
1226*c54f35caSApple OSS Distributions 		return -1;
1227*c54f35caSApple OSS Distributions 	}
1228*c54f35caSApple OSS Distributions 
1229*c54f35caSApple OSS Distributions 	return 0;
1230*c54f35caSApple OSS Distributions }
1231*c54f35caSApple OSS Distributions 
1232*c54f35caSApple OSS Distributions /*
1233*c54f35caSApple OSS Distributions  * Miscellaneous
1234*c54f35caSApple OSS Distributions  */
1235*c54f35caSApple OSS Distributions extern boolean_t dtrace_tally_fault(user_addr_t);
1236*c54f35caSApple OSS Distributions 
1237*c54f35caSApple OSS Distributions boolean_t
dtrace_tally_fault(user_addr_t uaddr)1238*c54f35caSApple OSS Distributions dtrace_tally_fault(user_addr_t uaddr)
1239*c54f35caSApple OSS Distributions {
1240*c54f35caSApple OSS Distributions 	DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1241*c54f35caSApple OSS Distributions 	cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
1242*c54f35caSApple OSS Distributions 	return DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT) ? TRUE : FALSE;
1243*c54f35caSApple OSS Distributions }
1244*c54f35caSApple OSS Distributions 
1245*c54f35caSApple OSS Distributions #define TOTTY   0x02
1246*c54f35caSApple OSS Distributions extern int prf(const char *, va_list, int, struct tty *); /* bsd/kern/subr_prf.h */
1247*c54f35caSApple OSS Distributions 
1248*c54f35caSApple OSS Distributions int
vuprintf(const char * format,va_list ap)1249*c54f35caSApple OSS Distributions vuprintf(const char *format, va_list ap)
1250*c54f35caSApple OSS Distributions {
1251*c54f35caSApple OSS Distributions 	return prf(format, ap, TOTTY, NULL);
1252*c54f35caSApple OSS Distributions }
1253*c54f35caSApple OSS Distributions 
1254*c54f35caSApple OSS Distributions /* Not called from probe context */
1255*c54f35caSApple OSS Distributions void
cmn_err(int level,const char * format,...)1256*c54f35caSApple OSS Distributions cmn_err( int level, const char *format, ... )
1257*c54f35caSApple OSS Distributions {
1258*c54f35caSApple OSS Distributions #pragma unused(level)
1259*c54f35caSApple OSS Distributions 	va_list alist;
1260*c54f35caSApple OSS Distributions 
1261*c54f35caSApple OSS Distributions 	va_start(alist, format);
1262*c54f35caSApple OSS Distributions 	vuprintf(format, alist);
1263*c54f35caSApple OSS Distributions 	va_end(alist);
1264*c54f35caSApple OSS Distributions 	uprintf("\n");
1265*c54f35caSApple OSS Distributions }
1266*c54f35caSApple OSS Distributions 
1267*c54f35caSApple OSS Distributions const void*
bsearch(const void * key,const void * base0,size_t nmemb,size_t size,int (* compar)(const void *,const void *))1268*c54f35caSApple OSS Distributions bsearch(const void *key, const void *base0, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
1269*c54f35caSApple OSS Distributions {
1270*c54f35caSApple OSS Distributions 	const char *base = base0;
1271*c54f35caSApple OSS Distributions 	size_t lim;
1272*c54f35caSApple OSS Distributions 	int cmp;
1273*c54f35caSApple OSS Distributions 	const void *p;
1274*c54f35caSApple OSS Distributions 	for (lim = nmemb; lim != 0; lim >>= 1) {
1275*c54f35caSApple OSS Distributions 		p = base + (lim >> 1) * size;
1276*c54f35caSApple OSS Distributions 		cmp = (*compar)(key, p);
1277*c54f35caSApple OSS Distributions 		if (cmp == 0) {
1278*c54f35caSApple OSS Distributions 			return p;
1279*c54f35caSApple OSS Distributions 		}
1280*c54f35caSApple OSS Distributions 		if (cmp > 0) {  /* key > p: move right */
1281*c54f35caSApple OSS Distributions 			base = (const char *)p + size;
1282*c54f35caSApple OSS Distributions 			lim--;
1283*c54f35caSApple OSS Distributions 		}               /* else move left */
1284*c54f35caSApple OSS Distributions 	}
1285*c54f35caSApple OSS Distributions 	return NULL;
1286*c54f35caSApple OSS Distributions }
1287*c54f35caSApple OSS Distributions 
1288*c54f35caSApple OSS Distributions /*
1289*c54f35caSApple OSS Distributions  * Runtime and ABI
1290*c54f35caSApple OSS Distributions  */
1291*c54f35caSApple OSS Distributions uintptr_t
dtrace_caller(int ignore)1292*c54f35caSApple OSS Distributions dtrace_caller(int ignore)
1293*c54f35caSApple OSS Distributions {
1294*c54f35caSApple OSS Distributions #pragma unused(ignore)
1295*c54f35caSApple OSS Distributions 	return -1; /* Just as in Solaris dtrace_asm.s */
1296*c54f35caSApple OSS Distributions }
1297*c54f35caSApple OSS Distributions 
1298*c54f35caSApple OSS Distributions int
dtrace_getstackdepth(int aframes)1299*c54f35caSApple OSS Distributions dtrace_getstackdepth(int aframes)
1300*c54f35caSApple OSS Distributions {
1301*c54f35caSApple OSS Distributions 	struct frame *fp = (struct frame *)__builtin_frame_address(0);
1302*c54f35caSApple OSS Distributions 	struct frame *nextfp, *minfp, *stacktop;
1303*c54f35caSApple OSS Distributions 	int depth = 0;
1304*c54f35caSApple OSS Distributions 	int on_intr;
1305*c54f35caSApple OSS Distributions 
1306*c54f35caSApple OSS Distributions 	if ((on_intr = CPU_ON_INTR(CPU)) != 0) {
1307*c54f35caSApple OSS Distributions 		stacktop = (struct frame *)dtrace_get_cpu_int_stack_top();
1308*c54f35caSApple OSS Distributions 	} else {
1309*c54f35caSApple OSS Distributions 		stacktop = (struct frame *)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size);
1310*c54f35caSApple OSS Distributions 	}
1311*c54f35caSApple OSS Distributions 
1312*c54f35caSApple OSS Distributions 	minfp = fp;
1313*c54f35caSApple OSS Distributions 
1314*c54f35caSApple OSS Distributions 	aframes++;
1315*c54f35caSApple OSS Distributions 
1316*c54f35caSApple OSS Distributions 	for (;;) {
1317*c54f35caSApple OSS Distributions 		depth++;
1318*c54f35caSApple OSS Distributions 
1319*c54f35caSApple OSS Distributions 		nextfp = *(struct frame **)fp;
1320*c54f35caSApple OSS Distributions 
1321*c54f35caSApple OSS Distributions 		if (nextfp <= minfp || nextfp >= stacktop) {
1322*c54f35caSApple OSS Distributions 			if (on_intr) {
1323*c54f35caSApple OSS Distributions 				/*
1324*c54f35caSApple OSS Distributions 				 * Hop from interrupt stack to thread stack.
1325*c54f35caSApple OSS Distributions 				 */
1326*c54f35caSApple OSS Distributions 				vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread());
1327*c54f35caSApple OSS Distributions 
1328*c54f35caSApple OSS Distributions 				minfp = (struct frame *)kstack_base;
1329*c54f35caSApple OSS Distributions 				stacktop = (struct frame *)(kstack_base + kernel_stack_size);
1330*c54f35caSApple OSS Distributions 
1331*c54f35caSApple OSS Distributions 				on_intr = 0;
1332*c54f35caSApple OSS Distributions 				continue;
1333*c54f35caSApple OSS Distributions 			}
1334*c54f35caSApple OSS Distributions 			break;
1335*c54f35caSApple OSS Distributions 		}
1336*c54f35caSApple OSS Distributions 
1337*c54f35caSApple OSS Distributions 		fp = nextfp;
1338*c54f35caSApple OSS Distributions 		minfp = fp;
1339*c54f35caSApple OSS Distributions 	}
1340*c54f35caSApple OSS Distributions 
1341*c54f35caSApple OSS Distributions 	if (depth <= aframes) {
1342*c54f35caSApple OSS Distributions 		return 0;
1343*c54f35caSApple OSS Distributions 	}
1344*c54f35caSApple OSS Distributions 
1345*c54f35caSApple OSS Distributions 	return depth - aframes;
1346*c54f35caSApple OSS Distributions }
1347*c54f35caSApple OSS Distributions 
1348*c54f35caSApple OSS Distributions int
dtrace_addr_in_module(const void * addr,const struct modctl * ctl)1349*c54f35caSApple OSS Distributions dtrace_addr_in_module(const void* addr, const struct modctl *ctl)
1350*c54f35caSApple OSS Distributions {
1351*c54f35caSApple OSS Distributions 	return OSKextKextForAddress(addr) == (void*)ctl->mod_address;
1352*c54f35caSApple OSS Distributions }
1353*c54f35caSApple OSS Distributions 
1354*c54f35caSApple OSS Distributions /*
1355*c54f35caSApple OSS Distributions  * Unconsidered
1356*c54f35caSApple OSS Distributions  */
1357*c54f35caSApple OSS Distributions void
dtrace_vtime_enable(void)1358*c54f35caSApple OSS Distributions dtrace_vtime_enable(void)
1359*c54f35caSApple OSS Distributions {
1360*c54f35caSApple OSS Distributions }
1361*c54f35caSApple OSS Distributions 
1362*c54f35caSApple OSS Distributions void
dtrace_vtime_disable(void)1363*c54f35caSApple OSS Distributions dtrace_vtime_disable(void)
1364*c54f35caSApple OSS Distributions {
1365*c54f35caSApple OSS Distributions }
1366