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