2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Microkernel interface to common profiling.
32 #include <profiling/profile-mk.h>
34 #include <kern/cpu_number.h>
35 #include <kern/processor.h>
37 #include <kern/misc_protos.h>
38 #include <vm/vm_kern.h>
39 #include <mach/vm_param.h>
41 #include <device/ds_routines.h>
42 #include <device/io_req.h>
43 #include <device/buf.h>
45 extern char etext
[], pstart
[];
48 struct profile_vars
*_profile_vars_cpus
[NCPUS
] = { &_profile_vars
};
49 struct profile_vars _profile_vars_aux
[NCPUS
-1];
53 _profile_alloc_pages (size_t size
)
58 * For the MK, we can't support allocating pages at runtime, because we
59 * might be at interrupt level, so abort if we didn't size the table
63 if (PROFILE_VARS(0)->active
) {
64 panic("Call to _profile_alloc_pages while profiling is running.");
67 if (kmem_alloc(kernel_map
, &addr
, size
)) {
68 panic("Could not allocate memory for profiling");
71 memset((void *)addr
, '\0', size
);
72 if (PROFILE_VARS(0)->debug
) {
73 printf("Allocated %d bytes for profiling, address 0x%x\n", (int)size
, (int)addr
);
76 return((caddr_t
)addr
);
80 _profile_free_pages(void *addr
, size_t size
)
82 if (PROFILE_VARS(0)->debug
) {
83 printf("Freed %d bytes for profiling, address 0x%x\n", (int)size
, (int)addr
);
86 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
90 void _profile_error(struct profile_vars
*pv
)
92 panic("Fatal error in profiling");
98 prof_uptrint_t textsize
;
99 prof_uptrint_t monsize
;
100 prof_uptrint_t lowpc
;
101 prof_uptrint_t highpc
;
103 struct profile_vars
*pv
;
106 * round lowpc and highpc to multiples of the density we're using
107 * so the rest of the scaling (here and in gprof) stays in ints.
110 lowpc
= ROUNDDOWN((prof_uptrint_t
)&pstart
[0], HISTFRACTION
*sizeof(LHISTCOUNTER
));
111 highpc
= ROUNDUP((prof_uptrint_t
)&etext
[0], HISTFRACTION
*sizeof(LHISTCOUNTER
));
112 textsize
= highpc
- lowpc
;
113 monsize
= (textsize
/ HISTFRACTION
) * sizeof(LHISTCOUNTER
);
115 for (i
= 0; i
< NCPUS
; i
++) {
116 pv
= PROFILE_VARS(i
);
120 _profile_vars_cpus
[i
] = pv
= &_profile_vars_aux
[i
-i
];
127 pv
->page_size
= PAGE_SIZE
;
128 _profile_md_init(pv
, PROFILE_GPROF
, PROFILE_ALLOC_MEM_YES
);
130 /* Profil related variables */
131 pv
->profil_buf
= _profile_alloc (pv
, monsize
, ACONTEXT_PROFIL
);
132 pv
->profil_info
.highpc
= highpc
;
133 pv
->profil_info
.lowpc
= lowpc
;
134 pv
->profil_info
.text_len
= textsize
;
135 pv
->profil_info
.profil_len
= monsize
;
136 pv
->profil_info
.counter_size
= sizeof(LHISTCOUNTER
);
137 pv
->profil_info
.scale
= 0x10000 / HISTFRACTION
;
138 pv
->stats
.profil_buckets
= monsize
/ sizeof(LHISTCOUNTER
);
140 /* Other gprof variables */
141 pv
->stats
.my_cpu
= i
;
142 pv
->stats
.max_cpu
= NCPUS
;
147 pv
->check_funcs
= 1; /* for now */
150 printf("Profiling kernel, s_textsize=%ld, monsize=%ld [0x%lx..0x%lx], cpu = %d\n",
162 /* driver component */
165 gprofprobe(caddr_t port
, void *ctlr
)
177 /* struct bus_device *gprofinfo[NGPROF]; */
178 struct bus_device
*gprofinfo
[1];
180 struct bus_driver gprof_driver
= {
181 gprofprobe
, 0, gprofattach
, 0, 0, "gprof", gprofinfo
, "gprofc", 0, 0};
189 ior
->io_error
= D_SUCCESS
;
194 gprofclose(dev_t dev
)
200 gprofstrategy(io_req_t ior
)
202 void *sys_ptr
= (void *)0;
204 long count
= _profile_kgmon(!(ior
->io_op
& IO_READ
),
209 (void (*)(kgmon_control_t
))0);
212 ior
->io_error
= D_INVALID_RECNUM
;
215 if (count
> 0 && sys_ptr
!= (void *)0) {
216 if (ior
->io_op
& IO_READ
) {
217 memcpy((void *)ior
->io_data
, sys_ptr
, count
);
219 memcpy(sys_ptr
, (void *)ior
->io_data
, count
);
223 ior
->io_error
= D_SUCCESS
;
224 ior
->io_residual
= ior
->io_count
- count
;
234 return(block_io(gprofstrategy
, minphys
, ior
));
238 gprofwrite(dev_t dev
,
241 return (block_io(gprofstrategy
, minphys
, ior
));