]> git.saurik.com Git - apple/xnu.git/blob - osfmk/profiling/profile-mk.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / osfmk / profiling / profile-mk.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Microkernel interface to common profiling.
33 */
34
35 #include <profiling/profile-mk.h>
36 #include <string.h>
37 #include <kern/cpu_number.h>
38 #include <kern/processor.h>
39 #include <kern/spl.h>
40 #include <kern/misc_protos.h>
41 #include <vm/vm_kern.h>
42 #include <mach/vm_param.h>
43
44 #include <device/ds_routines.h>
45 #include <device/io_req.h>
46 #include <device/buf.h>
47
48 extern char etext[], pstart[];
49
50 #if NCPUS > 1
51 struct profile_vars *_profile_vars_cpus[NCPUS] = { &_profile_vars };
52 struct profile_vars _profile_vars_aux[NCPUS-1];
53 #endif
54
55 void *
56 _profile_alloc_pages (size_t size)
57 {
58 vm_offset_t addr;
59
60 /*
61 * For the MK, we can't support allocating pages at runtime, because we
62 * might be at interrupt level, so abort if we didn't size the table
63 * properly.
64 */
65
66 if (PROFILE_VARS(0)->active) {
67 panic("Call to _profile_alloc_pages while profiling is running.");
68 }
69
70 if (kmem_alloc(kernel_map, &addr, size)) {
71 panic("Could not allocate memory for profiling");
72 }
73
74 memset((void *)addr, '\0', size);
75 if (PROFILE_VARS(0)->debug) {
76 printf("Allocated %d bytes for profiling, address 0x%x\n", (int)size, (int)addr);
77 }
78
79 return((caddr_t)addr);
80 }
81
82 void
83 _profile_free_pages(void *addr, size_t size)
84 {
85 if (PROFILE_VARS(0)->debug) {
86 printf("Freed %d bytes for profiling, address 0x%x\n", (int)size, (int)addr);
87 }
88
89 kmem_free(kernel_map, (vm_offset_t)addr, size);
90 return;
91 }
92
93 void _profile_error(struct profile_vars *pv)
94 {
95 panic("Fatal error in profiling");
96 }
97
98 void
99 kmstartup(void)
100 {
101 prof_uptrint_t textsize;
102 prof_uptrint_t monsize;
103 prof_uptrint_t lowpc;
104 prof_uptrint_t highpc;
105 int i;
106 struct profile_vars *pv;
107
108 /*
109 * round lowpc and highpc to multiples of the density we're using
110 * so the rest of the scaling (here and in gprof) stays in ints.
111 */
112
113 lowpc = ROUNDDOWN((prof_uptrint_t)&pstart[0], HISTFRACTION*sizeof(LHISTCOUNTER));
114 highpc = ROUNDUP((prof_uptrint_t)&etext[0], HISTFRACTION*sizeof(LHISTCOUNTER));
115 textsize = highpc - lowpc;
116 monsize = (textsize / HISTFRACTION) * sizeof(LHISTCOUNTER);
117
118 for (i = 0; i < NCPUS; i++) {
119 pv = PROFILE_VARS(i);
120
121 #if NCPUS > 1
122 if (!pv) {
123 _profile_vars_cpus[i] = pv = &_profile_vars_aux[i-i];
124 }
125 #endif
126
127 #ifdef DEBUG_PROFILE
128 pv->debug = 1;
129 #endif
130 pv->page_size = PAGE_SIZE;
131 _profile_md_init(pv, PROFILE_GPROF, PROFILE_ALLOC_MEM_YES);
132
133 /* Profil related variables */
134 pv->profil_buf = _profile_alloc (pv, monsize, ACONTEXT_PROFIL);
135 pv->profil_info.highpc = highpc;
136 pv->profil_info.lowpc = lowpc;
137 pv->profil_info.text_len = textsize;
138 pv->profil_info.profil_len = monsize;
139 pv->profil_info.counter_size = sizeof(LHISTCOUNTER);
140 pv->profil_info.scale = 0x10000 / HISTFRACTION;
141 pv->stats.profil_buckets = monsize / sizeof(LHISTCOUNTER);
142
143 /* Other gprof variables */
144 pv->stats.my_cpu = i;
145 pv->stats.max_cpu = NCPUS;
146 pv->init = 1;
147 pv->active = 1;
148 pv->use_dci = 0;
149 pv->use_profil = 1;
150 pv->check_funcs = 1; /* for now */
151
152 if (pv->debug) {
153 printf("Profiling kernel, s_textsize=%ld, monsize=%ld [0x%lx..0x%lx], cpu = %d\n",
154 (long)textsize,
155 (long)monsize,
156 (long)lowpc,
157 (long)highpc,
158 i);
159 }
160 }
161
162 _profile_md_start();
163 }
164
165 /* driver component */
166
167 int
168 gprofprobe(caddr_t port, void *ctlr)
169 {
170 return(1);
171 }
172
173 void
174 gprofattach(void)
175 {
176 kmstartup();
177 return;
178 }
179
180 /* struct bus_device *gprofinfo[NGPROF]; */
181 struct bus_device *gprofinfo[1];
182
183 struct bus_driver gprof_driver = {
184 gprofprobe, 0, gprofattach, 0, 0, "gprof", gprofinfo, "gprofc", 0, 0};
185
186
187 io_return_t
188 gprofopen(dev_t dev,
189 int flags,
190 io_req_t ior)
191 {
192 ior->io_error = D_SUCCESS;
193 return(0);
194 }
195
196 void
197 gprofclose(dev_t dev)
198 {
199 return;
200 }
201
202 void
203 gprofstrategy(io_req_t ior)
204 {
205 void *sys_ptr = (void *)0;
206
207 long count = _profile_kgmon(!(ior->io_op & IO_READ),
208 ior->io_count,
209 ior->io_recnum,
210 NCPUS,
211 &sys_ptr,
212 (void (*)(kgmon_control_t))0);
213
214 if (count < 0) {
215 ior->io_error = D_INVALID_RECNUM;
216
217 } else {
218 if (count > 0 && sys_ptr != (void *)0) {
219 if (ior->io_op & IO_READ) {
220 memcpy((void *)ior->io_data, sys_ptr, count);
221 } else {
222 memcpy(sys_ptr, (void *)ior->io_data, count);
223 }
224 }
225
226 ior->io_error = D_SUCCESS;
227 ior->io_residual = ior->io_count - count;
228 }
229
230 iodone(ior);
231 }
232
233 io_return_t
234 gprofread(dev_t dev,
235 io_req_t ior)
236 {
237 return(block_io(gprofstrategy, minphys, ior));
238 }
239
240 io_return_t
241 gprofwrite(dev_t dev,
242 io_req_t ior)
243 {
244 return (block_io(gprofstrategy, minphys, ior));
245 }