2 * Copyright (c) 2003 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@
24 * Here's what to do if you want to add a new routine to the comm page:
26 * 1. Add a definition for it's address in osfmk/ppc/cpu_capabilities.h,
27 * being careful to reserve room for future expansion.
29 * 2. Write one or more versions of the routine, each with it's own
30 * commpage_descriptor. The tricky part is getting the "special",
31 * "musthave", and "canthave" fields right, so that exactly one
32 * version of the routine is selected for every machine.
33 * The source files should be in osfmk/ppc/commpage/.
35 * 3. Add a ptr to your new commpage_descriptor(s) in the "routines"
36 * array in commpage_populate(). Of course, you'll also have to
37 * declare them "extern" in commpage_populate().
39 * 4. Write the code in Libc to use the new routine.
42 #include <mach/mach_types.h>
43 #include <mach/machine.h>
44 #include <i386/machine_routines.h>
45 #include <machine/cpu_capabilities.h>
46 #include <machine/commpage.h>
47 #include <machine/pmap.h>
48 #include <vm/vm_kern.h>
49 #include <mach/vm_map.h>
51 static uintptr_t next
= 0; // next available byte in comm page
52 static int cur_routine
= 0; // comm page address of "current" routine
53 static int matched
; // true if we've found a match for "current" routine
55 int _cpu_capabilities
= 0; // define the capability vector
57 char *commPagePtr
= NULL
; // virtual address of comm page in kernel map
59 /* Allocate the commpage and add to the shared submap created by vm:
60 * 1. allocate a page in the kernel map (RW)
62 * 3. make a memory entry out of it
63 * 4. map that entry into the shared comm region map (R-only)
67 commpage_allocate( void )
69 extern vm_map_t com_region_map
; // the shared submap, set up in vm init
70 vm_offset_t kernel_addr
; // address of commpage in kernel map
72 vm_size_t size
= _COMM_PAGE_AREA_LENGTH
;
75 if (com_region_map
== NULL
)
76 panic("commpage map is null");
78 if (vm_allocate(kernel_map
,&kernel_addr
,_COMM_PAGE_AREA_LENGTH
,VM_FLAGS_ANYWHERE
))
79 panic("cannot allocate commpage");
81 if (vm_map_wire(kernel_map
,kernel_addr
,kernel_addr
+_COMM_PAGE_AREA_LENGTH
,VM_PROT_DEFAULT
,FALSE
))
82 panic("cannot wire commpage");
84 if (mach_make_memory_entry( kernel_map
, // target map
86 kernel_addr
, // offset (address in kernel map)
87 VM_PROT_DEFAULT
, // map it RW
88 &handle
, // this is the object handle we get
89 NULL
)) // parent_entry (what is this?)
90 panic("cannot make entry for commpage");
92 if (vm_map_64( com_region_map
, // target map (shared submap)
93 &zero
, // address (map into 1st page in submap)
94 _COMM_PAGE_AREA_LENGTH
, // size
96 VM_FLAGS_FIXED
, // flags (it must be 1st page in submap)
97 handle
, // port is the memory entry we just made
98 0, // offset (map 1st page in memory entry)
100 VM_PROT_READ
, // cur_protection (R-only in user map)
101 VM_PROT_READ
, // max_protection
102 VM_INHERIT_SHARE
)) // inheritance
103 panic("cannot map commpage");
105 ipc_port_release(handle
);
107 return (void*) kernel_addr
; // return address in kernel map
110 /* Get address (in kernel map) of a commpage field. */
114 int addr_at_runtime
)
116 return (void*) ((uintptr_t)commPagePtr
+ addr_at_runtime
- _COMM_PAGE_BASE_ADDRESS
);
119 /* Determine number of CPUs on this system. We cannot rely on
120 * machine_info.max_cpus this early in the boot.
123 commpage_cpus( void )
127 cpus
= ml_get_max_cpus(); // NB: this call can block
130 panic("commpage cpus==0");
137 /* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
140 commpage_init_cpu_capabilities( void )
144 ml_cpu_info_t cpu_info
;
147 ml_cpu_get_info(&cpu_info
);
149 switch (cpu_info
.vector_unit
) {
164 switch (cpu_info
.cache_line_size
) {
177 cpus
= commpage_cpus(); // how many CPUs do we have
182 bits
|= (cpus
<< kNumCPUsShift
);
184 _cpu_capabilities
= bits
; // set kernel version for use by drivers etc
187 /* Copy data into commpage. */
195 void *dest
= commpage_addr_of(address
);
197 if ((uintptr_t)dest
< next
)
198 panic("commpage overlap");
200 bcopy(source
,dest
,length
);
202 next
= ((uintptr_t)dest
+ length
);
205 /* Copy a routine into comm page if it matches running machine.
208 commpage_stuff_routine(
209 commpage_descriptor
*rd
)
213 if (rd
->commpage_address
!= cur_routine
) {
214 if ((cur_routine
!=0) && (matched
==0))
215 panic("commpage no match");
216 cur_routine
= rd
->commpage_address
;
220 must
= _cpu_capabilities
& rd
->musthave
;
221 cant
= _cpu_capabilities
& rd
->canthave
;
223 if ((must
== rd
->musthave
) && (cant
== 0)) {
225 panic("commpage duplicate matches");
228 commpage_stuff(rd
->commpage_address
,rd
->code_address
,rd
->code_length
);
232 /* Fill in commpage: called once, during kernel initialization, from the
233 * startup thread before user-mode code is running.
234 * See the top of this file for a list of what you have to do to add
235 * a new routine to the commpage.
239 commpage_populate( void )
241 commpage_descriptor
**rd
;
242 short version
= _COMM_PAGE_THIS_VERSION
;
245 extern char commpage_sigs_begin
[];
246 extern char commpage_sigs_end
[];
248 extern commpage_descriptor commpage_mach_absolute_time
;
249 extern commpage_descriptor commpage_spin_lock_try_mp
;
250 extern commpage_descriptor commpage_spin_lock_try_up
;
251 extern commpage_descriptor commpage_spin_lock_mp
;
252 extern commpage_descriptor commpage_spin_lock_up
;
253 extern commpage_descriptor commpage_spin_unlock
;
254 extern commpage_descriptor commpage_pthread_getspecific
;
255 extern commpage_descriptor commpage_gettimeofday
;
256 extern commpage_descriptor commpage_sys_flush_dcache
;
257 extern commpage_descriptor commpage_sys_icache_invalidate
;
258 extern commpage_descriptor commpage_pthread_self
;
259 extern commpage_descriptor commpage_relinquish
;
260 extern commpage_descriptor commpage_bzero_scalar
;
261 extern commpage_descriptor commpage_bcopy_scalar
;
263 static commpage_descriptor
*routines
[] = {
264 &commpage_mach_absolute_time
,
265 &commpage_spin_lock_try_mp
,
266 &commpage_spin_lock_try_up
,
267 &commpage_spin_lock_mp
,
268 &commpage_spin_lock_up
,
269 &commpage_spin_unlock
,
270 &commpage_pthread_getspecific
,
271 &commpage_gettimeofday
,
272 &commpage_sys_flush_dcache
,
273 &commpage_sys_icache_invalidate
,
274 &commpage_pthread_self
,
275 &commpage_relinquish
,
276 &commpage_bzero_scalar
,
277 &commpage_bcopy_scalar
,
281 commPagePtr
= (char *)commpage_allocate();
283 commpage_init_cpu_capabilities();
285 /* Stuff in the constants. We move things into the comm page in strictly
286 * ascending order, so we can check for overlap and panic if so.
289 commpage_stuff(_COMM_PAGE_VERSION
,&version
,sizeof(short));
290 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES
,&_cpu_capabilities
,
293 for( rd
= routines
; *rd
!= NULL
; rd
++ )
294 commpage_stuff_routine(*rd
);
297 panic("commpage no match on last routine");
299 if (next
> ((uintptr_t)commPagePtr
+ PAGE_SIZE
))
300 panic("commpage overflow");
302 #define STUFF_SIG(addr, func) \
303 extern char commpage_sig_ ## func []; \
304 sig_addr = (void *)( (uintptr_t)_COMM_PAGE_BASE_ADDRESS + \
305 (uintptr_t)_COMM_PAGE_SIGS_OFFSET + 0x1000 + \
306 (uintptr_t)&commpage_sig_ ## func - \
307 (uintptr_t)&commpage_sigs_begin ); \
308 commpage_stuff(addr + _COMM_PAGE_SIGS_OFFSET, &sig_addr, sizeof(void *));
310 STUFF_SIG(_COMM_PAGE_ABSOLUTE_TIME
, mach_absolute_time
);
311 STUFF_SIG(_COMM_PAGE_SPINLOCK_TRY
, spin_lock_try
);
312 STUFF_SIG(_COMM_PAGE_SPINLOCK_LOCK
, spin_lock
);
313 STUFF_SIG(_COMM_PAGE_SPINLOCK_UNLOCK
, spin_unlock
);
314 STUFF_SIG(_COMM_PAGE_PTHREAD_GETSPECIFIC
, pthread_getspecific
);
315 STUFF_SIG(_COMM_PAGE_GETTIMEOFDAY
, gettimeofday
);
316 STUFF_SIG(_COMM_PAGE_FLUSH_DCACHE
, sys_dcache_flush
);
317 STUFF_SIG(_COMM_PAGE_FLUSH_ICACHE
, sys_icache_invalidate
);
318 STUFF_SIG(_COMM_PAGE_PTHREAD_SELF
, pthread_self
);
319 STUFF_SIG(_COMM_PAGE_BZERO
, bzero
);
320 STUFF_SIG(_COMM_PAGE_BCOPY
, bcopy
);
321 STUFF_SIG(_COMM_PAGE_MEMCPY
, memmove
);
323 commpage_stuff(_COMM_PAGE_BASE_ADDRESS
+ _COMM_PAGE_SIGS_OFFSET
+ 0x1000, &commpage_sigs_begin
,
324 (uintptr_t)&commpage_sigs_end
- (uintptr_t)&commpage_sigs_begin
);