]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/dtrace_glue.h
xnu-1228.tar.gz
[apple/xnu.git] / bsd / sys / dtrace_glue.h
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 #ifndef _DTRACE_GLUE_H
30 #define _DTRACE_GLUE_H
31
32 #ifdef KERNEL_BUILD
33
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>
41 #include <stdarg.h>
42 #include <mach/kmod.h>
43 #include <libkern/OSAtomic.h>
44
45 #ifdef QUIET_PLEASE
46 #ifndef NULL
47 #define NULL ((void *)0) /* quiets many warnings */
48 #endif
49 #endif
50
51 /*
52 * cmn_err
53 */
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 */
59
60 extern void cmn_err( int, const char *, ... );
61
62 /*
63 * pid/proc
64 */
65
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 */
69
70 proc_t* sprlock(pid_t pid);
71 void sprunlock(proc_t *p);
72
73 /*
74 * uread/uwrite
75 */
76
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);
79
80 /*
81 * fuword / suword
82 */
83
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 *);
88
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 *);
93
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);
98
99 /*
100 * cpuvar
101 */
102 extern lck_mtx_t cpu_lock;
103 extern lck_mtx_t mod_lock;
104
105 /*
106 * Per-CPU data.
107 */
108 typedef struct cpu {
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 */
116 } cpu_t;
117
118 extern cpu_t *cpu_list;
119
120 /*
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.
126 */
127 #define CPU_CACHE_COHERENCE_SIZE 64
128 #define CPUC_SIZE (sizeof (uint16_t))
129 #define CPUC_PADSIZE CPU_CACHE_COHERENCE_SIZE - CPUC_SIZE
130
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 */
136 } cpu_core_t;
137
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. */
141
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
145
146 /*
147 * Routines used to register interest in cpu's being added to or removed
148 * from the system.
149 */
150 typedef enum {
151 CPU_INIT,
152 CPU_CONFIG,
153 CPU_UNCONFIG,
154 CPU_ON,
155 CPU_OFF,
156 CPU_CPUPART_IN,
157 CPU_CPUPART_OUT
158 } cpu_setup_t;
159
160 typedef int cpu_setup_func_t(cpu_setup_t, int, void *);
161
162 extern void register_cpu_setup_func(cpu_setup_func_t *, void *);
163 extern void unregister_cpu_setup_func(cpu_setup_func_t *, void *);
164
165 /*
166 * CPU_DTRACE
167 */
168
169 /*
170 * DTrace flags.
171 */
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 */
182 #if defined(__sparc)
183 //#define CPU_DTRACE_FAKERESTORE 0x0400 /* pid provider hint to getreg */
184 #endif
185 #define CPU_DTRACE_USTACK_FP 0x0400 /* pid provider hint to ustack() */
186 #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */
187
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)
193
194 /*
195 * cred_t
196 */
197 /* Privileges */
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 */
204
205 /* Privilege sets */
206 #define PRIV_EFFECTIVE 0
207
208 typedef struct ucred cred_t;
209 #define cr_suid cr_svuid
210 #define cr_sgid cr_svgid
211
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)
219
220 #define crhold(a) {}
221 #define crfree(a) {}
222
223 /*
224 * "cyclic"
225 */
226 #define CY_LOW_LEVEL 0
227 #define CY_LOCK_LEVEL 1
228 #define CY_HIGH_LEVEL 2
229 #define CY_SOFT_LEVELS 2
230 #define CY_LEVELS 3
231
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 *);
236
237 #define CYCLIC_NONE ((cyclic_id_t)0)
238
239 typedef struct cyc_time {
240 hrtime_t cyt_when;
241 hrtime_t cyt_interval;
242 } cyc_time_t;
243
244 typedef struct cyc_handler {
245 cyc_func_t cyh_func;
246 void *cyh_arg;
247 cyc_level_t cyh_level;
248 } cyc_handler_t;
249
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 *);
253 void *cyo_arg;
254 } cyc_omni_handler_t;
255
256 extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *);
257 extern void cyclic_remove(cyclic_id_t);
258
259 extern cyclic_id_list_t cyclic_add_omni(cyc_omni_handler_t *);
260 extern void cyclic_remove_omni(cyclic_id_list_t);
261
262 extern cyclic_id_t cyclic_timer_add(cyc_handler_t *, cyc_time_t *);
263 extern void cyclic_timer_remove(cyclic_id_t);
264
265 /*
266 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision)
267 */
268
269 thread_call_t dtrace_timeout(void (*func)(void *, void *), void* arg, uint64_t nanos);
270
271 /*
272 * ddi
273 */
274
275 #define DDI_SUCCESS 0
276 #define DDI_FAILURE -1
277
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)
281
282 #define DDI_PSEUDO "ddi_pseudo"
283
284 typedef enum {
285 DDI_ATTACH = 0,
286 DDI_RESUME = 1,
287 DDI_PM_RESUME = 2
288 } ddi_attach_cmd_t;
289
290 typedef enum {
291 DDI_DETACH = 0,
292 DDI_SUSPEND = 1,
293 DDI_PM_SUSPEND = 2,
294 DDI_HOTPLUG_DETACH = 3 /* detach, don't try to auto-unconfig */
295 } ddi_detach_cmd_t;
296
297 #define DDI_PROP_SUCCESS 0
298
299 #define DDI_PROP_DONTPASS 1
300 typedef uint_t major_t;
301 typedef uint_t minor_t;
302
303 typedef struct __dev_info *dev_info_t;
304
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 **);
311
312 int ddi_getprop(dev_t dev, dev_info_t *dip, int flags, const char *name, int defvalue);
313
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 *);
316
317 extern int ddi_driver_major(dev_info_t *);
318
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 *);
321
322 extern major_t getemajor(dev_t);
323 extern minor_t getminor(dev_t);
324
325 extern int _dtrace_dev;
326 extern dev_t makedevice(major_t, minor_t);
327
328 /*
329 * Kernel Debug Interface
330 */
331
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
337 } kdi_dtrace_set_t;
338
339 extern int kdi_dtrace_set(kdi_dtrace_set_t);
340 extern void debug_enter(char *);
341
342 /*
343 * DTrace specific zone allocation
344 */
345
346 /*
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
351 * emulated)
352 *
353 * #define DTRACE_MEMORY_ZONES 1
354 *
355 */
356
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);
361 #endif
362
363 /*
364 * kmem
365 */
366
367 #define KM_SLEEP 0x00000000
368 #define KM_NOSLEEP 0x00000001
369
370 typedef struct vmem vmem_t;
371 typedef struct kmem_cache kmem_cache_t;
372
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);
379
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);
383
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 *);
390
391 /*
392 * kthread
393 */
394
395 typedef struct _kthread kthread_t; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */
396
397 /*
398 * Loadable Modules
399 */
400
401 decl_simple_lock_data(extern,kmod_lock)
402
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
407
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 */
412 #define modules kmod
413
414 /*
415 * proc
416 */
417
418 #define DATAMODEL_MASK 0x0FF00000
419
420 #define DATAMODEL_ILP32 0x00100000
421 #define DATAMODEL_LP64 0x00200000
422
423 #define DATAMODEL_NONE 0
424
425 #if defined(__LP64__)
426 #define DATAMODEL_NATIVE DATAMODEL_LP64
427 #else
428 #define DATAMODEL_NATIVE DATAMODEL_ILP32
429 #endif /* __LP64__ */
430
431 typedef unsigned int model_t; /* For dtrace_instr_size_isa() prototype in <sys/dtrace.h> */
432
433 /*
434 * taskq
435 */
436
437 #define TQ_SLEEP 0x00 /* Can block for memory */
438
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;
443
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 *);
447
448 extern pri_t maxclsyspri;
449
450 /*
451 * vmem
452 */
453
454 #define VMC_IDENTIFIER 0x00040000 /* not backed by memory */
455 #define VM_SLEEP 0x00000000 /* same as KM_SLEEP */
456 #define VM_BESTFIT 0x00000100
457
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);
463
464 /*
465 * Atomic
466 */
467
468 static inline void atomic_add_32( uint32_t *theValue, int32_t theAmount )
469 {
470 (void)OSAddAtomic( theAmount, (SInt32 *)theValue );
471 }
472
473 /*
474 * Miscellaneous
475 */
476
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 */
483
484 extern volatile int panicwait; /* kern/debug.c */
485 #define panic_quiesce (panicwait)
486
487 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
488
489 extern void delay( int ); /* kern/clock.h */
490
491 extern int vuprintf(const char *, va_list);
492
493 extern boolean_t dtxnu_is_RAM_page(ppnum_t);
494
495 extern hrtime_t dtrace_abs_to_nano(uint64_t);
496
497 __private_extern__ char * strstr(const char *, const char *);
498
499 #undef proc_t
500
501 #endif /* KERNEL_BUILD */
502 #endif /* _DTRACE_GLUE_H */
503