2 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _DTRACE_GLUE_H
30 #define _DTRACE_GLUE_H
34 #include <libkern/libkern.h>
35 #include <kern/lock.h>
36 #include <kern/locks.h>
37 #include <kern/thread_call.h>
38 #include <machine/machine_routines.h>
39 #include <sys/syslog.h>
40 #include <sys/ucred.h>
42 #include <mach/kmod.h>
43 #include <libkern/OSAtomic.h>
47 #define NULL ((void *)0) /* quiets many warnings */
54 #define CE_CONT 0 /* continuation */
55 #define CE_NOTE 1 /* notice */
56 #define CE_WARN 2 /* warning */
57 #define CE_PANIC 3 /* panic */
58 #define CE_IGNORE 4 /* print nothing */
60 extern void cmn_err( int, const char *, ... );
66 typedef struct proc SUN_PROC_T
; /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
67 #define proc_t SUN_PROC_T /* replace all the original uses of (Solaris) proc_t */
68 #define curproc ((struct proc *)current_proc()) /* Called from probe context, must blacklist */
70 proc_t
* sprlock(pid_t pid
);
71 void sprunlock(proc_t
*p
);
77 int uread(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
);
78 int uwrite(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
);
84 int fuword8(user_addr_t
, uint8_t *);
85 int fuword16(user_addr_t
, uint16_t *);
86 int fuword32(user_addr_t
, uint32_t *);
87 int fuword64(user_addr_t
, uint64_t *);
89 void fuword8_noerr(user_addr_t
, uint8_t *);
90 void fuword16_noerr(user_addr_t
, uint16_t *);
91 void fuword32_noerr(user_addr_t
, uint32_t *);
92 void fuword64_noerr(user_addr_t
, uint64_t *);
94 int suword64(user_addr_t
, uint64_t value
);
95 int suword32(user_addr_t
, uint32_t value
);
96 int suword16(user_addr_t
, uint16_t value
);
97 int suword8(user_addr_t
, uint8_t value
);
102 extern lck_mtx_t cpu_lock
;
103 extern lck_mtx_t mod_lock
;
109 processorid_t cpu_id
; /* CPU number */
110 struct cpu
*cpu_next
; /* next existing CPU */
111 lck_rw_t cpu_ft_lock
; /* DTrace: fasttrap lock */
112 uintptr_t cpu_dtrace_caller
; /* DTrace: caller, if any */
113 hrtime_t cpu_dtrace_chillmark
; /* DTrace: chill mark time */
114 hrtime_t cpu_dtrace_chilled
; /* DTrace: total chill time */
115 boolean_t cpu_dtrace_invop_underway
; /* DTrace gaurds against invalid op re-entrancy */
118 extern cpu_t
*cpu_list
;
121 * The cpu_core structure consists of per-CPU state available in any context.
122 * On some architectures, this may mean that the page(s) containing the
123 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
124 * is up to the platform to assure that this is performed properly. Note that
125 * the structure is sized to avoid false sharing.
127 #define CPU_CACHE_COHERENCE_SIZE 64
128 #define CPUC_SIZE (sizeof (uint16_t))
129 #define CPUC_PADSIZE CPU_CACHE_COHERENCE_SIZE - CPUC_SIZE
131 typedef struct cpu_core
{
132 uint16_t cpuc_dtrace_flags
; /* DTrace flags */
133 uint8_t cpuc_pad
[CPUC_PADSIZE
]; /* padding */
134 uint64_t cpuc_dtrace_illval
; /* DTrace illegal value */
135 lck_mtx_t cpuc_pid_lock
; /* DTrace pid provider lock */
138 extern cpu_core_t
*cpu_core
; /* XXX TLB lockdown? */
139 extern unsigned int real_ncpus
;
140 extern int cpu_number(void); /* XXX #include <kern/cpu_number.h>. Called from probe context, must blacklist. */
142 #define CPU (&(cpu_list[cpu_number()])) /* Pointer to current CPU */
143 #define CPU_ON_INTR(cpup) ml_at_interrupt_context() /* always invoked on current cpu */
144 #define NCPU real_ncpus
147 * Routines used to register interest in cpu's being added to or removed
160 typedef int cpu_setup_func_t(cpu_setup_t
, int, void *);
162 extern void register_cpu_setup_func(cpu_setup_func_t
*, void *);
163 extern void unregister_cpu_setup_func(cpu_setup_func_t
*, void *);
172 #define CPU_DTRACE_NOFAULT 0x0001 /* Don't fault */
173 #define CPU_DTRACE_DROP 0x0002 /* Drop this ECB */
174 #define CPU_DTRACE_BADADDR 0x0004 /* DTrace fault: bad address */
175 #define CPU_DTRACE_BADALIGN 0x0008 /* DTrace fault: bad alignment */
176 #define CPU_DTRACE_DIVZERO 0x0010 /* DTrace fault: divide by zero */
177 #define CPU_DTRACE_ILLOP 0x0020 /* DTrace fault: illegal operation */
178 #define CPU_DTRACE_NOSCRATCH 0x0040 /* DTrace fault: out of scratch */
179 #define CPU_DTRACE_KPRIV 0x0080 /* DTrace fault: bad kernel access */
180 #define CPU_DTRACE_UPRIV 0x0100 /* DTrace fault: bad user access */
181 #define CPU_DTRACE_TUPOFLOW 0x0200 /* DTrace fault: tuple stack overflow */
183 //#define CPU_DTRACE_FAKERESTORE 0x0400 /* pid provider hint to getreg */
185 #define CPU_DTRACE_USTACK_FP 0x0400 /* pid provider hint to ustack() */
186 #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */
188 #define CPU_DTRACE_FAULT (CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \
189 CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \
190 CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \
191 CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW)
192 #define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP)
198 #define PRIV_DTRACE_KERNEL 3
199 #define PRIV_DTRACE_PROC 4
200 #define PRIV_DTRACE_USER 5
201 #define PRIV_PROC_OWNER 30
202 #define PRIV_PROC_ZONE 35
203 #define PRIV_ALL (-1) /* All privileges required */
206 #define PRIV_EFFECTIVE 0
208 typedef struct ucred cred_t
;
209 #define cr_suid cr_svuid
210 #define cr_sgid cr_svgid
212 extern cred_t
*dtrace_CRED(void); /* Safe to call from probe context. */
213 #define CRED() kauth_cred_get() /* Can't be called from probe context! */
214 extern int PRIV_POLICY_CHOICE(void *, int, int);
215 extern int PRIV_POLICY_ONLY(void *, int, int);
216 extern gid_t
crgetgid(const cred_t
*);
217 extern uid_t
crgetuid(const cred_t
*);
218 #define crgetzoneid(x) ((zoneid_t)0)
226 #define CY_LOW_LEVEL 0
227 #define CY_LOCK_LEVEL 1
228 #define CY_HIGH_LEVEL 2
229 #define CY_SOFT_LEVELS 2
232 typedef uintptr_t cyclic_id_t
;
233 typedef cyclic_id_t
*cyclic_id_list_t
;
234 typedef uint16_t cyc_level_t
;
235 typedef void (*cyc_func_t
)(void *);
237 #define CYCLIC_NONE ((cyclic_id_t)0)
239 typedef struct cyc_time
{
241 hrtime_t cyt_interval
;
244 typedef struct cyc_handler
{
247 cyc_level_t cyh_level
;
250 typedef struct cyc_omni_handler
{
251 void (*cyo_online
)(void *, cpu_t
*, cyc_handler_t
*, cyc_time_t
*);
252 void (*cyo_offline
)(void *, cpu_t
*, void *);
254 } cyc_omni_handler_t
;
256 extern cyclic_id_t
cyclic_add(cyc_handler_t
*, cyc_time_t
*);
257 extern void cyclic_remove(cyclic_id_t
);
259 extern cyclic_id_list_t
cyclic_add_omni(cyc_omni_handler_t
*);
260 extern void cyclic_remove_omni(cyclic_id_list_t
);
262 extern cyclic_id_t
cyclic_timer_add(cyc_handler_t
*, cyc_time_t
*);
263 extern void cyclic_timer_remove(cyclic_id_t
);
266 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision)
269 thread_call_t
dtrace_timeout(void (*func
)(void *, void *), void* arg
, uint64_t nanos
);
275 #define DDI_SUCCESS 0
276 #define DDI_FAILURE -1
278 #define DDI_DEV_T_NONE ((dev_t)-1)
279 #define DDI_DEV_T_ANY ((dev_t)-2)
280 #define DDI_MAJOR_T_UNKNOWN ((major_t)0)
282 #define DDI_PSEUDO "ddi_pseudo"
294 DDI_HOTPLUG_DETACH
= 3 /* detach, don't try to auto-unconfig */
297 #define DDI_PROP_SUCCESS 0
299 #define DDI_PROP_DONTPASS 1
300 typedef uint_t major_t
;
301 typedef uint_t minor_t
;
303 typedef struct __dev_info
*dev_info_t
;
305 extern void ddi_report_dev(dev_info_t
*);
306 extern int ddi_soft_state_init(void **, size_t, size_t);
307 extern void *ddi_get_soft_state(void *, int);
308 extern int ddi_soft_state_free(void *, int);
309 extern int ddi_soft_state_zalloc(void *, int);
310 extern void ddi_soft_state_fini(void **);
312 int ddi_getprop(dev_t dev
, dev_info_t
*dip
, int flags
, const char *name
, int defvalue
);
314 extern int ddi_prop_free(void *);
315 extern int ddi_prop_lookup_int_array(dev_t
, dev_info_t
*, uint_t
, char *, int **, uint_t
*);
317 extern int ddi_driver_major(dev_info_t
*);
319 extern int ddi_create_minor_node(dev_info_t
*, const char *, int, minor_t
, const char *, int);
320 extern void ddi_remove_minor_node(dev_info_t
*, char *);
322 extern major_t
getemajor(dev_t
);
323 extern minor_t
getminor(dev_t
);
325 extern int _dtrace_dev
;
326 extern dev_t
makedevice(major_t
, minor_t
);
329 * Kernel Debug Interface
332 typedef enum kdi_dtrace_set
{
333 KDI_DTSET_DTRACE_ACTIVATE
,
334 KDI_DTSET_DTRACE_DEACTIVATE
,
335 KDI_DTSET_KMDB_BPT_ACTIVATE
,
336 KDI_DTSET_KMDB_BPT_DEACTIVATE
339 extern int kdi_dtrace_set(kdi_dtrace_set_t
);
340 extern void debug_enter(char *);
343 * DTrace specific zone allocation
347 * To break dtrace memory usage out in a trackable
348 * fashion, uncomment the #define below. This will
349 * enable emulation of the general kalloc.XXX zones
350 * for most dtrace allocations. (kalloc.large is not
353 * #define DTRACE_MEMORY_ZONES 1
357 #if defined(DTRACE_MEMORY_ZONES)
358 void dtrace_alloc_init(void);
359 void *dtrace_alloc(vm_size_t
);
360 void dtrace_free(void *, vm_size_t
);
367 #define KM_SLEEP 0x00000000
368 #define KM_NOSLEEP 0x00000001
370 typedef struct vmem vmem_t
;
371 typedef struct kmem_cache kmem_cache_t
;
373 #define kmem_alloc dt_kmem_alloc /* Avoid clash with Darwin's kmem_alloc */
374 #define kmem_free dt_kmem_free /* Avoid clash with Darwin's kmem_free */
375 #define kmem_zalloc dt_kmem_zalloc /* Avoid clash with Darwin's kmem_zalloc */
376 extern void *dt_kmem_alloc(size_t, int);
377 extern void dt_kmem_free(void *, size_t);
378 extern void *dt_kmem_zalloc(size_t, int);
380 extern void *dt_kmem_alloc_aligned(size_t, size_t, int);
381 extern void *dt_kmem_zalloc_aligned(size_t, size_t, int);
382 extern void dt_kmem_free_aligned(void*, size_t);
384 extern kmem_cache_t
*
385 kmem_cache_create(char *, size_t, size_t, int (*)(void *, void *, int),
386 void (*)(void *, void *), void (*)(void *), void *, vmem_t
*, int);
387 extern void *kmem_cache_alloc(kmem_cache_t
*, int);
388 extern void kmem_cache_free(kmem_cache_t
*, void *);
389 extern void kmem_cache_destroy(kmem_cache_t
*);
395 typedef struct _kthread kthread_t
; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */
401 decl_simple_lock_data(extern,kmod_lock
)
403 /* Want to use Darwin's kmod_info in place of the Solaris modctl.
404 Can't typedef since the (many) usages in the code are "struct modctl *" */
405 extern kmod_info_t
*kmod
;
406 #define modctl kmod_info
408 #define mod_modname name
409 #define mod_loadcnt id
410 #define mod_next next
411 #define mod_loaded info_version /* XXX Is always > 0, hence TRUE */
418 #define DATAMODEL_MASK 0x0FF00000
420 #define DATAMODEL_ILP32 0x00100000
421 #define DATAMODEL_LP64 0x00200000
423 #define DATAMODEL_NONE 0
425 #if defined(__LP64__)
426 #define DATAMODEL_NATIVE DATAMODEL_LP64
428 #define DATAMODEL_NATIVE DATAMODEL_ILP32
429 #endif /* __LP64__ */
431 typedef unsigned int model_t
; /* For dtrace_instr_size_isa() prototype in <sys/dtrace.h> */
437 #define TQ_SLEEP 0x00 /* Can block for memory */
439 typedef uint_t pri_t
;
440 typedef struct taskq taskq_t
;
441 typedef void (task_func_t
)(void *);
442 typedef uintptr_t taskqid_t
;
444 extern taskq_t
*taskq_create(const char *, int, pri_t
, int, int, uint_t
);
445 extern taskqid_t
taskq_dispatch(taskq_t
*, task_func_t
, void *, uint_t
);
446 extern void taskq_destroy(taskq_t
*);
448 extern pri_t maxclsyspri
;
454 #define VMC_IDENTIFIER 0x00040000 /* not backed by memory */
455 #define VM_SLEEP 0x00000000 /* same as KM_SLEEP */
456 #define VM_BESTFIT 0x00000100
458 extern void *vmem_alloc(vmem_t
*, size_t, int);
459 extern vmem_t
*vmem_create(const char *, void *, size_t, size_t, void *,
460 void *, vmem_t
*, size_t, int);
461 extern void vmem_destroy(vmem_t
*);
462 extern void vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
);
468 static inline void atomic_add_32( uint32_t *theValue
, int32_t theAmount
)
470 (void)OSAddAtomic( theAmount
, (SInt32
*)theValue
);
477 typedef uintptr_t pc_t
;
478 typedef uintptr_t greg_t
; /* For dtrace_impl.h prototype of dtrace_getfp() */
479 extern struct regs
*find_user_regs( thread_t thread
);
480 extern vm_offset_t
dtrace_get_cpu_int_stack_top(void);
481 extern vm_offset_t
max_valid_stack_address(void); /* kern/thread.h */
482 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
); /* machine/pmap.h */
484 extern volatile int panicwait
; /* kern/debug.c */
485 #define panic_quiesce (panicwait)
487 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
489 extern void delay( int ); /* kern/clock.h */
491 extern int vuprintf(const char *, va_list);
493 extern boolean_t
dtxnu_is_RAM_page(ppnum_t
);
495 extern hrtime_t
dtrace_abs_to_nano(uint64_t);
497 __private_extern__
char * strstr(const char *, const char *);
501 #endif /* KERNEL_BUILD */
502 #endif /* _DTRACE_GLUE_H */