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/cpu_number.h> 36 #include <kern/locks.h> 37 #include <kern/debug.h> 38 #include <kern/thread_call.h> 39 #include <kern/thread.h> 40 #include <machine/atomic.h> 41 #include <machine/machine_routines.h> 42 #include <sys/syslog.h> 43 #include <sys/ucred.h> 44 #include <stdarg.h> 45 #include <mach/kmod.h> 46 47 #if defined(__i386__) || defined(__x86_64__) 48 #include <i386/mp.h> 49 #endif 50 51 /* 52 * cmn_err 53 */ 54 #define CE_NOTE 1 /* notice */ 55 #define CE_WARN 2 /* warning */ 56 57 extern void cmn_err( int, const char *, ... ); 58 59 /* 60 * pid/proc 61 */ 62 63 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */ 64 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */ 65 #define curproc ((struct proc *)current_proc()) /* Called from probe context, must blacklist */ 66 67 proc_t* sprlock(pid_t pid); 68 void sprunlock(proc_t *p); 69 70 void dtrace_sprlock(proc_t *p); 71 void dtrace_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 fuword32_noerr(user_addr_t, uint32_t *); 90 void fuword64_noerr(user_addr_t, uint64_t *); 91 92 int suword64(user_addr_t, uint64_t value); 93 int suword32(user_addr_t, uint32_t value); 94 95 /* 96 * cpuvar 97 */ 98 extern lck_mtx_t cpu_lock; 99 extern lck_mtx_t cyc_lock; 100 extern lck_mtx_t mod_lock; 101 102 /* 103 * wrap_timer_call: wrapper of timer_call for cyclic timers. 104 */ 105 struct wrap_timer_call; 106 107 /* 108 * Per-CPU data. 109 */ 110 typedef struct dtrace_cpu { 111 processorid_t cpu_id; /* CPU number */ 112 struct dtrace_cpu *cpu_next; /* next existing CPU */ 113 lck_rw_t cpu_ft_lock; /* DTrace: fasttrap lock */ 114 uintptr_t cpu_dtrace_caller; /* DTrace: caller, if any */ 115 hrtime_t cpu_dtrace_chillmark; /* DTrace: chill mark time */ 116 hrtime_t cpu_dtrace_chilled; /* DTrace: total chill time */ 117 boolean_t cpu_dtrace_invop_underway; /* DTrace gaurds against invalid op re-entrancy */ 118 119 /* Local cyclic timers on this CPU */ 120 LIST_HEAD(cyc_list_head, wrap_timer_call) cpu_cyc_list; 121 } dtrace_cpu_t; 122 123 extern dtrace_cpu_t *cpu_list; 124 125 /* 126 * The cpu_core structure consists of per-CPU state available in any context. 127 * On some architectures, this may mean that the page(s) containing the 128 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it 129 * is up to the platform to assure that this is performed properly. Note that 130 * the structure is sized to avoid false sharing. 131 */ 132 #define CPU_CACHE_COHERENCE_SIZE 64 133 134 typedef struct cpu_core { 135 uint64_t cpuc_dtrace_illval; /* DTrace illegal value */ 136 lck_mtx_t cpuc_pid_lock; /* DTrace pid provider lock */ 137 uint16_t cpuc_dtrace_flags; /* DTrace flags */ 138 uint64_t cpuc_missing_tos; /* Addr. of top most stack frame if missing */ 139 uint8_t cpuc_pad[CPU_CACHE_COHERENCE_SIZE - sizeof(uint64_t) - sizeof(lck_mtx_t) - sizeof(uint16_t) - sizeof(uint64_t)]; /* padding */ 140 } cpu_core_t; 141 142 extern cpu_core_t *cpu_core; 143 144 extern unsigned int dtrace_max_cpus; /* max number of enabled cpus */ 145 #define NCPU dtrace_max_cpus 146 147 #define CPU (&(cpu_list[cpu_number()])) /* Pointer to current CPU */ 148 #define CPU_ON_INTR(cpup) ml_at_interrupt_context() /* always invoked on current cpu */ 149 150 /* 151 * Routines used to register interest in cpu's being added to or removed 152 * from the system. 153 */ 154 typedef enum { 155 CPU_INIT, 156 CPU_CONFIG, 157 CPU_UNCONFIG, 158 CPU_ON, 159 CPU_OFF, 160 CPU_CPUPART_IN, 161 CPU_CPUPART_OUT 162 } cpu_setup_t; 163 164 typedef int cpu_setup_func_t(cpu_setup_t, processorid_t); 165 166 extern void register_cpu_setup_func(cpu_setup_func_t *, void *); 167 extern void unregister_cpu_setup_func(cpu_setup_func_t *, void *); 168 169 /* 170 * CPU_DTRACE 171 */ 172 173 /* 174 * DTrace flags. 175 */ 176 #define CPU_DTRACE_NOFAULT 0x0001 /* Don't fault */ 177 #define CPU_DTRACE_DROP 0x0002 /* Drop this ECB */ 178 #define CPU_DTRACE_BADADDR 0x0004 /* DTrace fault: bad address */ 179 #define CPU_DTRACE_BADALIGN 0x0008 /* DTrace fault: bad alignment */ 180 #define CPU_DTRACE_DIVZERO 0x0010 /* DTrace fault: divide by zero */ 181 #define CPU_DTRACE_ILLOP 0x0020 /* DTrace fault: illegal operation */ 182 #define CPU_DTRACE_NOSCRATCH 0x0040 /* DTrace fault: out of scratch */ 183 #define CPU_DTRACE_KPRIV 0x0080 /* DTrace fault: bad kernel access */ 184 #define CPU_DTRACE_UPRIV 0x0100 /* DTrace fault: bad user access */ 185 #define CPU_DTRACE_TUPOFLOW 0x0200 /* DTrace fault: tuple stack overflow */ 186 #define CPU_DTRACE_USTACK_FP 0x0400 /* pid provider hint to ustack() */ 187 #define CPU_DTRACE_ENTRY 0x0800 /* pid provider hint to ustack() */ 188 #define CPU_DTRACE_BADSTACK 0x1000 /* DTrace fault: bad stack */ 189 190 #define CPU_DTRACE_FAULT (CPU_DTRACE_BADADDR | CPU_DTRACE_BADALIGN | \ 191 CPU_DTRACE_DIVZERO | CPU_DTRACE_ILLOP | \ 192 CPU_DTRACE_NOSCRATCH | CPU_DTRACE_KPRIV | \ 193 CPU_DTRACE_UPRIV | CPU_DTRACE_TUPOFLOW | \ 194 CPU_DTRACE_BADSTACK) 195 #define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP) 196 197 /* 198 * Loadable Modules 199 */ 200 201 /* Keep the compiler happy */ 202 struct dtrace_module_symbols; 203 204 /* Solaris' modctl structure, greatly simplified, shadowing parts of xnu kmod structure. */ 205 typedef struct modctl { 206 struct modctl *mod_next; 207 struct modctl *mod_stale; // stale module chain 208 uint32_t mod_id; // the kext unique identifier 209 char mod_modname[KMOD_MAX_NAME]; 210 int mod_loadcnt; 211 char mod_loaded; 212 uint16_t mod_flags; // See flags below 213 int mod_nenabled; // # of enabled DTrace probes in module 214 vm_address_t mod_address; // starting address (of Mach-o header blob) 215 vm_size_t mod_size; // total size (of blob) 216 UUID mod_uuid; 217 struct dtrace_module_symbols* mod_user_symbols; 218 /* 219 * SDT probe data are directly stored in modctl. That's fine for XNU as modctl serves 220 * different purpose than on Solaris and is allocated/freed as required. 221 */ 222 int mod_sdtprobecnt; // Amount of provided SDT probes 223 void *mod_sdtdesc; // Pointer to sdt_probedesc_t 224 } modctl_t; 225 226 /* Definitions for mod_flags */ 227 #define MODCTL_IS_MACH_KERNEL 0x01 // This module represents /mach_kernel 228 #define MODCTL_HAS_KERNEL_SYMBOLS 0x02 // Kernel symbols (nlist) are available 229 #define MODCTL_FBT_PROBES_PROVIDED 0x04 // fbt probes have been provided 230 #define MODCTL_FBT_INVALID 0x08 // Module is invalid for fbt probes 231 #define MODCTL_SDT_PROBES_PROVIDED 0x10 // sdt probes have been provided 232 #define MODCTL_SDT_INVALID 0x20 // Module is invalid for sdt probes 233 #define MODCTL_HAS_UUID 0x40 // Module has UUID 234 #define MODCTL_FBT_PROVIDE_BLACKLISTED_PROBES 0x200 // fbt provider must provide blacklisted probes 235 #define MODCTL_FBT_BLACKLISTED_PROBES_PROVIDED 0x400 // fbt blacklisted probes have been provided 236 #define MODCTL_IS_STATIC_KEXT 0x800 // module is a static kext 237 238 /* Simple/singular mod_flags accessors */ 239 #define MOD_IS_MACH_KERNEL(mod) (mod->mod_flags & MODCTL_IS_MACH_KERNEL) 240 #define MOD_HAS_KERNEL_SYMBOLS(mod) (mod->mod_flags & MODCTL_HAS_KERNEL_SYMBOLS) 241 #define MOD_HAS_USERSPACE_SYMBOLS(mod) (mod->mod_user_symbols) /* No point in duplicating state in the flags bits */ 242 #define MOD_FBT_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_FBT_PROBES_PROVIDED) 243 #define MOD_FBT_INVALID(mod) (mod->mod_flags & MODCTL_FBT_INVALID) 244 #define MOD_SDT_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_SDT_PROBES_PROVIDED) 245 #define MOD_SDT_INVALID(mod) (mod->mod_flags & MODCTL_SDT_INVALID) 246 #define MOD_HAS_UUID(mod) (mod->mod_flags & MODCTL_HAS_UUID) 247 #define MOD_FBT_BLACKLISTED_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_FBT_BLACKLISTED_PROBES_PROVIDED) 248 #define MOD_FBT_PROVIDE_BLACKLISTED_PROBES(mod) (mod->mod_flags & MODCTL_FBT_PROVIDE_BLACKLISTED_PROBES) 249 #define MOD_IS_STATIC_KEXT(mod) (mod->mod_flags & MODCTL_IS_STATIC_KEXT) 250 251 /* Compound accessors */ 252 #define MOD_FBT_BLACKLISTED_PROBES_DONE(mod) (MOD_FBT_BLACKLISTED_PROBES_PROVIDED(mod) || !MOD_FBT_PROVIDE_BLACKLISTED_PROBES(mod)) 253 #define MOD_FBT_DONE(mod) ((MOD_FBT_PROBES_PROVIDED(mod) && MOD_FBT_BLACKLISTED_PROBES_DONE(mod)) || MOD_FBT_INVALID(mod)) 254 #define MOD_SDT_DONE(mod) (MOD_SDT_PROBES_PROVIDED(mod) || MOD_SDT_INVALID(mod)) 255 #define MOD_SYMBOLS_DONE(mod) (MOD_FBT_DONE(mod) && MOD_SDT_DONE(mod)) 256 257 extern modctl_t *dtrace_modctl_list; 258 259 extern int dtrace_addr_in_module(const void*, const struct modctl*); 260 261 /* 262 * cred_t 263 */ 264 /* Privileges */ 265 #define PRIV_DTRACE_KERNEL 3 266 #define PRIV_DTRACE_PROC 4 267 #define PRIV_DTRACE_USER 5 268 #define PRIV_PROC_OWNER 30 269 #define PRIV_PROC_ZONE 35 270 #define PRIV_ALL (-1) /* All privileges required */ 271 272 /* Privilege sets */ 273 #define PRIV_EFFECTIVE 0 274 275 typedef struct ucred cred_t; 276 #define cr_suid cr_svuid 277 #define cr_sgid cr_svgid 278 279 extern cred_t *dtrace_CRED(void); /* Safe to call from probe context. */ 280 #define CRED() kauth_cred_get() /* Can't be called from probe context! */ 281 extern int PRIV_POLICY_CHOICE(void *, int, int); 282 extern int PRIV_POLICY_ONLY(void *, int, int); 283 extern uid_t crgetuid(const cred_t *); 284 #define crgetzoneid(x) ((zoneid_t)0) 285 286 /* 287 * "cyclic" 288 */ 289 #define CY_LOW_LEVEL 0 290 #define CY_HIGH_LEVEL 2 291 #define CY_LEVELS 3 292 293 typedef uintptr_t cyclic_id_t; 294 typedef cyclic_id_t *cyclic_id_list_t; 295 typedef uint16_t cyc_level_t; 296 typedef void (*cyc_func_t)(void *); 297 298 #define CYCLIC_NONE ((cyclic_id_t)0) 299 300 typedef struct cyc_time { 301 hrtime_t cyt_when; 302 hrtime_t cyt_interval; 303 } cyc_time_t; 304 305 typedef struct cyc_handler { 306 cyc_func_t cyh_func; 307 void *cyh_arg; 308 cyc_level_t cyh_level; 309 } cyc_handler_t; 310 311 typedef struct cyc_omni_handler { 312 void (*cyo_online)(void *, dtrace_cpu_t *, cyc_handler_t *, cyc_time_t *); 313 void (*cyo_offline)(void *, dtrace_cpu_t *, void *); 314 void *cyo_arg; 315 } cyc_omni_handler_t; 316 317 extern void dtrace_install_cpu_hooks(void); 318 319 extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *); 320 extern void cyclic_remove(cyclic_id_t); 321 322 extern cyclic_id_list_t cyclic_add_omni(cyc_omni_handler_t *); 323 extern void cyclic_remove_omni(cyclic_id_list_t); 324 325 extern cyclic_id_t cyclic_timer_add(cyc_handler_t *, cyc_time_t *); 326 extern void cyclic_timer_remove(cyclic_id_t); 327 328 /* 329 * ddi 330 */ 331 332 #define DDI_SUCCESS 0 333 #define DDI_FAILURE -1 334 335 #define DDI_PSEUDO "ddi_pseudo" 336 337 typedef enum { 338 DDI_DETACH = 0, 339 DDI_SUSPEND = 1, 340 DDI_PM_SUSPEND = 2, 341 DDI_HOTPLUG_DETACH = 3 /* detach, don't try to auto-unconfig */ 342 } ddi_detach_cmd_t; 343 344 #define DDI_PROP_SUCCESS 0 345 346 #define DDI_PROP_DONTPASS 1 347 typedef uint_t major_t; 348 typedef uint_t minor_t; 349 350 typedef struct __dev_info *dev_info_t; 351 352 extern int ddi_driver_major(dev_info_t *); 353 354 extern int ddi_create_minor_node(dev_info_t *, const char *, int, minor_t, const char *, int); 355 extern void ddi_remove_minor_node(dev_info_t *, char *); 356 357 extern major_t getemajor(dev_t); 358 extern minor_t getminor(dev_t); 359 360 /* 361 * Kernel Debug Interface 362 */ 363 extern void debug_enter(char *); 364 365 /* 366 * DTrace specific zone allocation 367 */ 368 369 /* 370 * kmem 371 */ 372 373 #define KM_SLEEP 0x00000000 374 #define KM_NOSLEEP 0x00000001 375 376 typedef struct vmem vmem_t; 377 typedef struct kmem_cache kmem_cache_t; 378 379 #define kmem_free dt_kmem_free /* Avoid clash with Darwin's kmem_free */ 380 #define kmem_free_aligned dt_kmem_free_aligned 381 382 #define kmem_alloc(size, kmflag) \ 383 dt_kmem_alloc_tag(size, kmflag, VM_ALLOC_SITE_TAG()) 384 385 extern void *dt_kmem_alloc_tag(size_t, int, vm_tag_t); 386 extern void dt_kmem_free(void *, size_t); 387 388 #define kmem_zalloc(size, kmflag) \ 389 dt_kmem_zalloc_tag(size, kmflag, VM_ALLOC_SITE_TAG()) 390 391 extern void *dt_kmem_zalloc_tag(size_t, int, vm_tag_t); 392 393 #define kmem_alloc_aligned(size, align, kmflag) \ 394 dt_kmem_alloc_aligned_tag(size, align, kmflag, VM_ALLOC_SITE_TAG()) 395 extern void *dt_kmem_alloc_aligned_tag(size_t, size_t, int, vm_tag_t); 396 397 #define kmem_zalloc_aligned(size, align, kmflag) \ 398 dt_kmem_zalloc_aligned_tag(size, align, kmflag, VM_ALLOC_SITE_TAG()) 399 extern void *dt_kmem_zalloc_aligned_tag(size_t, size_t, int, vm_tag_t); 400 401 extern void dt_kmem_free_aligned(void*, size_t); 402 403 /* 404 * kthread 405 */ 406 407 typedef struct _kthread kthread_t; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */ 408 409 /* 410 * proc 411 */ 412 413 414 #define DATAMODEL_ILP32 0x00100000 415 #define DATAMODEL_LP64 0x00200000 416 417 #define DATAMODEL_NONE 0 418 419 #if defined(__LP64__) 420 #define DATAMODEL_NATIVE DATAMODEL_LP64 421 #else 422 #define DATAMODEL_NATIVE DATAMODEL_ILP32 423 #endif /* __LP64__ */ 424 425 typedef unsigned int model_t; /* For dtrace_instr_size_isa() prototype in <sys/dtrace.h> */ 426 427 /* 428 * vmem 429 */ 430 431 #define VMC_IDENTIFIER 0x00040000 /* not backed by memory */ 432 #define VM_SLEEP 0x00000000 /* same as KM_SLEEP */ 433 #define VM_BESTFIT 0x00000100 434 435 extern void *vmem_alloc(vmem_t *, size_t, int); 436 extern vmem_t *vmem_create(const char *, void *, size_t, size_t, void *, 437 void *, vmem_t *, size_t, int); 438 extern void vmem_destroy(vmem_t *); 439 extern void vmem_free(vmem_t *vmp, void *vaddr, size_t size); 440 441 /* 442 * Miscellaneous 443 */ 444 445 typedef uintptr_t pc_t; 446 typedef uintptr_t greg_t; /* For dtrace_impl.h prototype of dtrace_getfp() */ 447 #if defined(__arm__) || defined(__arm64__) 448 #define regs arm_saved_state 449 #endif 450 extern struct regs *find_user_regs( thread_t thread); 451 extern vm_offset_t dtrace_get_cpu_int_stack_top(void); 452 453 #define panic_quiesce (panic_active()) 454 455 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) 456 457 extern int vuprintf(const char *, va_list); 458 459 extern hrtime_t dtrace_abs_to_nano(uint64_t); 460 461 const void* bsearch(const void*, const void*, size_t, size_t, int (*compar)(const void *, const void *)); 462 463 int dtrace_copy_maxsize(void); 464 int dtrace_buffer_copyout(const void*, user_addr_t, vm_size_t); 465 466 467 #define DTRACE_NCLIENTS 32 468 469 #undef proc_t 470 471 /* 472 * Safe counted string compare against a literal string. The sizeof() intentionally 473 * counts the trailing NUL, and so ensures that all the characters in the literal 474 * can participate in the comparison. 475 */ 476 #define LIT_STRNEQL(s1, lit_s2) (0 == strncmp( (s1), (lit_s2), sizeof((lit_s2)) )) 477 478 #define KERNELBASE VM_MIN_KERNEL_ADDRESS 479 #endif /* KERNEL_BUILD */ 480 #endif /* _DTRACE_GLUE_H */ 481