]>
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 | ||
666 | /* Sanity and security check for addresses to/from a user */ | |
667 | if ((copy_type == COPYIN || | |
668 | copy_type == COPYINSTR || | |
669 | copy_type == COPYOUT) && | |
670 | (pmap != kernel_pmap) && | |
671 | ((vm_offset_t)kernel_addr < VM_MIN_KERNEL_AND_KEXT_ADDRESS || | |
672 | !IS_USERADDR64_CANONICAL(user_addr))) { | |
673 | error = EACCES; | |
674 | goto out; | |
675 | } | |
676 | ||
677 | /* | |
678 | * If the no_shared_cr3 boot-arg is set (true), the kernel runs on | |
679 | * its own pmap and cr3 rather than the user's -- so that wild accesses | |
680 | * from kernel or kexts can be trapped. So, during copyin and copyout, | |
681 | * we need to switch back to the user's map/cr3. The thread is flagged | |
682 | * "CopyIOActive" at this time so that if the thread is pre-empted, | |
683 | * we will later restore the correct cr3. | |
684 | */ | |
685 | recursive_CopyIOActive = thread->machine.specFlags & CopyIOActive; | |
686 | thread->machine.specFlags |= CopyIOActive; | |
687 | if (no_shared_cr3) { | |
688 | istate = ml_set_interrupts_enabled(FALSE); | |
689 | if (get_cr3() != pmap->pm_cr3) | |
690 | set_cr3(pmap->pm_cr3); | |
691 | } | |
692 | ||
693 | /* | |
694 | * Ensure that we're running on the target thread's cr3. | |
695 | */ | |
696 | if ((pmap != kernel_pmap) && !use_kernel_map && | |
697 | (get_cr3() != pmap->pm_cr3)) { | |
698 | panic("copyio(%d,%p,%p,%ld,%p,%d) cr3 is %p expects %p", | |
699 | copy_type, (void *)user_addr, kernel_addr, nbytes, lencopied, use_kernel_map, | |
700 | (void *) get_cr3(), (void *) pmap->pm_cr3); | |
701 | } | |
702 | if (no_shared_cr3) | |
703 | (void) ml_set_interrupts_enabled(istate); | |
704 | ||
705 | KERNEL_DEBUG(0xeff70044 | DBG_FUNC_NONE, (unsigned)user_addr, | |
706 | (unsigned)kernel_addr, nbytes, 0, 0); | |
707 | ||
708 | switch (copy_type) { | |
709 | ||
710 | case COPYIN: | |
711 | error = _bcopy((const void *) user_addr, | |
712 | kernel_addr, | |
713 | nbytes); | |
714 | break; | |
715 | ||
716 | case COPYOUT: | |
717 | error = _bcopy(kernel_addr, | |
718 | (void *) user_addr, | |
719 | nbytes); | |
720 | break; | |
721 | ||
722 | case COPYINPHYS: | |
723 | error = _bcopy((const void *) user_addr, | |
724 | PHYSMAP_PTOV(kernel_addr), | |
725 | nbytes); | |
726 | break; | |
727 | ||
728 | case COPYOUTPHYS: | |
729 | error = _bcopy((const void *) PHYSMAP_PTOV(kernel_addr), | |
730 | (void *) user_addr, | |
731 | nbytes); | |
732 | break; | |
733 | ||
734 | case COPYINSTR: | |
735 | error = _bcopystr((const void *) user_addr, | |
736 | kernel_addr, | |
737 | (int) nbytes, | |
738 | &bytes_copied); | |
739 | ||
740 | /* | |
741 | * lencopied should be updated on success | |
742 | * or ENAMETOOLONG... but not EFAULT | |
743 | */ | |
744 | if (error != EFAULT) | |
745 | *lencopied = bytes_copied; | |
746 | ||
747 | if (error) { | |
748 | #if KDEBUG | |
749 | nbytes = *lencopied; | |
750 | #endif | |
751 | break; | |
752 | } | |
753 | if (*(kernel_addr + bytes_copied - 1) == 0) { | |
754 | /* | |
755 | * we found a NULL terminator... we're done | |
756 | */ | |
757 | #if KDEBUG | |
758 | nbytes = *lencopied; | |
759 | #endif | |
760 | break; | |
761 | } else { | |
762 | /* | |
763 | * no more room in the buffer and we haven't | |
764 | * yet come across a NULL terminator | |
765 | */ | |
766 | #if KDEBUG | |
767 | nbytes = *lencopied; | |
768 | #endif | |
769 | error = ENAMETOOLONG; | |
770 | break; | |
771 | } | |
772 | break; | |
773 | } | |
774 | ||
775 | if (!recursive_CopyIOActive) | |
776 | thread->machine.specFlags &= ~CopyIOActive; | |
777 | if (no_shared_cr3) { | |
778 | istate = ml_set_interrupts_enabled(FALSE); | |
779 | if (get_cr3() != kernel_pmap->pm_cr3) | |
780 | set_cr3(kernel_pmap->pm_cr3); | |
781 | (void) ml_set_interrupts_enabled(istate); | |
782 | } | |
783 | ||
784 | out: | |
785 | KERNEL_DEBUG(debug_type | DBG_FUNC_END, (unsigned)user_addr, | |
786 | (unsigned)kernel_addr, (unsigned)nbytes, error, 0); | |
787 | ||
788 | return (error); | |
789 | } | |
790 | ||
791 | ||
792 | static int | |
793 | copyio_phys(addr64_t source, addr64_t sink, vm_size_t csize, int which) | |
794 | { | |
795 | char *paddr; | |
796 | user_addr_t vaddr; | |
797 | int ctype; | |
798 | ||
799 | if (which & cppvPsnk) { | |
800 | paddr = (char *)sink; | |
801 | vaddr = (user_addr_t)source; | |
802 | ctype = COPYINPHYS; | |
803 | } else { | |
804 | paddr = (char *)source; | |
805 | vaddr = (user_addr_t)sink; | |
806 | ctype = COPYOUTPHYS; | |
807 | } | |
808 | return copyio(ctype, vaddr, paddr, csize, NULL, which & cppvKmap); | |
809 | } | |
810 | ||
811 | int | |
812 | copyinmsg(const user_addr_t user_addr, char *kernel_addr, mach_msg_size_t nbytes) | |
813 | { | |
814 | return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0); | |
815 | } | |
816 | ||
817 | int | |
818 | copyin(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes) | |
819 | { | |
820 | return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0); | |
821 | } | |
822 | ||
823 | int | |
824 | copyinstr(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes, vm_size_t *lencopied) | |
825 | { | |
826 | *lencopied = 0; | |
827 | ||
828 | return copyio(COPYINSTR, user_addr, kernel_addr, nbytes, lencopied, 0); | |
829 | } | |
830 | ||
831 | int | |
832 | copyoutmsg(const char *kernel_addr, user_addr_t user_addr, mach_msg_size_t nbytes) | |
833 | { | |
834 | return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0); | |
835 | } | |
836 | ||
837 | int | |
838 | copyout(const void *kernel_addr, user_addr_t user_addr, vm_size_t nbytes) | |
839 | { | |
840 | return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0); | |
841 | } | |
842 | ||
843 | ||
844 | kern_return_t | |
845 | copypv(addr64_t src64, addr64_t snk64, unsigned int size, int which) | |
846 | { | |
847 | unsigned int lop, csize; | |
848 | int bothphys = 0; | |
849 | ||
850 | KERNEL_DEBUG(0xeff7004c | DBG_FUNC_START, (unsigned)src64, | |
851 | (unsigned)snk64, size, which, 0); | |
852 | ||
853 | if ((which & (cppvPsrc | cppvPsnk)) == 0 ) /* Make sure that only one is virtual */ | |
854 | panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */ | |
855 | ||
856 | if ((which & (cppvPsrc | cppvPsnk)) == (cppvPsrc | cppvPsnk)) | |
857 | bothphys = 1; /* both are physical */ | |
858 | ||
859 | while (size) { | |
860 | ||
861 | if (bothphys) { | |
862 | lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); /* Assume sink smallest */ | |
863 | ||
864 | if (lop > (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)))) | |
865 | lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); /* No, source is smaller */ | |
866 | } else { | |
867 | /* | |
868 | * only need to compute the resid for the physical page | |
869 | * address... we don't care about where we start/finish in | |
870 | * the virtual since we just call the normal copyin/copyout | |
871 | */ | |
872 | if (which & cppvPsrc) | |
873 | lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); | |
874 | else | |
875 | lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); | |
876 | } | |
877 | csize = size; /* Assume we can copy it all */ | |
878 | if (lop < size) | |
879 | csize = lop; /* Nope, we can't do it all */ | |
880 | #if 0 | |
881 | /* | |
882 | * flush_dcache64 is currently a nop on the i386... | |
883 | * it's used when copying to non-system memory such | |
884 | * as video capture cards... on PPC there was a need | |
885 | * to flush due to how we mapped this memory... not | |
886 | * sure if it's needed on i386. | |
887 | */ | |
888 | if (which & cppvFsrc) | |
889 | flush_dcache64(src64, csize, 1); /* If requested, flush source before move */ | |
890 | if (which & cppvFsnk) | |
891 | flush_dcache64(snk64, csize, 1); /* If requested, flush sink before move */ | |
892 | #endif | |
893 | if (bothphys) | |
894 | bcopy_phys(src64, snk64, csize); /* Do a physical copy, virtually */ | |
895 | else { | |
896 | if (copyio_phys(src64, snk64, csize, which)) | |
897 | return (KERN_FAILURE); | |
898 | } | |
899 | #if 0 | |
900 | if (which & cppvFsrc) | |
901 | flush_dcache64(src64, csize, 1); /* If requested, flush source after move */ | |
902 | if (which & cppvFsnk) | |
903 | flush_dcache64(snk64, csize, 1); /* If requested, flush sink after move */ | |
904 | #endif | |
905 | size -= csize; /* Calculate what is left */ | |
906 | snk64 += csize; /* Bump sink to next physical address */ | |
907 | src64 += csize; /* Bump source to next physical address */ | |
908 | } | |
909 | KERNEL_DEBUG(0xeff7004c | DBG_FUNC_END, (unsigned)src64, | |
910 | (unsigned)snk64, size, which, 0); | |
911 | ||
912 | return KERN_SUCCESS; | |
913 | } | |
914 | ||
915 | #if !MACH_KDP | |
916 | void | |
917 | kdp_register_callout(void) | |
918 | { | |
919 | } | |
920 | #endif | |
921 | ||
922 | #if !CONFIG_VMX | |
923 | int host_vmxon(boolean_t exclusive __unused) | |
924 | { | |
925 | return VMX_UNSUPPORTED; | |
926 | } | |
927 | ||
928 | void host_vmxoff(void) | |
929 | { | |
930 | return; | |
931 | } | |
932 | #endif |