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#if defined(__i386__) || defined(__x86_64__) 47#include <i386/mp.h> 48#endif 49 50/* 51 * cmn_err 52 */ 53#define CE_CONT 0 /* continuation */ 54#define CE_NOTE 1 /* notice */ 55#define CE_WARN 2 /* warning */ 56#define CE_PANIC 3 /* panic */ 57#define CE_IGNORE 4 /* print nothing */ 58 59extern void cmn_err( int, const char *, ... ); 60 61/* 62 * pid/proc 63 */ 64 65/* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */ 66#define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */ 67#define curproc ((struct proc *)current_proc()) /* Called from probe context, must blacklist */ 68 69proc_t* sprlock(pid_t pid); 70void sprunlock(proc_t *p); 71 72/* 73 * uread/uwrite 74 */ 75 76int uread(proc_t *p, void *buf, user_size_t len, user_addr_t a); 77int uwrite(proc_t *p, void *buf, user_size_t len, user_addr_t a); 78 79/* 80 * fuword / suword 81 */ 82 83int fuword8(user_addr_t, uint8_t *); 84int fuword16(user_addr_t, uint16_t *); 85int fuword32(user_addr_t, uint32_t *); 86int fuword64(user_addr_t, uint64_t *); 87 88void fuword8_noerr(user_addr_t, uint8_t *); 89void fuword16_noerr(user_addr_t, uint16_t *); 90void fuword32_noerr(user_addr_t, uint32_t *); 91void fuword64_noerr(user_addr_t, uint64_t *); 92 93int suword64(user_addr_t, uint64_t value); 94int suword32(user_addr_t, uint32_t value); 95int suword16(user_addr_t, uint16_t value); 96int suword8(user_addr_t, uint8_t value); 97 98/* 99 * cpuvar 100 */ 101extern lck_mtx_t cpu_lock; 102extern lck_mtx_t mod_lock; 103 104/* 105 * Per-CPU data. 106 */ 107typedef struct dtrace_cpu { 108 processorid_t cpu_id; /* CPU number */ 109 struct dtrace_cpu *cpu_next; /* next existing CPU */ 110 lck_rw_t cpu_ft_lock; /* DTrace: fasttrap lock */ 111 uintptr_t cpu_dtrace_caller; /* DTrace: caller, if any */ 112 hrtime_t cpu_dtrace_chillmark; /* DTrace: chill mark time */ 113 hrtime_t cpu_dtrace_chilled; /* DTrace: total chill time */ 114 boolean_t cpu_dtrace_invop_underway; /* DTrace gaurds against invalid op re-entrancy */ 115} dtrace_cpu_t; 116 117extern dtrace_cpu_t *cpu_list; 118 119/* 120 * The cpu_core structure consists of per-CPU state available in any context. 121 * On some architectures, this may mean that the page(s) containing the 122 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it 123 * is up to the platform to assure that this is performed properly. Note that 124 * the structure is sized to avoid false sharing. 125 */ 126#define CPU_CACHE_COHERENCE_SIZE 64 127 128typedef struct cpu_core { 129 uint64_t cpuc_dtrace_illval; /* DTrace illegal value */ 130 lck_mtx_t cpuc_pid_lock; /* DTrace pid provider lock */ 131 uint16_t cpuc_dtrace_flags; /* DTrace flags */ 132 uint64_t cpuc_missing_tos; /* Addr. of top most stack frame if missing */ 133 uint8_t cpuc_pad[CPU_CACHE_COHERENCE_SIZE - sizeof(uint64_t) - sizeof(lck_mtx_t) - sizeof(uint16_t) - sizeof(uint64_t) ]; /* padding */ 134} cpu_core_t; 135 136extern cpu_core_t *cpu_core; 137 138extern unsigned int dtrace_max_cpus; /* max number of enabled cpus */ 139#define NCPU dtrace_max_cpus 140 141extern int cpu_number(void); /* From #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 146/* 147 * Routines used to register interest in cpu's being added to or removed 148 * from the system. 149 */ 150typedef 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 160typedef int cpu_setup_func_t(cpu_setup_t, int, void *); 161 162extern void register_cpu_setup_func(cpu_setup_func_t *, void *); 163extern 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#define CPU_DTRACE_BADSTACK 0x1000 /* DTrace fault: bad stack */ 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 CPU_DTRACE_BADSTACK) 194#define CPU_DTRACE_ERROR (CPU_DTRACE_FAULT | CPU_DTRACE_DROP) 195 196/* 197 * Loadable Modules 198 */ 199 200/* Keep the compiler happy */ 201struct dtrace_module_symbols; 202 203/* Solaris' modctl structure, greatly simplified, shadowing parts of xnu kmod structure. */ 204typedef struct modctl { 205 struct modctl *mod_next; 206 struct modctl *mod_stale; // stale module chain 207 uint32_t mod_id; // the kext unique identifier 208 char mod_modname[KMOD_MAX_NAME]; 209 int mod_loadcnt; 210 char mod_loaded; 211 char mod_flags; // See flags below 212 int mod_nenabled; // # of enabled DTrace probes in module 213 vm_address_t mod_address; // starting address (of Mach-o header blob) 214 vm_size_t mod_size; // total size (of blob) 215 UUID mod_uuid; 216 struct dtrace_module_symbols* mod_user_symbols; 217} modctl_t; 218 219/* Definitions for mod_flags */ 220#define MODCTL_IS_MACH_KERNEL 0x01 // This module represents /mach_kernel 221#define MODCTL_HAS_KERNEL_SYMBOLS 0x02 // Kernel symbols (nlist) are available 222#define MODCTL_FBT_PROBES_PROVIDED 0x04 // fbt probes have been provided 223#define MODCTL_FBT_INVALID 0x08 // Module is invalid for fbt probes 224#define MODCTL_SDT_PROBES_PROVIDED 0x10 // sdt probes have been provided 225#define MODCTL_SDT_INVALID 0x20 // Module is invalid for sdt probes 226#define MODCTL_HAS_UUID 0x40 // Module has UUID 227 228/* Simple/singular mod_flags accessors */ 229#define MOD_IS_MACH_KERNEL(mod) (mod->mod_flags & MODCTL_IS_MACH_KERNEL) 230#define MOD_HAS_KERNEL_SYMBOLS(mod) (mod->mod_flags & MODCTL_HAS_KERNEL_SYMBOLS) 231#define MOD_HAS_USERSPACE_SYMBOLS(mod) (mod->mod_user_symbols) /* No point in duplicating state in the flags bits */ 232#define MOD_FBT_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_FBT_PROBES_PROVIDED) 233#define MOD_FBT_INVALID(mod) (mod->mod_flags & MODCTL_FBT_INVALID) 234#define MOD_SDT_PROBES_PROVIDED(mod) (mod->mod_flags & MODCTL_SDT_PROBES_PROVIDED) 235#define MOD_SDT_INVALID(mod) (mod->mod_flags & MODCTL_SDT_INVALID) 236#define MOD_HAS_UUID(mod) (mod->mod_flags & MODCTL_HAS_UUID) 237 238/* Compound accessors */ 239#define MOD_FBT_DONE(mod) (MOD_FBT_PROBES_PROVIDED(mod) || MOD_FBT_INVALID(mod)) 240#define MOD_SDT_DONE(mod) (MOD_SDT_PROBES_PROVIDED(mod) || MOD_SDT_INVALID(mod)) 241#define MOD_SYMBOLS_DONE(mod) (MOD_FBT_DONE(mod) && MOD_SDT_DONE(mod)) 242 243extern modctl_t *dtrace_modctl_list; 244 245/* 246 * cred_t 247 */ 248/* Privileges */ 249#define PRIV_DTRACE_KERNEL 3 250#define PRIV_DTRACE_PROC 4 251#define PRIV_DTRACE_USER 5 252#define PRIV_PROC_OWNER 30 253#define PRIV_PROC_ZONE 35 254#define PRIV_ALL (-1) /* All privileges required */ 255 256/* Privilege sets */ 257#define PRIV_EFFECTIVE 0 258 259typedef struct ucred cred_t; 260#define cr_suid cr_svuid 261#define cr_sgid cr_svgid 262 263extern cred_t *dtrace_CRED(void); /* Safe to call from probe context. */ 264#define CRED() kauth_cred_get() /* Can't be called from probe context! */ 265extern int PRIV_POLICY_CHOICE(void *, int, int); 266extern int PRIV_POLICY_ONLY(void *, int, int); 267extern gid_t crgetgid(const cred_t *); 268extern uid_t crgetuid(const cred_t *); 269#define crgetzoneid(x) ((zoneid_t)0) 270 271#define crhold(a) {} 272#define crfree(a) {} 273 274/* 275 * "cyclic" 276 */ 277#define CY_LOW_LEVEL 0 278#define CY_LOCK_LEVEL 1 279#define CY_HIGH_LEVEL 2 280#define CY_SOFT_LEVELS 2 281#define CY_LEVELS 3 282 283typedef uintptr_t cyclic_id_t; 284typedef cyclic_id_t *cyclic_id_list_t; 285typedef uint16_t cyc_level_t; 286typedef void (*cyc_func_t)(void *); 287 288#define CYCLIC_NONE ((cyclic_id_t)0) 289 290typedef struct cyc_time { 291 hrtime_t cyt_when; 292 hrtime_t cyt_interval; 293} cyc_time_t; 294 295typedef struct cyc_handler { 296 cyc_func_t cyh_func; 297 void *cyh_arg; 298 cyc_level_t cyh_level; 299} cyc_handler_t; 300 301typedef struct cyc_omni_handler { 302 void (*cyo_online)(void *, dtrace_cpu_t *, cyc_handler_t *, cyc_time_t *); 303 void (*cyo_offline)(void *, dtrace_cpu_t *, void *); 304 void *cyo_arg; 305} cyc_omni_handler_t; 306 307extern cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *); 308extern void cyclic_remove(cyclic_id_t); 309 310extern cyclic_id_list_t cyclic_add_omni(cyc_omni_handler_t *); 311extern void cyclic_remove_omni(cyclic_id_list_t); 312 313extern cyclic_id_t cyclic_timer_add(cyc_handler_t *, cyc_time_t *); 314extern void cyclic_timer_remove(cyclic_id_t); 315 316/* 317 * timeout / untimeout (converted to dtrace_timeout / dtrace_untimeout due to name collision) 318 */ 319 320thread_call_t dtrace_timeout(void (*func)(void *, void *), void* arg, uint64_t nanos); 321 322/* 323 * ddi 324 */ 325 326#define DDI_SUCCESS 0 327#define DDI_FAILURE -1 328 329#define DDI_DEV_T_NONE ((dev_t)-1) 330#define DDI_DEV_T_ANY ((dev_t)-2) 331#define DDI_MAJOR_T_UNKNOWN ((major_t)0) 332 333#define DDI_PSEUDO "ddi_pseudo" 334 335typedef enum { 336 DDI_ATTACH = 0, 337 DDI_RESUME = 1, 338 DDI_PM_RESUME = 2 339} ddi_attach_cmd_t; 340 341typedef enum { 342 DDI_DETACH = 0, 343 DDI_SUSPEND = 1, 344 DDI_PM_SUSPEND = 2, 345 DDI_HOTPLUG_DETACH = 3 /* detach, don't try to auto-unconfig */ 346} ddi_detach_cmd_t; 347 348#define DDI_PROP_SUCCESS 0 349 350#define DDI_PROP_DONTPASS 1 351typedef uint_t major_t; 352typedef uint_t minor_t; 353 354typedef struct __dev_info *dev_info_t; 355 356extern void ddi_report_dev(dev_info_t *); 357extern int ddi_soft_state_init(void **, size_t, size_t); 358extern void *ddi_get_soft_state(void *, int); 359extern int ddi_soft_state_free(void *, int); 360extern int ddi_soft_state_zalloc(void *, int); 361extern void ddi_soft_state_fini(void **); 362 363int ddi_getprop(dev_t dev, dev_info_t *dip, int flags, const char *name, int defvalue); 364 365extern int ddi_prop_free(void *); 366extern int ddi_prop_lookup_int_array(dev_t, dev_info_t *, uint_t, const char *, int **, uint_t *); 367 368extern int ddi_driver_major(dev_info_t *); 369 370extern int ddi_create_minor_node(dev_info_t *, const char *, int, minor_t, const char *, int); 371extern void ddi_remove_minor_node(dev_info_t *, char *); 372 373extern major_t getemajor(dev_t); 374extern minor_t getminor(dev_t); 375 376extern dev_t makedevice(major_t, minor_t); 377 378/* 379 * Kernel Debug Interface 380 */ 381 382typedef enum kdi_dtrace_set { 383 KDI_DTSET_DTRACE_ACTIVATE, 384 KDI_DTSET_DTRACE_DEACTIVATE, 385 KDI_DTSET_KMDB_BPT_ACTIVATE, 386 KDI_DTSET_KMDB_BPT_DEACTIVATE 387} kdi_dtrace_set_t; 388 389extern int kdi_dtrace_set(kdi_dtrace_set_t); 390extern void debug_enter(char *); 391 392/* 393 * DTrace specific zone allocation 394 */ 395 396/* 397 * To break dtrace memory usage out in a trackable 398 * fashion, uncomment the #define below. This will 399 * enable emulation of the general kalloc.XXX zones 400 * for most dtrace allocations. (kalloc.large is not 401 * emulated) 402 * 403 * #define DTRACE_MEMORY_ZONES 1 404 * 405 */ 406 407#if defined(DTRACE_MEMORY_ZONES) 408void dtrace_alloc_init(void); 409void *dtrace_alloc(vm_size_t); 410void dtrace_free(void *, vm_size_t); 411#endif 412 413/* 414 * kmem 415 */ 416 417#define KM_SLEEP 0x00000000 418#define KM_NOSLEEP 0x00000001 419 420typedef struct vmem vmem_t; 421typedef struct kmem_cache kmem_cache_t; 422 423#define kmem_alloc dt_kmem_alloc /* Avoid clash with Darwin's kmem_alloc */ 424#define kmem_free dt_kmem_free /* Avoid clash with Darwin's kmem_free */ 425#define kmem_zalloc dt_kmem_zalloc /* Avoid clash with Darwin's kmem_zalloc */ 426extern void *dt_kmem_alloc(size_t, int); 427extern void dt_kmem_free(void *, size_t); 428extern void *dt_kmem_zalloc(size_t, int); 429 430extern void *dt_kmem_alloc_aligned(size_t, size_t, int); 431extern void *dt_kmem_zalloc_aligned(size_t, size_t, int); 432extern void dt_kmem_free_aligned(void*, size_t); 433 434extern kmem_cache_t * 435kmem_cache_create(const char *, size_t, size_t, int (*)(void *, void *, int), 436 void (*)(void *, void *), void (*)(void *), void *, vmem_t *, int); 437extern void *kmem_cache_alloc(kmem_cache_t *, int); 438extern void kmem_cache_free(kmem_cache_t *, void *); 439extern void kmem_cache_destroy(kmem_cache_t *); 440 441/* 442 * kthread 443 */ 444 445typedef struct _kthread kthread_t; /* For dtrace_vtime_switch(), dtrace_panicked and dtrace_errthread */ 446 447/* 448 * proc 449 */ 450 451#define DATAMODEL_MASK 0x0FF00000 452 453#define DATAMODEL_ILP32 0x00100000 454#define DATAMODEL_LP64 0x00200000 455 456#define DATAMODEL_NONE 0 457 458#if defined(__LP64__) 459#define DATAMODEL_NATIVE DATAMODEL_LP64 460#else 461#define DATAMODEL_NATIVE DATAMODEL_ILP32 462#endif /* __LP64__ */ 463 464typedef unsigned int model_t; /* For dtrace_instr_size_isa() prototype in <sys/dtrace.h> */ 465 466/* 467 * taskq 468 */ 469 470#define TQ_SLEEP 0x00 /* Can block for memory */ 471 472typedef uint_t pri_t; 473typedef struct taskq taskq_t; 474typedef void (task_func_t)(void *); 475typedef uintptr_t taskqid_t; 476 477extern taskq_t *taskq_create(const char *, int, pri_t, int, int, uint_t); 478extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t); 479extern void taskq_destroy(taskq_t *); 480 481extern pri_t maxclsyspri; 482 483/* 484 * vmem 485 */ 486 487#define VMC_IDENTIFIER 0x00040000 /* not backed by memory */ 488#define VM_SLEEP 0x00000000 /* same as KM_SLEEP */ 489#define VM_BESTFIT 0x00000100 490 491extern void *vmem_alloc(vmem_t *, size_t, int); 492extern vmem_t *vmem_create(const char *, void *, size_t, size_t, void *, 493 void *, vmem_t *, size_t, int); 494extern void vmem_destroy(vmem_t *); 495extern void vmem_free(vmem_t *vmp, void *vaddr, size_t size); 496 497/* 498 * Atomic 499 */ 500 501static inline void atomic_add_32( uint32_t *theAddress, int32_t theAmount ) 502{ 503 (void)OSAddAtomic( theAmount, theAddress ); 504} 505 506#if defined(__i386__) || defined(__x86_64__) 507static inline void atomic_add_64( uint64_t *theAddress, int64_t theAmount ) 508{ 509 (void)OSAddAtomic64( theAmount, (SInt64 *)theAddress ); 510} 511#endif 512 513/* 514 * Miscellaneous 515 */ 516 517typedef uintptr_t pc_t; 518typedef uintptr_t greg_t; /* For dtrace_impl.h prototype of dtrace_getfp() */ 519extern struct regs *find_user_regs( thread_t thread); 520extern vm_offset_t dtrace_get_cpu_int_stack_top(void); 521extern vm_offset_t max_valid_stack_address(void); /* kern/thread.h */ 522 523extern volatile int panicwait; /* kern/debug.c */ 524#define panic_quiesce (panicwait) 525 526#define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) 527 528extern void delay( int ); /* kern/clock.h */ 529 530extern int vuprintf(const char *, va_list); 531 532extern hrtime_t dtrace_abs_to_nano(uint64_t); 533 534__private_extern__ const char * strstr(const char *, const char *); 535 536#undef proc_t 537 538/* 539 * Safe counted string compare against a literal string. The sizeof() intentionally 540 * counts the trailing NUL, and so ensures that all the characters in the literal 541 * can participate in the comparison. 542 */ 543#define LIT_STRNEQL(s1, lit_s2) (0 == strncmp( (s1), (lit_s2), sizeof((lit_s2)) )) 544 545/* 546 * Safe counted string compare of a literal against the beginning of a string. Here 547 * the sizeof() is reduced by 1 so that the trailing null of the literal does not 548 * participate in the comparison. 549 */ 550#define LIT_STRNSTART(s1, lit_s2) (0 == strncmp( (s1), (lit_s2), sizeof((lit_s2)) - 1 )) 551 552#define KERNELBASE VM_MIN_KERNEL_ADDRESS 553#endif /* KERNEL_BUILD */ 554#endif /* _DTRACE_GLUE_H */ 555 556