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 <mach/vm_map.h>
45 #include <i386/machine_routines.h>
46 #include <machine/cpu_capabilities.h>
47 #include <machine/commpage.h>
48 #include <machine/pmap.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_map.h>
51 #include <ipc/ipc_port.h>
54 extern vm_map_t com_region_map32
; // the shared submap, set up in vm init
56 static uintptr_t next
= 0; // next available byte in comm page
57 static int cur_routine
= 0; // comm page address of "current" routine
58 static int matched
; // true if we've found a match for "current" routine
60 int _cpu_capabilities
= 0; // define the capability vector
62 char *commPagePtr
= NULL
; // virtual address of comm page in kernel map
64 /* Allocate the commpage and add to the shared submap created by vm:
65 * 1. allocate a page in the kernel map (RW)
67 * 3. make a memory entry out of it
68 * 4. map that entry into the shared comm region map (R-only)
72 commpage_allocate( void )
74 vm_offset_t kernel_addr
; // address of commpage in kernel map
76 vm_size_t size
= _COMM_PAGE_AREA_LENGTH
;
80 if (com_region_map32
== NULL
)
81 panic("commpage map is null");
83 if (vm_allocate(kernel_map
,&kernel_addr
,_COMM_PAGE_AREA_LENGTH
,VM_FLAGS_ANYWHERE
))
84 panic("cannot allocate commpage");
86 if (vm_map_wire(kernel_map
,kernel_addr
,kernel_addr
+_COMM_PAGE_AREA_LENGTH
,VM_PROT_DEFAULT
,FALSE
))
87 panic("cannot wire commpage");
90 * Now that the object is created and wired into the kernel map, mark it so that no delay
91 * copy-on-write will ever be performed on it as a result of mapping it into user-space.
92 * If such a delayed copy ever occurred, we could remove the kernel's wired mapping - and
93 * that would be a real disaster.
95 * JMM - What we really need is a way to create it like this in the first place.
97 if (!vm_map_lookup_entry( kernel_map
, vm_map_trunc_page(kernel_addr
), &entry
) || entry
->is_sub_map
)
98 panic("cannot find commpage entry");
99 entry
->object
.vm_object
->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
101 if (mach_make_memory_entry( kernel_map
, // target map
103 kernel_addr
, // offset (address in kernel map)
104 VM_PROT_DEFAULT
, // map it RW
105 &handle
, // this is the object handle we get
106 NULL
)) // parent_entry (what is this?)
107 panic("cannot make entry for commpage");
109 if (vm_map_64( com_region_map32
, // target map (shared submap)
110 &zero
, // address (map into 1st page in submap)
111 _COMM_PAGE_AREA_LENGTH
, // size
113 VM_FLAGS_FIXED
, // flags (it must be 1st page in submap)
114 handle
, // port is the memory entry we just made
115 0, // offset (map 1st page in memory entry)
117 VM_PROT_READ
, // cur_protection (R-only in user map)
118 VM_PROT_READ
, // max_protection
119 VM_INHERIT_SHARE
)) // inheritance
120 panic("cannot map commpage");
122 ipc_port_release(handle
);
124 return (void*) kernel_addr
; // return address in kernel map
127 /* Get address (in kernel map) of a commpage field. */
131 int addr_at_runtime
)
133 return (void*) ((uintptr_t)commPagePtr
+ addr_at_runtime
- _COMM_PAGE_BASE_ADDRESS
);
136 /* Determine number of CPUs on this system. We cannot rely on
137 * machine_info.max_cpus this early in the boot.
140 commpage_cpus( void )
144 cpus
= ml_get_max_cpus(); // NB: this call can block
147 panic("commpage cpus==0");
154 /* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
157 commpage_init_cpu_capabilities( void )
161 ml_cpu_info_t cpu_info
;
164 ml_cpu_get_info(&cpu_info
);
166 switch (cpu_info
.vector_unit
) {
181 switch (cpu_info
.cache_line_size
) {
194 cpus
= commpage_cpus(); // how many CPUs do we have
199 bits
|= (cpus
<< kNumCPUsShift
);
201 bits
|= kFastThreadLocalStorage
; // we use %gs for TLS
203 _cpu_capabilities
= bits
; // set kernel version for use by drivers etc
206 /* Copy data into commpage. */
214 void *dest
= commpage_addr_of(address
);
216 if ((uintptr_t)dest
< next
)
217 panic("commpage overlap at address 0x%x, 0x%x < 0x%x", address
, dest
, next
);
219 bcopy(source
,dest
,length
);
221 next
= ((uintptr_t)dest
+ length
);
231 commpage_stuff(address
, source
, length
);
234 /* Copy a routine into comm page if it matches running machine.
237 commpage_stuff_routine(
238 commpage_descriptor
*rd
)
242 if (rd
->commpage_address
!= cur_routine
) {
243 if ((cur_routine
!=0) && (matched
==0))
244 panic("commpage no match");
245 cur_routine
= rd
->commpage_address
;
249 must
= _cpu_capabilities
& rd
->musthave
;
250 cant
= _cpu_capabilities
& rd
->canthave
;
252 if ((must
== rd
->musthave
) && (cant
== 0)) {
254 panic("commpage duplicate matches");
257 commpage_stuff(rd
->commpage_address
,rd
->code_address
,rd
->code_length
);
262 #define COMMPAGE_DESC(name) commpage_ ## name
263 #define EXTERN_COMMPAGE_DESC(name) \
264 extern commpage_descriptor COMMPAGE_DESC(name)
266 EXTERN_COMMPAGE_DESC(compare_and_swap32_mp
);
267 EXTERN_COMMPAGE_DESC(compare_and_swap32_up
);
268 EXTERN_COMMPAGE_DESC(compare_and_swap64_mp
);
269 EXTERN_COMMPAGE_DESC(compare_and_swap64_up
);
270 EXTERN_COMMPAGE_DESC(atomic_add32_mp
);
271 EXTERN_COMMPAGE_DESC(atomic_add32_up
);
272 EXTERN_COMMPAGE_DESC(mach_absolute_time
);
273 EXTERN_COMMPAGE_DESC(spin_lock_try_mp
);
274 EXTERN_COMMPAGE_DESC(spin_lock_try_up
);
275 EXTERN_COMMPAGE_DESC(spin_lock_mp
);
276 EXTERN_COMMPAGE_DESC(spin_lock_up
);
277 EXTERN_COMMPAGE_DESC(spin_unlock
);
278 EXTERN_COMMPAGE_DESC(pthread_getspecific
);
279 EXTERN_COMMPAGE_DESC(gettimeofday
);
280 EXTERN_COMMPAGE_DESC(sys_flush_dcache
);
281 EXTERN_COMMPAGE_DESC(sys_icache_invalidate
);
282 EXTERN_COMMPAGE_DESC(pthread_self
);
283 EXTERN_COMMPAGE_DESC(relinquish
);
284 EXTERN_COMMPAGE_DESC(bit_test_and_set_mp
);
285 EXTERN_COMMPAGE_DESC(bit_test_and_set_up
);
286 EXTERN_COMMPAGE_DESC(bit_test_and_clear_mp
);
287 EXTERN_COMMPAGE_DESC(bit_test_and_clear_up
);
288 EXTERN_COMMPAGE_DESC(bzero_scalar
);
289 EXTERN_COMMPAGE_DESC(bcopy_scalar
);
290 EXTERN_COMMPAGE_DESC(nanotime
);
292 static commpage_descriptor
*routines
[] = {
293 &COMMPAGE_DESC(compare_and_swap32_mp
),
294 &COMMPAGE_DESC(compare_and_swap32_up
),
295 &COMMPAGE_DESC(compare_and_swap64_mp
),
296 &COMMPAGE_DESC(compare_and_swap64_up
),
297 &COMMPAGE_DESC(atomic_add32_mp
),
298 &COMMPAGE_DESC(atomic_add32_up
),
299 &COMMPAGE_DESC(mach_absolute_time
),
300 &COMMPAGE_DESC(spin_lock_try_mp
),
301 &COMMPAGE_DESC(spin_lock_try_up
),
302 &COMMPAGE_DESC(spin_lock_mp
),
303 &COMMPAGE_DESC(spin_lock_up
),
304 &COMMPAGE_DESC(spin_unlock
),
305 &COMMPAGE_DESC(pthread_getspecific
),
306 &COMMPAGE_DESC(gettimeofday
),
307 &COMMPAGE_DESC(sys_flush_dcache
),
308 &COMMPAGE_DESC(sys_icache_invalidate
),
309 &COMMPAGE_DESC(pthread_self
),
310 &COMMPAGE_DESC(relinquish
),
311 &COMMPAGE_DESC(bit_test_and_set_mp
),
312 &COMMPAGE_DESC(bit_test_and_set_up
),
313 &COMMPAGE_DESC(bit_test_and_clear_mp
),
314 &COMMPAGE_DESC(bit_test_and_clear_up
),
315 &COMMPAGE_DESC(bzero_scalar
),
316 &COMMPAGE_DESC(bcopy_scalar
),
317 &COMMPAGE_DESC(nanotime
),
322 /* Fill in commpage: called once, during kernel initialization, from the
323 * startup thread before user-mode code is running.
324 * See the top of this file for a list of what you have to do to add
325 * a new routine to the commpage.
329 commpage_populate( void )
332 static double two52
= 1048576.0 * 1048576.0 * 4096.0; // 2**52
333 static double ten6
= 1000000.0; // 10**6
334 commpage_descriptor
**rd
;
335 short version
= _COMM_PAGE_THIS_VERSION
;
337 commPagePtr
= (char *)commpage_allocate();
339 commpage_init_cpu_capabilities();
341 /* Stuff in the constants. We move things into the comm page in strictly
342 * ascending order, so we can check for overlap and panic if so.
345 commpage_stuff2(_COMM_PAGE_VERSION
,&version
,sizeof(short));
346 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES
,&_cpu_capabilities
,
349 if (_cpu_capabilities
& kCache32
)
351 else if (_cpu_capabilities
& kCache64
)
353 else if (_cpu_capabilities
& kCache128
)
355 commpage_stuff(_COMM_PAGE_CACHE_LINESIZE
,&c2
,2);
359 commpage_stuff2(_COMM_PAGE_2_TO_52
,&two52
,8);
361 commpage_stuff2(_COMM_PAGE_10_TO_6
,&ten6
,8);
363 for( rd
= routines
; *rd
!= NULL
; rd
++ )
364 commpage_stuff_routine(*rd
);
367 panic("commpage no match on last routine");
369 if (next
> (uintptr_t)_COMM_PAGE_END
)
370 panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%08x", next
, (uintptr_t)commPagePtr
);
373 pmap_commpage_init((vm_offset_t
) commPagePtr
, _COMM_PAGE_BASE_ADDRESS
,
374 _COMM_PAGE_AREA_LENGTH
/INTEL_PGBYTES
);
378 * This macro prevents compiler instruction scheduling:
380 #define NO_REORDERING asm volatile("" : : : "memory")
383 commpage_set_nanotime(commpage_nanotime_t
*newp
)
385 commpage_nanotime_t
*cnp
;
387 /* Nop if commpage not set up yet */
388 if (commPagePtr
== NULL
)
391 cnp
= (commpage_nanotime_t
*)commpage_addr_of(_COMM_PAGE_NANOTIME_INFO
);
394 * Update in reverse order:
395 * check_tsc first - it's read and compared with base_tsc last.
397 cnp
->nt_check_tsc
= newp
->nt_base_tsc
; NO_REORDERING
;
398 cnp
->nt_shift
= newp
->nt_shift
; NO_REORDERING
;
399 cnp
->nt_scale
= newp
->nt_scale
; NO_REORDERING
;
400 cnp
->nt_base_ns
= newp
->nt_base_ns
; NO_REORDERING
;
401 cnp
->nt_base_tsc
= newp
->nt_base_tsc
;