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