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