Initial import, 0.1 + pk 0.2.4-B1
[unix-history] / sys / kern / subr_mcount.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)subr_mcount.c 7.10 (Berkeley) 5/7/91
34 */
35
36#ifdef GPROF
37#include "gprof.h"
38#include "param.h"
39#include "systm.h"
40#include "malloc.h"
41
42/*
43 * Froms is actually a bunch of unsigned shorts indexing tos
44 */
45int profiling = 3;
46u_short *froms;
47struct tostruct *tos = 0;
48long tolimit = 0;
49char *s_lowpc = (char *)KERNBASE;
50extern char etext;
51char *s_highpc = &etext;
52u_long s_textsize = 0;
53int ssiz;
54u_short *sbuf;
55u_short *kcount;
56
57kmstartup()
58{
59 u_long fromssize, tossize;
60
61 /*
62 * Round lowpc and highpc to multiples of the density we're using
63 * so the rest of the scaling (here and in gprof) stays in ints.
64 */
65 s_lowpc = (char *)
66 ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER));
67 s_highpc = (char *)
68 ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER));
69 s_textsize = s_highpc - s_lowpc;
70 printf("Profiling kernel, s_textsize=%d [%x..%x]\n",
71 s_textsize, s_lowpc, s_highpc);
72 ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr);
73 sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK);
74 if (sbuf == 0) {
75 printf("No space for monitor buffer(s)\n");
76 return;
77 }
78 bzero(sbuf, ssiz);
79 fromssize = s_textsize / HASHFRACTION;
80 froms = (u_short *)malloc(fromssize, M_GPROF, M_NOWAIT);
81 if (froms == 0) {
82 printf("No space for monitor buffer(s)\n");
83 free(sbuf, M_GPROF);
84 sbuf = 0;
85 return;
86 }
87 bzero(froms, fromssize);
88 tolimit = s_textsize * ARCDENSITY / 100;
89 if (tolimit < MINARCS)
90 tolimit = MINARCS;
91 else if (tolimit > (0xffff - 1))
92 tolimit = 0xffff - 1;
93 tossize = tolimit * sizeof (struct tostruct);
94 tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK);
95 if (tos == 0) {
96 printf("No space for monitor buffer(s)\n");
97 free(sbuf, M_GPROF), sbuf = 0;
98 free(froms, M_GPROF), froms = 0;
99 return;
100 }
101 bzero(tos, tossize);
102 tos[0].link = 0;
103 ((struct phdr *)sbuf)->lpc = s_lowpc;
104 ((struct phdr *)sbuf)->hpc = s_highpc;
105 ((struct phdr *)sbuf)->ncnt = ssiz;
106 kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr));
107}
108
109mcount()
110{
111 register char *selfpc; /* r11 => r5 */
112 register u_short *frompcindex; /* r10 => r4 */
113 register struct tostruct *top; /* r9 => r3 */
114 register struct tostruct *prevtop; /* r8 => r2 */
115 register long toindex; /* r7 => r1 */
116 static int s;
117
118 /*
119 * Check that we are profiling.
120 */
121 if (profiling)
122 goto out;
123 /*
124 * Find the return address for mcount,
125 * and the return address for mcount's caller.
126 */
127#ifdef lint
128 selfpc = (char *)0;
129 frompcindex = 0;
130#else
131 ; /* avoid label botch */
132#ifdef __GNUC__
133#if defined(vax)
134 Fix Me!!
135#endif
136#if defined(tahoe)
137 Fix Me!!
138#endif
139#if defined(hp300)
140 /*
141 * selfpc = pc pushed by mcount jsr,
142 * frompcindex = pc pushed by jsr into self.
143 * In GCC the caller's stack frame has already been built so we
144 * have to chase a6 to find caller's raddr. This assumes that all
145 * routines we are profiling were built with GCC and that all
146 * profiled routines use link/unlk.
147 */
148 asm("movl a6@(4),%0" : "=r" (selfpc));
149 asm("movl a6@(0)@(4),%0" : "=r" (frompcindex));
150#endif
151#else
152#if defined(vax)
153 asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */
154 asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */
155#endif
156#if defined(i386)
157 /*
158 * selfpc = pc pushed by mcount call
159 */
160 asm("movl 4(%%ebp),%0" : "=r" (selfpc));
161 /*
162 * frompcindex = pc pushed by jsr into self.
163 * in GCC, the caller's stack frame has already been built, so we
164 * have to chase the base pointer to find caller's raddr.
165 */
166 asm("movl (%%ebp),%0" : "=r" (frompcindex));
167 frompcindex = ((unsigned short **)frompcindex)[1];
168#endif /* i386 */
169#if defined(tahoe)
170 asm(" movl -8(fp),r12"); /* selfpc = callf frame */
171 asm(" movl (fp),r11");
172 asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */
173#endif
174#if defined(hp300)
175 Fix Me!!
176#endif
177#endif /* not __GNUC__ */
178#endif /* not lint */
179 /*
180 * Insure that we cannot be recursively invoked.
181 * this requires that splhigh() and splx() below
182 * do NOT call mcount!
183 */
184#if defined(hp300)
185 asm("movw sr,%0" : "=g" (s));
186 asm("movw #0x2700,sr");
187#else
188 s = splhigh();
189#endif
190 /*
191 * Check that frompcindex is a reasonable pc value.
192 * For example: signal catchers get called from the stack,
193 * not from text space. too bad.
194 */
195 frompcindex = (u_short *)((u_long)frompcindex - (u_long)s_lowpc);
196 if ((u_long)frompcindex > s_textsize)
197 goto done;
198 frompcindex =
199 &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))];
200 toindex = *frompcindex;
201 if (toindex == 0) {
202 /*
203 * First time traversing this arc
204 */
205 toindex = ++tos[0].link;
206 if (toindex >= tolimit)
207 goto overflow;
208 *frompcindex = toindex;
209 top = &tos[toindex];
210 top->selfpc = selfpc;
211 top->count = 1;
212 top->link = 0;
213 goto done;
214 }
215 top = &tos[toindex];
216 if (top->selfpc == selfpc) {
217 /*
218 * Arc at front of chain; usual case.
219 */
220 top->count++;
221 goto done;
222 }
223 /*
224 * Have to go looking down chain for it.
225 * Top points to what we are looking at,
226 * prevtop points to previous top.
227 * We know it is not at the head of the chain.
228 */
229 for (; /* goto done */; ) {
230 if (top->link == 0) {
231 /*
232 * Top is end of the chain and none of the chain
233 * had top->selfpc == selfpc.
234 * So we allocate a new tostruct
235 * and link it to the head of the chain.
236 */
237 toindex = ++tos[0].link;
238 if (toindex >= tolimit)
239 goto overflow;
240 top = &tos[toindex];
241 top->selfpc = selfpc;
242 top->count = 1;
243 top->link = *frompcindex;
244 *frompcindex = toindex;
245 goto done;
246 }
247 /*
248 * Otherwise, check the next arc on the chain.
249 */
250 prevtop = top;
251 top = &tos[top->link];
252 if (top->selfpc == selfpc) {
253 /*
254 * There it is, increment its count and
255 * move it to the head of the chain.
256 */
257 top->count++;
258 toindex = prevtop->link;
259 prevtop->link = top->link;
260 top->link = *frompcindex;
261 *frompcindex = toindex;
262 goto done;
263 }
264
265 }
266done:
267#if defined(hp300)
268 asm("movw %0,sr" : : "g" (s));
269#else
270 splx(s);
271#endif
272 /* and fall through */
273out:
274#if defined(vax)
275 asm(" rsb");
276#endif
277 return;
278overflow:
279 profiling = 3;
280 printf("mcount: tos overflow\n");
281 goto out;
282}
283#endif