| 1 | /* |
| 2 | * Copyright (c) 2000-2008 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 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ |
| 29 | /*- |
| 30 | * Copyright (c) 1982, 1986, 1993 |
| 31 | * The Regents of the University of California. All rights reserved. |
| 32 | * |
| 33 | * Redistribution and use in source and binary forms, with or without |
| 34 | * modification, are permitted provided that the following conditions |
| 35 | * are met: |
| 36 | * 1. Redistributions of source code must retain the above copyright |
| 37 | * notice, this list of conditions and the following disclaimer. |
| 38 | * 2. Redistributions in binary form must reproduce the above copyright |
| 39 | * notice, this list of conditions and the following disclaimer in the |
| 40 | * documentation and/or other materials provided with the distribution. |
| 41 | * 3. All advertising materials mentioning features or use of this software |
| 42 | * must display the following acknowledgement: |
| 43 | * This product includes software developed by the University of |
| 44 | * California, Berkeley and its contributors. |
| 45 | * 4. Neither the name of the University nor the names of its contributors |
| 46 | * may be used to endorse or promote products derived from this software |
| 47 | * without specific prior written permission. |
| 48 | * |
| 49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 59 | * SUCH DAMAGE. |
| 60 | * |
| 61 | * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93 |
| 62 | */ |
| 63 | |
| 64 | #ifdef GPROF |
| 65 | #include <libkern/kernel_mach_header.h> |
| 66 | #endif |
| 67 | |
| 68 | #include <sys/param.h> |
| 69 | #include <sys/systm.h> |
| 70 | #include <sys/kernel.h> |
| 71 | #include <sys/proc_internal.h> |
| 72 | #include <sys/user.h> |
| 73 | #include <machine/machine_routines.h> |
| 74 | |
| 75 | #include <sys/mount_internal.h> |
| 76 | #include <sys/sysproto.h> |
| 77 | |
| 78 | #include <mach/mach_types.h> |
| 79 | #include <kern/kern_types.h> |
| 80 | #include <kern/cpu_number.h> |
| 81 | #include <kern/kalloc.h> |
| 82 | |
| 83 | #ifdef GPROF |
| 84 | #include <sys/malloc.h> |
| 85 | #include <sys/gmon.h> |
| 86 | |
| 87 | extern int sysctl_doprof(int *, u_int, user_addr_t, size_t *, |
| 88 | user_addr_t, size_t newlen); |
| 89 | extern int sysctl_struct(user_addr_t, size_t *, |
| 90 | user_addr_t, size_t, void *, int); |
| 91 | |
| 92 | lck_spin_t * mcount_lock; |
| 93 | lck_grp_t * mcount_lock_grp; |
| 94 | lck_attr_t * mcount_lock_attr; |
| 95 | |
| 96 | /* |
| 97 | * Froms is actually a bunch of unsigned shorts indexing tos |
| 98 | */ |
| 99 | struct gmonparam _gmonparam = { .state = GMON_PROF_OFF }; |
| 100 | |
| 101 | /* |
| 102 | * This code uses 32 bit mach object segment information from the currently |
| 103 | * running kernel. |
| 104 | */ |
| 105 | void |
| 106 | kmstartup(void) |
| 107 | { |
| 108 | tostruct_t *cp; |
| 109 | kernel_segment_command_t *sgp; /* 32 bit mach object file segment */ |
| 110 | struct gmonparam *p = &_gmonparam; |
| 111 | |
| 112 | sgp = getsegbyname("__TEXT" ); |
| 113 | p->lowpc = (u_int32_t)sgp->vmaddr; |
| 114 | p->highpc = (u_int32_t)(sgp->vmaddr + sgp->vmsize); |
| 115 | |
| 116 | /* |
| 117 | * Round lowpc and highpc to multiples of the density we're using |
| 118 | * so the rest of the scaling (here and in gprof) stays in ints. |
| 119 | */ |
| 120 | p->lowpc = ROUNDDOWN(p->lowpc, HISTFRACTION * sizeof(HISTCOUNTER)); |
| 121 | p->highpc = ROUNDUP(p->highpc, HISTFRACTION * sizeof(HISTCOUNTER)); |
| 122 | p->textsize = p->highpc - p->lowpc; |
| 123 | printf("Profiling kernel, textsize=%lu [0x%016lx..0x%016lx]\n" , |
| 124 | p->textsize, p->lowpc, p->highpc); |
| 125 | p->kcountsize = p->textsize / HISTFRACTION; |
| 126 | p->hashfraction = HASHFRACTION; |
| 127 | p->fromssize = p->textsize / HASHFRACTION; |
| 128 | p->tolimit = p->textsize * ARCDENSITY / 100; |
| 129 | if (p->tolimit < MINARCS) |
| 130 | p->tolimit = MINARCS; |
| 131 | else if (p->tolimit > MAXARCS) |
| 132 | p->tolimit = MAXARCS; |
| 133 | p->tossize = p->tolimit * sizeof(tostruct_t); |
| 134 | /* Why not use MALLOC with M_GPROF ? */ |
| 135 | cp = (tostruct_t *)kalloc(p->kcountsize + p->fromssize + p->tossize); |
| 136 | if (cp == 0) { |
| 137 | printf("No memory for profiling.\n" ); |
| 138 | return; |
| 139 | } |
| 140 | bzero(cp, p->kcountsize + p->tossize + p->fromssize); |
| 141 | p->tos = cp; |
| 142 | cp = (tostruct_t *)((vm_offset_t)cp + p->tossize); |
| 143 | p->kcount = (u_short *)cp; |
| 144 | cp = (tostruct_t *)((vm_offset_t)cp + p->kcountsize); |
| 145 | p->froms = (u_short *)cp; |
| 146 | |
| 147 | mcount_lock_grp = lck_grp_alloc_init("MCOUNT" , LCK_GRP_ATTR_NULL); |
| 148 | mcount_lock_attr = lck_attr_alloc_init(); |
| 149 | mcount_lock = lck_spin_alloc_init(mcount_lock_grp, mcount_lock_attr); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * XXX These should be broken out into per-argument OID values, |
| 155 | * XXX since there are no sub-OID parameter values, but unfortunately |
| 156 | * XXX there is barely enough time for an initial conversion. |
| 157 | * |
| 158 | * Note: These items appear to be read/write. |
| 159 | */ |
| 160 | STATIC int |
| 161 | sysctl_doprofhandle SYSCTL_HANDLER_ARGS |
| 162 | { |
| 163 | sysctl_doprof(int *name, u_int namelen, user_addr_t oldp, size_t *oldlenp, |
| 164 | user_addr_t newp, size_t newlen) |
| 165 | { |
| 166 | __unused int cmd = oidp->oid_arg2; /* subcommand*/ |
| 167 | int *name = arg1; /* oid element argument vector */ |
| 168 | int namelen = arg2; /* number of oid element arguments */ |
| 169 | user_addr_t oldp = req->oldptr; /* user buffer copy out address */ |
| 170 | size_t *oldlenp = req->oldlen; /* user buffer copy out size */ |
| 171 | user_addr_t newp = req->newptr; /* user buffer copy in address */ |
| 172 | size_t newlen = req->newlen; /* user buffer copy in size */ |
| 173 | |
| 174 | struct gmonparam *gp = &_gmonparam; |
| 175 | int error = 0; |
| 176 | |
| 177 | /* all sysctl names at this level are terminal */ |
| 178 | if (namelen != 1) |
| 179 | return (ENOTDIR); /* overloaded */ |
| 180 | |
| 181 | switch (name[0]) { |
| 182 | case GPROF_STATE: |
| 183 | error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state); |
| 184 | if (error) |
| 185 | break; |
| 186 | if (gp->state == GMON_PROF_OFF) |
| 187 | stopprofclock(kernproc); |
| 188 | else |
| 189 | startprofclock(kernproc); |
| 190 | break; |
| 191 | case GPROF_COUNT: |
| 192 | error = sysctl_struct(oldp, oldlenp, newp, newlen, |
| 193 | gp->kcount, gp->kcountsize); |
| 194 | break; |
| 195 | case GPROF_FROMS: |
| 196 | error = sysctl_struct(oldp, oldlenp, newp, newlen, |
| 197 | gp->froms, gp->fromssize); |
| 198 | break; |
| 199 | case GPROF_TOS: |
| 200 | error = sysctl_struct(oldp, oldlenp, newp, newlen, |
| 201 | gp->tos, gp->tossize); |
| 202 | break; |
| 203 | case GPROF_GMONPARAM: |
| 204 | error = sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp); |
| 205 | break; |
| 206 | default: |
| 207 | error = ENOTSUP; |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | /* adjust index so we return the right required/consumed amount */ |
| 212 | if (!error) |
| 213 | req->oldidx += req->oldlen; |
| 214 | |
| 215 | return(error); |
| 216 | } |
| 217 | SYSCTL_PROC(_kern, KERN_PROF, prof, STLFLAG_NODE|CTLFLAG_RW | CTLFLAG_LOCKED, |
| 218 | 0, /* Pointer argument (arg1) */ |
| 219 | 0, /* Integer argument (arg2) */ |
| 220 | sysctl_doprofhandle, /* Handler function */ |
| 221 | NULL, /* No explicit data */ |
| 222 | "" ); |
| 223 | |
| 224 | |
| 225 | /* |
| 226 | * mcount() called with interrupts disabled. |
| 227 | */ |
| 228 | void |
| 229 | mcount( |
| 230 | uintptr_t frompc, |
| 231 | uintptr_t selfpc |
| 232 | ) |
| 233 | { |
| 234 | unsigned short *frompcindex; |
| 235 | tostruct_t *top, *prevtop; |
| 236 | struct gmonparam *p = &_gmonparam; |
| 237 | long toindex; |
| 238 | |
| 239 | /* |
| 240 | * check that we are profiling |
| 241 | * and that we aren't recursively invoked. |
| 242 | */ |
| 243 | if (p->state != GMON_PROF_ON) |
| 244 | return; |
| 245 | |
| 246 | lck_spin_lock(mcount_lock); |
| 247 | |
| 248 | /* |
| 249 | * check that frompcindex is a reasonable pc value. |
| 250 | * for example: signal catchers get called from the stack, |
| 251 | * not from text space. too bad. |
| 252 | */ |
| 253 | frompc -= p->lowpc; |
| 254 | if (frompc > p->textsize) |
| 255 | goto done; |
| 256 | |
| 257 | frompcindex = &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))]; |
| 258 | toindex = *frompcindex; |
| 259 | if (toindex == 0) { |
| 260 | /* |
| 261 | * first time traversing this arc |
| 262 | */ |
| 263 | toindex = ++p->tos[0].link; |
| 264 | if (toindex >= p->tolimit) { |
| 265 | /* halt further profiling */ |
| 266 | goto overflow; |
| 267 | } |
| 268 | *frompcindex = toindex; |
| 269 | top = &p->tos[toindex]; |
| 270 | top->selfpc = selfpc; |
| 271 | top->count = 1; |
| 272 | top->link = 0; |
| 273 | goto done; |
| 274 | } |
| 275 | top = &p->tos[toindex]; |
| 276 | if (top->selfpc == selfpc) { |
| 277 | /* |
| 278 | * arc at front of chain; usual case. |
| 279 | */ |
| 280 | top->count++; |
| 281 | goto done; |
| 282 | } |
| 283 | /* |
| 284 | * have to go looking down chain for it. |
| 285 | * top points to what we are looking at, |
| 286 | * prevtop points to previous top. |
| 287 | * we know it is not at the head of the chain. |
| 288 | */ |
| 289 | for (; /* goto done */; ) { |
| 290 | if (top->link == 0) { |
| 291 | /* |
| 292 | * top is end of the chain and none of the chain |
| 293 | * had top->selfpc == selfpc. |
| 294 | * so we allocate a new tostruct |
| 295 | * and link it to the head of the chain. |
| 296 | */ |
| 297 | toindex = ++p->tos[0].link; |
| 298 | if (toindex >= p->tolimit) { |
| 299 | goto overflow; |
| 300 | } |
| 301 | top = &p->tos[toindex]; |
| 302 | top->selfpc = selfpc; |
| 303 | top->count = 1; |
| 304 | top->link = *frompcindex; |
| 305 | *frompcindex = toindex; |
| 306 | goto done; |
| 307 | } |
| 308 | /* |
| 309 | * otherwise, check the next arc on the chain. |
| 310 | */ |
| 311 | prevtop = top; |
| 312 | top = &p->tos[top->link]; |
| 313 | if (top->selfpc == selfpc) { |
| 314 | /* |
| 315 | * there it is. |
| 316 | * increment its count |
| 317 | * move it to the head of the chain. |
| 318 | */ |
| 319 | top->count++; |
| 320 | toindex = prevtop->link; |
| 321 | prevtop->link = top->link; |
| 322 | top->link = *frompcindex; |
| 323 | *frompcindex = toindex; |
| 324 | goto done; |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | done: |
| 329 | lck_spin_unlock(mcount_lock); |
| 330 | return; |
| 331 | |
| 332 | overflow: |
| 333 | p->state = GMON_PROF_ERROR; |
| 334 | lck_spin_unlock(mcount_lock); |
| 335 | printf("mcount: tos overflow\n" ); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | #endif /* GPROF */ |
| 340 | |
| 341 | #define PROFILE_LOCK(x) |
| 342 | #define PROFILE_UNLOCK(x) |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * Scale is a fixed-point number with the binary point 16 bits |
| 347 | * into the value, and is <= 1.0. pc is at most 32 bits, so the |
| 348 | * intermediate result is at most 48 bits. |
| 349 | */ |
| 350 | //K64todo - this doesn't fit into 64 bit any more, it needs 64+16 |
| 351 | #define PC_TO_INDEX(pc, prof) \ |
| 352 | ((user_addr_t)(((u_quad_t)((pc) - (prof)->pr_off) * \ |
| 353 | (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) |
| 354 | |
| 355 | /* |
| 356 | * Collect user-level profiling statistics; called on a profiling tick, |
| 357 | * when a process is running in user-mode. We use |
| 358 | * an AST that will vector us to trap() with a context in which copyin |
| 359 | * and copyout will work. Trap will then call addupc_task(). |
| 360 | * |
| 361 | * Note that we may (rarely) not get around to the AST soon enough, and |
| 362 | * lose profile ticks when the next tick overwrites this one, but in this |
| 363 | * case the system is overloaded and the profile is probably already |
| 364 | * inaccurate. |
| 365 | * |
| 366 | * We can afford to take faults here. If the |
| 367 | * update fails, we simply turn off profiling. |
| 368 | */ |
| 369 | void |
| 370 | addupc_task(struct proc *p, user_addr_t pc, u_int ticks) |
| 371 | { |
| 372 | user_addr_t off; |
| 373 | u_short count; |
| 374 | |
| 375 | /* Testing P_PROFIL may be unnecessary, but is certainly safe. */ |
| 376 | if ((p->p_flag & P_PROFIL) == 0 || ticks == 0) |
| 377 | return; |
| 378 | |
| 379 | if (proc_is64bit(p)) { |
| 380 | struct user_uprof *prof; |
| 381 | user_addr_t cell; |
| 382 | |
| 383 | for (prof = &p->p_stats->user_p_prof; prof; prof = prof->pr_next) { |
| 384 | off = PC_TO_INDEX(pc, prof); |
| 385 | cell = (prof->pr_base + off); |
| 386 | if (cell >= prof->pr_base && |
| 387 | cell < (prof->pr_size + prof->pr_base)) { |
| 388 | if (copyin(cell, (caddr_t) &count, sizeof(count)) == 0) { |
| 389 | count += ticks; |
| 390 | if(copyout((caddr_t) &count, cell, sizeof(count)) == 0) |
| 391 | return; |
| 392 | } |
| 393 | p->p_stats->user_p_prof.pr_scale = 0; |
| 394 | stopprofclock(p); |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | else { |
| 400 | struct uprof *prof; |
| 401 | short *cell; |
| 402 | |
| 403 | for (prof = &p->p_stats->p_prof; prof; prof = prof->pr_next) { |
| 404 | off = PC_TO_INDEX(pc,prof); |
| 405 | cell = (short *)(prof->pr_base + off); |
| 406 | if (cell >= (short *)prof->pr_base && |
| 407 | cell < (short*)(prof->pr_size + prof->pr_base)) { |
| 408 | if (copyin(CAST_USER_ADDR_T(cell), (caddr_t) &count, sizeof(count)) == 0) { |
| 409 | count += ticks; |
| 410 | if(copyout((caddr_t) &count, CAST_USER_ADDR_T(cell), sizeof(count)) == 0) |
| 411 | return; |
| 412 | } |
| 413 | p->p_stats->p_prof.pr_scale = 0; |
| 414 | stopprofclock(p); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |