2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Microkernel interface to common profiling.
37 #include <profiling/profile-mk.h>
39 #include <kern/cpu_number.h>
40 #include <kern/processor.h>
42 #include <kern/misc_protos.h>
43 #include <vm/vm_kern.h>
44 #include <mach/vm_param.h>
46 #include <device/ds_routines.h>
47 #include <device/io_req.h>
48 #include <device/buf.h>
50 extern char etext
[], pstart
[];
53 struct profile_vars
*_profile_vars_cpus
[NCPUS
] = { &_profile_vars
};
54 struct profile_vars _profile_vars_aux
[NCPUS
-1];
58 _profile_alloc_pages (size_t size
)
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
68 if (PROFILE_VARS(0)->active
) {
69 panic("Call to _profile_alloc_pages while profiling is running.");
72 if (kmem_alloc(kernel_map
, &addr
, size
)) {
73 panic("Could not allocate memory for profiling");
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
);
81 return((caddr_t
)addr
);
85 _profile_free_pages(void *addr
, size_t size
)
87 if (PROFILE_VARS(0)->debug
) {
88 printf("Freed %d bytes for profiling, address 0x%x\n", (int)size
, (int)addr
);
91 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
95 void _profile_error(struct profile_vars
*pv
)
97 panic("Fatal error in profiling");
103 prof_uptrint_t textsize
;
104 prof_uptrint_t monsize
;
105 prof_uptrint_t lowpc
;
106 prof_uptrint_t highpc
;
108 struct profile_vars
*pv
;
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.
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
);
120 for (i
= 0; i
< NCPUS
; i
++) {
121 pv
= PROFILE_VARS(i
);
125 _profile_vars_cpus
[i
] = pv
= &_profile_vars_aux
[i
-i
];
132 pv
->page_size
= PAGE_SIZE
;
133 _profile_md_init(pv
, PROFILE_GPROF
, PROFILE_ALLOC_MEM_YES
);
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
);
145 /* Other gprof variables */
146 pv
->stats
.my_cpu
= i
;
147 pv
->stats
.max_cpu
= NCPUS
;
152 pv
->check_funcs
= 1; /* for now */
155 printf("Profiling kernel, s_textsize=%ld, monsize=%ld [0x%lx..0x%lx], cpu = %d\n",
167 /* driver component */
170 gprofprobe(caddr_t port
, void *ctlr
)
182 /* struct bus_device *gprofinfo[NGPROF]; */
183 struct bus_device
*gprofinfo
[1];
185 struct bus_driver gprof_driver
= {
186 gprofprobe
, 0, gprofattach
, 0, 0, "gprof", gprofinfo
, "gprofc", 0, 0};
194 ior
->io_error
= D_SUCCESS
;
199 gprofclose(dev_t dev
)
205 gprofstrategy(io_req_t ior
)
207 void *sys_ptr
= (void *)0;
209 long count
= _profile_kgmon(!(ior
->io_op
& IO_READ
),
214 (void (*)(kgmon_control_t
))0);
217 ior
->io_error
= D_INVALID_RECNUM
;
220 if (count
> 0 && sys_ptr
!= (void *)0) {
221 if (ior
->io_op
& IO_READ
) {
222 memcpy((void *)ior
->io_data
, sys_ptr
, count
);
224 memcpy(sys_ptr
, (void *)ior
->io_data
, count
);
228 ior
->io_error
= D_SUCCESS
;
229 ior
->io_residual
= ior
->io_count
- count
;
239 return(block_io(gprofstrategy
, minphys
, ior
));
243 gprofwrite(dev_t dev
,
246 return (block_io(gprofstrategy
, minphys
, ior
));