2 * Copyright (c) 2008 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/mach_types.h>
30 #include <mach/vm_attributes.h>
31 #include <mach/vm_param.h>
32 #include <libsa/types.h>
34 #include <vm/vm_map.h>
35 #include <i386/pmap.h>
37 #include <i386/misc_protos.h>
39 #include <i386/proc_reg.h>
41 #include <i386/pmap_internal.h>
43 #include <kdp/kdp_internal.h>
44 #include <mach/vm_map.h>
46 #include <vm/vm_protos.h>
47 #include <vm/vm_kern.h>
49 #include <machine/pal_routines.h>
51 // #define KDP_VM_READ_DEBUG 1
52 // #define KDP_VM_WRITE_DEBUG 1
54 boolean_t kdp_read_io
;
55 boolean_t kdp_trans_off
;
57 static addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
69 pp
= pmap_find_phys(pmap
, va
);
72 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
);
78 kdp_machine_vm_read( mach_vm_address_t src
, caddr_t dst
, mach_vm_size_t len
)
80 addr64_t cur_virt_src
= PAL_KDP_ADDR((addr64_t
)src
);
81 addr64_t cur_virt_dst
= PAL_KDP_ADDR((addr64_t
)(intptr_t)dst
);
82 addr64_t cur_phys_dst
, cur_phys_src
;
83 mach_vm_size_t resid
= len
;
84 mach_vm_size_t cnt
= 0, cnt_src
, cnt_dst
;
85 pmap_t src_pmap
= kernel_pmap
;
87 #ifdef KDP_VM_READ_DEBUG
88 printf("kdp_vm_read: src %llx dst %p len %llx\n", src
, (void *)dst
, len
);
92 kdp_readphysmem64_req_t rq
;
96 rq
.nbytes
= (uint32_t)len
;
97 ret
= kdp_machine_phys_read(&rq
, dst
, KDP_CURRENT_LCPU
);
101 /* If a different pmap has been specified with kdp_pmap, use it to translate the
102 * source (cur_virt_src); otherwise, the source is translated using the
109 if (!(cur_phys_src
= kdp_vtophys(src_pmap
,
113 /* Always translate the destination buffer using the kernel_pmap */
114 if(!(cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)))
117 /* Validate physical page numbers unless kdp_read_io is set */
118 if (kdp_read_io
== FALSE
)
119 if (!pmap_valid_page(i386_btop(cur_phys_dst
)) || !pmap_valid_page(i386_btop(cur_phys_src
)))
122 /* Get length left on page */
123 cnt_src
= PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
);
124 cnt_dst
= PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
);
125 if (cnt_src
> cnt_dst
)
132 /* Do a physical copy */
133 ml_copy_phys(cur_phys_src
, cur_phys_dst
, (vm_size_t
)cnt
);
140 return (len
- resid
);
144 kdp_machine_phys_read(kdp_readphysmem64_req_t
*rq
, caddr_t dst
,
147 mach_vm_address_t src
= rq
->address
;
148 mach_vm_size_t len
= rq
->nbytes
;
150 addr64_t cur_virt_dst
;
151 addr64_t cur_phys_dst
, cur_phys_src
;
152 mach_vm_size_t resid
= len
;
153 mach_vm_size_t cnt
= 0, cnt_src
, cnt_dst
;
155 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
156 return (mach_vm_size_t
)
157 kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_phys_read
, rq
, dst
);
160 #ifdef KDP_VM_READ_DEBUG
161 printf("kdp_phys_read: src %llx dst %p len %llx\n", src
, (void *)dst
, len
);
164 cur_virt_dst
= (addr64_t
)(intptr_t)dst
;
165 cur_phys_src
= (addr64_t
)src
;
169 if(!(cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)))
172 /* Get length left on page */
173 cnt_src
= PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
);
174 cnt_dst
= PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
);
175 if (cnt_src
> cnt_dst
)
182 /* Do a physical copy; use ml_copy_phys() in the event this is
183 * a short read with potential side effects.
185 ml_copy_phys(cur_phys_src
, cur_phys_dst
, (vm_size_t
)cnt
);
191 return (len
- resid
);
198 kdp_machine_vm_write( caddr_t src
, mach_vm_address_t dst
, mach_vm_size_t len
)
200 addr64_t cur_virt_src
, cur_virt_dst
;
201 addr64_t cur_phys_src
, cur_phys_dst
;
202 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
204 #ifdef KDP_VM_WRITE_DEBUG
205 printf("kdp_vm_write: src %p dst %llx len %llx - %08X %08X\n", (void *)src
, dst
, len
, ((unsigned int *)src
)[0], ((unsigned int *)src
)[1]);
208 cur_virt_src
= PAL_KDP_ADDR((addr64_t
)(intptr_t)src
);
209 cur_virt_dst
= PAL_KDP_ADDR((addr64_t
)dst
);
211 resid
= (unsigned)len
;
214 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
217 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
220 /* Copy as many bytes as possible without crossing a page */
221 cnt_src
= (unsigned)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
222 cnt_dst
= (unsigned)(PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
));
224 if (cnt_src
> cnt_dst
)
231 ml_copy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
238 return (len
- resid
);
245 kdp_machine_phys_write(kdp_writephysmem64_req_t
*rq
, caddr_t src
,
248 mach_vm_address_t dst
= rq
->address
;
249 mach_vm_size_t len
= rq
->nbytes
;
250 addr64_t cur_virt_src
;
251 addr64_t cur_phys_src
, cur_phys_dst
;
252 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
254 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
255 return (mach_vm_size_t
)
256 kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_phys_write
, rq
, src
);
259 #ifdef KDP_VM_WRITE_DEBUG
260 printf("kdp_phys_write: src %p dst %llx len %llx - %08X %08X\n", (void *)src
, dst
, len
, ((unsigned int *)src
)[0], ((unsigned int *)src
)[1]);
263 cur_virt_src
= (addr64_t
)(intptr_t)src
;
264 cur_phys_dst
= (addr64_t
)dst
;
266 resid
= (unsigned)len
;
269 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
272 /* Copy as many bytes as possible without crossing a page */
273 cnt_src
= (unsigned)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
274 cnt_dst
= (unsigned)(PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
));
276 if (cnt_src
> cnt_dst
)
283 ml_copy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
291 return (len
- resid
);
295 kdp_machine_ioport_read(kdp_readioport_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
297 uint16_t addr
= rq
->address
;
298 uint16_t size
= rq
->nbytes
;
300 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
301 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_ioport_read
, rq
, data
);
307 *((uint8_t *) data
) = inb(addr
);
310 *((uint16_t *) data
) = inw(addr
);
313 *((uint32_t *) data
) = inl(addr
);
316 return KDPERR_BADFLAVOR
;
320 return KDPERR_NO_ERROR
;
324 kdp_machine_ioport_write(kdp_writeioport_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
326 uint16_t addr
= rq
->address
;
327 uint16_t size
= rq
->nbytes
;
329 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
330 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_ioport_write
, rq
, data
);
336 outb(addr
, *((uint8_t *) data
));
339 outw(addr
, *((uint16_t *) data
));
342 outl(addr
, *((uint32_t *) data
));
345 return KDPERR_BADFLAVOR
;
349 return KDPERR_NO_ERROR
;
353 kdp_machine_msr64_read(kdp_readmsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
355 uint64_t *value
= (uint64_t *) data
;
356 uint32_t msr
= rq
->address
;
358 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
359 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_msr64_read
, rq
, data
);
362 *value
= rdmsr64(msr
);
363 return KDPERR_NO_ERROR
;
367 kdp_machine_msr64_write(kdp_writemsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
369 uint64_t *value
= (uint64_t *) data
;
370 uint32_t msr
= rq
->address
;
372 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
373 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_msr64_write
, rq
, data
);
376 wrmsr64(msr
, *value
);
377 return KDPERR_NO_ERROR
;
380 pt_entry_t
*debugger_ptep
;
381 vm_map_offset_t debugger_window_kva
;
383 /* Establish a pagetable window that can be remapped on demand.
384 * This is utilized by the debugger to address regions outside
389 kdp_machine_init(void) {
390 if (debug_boot_arg
== 0)
394 kern_return_t kr
= vm_map_find_space(kernel_map
,
395 &debugger_window_kva
,
397 VM_MAKE_TAG(VM_MEMORY_IOKIT
), &e
);
399 if (kr
!= KERN_SUCCESS
) {
400 panic("%s: vm_map_find_space failed with %d\n", __FUNCTION__
, kr
);
403 vm_map_unlock(kernel_map
);
405 debugger_ptep
= pmap_pte(kernel_pmap
, debugger_window_kva
);
407 if (debugger_ptep
== NULL
) {
408 pmap_expand(kernel_pmap
, debugger_window_kva
);
409 debugger_ptep
= pmap_pte(kernel_pmap
, debugger_window_kva
);