1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)subr_prof.c 7.10 (Berkeley) 05/07/91 8 */ 9 10 #ifdef GPROF 11 #include "gprof.h" 12 #include "param.h" 13 #include "systm.h" 14 #include "malloc.h" 15 16 /* 17 * Froms is actually a bunch of unsigned shorts indexing tos 18 */ 19 int profiling = 3; 20 u_short *froms; 21 struct tostruct *tos = 0; 22 long tolimit = 0; 23 char *s_lowpc = (char *)KERNBASE; 24 extern char etext; 25 char *s_highpc = &etext; 26 u_long s_textsize = 0; 27 int ssiz; 28 u_short *sbuf; 29 u_short *kcount; 30 31 kmstartup() 32 { 33 u_long fromssize, tossize; 34 35 /* 36 * Round lowpc and highpc to multiples of the density we're using 37 * so the rest of the scaling (here and in gprof) stays in ints. 38 */ 39 s_lowpc = (char *) 40 ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER)); 41 s_highpc = (char *) 42 ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER)); 43 s_textsize = s_highpc - s_lowpc; 44 printf("Profiling kernel, s_textsize=%d [%x..%x]\n", 45 s_textsize, s_lowpc, s_highpc); 46 ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr); 47 sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK); 48 if (sbuf == 0) { 49 printf("No space for monitor buffer(s)\n"); 50 return; 51 } 52 bzero(sbuf, ssiz); 53 fromssize = s_textsize / HASHFRACTION; 54 froms = (u_short *)malloc(fromssize, M_GPROF, M_NOWAIT); 55 if (froms == 0) { 56 printf("No space for monitor buffer(s)\n"); 57 free(sbuf, M_GPROF); 58 sbuf = 0; 59 return; 60 } 61 bzero(froms, fromssize); 62 tolimit = s_textsize * ARCDENSITY / 100; 63 if (tolimit < MINARCS) 64 tolimit = MINARCS; 65 else if (tolimit > (0xffff - 1)) 66 tolimit = 0xffff - 1; 67 tossize = tolimit * sizeof (struct tostruct); 68 tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK); 69 if (tos == 0) { 70 printf("No space for monitor buffer(s)\n"); 71 free(sbuf, M_GPROF), sbuf = 0; 72 free(froms, M_GPROF), froms = 0; 73 return; 74 } 75 bzero(tos, tossize); 76 tos[0].link = 0; 77 ((struct phdr *)sbuf)->lpc = s_lowpc; 78 ((struct phdr *)sbuf)->hpc = s_highpc; 79 ((struct phdr *)sbuf)->ncnt = ssiz; 80 kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr)); 81 } 82 83 /* 84 * This routine is massaged so that it may be jsb'ed to on vax. 85 */ 86 asm(".text"); 87 asm("#the beginning of mcount()"); 88 asm(".data"); 89 mcount() 90 { 91 register char *selfpc; /* r11 => r5 */ 92 register u_short *frompcindex; /* r10 => r4 */ 93 register struct tostruct *top; /* r9 => r3 */ 94 register struct tostruct *prevtop; /* r8 => r2 */ 95 register long toindex; /* r7 => r1 */ 96 static int s; 97 98 asm(" .text"); /* make sure we're in text space */ 99 /* 100 * Check that we are profiling. 101 */ 102 if (profiling) 103 goto out; 104 /* 105 * Find the return address for mcount, 106 * and the return address for mcount's caller. 107 */ 108 #ifdef lint 109 selfpc = (char *)0; 110 frompcindex = 0; 111 #else 112 ; /* avoid label botch */ 113 #ifdef __GNUC__ 114 #if defined(vax) 115 Fix Me!! 116 #endif 117 #if defined(tahoe) 118 Fix Me!! 119 #endif 120 #if defined(hp300) 121 /* 122 * selfpc = pc pushed by mcount jsr, 123 * frompcindex = pc pushed by jsr into self. 124 * In GCC the caller's stack frame has already been built so we 125 * have to chase a6 to find caller's raddr. This assumes that all 126 * routines we are profiling were built with GCC and that all 127 * profiled routines use link/unlk. 128 */ 129 asm("movl a6@(4),%0" : "=r" (selfpc)); 130 asm("movl a6@(0)@(4),%0" : "=r" (frompcindex)); 131 #endif 132 #else 133 #if defined(vax) 134 asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */ 135 asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */ 136 #endif 137 #if defined(tahoe) 138 asm(" movl -8(fp),r12"); /* selfpc = callf frame */ 139 asm(" movl (fp),r11"); 140 asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */ 141 #endif 142 #if defined(hp300) 143 Fix Me!! 144 #endif 145 #endif /* not __GNUC__ */ 146 #endif /* not lint */ 147 /* 148 * Insure that we cannot be recursively invoked. 149 * this requires that splhigh() and splx() below 150 * do NOT call mcount! 151 */ 152 #if defined(hp300) 153 asm("movw sr,%0" : "=g" (s)); 154 asm("movw #0x2700,sr"); 155 #else 156 s = splhigh(); 157 #endif 158 /* 159 * Check that frompcindex is a reasonable pc value. 160 * For example: signal catchers get called from the stack, 161 * not from text space. too bad. 162 */ 163 frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc); 164 if ((u_long)frompcindex > s_textsize) 165 goto done; 166 frompcindex = 167 &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))]; 168 toindex = *frompcindex; 169 if (toindex == 0) { 170 /* 171 * First time traversing this arc 172 */ 173 toindex = ++tos[0].link; 174 if (toindex >= tolimit) 175 goto overflow; 176 *frompcindex = toindex; 177 top = &tos[toindex]; 178 top->selfpc = selfpc; 179 top->count = 1; 180 top->link = 0; 181 goto done; 182 } 183 top = &tos[toindex]; 184 if (top->selfpc == selfpc) { 185 /* 186 * Arc at front of chain; usual case. 187 */ 188 top->count++; 189 goto done; 190 } 191 /* 192 * Have to go looking down chain for it. 193 * Top points to what we are looking at, 194 * prevtop points to previous top. 195 * We know it is not at the head of the chain. 196 */ 197 for (; /* goto done */; ) { 198 if (top->link == 0) { 199 /* 200 * Top is end of the chain and none of the chain 201 * had top->selfpc == selfpc. 202 * So we allocate a new tostruct 203 * and link it to the head of the chain. 204 */ 205 toindex = ++tos[0].link; 206 if (toindex >= tolimit) 207 goto overflow; 208 top = &tos[toindex]; 209 top->selfpc = selfpc; 210 top->count = 1; 211 top->link = *frompcindex; 212 *frompcindex = toindex; 213 goto done; 214 } 215 /* 216 * Otherwise, check the next arc on the chain. 217 */ 218 prevtop = top; 219 top = &tos[top->link]; 220 if (top->selfpc == selfpc) { 221 /* 222 * There it is, increment its count and 223 * move it to the head of the chain. 224 */ 225 top->count++; 226 toindex = prevtop->link; 227 prevtop->link = top->link; 228 top->link = *frompcindex; 229 *frompcindex = toindex; 230 goto done; 231 } 232 233 } 234 done: 235 #if defined(hp300) 236 asm("movw %0,sr" : : "g" (s)); 237 #else 238 splx(s); 239 #endif 240 /* and fall through */ 241 out: 242 #if defined(vax) 243 asm(" rsb"); 244 #endif 245 return; 246 overflow: 247 profiling = 3; 248 printf("mcount: tos overflow\n"); 249 goto out; 250 } 251 asm(".text"); 252 asm("#the end of mcount()"); 253 asm(".data"); 254 #endif 255