2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 #include <mach_assert.h>
61 #include <mach/boolean.h>
62 #include <mach/i386/vm_types.h>
63 #include <mach/i386/vm_param.h>
64 #include <kern/kern_types.h>
65 #include <kern/misc_protos.h>
66 #include <sys/errno.h>
67 #include <i386/param.h>
68 #include <i386/misc_protos.h>
69 #include <i386/cpu_data.h>
70 #include <i386/machine_routines.h>
71 #include <i386/cpuid.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_fault.h>
78 #include <libkern/OSAtomic.h>
79 #include <sys/kdebug.h>
82 #include <kdp/kdp_callout.h>
83 #endif /* !MACH_KDP */
85 #include <libkern/OSDebug.h>
93 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
98 /* XXX - should be gone from here */
99 extern void invalidate_icache64(addr64_t addr
, unsigned cnt
, int phys
);
100 extern void flush_dcache64(addr64_t addr
, unsigned count
, int phys
);
101 extern boolean_t
phys_page_exists(ppnum_t
);
102 extern void bcopy_no_overwrite(const char *from
, char *to
,vm_size_t bytes
);
103 extern void pmap_set_reference(ppnum_t pn
);
104 extern void mapping_set_mod(ppnum_t pa
);
105 extern void mapping_set_ref(ppnum_t pn
);
107 extern void ovbcopy(const char *from
,
110 void machine_callstack(uintptr_t *buf
, vm_size_t callstack_max
);
113 #define value_64bit(value) ((value) & 0xFFFFFFFF00000000ULL)
114 #define low32(x) ((unsigned int)((x) & 0x00000000FFFFFFFFULL))
116 #define INT_SIZE (BYTE_SIZE * sizeof (int))
119 * Set indicated bit in bit string.
122 setbit(int bitno
, int *s
)
124 s
[bitno
/ INT_SIZE
] |= 1 << (bitno
% INT_SIZE
);
128 * Clear indicated bit in bit string.
131 clrbit(int bitno
, int *s
)
133 s
[bitno
/ INT_SIZE
] &= ~(1 << (bitno
% INT_SIZE
));
137 * Test if indicated bit is set in bit string.
140 testbit(int bitno
, int *s
)
142 return s
[bitno
/ INT_SIZE
] & (1 << (bitno
% INT_SIZE
));
146 * Find first bit set in bit string.
153 for (offset
= 0; !*s
; offset
+= (int)INT_SIZE
, ++s
);
154 return offset
+ __builtin_ctz(*s
);
158 ffs(unsigned int mask
)
164 * NOTE: cannot use __builtin_ffs because it generates a call to
167 return 1 + __builtin_ctz(mask
);
175 bzero_phys(src64
,bytes
);
183 bzero(PHYSMAP_PTOV(src64
), bytes
);
188 * bcopy_phys - like bcopy but copies from/to physical addresses.
197 /* Not necessary for K64 - but ensure we stay within a page */
198 if (((((uint32_t)src64
& (NBPG
-1)) + bytes
) > NBPG
) ||
199 ((((uint32_t)dst64
& (NBPG
-1)) + bytes
) > NBPG
) ) {
200 panic("bcopy_phys alignment");
202 bcopy(PHYSMAP_PTOV(src64
), PHYSMAP_PTOV(dst64
), bytes
);
206 * allow a function to get a quick virtual mapping of a physical page
213 int (*func
)(void * buffer
, vm_size_t bytes
, void * arg
),
216 /* Not necessary for K64 - but ensure we stay within a page */
217 if (((((uint32_t)dst64
& (NBPG
-1)) + bytes
) > NBPG
) ) {
218 panic("apply_func_phys alignment");
221 return func(PHYSMAP_PTOV(dst64
), bytes
, arg
);
225 * ovbcopy - like bcopy, but recognizes overlapping ranges and handles
233 vm_size_t bytes
) /* num bytes to copy */
235 /* Assume that bcopy copies left-to-right (low addr first). */
236 if (from
+ bytes
<= to
|| to
+ bytes
<= from
|| to
== from
)
237 bcopy_no_overwrite(from
, to
, bytes
); /* non-overlapping or no-op*/
239 bcopy_no_overwrite(from
, to
, bytes
); /* overlapping but OK */
241 /* to > from: overlapping, and must copy right-to-left. */
251 * Read data from a physical address. Memory should not be cache inhibited.
254 uint64_t reportphyreaddelayabs
;
255 uint32_t reportphyreadosbt
;
257 static inline unsigned int
258 ml_phys_read_data(pmap_paddr_t paddr
, int size
)
260 unsigned int result
= 0;
266 if (__improbable(!physmap_enclosed(paddr
)))
267 panic("%s: 0x%llx out of bounds\n", __FUNCTION__
, paddr
);
269 if (__improbable(reportphyreaddelayabs
!= 0)) {
270 istate
= ml_set_interrupts_enabled(FALSE
);
271 sabs
= mach_absolute_time();
276 s1
= *(volatile unsigned char *)PHYSMAP_PTOV(paddr
);
280 s2
= *(volatile unsigned short *)PHYSMAP_PTOV(paddr
);
284 result
= *(volatile unsigned int *)PHYSMAP_PTOV(paddr
);
287 panic("Invalid size %d for ml_phys_read_data\n", size
);
291 if (__improbable(reportphyreaddelayabs
!= 0)) {
292 eabs
= mach_absolute_time();
293 (void)ml_set_interrupts_enabled(istate
);
295 if ((eabs
- sabs
) > reportphyreaddelayabs
) {
296 if (reportphyreadosbt
) {
297 OSReportWithBacktrace("ml_phys_read_data took %lluus\n", (eabs
- sabs
) / 1000);
300 DTRACE_PHYSLAT3(physread
, uint64_t, (eabs
- sabs
),
301 pmap_paddr_t
, paddr
, uint32_t, size
);
309 static unsigned long long
310 ml_phys_read_long_long(pmap_paddr_t paddr
)
312 if (!physmap_enclosed(paddr
))
313 panic("%s: 0x%llx out of bounds\n", __FUNCTION__
, paddr
);
314 return *(volatile unsigned long long *)PHYSMAP_PTOV(paddr
);
317 unsigned int ml_phys_read( vm_offset_t paddr
)
319 return ml_phys_read_data((pmap_paddr_t
)paddr
, 4);
322 unsigned int ml_phys_read_word(vm_offset_t paddr
) {
324 return ml_phys_read_data((pmap_paddr_t
)paddr
, 4);
327 unsigned int ml_phys_read_64(addr64_t paddr64
)
329 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 4);
332 unsigned int ml_phys_read_word_64(addr64_t paddr64
)
334 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 4);
337 unsigned int ml_phys_read_half(vm_offset_t paddr
)
339 return ml_phys_read_data((pmap_paddr_t
)paddr
, 2);
342 unsigned int ml_phys_read_half_64(addr64_t paddr64
)
344 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 2);
347 unsigned int ml_phys_read_byte(vm_offset_t paddr
)
349 return ml_phys_read_data((pmap_paddr_t
)paddr
, 1);
352 unsigned int ml_phys_read_byte_64(addr64_t paddr64
)
354 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 1);
357 unsigned long long ml_phys_read_double(vm_offset_t paddr
)
359 return ml_phys_read_long_long((pmap_paddr_t
)paddr
);
362 unsigned long long ml_phys_read_double_64(addr64_t paddr64
)
364 return ml_phys_read_long_long((pmap_paddr_t
)paddr64
);
370 * Write data to a physical address. Memory should not be cache inhibited.
374 ml_phys_write_data(pmap_paddr_t paddr
, unsigned long data
, int size
)
376 if (!physmap_enclosed(paddr
))
377 panic("%s: 0x%llx out of bounds\n", __FUNCTION__
, paddr
);
381 *(volatile unsigned char *)PHYSMAP_PTOV(paddr
) = (unsigned char)data
;
384 *(volatile unsigned short *)PHYSMAP_PTOV(paddr
) = (unsigned short)data
;
387 *(volatile unsigned int *)PHYSMAP_PTOV(paddr
) = (unsigned int)data
;
390 panic("Invalid size %d for ml_phys_write_data\n", size
);
396 ml_phys_write_long_long(pmap_paddr_t paddr
, unsigned long long data
)
398 if (!physmap_enclosed(paddr
))
399 panic("%s: 0x%llx out of bounds\n", __FUNCTION__
, paddr
);
401 *(volatile unsigned long long *)PHYSMAP_PTOV(paddr
) = data
;
404 void ml_phys_write_byte(vm_offset_t paddr
, unsigned int data
)
406 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 1);
409 void ml_phys_write_byte_64(addr64_t paddr64
, unsigned int data
)
411 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 1);
414 void ml_phys_write_half(vm_offset_t paddr
, unsigned int data
)
416 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 2);
419 void ml_phys_write_half_64(addr64_t paddr64
, unsigned int data
)
421 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 2);
424 void ml_phys_write(vm_offset_t paddr
, unsigned int data
)
426 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 4);
429 void ml_phys_write_64(addr64_t paddr64
, unsigned int data
)
431 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 4);
434 void ml_phys_write_word(vm_offset_t paddr
, unsigned int data
)
436 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 4);
439 void ml_phys_write_word_64(addr64_t paddr64
, unsigned int data
)
441 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 4);
444 void ml_phys_write_double(vm_offset_t paddr
, unsigned long long data
)
446 ml_phys_write_long_long((pmap_paddr_t
)paddr
, data
);
449 void ml_phys_write_double_64(addr64_t paddr64
, unsigned long long data
)
451 ml_phys_write_long_long((pmap_paddr_t
)paddr64
, data
);
455 /* PCI config cycle probing
458 * Read the memory location at physical address paddr.
459 * *Does not* recover from machine checks, unlike the PowerPC implementation.
460 * Should probably be deprecated.
464 ml_probe_read(vm_offset_t paddr
, unsigned int *val
)
466 if ((PAGE_SIZE
- (paddr
& PAGE_MASK
)) < 4)
469 *val
= ml_phys_read((pmap_paddr_t
)paddr
);
475 * Read the memory location at physical address paddr.
476 * This is a part of a device probe, so there is a good chance we will
477 * have a machine check here. So we have to be able to handle that.
478 * We assume that machine checks are enabled both in MSR and HIDs
481 ml_probe_read_64(addr64_t paddr64
, unsigned int *val
)
483 if ((PAGE_SIZE
- (paddr64
& PAGE_MASK
)) < 4)
486 *val
= ml_phys_read_64((pmap_paddr_t
)paddr64
);
496 const char *a
= (const char *)pa
;
497 const char *b
= (const char *)pb
;
511 memcmp(const void *s1
, const void *s2
, size_t n
)
514 const unsigned char *p1
= s1
, *p2
= s2
;
518 return (*--p1
- *--p2
);
525 memmove(void *dst
, const void *src
, size_t ulen
)
527 bcopy(src
, dst
, ulen
);
533 * strlen returns the number of characters in "string" preceeding
534 * the terminating null character.
539 register const char *string
)
541 register const char *ret
= string
;
543 while (*string
++ != '\0')
545 return string
- 1 - ret
;
549 hw_compare_and_store(uint32_t oldval
, uint32_t newval
, volatile uint32_t *dest
)
551 return OSCompareAndSwap((UInt32
)oldval
,
553 (volatile UInt32
*)dest
);
559 * Machine-dependent routine to fill in an array with up to callstack_max
560 * levels of return pc information.
562 void machine_callstack(
563 __unused
uintptr_t *buf
,
564 __unused vm_size_t callstack_max
)
568 #endif /* MACH_ASSERT */
570 void fillPage(ppnum_t pa
, unsigned int fill
)
574 int cnt
= PAGE_SIZE
/ sizeof(unsigned int);
578 for (i
= 0, addr
= (unsigned int *)PHYSMAP_PTOV(src
); i
< cnt
; i
++)
582 static inline void __clflush(void *ptr
)
584 __asm__
volatile("clflush (%0)" : : "r" (ptr
));
587 void dcache_incoherent_io_store64(addr64_t pa
, unsigned int count
)
589 addr64_t linesize
= cpuid_info()->cache_linesize
;
590 addr64_t bound
= (pa
+ count
+ linesize
- 1) & ~(linesize
- 1);
595 __clflush(PHYSMAP_PTOV(pa
));
602 void dcache_incoherent_io_flush64(addr64_t pa
, unsigned int count
)
604 return(dcache_incoherent_io_store64(pa
,count
));
608 flush_dcache64(addr64_t addr
, unsigned count
, int phys
)
611 dcache_incoherent_io_flush64(addr
, count
);
614 uint64_t linesize
= cpuid_info()->cache_linesize
;
615 addr64_t bound
= (addr
+ count
+ linesize
-1) & ~(linesize
- 1);
617 while (addr
< bound
) {
618 __clflush((void *) (uintptr_t) addr
);
626 invalidate_icache64(__unused addr64_t addr
,
627 __unused
unsigned count
,
633 addr64_t vm_last_addr
;
636 mapping_set_mod(ppnum_t pn
)
642 mapping_set_ref(ppnum_t pn
)
644 pmap_set_reference(pn
);
647 extern i386_cpu_info_t cpuid_cpu_info
;
649 cache_flush_page_phys(ppnum_t pa
)
652 unsigned char *cacheline_addr
;
653 i386_cpu_info_t
*cpuid_infop
= cpuid_info();
655 int cachelines_to_flush
;
657 cacheline_size
= cpuid_infop
->cache_linesize
;
658 if (cacheline_size
== 0)
659 panic("cacheline_size=0 cpuid_infop=%p\n", cpuid_infop
);
660 cachelines_to_flush
= PAGE_SIZE
/cacheline_size
;
664 istate
= ml_set_interrupts_enabled(FALSE
);
666 for (cacheline_addr
= (unsigned char *)PHYSMAP_PTOV(i386_ptob(pa
));
667 cachelines_to_flush
> 0;
668 cachelines_to_flush
--, cacheline_addr
+= cacheline_size
) {
669 __clflush((void *) cacheline_addr
);
672 (void) ml_set_interrupts_enabled(istate
);
680 kdp_register_callout(kdp_callout_fn_t fn
, void *arg
)
682 #pragma unused(fn,arg)
687 int host_vmxon(boolean_t exclusive __unused
)
689 return VMX_UNSUPPORTED
;
692 void host_vmxoff(void)