2 * Copyright (c) 2007-2016 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@
29 #include <mach_assert.h>
30 #include <mach/vm_types.h>
31 #include <mach/mach_time.h>
32 #include <kern/timer.h>
33 #include <kern/clock.h>
34 #include <kern/machine.h>
35 #include <mach/machine.h>
36 #include <mach/machine/vm_param.h>
38 #include <kdp/kdp_udp.h>
40 #include <kdp/kdp_callout.h>
41 #endif /* !MACH_KDP */
42 #include <arm/cpu_data.h>
43 #include <arm/cpu_data_internal.h>
44 #include <arm/caches_internal.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_map.h>
50 #include <arm/misc_protos.h>
52 #include <sys/errno.h>
54 #define INT_SIZE (BYTE_SIZE * sizeof (int))
58 bcopy_phys(addr64_t src
, addr64_t dst
, vm_size_t bytes
)
60 unsigned int src_index
;
61 unsigned int dst_index
;
62 vm_offset_t src_offset
;
63 vm_offset_t dst_offset
;
65 unsigned int wimg_bits_src
, wimg_bits_dst
;
66 ppnum_t pn_src
= (src
>> PAGE_SHIFT
);
67 ppnum_t pn_dst
= (dst
>> PAGE_SHIFT
);
69 wimg_bits_src
= pmap_cache_attributes(pn_src
);
70 wimg_bits_dst
= pmap_cache_attributes(pn_dst
);
72 if (mmu_kvtop_wpreflight(phystokv((pmap_paddr_t
) dst
)) &&
73 ((wimg_bits_src
& VM_WIMG_MASK
) == VM_WIMG_DEFAULT
) &&
74 ((wimg_bits_dst
& VM_WIMG_MASK
) == VM_WIMG_DEFAULT
)) {
75 /* Fast path - dst is writable and both source and destination have default attributes */
76 bcopy((char *)phystokv((pmap_paddr_t
) src
), (char *)phystokv((pmap_paddr_t
) dst
), bytes
);
80 src_offset
= src
& PAGE_MASK
;
81 dst_offset
= dst
& PAGE_MASK
;
83 if ((src_offset
+ bytes
) > PAGE_SIZE
|| (dst_offset
+ bytes
) > PAGE_SIZE
) {
84 panic("bcopy extends beyond copy windows");
87 mp_disable_preemption();
88 cpu_num
= cpu_number();
89 src_index
= pmap_map_cpu_windows_copy(pn_src
, VM_PROT_READ
, wimg_bits_src
);
90 dst_index
= pmap_map_cpu_windows_copy(pn_dst
, VM_PROT_READ
| VM_PROT_WRITE
, wimg_bits_dst
);
92 bcopy((char *)(pmap_cpu_windows_copy_addr(cpu_num
, src_index
) + src_offset
),
93 (char *)(pmap_cpu_windows_copy_addr(cpu_num
, dst_index
) + dst_offset
),
96 pmap_unmap_cpu_windows_copy(src_index
);
97 pmap_unmap_cpu_windows_copy(dst_index
);
98 mp_enable_preemption();
102 bzero_phys_nc(addr64_t src64
, vm_size_t bytes
)
104 bzero_phys(src64
, bytes
);
107 /* Zero bytes starting at a physical address */
109 bzero_phys(addr64_t src
, vm_size_t bytes
)
111 unsigned int wimg_bits
;
112 ppnum_t pn
= (src
>> PAGE_SHIFT
);
114 wimg_bits
= pmap_cache_attributes(pn
);
115 if ((wimg_bits
& VM_WIMG_MASK
) == VM_WIMG_DEFAULT
) {
116 /* Fast path - default attributes */
117 bzero((char *)phystokv((pmap_paddr_t
) src
), bytes
);
119 mp_disable_preemption();
121 unsigned int cpu_num
= cpu_number();
124 vm_offset_t offset
= src
& PAGE_MASK
;
125 uint32_t count
= PAGE_SIZE
- offset
;
131 unsigned int index
= pmap_map_cpu_windows_copy(src
>> PAGE_SHIFT
, VM_PROT_READ
| VM_PROT_WRITE
, wimg_bits
);
133 bzero((char *)(pmap_cpu_windows_copy_addr(cpu_num
, index
) + offset
), count
);
135 pmap_unmap_cpu_windows_copy(index
);
141 mp_enable_preemption();
146 * Read data from a physical address.
151 ml_phys_read_data(pmap_paddr_t paddr
, int size
)
155 unsigned int wimg_bits
;
156 ppnum_t pn
= (paddr
>> PAGE_SHIFT
);
159 vm_offset_t copywindow_vaddr
= 0;
161 mp_disable_preemption();
162 wimg_bits
= pmap_cache_attributes(pn
);
163 index
= pmap_map_cpu_windows_copy(pn
, VM_PROT_READ
, wimg_bits
);
164 copywindow_vaddr
= pmap_cpu_windows_copy_addr(cpu_number(), index
) | ((uint32_t)paddr
& PAGE_MASK
);;
168 s1
= *(volatile unsigned char *)(copywindow_vaddr
);
172 s2
= *(volatile unsigned short *)(copywindow_vaddr
);
177 result
= *(volatile unsigned int *)(copywindow_vaddr
);
181 pmap_unmap_cpu_windows_copy(index
);
182 mp_enable_preemption();
187 static unsigned long long
188 ml_phys_read_long_long(pmap_paddr_t paddr
)
192 unsigned int wimg_bits
;
193 ppnum_t pn
= (paddr
>> PAGE_SHIFT
);
195 mp_disable_preemption();
196 wimg_bits
= pmap_cache_attributes(pn
);
197 index
= pmap_map_cpu_windows_copy(pn
, VM_PROT_READ
, wimg_bits
);
199 result
= *(volatile unsigned long long *)(pmap_cpu_windows_copy_addr(cpu_number(), index
)
200 | ((uint32_t)paddr
& PAGE_MASK
));
202 pmap_unmap_cpu_windows_copy(index
);
203 mp_enable_preemption();
209 ml_phys_read( vm_offset_t paddr
)
211 return ml_phys_read_data((pmap_paddr_t
)paddr
, 4);
215 ml_phys_read_word(vm_offset_t paddr
)
217 return ml_phys_read_data((pmap_paddr_t
)paddr
, 4);
221 ml_phys_read_64(addr64_t paddr64
)
223 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 4);
227 ml_phys_read_word_64(addr64_t paddr64
)
229 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 4);
233 ml_phys_read_half(vm_offset_t paddr
)
235 return ml_phys_read_data((pmap_paddr_t
)paddr
, 2);
239 ml_phys_read_half_64(addr64_t paddr64
)
241 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 2);
245 ml_phys_read_byte(vm_offset_t paddr
)
247 return ml_phys_read_data((pmap_paddr_t
)paddr
, 1);
251 ml_phys_read_byte_64(addr64_t paddr64
)
253 return ml_phys_read_data((pmap_paddr_t
)paddr64
, 1);
257 ml_phys_read_double(vm_offset_t paddr
)
259 return ml_phys_read_long_long((pmap_paddr_t
)paddr
);
263 ml_phys_read_double_64(addr64_t paddr64
)
265 return ml_phys_read_long_long((pmap_paddr_t
)paddr64
);
271 * Write data to a physical address.
275 ml_phys_write_data(pmap_paddr_t paddr
, unsigned long data
, int size
)
278 unsigned int wimg_bits
;
279 ppnum_t pn
= (paddr
>> PAGE_SHIFT
);
280 vm_offset_t copywindow_vaddr
= 0;
282 mp_disable_preemption();
283 wimg_bits
= pmap_cache_attributes(pn
);
284 index
= pmap_map_cpu_windows_copy(pn
, VM_PROT_READ
| VM_PROT_WRITE
, wimg_bits
);
285 copywindow_vaddr
= pmap_cpu_windows_copy_addr(cpu_number(), index
) | ((uint32_t) paddr
& PAGE_MASK
);
289 *(volatile unsigned char *)(copywindow_vaddr
) = (unsigned char)data
;
292 *(volatile unsigned short *)(copywindow_vaddr
) = (unsigned short)data
;
296 *(volatile unsigned int *)(copywindow_vaddr
) = (uint32_t)data
;
300 pmap_unmap_cpu_windows_copy(index
);
301 mp_enable_preemption();
305 ml_phys_write_long_long(pmap_paddr_t paddr
, unsigned long long data
)
308 unsigned int wimg_bits
;
309 ppnum_t pn
= (paddr
>> PAGE_SHIFT
);
311 mp_disable_preemption();
312 wimg_bits
= pmap_cache_attributes(pn
);
313 index
= pmap_map_cpu_windows_copy(pn
, VM_PROT_READ
| VM_PROT_WRITE
, wimg_bits
);
315 *(volatile unsigned long long *)(pmap_cpu_windows_copy_addr(cpu_number(), index
)
316 | ((uint32_t)paddr
& PAGE_MASK
)) = data
;
318 pmap_unmap_cpu_windows_copy(index
);
319 mp_enable_preemption();
325 ml_phys_write_byte(vm_offset_t paddr
, unsigned int data
)
327 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 1);
331 ml_phys_write_byte_64(addr64_t paddr64
, unsigned int data
)
333 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 1);
337 ml_phys_write_half(vm_offset_t paddr
, unsigned int data
)
339 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 2);
343 ml_phys_write_half_64(addr64_t paddr64
, unsigned int data
)
345 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 2);
349 ml_phys_write(vm_offset_t paddr
, unsigned int data
)
351 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 4);
355 ml_phys_write_64(addr64_t paddr64
, unsigned int data
)
357 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 4);
361 ml_phys_write_word(vm_offset_t paddr
, unsigned int data
)
363 ml_phys_write_data((pmap_paddr_t
)paddr
, data
, 4);
367 ml_phys_write_word_64(addr64_t paddr64
, unsigned int data
)
369 ml_phys_write_data((pmap_paddr_t
)paddr64
, data
, 4);
373 ml_phys_write_double(vm_offset_t paddr
, unsigned long long data
)
375 ml_phys_write_long_long((pmap_paddr_t
)paddr
, data
);
379 ml_phys_write_double_64(addr64_t paddr64
, unsigned long long data
)
381 ml_phys_write_long_long((pmap_paddr_t
)paddr64
, data
);
386 * Set indicated bit in bit string.
389 setbit(int bitno
, int *s
)
391 s
[bitno
/ INT_SIZE
] |= 1 << (bitno
% INT_SIZE
);
395 * Clear indicated bit in bit string.
398 clrbit(int bitno
, int *s
)
400 s
[bitno
/ INT_SIZE
] &= ~(1 << (bitno
% INT_SIZE
));
404 * Test if indicated bit is set in bit string.
407 testbit(int bitno
, int *s
)
409 return s
[bitno
/ INT_SIZE
] & (1 << (bitno
% INT_SIZE
));
413 * Find first bit set in bit string.
420 for (offset
= 0; !*s
; offset
+= INT_SIZE
, ++s
) {
423 return offset
+ __builtin_ctz(*s
);
427 ffs(unsigned int mask
)
434 * NOTE: cannot use __builtin_ffs because it generates a call to
437 return 1 + __builtin_ctz(mask
);
441 ffsll(unsigned long long mask
)
448 * NOTE: cannot use __builtin_ffsll because it generates a call to
451 return 1 + __builtin_ctzll(mask
);
455 * Find last bit set in bit string.
458 fls(unsigned int mask
)
464 return (sizeof(mask
) << 3) - __builtin_clz(mask
);
468 flsll(unsigned long long mask
)
474 return (sizeof(mask
) << 3) - __builtin_clzll(mask
);
483 const char *a
= (const char *) pa
;
484 const char *b
= (const char *) pb
;
500 memcmp(const void *s1
, const void *s2
, size_t n
)
503 const unsigned char *p1
= s1
, *p2
= s2
;
506 if (*p1
++ != *p2
++) {
507 return *--p1
- *--p2
;
515 copypv(addr64_t source
, addr64_t sink
, unsigned int size
, int which
)
517 kern_return_t retval
= KERN_SUCCESS
;
519 unsigned int from_wimg_bits
, to_wimg_bits
;
521 from
= CAST_DOWN(void *, source
);
522 to
= CAST_DOWN(void *, sink
);
524 if ((which
& (cppvPsrc
| cppvPsnk
)) == 0) { /* Make sure that only
526 panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */
528 if (which
& cppvPsrc
) {
529 from
= (void *)phystokv((pmap_paddr_t
)from
);
531 if (which
& cppvPsnk
) {
532 to
= (void *)phystokv((pmap_paddr_t
)to
);
535 if ((which
& (cppvPsrc
| cppvKmap
)) == 0) { /* Source is virtual in
537 retval
= copyin((user_addr_t
) from
, to
, size
);
538 } else if ((which
& (cppvPsnk
| cppvKmap
)) == 0) { /* Sink is virtual in
540 retval
= copyout(from
, (user_addr_t
) to
, size
);
541 } else { /* both addresses are physical or kernel map */
542 bcopy(from
, to
, size
);
545 if (which
& cppvFsrc
) {
546 flush_dcache64(source
, size
, ((which
& cppvPsrc
) == cppvPsrc
));
547 } else if (which
& cppvPsrc
) {
548 from_wimg_bits
= pmap_cache_attributes(source
>> PAGE_SHIFT
);
549 if ((from_wimg_bits
!= VM_WIMG_COPYBACK
) && (from_wimg_bits
!= VM_WIMG_WTHRU
)) {
550 flush_dcache64(source
, size
, TRUE
);
554 if (which
& cppvFsnk
) {
555 flush_dcache64(sink
, size
, ((which
& cppvPsnk
) == cppvPsnk
));
556 } else if (which
& cppvPsnk
) {
557 to_wimg_bits
= pmap_cache_attributes(sink
>> PAGE_SHIFT
);
558 if (to_wimg_bits
!= VM_WIMG_COPYBACK
) {
559 flush_dcache64(sink
, size
, TRUE
);
566 * Copy sizes bigger than this value will cause a kernel panic.
568 * Yes, this is an arbitrary fixed limit, but it's almost certainly
569 * a programming error to be copying more than this amount between
570 * user and wired kernel memory in a single invocation on this
573 const int copysize_limit_panic
= (64 * 1024 * 1024);
576 * Validate the arguments to copy{in,out} on this platform.
578 * Called when nbytes is "large" e.g. more than a page. Such sizes are
579 * infrequent, and very large sizes are likely indications of attempts
580 * to exploit kernel programming errors (bugs).
583 copy_validate(const user_addr_t user_addr
,
584 uintptr_t kernel_addr
, vm_size_t nbytes
)
586 uintptr_t kernel_addr_last
= kernel_addr
+ nbytes
;
588 if (__improbable(kernel_addr
< VM_MIN_KERNEL_ADDRESS
||
589 kernel_addr
> VM_MAX_KERNEL_ADDRESS
||
590 kernel_addr_last
< kernel_addr
||
591 kernel_addr_last
> VM_MAX_KERNEL_ADDRESS
)) {
592 panic("%s(%p, %p, %u) - kaddr not in kernel", __func__
,
593 (void *)user_addr
, (void *)kernel_addr
, nbytes
);
596 user_addr_t user_addr_last
= user_addr
+ nbytes
;
598 if (__improbable((user_addr_last
< user_addr
) || ((user_addr
+ nbytes
) > vm_map_max(current_thread()->map
)) ||
599 (user_addr
< vm_map_min(current_thread()->map
)))) {
603 if (__improbable(nbytes
> copysize_limit_panic
)) {
604 panic("%s(%p, %p, %u) - transfer too large", __func__
,
605 (void *)user_addr
, (void *)kernel_addr
, nbytes
);
612 copyin_validate(const user_addr_t ua
, uintptr_t ka
, vm_size_t nbytes
)
614 return copy_validate(ua
, ka
, nbytes
);
618 copyout_validate(uintptr_t ka
, const user_addr_t ua
, vm_size_t nbytes
)
620 return copy_validate(ua
, ka
, nbytes
);
625 extern int copyinframe(vm_address_t fp
, char *frame
);
628 * Machine-dependent routine to fill in an array with up to callstack_max
629 * levels of return pc information.
634 vm_size_t callstack_max
)
636 /* Captures the USER call stack */
640 struct arm_saved_state
* state
= find_user_regs(current_thread());
643 while (i
< callstack_max
) {
647 buf
[i
++] = (uintptr_t)state
->pc
;
648 frame
[0] = state
->r
[7];
650 while (i
< callstack_max
&& frame
[0] != 0) {
651 if (copyinframe(frame
[0], (void*) frame
)) {
654 buf
[i
++] = (uintptr_t)frame
[1];
657 while (i
< callstack_max
) {
663 #endif /* MACH_ASSERT */
674 __unused vm_offset_t paddr
,
675 __unused
unsigned int *val
)
677 panic("ml_probe_read() unimplemented");
683 __unused addr64_t paddr
,
684 __unused
unsigned int *val
)
686 panic("ml_probe_read_64() unimplemented");
693 __unused thread_t thread
,
694 __unused
unsigned policy_id
,
695 __unused
unsigned policy_info
)
697 // <rdar://problem/7141284>: Reduce print noise
698 // kprintf("ml_thread_policy() unimplemented\n");
703 kdp_register_callout(kdp_callout_fn_t fn
, void *arg
)
705 #pragma unused(fn,arg)