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