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