2 * Copyright (c) 2000-2009 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.
58 #include <platforms.h>
60 #include <mach_assert.h>
62 #include <sys/errno.h>
64 #include <i386/cpuid.h>
65 #include <i386/eflags.h>
66 #include <i386/proc_reg.h>
67 #include <i386/trap.h>
70 #include <config_dtrace.h>
73 * PTmap is recursive pagemap at top of virtual address space.
74 * Within PTmap, the page directory can be found (third indirection).
76 .globl _PTmap,_PTD,_PTDpde
77 .set _PTmap,(PTDPTDI << PDESHIFT)
78 .set _PTD,_PTmap + (PTDPTDI * NBPG)
79 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE)
82 /* Under Mach-O, etext is a variable which contains
83 * the last text address
85 #define ETEXT_ADDR (EXT(etext))
87 /* Under ELF and other non-Mach-O formats, the address of
88 * etext represents the last text address
90 #define ETEXT_ADDR $ EXT(etext)
102 #define RECOVERY_SECTION .section __VECTORS, __recover
104 #define RECOVERY_SECTION .text
105 #define RECOVERY_SECTION .text
108 #define RECOVER_TABLE_START \
110 .globl EXT(recover_table) ;\
111 LEXT(recover_table) ;\
114 #define RECOVER(addr) \
121 #define RECOVER_TABLE_END \
123 .globl EXT(recover_table_end) ;\
124 LEXT(recover_table_end) ;\
125 .long 0 /* workaround see comment below */ ;\
129 * the .long 0 is to work around a linker bug (insert radar# here)
130 * basically recover_table_end has zero size and bumps up right against saved_esp in acpi_wakeup.s
131 * recover_table_end is in __RECOVER,__vectors and saved_esp is in __SLEEP,__data, but they're right next to each
132 * other and so the linker combines them and incorrectly relocates everything referencing recover_table_end to point
133 * into the SLEEP section
137 * Allocate recovery and table.
144 * Called as a function, makes the current thread
145 * return from the kernel as if from an exception.
146 * We will consult with DTrace if this is a
147 * newly created thread and we need to fire a probe.
150 .globl EXT(thread_exception_return)
151 .globl EXT(thread_bootstrap_return)
152 LEXT(thread_bootstrap_return)
154 call EXT(dtrace_thread_bootstrap)
157 LEXT(thread_exception_return)
159 xorl %ecx,%ecx /* don't check if in the PFZ */
160 cmpl $0, %gs:CPU_IS64BIT
161 je EXT(return_from_trap32)
162 jmp EXT(return_from_trap)
170 * Copy from user/kernel address space.
171 * arg0: window offset or kernel address
172 * arg1: kernel address
175 Entry(copyinphys_user)
176 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
179 Entry(copyinphys_kern)
180 movl $(PHYS_WINDOW_SEL),%ecx /* physical access through kernel window */
185 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
192 pushl %edi /* save registers */
194 movl 8+S_ARG0,%esi /* get source - window offset or kernel address */
195 movl 8+S_ARG1,%edi /* get destination - kernel address */
196 movl 8+S_ARG2,%edx /* get count */
199 movl %edx,%ecx /* move by longwords first */
204 movsl /* move longwords */
205 movl %edx,%ecx /* now move remaining bytes */
211 xorl %eax,%eax /* return 0 for success */
213 mov %ss,%cx /* restore kernel data and extended segments */
217 popl %edi /* restore registers */
222 movl $(EFAULT),%eax /* return error for failure */
223 jmp copyin_ret /* pop frame and return */
228 * Copy string from user/kern address space.
229 * arg0: window offset or kernel address
230 * arg1: kernel address
231 * arg2: max byte count
232 * arg3: actual byte count (OUT)
234 Entry(copyinstr_kern)
238 Entry(copyinstr_user)
239 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
245 pushl %edi /* save registers */
247 movl 8+S_ARG0,%esi /* get source - window offset or kernel address */
248 movl 8+S_ARG1,%edi /* get destination - kernel address */
249 movl 8+S_ARG2,%edx /* get count */
251 xorl %eax,%eax /* set to 0 here so that the high 24 bits */
252 /* are 0 for the cmpl against 0 */
255 RECOVER(copystr_fail) /* copy bytes... */
258 testl %edi,%edi /* if kernel address is ... */
260 movb %al,(%edi) /* copy the byte */
263 testl %eax,%eax /* did we just stuff the 0-byte? */
264 jz 4f /* yes, return 0 status already in %eax */
265 decl %edx /* decrement #bytes left in buffer */
266 jnz 2b /* buffer not full so copy in another byte */
267 movl $(ENAMETOOLONG),%eax /* buffer full but no 0-byte: ENAMETOOLONG */
269 movl 8+S_ARG3,%edi /* get OUT len ptr */
271 jz copystr_ret /* if null, just return */
273 movl %esi,(%edi) /* else set OUT arg to xfer len */
275 popl %edi /* restore registers */
280 movl $(EFAULT),%eax /* return error for failure */
281 jmp copystr_ret /* pop frame and return */
285 * Copy to user/kern address space.
286 * arg0: kernel address
287 * arg1: window offset or kernel address
290 ENTRY(copyoutphys_user)
291 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
294 ENTRY(copyoutphys_kern)
295 movl $(PHYS_WINDOW_SEL),%ecx /* physical access through kernel window */
300 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
307 pushl %edi /* save registers */
309 movl 8+S_ARG0,%esi /* get source - kernel address */
310 movl 8+S_ARG1,%edi /* get destination - window offset or kernel address */
311 movl 8+S_ARG2,%edx /* get count */
314 movl %edx,%ecx /* move by longwords first */
317 RECOVER(copyout_fail)
320 movl %edx,%ecx /* now move remaining bytes */
323 RECOVER(copyout_fail)
326 xorl %eax,%eax /* return 0 for success */
328 mov %ss,%cx /* restore kernel segment */
332 popl %edi /* restore registers */
337 movl $(EFAULT),%eax /* return error for failure */
338 jmp copyout_ret /* pop frame and return */
342 * io register must not be used on slaves (no AT bus)
352 #define PUSH_FRAME FRAME
353 #define POP_FRAME EMARF
355 #else /* MACH_ASSERT */
363 #endif /* MACH_ASSERT */
367 * int rdmsr_carefully(uint32_t msr, uint32_t *lo, uint32_t *hi)
369 ENTRY(rdmsr_carefully)
386 * Done with recovery table.
412 lidt null_idtr /* disable the interrupt handler */
413 xor %ecx,%ecx /* generate a divide by zero */
414 div %ecx,%eax /* reboot now */
415 ret /* this will "never" be executed */
419 * setbit(int bitno, int *s) - set bit in bit string
422 movl S_ARG0, %ecx /* bit number */
423 movl S_ARG1, %eax /* address */
424 btsl %ecx, (%eax) /* set bit */
428 * clrbit(int bitno, int *s) - clear bit in bit string
431 movl S_ARG0, %ecx /* bit number */
432 movl S_ARG1, %eax /* address */
433 btrl %ecx, (%eax) /* clear bit */
437 * ffsbit(int *s) - find first set bit in bit string
440 movl S_ARG0, %ecx /* address */
441 movl $0, %edx /* base offset */
443 bsfl (%ecx), %eax /* check argument bits */
444 jnz 1f /* found bit, return */
445 addl $4, %ecx /* increment address */
446 addl $32, %edx /* increment offset */
447 jmp 0b /* try again */
449 addl %edx, %eax /* return offset */
453 * testbit(int nr, volatile void *array)
455 * Test to see if the bit is set within the bit string
459 movl S_ARG0,%eax /* Get the bit to test */
460 movl S_ARG1,%ecx /* get the array string */