]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2000-2006 Apple Computer, 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 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | #include <mach_assert.h> | |
59 | ||
60 | #include <string.h> | |
61 | #include <mach/boolean.h> | |
62 | #include <mach/i386/vm_types.h> | |
63 | #include <mach/i386/vm_param.h> | |
64 | #include <kern/kern_types.h> | |
65 | #include <kern/misc_protos.h> | |
66 | #include <sys/errno.h> | |
67 | #include <i386/param.h> | |
68 | #include <i386/misc_protos.h> | |
69 | #include <i386/cpu_data.h> | |
70 | #include <i386/machine_routines.h> | |
71 | #include <i386/cpuid.h> | |
72 | #include <i386/vmx.h> | |
73 | #include <vm/pmap.h> | |
74 | #include <vm/vm_map.h> | |
75 | #include <vm/vm_kern.h> | |
76 | #include <vm/vm_fault.h> | |
77 | ||
78 | #include <libkern/OSAtomic.h> | |
79 | #include <sys/kdebug.h> | |
80 | ||
81 | #if 0 | |
82 | ||
83 | #undef KERNEL_DEBUG | |
84 | #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT | |
85 | #define KDEBUG 1 | |
86 | ||
87 | #endif | |
88 | ||
89 | /* XXX - should be gone from here */ | |
90 | extern void invalidate_icache64(addr64_t addr, unsigned cnt, int phys); | |
91 | extern void flush_dcache64(addr64_t addr, unsigned count, int phys); | |
92 | extern boolean_t phys_page_exists(ppnum_t); | |
93 | extern void bcopy_no_overwrite(const char *from, char *to,vm_size_t bytes); | |
94 | extern void pmap_set_reference(ppnum_t pn); | |
95 | extern void mapping_set_mod(ppnum_t pa); | |
96 | extern void mapping_set_ref(ppnum_t pn); | |
97 | ||
98 | extern void ovbcopy(const char *from, | |
99 | char *to, | |
100 | vm_size_t nbytes); | |
101 | void machine_callstack(natural_t *buf, vm_size_t callstack_max); | |
102 | ||
103 | ||
104 | #define value_64bit(value) ((value) & 0xFFFFFFFF00000000ULL) | |
105 | #define low32(x) ((unsigned int)((x) & 0x00000000FFFFFFFFULL)) | |
106 | ||
107 | #define INT_SIZE (BYTE_SIZE * sizeof (int)) | |
108 | ||
109 | /* | |
110 | * Set indicated bit in bit string. | |
111 | */ | |
112 | void | |
113 | setbit(int bitno, int *s) | |
114 | { | |
115 | s[bitno / INT_SIZE] |= 1 << (bitno % INT_SIZE); | |
116 | } | |
117 | ||
118 | /* | |
119 | * Clear indicated bit in bit string. | |
120 | */ | |
121 | void | |
122 | clrbit(int bitno, int *s) | |
123 | { | |
124 | s[bitno / INT_SIZE] &= ~(1 << (bitno % INT_SIZE)); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Test if indicated bit is set in bit string. | |
129 | */ | |
130 | int | |
131 | testbit(int bitno, int *s) | |
132 | { | |
133 | return s[bitno / INT_SIZE] & (1 << (bitno % INT_SIZE)); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Find first bit set in bit string. | |
138 | */ | |
139 | int | |
140 | ffsbit(int *s) | |
141 | { | |
142 | int offset; | |
143 | ||
144 | for (offset = 0; !*s; offset += (int)INT_SIZE, ++s); | |
145 | return offset + __builtin_ctz(*s); | |
146 | } | |
147 | ||
148 | int | |
149 | ffs(unsigned int mask) | |
150 | { | |
151 | if (mask == 0) | |
152 | return 0; | |
153 | ||
154 | /* | |
155 | * NOTE: cannot use __builtin_ffs because it generates a call to | |
156 | * 'ffs' | |
157 | */ | |
158 | return 1 + __builtin_ctz(mask); | |
159 | } | |
160 | ||
161 | void | |
162 | bzero_phys_nc( | |
163 | addr64_t src64, | |
164 | uint32_t bytes) | |
165 | { | |
166 | bzero_phys(src64,bytes); | |
167 | } | |
168 | ||
169 | void | |
170 | bzero_phys( | |
171 | addr64_t src64, | |
172 | uint32_t bytes) | |
173 | { | |
174 | bzero(PHYSMAP_PTOV(src64), bytes); | |
175 | } | |
176 | ||
177 | ||
178 | /* | |
179 | * bcopy_phys - like bcopy but copies from/to physical addresses. | |
180 | */ | |
181 | ||
182 | void | |
183 | bcopy_phys( | |
184 | addr64_t src64, | |
185 | addr64_t dst64, | |
186 | vm_size_t bytes) | |
187 | { | |
188 | /* Not necessary for K64 - but ensure we stay within a page */ | |
189 | if (((((uint32_t)src64 & (NBPG-1)) + bytes) > NBPG) || | |
190 | ((((uint32_t)dst64 & (NBPG-1)) + bytes) > NBPG) ) { | |
191 | panic("bcopy_phys alignment"); | |
192 | } | |
193 | bcopy(PHYSMAP_PTOV(src64), PHYSMAP_PTOV(dst64), bytes); | |
194 | } | |
195 | ||
196 | /* | |
197 | * ovbcopy - like bcopy, but recognizes overlapping ranges and handles | |
198 | * them correctly. | |
199 | */ | |
200 | ||
201 | void | |
202 | ovbcopy( | |
203 | const char *from, | |
204 | char *to, | |
205 | vm_size_t bytes) /* num bytes to copy */ | |
206 | { | |
207 | /* Assume that bcopy copies left-to-right (low addr first). */ | |
208 | if (from + bytes <= to || to + bytes <= from || to == from) | |
209 | bcopy_no_overwrite(from, to, bytes); /* non-overlapping or no-op*/ | |
210 | else if (from > to) | |
211 | bcopy_no_overwrite(from, to, bytes); /* overlapping but OK */ | |
212 | else { | |
213 | /* to > from: overlapping, and must copy right-to-left. */ | |
214 | from += bytes - 1; | |
215 | to += bytes - 1; | |
216 | while (bytes-- > 0) | |
217 | *to-- = *from--; | |
218 | } | |
219 | } | |
220 | ||
221 | ||
222 | /* | |
223 | * Read data from a physical address. Memory should not be cache inhibited. | |
224 | */ | |
225 | ||
226 | ||
227 | static unsigned int | |
228 | ml_phys_read_data(pmap_paddr_t paddr, int size) | |
229 | { | |
230 | unsigned int result; | |
231 | ||
232 | switch (size) { | |
233 | unsigned char s1; | |
234 | unsigned short s2; | |
235 | case 1: | |
236 | s1 = *(unsigned char *)PHYSMAP_PTOV(paddr); | |
237 | result = s1; | |
238 | break; | |
239 | case 2: | |
240 | s2 = *(unsigned short *)PHYSMAP_PTOV(paddr); | |
241 | result = s2; | |
242 | break; | |
243 | case 4: | |
244 | default: | |
245 | result = *(unsigned int *)PHYSMAP_PTOV(paddr); | |
246 | break; | |
247 | } | |
248 | ||
249 | return result; | |
250 | } | |
251 | ||
252 | static unsigned long long | |
253 | ml_phys_read_long_long(pmap_paddr_t paddr ) | |
254 | { | |
255 | return *(unsigned long long *)PHYSMAP_PTOV(paddr); | |
256 | } | |
257 | ||
258 | ||
259 | ||
260 | unsigned int ml_phys_read( vm_offset_t paddr) | |
261 | { | |
262 | return ml_phys_read_data((pmap_paddr_t)paddr, 4); | |
263 | } | |
264 | ||
265 | unsigned int ml_phys_read_word(vm_offset_t paddr) { | |
266 | ||
267 | return ml_phys_read_data((pmap_paddr_t)paddr, 4); | |
268 | } | |
269 | ||
270 | unsigned int ml_phys_read_64(addr64_t paddr64) | |
271 | { | |
272 | return ml_phys_read_data((pmap_paddr_t)paddr64, 4); | |
273 | } | |
274 | ||
275 | unsigned int ml_phys_read_word_64(addr64_t paddr64) | |
276 | { | |
277 | return ml_phys_read_data((pmap_paddr_t)paddr64, 4); | |
278 | } | |
279 | ||
280 | unsigned int ml_phys_read_half(vm_offset_t paddr) | |
281 | { | |
282 | return ml_phys_read_data((pmap_paddr_t)paddr, 2); | |
283 | } | |
284 | ||
285 | unsigned int ml_phys_read_half_64(addr64_t paddr64) | |
286 | { | |
287 | return ml_phys_read_data((pmap_paddr_t)paddr64, 2); | |
288 | } | |
289 | ||
290 | unsigned int ml_phys_read_byte(vm_offset_t paddr) | |
291 | { | |
292 | return ml_phys_read_data((pmap_paddr_t)paddr, 1); | |
293 | } | |
294 | ||
295 | unsigned int ml_phys_read_byte_64(addr64_t paddr64) | |
296 | { | |
297 | return ml_phys_read_data((pmap_paddr_t)paddr64, 1); | |
298 | } | |
299 | ||
300 | unsigned long long ml_phys_read_double(vm_offset_t paddr) | |
301 | { | |
302 | return ml_phys_read_long_long((pmap_paddr_t)paddr); | |
303 | } | |
304 | ||
305 | unsigned long long ml_phys_read_double_64(addr64_t paddr64) | |
306 | { | |
307 | return ml_phys_read_long_long((pmap_paddr_t)paddr64); | |
308 | } | |
309 | ||
310 | ||
311 | ||
312 | /* | |
313 | * Write data to a physical address. Memory should not be cache inhibited. | |
314 | */ | |
315 | ||
316 | static void | |
317 | ml_phys_write_data(pmap_paddr_t paddr, unsigned long data, int size) | |
318 | { | |
319 | switch (size) { | |
320 | case 1: | |
321 | *(unsigned char *)PHYSMAP_PTOV(paddr) = (unsigned char)data; | |
322 | break; | |
323 | case 2: | |
324 | *(unsigned short *)PHYSMAP_PTOV(paddr) = (unsigned short)data; | |
325 | break; | |
326 | case 4: | |
327 | default: | |
328 | *(unsigned int *)PHYSMAP_PTOV(paddr) = (unsigned int)data; | |
329 | break; | |
330 | } | |
331 | } | |
332 | ||
333 | static void | |
334 | ml_phys_write_long_long(pmap_paddr_t paddr, unsigned long long data) | |
335 | { | |
336 | *(unsigned long long *)PHYSMAP_PTOV(paddr) = data; | |
337 | } | |
338 | ||
339 | ||
340 | ||
341 | void ml_phys_write_byte(vm_offset_t paddr, unsigned int data) | |
342 | { | |
343 | ml_phys_write_data((pmap_paddr_t)paddr, data, 1); | |
344 | } | |
345 | ||
346 | void ml_phys_write_byte_64(addr64_t paddr64, unsigned int data) | |
347 | { | |
348 | ml_phys_write_data((pmap_paddr_t)paddr64, data, 1); | |
349 | } | |
350 | ||
351 | void ml_phys_write_half(vm_offset_t paddr, unsigned int data) | |
352 | { | |
353 | ml_phys_write_data((pmap_paddr_t)paddr, data, 2); | |
354 | } | |
355 | ||
356 | void ml_phys_write_half_64(addr64_t paddr64, unsigned int data) | |
357 | { | |
358 | ml_phys_write_data((pmap_paddr_t)paddr64, data, 2); | |
359 | } | |
360 | ||
361 | void ml_phys_write(vm_offset_t paddr, unsigned int data) | |
362 | { | |
363 | ml_phys_write_data((pmap_paddr_t)paddr, data, 4); | |
364 | } | |
365 | ||
366 | void ml_phys_write_64(addr64_t paddr64, unsigned int data) | |
367 | { | |
368 | ml_phys_write_data((pmap_paddr_t)paddr64, data, 4); | |
369 | } | |
370 | ||
371 | void ml_phys_write_word(vm_offset_t paddr, unsigned int data) | |
372 | { | |
373 | ml_phys_write_data((pmap_paddr_t)paddr, data, 4); | |
374 | } | |
375 | ||
376 | void ml_phys_write_word_64(addr64_t paddr64, unsigned int data) | |
377 | { | |
378 | ml_phys_write_data((pmap_paddr_t)paddr64, data, 4); | |
379 | } | |
380 | ||
381 | void ml_phys_write_double(vm_offset_t paddr, unsigned long long data) | |
382 | { | |
383 | ml_phys_write_long_long((pmap_paddr_t)paddr, data); | |
384 | } | |
385 | ||
386 | void ml_phys_write_double_64(addr64_t paddr64, unsigned long long data) | |
387 | { | |
388 | ml_phys_write_long_long((pmap_paddr_t)paddr64, data); | |
389 | } | |
390 | ||
391 | ||
392 | /* PCI config cycle probing | |
393 | * | |
394 | * | |
395 | * Read the memory location at physical address paddr. | |
396 | * This is a part of a device probe, so there is a good chance we will | |
397 | * have a machine check here. So we have to be able to handle that. | |
398 | * We assume that machine checks are enabled both in MSR and HIDs | |
399 | */ | |
400 | ||
401 | boolean_t | |
402 | ml_probe_read(vm_offset_t paddr, unsigned int *val) | |
403 | { | |
404 | if ((PAGE_SIZE - (paddr & PAGE_MASK)) < 4) | |
405 | return FALSE; | |
406 | ||
407 | *val = ml_phys_read((pmap_paddr_t)paddr); | |
408 | ||
409 | return TRUE; | |
410 | } | |
411 | ||
412 | /* | |
413 | * Read the memory location at physical address paddr. | |
414 | * This is a part of a device probe, so there is a good chance we will | |
415 | * have a machine check here. So we have to be able to handle that. | |
416 | * We assume that machine checks are enabled both in MSR and HIDs | |
417 | */ | |
418 | boolean_t | |
419 | ml_probe_read_64(addr64_t paddr64, unsigned int *val) | |
420 | { | |
421 | if ((PAGE_SIZE - (paddr64 & PAGE_MASK)) < 4) | |
422 | return FALSE; | |
423 | ||
424 | *val = ml_phys_read_64((pmap_paddr_t)paddr64); | |
425 | return TRUE; | |
426 | } | |
427 | ||
428 | ||
429 | int bcmp( | |
430 | const void *pa, | |
431 | const void *pb, | |
432 | size_t len) | |
433 | { | |
434 | const char *a = (const char *)pa; | |
435 | const char *b = (const char *)pb; | |
436 | ||
437 | if (len == 0) | |
438 | return 0; | |
439 | ||
440 | do | |
441 | if (*a++ != *b++) | |
442 | break; | |
443 | while (--len); | |
444 | ||
445 | return (int)len; | |
446 | } | |
447 | ||
448 | int | |
449 | memcmp(const void *s1, const void *s2, size_t n) | |
450 | { | |
451 | if (n != 0) { | |
452 | const unsigned char *p1 = s1, *p2 = s2; | |
453 | ||
454 | do { | |
455 | if (*p1++ != *p2++) | |
456 | return (*--p1 - *--p2); | |
457 | } while (--n != 0); | |
458 | } | |
459 | return (0); | |
460 | } | |
461 | ||
462 | /* | |
463 | * Abstract: | |
464 | * strlen returns the number of characters in "string" preceeding | |
465 | * the terminating null character. | |
466 | */ | |
467 | ||
468 | size_t | |
469 | strlen( | |
470 | register const char *string) | |
471 | { | |
472 | register const char *ret = string; | |
473 | ||
474 | while (*string++ != '\0') | |
475 | continue; | |
476 | return string - 1 - ret; | |
477 | } | |
478 | ||
479 | uint32_t | |
480 | hw_compare_and_store(uint32_t oldval, uint32_t newval, volatile uint32_t *dest) | |
481 | { | |
482 | return OSCompareAndSwap((UInt32)oldval, | |
483 | (UInt32)newval, | |
484 | (volatile UInt32 *)dest); | |
485 | } | |
486 | ||
487 | #if MACH_ASSERT | |
488 | ||
489 | /* | |
490 | * Machine-dependent routine to fill in an array with up to callstack_max | |
491 | * levels of return pc information. | |
492 | */ | |
493 | void machine_callstack( | |
494 | __unused natural_t *buf, | |
495 | __unused vm_size_t callstack_max) | |
496 | { | |
497 | } | |
498 | ||
499 | #endif /* MACH_ASSERT */ | |
500 | ||
501 | void fillPage(ppnum_t pa, unsigned int fill) | |
502 | { | |
503 | pmap_paddr_t src; | |
504 | int i; | |
505 | int cnt = PAGE_SIZE / sizeof(unsigned int); | |
506 | unsigned int *addr; | |
507 | ||
508 | src = i386_ptob(pa); | |
509 | for (i = 0, addr = (unsigned int *)PHYSMAP_PTOV(src); i < cnt; i++) | |
510 | *addr++ = fill; | |
511 | } | |
512 | ||
513 | static inline void __sfence(void) | |
514 | { | |
515 | __asm__ volatile("sfence"); | |
516 | } | |
517 | static inline void __mfence(void) | |
518 | { | |
519 | __asm__ volatile("mfence"); | |
520 | } | |
521 | static inline void __wbinvd(void) | |
522 | { | |
523 | __asm__ volatile("wbinvd"); | |
524 | } | |
525 | static inline void __clflush(void *ptr) | |
526 | { | |
527 | __asm__ volatile("clflush (%0)" : : "r" (ptr)); | |
528 | } | |
529 | ||
530 | void dcache_incoherent_io_store64(addr64_t pa, unsigned int count) | |
531 | { | |
532 | uint32_t linesize = cpuid_info()->cache_linesize; | |
533 | addr64_t addr; | |
534 | boolean_t istate; | |
535 | ||
536 | __mfence(); | |
537 | ||
538 | istate = ml_set_interrupts_enabled(FALSE); | |
539 | ||
540 | for (addr = pa; addr < pa + count; addr += linesize) | |
541 | __clflush(PHYSMAP_PTOV(addr)); | |
542 | ||
543 | (void) ml_set_interrupts_enabled(istate); | |
544 | ||
545 | __mfence(); | |
546 | } | |
547 | ||
548 | void dcache_incoherent_io_flush64(addr64_t pa, unsigned int count) | |
549 | { | |
550 | return(dcache_incoherent_io_store64(pa,count)); | |
551 | } | |
552 | ||
553 | void | |
554 | flush_dcache64(__unused addr64_t addr, | |
555 | __unused unsigned count, | |
556 | __unused int phys) | |
557 | { | |
558 | } | |
559 | ||
560 | void | |
561 | invalidate_icache64(__unused addr64_t addr, | |
562 | __unused unsigned count, | |
563 | __unused int phys) | |
564 | { | |
565 | } | |
566 | ||
567 | ||
568 | addr64_t vm_last_addr; | |
569 | ||
570 | void | |
571 | mapping_set_mod(ppnum_t pn) | |
572 | { | |
573 | pmap_set_modify(pn); | |
574 | } | |
575 | ||
576 | void | |
577 | mapping_set_ref(ppnum_t pn) | |
578 | { | |
579 | pmap_set_reference(pn); | |
580 | } | |
581 | ||
582 | void | |
583 | cache_flush_page_phys(ppnum_t pa) | |
584 | { | |
585 | boolean_t istate; | |
586 | unsigned char *cacheline_addr; | |
587 | int cacheline_size = cpuid_info()->cache_linesize; | |
588 | int cachelines_to_flush = PAGE_SIZE/cacheline_size; | |
589 | ||
590 | __mfence(); | |
591 | ||
592 | istate = ml_set_interrupts_enabled(FALSE); | |
593 | ||
594 | for (cacheline_addr = (unsigned char *)PHYSMAP_PTOV(i386_ptob(pa)); | |
595 | cachelines_to_flush > 0; | |
596 | cachelines_to_flush--, cacheline_addr += cacheline_size) { | |
597 | __clflush((void *) cacheline_addr); | |
598 | } | |
599 | ||
600 | (void) ml_set_interrupts_enabled(istate); | |
601 | ||
602 | __mfence(); | |
603 | } | |
604 | ||
605 | ||
606 | static int copyio(int, user_addr_t, char *, vm_size_t, vm_size_t *, int); | |
607 | static int copyio_phys(addr64_t, addr64_t, vm_size_t, int); | |
608 | ||
609 | /* | |
610 | * The copy engine has the following characteristics | |
611 | * - copyio() handles copies to/from user or kernel space | |
612 | * - copypv() deals with physical or virtual addresses | |
613 | * | |
614 | * Readers familiar with the 32-bit kernel will expect Joe's thesis at this | |
615 | * point describing the full glory of the copy window implementation. In K64, | |
616 | * however, there is no need for windowing. Thanks to the vast shared address | |
617 | * space, the kernel has direct access to userspace and to physical memory. | |
618 | * | |
619 | * User virtual addresses are accessible provided the user's cr3 is loaded. | |
620 | * Physical addresses are accessible via the direct map and the PHYSMAP_PTOV() | |
621 | * translation. | |
622 | * | |
623 | * Copyin/out variants all boil done to just these 2 routines in locore.s which | |
624 | * provide fault-recoverable copying: | |
625 | */ | |
626 | extern int _bcopy(const void *, void *, vm_size_t); | |
627 | extern int _bcopystr(const void *, void *, vm_size_t, vm_size_t *); | |
628 | ||
629 | ||
630 | /* | |
631 | * Types of copies: | |
632 | */ | |
633 | #define COPYIN 0 /* from user virtual to kernel virtual */ | |
634 | #define COPYOUT 1 /* from kernel virtual to user virtual */ | |
635 | #define COPYINSTR 2 /* string variant of copyout */ | |
636 | #define COPYINPHYS 3 /* from user virtual to kernel physical */ | |
637 | #define COPYOUTPHYS 4 /* from kernel physical to user virtual */ | |
638 | ||
639 | ||
640 | static int | |
641 | copyio(int copy_type, user_addr_t user_addr, char *kernel_addr, | |
642 | vm_size_t nbytes, vm_size_t *lencopied, int use_kernel_map) | |
643 | { | |
644 | thread_t thread; | |
645 | pmap_t pmap; | |
646 | vm_size_t bytes_copied; | |
647 | int error = 0; | |
648 | boolean_t istate = FALSE; | |
649 | boolean_t recursive_CopyIOActive; | |
650 | #if KDEBUG | |
651 | int debug_type = 0xeff70010; | |
652 | debug_type += (copy_type << 2); | |
653 | #endif | |
654 | ||
655 | thread = current_thread(); | |
656 | ||
657 | KERNEL_DEBUG(debug_type | DBG_FUNC_START, | |
658 | (unsigned)(user_addr >> 32), (unsigned)user_addr, | |
659 | nbytes, thread->machine.copyio_state, 0); | |
660 | ||
661 | if (nbytes == 0) | |
662 | goto out; | |
663 | ||
664 | pmap = thread->map->pmap; | |
665 | ||
b7266188 A |
666 | |
667 | assert((vm_offset_t)kernel_addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS || | |
668 | copy_type == COPYINPHYS || copy_type == COPYOUTPHYS); | |
669 | ||
b0d623f7 | 670 | /* Sanity and security check for addresses to/from a user */ |
b7266188 A |
671 | |
672 | if (((pmap != kernel_pmap) && (use_kernel_map == 0)) && | |
673 | ((nbytes && (user_addr+nbytes <= user_addr)) || ((user_addr + nbytes) > vm_map_max(thread->map)))) { | |
674 | error = EFAULT; | |
b0d623f7 A |
675 | goto out; |
676 | } | |
677 | ||
678 | /* | |
679 | * If the no_shared_cr3 boot-arg is set (true), the kernel runs on | |
680 | * its own pmap and cr3 rather than the user's -- so that wild accesses | |
681 | * from kernel or kexts can be trapped. So, during copyin and copyout, | |
682 | * we need to switch back to the user's map/cr3. The thread is flagged | |
683 | * "CopyIOActive" at this time so that if the thread is pre-empted, | |
684 | * we will later restore the correct cr3. | |
685 | */ | |
686 | recursive_CopyIOActive = thread->machine.specFlags & CopyIOActive; | |
687 | thread->machine.specFlags |= CopyIOActive; | |
688 | if (no_shared_cr3) { | |
689 | istate = ml_set_interrupts_enabled(FALSE); | |
690 | if (get_cr3() != pmap->pm_cr3) | |
691 | set_cr3(pmap->pm_cr3); | |
692 | } | |
693 | ||
694 | /* | |
695 | * Ensure that we're running on the target thread's cr3. | |
696 | */ | |
697 | if ((pmap != kernel_pmap) && !use_kernel_map && | |
698 | (get_cr3() != pmap->pm_cr3)) { | |
699 | panic("copyio(%d,%p,%p,%ld,%p,%d) cr3 is %p expects %p", | |
700 | copy_type, (void *)user_addr, kernel_addr, nbytes, lencopied, use_kernel_map, | |
701 | (void *) get_cr3(), (void *) pmap->pm_cr3); | |
702 | } | |
703 | if (no_shared_cr3) | |
704 | (void) ml_set_interrupts_enabled(istate); | |
705 | ||
706 | KERNEL_DEBUG(0xeff70044 | DBG_FUNC_NONE, (unsigned)user_addr, | |
707 | (unsigned)kernel_addr, nbytes, 0, 0); | |
708 | ||
709 | switch (copy_type) { | |
710 | ||
711 | case COPYIN: | |
712 | error = _bcopy((const void *) user_addr, | |
713 | kernel_addr, | |
714 | nbytes); | |
715 | break; | |
716 | ||
717 | case COPYOUT: | |
718 | error = _bcopy(kernel_addr, | |
719 | (void *) user_addr, | |
720 | nbytes); | |
721 | break; | |
722 | ||
723 | case COPYINPHYS: | |
724 | error = _bcopy((const void *) user_addr, | |
725 | PHYSMAP_PTOV(kernel_addr), | |
726 | nbytes); | |
727 | break; | |
728 | ||
729 | case COPYOUTPHYS: | |
730 | error = _bcopy((const void *) PHYSMAP_PTOV(kernel_addr), | |
731 | (void *) user_addr, | |
732 | nbytes); | |
733 | break; | |
734 | ||
735 | case COPYINSTR: | |
736 | error = _bcopystr((const void *) user_addr, | |
737 | kernel_addr, | |
738 | (int) nbytes, | |
739 | &bytes_copied); | |
740 | ||
741 | /* | |
742 | * lencopied should be updated on success | |
743 | * or ENAMETOOLONG... but not EFAULT | |
744 | */ | |
745 | if (error != EFAULT) | |
746 | *lencopied = bytes_copied; | |
747 | ||
748 | if (error) { | |
749 | #if KDEBUG | |
750 | nbytes = *lencopied; | |
751 | #endif | |
752 | break; | |
753 | } | |
754 | if (*(kernel_addr + bytes_copied - 1) == 0) { | |
755 | /* | |
756 | * we found a NULL terminator... we're done | |
757 | */ | |
758 | #if KDEBUG | |
759 | nbytes = *lencopied; | |
760 | #endif | |
761 | break; | |
762 | } else { | |
763 | /* | |
764 | * no more room in the buffer and we haven't | |
765 | * yet come across a NULL terminator | |
766 | */ | |
767 | #if KDEBUG | |
768 | nbytes = *lencopied; | |
769 | #endif | |
770 | error = ENAMETOOLONG; | |
771 | break; | |
772 | } | |
773 | break; | |
774 | } | |
775 | ||
776 | if (!recursive_CopyIOActive) | |
777 | thread->machine.specFlags &= ~CopyIOActive; | |
778 | if (no_shared_cr3) { | |
779 | istate = ml_set_interrupts_enabled(FALSE); | |
780 | if (get_cr3() != kernel_pmap->pm_cr3) | |
781 | set_cr3(kernel_pmap->pm_cr3); | |
782 | (void) ml_set_interrupts_enabled(istate); | |
783 | } | |
784 | ||
785 | out: | |
786 | KERNEL_DEBUG(debug_type | DBG_FUNC_END, (unsigned)user_addr, | |
787 | (unsigned)kernel_addr, (unsigned)nbytes, error, 0); | |
788 | ||
789 | return (error); | |
790 | } | |
791 | ||
792 | ||
793 | static int | |
794 | copyio_phys(addr64_t source, addr64_t sink, vm_size_t csize, int which) | |
795 | { | |
796 | char *paddr; | |
797 | user_addr_t vaddr; | |
798 | int ctype; | |
799 | ||
800 | if (which & cppvPsnk) { | |
801 | paddr = (char *)sink; | |
802 | vaddr = (user_addr_t)source; | |
803 | ctype = COPYINPHYS; | |
804 | } else { | |
805 | paddr = (char *)source; | |
806 | vaddr = (user_addr_t)sink; | |
807 | ctype = COPYOUTPHYS; | |
808 | } | |
809 | return copyio(ctype, vaddr, paddr, csize, NULL, which & cppvKmap); | |
810 | } | |
811 | ||
812 | int | |
813 | copyinmsg(const user_addr_t user_addr, char *kernel_addr, mach_msg_size_t nbytes) | |
814 | { | |
815 | return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0); | |
816 | } | |
817 | ||
818 | int | |
819 | copyin(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes) | |
820 | { | |
821 | return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0); | |
822 | } | |
823 | ||
824 | int | |
825 | copyinstr(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes, vm_size_t *lencopied) | |
826 | { | |
827 | *lencopied = 0; | |
828 | ||
829 | return copyio(COPYINSTR, user_addr, kernel_addr, nbytes, lencopied, 0); | |
830 | } | |
831 | ||
832 | int | |
833 | copyoutmsg(const char *kernel_addr, user_addr_t user_addr, mach_msg_size_t nbytes) | |
834 | { | |
835 | return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0); | |
836 | } | |
837 | ||
838 | int | |
839 | copyout(const void *kernel_addr, user_addr_t user_addr, vm_size_t nbytes) | |
840 | { | |
841 | return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0); | |
842 | } | |
843 | ||
844 | ||
845 | kern_return_t | |
846 | copypv(addr64_t src64, addr64_t snk64, unsigned int size, int which) | |
847 | { | |
848 | unsigned int lop, csize; | |
849 | int bothphys = 0; | |
850 | ||
851 | KERNEL_DEBUG(0xeff7004c | DBG_FUNC_START, (unsigned)src64, | |
852 | (unsigned)snk64, size, which, 0); | |
853 | ||
854 | if ((which & (cppvPsrc | cppvPsnk)) == 0 ) /* Make sure that only one is virtual */ | |
855 | panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */ | |
856 | ||
857 | if ((which & (cppvPsrc | cppvPsnk)) == (cppvPsrc | cppvPsnk)) | |
858 | bothphys = 1; /* both are physical */ | |
859 | ||
860 | while (size) { | |
861 | ||
862 | if (bothphys) { | |
863 | lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); /* Assume sink smallest */ | |
864 | ||
865 | if (lop > (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)))) | |
866 | lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); /* No, source is smaller */ | |
867 | } else { | |
868 | /* | |
869 | * only need to compute the resid for the physical page | |
870 | * address... we don't care about where we start/finish in | |
871 | * the virtual since we just call the normal copyin/copyout | |
872 | */ | |
873 | if (which & cppvPsrc) | |
874 | lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); | |
875 | else | |
876 | lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); | |
877 | } | |
878 | csize = size; /* Assume we can copy it all */ | |
879 | if (lop < size) | |
880 | csize = lop; /* Nope, we can't do it all */ | |
881 | #if 0 | |
882 | /* | |
883 | * flush_dcache64 is currently a nop on the i386... | |
884 | * it's used when copying to non-system memory such | |
885 | * as video capture cards... on PPC there was a need | |
886 | * to flush due to how we mapped this memory... not | |
887 | * sure if it's needed on i386. | |
888 | */ | |
889 | if (which & cppvFsrc) | |
890 | flush_dcache64(src64, csize, 1); /* If requested, flush source before move */ | |
891 | if (which & cppvFsnk) | |
892 | flush_dcache64(snk64, csize, 1); /* If requested, flush sink before move */ | |
893 | #endif | |
894 | if (bothphys) | |
895 | bcopy_phys(src64, snk64, csize); /* Do a physical copy, virtually */ | |
896 | else { | |
897 | if (copyio_phys(src64, snk64, csize, which)) | |
898 | return (KERN_FAILURE); | |
899 | } | |
900 | #if 0 | |
901 | if (which & cppvFsrc) | |
902 | flush_dcache64(src64, csize, 1); /* If requested, flush source after move */ | |
903 | if (which & cppvFsnk) | |
904 | flush_dcache64(snk64, csize, 1); /* If requested, flush sink after move */ | |
905 | #endif | |
906 | size -= csize; /* Calculate what is left */ | |
907 | snk64 += csize; /* Bump sink to next physical address */ | |
908 | src64 += csize; /* Bump source to next physical address */ | |
909 | } | |
910 | KERNEL_DEBUG(0xeff7004c | DBG_FUNC_END, (unsigned)src64, | |
911 | (unsigned)snk64, size, which, 0); | |
912 | ||
913 | return KERN_SUCCESS; | |
914 | } | |
915 | ||
916 | #if !MACH_KDP | |
917 | void | |
918 | kdp_register_callout(void) | |
919 | { | |
920 | } | |
921 | #endif | |
922 | ||
923 | #if !CONFIG_VMX | |
924 | int host_vmxon(boolean_t exclusive __unused) | |
925 | { | |
926 | return VMX_UNSUPPORTED; | |
927 | } | |
928 | ||
929 | void host_vmxoff(void) | |
930 | { | |
931 | return; | |
932 | } | |
933 | #endif |