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_kgdb.h>
62 #include <stat_time.h>
63 #include <mach_assert.h>
65 #include <sys/errno.h>
67 #include <i386/cpuid.h>
68 #include <i386/eflags.h>
69 #include <i386/proc_reg.h>
70 #include <i386/trap.h>
73 #include <config_dtrace.h>
76 * PTmap is recursive pagemap at top of virtual address space.
77 * Within PTmap, the page directory can be found (third indirection).
79 .globl _PTmap,_PTD,_PTDpde
80 .set _PTmap,(PTDPTDI << PDESHIFT)
81 .set _PTD,_PTmap + (PTDPTDI * NBPG)
82 .set _PTDpde,_PTD + (PTDPTDI * PDESIZE)
85 /* Under Mach-O, etext is a variable which contains
86 * the last text address
88 #define ETEXT_ADDR (EXT(etext))
90 /* Under ELF and other non-Mach-O formats, the address of
91 * etext represents the last text address
93 #define ETEXT_ADDR $ EXT(etext)
105 #define RECOVERY_SECTION .section __VECTORS, __recover
107 #define RECOVERY_SECTION .text
108 #define RECOVERY_SECTION .text
111 #define RECOVER_TABLE_START \
113 .globl EXT(recover_table) ;\
114 LEXT(recover_table) ;\
117 #define RECOVER(addr) \
124 #define RECOVER_TABLE_END \
126 .globl EXT(recover_table_end) ;\
127 LEXT(recover_table_end) ;\
128 .long 0 /* workaround see comment below */ ;\
132 * the .long 0 is to work around a linker bug (insert radar# here)
133 * basically recover_table_end has zero size and bumps up right against saved_esp in acpi_wakeup.s
134 * recover_table_end is in __RECOVER,__vectors and saved_esp is in __SLEEP,__data, but they're right next to each
135 * other and so the linker combines them and incorrectly relocates everything referencing recover_table_end to point
136 * into the SLEEP section
140 * Allocate recovery and table.
147 * Called as a function, makes the current thread
148 * return from the kernel as if from an exception.
149 * We will consult with DTrace if this is a
150 * newly created thread and we need to fire a probe.
153 .globl EXT(thread_exception_return)
154 .globl EXT(thread_bootstrap_return)
155 LEXT(thread_bootstrap_return)
157 call EXT(dtrace_thread_bootstrap)
160 LEXT(thread_exception_return)
162 xorl %ecx,%ecx /* don't check if in the PFZ */
163 cmpl $0, %gs:CPU_IS64BIT
164 je EXT(return_from_trap32)
165 jmp EXT(return_from_trap)
173 * Copy from user/kernel address space.
174 * arg0: window offset or kernel address
175 * arg1: kernel address
178 Entry(copyinphys_user)
179 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
182 Entry(copyinphys_kern)
183 movl $(PHYS_WINDOW_SEL),%ecx /* physical access through kernel window */
188 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
195 pushl %edi /* save registers */
197 movl 8+S_ARG0,%esi /* get source - window offset or kernel address */
198 movl 8+S_ARG1,%edi /* get destination - kernel address */
199 movl 8+S_ARG2,%edx /* get count */
202 movl %edx,%ecx /* move by longwords first */
207 movsl /* move longwords */
208 movl %edx,%ecx /* now move remaining bytes */
214 xorl %eax,%eax /* return 0 for success */
216 mov %ss,%cx /* restore kernel data and extended segments */
220 popl %edi /* restore registers */
225 movl $(EFAULT),%eax /* return error for failure */
226 jmp copyin_ret /* pop frame and return */
231 * Copy string from user/kern address space.
232 * arg0: window offset or kernel address
233 * arg1: kernel address
234 * arg2: max byte count
235 * arg3: actual byte count (OUT)
237 Entry(copyinstr_kern)
241 Entry(copyinstr_user)
242 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
248 pushl %edi /* save registers */
250 movl 8+S_ARG0,%esi /* get source - window offset or kernel address */
251 movl 8+S_ARG1,%edi /* get destination - kernel address */
252 movl 8+S_ARG2,%edx /* get count */
254 xorl %eax,%eax /* set to 0 here so that the high 24 bits */
255 /* are 0 for the cmpl against 0 */
258 RECOVER(copystr_fail) /* copy bytes... */
261 testl %edi,%edi /* if kernel address is ... */
263 movb %al,(%edi) /* copy the byte */
266 testl %eax,%eax /* did we just stuff the 0-byte? */
267 jz 4f /* yes, return 0 status already in %eax */
268 decl %edx /* decrement #bytes left in buffer */
269 jnz 2b /* buffer not full so copy in another byte */
270 movl $(ENAMETOOLONG),%eax /* buffer full but no 0-byte: ENAMETOOLONG */
272 movl 8+S_ARG3,%edi /* get OUT len ptr */
274 jz copystr_ret /* if null, just return */
276 movl %esi,(%edi) /* else set OUT arg to xfer len */
278 popl %edi /* restore registers */
283 movl $(EFAULT),%eax /* return error for failure */
284 jmp copystr_ret /* pop frame and return */
288 * Copy to user/kern address space.
289 * arg0: kernel address
290 * arg1: window offset or kernel address
293 ENTRY(copyoutphys_user)
294 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
297 ENTRY(copyoutphys_kern)
298 movl $(PHYS_WINDOW_SEL),%ecx /* physical access through kernel window */
303 movl $(USER_WINDOW_SEL),%ecx /* user data segment access through kernel window */
310 pushl %edi /* save registers */
312 movl 8+S_ARG0,%esi /* get source - kernel address */
313 movl 8+S_ARG1,%edi /* get destination - window offset or kernel address */
314 movl 8+S_ARG2,%edx /* get count */
317 movl %edx,%ecx /* move by longwords first */
320 RECOVER(copyout_fail)
323 movl %edx,%ecx /* now move remaining bytes */
326 RECOVER(copyout_fail)
329 xorl %eax,%eax /* return 0 for success */
331 mov %ss,%cx /* restore kernel segment */
335 popl %edi /* restore registers */
340 movl $(EFAULT),%eax /* return error for failure */
341 jmp copyout_ret /* pop frame and return */
345 * io register must not be used on slaves (no AT bus)
355 #define PUSH_FRAME FRAME
356 #define POP_FRAME EMARF
358 #else /* MACH_ASSERT */
366 #endif /* MACH_ASSERT */
370 * int rdmsr_carefully(uint32_t msr, uint32_t *lo, uint32_t *hi)
372 ENTRY(rdmsr_carefully)
389 * Done with recovery table.
427 lidt null_idtr /* disable the interrupt handler */
428 xor %ecx,%ecx /* generate a divide by zero */
429 div %ecx,%eax /* reboot now */
430 ret /* this will "never" be executed */
434 * setbit(int bitno, int *s) - set bit in bit string
437 movl S_ARG0, %ecx /* bit number */
438 movl S_ARG1, %eax /* address */
439 btsl %ecx, (%eax) /* set bit */
443 * clrbit(int bitno, int *s) - clear bit in bit string
446 movl S_ARG0, %ecx /* bit number */
447 movl S_ARG1, %eax /* address */
448 btrl %ecx, (%eax) /* clear bit */
452 * ffsbit(int *s) - find first set bit in bit string
455 movl S_ARG0, %ecx /* address */
456 movl $0, %edx /* base offset */
458 bsfl (%ecx), %eax /* check argument bits */
459 jnz 1f /* found bit, return */
460 addl $4, %ecx /* increment address */
461 addl $32, %edx /* increment offset */
462 jmp 0b /* try again */
464 addl %edx, %eax /* return offset */
468 * testbit(int nr, volatile void *array)
470 * Test to see if the bit is set within the bit string
474 movl S_ARG0,%eax /* Get the bit to test */
475 movl S_ARG1,%ecx /* get the array string */