Missing header file from Net/2 tape.
[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
dd18dc33
DG
151#if defined(i386)
152 /*
153 * selfpc = pc pushed by mcount call
154 */
155 asm("movl 4(%%ebp),%0" : "=r" (selfpc));
156 /*
157 * frompcindex = pc pushed by jsr into self.
158 * in GCC, the caller's stack frame has already been built, so we
159 * have to chase the base pointer to find caller's raddr.
160 */
161 asm("movl (%%ebp),%0" : "=r" (frompcindex));
162 frompcindex = ((unsigned short **)frompcindex)[1];
163#endif /* i386 */
15637ed4
RG
164#else
165#if defined(vax)
166 asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */
167 asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */
168#endif
169#if defined(i386)
dd18dc33 170 Fix Me!!
15637ed4
RG
171#endif /* i386 */
172#if defined(tahoe)
173 asm(" movl -8(fp),r12"); /* selfpc = callf frame */
174 asm(" movl (fp),r11");
175 asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */
176#endif
177#if defined(hp300)
178 Fix Me!!
179#endif
180#endif /* not __GNUC__ */
181#endif /* not lint */
182 /*
183 * Insure that we cannot be recursively invoked.
184 * this requires that splhigh() and splx() below
185 * do NOT call mcount!
186 */
187#if defined(hp300)
188 asm("movw sr,%0" : "=g" (s));
189 asm("movw #0x2700,sr");
190#else
191 s = splhigh();
192#endif
193 /*
194 * Check that frompcindex is a reasonable pc value.
195 * For example: signal catchers get called from the stack,
196 * not from text space. too bad.
197 */
198 frompcindex = (u_short *)((u_long)frompcindex - (u_long)s_lowpc);
199 if ((u_long)frompcindex > s_textsize)
200 goto done;
201 frompcindex =
202 &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))];
203 toindex = *frompcindex;
204 if (toindex == 0) {
205 /*
206 * First time traversing this arc
207 */
208 toindex = ++tos[0].link;
209 if (toindex >= tolimit)
210 goto overflow;
211 *frompcindex = toindex;
212 top = &tos[toindex];
213 top->selfpc = selfpc;
214 top->count = 1;
215 top->link = 0;
216 goto done;
217 }
218 top = &tos[toindex];
219 if (top->selfpc == selfpc) {
220 /*
221 * Arc at front of chain; usual case.
222 */
223 top->count++;
224 goto done;
225 }
226 /*
227 * Have to go looking down chain for it.
228 * Top points to what we are looking at,
229 * prevtop points to previous top.
230 * We know it is not at the head of the chain.
231 */
232 for (; /* goto done */; ) {
233 if (top->link == 0) {
234 /*
235 * Top is end of the chain and none of the chain
236 * had top->selfpc == selfpc.
237 * So we allocate a new tostruct
238 * and link it to the head of the chain.
239 */
240 toindex = ++tos[0].link;
241 if (toindex >= tolimit)
242 goto overflow;
243 top = &tos[toindex];
244 top->selfpc = selfpc;
245 top->count = 1;
246 top->link = *frompcindex;
247 *frompcindex = toindex;
248 goto done;
249 }
250 /*
251 * Otherwise, check the next arc on the chain.
252 */
253 prevtop = top;
254 top = &tos[top->link];
255 if (top->selfpc == selfpc) {
256 /*
257 * There it is, increment its count and
258 * move it to the head of the chain.
259 */
260 top->count++;
261 toindex = prevtop->link;
262 prevtop->link = top->link;
263 top->link = *frompcindex;
264 *frompcindex = toindex;
265 goto done;
266 }
267
268 }
269done:
270#if defined(hp300)
271 asm("movw %0,sr" : : "g" (s));
272#else
273 splx(s);
274#endif
275 /* and fall through */
276out:
277#if defined(vax)
278 asm(" rsb");
279#endif
280 return;
281overflow:
282 profiling = 3;
283 printf("mcount: tos overflow\n");
284 goto out;
285}
286#endif