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 <kdp/kdp_internal.h>
42 #include <mach/vm_map.h>
44 #include <vm/vm_protos.h>
45 #include <vm/vm_kern.h>
47 // #define KDP_VM_READ_DEBUG 1
48 // #define KDP_VM_WRITE_DEBUG 1
50 boolean_t kdp_read_io
;
51 boolean_t kdp_trans_off
;
53 static addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
65 pp
= pmap_find_phys(pmap
, va
);
68 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
);
74 kdp_machine_vm_read( mach_vm_address_t src
, caddr_t dst
, mach_vm_size_t len
)
76 addr64_t cur_virt_src
= (addr64_t
)src
;
77 addr64_t cur_virt_dst
= (addr64_t
)(intptr_t)dst
;
78 addr64_t cur_phys_dst
, cur_phys_src
;
79 mach_vm_size_t resid
= len
;
80 mach_vm_size_t cnt
= 0, cnt_src
, cnt_dst
;
81 pmap_t src_pmap
= kernel_pmap
;
83 #ifdef KDP_VM_READ_DEBUG
84 printf("kdp_vm_read: src %llx dst %p len %llx\n", src
, (void *)dst
, len
);
88 kdp_readphysmem64_req_t rq
;
92 rq
.nbytes
= (uint32_t)len
;
93 ret
= kdp_machine_phys_read(&rq
, dst
, KDP_CURRENT_LCPU
);
97 /* If a different pmap has been specified with kdp_pmap, use it to translate the
98 * source (cur_virt_src); otherwise, the source is translated using the
105 if (!(cur_phys_src
= kdp_vtophys(src_pmap
,
109 /* Always translate the destination buffer using the kernel_pmap */
110 if(!(cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)))
113 /* Validate physical page numbers unless kdp_read_io is set */
114 if (kdp_read_io
== FALSE
)
115 if (!pmap_valid_page(i386_btop(cur_phys_dst
)) || !pmap_valid_page(i386_btop(cur_phys_src
)))
118 /* Get length left on page */
119 cnt_src
= PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
);
120 cnt_dst
= PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
);
121 if (cnt_src
> cnt_dst
)
128 /* Do a physical copy */
129 ml_copy_phys(cur_phys_src
, cur_phys_dst
, (vm_size_t
)cnt
);
136 return (len
- resid
);
140 kdp_machine_phys_read(kdp_readphysmem64_req_t
*rq
, caddr_t dst
,
143 mach_vm_address_t src
= rq
->address
;
144 mach_vm_size_t len
= rq
->nbytes
;
146 addr64_t cur_virt_dst
;
147 addr64_t cur_phys_dst
, cur_phys_src
;
148 mach_vm_size_t resid
= len
;
149 mach_vm_size_t cnt
= 0, cnt_src
, cnt_dst
;
151 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
152 return (mach_vm_size_t
)
153 kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_phys_read
, rq
, dst
);
156 #ifdef KDP_VM_READ_DEBUG
157 printf("kdp_phys_read: src %llx dst %p len %llx\n", src
, (void *)dst
, len
);
160 cur_virt_dst
= (addr64_t
)(intptr_t)dst
;
161 cur_phys_src
= (addr64_t
)src
;
165 if(!(cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)))
168 /* Get length left on page */
169 cnt_src
= PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
);
170 cnt_dst
= PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
);
171 if (cnt_src
> cnt_dst
)
178 /* Do a physical copy; use ml_copy_phys() in the event this is
179 * a short read with potential side effects.
181 ml_copy_phys(cur_phys_src
, cur_phys_dst
, (vm_size_t
)cnt
);
187 return (len
- resid
);
194 kdp_machine_vm_write( caddr_t src
, mach_vm_address_t dst
, mach_vm_size_t len
)
196 addr64_t cur_virt_src
, cur_virt_dst
;
197 addr64_t cur_phys_src
, cur_phys_dst
;
198 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
200 #ifdef KDP_VM_WRITE_DEBUG
201 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]);
204 cur_virt_src
= (addr64_t
)(intptr_t)src
;
205 cur_virt_dst
= (addr64_t
)dst
;
207 resid
= (unsigned)len
;
210 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
213 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
216 /* Copy as many bytes as possible without crossing a page */
217 cnt_src
= (unsigned)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
218 cnt_dst
= (unsigned)(PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
));
220 if (cnt_src
> cnt_dst
)
227 ml_copy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
234 return (len
- resid
);
241 kdp_machine_phys_write(kdp_writephysmem64_req_t
*rq
, caddr_t src
,
244 mach_vm_address_t dst
= rq
->address
;
245 mach_vm_size_t len
= rq
->nbytes
;
246 addr64_t cur_virt_src
;
247 addr64_t cur_phys_src
, cur_phys_dst
;
248 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
250 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
251 return (mach_vm_size_t
)
252 kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_phys_write
, rq
, src
);
255 #ifdef KDP_VM_WRITE_DEBUG
256 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]);
259 cur_virt_src
= (addr64_t
)(intptr_t)src
;
260 cur_phys_dst
= (addr64_t
)dst
;
262 resid
= (unsigned)len
;
265 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
268 /* Copy as many bytes as possible without crossing a page */
269 cnt_src
= (unsigned)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
270 cnt_dst
= (unsigned)(PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
));
272 if (cnt_src
> cnt_dst
)
279 ml_copy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
287 return (len
- resid
);
291 kdp_machine_ioport_read(kdp_readioport_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
293 uint16_t addr
= rq
->address
;
294 uint16_t size
= rq
->nbytes
;
296 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
297 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_ioport_read
, rq
, data
);
303 *((uint8_t *) data
) = inb(addr
);
306 *((uint16_t *) data
) = inw(addr
);
309 *((uint32_t *) data
) = inl(addr
);
312 return KDPERR_BADFLAVOR
;
316 return KDPERR_NO_ERROR
;
320 kdp_machine_ioport_write(kdp_writeioport_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
322 uint16_t addr
= rq
->address
;
323 uint16_t size
= rq
->nbytes
;
325 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
326 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_ioport_write
, rq
, data
);
332 outb(addr
, *((uint8_t *) data
));
335 outw(addr
, *((uint16_t *) data
));
338 outl(addr
, *((uint32_t *) data
));
341 return KDPERR_BADFLAVOR
;
345 return KDPERR_NO_ERROR
;
349 kdp_machine_msr64_read(kdp_readmsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
351 uint64_t *value
= (uint64_t *) data
;
352 uint32_t msr
= rq
->address
;
354 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
355 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_msr64_read
, rq
, data
);
358 *value
= rdmsr64(msr
);
359 return KDPERR_NO_ERROR
;
363 kdp_machine_msr64_write(kdp_writemsr64_req_t
*rq
, caddr_t data
, uint16_t lcpu
)
365 uint64_t *value
= (uint64_t *) data
;
366 uint32_t msr
= rq
->address
;
368 if ((lcpu
!= KDP_CURRENT_LCPU
) && (lcpu
!= cpu_number())) {
369 return (int) kdp_x86_xcpu_invoke(lcpu
, (kdp_x86_xcpu_func_t
)kdp_machine_msr64_write
, rq
, data
);
372 wrmsr64(msr
, *value
);
373 return KDPERR_NO_ERROR
;