1 /**	$MirOS: src/sys/kern/subr_prof.c,v 1.2 2005/03/06 21:28:03 tg Exp $ */
2 /*	$OpenBSD: subr_prof.c,v 1.14 2003/09/01 18:06:03 henning Exp $	*/
3 /*	$NetBSD: subr_prof.c,v 1.12 1996/04/22 01:38:50 christos Exp $	*/
4 
5 /*-
6  * Copyright (c) 1982, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. 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_prof.c	8.3 (Berkeley) 9/23/93
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/user.h>
41 #include <sys/mount.h>
42 #include <sys/syscallargs.h>
43 
44 #include <machine/cpu.h>
45 
46 /*
47  * Profiling system call.
48  *
49  * The scale factor is a fixed point number with 16 bits of fraction, so that
50  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
51  */
52 /* ARGSUSED */
53 int
sys_profil(p,v,retval)54 sys_profil(p, v, retval)
55 	struct proc *p;
56 	void *v;
57 	register_t *retval;
58 {
59 	register struct sys_profil_args /* {
60 		syscallarg(caddr_t) samples;
61 		syscallarg(size_t) size;
62 		syscallarg(u_long) offset;
63 		syscallarg(u_int) scale;
64 	} */ *uap = v;
65 	register struct uprof *upp;
66 	int s;
67 
68 	if (SCARG(uap, scale) > (1 << 16))
69 		return (EINVAL);
70 	if (SCARG(uap, scale) == 0) {
71 		stopprofclock(p);
72 		return (0);
73 	}
74 	upp = &p->p_stats->p_prof;
75 
76 	/* Block profile interrupts while changing state. */
77 	s = splstatclock();
78 	upp->pr_off = SCARG(uap, offset);
79 	upp->pr_scale = SCARG(uap, scale);
80 	upp->pr_base = (caddr_t)SCARG(uap, samples);
81 	upp->pr_size = SCARG(uap, size);
82 	startprofclock(p);
83 	splx(s);
84 
85 	return (0);
86 }
87 
88 /*
89  * Scale is a fixed-point number with the binary point 16 bits
90  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
91  * intermediate result is at most 48 bits.
92  */
93 #define	PC_TO_INDEX(pc, prof) \
94 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
95 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
96 
97 /*
98  * Collect user-level profiling statistics; called on a profiling tick,
99  * when a process is running in user-mode.  This routine may be called
100  * from an interrupt context. Schedule an AST that will vector us to
101  * trap() with a context in which copyin and copyout will work.
102  * Trap will then call addupc_task().
103  */
104 void
addupc_intr(struct proc * p,u_long pc)105 addupc_intr(struct proc *p, u_long pc)
106 {
107 	struct uprof *prof;
108 
109 	prof = &p->p_stats->p_prof;
110 	if (pc < prof->pr_off || PC_TO_INDEX(pc, prof) >= prof->pr_size)
111 		return;			/* out of range; ignore */
112 
113 	prof->pr_addr = pc;
114 	need_proftick(p);
115 }
116 
117 
118 /*
119  * Much like before, but we can afford to take faults here.  If the
120  * update fails, we simply turn off profiling.
121  */
122 void
addupc_task(struct proc * p,u_long pc,u_int nticks)123 addupc_task(struct proc *p, u_long pc, u_int nticks)
124 {
125 	struct uprof *prof;
126 	caddr_t addr;
127 	u_int i;
128 	u_short v;
129 
130 	/* Testing P_PROFIL may be unnecessary, but is certainly safe. */
131 	if ((p->p_flag & P_PROFIL) == 0 || nticks == 0)
132 		return;
133 
134 	prof = &p->p_stats->p_prof;
135 	if (pc < prof->pr_off ||
136 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
137 		return;
138 
139 	addr = prof->pr_base + i;
140 	if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
141 		v += nticks;
142 		if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
143 			return;
144 	}
145 	stopprofclock(p);
146 }
147