2 * Copyright (c) 2000-2007 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 #include <mach_assert.h>
61 #include <sys/errno.h>
63 #include <i386/cpuid.h>
64 #include <i386/eflags.h>
65 #include <i386/postcode.h>
66 #include <i386/proc_reg.h>
67 #include <i386/trap.h>
69 #include <mach/exception_types.h>
70 #include <config_dtrace.h>
72 #define _ARCH_I386_ASM_HELP_H_ /* Prevent inclusion of user header */
73 #include <mach/i386/syscall_sw.h>
80 #define RECOVERY_SECTION .section __VECTORS, __recover
82 #define RECOVERY_SECTION .text
85 #define RECOVER_TABLE_START \
87 .globl EXT(recover_table) ;\
88 LEXT(recover_table) ;\
91 #define RECOVER(addr) \
98 #define RECOVER_TABLE_END \
100 .globl EXT(recover_table_end) ;\
101 LEXT(recover_table_end) ;\
105 * Allocate recovery and table.
111 * int rdmsr_carefully(uint32_t msr, uint32_t *lo, uint32_t *hi)
113 ENTRY(rdmsr_carefully)
128 * int rdmsr64_carefully(uint32_t msr, uint64_t *val);
131 ENTRY(rdmsr64_carefully)
134 RECOVER(rdmsr64_carefully_fail)
140 rdmsr64_carefully_fail:
144 * int wrmsr64_carefully(uint32_t msr, uint64_t val);
147 ENTRY(wrmsr_carefully)
168 .globl EXT(thread_exception_return_internal)
170 .globl EXT(thread_exception_return)
172 .globl EXT(thread_bootstrap_return)
173 LEXT(thread_bootstrap_return)
175 call EXT(dtrace_thread_bootstrap)
179 LEXT(thread_exception_return_internal)
181 LEXT(thread_exception_return)
184 xorl %ecx, %ecx /* don't check if we're in the PFZ */
185 jmp EXT(return_from_trap)
188 * Copyin/out from user/kernel address space.
189 * rdi: source address
190 * rsi: destination address
191 * rdx: byte count (in fact, always < 64MB -- see copyio)
194 xchg %rdi, %rsi /* source %rsi, dest %rdi */
197 mov %rdx, %rcx /* move by longwords first */
202 movsq /* move longwords */
204 movl %edx, %ecx /* now move remaining bytes */
211 xorl %eax,%eax /* return 0 for success */
215 movl $(EFAULT),%eax /* return error for failure */
218 Entry(pmap_safe_read)
220 RECOVER(_pmap_safe_read_fail)
225 _pmap_safe_read_fail:
230 * 2-byte copy used by ml_copy_phys().
231 * rdi: source address
232 * rsi: destination address
242 xorl %eax,%eax /* return 0 for success */
246 * 4-byte copy used by ml_copy_phys().
247 * rdi: source address
248 * rsi: destination address
258 xorl %eax,%eax /* return 0 for success */
262 * 8-byte copy used by ml_copy_phys().
263 * rdi: source address
264 * rsi: destination address
274 xorl %eax,%eax /* return 0 for success */
280 * Copyin string from user/kern address space.
281 * rdi: source address
282 * rsi: destination address
283 * rdx: max byte count
284 * rcx: actual byte count (OUT)
288 xchgq %rdi, %rsi /* source %rsi, dest %rdi */
290 xorl %eax,%eax /* set to 0 here so that high 24 bits */
291 /* are 0 for the cmpl against 0 */
294 RECOVER(_bcopystr_fail) /* copy bytes... */
297 testq %rdi,%rdi /* if kernel address is ... */
299 movb %al,(%rdi) /* copy the byte */
302 testl %eax,%eax /* did we just stuff the 0-byte? */
303 jz 4f /* yes, return 0 already in %eax */
304 decq %rdx /* decrement #bytes left in buffer */
305 jnz 2b /* buffer not full, copy another byte */
306 movl $(ENAMETOOLONG),%eax /* buffer full, no \0: ENAMETOOLONG */
308 cmpq $0,%rcx /* get OUT len ptr */
309 jz _bcopystr_ret /* if null, just return */
311 movq %rsi,(%rcx) /* else set OUT arg to xfer len */
312 popq %rdi /* restore registers */
317 popq %rdi /* restore registers */
318 movl $(EFAULT),%eax /* return error for failure */
322 * Copyin 32 bit aligned word as a single transaction
323 * rdi: source address (user)
324 * rsi: destination address (kernel)
326 Entry(_copyin_atomic32)
327 pushq %rbp /* Save registers */
330 RECOVER(L_copyin_atomic32_fail) /* Set up recovery handler for next instruction */
331 movl (%rdi), %eax /* Load long from user */
332 movl %eax, (%rsi) /* Store to kernel */
333 xorl %eax, %eax /* Return success */
334 popq %rbp /* Restore registers */
337 L_copyin_atomic32_fail:
338 movl $(EFAULT), %eax /* Return error for failure */
339 popq %rbp /* Restore registers */
343 * Copyin 64 bit aligned word as a single transaction
344 * rdi: source address (user)
345 * rsi: destination address (kernel)
347 Entry(_copyin_atomic64)
348 pushq %rbp /* Save registers */
351 RECOVER(L_copyin_atomic64_fail) /* Set up recovery handler for next instruction*/
352 movq (%rdi), %rax /* Load quad from user */
353 movq %rax, (%rsi) /* Store to kernel */
354 xorl %eax, %eax /* Return success */
355 popq %rbp /* Restore registers */
358 L_copyin_atomic64_fail:
359 movl $(EFAULT), %eax /* Return error for failure */
360 popq %rbp /* Restore registers */
364 * Copyin 32 bit aligned word as a single transaction
365 * rdi: source address (kernel)
366 * rsi: destination address (user)
368 Entry(_copyout_atomic32)
369 pushq %rbp /* Save registers */
371 movl (%rdi), %eax /* Load long from kernel */
373 RECOVER(L_copyout_atomic32_fail) /* Set up recovery handler for next instruction*/
374 movl %eax, (%rsi) /* Store long to user */
375 xorl %eax, %eax /* Return success */
376 popq %rbp /* Restore registers */
379 L_copyout_atomic32_fail:
380 movl $(EFAULT), %eax /* Return error for failure */
381 popq %rbp /* Restore registers */
385 * Copyin 64 bit aligned word as a single transaction
386 * rdi: source address (kernel)
387 * rsi: destination address (user)
389 Entry(_copyout_atomic64)
390 pushq %rbp /* Save registers */
392 movq (%rdi), %rax /* Load quad from kernel */
394 RECOVER(L_copyout_atomic64_fail) /* Set up recovery handler for next instruction*/
395 movq %rax, (%rsi) /* Store quad to user */
396 xorl %eax, %eax /* Return success */
397 popq %rbp /* Restore registers */
400 L_copyout_atomic64_fail:
401 movl $(EFAULT), %eax /* Return error for failure */
402 popq %rbp /* Restore registers */
407 * Done with recovery table.
414 * Vector here on any exception at startup prior to switching to
415 * the kernel's idle page-tables and installing the kernel master IDT.
417 Entry(vstart_trap_handler)
418 POSTCODE(BOOT_TRAP_HLT)