]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/ml/i386/kdp_x86_common.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kdp / ml / i386 / kdp_x86_common.c
1 /*
2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/errno.h>
30
31 #include <mach/mach_types.h>
32 #include <mach/vm_attributes.h>
33 #include <mach/vm_param.h>
34 #include <libsa/types.h>
35
36 #include <vm/vm_map.h>
37 #include <i386/pmap.h>
38 #include <i386/pmap_internal.h> /* pmap_pde */
39 #include <i386/mp.h>
40 #include <i386/misc_protos.h>
41 #include <i386/pio.h>
42 #include <i386/proc_reg.h>
43
44 #include <i386/pmap_internal.h>
45
46 #include <kdp/kdp_internal.h>
47 #include <kdp/kdp_core.h>
48 #include <kdp/ml/i386/kdp_x86_common.h>
49 #include <mach/vm_map.h>
50
51 #include <vm/vm_protos.h>
52 #include <vm/vm_kern.h>
53
54 #include <machine/pal_routines.h>
55 #include <libkern/kernel_mach_header.h>
56
57 // #define KDP_VM_READ_DEBUG 1
58 // #define KDP_VM_WRITE_DEBUG 1
59
60 /*
61 * A (potentially valid) physical address is not a kernel address
62 * i.e. it'a a user address.
63 */
64 #define IS_PHYS_ADDR(addr) IS_USERADDR64_CANONICAL(addr)
65
66 boolean_t kdp_read_io;
67 boolean_t kdp_trans_off;
68
69 pmap_paddr_t kdp_vtophys(pmap_t pmap, vm_offset_t va);
70
71 pmap_t kdp_pmap = 0;
72
73 kdp_jtag_coredump_t kdp_jtag_coredump;
74
75 pmap_paddr_t
76 kdp_vtophys(
77 pmap_t pmap,
78 vm_offset_t va)
79 {
80 pmap_paddr_t pa;
81
82 pa = pmap_find_pa(pmap, va);
83
84 return pa;
85 }
86
87 mach_vm_size_t
88 kdp_machine_vm_read( mach_vm_address_t src, caddr_t dst, mach_vm_size_t len)
89 {
90 addr64_t cur_virt_src = PAL_KDP_ADDR((addr64_t)src);
91 addr64_t cur_virt_dst = PAL_KDP_ADDR((addr64_t)(intptr_t)dst);
92 addr64_t cur_phys_dst, cur_phys_src;
93 mach_vm_size_t resid = len;
94 mach_vm_size_t cnt = 0, cnt_src, cnt_dst;
95 pmap_t src_pmap = kernel_pmap;
96
97 #ifdef KDP_VM_READ_DEBUG
98 printf("kdp_vm_read: src %llx dst %p len %llx\n", src, (void *)dst, len);
99 #endif
100
101 if (kdp_trans_off && IS_PHYS_ADDR(src)) {
102 kdp_readphysmem64_req_t rq;
103 mach_vm_size_t ret;
104
105 rq.address = src;
106 rq.nbytes = (uint32_t)len;
107 ret = kdp_machine_phys_read(&rq, dst, KDP_CURRENT_LCPU);
108 return ret;
109 }
110
111 /* If a different pmap has been specified with kdp_pmap, use it to translate the
112 * source (cur_virt_src); otherwise, the source is translated using the
113 * kernel_pmap.
114 */
115 if (kdp_pmap) {
116 src_pmap = kdp_pmap;
117 }
118
119 while (resid != 0) {
120 if (!(cur_phys_src = kdp_vtophys(src_pmap,
121 cur_virt_src))) {
122 goto exit;
123 }
124
125 /* Always translate the destination buffer using the kernel_pmap */
126 if (!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst))) {
127 goto exit;
128 }
129
130 /* Validate physical page numbers unless kdp_read_io is set */
131 if (kdp_read_io == FALSE) {
132 if (!pmap_valid_page(i386_btop(cur_phys_dst)) || !pmap_valid_page(i386_btop(cur_phys_src))) {
133 goto exit;
134 }
135 }
136
137 /* Get length left on page */
138 cnt_src = PAGE_SIZE - (cur_phys_src & PAGE_MASK);
139 cnt_dst = PAGE_SIZE - (cur_phys_dst & PAGE_MASK);
140 if (cnt_src > cnt_dst) {
141 cnt = cnt_dst;
142 } else {
143 cnt = cnt_src;
144 }
145 if (cnt > resid) {
146 cnt = resid;
147 }
148
149 /* Do a physical copy */
150 if (EFAULT == ml_copy_phys(cur_phys_src,
151 cur_phys_dst,
152 (vm_size_t)cnt)) {
153 goto exit;
154 }
155 cur_virt_src += cnt;
156 cur_virt_dst += cnt;
157 resid -= cnt;
158 }
159 exit:
160 return len - resid;
161 }
162
163 mach_vm_size_t
164 kdp_machine_phys_read(kdp_readphysmem64_req_t *rq, caddr_t dst,
165 uint16_t lcpu)
166 {
167 mach_vm_address_t src = rq->address;
168 mach_vm_size_t len = rq->nbytes;
169
170 addr64_t cur_virt_dst;
171 addr64_t cur_phys_dst, cur_phys_src;
172 mach_vm_size_t resid = len;
173 mach_vm_size_t cnt = 0, cnt_src, cnt_dst;
174
175 if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
176 return (mach_vm_size_t)
177 kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_phys_read, rq, dst);
178 }
179
180 #ifdef KDP_VM_READ_DEBUG
181 printf("kdp_phys_read: src %llx dst %p len %llx\n", src, (void *)dst, len);
182 #endif
183
184 cur_virt_dst = (addr64_t)(intptr_t)dst;
185 cur_phys_src = (addr64_t)src;
186
187 while (resid != 0) {
188 if (!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst))) {
189 goto exit;
190 }
191
192 /* Get length left on page */
193 cnt_src = PAGE_SIZE - (cur_phys_src & PAGE_MASK);
194 cnt_dst = PAGE_SIZE - (cur_phys_dst & PAGE_MASK);
195 if (cnt_src > cnt_dst) {
196 cnt = cnt_dst;
197 } else {
198 cnt = cnt_src;
199 }
200 if (cnt > resid) {
201 cnt = resid;
202 }
203
204 /* Do a physical copy; use ml_copy_phys() in the event this is
205 * a short read with potential side effects.
206 */
207 if (EFAULT == ml_copy_phys(cur_phys_src,
208 cur_phys_dst,
209 (vm_size_t)cnt)) {
210 goto exit;
211 }
212 cur_phys_src += cnt;
213 cur_virt_dst += cnt;
214 resid -= cnt;
215 }
216 exit:
217 return len - resid;
218 }
219
220 /*
221 *
222 */
223 mach_vm_size_t
224 kdp_machine_vm_write( caddr_t src, mach_vm_address_t dst, mach_vm_size_t len)
225 {
226 addr64_t cur_virt_src, cur_virt_dst;
227 addr64_t cur_phys_src, cur_phys_dst;
228 unsigned resid, cnt, cnt_src, cnt_dst;
229
230 #ifdef KDP_VM_WRITE_DEBUG
231 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]);
232 #endif
233
234 cur_virt_src = PAL_KDP_ADDR((addr64_t)(intptr_t)src);
235 cur_virt_dst = PAL_KDP_ADDR((addr64_t)dst);
236
237 resid = (unsigned)len;
238
239 while (resid != 0) {
240 if ((cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)) == 0) {
241 goto exit;
242 }
243
244 if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0) {
245 goto exit;
246 }
247
248 /* Copy as many bytes as possible without crossing a page */
249 cnt_src = (unsigned)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
250 cnt_dst = (unsigned)(PAGE_SIZE - (cur_phys_dst & PAGE_MASK));
251
252 if (cnt_src > cnt_dst) {
253 cnt = cnt_dst;
254 } else {
255 cnt = cnt_src;
256 }
257 if (cnt > resid) {
258 cnt = resid;
259 }
260
261 if (EFAULT == ml_copy_phys(cur_phys_src, cur_phys_dst, cnt)) {
262 goto exit; /* Copy stuff over */
263 }
264 cur_virt_src += cnt;
265 cur_virt_dst += cnt;
266 resid -= cnt;
267 }
268 exit:
269 return len - resid;
270 }
271
272 /*
273 *
274 */
275 mach_vm_size_t
276 kdp_machine_phys_write(kdp_writephysmem64_req_t *rq, caddr_t src,
277 uint16_t lcpu)
278 {
279 mach_vm_address_t dst = rq->address;
280 mach_vm_size_t len = rq->nbytes;
281 addr64_t cur_virt_src;
282 addr64_t cur_phys_src, cur_phys_dst;
283 unsigned resid, cnt, cnt_src, cnt_dst;
284
285 if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
286 return (mach_vm_size_t)
287 kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_phys_write, rq, src);
288 }
289
290 #ifdef KDP_VM_WRITE_DEBUG
291 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]);
292 #endif
293
294 cur_virt_src = (addr64_t)(intptr_t)src;
295 cur_phys_dst = (addr64_t)dst;
296
297 resid = (unsigned)len;
298
299 while (resid != 0) {
300 if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0) {
301 goto exit;
302 }
303
304 /* Copy as many bytes as possible without crossing a page */
305 cnt_src = (unsigned)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
306 cnt_dst = (unsigned)(PAGE_SIZE - (cur_phys_dst & PAGE_MASK));
307
308 if (cnt_src > cnt_dst) {
309 cnt = cnt_dst;
310 } else {
311 cnt = cnt_src;
312 }
313 if (cnt > resid) {
314 cnt = resid;
315 }
316
317 if (EFAULT == ml_copy_phys(cur_phys_src, cur_phys_dst, cnt)) {
318 goto exit; /* Copy stuff over */
319 }
320 cur_virt_src += cnt;
321 cur_phys_dst += cnt;
322 resid -= cnt;
323 }
324
325 exit:
326 return len - resid;
327 }
328
329 int
330 kdp_machine_ioport_read(kdp_readioport_req_t *rq, caddr_t data, uint16_t lcpu)
331 {
332 uint16_t addr = rq->address;
333 uint16_t size = rq->nbytes;
334
335 if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
336 return (int) kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_ioport_read, rq, data);
337 }
338
339 switch (size) {
340 case 1:
341 *((uint8_t *) data) = inb(addr);
342 break;
343 case 2:
344 *((uint16_t *) data) = inw(addr);
345 break;
346 case 4:
347 *((uint32_t *) data) = inl(addr);
348 break;
349 default:
350 return KDPERR_BADFLAVOR;
351 }
352
353 return KDPERR_NO_ERROR;
354 }
355
356 int
357 kdp_machine_ioport_write(kdp_writeioport_req_t *rq, caddr_t data, uint16_t lcpu)
358 {
359 uint16_t addr = rq->address;
360 uint16_t size = rq->nbytes;
361
362 if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
363 return (int) kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_ioport_write, rq, data);
364 }
365
366 switch (size) {
367 case 1:
368 outb(addr, *((uint8_t *) data));
369 break;
370 case 2:
371 outw(addr, *((uint16_t *) data));
372 break;
373 case 4:
374 outl(addr, *((uint32_t *) data));
375 break;
376 default:
377 return KDPERR_BADFLAVOR;
378 }
379
380 return KDPERR_NO_ERROR;
381 }
382
383 int
384 kdp_machine_msr64_read(kdp_readmsr64_req_t *rq, caddr_t data, uint16_t lcpu)
385 {
386 uint64_t *value = (uint64_t *) data;
387 uint32_t msr = rq->address;
388
389 if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
390 return (int) kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_msr64_read, rq, data);
391 }
392
393 *value = rdmsr64(msr);
394 return KDPERR_NO_ERROR;
395 }
396
397 int
398 kdp_machine_msr64_write(kdp_writemsr64_req_t *rq, caddr_t data, uint16_t lcpu)
399 {
400 uint64_t *value = (uint64_t *) data;
401 uint32_t msr = rq->address;
402
403 if ((lcpu != KDP_CURRENT_LCPU) && (lcpu != cpu_number())) {
404 return (int) kdp_x86_xcpu_invoke(lcpu, (kdp_x86_xcpu_func_t)kdp_machine_msr64_write, rq, data);
405 }
406
407 wrmsr64(msr, *value);
408 return KDPERR_NO_ERROR;
409 }
410
411 pt_entry_t *debugger_ptep;
412 vm_map_offset_t debugger_window_kva;
413
414 /* Establish a pagetable window that can be remapped on demand.
415 * This is utilized by the debugger to address regions outside
416 * the physical map.
417 */
418
419 void
420 kdp_map_debug_pagetable_window(void)
421 {
422 vm_map_entry_t e;
423 kern_return_t kr;
424
425 kr = vm_map_find_space(kernel_map,
426 &debugger_window_kva,
427 PAGE_SIZE, 0,
428 0,
429 VM_MAP_KERNEL_FLAGS_NONE,
430 VM_KERN_MEMORY_OSFMK,
431 &e);
432
433 if (kr != KERN_SUCCESS) {
434 panic("%s: vm_map_find_space failed with %d\n", __FUNCTION__, kr);
435 }
436
437 vm_map_unlock(kernel_map);
438
439 debugger_ptep = pmap_pte(kernel_pmap, debugger_window_kva);
440
441 if (debugger_ptep == NULL) {
442 pmap_expand(kernel_pmap, debugger_window_kva, PMAP_EXPAND_OPTIONS_NONE);
443 debugger_ptep = pmap_pte(kernel_pmap, debugger_window_kva);
444 }
445 }
446
447 /* initialize kdp_jtag_coredump with data needed for JTAG coredump extraction */
448
449 void
450 kdp_jtag_coredump_init(void)
451 {
452 kdp_jtag_coredump.version = (uint64_t) KDP_JTAG_COREDUMP_VERSION_1;
453 kdp_jtag_coredump.kernel_map_start = (uint64_t) kernel_map->min_offset;
454 kdp_jtag_coredump.kernel_map_end = (uint64_t) kernel_map->max_offset;
455 kdp_jtag_coredump.kernel_pmap_pml4 = (uint64_t) kernel_pmap->pm_pml4;
456 kdp_jtag_coredump.pmap_memory_regions = (uint64_t) &pmap_memory_regions;
457 kdp_jtag_coredump.pmap_memory_region_count = (uint64_t) pmap_memory_region_count;
458 kdp_jtag_coredump.pmap_memory_region_t_size = (uint64_t) sizeof(pmap_memory_region_t);
459 kdp_jtag_coredump.physmap_base = (uint64_t) &physmap_base;
460
461 /* update signature last so that JTAG can trust that structure has valid data */
462 kdp_jtag_coredump.signature = (uint64_t) KDP_JTAG_COREDUMP_SIGNATURE;
463 }
464
465 void
466 kdp_machine_init(void)
467 {
468 if (debug_boot_arg == 0) {
469 return;
470 }
471
472 kdp_map_debug_pagetable_window();
473 kdp_jtag_coredump_init();
474 }