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 <kern/thread.h>
39 #include <machine/machine_routines.h>
40 #include <sys/syslog.h>
41 #include <sys/ucred.h>
43 #include <mach/kmod.h>
44 #include <libkern/OSAtomic.h>
48 #define NULL ((void *)0) /* quiets many warnings */
55 #define CE_CONT 0 /* continuation */
56 #define CE_NOTE 1 /* notice */
57 #define CE_WARN 2 /* warning */
58 #define CE_PANIC 3 /* panic */
59 #define CE_IGNORE 4 /* print nothing */
61 extern void cmn_err( int, const char *, ... );
67 typedef struct proc SUN_PROC_T
; /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
68 #define proc_t SUN_PROC_T /* replace all the original uses of (Solaris) proc_t */
69 #define curproc ((struct proc *)current_proc()) /* Called from probe context, must blacklist */
71 proc_t
* sprlock(pid_t pid
);
72 void sprunlock(proc_t
*p
);
78 int uread(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
);
79 int uwrite(proc_t
*p
, void *buf
, user_size_t len
, user_addr_t a
);
85 int fuword8(user_addr_t
, uint8_t *);
86 int fuword16(user_addr_t
, uint16_t *);
87 int fuword32(user_addr_t
, uint32_t *);
88 int fuword64(user_addr_t
, uint64_t *);
90 void fuword8_noerr(user_addr_t
, uint8_t *);
91 void fuword16_noerr(user_addr_t
, uint16_t *);
92 void fuword32_noerr(user_addr_t
, uint32_t *);
93 void fuword64_noerr(user_addr_t
, uint64_t *);
95 int suword64(user_addr_t
, uint64_t value
);
96 int suword32(user_addr_t
, uint32_t value
);
97 int suword16(user_addr_t
, uint16_t value
);
98 int suword8(user_addr_t
, uint8_t value
);
103 extern lck_mtx_t cpu_lock
;
104 extern lck_mtx_t mod_lock
;
110 processorid_t cpu_id
; /* CPU number */
111 struct 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 */
119 extern cpu_t
*cpu_list
;
122 * The cpu_core structure consists of per-CPU state available in any context.
123 * On some architectures, this may mean that the page(s) containing the
124 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
125 * is up to the platform to assure that this is performed properly. Note that
126 * the structure is sized to avoid false sharing.
128 #define CPU_CACHE_COHERENCE_SIZE 64
129 #define CPUC_SIZE (sizeof (uint16_t))
130 #define CPUC_PADSIZE CPU_CACHE_COHERENCE_SIZE - CPUC_SIZE
132 typedef struct cpu_core
{
133 uint16_t cpuc_dtrace_flags
; /* DTrace flags */
134 uint8_t cpuc_pad
[CPUC_PADSIZE
]; /* padding */
135 uint64_t cpuc_dtrace_illval
; /* DTrace illegal value */
136 lck_mtx_t cpuc_pid_lock
; /* DTrace pid provider lock */
139 extern cpu_core_t
*cpu_core
; /* XXX TLB lockdown? */
140 extern unsigned int real_ncpus
;
141 extern int cpu_number(void); /* XXX #include <kern/cpu_number.h>. Called from probe context, must blacklist. */
143 #define CPU (&(cpu_list[cpu_number()])) /* Pointer to current CPU */
144 #define CPU_ON_INTR(cpup) ml_at_interrupt_context() /* always invoked on current cpu */
145 #define NCPU real_ncpus
148 * Routines used to register interest in cpu's being added to or removed
161 typedef int cpu_setup_func_t(cpu_setup_t
, int, void *);
163 extern void register_cpu_setup_func(cpu_setup_func_t
*, void *);
164 extern void unregister_cpu_setup_func(cpu_setup_func_t
*, void *);
173 #define CPU_DTRACE_NOFAULT 0x0001 /* Don't fault */
174 #define CPU_DTRACE_DROP 0x0002 /* Drop this ECB */
175 #define CPU_DTRACE_BADADDR 0x0004 /* DTrace fault: bad address */
176 #define CPU_DTRACE_BADALIGN 0x0008 /* DTrace fault: bad alignment */
177 #define CPU_DTRACE_DIVZERO 0x0010 /* DTrace fault: divide by zero */
178 #define CPU_DTRACE_ILLOP 0x0020 /* DTrace fault: illegal operation */
179 #define CPU_DTRACE_NOSCRATCH 0x0040 /* DTrace fault: out of scratch */
180 #define CPU_DTRACE_KPRIV 0x0080 /* DTrace fault: bad kernel access */
181 #define CPU_DTRACE_UPRIV 0x0100 /* DTrace fault: bad user access */
182 #define CPU_DTRACE_TUPOFLOW 0x0200 /* DTrace fault: tuple stack overflow */
184 //#define CPU_DTRACE_FAKERESTORE 0x0400 /* pid provider hint to getreg */
186 #define CPU_DTRACE_USTACK_FP 0x0400 /* pid provider hint to ustack() */
187 #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */
189 #define CPU_DTRACE_FAULT (CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \
190 CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \
191 CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \
192 CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW)
193 #define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP)
199 #define PRIV_DTRACE_KERNEL 3
200 #define PRIV_DTRACE_PROC 4
201 #define PRIV_DTRACE_USER 5
202 #define PRIV_PROC_OWNER 30
203 #define PRIV_PROC_ZONE 35
204 #define PRIV_ALL (-1) /* All privileges required */
207 #define PRIV_EFFECTIVE 0
209 typedef struct ucred cred_t
;
210 #define cr_suid cr_svuid
211 #define cr_sgid cr_svgid
213 extern cred_t
*dtrace_CRED(void); /* Safe to call from probe context. */
214 #define CRED() kauth_cred_get() /* Can't be called from probe context! */
215 extern int PRIV_POLICY_CHOICE(void *, int, int);
216 extern int PRIV_POLICY_ONLY(void *, int, int);
217 extern gid_t
crgetgid(const cred_t
*);
218 extern uid_t
crgetuid(const cred_t
*);
219 #define crgetzoneid(x) ((zoneid_t)0)
227 #define CY_LOW_LEVEL 0
228 #define CY_LOCK_LEVEL 1
229 #define CY_HIGH_LEVEL 2
230 #define CY_SOFT_LEVELS 2
233 typedef uintptr_t cyclic_id_t
;
234 typedef cyclic_id_t
*cyclic_id_list_t
;
235 typedef uint16_t cyc_level_t
;
236 typedef void (*cyc_func_t
)(void *);
238 #define CYCLIC_NONE ((cyclic_id_t)0)
240 typedef struct cyc_time
{
242 hrtime_t cyt_interval
;
245 typedef struct cyc_handler
{
248 cyc_level_t cyh_level
;
251 typedef struct cyc_omni_handler
{
252 void (*cyo_online
)(void *, cpu_t
*, cyc_handler_t
*, cyc_time_t
*);
253 void (*cyo_offline
)(void *, cpu_t
*, void *);
255 } cyc_omni_handler_t
;
257 extern cyclic_id_t
cyclic_add(cyc_handler_t
*, cyc_time_t
*);
258 extern void cyclic_remove(cyclic_id_t
);
260 extern cyclic_id_list_t
cyclic_add_omni(cyc_omni_handler_t
*);
261 extern void cyclic_remove_omni(cyclic_id_list_t
);
263 extern cyclic_id_t
cyclic_timer_add(cyc_handler_t
*, cyc_time_t
*);
264 extern void cyclic_timer_remove(cyclic_id_t
);
267 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision)
270 thread_call_t
dtrace_timeout(void (*func
)(void *, void *), void* arg
, uint64_t nanos
);
276 #define DDI_SUCCESS 0
277 #define DDI_FAILURE -1
279 #define DDI_DEV_T_NONE ((dev_t)-1)
280 #define DDI_DEV_T_ANY ((dev_t)-2)
281 #define DDI_MAJOR_T_UNKNOWN ((major_t)0)
283 #define DDI_PSEUDO "ddi_pseudo"
295 DDI_HOTPLUG_DETACH
= 3 /* detach, don't try to auto-unconfig */
298 #define DDI_PROP_SUCCESS 0
300 #define DDI_PROP_DONTPASS 1
301 typedef uint_t major_t
;
302 typedef uint_t minor_t
;
304 typedef struct __dev_info
*dev_info_t
;
306 extern void ddi_report_dev(dev_info_t
*);
307 extern int ddi_soft_state_init(void **, size_t, size_t);
308 extern void *ddi_get_soft_state(void *, int);
309 extern int ddi_soft_state_free(void *, int);
310 extern int ddi_soft_state_zalloc(void *, int);
311 extern void ddi_soft_state_fini(void **);
313 int ddi_getprop(dev_t dev
, dev_info_t
*dip
, int flags
, const char *name
, int defvalue
);
315 extern int ddi_prop_free(void *);
316 extern int ddi_prop_lookup_int_array(dev_t
, dev_info_t
*, uint_t
, char *, int **, uint_t
*);
318 extern int ddi_driver_major(dev_info_t
*);
320 extern int ddi_create_minor_node(dev_info_t
*, const char *, int, minor_t
, const char *, int);
321 extern void ddi_remove_minor_node(dev_info_t
*, char *);
323 extern major_t
getemajor(dev_t
);
324 extern minor_t
getminor(dev_t
);
326 extern int _dtrace_dev
;
327 extern dev_t
makedevice(major_t
, minor_t
);
330 * Kernel Debug Interface
333 typedef enum kdi_dtrace_set
{
334 KDI_DTSET_DTRACE_ACTIVATE
,
335 KDI_DTSET_DTRACE_DEACTIVATE
,
336 KDI_DTSET_KMDB_BPT_ACTIVATE
,
337 KDI_DTSET_KMDB_BPT_DEACTIVATE
340 extern int kdi_dtrace_set(kdi_dtrace_set_t
);
341 extern void debug_enter(char *);
344 * DTrace specific zone allocation
348 * To break dtrace memory usage out in a trackable
349 * fashion, uncomment the #define below. This will
350 * enable emulation of the general kalloc.XXX zones
351 * for most dtrace allocations. (kalloc.large is not
354 * #define DTRACE_MEMORY_ZONES 1
358 #if defined(DTRACE_MEMORY_ZONES)
359 void dtrace_alloc_init(void);
360 void *dtrace_alloc(vm_size_t
);
361 void dtrace_free(void *, vm_size_t
);
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_alloc dt_kmem_alloc /* Avoid clash with Darwin's kmem_alloc */
375 #define kmem_free dt_kmem_free /* Avoid clash with Darwin's kmem_free */
376 #define kmem_zalloc dt_kmem_zalloc /* Avoid clash with Darwin's kmem_zalloc */
377 extern void *dt_kmem_alloc(size_t, int);
378 extern void dt_kmem_free(void *, size_t);
379 extern void *dt_kmem_zalloc(size_t, int);
381 extern void *dt_kmem_alloc_aligned(size_t, size_t, int);
382 extern void *dt_kmem_zalloc_aligned(size_t, size_t, int);
383 extern void dt_kmem_free_aligned(void*, size_t);
385 extern kmem_cache_t
*
386 kmem_cache_create(char *, size_t, size_t, int (*)(void *, void *, int),
387 void (*)(void *, void *), void (*)(void *), void *, vmem_t
*, int);
388 extern void *kmem_cache_alloc(kmem_cache_t
*, int);
389 extern void kmem_cache_free(kmem_cache_t
*, void *);
390 extern void kmem_cache_destroy(kmem_cache_t
*);
396 typedef struct _kthread kthread_t
; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */
402 decl_simple_lock_data(extern,kmod_lock
)
404 /* Want to use Darwin's kmod_info in place of the Solaris modctl.
405 Can't typedef since the (many) usages in the code are "struct modctl *" */
406 extern kmod_info_t
*kmod
;
407 #define modctl kmod_info
409 #define mod_modname name
410 #define mod_loadcnt id
411 #define mod_next next
412 #define mod_loaded info_version /* XXX Is always > 0, hence TRUE */
419 #define DATAMODEL_MASK 0x0FF00000
421 #define DATAMODEL_ILP32 0x00100000
422 #define DATAMODEL_LP64 0x00200000
424 #define DATAMODEL_NONE 0
426 #if defined(__LP64__)
427 #define DATAMODEL_NATIVE DATAMODEL_LP64
429 #define DATAMODEL_NATIVE DATAMODEL_ILP32
430 #endif /* __LP64__ */
432 typedef unsigned int model_t
; /* For dtrace_instr_size_isa() prototype in <sys/dtrace.h> */
438 #define TQ_SLEEP 0x00 /* Can block for memory */
440 typedef uint_t pri_t
;
441 typedef struct taskq taskq_t
;
442 typedef void (task_func_t
)(void *);
443 typedef uintptr_t taskqid_t
;
445 extern taskq_t
*taskq_create(const char *, int, pri_t
, int, int, uint_t
);
446 extern taskqid_t
taskq_dispatch(taskq_t
*, task_func_t
, void *, uint_t
);
447 extern void taskq_destroy(taskq_t
*);
449 extern pri_t maxclsyspri
;
455 #define VMC_IDENTIFIER 0x00040000 /* not backed by memory */
456 #define VM_SLEEP 0x00000000 /* same as KM_SLEEP */
457 #define VM_BESTFIT 0x00000100
459 extern void *vmem_alloc(vmem_t
*, size_t, int);
460 extern vmem_t
*vmem_create(const char *, void *, size_t, size_t, void *,
461 void *, vmem_t
*, size_t, int);
462 extern void vmem_destroy(vmem_t
*);
463 extern void vmem_free(vmem_t
*vmp
, void *vaddr
, size_t size
);
469 static inline void atomic_add_32( uint32_t *theValue
, int32_t theAmount
)
471 (void)OSAddAtomic( theAmount
, (SInt32
*)theValue
);
478 typedef uintptr_t pc_t
;
479 typedef uintptr_t greg_t
; /* For dtrace_impl.h prototype of dtrace_getfp() */
480 extern struct regs
*find_user_regs( thread_t thread
);
481 extern vm_offset_t
dtrace_get_cpu_int_stack_top(void);
482 extern vm_offset_t
max_valid_stack_address(void); /* kern/thread.h */
483 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
); /* machine/pmap.h */
485 extern volatile int panicwait
; /* kern/debug.c */
486 #define panic_quiesce (panicwait)
488 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
490 extern void delay( int ); /* kern/clock.h */
492 extern int vuprintf(const char *, va_list);
494 extern boolean_t
dtxnu_is_RAM_page(ppnum_t
);
496 extern hrtime_t
dtrace_abs_to_nano(uint64_t);
498 __private_extern__
char * strstr(const char *, const char *);
502 #endif /* KERNEL_BUILD */
503 #endif /* _DTRACE_GLUE_H */