]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/ml/i386/kdp_x86_common.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / kdp / ml / i386 / kdp_x86_common.c
1 /*
2 * Copyright (c) 2008 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 <mach/mach_types.h>
30 #include <mach/vm_attributes.h>
31 #include <mach/vm_param.h>
32 #include <libsa/types.h>
33
34 #include <vm/vm_map.h>
35 #include <i386/pmap.h>
36 #include <i386/mp.h>
37 #include <i386/misc_protos.h>
38 #include <i386/pio.h>
39 #include <i386/proc_reg.h>
40
41 #include <kdp/kdp_internal.h>
42 #include <mach/vm_map.h>
43
44 #include <vm/vm_protos.h>
45 #include <vm/vm_kern.h>
46
47 // #define KDP_VM_READ_DEBUG 1
48 // #define KDP_VM_WRITE_DEBUG 1
49
50 boolean_t kdp_read_io;
51 boolean_t kdp_trans_off;
52
53 static addr64_t kdp_vtophys(pmap_t pmap, addr64_t va);
54
55 pmap_t kdp_pmap = 0;
56
57 static addr64_t
58 kdp_vtophys(
59 pmap_t pmap,
60 addr64_t va)
61 {
62 addr64_t pa;
63 ppnum_t pp;
64
65 pp = pmap_find_phys(pmap, va);
66 if(!pp) return 0;
67
68 pa = ((addr64_t)pp << 12) | (va & 0x0000000000000FFFULL);
69
70 return(pa);
71 }
72
73 mach_vm_size_t
74 kdp_machine_vm_read( mach_vm_address_t src, caddr_t dst, mach_vm_size_t len)
75 {
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;
82
83 #ifdef KDP_VM_READ_DEBUG
84 printf("kdp_vm_read: src %llx dst %p len %llx\n", src, (void *)dst, len);
85 #endif
86
87 if (kdp_trans_off) {
88 kdp_readphysmem64_req_t rq;
89 mach_vm_size_t ret;
90
91 rq.address = src;
92 rq.nbytes = (uint32_t)len;
93 ret = kdp_machine_phys_read(&rq, dst, KDP_CURRENT_LCPU);
94 return ret;
95 }
96
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
99 * kernel_pmap.
100 */
101 if (kdp_pmap)
102 src_pmap = kdp_pmap;
103
104 while (resid != 0) {
105 if (!(cur_phys_src = kdp_vtophys(src_pmap,
106 cur_virt_src)))
107 goto exit;
108
109 /* Always translate the destination buffer using the kernel_pmap */
110 if(!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)))
111 goto exit;
112
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)))
116 goto exit;
117
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)
122 cnt = cnt_dst;
123 else
124 cnt = cnt_src;
125 if (cnt > resid)
126 cnt = resid;
127
128 /* Do a physical copy */
129 ml_copy_phys(cur_phys_src, cur_phys_dst, (vm_size_t)cnt);
130
131 cur_virt_src += cnt;
132 cur_virt_dst += cnt;
133 resid -= cnt;
134 }
135 exit:
136 return (len - resid);
137 }
138
139 mach_vm_size_t
140 kdp_machine_phys_read(kdp_readphysmem64_req_t *rq, caddr_t dst,
141 uint16_t lcpu)
142 {
143 mach_vm_address_t src = rq->address;
144 mach_vm_size_t len = rq->nbytes;
145
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;
150
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);
154 }
155
156 #ifdef KDP_VM_READ_DEBUG
157 printf("kdp_phys_read: src %llx dst %p len %llx\n", src, (void *)dst, len);
158 #endif
159
160 cur_virt_dst = (addr64_t)(intptr_t)dst;
161 cur_phys_src = (addr64_t)src;
162
163 while (resid != 0) {
164
165 if(!(cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)))
166 goto exit;
167
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)
172 cnt = cnt_dst;
173 else
174 cnt = cnt_src;
175 if (cnt > resid)
176 cnt = resid;
177
178 /* Do a physical copy; use ml_copy_phys() in the event this is
179 * a short read with potential side effects.
180 */
181 ml_copy_phys(cur_phys_src, cur_phys_dst, (vm_size_t)cnt);
182 cur_phys_src += cnt;
183 cur_virt_dst += cnt;
184 resid -= cnt;
185 }
186 exit:
187 return (len - resid);
188 }
189
190 /*
191 *
192 */
193 mach_vm_size_t
194 kdp_machine_vm_write( caddr_t src, mach_vm_address_t dst, mach_vm_size_t len)
195 {
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;
199
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]);
202 #endif
203
204 cur_virt_src = (addr64_t)(intptr_t)src;
205 cur_virt_dst = (addr64_t)dst;
206
207 resid = (unsigned)len;
208
209 while (resid != 0) {
210 if ((cur_phys_dst = kdp_vtophys(kernel_pmap, cur_virt_dst)) == 0)
211 goto exit;
212
213 if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0)
214 goto exit;
215
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));
219
220 if (cnt_src > cnt_dst)
221 cnt = cnt_dst;
222 else
223 cnt = cnt_src;
224 if (cnt > resid)
225 cnt = resid;
226
227 ml_copy_phys(cur_phys_src, cur_phys_dst, cnt); /* Copy stuff over */
228
229 cur_virt_src +=cnt;
230 cur_virt_dst +=cnt;
231 resid -= cnt;
232 }
233 exit:
234 return (len - resid);
235 }
236
237 /*
238 *
239 */
240 mach_vm_size_t
241 kdp_machine_phys_write(kdp_writephysmem64_req_t *rq, caddr_t src,
242 uint16_t lcpu)
243 {
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;
249
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);
253 }
254
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]);
257 #endif
258
259 cur_virt_src = (addr64_t)(intptr_t)src;
260 cur_phys_dst = (addr64_t)dst;
261
262 resid = (unsigned)len;
263
264 while (resid != 0) {
265 if ((cur_phys_src = kdp_vtophys(kernel_pmap, cur_virt_src)) == 0)
266 goto exit;
267
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));
271
272 if (cnt_src > cnt_dst)
273 cnt = cnt_dst;
274 else
275 cnt = cnt_src;
276 if (cnt > resid)
277 cnt = resid;
278
279 ml_copy_phys(cur_phys_src, cur_phys_dst, cnt); /* Copy stuff over */
280
281 cur_virt_src +=cnt;
282 cur_phys_dst +=cnt;
283 resid -= cnt;
284 }
285
286 exit:
287 return (len - resid);
288 }
289
290 int
291 kdp_machine_ioport_read(kdp_readioport_req_t *rq, caddr_t data, uint16_t lcpu)
292 {
293 uint16_t addr = rq->address;
294 uint16_t size = rq->nbytes;
295
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);
298 }
299
300 switch (size)
301 {
302 case 1:
303 *((uint8_t *) data) = inb(addr);
304 break;
305 case 2:
306 *((uint16_t *) data) = inw(addr);
307 break;
308 case 4:
309 *((uint32_t *) data) = inl(addr);
310 break;
311 default:
312 return KDPERR_BADFLAVOR;
313 break;
314 }
315
316 return KDPERR_NO_ERROR;
317 }
318
319 int
320 kdp_machine_ioport_write(kdp_writeioport_req_t *rq, caddr_t data, uint16_t lcpu)
321 {
322 uint16_t addr = rq->address;
323 uint16_t size = rq->nbytes;
324
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);
327 }
328
329 switch (size)
330 {
331 case 1:
332 outb(addr, *((uint8_t *) data));
333 break;
334 case 2:
335 outw(addr, *((uint16_t *) data));
336 break;
337 case 4:
338 outl(addr, *((uint32_t *) data));
339 break;
340 default:
341 return KDPERR_BADFLAVOR;
342 break;
343 }
344
345 return KDPERR_NO_ERROR;
346 }
347
348 int
349 kdp_machine_msr64_read(kdp_readmsr64_req_t *rq, caddr_t data, uint16_t lcpu)
350 {
351 uint64_t *value = (uint64_t *) data;
352 uint32_t msr = rq->address;
353
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);
356 }
357
358 *value = rdmsr64(msr);
359 return KDPERR_NO_ERROR;
360 }
361
362 int
363 kdp_machine_msr64_write(kdp_writemsr64_req_t *rq, caddr_t data, uint16_t lcpu)
364 {
365 uint64_t *value = (uint64_t *) data;
366 uint32_t msr = rq->address;
367
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);
370 }
371
372 wrmsr64(msr, *value);
373 return KDPERR_NO_ERROR;
374 }