]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/dtrace_glue.h
xnu-1504.3.12.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 /*
47 * cmn_err
48 */
49 #define CE_CONT 0 /* continuation */
50 #define CE_NOTE 1 /* notice */
51 #define CE_WARN 2 /* warning */
52 #define CE_PANIC 3 /* panic */
53 #define CE_IGNORE 4 /* print nothing */
54
55 extern void cmn_err( int, const char *, ... );
56
57 /*
58 * pid/proc
59 */
60
61 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
62 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
63 #define curproc ((struct proc *)current_proc()) /* Called from probe context, must blacklist */
64
65 proc_t* sprlock(pid_t pid);
66 void sprunlock(proc_t *p);
67
68 /*
69 * uread/uwrite
70 */
71
72 int uread(proc_t *p, void *buf, user_size_t len, user_addr_t a);
73 int uwrite(proc_t *p, void *buf, user_size_t len, user_addr_t a);
74
75 /*
76 * fuword / suword
77 */
78
79 int fuword8(user_addr_t, uint8_t *);
80 int fuword16(user_addr_t, uint16_t *);
81 int fuword32(user_addr_t, uint32_t *);
82 int fuword64(user_addr_t, uint64_t *);
83
84 void fuword8_noerr(user_addr_t, uint8_t *);
85 void fuword16_noerr(user_addr_t, uint16_t *);
86 void fuword32_noerr(user_addr_t, uint32_t *);
87 void fuword64_noerr(user_addr_t, uint64_t *);
88
89 int suword64(user_addr_t, uint64_t value);
90 int suword32(user_addr_t, uint32_t value);
91 int suword16(user_addr_t, uint16_t value);
92 int suword8(user_addr_t, uint8_t value);
93
94 /*
95 * cpuvar
96 */
97 extern lck_mtx_t cpu_lock;
98 extern lck_mtx_t mod_lock;
99
100 /*
101 * Per-CPU data.
102 */
103 typedef struct cpu {
104 processorid_t cpu_id; /* CPU number */
105 struct cpu *cpu_next; /* next existing CPU */
106 lck_rw_t cpu_ft_lock; /* DTrace: fasttrap lock */
107 uintptr_t cpu_dtrace_caller; /* DTrace: caller, if any */
108 hrtime_t cpu_dtrace_chillmark; /* DTrace: chill mark time */
109 hrtime_t cpu_dtrace_chilled; /* DTrace: total chill time */
110 boolean_t cpu_dtrace_invop_underway; /* DTrace gaurds against invalid op re-entrancy */
111 } cpu_t;
112
113 extern cpu_t *cpu_list;
114
115 /*
116 * The cpu_core structure consists of per-CPU state available in any context.
117 * On some architectures, this may mean that the page(s) containing the
118 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
119 * is up to the platform to assure that this is performed properly. Note that
120 * the structure is sized to avoid false sharing.
121 */
122 #define CPU_CACHE_COHERENCE_SIZE 64
123
124 typedef struct cpu_core {
125 uint64_t cpuc_dtrace_illval; /* DTrace illegal value */
126 lck_mtx_t cpuc_pid_lock; /* DTrace pid provider lock */
127 uint16_t cpuc_dtrace_flags; /* DTrace flags */
128 uint64_t cpuc_missing_tos; /* Addr. of top most stack frame if missing */
129 uint8_t cpuc_pad[CPU_CACHE_COHERENCE_SIZE - sizeof(uint64_t) - sizeof(lck_mtx_t) - sizeof(uint16_t) - sizeof(uint64_t) ]; /* padding */
130 } cpu_core_t;
131
132 extern cpu_core_t *cpu_core;
133 extern unsigned int real_ncpus;
134 extern int cpu_number(void); /* From #include <kern/cpu_number.h>. Called from probe context, must blacklist. */
135
136 #define CPU (&(cpu_list[cpu_number()])) /* Pointer to current CPU */
137 #define CPU_ON_INTR(cpup) ml_at_interrupt_context() /* always invoked on current cpu */
138 #define NCPU real_ncpus
139
140 /*
141 * Routines used to register interest in cpu's being added to or removed
142 * from the system.
143 */
144 typedef enum {
145 CPU_INIT,
146 CPU_CONFIG,
147 CPU_UNCONFIG,
148 CPU_ON,
149 CPU_OFF,
150 CPU_CPUPART_IN,
151 CPU_CPUPART_OUT
152 } cpu_setup_t;
153
154 typedef int cpu_setup_func_t(cpu_setup_t, int, void *);
155
156 extern void register_cpu_setup_func(cpu_setup_func_t *, void *);
157 extern void unregister_cpu_setup_func(cpu_setup_func_t *, void *);
158
159 /*
160 * CPU_DTRACE
161 */
162
163 /*
164 * DTrace flags.
165 */
166 #define CPU_DTRACE_NOFAULT 0x0001 /* Don't fault */
167 #define CPU_DTRACE_DROP 0x0002 /* Drop this ECB */
168 #define CPU_DTRACE_BADADDR 0x0004 /* DTrace fault: bad address */
169 #define CPU_DTRACE_BADALIGN 0x0008 /* DTrace fault: bad alignment */
170 #define CPU_DTRACE_DIVZERO 0x0010 /* DTrace fault: divide by zero */
171 #define CPU_DTRACE_ILLOP 0x0020 /* DTrace fault: illegal operation */
172 #define CPU_DTRACE_NOSCRATCH 0x0040 /* DTrace fault: out of scratch */
173 #define CPU_DTRACE_KPRIV 0x0080 /* DTrace fault: bad kernel access */
174 #define CPU_DTRACE_UPRIV 0x0100 /* DTrace fault: bad user access */
175 #define CPU_DTRACE_TUPOFLOW 0x0200 /* DTrace fault: tuple stack overflow */
176 #if defined(__sparc)
177 //#define CPU_DTRACE_FAKERESTORE 0x0400 /* pid provider hint to getreg */
178 #endif
179 #define CPU_DTRACE_USTACK_FP 0x0400 /* pid provider hint to ustack() */
180 #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */
181 #define CPU_DTRACE_BADSTACK 0x1000 /* DTrace fault: bad stack */
182
183 #define CPU_DTRACE_FAULT (CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \
184 CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \
185 CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \
186 CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW | \
187 CPU_DTRACE_BADSTACK)
188 #define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP)
189
190 /*
191 * cred_t
192 */
193 /* Privileges */
194 #define PRIV_DTRACE_KERNEL 3
195 #define PRIV_DTRACE_PROC 4
196 #define PRIV_DTRACE_USER 5
197 #define PRIV_PROC_OWNER 30
198 #define PRIV_PROC_ZONE 35
199 #define PRIV_ALL (-1) /* All privileges required */
200
201 /* Privilege sets */
202 #define PRIV_EFFECTIVE 0
203
204 typedef struct ucred cred_t;
205 #define cr_suid cr_svuid
206 #define cr_sgid cr_svgid
207
208 extern cred_t *dtrace_CRED(void); /* Safe to call from probe context. */
209 #define CRED() kauth_cred_get() /* Can't be called from probe context! */
210 extern int PRIV_POLICY_CHOICE(void *, int, int);
211 extern int PRIV_POLICY_ONLY(void *, int, int);
212 extern gid_t crgetgid(const cred_t *);
213 extern uid_t crgetuid(const cred_t *);
214 #define crgetzoneid(x) ((zoneid_t)0)
215
216 #define crhold(a) {}
217 #define crfree(a) {}
218
219 /*
220 * "cyclic"
221 */
222 #define CY_LOW_LEVEL 0
223 #define CY_LOCK_LEVEL 1
224 #define CY_HIGH_LEVEL 2
225 #define CY_SOFT_LEVELS 2
226 #define CY_LEVELS 3
227
228 typedef uintptr_t cyclic_id_t;
229 typedef cyclic_id_t *cyclic_id_list_t;
230 typedef uint16_t cyc_level_t;
231 typedef void (*cyc_func_t)(void *);
232
233 #define CYCLIC_NONE ((cyclic_id_t)0)
234
235 typedef struct cyc_time {
236 hrtime_t cyt_when;
237 hrtime_t cyt_interval;
238 } cyc_time_t;
239
240 typedef struct cyc_handler {
241 cyc_func_t cyh_func;
242 void *cyh_arg;
243 cyc_level_t cyh_level;
244 } cyc_handler_t;
245
246 typedef struct cyc_omni_handler {
247 void (*cyo_online)(void *, cpu_t *, cyc_handler_t *, cyc_time_t *);
248 void (*cyo_offline)(void *, cpu_t *, void *);
249 void *cyo_arg;
250 } cyc_omni_handler_t;
251
252 extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *);
253 extern void cyclic_remove(cyclic_id_t);
254
255 extern cyclic_id_list_t cyclic_add_omni(cyc_omni_handler_t *);
256 extern void cyclic_remove_omni(cyclic_id_list_t);
257
258 extern cyclic_id_t cyclic_timer_add(cyc_handler_t *, cyc_time_t *);
259 extern void cyclic_timer_remove(cyclic_id_t);
260
261 /*
262 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision)
263 */
264
265 thread_call_t dtrace_timeout(void (*func)(void *, void *), void* arg, uint64_t nanos);
266
267 /*
268 * ddi
269 */
270
271 #define DDI_SUCCESS 0
272 #define DDI_FAILURE -1
273
274 #define DDI_DEV_T_NONE ((dev_t)-1)
275 #define DDI_DEV_T_ANY ((dev_t)-2)
276 #define DDI_MAJOR_T_UNKNOWN ((major_t)0)
277
278 #define DDI_PSEUDO "ddi_pseudo"
279
280 typedef enum {
281 DDI_ATTACH = 0,
282 DDI_RESUME = 1,
283 DDI_PM_RESUME = 2
284 } ddi_attach_cmd_t;
285
286 typedef enum {
287 DDI_DETACH = 0,
288 DDI_SUSPEND = 1,
289 DDI_PM_SUSPEND = 2,
290 DDI_HOTPLUG_DETACH = 3 /* detach, don't try to auto-unconfig */
291 } ddi_detach_cmd_t;
292
293 #define DDI_PROP_SUCCESS 0
294
295 #define DDI_PROP_DONTPASS 1
296 typedef uint_t major_t;
297 typedef uint_t minor_t;
298
299 typedef struct __dev_info *dev_info_t;
300
301 extern void ddi_report_dev(dev_info_t *);
302 extern int ddi_soft_state_init(void **, size_t, size_t);
303 extern void *ddi_get_soft_state(void *, int);
304 extern int ddi_soft_state_free(void *, int);
305 extern int ddi_soft_state_zalloc(void *, int);
306 extern void ddi_soft_state_fini(void **);
307
308 int ddi_getprop(dev_t dev, dev_info_t *dip, int flags, const char *name, int defvalue);
309
310 extern int ddi_prop_free(void *);
311 extern int ddi_prop_lookup_int_array(dev_t, dev_info_t *, uint_t, const char *, int **, uint_t *);
312
313 extern int ddi_driver_major(dev_info_t *);
314
315 extern int ddi_create_minor_node(dev_info_t *, const char *, int, minor_t, const char *, int);
316 extern void ddi_remove_minor_node(dev_info_t *, char *);
317
318 extern major_t getemajor(dev_t);
319 extern minor_t getminor(dev_t);
320
321 extern dev_t makedevice(major_t, minor_t);
322
323 /*
324 * Kernel Debug Interface
325 */
326
327 typedef enum kdi_dtrace_set {
328 KDI_DTSET_DTRACE_ACTIVATE,
329 KDI_DTSET_DTRACE_DEACTIVATE,
330 KDI_DTSET_KMDB_BPT_ACTIVATE,
331 KDI_DTSET_KMDB_BPT_DEACTIVATE
332 } kdi_dtrace_set_t;
333
334 extern int kdi_dtrace_set(kdi_dtrace_set_t);
335 extern void debug_enter(char *);
336
337 /*
338 * DTrace specific zone allocation
339 */
340
341 /*
342 * To break dtrace memory usage out in a trackable
343 * fashion, uncomment the #define below. This will
344 * enable emulation of the general kalloc.XXX zones
345 * for most dtrace allocations. (kalloc.large is not
346 * emulated)
347 *
348 * #define DTRACE_MEMORY_ZONES 1
349 *
350 */
351
352 #if defined(DTRACE_MEMORY_ZONES)
353 void dtrace_alloc_init(void);
354 void *dtrace_alloc(vm_size_t);
355 void dtrace_free(void *, vm_size_t);
356 #endif
357
358 /*
359 * kmem
360 */
361
362 #define KM_SLEEP 0x00000000
363 #define KM_NOSLEEP 0x00000001
364
365 typedef struct vmem vmem_t;
366 typedef struct kmem_cache kmem_cache_t;
367
368 #define kmem_alloc dt_kmem_alloc /* Avoid clash with Darwin's kmem_alloc */
369 #define kmem_free dt_kmem_free /* Avoid clash with Darwin's kmem_free */
370 #define kmem_zalloc dt_kmem_zalloc /* Avoid clash with Darwin's kmem_zalloc */
371 extern void *dt_kmem_alloc(size_t, int);
372 extern void dt_kmem_free(void *, size_t);
373 extern void *dt_kmem_zalloc(size_t, int);
374
375 extern void *dt_kmem_alloc_aligned(size_t, size_t, int);
376 extern void *dt_kmem_zalloc_aligned(size_t, size_t, int);
377 extern void dt_kmem_free_aligned(void*, size_t);
378
379 extern kmem_cache_t *
380 kmem_cache_create(const char *, size_t, size_t, int (*)(void *, void *, int),
381 void (*)(void *, void *), void (*)(void *), void *, vmem_t *, int);
382 extern void *kmem_cache_alloc(kmem_cache_t *, int);
383 extern void kmem_cache_free(kmem_cache_t *, void *);
384 extern void kmem_cache_destroy(kmem_cache_t *);
385
386 /*
387 * kthread
388 */
389
390 typedef struct _kthread kthread_t; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */
391
392 /*
393 * Loadable Modules
394 */
395
396 #if 0 /* kmod_lock has been removed */
397 decl_simple_lock_data(extern,kmod_lock)
398 #endif /* 0 */
399
400 /* Want to use Darwin's kmod_info in place of the Solaris modctl.
401 Can't typedef since the (many) usages in the code are "struct modctl *" */
402 extern kmod_info_t *kmod;
403 #define modctl kmod_info
404
405 #define mod_modname name
406 #define mod_loadcnt id
407 #define mod_next next
408 #define mod_loaded info_version /* XXX Is always > 0, hence TRUE */
409 #define modules kmod
410
411 /*
412 * proc
413 */
414
415 #define DATAMODEL_MASK 0x0FF00000
416
417 #define DATAMODEL_ILP32 0x00100000
418 #define DATAMODEL_LP64 0x00200000
419
420 #define DATAMODEL_NONE 0
421
422 #if defined(__LP64__)
423 #define DATAMODEL_NATIVE DATAMODEL_LP64
424 #else
425 #define DATAMODEL_NATIVE DATAMODEL_ILP32
426 #endif /* __LP64__ */
427
428 typedef unsigned int model_t; /* For dtrace_instr_size_isa() prototype in <sys/dtrace.h> */
429
430 /*
431 * taskq
432 */
433
434 #define TQ_SLEEP 0x00 /* Can block for memory */
435
436 typedef uint_t pri_t;
437 typedef struct taskq taskq_t;
438 typedef void (task_func_t)(void *);
439 typedef uintptr_t taskqid_t;
440
441 extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t);
442 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
443 extern void taskq_destroy(taskq_t *);
444
445 extern pri_t maxclsyspri;
446
447 /*
448 * vmem
449 */
450
451 #define VMC_IDENTIFIER 0x00040000 /* not backed by memory */
452 #define VM_SLEEP 0x00000000 /* same as KM_SLEEP */
453 #define VM_BESTFIT 0x00000100
454
455 extern void *vmem_alloc(vmem_t *, size_t, int);
456 extern vmem_t *vmem_create(const char *, void *, size_t, size_t, void *,
457 void *, vmem_t *, size_t, int);
458 extern void vmem_destroy(vmem_t *);
459 extern void vmem_free(vmem_t *vmp, void *vaddr, size_t size);
460
461 /*
462 * Atomic
463 */
464
465 static inline void atomic_add_32( uint32_t *theValue, int32_t theAmount )
466 {
467 (void)OSAddAtomic( theAmount, theValue );
468 }
469
470 #if defined(__i386__) || defined(__x86_64__)
471 static inline void atomic_add_64( uint64_t *theValue, int64_t theAmount )
472 {
473 (void)OSAddAtomic64( theAmount, (SInt64 *)theValue );
474 }
475 #elif defined(__ppc__)
476 static inline void atomic_add_64( uint64_t *theValue, int64_t theAmount )
477 {
478 // FIXME
479 // atomic_add_64() is at present only called from fasttrap.c to increment
480 // or decrement a 64bit counter. Narrow to 32bits since ppc32 (G4) has
481 // no convenient 64bit atomic op.
482 (void)OSAddAtomic( (int32_t)theAmount, &(((SInt32 *)theValue)[1]));
483 }
484 #endif
485
486 /*
487 * Miscellaneous
488 */
489
490 typedef uintptr_t pc_t;
491 typedef uintptr_t greg_t; /* For dtrace_impl.h prototype of dtrace_getfp() */
492 extern struct regs *find_user_regs( thread_t thread);
493 extern vm_offset_t dtrace_get_cpu_int_stack_top(void);
494 extern vm_offset_t max_valid_stack_address(void); /* kern/thread.h */
495
496 extern volatile int panicwait; /* kern/debug.c */
497 #define panic_quiesce (panicwait)
498
499 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
500
501 extern void delay( int ); /* kern/clock.h */
502
503 extern int vuprintf(const char *, va_list);
504
505 extern hrtime_t dtrace_abs_to_nano(uint64_t);
506
507 __private_extern__ const char * strstr(const char *, const char *);
508
509 #undef proc_t
510
511 /*
512 * Safe counted string compare against a literal string. The sizeof() intentionally
513 * counts the trailing NUL, and so ensures that all the characters in the literal
514 * can participate in the comparison.
515 */
516 #define LIT_STRNEQL(s1, lit_s2) (0 == strncmp( (s1), (lit_s2), sizeof((lit_s2)) ))
517
518 /*
519 * Safe counted string compare of a literal against the beginning of a string. Here
520 * the sizeof() is reduced by 1 so that the trailing null of the literal does not
521 * participate in the comparison.
522 */
523 #define LIT_STRNSTART(s1, lit_s2) (0 == strncmp( (s1), (lit_s2), sizeof((lit_s2)) - 1 ))
524
525 #define KERNELBASE VM_MIN_KERNEL_ADDRESS
526 #endif /* KERNEL_BUILD */
527 #endif /* _DTRACE_GLUE_H */
528