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/locks.h>
36 #include <kern/debug.h>
37 #include <kern/thread_call.h>
38 #include <kern/thread.h>
39 #include <machine/atomic.h>
40 #include <machine/machine_routines.h>
41 #include <sys/syslog.h>
42 #include <sys/ucred.h>
44 #include <mach/kmod.h>
46 #if defined(__i386__) || defined(__x86_64__)
53 #define CE_NOTE 1 /* notice */
54 #define CE_WARN 2 /* warning */
56 extern void cmn_err( int, const char *, ... );
62 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
63 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
64 #define curproc ((struct proc *)current_proc()) /* Called from probe context, must blacklist */
66 proc_t
* sprlock(pid_t pid
);
67 void sprunlock(proc_t
*p
);
69 void dtrace_sprlock(proc_t
*p
);
70 void dtrace_sprunlock(proc_t
*p
);
76 int uread(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
);
77 int uwrite(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
);
83 int fuword8(user_addr_t
, uint8_t *);
84 int fuword16(user_addr_t
, uint16_t *);
85 int fuword32(user_addr_t
, uint32_t *);
86 int fuword64(user_addr_t
, uint64_t *);
88 void fuword32_noerr(user_addr_t
, uint32_t *);
89 void fuword64_noerr(user_addr_t
, uint64_t *);
91 int suword64(user_addr_t
, uint64_t value
);
92 int suword32(user_addr_t
, uint32_t value
);
97 extern lck_mtx_t cpu_lock
;
98 extern lck_mtx_t cyc_lock
;
99 extern lck_mtx_t mod_lock
;
102 * wrap_timer_call: wrapper of timer_call for cyclic timers.
104 struct wrap_timer_call
;
109 typedef struct dtrace_cpu
{
110 processorid_t cpu_id
; /* CPU number */
111 struct dtrace_cpu
*cpu_next
; /* next existing CPU */
112 lck_rw_t cpu_ft_lock
; /* DTrace: fasttrap lock */
113 uintptr_t cpu_dtrace_caller
; /* DTrace: caller, if any */
114 hrtime_t cpu_dtrace_chillmark
; /* DTrace: chill mark time */
115 hrtime_t cpu_dtrace_chilled
; /* DTrace: total chill time */
116 boolean_t cpu_dtrace_invop_underway
; /* DTrace gaurds against invalid op re-entrancy */
118 /* Local cyclic timers on this CPU */
119 LIST_HEAD(cyc_list_head
, wrap_timer_call
) cpu_cyc_list
;
122 extern dtrace_cpu_t
*cpu_list
;
125 * The cpu_core structure consists of per-CPU state available in any context.
126 * On some architectures, this may mean that the page(s) containing the
127 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
128 * is up to the platform to assure that this is performed properly. Note that
129 * the structure is sized to avoid false sharing.
131 #define CPU_CACHE_COHERENCE_SIZE 64
133 typedef struct cpu_core
{
134 uint64_t cpuc_dtrace_illval
; /* DTrace illegal value */
135 lck_mtx_t cpuc_pid_lock
; /* DTrace pid provider lock */
136 uint16_t cpuc_dtrace_flags
; /* DTrace flags */
137 uint64_t cpuc_missing_tos
; /* Addr. of top most stack frame if missing */
138 uint8_t cpuc_pad
[CPU_CACHE_COHERENCE_SIZE
- sizeof(uint64_t) - sizeof(lck_mtx_t
) - sizeof(uint16_t) - sizeof(uint64_t)]; /* padding */
141 extern cpu_core_t
*cpu_core
;
143 extern unsigned int dtrace_max_cpus
; /* max number of enabled cpus */
144 #define NCPU dtrace_max_cpus
146 extern int cpu_number(void); /* From #include <kern/cpu_number.h>. Called from probe context, must blacklist. */
148 #define CPU (&(cpu_list[cpu_number()])) /* Pointer to current CPU */
149 #define CPU_ON_INTR(cpup) ml_at_interrupt_context() /* always invoked on current cpu */
152 * Routines used to register interest in cpu's being added to or removed
165 typedef int cpu_setup_func_t(cpu_setup_t
, int, void *);
167 extern void register_cpu_setup_func(cpu_setup_func_t
*, void *);
168 extern void unregister_cpu_setup_func(cpu_setup_func_t
*, void *);
177 #define CPU_DTRACE_NOFAULT 0x0001 /* Don't fault */
178 #define CPU_DTRACE_DROP 0x0002 /* Drop this ECB */
179 #define CPU_DTRACE_BADADDR 0x0004 /* DTrace fault: bad address */
180 #define CPU_DTRACE_BADALIGN 0x0008 /* DTrace fault: bad alignment */
181 #define CPU_DTRACE_DIVZERO 0x0010 /* DTrace fault: divide by zero */
182 #define CPU_DTRACE_ILLOP 0x0020 /* DTrace fault: illegal operation */
183 #define CPU_DTRACE_NOSCRATCH 0x0040 /* DTrace fault: out of scratch */
184 #define CPU_DTRACE_KPRIV 0x0080 /* DTrace fault: bad kernel access */
185 #define CPU_DTRACE_UPRIV 0x0100 /* DTrace fault: bad user access */
186 #define CPU_DTRACE_TUPOFLOW 0x0200 /* DTrace fault: tuple stack overflow */
187 #define CPU_DTRACE_USTACK_FP 0x0400 /* pid provider hint to ustack() */
188 #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */
189 #define CPU_DTRACE_BADSTACK 0x1000 /* DTrace fault: bad stack */
191 #define CPU_DTRACE_FAULT (CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \
192 CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \
193 CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \
194 CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW | \
196 #define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP)
202 /* Keep the compiler happy */
203 struct dtrace_module_symbols
;
205 /* Solaris' modctl structure, greatly simplified, shadowing parts of xnu kmod structure. */
206 typedef struct modctl
{
207 struct modctl
*mod_next
;
208 struct modctl
*mod_stale
; // stale module chain
209 uint32_t mod_id
; // the kext unique identifier
210 char mod_modname
[KMOD_MAX_NAME
];
213 uint16_t mod_flags
; // See flags below
214 int mod_nenabled
; // # of enabled DTrace probes in module
215 vm_address_t mod_address
; // starting address (of Mach-o header blob)
216 vm_size_t mod_size
; // total size (of blob)
218 struct dtrace_module_symbols
* mod_user_symbols
;
221 /* Definitions for mod_flags */
222 #define MODCTL_IS_MACH_KERNEL 0x01 // This module represents /mach_kernel
223 #define MODCTL_HAS_KERNEL_SYMBOLS 0x02 // Kernel symbols (nlist) are available
224 #define MODCTL_FBT_PROBES_PROVIDED 0x04 // fbt probes have been provided
225 #define MODCTL_FBT_INVALID 0x08 // Module is invalid for fbt probes
226 #define MODCTL_SDT_PROBES_PROVIDED 0x10 // sdt probes have been provided
227 #define MODCTL_SDT_INVALID 0x20 // Module is invalid for sdt probes
228 #define MODCTL_HAS_UUID 0x40 // Module has UUID
229 #define MODCTL_FBT_PROVIDE_BLACKLISTED_PROBES 0x200 // fbt provider must provide blacklisted probes
230 #define MODCTL_FBT_BLACKLISTED_PROBES_PROVIDED 0x400 // fbt blacklisted probes have been provided
231 #define MODCTL_IS_STATIC_KEXT 0x800 // module is a static kext
233 /* Simple/singular mod_flags accessors */
234 #define MOD_IS_MACH_KERNEL(mod) (mod->mod_flags & MODCTL_IS_MACH_KERNEL)
235 #define MOD_HAS_KERNEL_SYMBOLS(mod) (mod->mod_flags & MODCTL_HAS_KERNEL_SYMBOLS)
236 #define MOD_HAS_USERSPACE_SYMBOLS(mod) (mod->mod_user_symbols) /* No point in duplicating state in the flags bits */
237 #define MOD_FBT_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_FBT_PROBES_PROVIDED)
238 #define MOD_FBT_INVALID(mod) (mod->mod_flags & MODCTL_FBT_INVALID)
239 #define MOD_SDT_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_SDT_PROBES_PROVIDED)
240 #define MOD_SDT_INVALID(mod) (mod->mod_flags & MODCTL_SDT_INVALID)
241 #define MOD_HAS_UUID(mod) (mod->mod_flags & MODCTL_HAS_UUID)
242 #define MOD_FBT_BLACKLISTED_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_FBT_BLACKLISTED_PROBES_PROVIDED)
243 #define MOD_FBT_PROVIDE_BLACKLISTED_PROBES(mod) (mod->mod_flags & MODCTL_FBT_PROVIDE_BLACKLISTED_PROBES)
244 #define MOD_IS_STATIC_KEXT(mod) (mod->mod_flags & MODCTL_IS_STATIC_KEXT)
246 /* Compound accessors */
247 #define MOD_FBT_BLACKLISTED_PROBES_DONE(mod) (MOD_FBT_BLACKLISTED_PROBES_PROVIDED(mod) || !MOD_FBT_PROVIDE_BLACKLISTED_PROBES(mod))
248 #define MOD_FBT_DONE(mod) ((MOD_FBT_PROBES_PROVIDED(mod) && MOD_FBT_BLACKLISTED_PROBES_DONE(mod)) || MOD_FBT_INVALID(mod))
249 #define MOD_SDT_DONE(mod) (MOD_SDT_PROBES_PROVIDED(mod) || MOD_SDT_INVALID(mod))
250 #define MOD_SYMBOLS_DONE(mod) (MOD_FBT_DONE(mod) && MOD_SDT_DONE(mod))
252 extern modctl_t
*dtrace_modctl_list
;
254 extern int dtrace_addr_in_module(void*, struct modctl
*);
260 #define PRIV_DTRACE_KERNEL 3
261 #define PRIV_DTRACE_PROC 4
262 #define PRIV_DTRACE_USER 5
263 #define PRIV_PROC_OWNER 30
264 #define PRIV_PROC_ZONE 35
265 #define PRIV_ALL (-1) /* All privileges required */
268 #define PRIV_EFFECTIVE 0
270 typedef struct ucred cred_t
;
271 #define cr_suid cr_svuid
272 #define cr_sgid cr_svgid
274 extern cred_t
*dtrace_CRED(void); /* Safe to call from probe context. */
275 #define CRED() kauth_cred_get() /* Can't be called from probe context! */
276 extern int PRIV_POLICY_CHOICE(void *, int, int);
277 extern int PRIV_POLICY_ONLY(void *, int, int);
278 extern uid_t
crgetuid(const cred_t
*);
279 #define crgetzoneid(x) ((zoneid_t)0)
284 #define CY_LOW_LEVEL 0
285 #define CY_HIGH_LEVEL 2
288 typedef uintptr_t cyclic_id_t
;
289 typedef cyclic_id_t
*cyclic_id_list_t
;
290 typedef uint16_t cyc_level_t
;
291 typedef void (*cyc_func_t
)(void *);
293 #define CYCLIC_NONE ((cyclic_id_t)0)
295 typedef struct cyc_time
{
297 hrtime_t cyt_interval
;
300 typedef struct cyc_handler
{
303 cyc_level_t cyh_level
;
306 typedef struct cyc_omni_handler
{
307 void (*cyo_online
)(void *, dtrace_cpu_t
*, cyc_handler_t
*, cyc_time_t
*);
308 void (*cyo_offline
)(void *, dtrace_cpu_t
*, void *);
310 } cyc_omni_handler_t
;
312 extern void dtrace_install_cpu_hooks(void);
314 extern cyclic_id_t
cyclic_add(cyc_handler_t
*, cyc_time_t
*);
315 extern void cyclic_remove(cyclic_id_t
);
317 extern cyclic_id_list_t
cyclic_add_omni(cyc_omni_handler_t
*);
318 extern void cyclic_remove_omni(cyclic_id_list_t
);
320 extern cyclic_id_t
cyclic_timer_add(cyc_handler_t
*, cyc_time_t
*);
321 extern void cyclic_timer_remove(cyclic_id_t
);
327 #define DDI_SUCCESS 0
328 #define DDI_FAILURE -1
330 #define DDI_PSEUDO "ddi_pseudo"
336 DDI_HOTPLUG_DETACH
= 3 /* detach, don't try to auto-unconfig */
339 #define DDI_PROP_SUCCESS 0
341 #define DDI_PROP_DONTPASS 1
342 typedef uint_t major_t
;
343 typedef uint_t minor_t
;
345 typedef struct __dev_info
*dev_info_t
;
347 extern int ddi_driver_major(dev_info_t
*);
349 extern int ddi_create_minor_node(dev_info_t
*, const char *, int, minor_t
, const char *, int);
350 extern void ddi_remove_minor_node(dev_info_t
*, char *);
352 extern major_t
getemajor(dev_t
);
353 extern minor_t
getminor(dev_t
);
356 * Kernel Debug Interface
358 extern void debug_enter(char *);
361 * DTrace specific zone allocation
368 #define KM_SLEEP 0x00000000
369 #define KM_NOSLEEP 0x00000001
371 typedef struct vmem vmem_t
;
372 typedef struct kmem_cache kmem_cache_t
;
374 #define kmem_free dt_kmem_free /* Avoid clash with Darwin's kmem_free */
375 #define kmem_free_aligned dt_kmem_free_aligned
377 #define kmem_alloc(size, kmflag) \
378 ({ VM_ALLOC_SITE_STATIC(0, 0); \
379 dt_kmem_alloc_site(size, kmflag, &site); })
381 extern void *dt_kmem_alloc_site(size_t, int, vm_allocation_site_t
*);
382 extern void dt_kmem_free(void *, size_t);
384 #define kmem_zalloc(size, kmflag) \
385 ({ VM_ALLOC_SITE_STATIC(0, 0); \
386 dt_kmem_zalloc_site(size, kmflag, &site); })
388 extern void *dt_kmem_zalloc_site(size_t, int, vm_allocation_site_t
*);
390 #define kmem_alloc_aligned(size, align, kmflag) \
391 ({ VM_ALLOC_SITE_STATIC(0, 0); \
392 dt_kmem_alloc_aligned_site(size, align, kmflag, &site); })
393 extern void *dt_kmem_alloc_aligned_site(size_t, size_t, int, vm_allocation_site_t
*);
395 #define kmem_zalloc_aligned(size, align, kmflag) \
396 ({ VM_ALLOC_SITE_STATIC(0, 0); \
397 dt_kmem_zalloc_aligned_site(size, align, kmflag, &site); })
398 extern void *dt_kmem_zalloc_aligned_site(size_t, size_t, int, vm_allocation_site_t
*);
400 extern void dt_kmem_free_aligned(void*, size_t);
402 extern kmem_cache_t
*
403 kmem_cache_create(const char *, size_t, size_t, int (*)(void *, void *, int),
404 void (*)(void *, void *), void (*)(void *), void *, vmem_t
*, int);
405 extern void *kmem_cache_alloc(kmem_cache_t
*, int);
406 extern void kmem_cache_free(kmem_cache_t
*, void *);
407 extern void kmem_cache_destroy(kmem_cache_t
*);
413 typedef struct _kthread kthread_t
; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */
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 VMC_IDENTIFIER 0x00040000 /* not backed by memory */
438 #define VM_SLEEP 0x00000000 /* same as KM_SLEEP */
439 #define VM_BESTFIT 0x00000100
441 extern void *vmem_alloc(vmem_t
*, size_t, int);
442 extern vmem_t
*vmem_create(const char *, void *, size_t, size_t, void *,
443 void *, vmem_t
*, size_t, int);
444 extern void vmem_destroy(vmem_t
*);
445 extern void vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
);
451 typedef uintptr_t pc_t
;
452 typedef uintptr_t greg_t
; /* For dtrace_impl.h prototype of dtrace_getfp() */
453 #if defined(__arm__) || defined(__arm64__)
454 #define regs arm_saved_state
456 extern struct regs
*find_user_regs( thread_t thread
);
457 extern vm_offset_t
dtrace_get_cpu_int_stack_top(void);
459 #define panic_quiesce (panic_active())
461 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
463 extern int vuprintf(const char *, va_list);
465 extern hrtime_t
dtrace_abs_to_nano(uint64_t);
467 __private_extern__
const char * strstr(const char *, const char *);
468 const void* bsearch(const void*, const void*, size_t, size_t, int (*compar
)(const void *, const void *));
470 int dtrace_copy_maxsize(void);
471 int dtrace_buffer_copyout(const void*, user_addr_t
, vm_size_t
);
474 #define DTRACE_NCLIENTS 32
479 * Safe counted string compare against a literal string. The sizeof() intentionally
480 * counts the trailing NUL, and so ensures that all the characters in the literal
481 * can participate in the comparison.
483 #define LIT_STRNEQL(s1, lit_s2) (0 == strncmp( (s1), (lit_s2), sizeof((lit_s2)) ))
485 #define KERNELBASE VM_MIN_KERNEL_ADDRESS
486 #endif /* KERNEL_BUILD */
487 #endif /* _DTRACE_GLUE_H */