2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Microkernel interface to common profiling.
29 #include <profiling/profile-mk.h>
31 #include <kern/cpu_number.h>
32 #include <kern/processor.h>
34 #include <kern/misc_protos.h>
35 #include <vm/vm_kern.h>
36 #include <mach/vm_param.h>
38 #include <device/ds_routines.h>
39 #include <device/io_req.h>
40 #include <device/buf.h>
42 extern char etext
[], pstart
[];
45 struct profile_vars
*_profile_vars_cpus
[NCPUS
] = { &_profile_vars
};
46 struct profile_vars _profile_vars_aux
[NCPUS
-1];
50 _profile_alloc_pages (size_t size
)
55 * For the MK, we can't support allocating pages at runtime, because we
56 * might be at interrupt level, so abort if we didn't size the table
60 if (PROFILE_VARS(0)->active
) {
61 panic("Call to _profile_alloc_pages while profiling is running.");
64 if (kmem_alloc(kernel_map
, &addr
, size
)) {
65 panic("Could not allocate memory for profiling");
68 memset((void *)addr
, '\0', size
);
69 if (PROFILE_VARS(0)->debug
) {
70 printf("Allocated %d bytes for profiling, address 0x%x\n", (int)size
, (int)addr
);
73 return((caddr_t
)addr
);
77 _profile_free_pages(void *addr
, size_t size
)
79 if (PROFILE_VARS(0)->debug
) {
80 printf("Freed %d bytes for profiling, address 0x%x\n", (int)size
, (int)addr
);
83 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
87 void _profile_error(struct profile_vars
*pv
)
89 panic("Fatal error in profiling");
95 prof_uptrint_t textsize
;
96 prof_uptrint_t monsize
;
98 prof_uptrint_t highpc
;
100 struct profile_vars
*pv
;
103 * round lowpc and highpc to multiples of the density we're using
104 * so the rest of the scaling (here and in gprof) stays in ints.
107 lowpc
= ROUNDDOWN((prof_uptrint_t
)&pstart
[0], HISTFRACTION
*sizeof(LHISTCOUNTER
));
108 highpc
= ROUNDUP((prof_uptrint_t
)&etext
[0], HISTFRACTION
*sizeof(LHISTCOUNTER
));
109 textsize
= highpc
- lowpc
;
110 monsize
= (textsize
/ HISTFRACTION
) * sizeof(LHISTCOUNTER
);
112 for (i
= 0; i
< NCPUS
; i
++) {
113 pv
= PROFILE_VARS(i
);
117 _profile_vars_cpus
[i
] = pv
= &_profile_vars_aux
[i
-i
];
124 pv
->page_size
= PAGE_SIZE
;
125 _profile_md_init(pv
, PROFILE_GPROF
, PROFILE_ALLOC_MEM_YES
);
127 /* Profil related variables */
128 pv
->profil_buf
= _profile_alloc (pv
, monsize
, ACONTEXT_PROFIL
);
129 pv
->profil_info
.highpc
= highpc
;
130 pv
->profil_info
.lowpc
= lowpc
;
131 pv
->profil_info
.text_len
= textsize
;
132 pv
->profil_info
.profil_len
= monsize
;
133 pv
->profil_info
.counter_size
= sizeof(LHISTCOUNTER
);
134 pv
->profil_info
.scale
= 0x10000 / HISTFRACTION
;
135 pv
->stats
.profil_buckets
= monsize
/ sizeof(LHISTCOUNTER
);
137 /* Other gprof variables */
138 pv
->stats
.my_cpu
= i
;
139 pv
->stats
.max_cpu
= NCPUS
;
144 pv
->check_funcs
= 1; /* for now */
147 printf("Profiling kernel, s_textsize=%ld, monsize=%ld [0x%lx..0x%lx], cpu = %d\n",
159 /* driver component */
162 gprofprobe(caddr_t port
, void *ctlr
)
174 /* struct bus_device *gprofinfo[NGPROF]; */
175 struct bus_device
*gprofinfo
[1];
177 struct bus_driver gprof_driver
= {
178 gprofprobe
, 0, gprofattach
, 0, 0, "gprof", gprofinfo
, "gprofc", 0, 0};
186 ior
->io_error
= D_SUCCESS
;
191 gprofclose(dev_t dev
)
197 gprofstrategy(io_req_t ior
)
199 void *sys_ptr
= (void *)0;
201 long count
= _profile_kgmon(!(ior
->io_op
& IO_READ
),
206 (void (*)(kgmon_control_t
))0);
209 ior
->io_error
= D_INVALID_RECNUM
;
212 if (count
> 0 && sys_ptr
!= (void *)0) {
213 if (ior
->io_op
& IO_READ
) {
214 memcpy((void *)ior
->io_data
, sys_ptr
, count
);
216 memcpy(sys_ptr
, (void *)ior
->io_data
, count
);
220 ior
->io_error
= D_SUCCESS
;
221 ior
->io_residual
= ior
->io_count
- count
;
231 return(block_io(gprofstrategy
, minphys
, ior
));
235 gprofwrite(dev_t dev
,
238 return (block_io(gprofstrategy
, minphys
, ior
));