2 * Copyright (c) 2009-2010 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@
30 * file: pal_routines.c
31 * Platform Abstraction Layer routines for bare-metal i386 and x86_64
35 #include <kern/kern_types.h>
36 #include <mach/mach_types.h>
37 #include <kern/thread.h>
38 #include <kern/simple_lock.h>
40 #include <sys/kdebug.h>
41 #include <machine/pal_routines.h>
42 #include <i386/serial_io.h>
43 #include <i386/lapic.h>
44 #include <i386/proc_reg.h>
45 #include <i386/misc_protos.h>
46 #include <i386/machine_routines.h>
47 #include <i386/pmap.h>
51 #define DBG(x...) kprintf("PAL_DBG: " x)
54 #endif /* PAL_DEBUG */
56 extern void *gPEEFIRuntimeServices
;
57 extern void *gPEEFISystemTable
;
59 /* nanotime conversion information */
60 pal_rtc_nanotime_t pal_rtc_nanotime_info
= {0,0,0,0,1,0};
62 /* APIC kext may use this to access xnu internal state */
63 struct pal_apic_table
*apic_table
= NULL
;
65 decl_simple_lock_data(static , pal_efi_lock
);
67 static pml4_entry_t IDPML4
[PTE_PER_PAGE
] __attribute__ ((aligned (4096)));
68 uint64_t pal_efi_saved_cr0
;
69 uint64_t pal_efi_saved_cr3
;
81 pal_serial_putc(char c
)
93 /* Generic routines */
97 simple_lock_init(&pal_efi_lock
, 0);
101 pal_get_control_registers( pal_cr_t
*cr0
, pal_cr_t
*cr2
,
102 pal_cr_t
*cr3
, pal_cr_t
*cr4
)
106 *cr3
= get_cr3_raw();
112 * define functions below here to ensure we have symbols for these,
113 * even though they're not used on this platform.
115 #undef pal_dbg_page_fault
117 pal_dbg_page_fault( thread_t thread __unused
,
118 user_addr_t vaddr __unused
,
119 kern_return_t kr __unused
)
123 #undef pal_dbg_set_task_name
125 pal_dbg_set_task_name( task_t task __unused
)
129 #undef pal_set_signal_delivery
131 pal_set_signal_delivery(thread_t thread __unused
)
137 _pal_efi_call_in_64bit_mode_asm(uint64_t func
,
138 struct pal_efi_registers
*efi_reg
,
139 void *stack_contents
,
140 size_t stack_contents_size
);
143 pal_efi_call_in_64bit_mode(uint64_t func
,
144 struct pal_efi_registers
*efi_reg
,
145 void *stack_contents
,
146 size_t stack_contents_size
, /* 16-byte multiple */
147 uint64_t *efi_status
)
149 DBG("pal_efi_call_in_64bit_mode(0x%016llx, %p, %p, %lu, %p)\n",
150 func
, efi_reg
, stack_contents
, stack_contents_size
, efi_status
);
153 return KERN_INVALID_ADDRESS
;
156 if ((efi_reg
== NULL
)
157 || (stack_contents
== NULL
)
158 || (stack_contents_size
% 16 != 0)) {
159 return KERN_INVALID_ARGUMENT
;
162 if (!gPEEFISystemTable
|| !gPEEFIRuntimeServices
) {
163 return KERN_NOT_SUPPORTED
;
166 if (func
< VM_MIN_KERNEL_ADDRESS
) {
168 * EFI Runtime Services must be mapped in our address
169 * space at an appropriate location.
171 return KERN_INVALID_ADDRESS
;
174 _pal_efi_call_in_64bit_mode_asm(func
,
177 stack_contents_size
);
179 *efi_status
= efi_reg
->rax
;
185 _pal_efi_call_in_32bit_mode_asm(uint32_t func
,
186 struct pal_efi_registers
*efi_reg
,
187 void *stack_contents
,
188 size_t stack_contents_size
);
191 pal_efi_call_in_32bit_mode(uint32_t func
,
192 struct pal_efi_registers
*efi_reg
,
193 void *stack_contents
,
194 size_t stack_contents_size
, /* 16-byte multiple */
195 uint32_t *efi_status
)
197 DBG("pal_efi_call_in_32bit_mode(0x%08x, %p, %p, %lu, %p)\n",
198 func
, efi_reg
, stack_contents
, stack_contents_size
, efi_status
);
201 return KERN_INVALID_ADDRESS
;
204 if ((efi_reg
== NULL
)
205 || (stack_contents
== NULL
)
206 || (stack_contents_size
% 16 != 0)) {
207 return KERN_INVALID_ARGUMENT
;
210 if (!gPEEFISystemTable
|| !gPEEFIRuntimeServices
) {
211 return KERN_NOT_SUPPORTED
;
214 DBG("pal_efi_call_in_32bit_mode() efi_reg:\n");
215 DBG(" rcx: 0x%016llx\n", efi_reg
->rcx
);
216 DBG(" rdx: 0x%016llx\n", efi_reg
->rdx
);
217 DBG(" r8: 0x%016llx\n", efi_reg
->r8
);
218 DBG(" r9: 0x%016llx\n", efi_reg
->r9
);
219 DBG(" rax: 0x%016llx\n", efi_reg
->rax
);
221 DBG("pal_efi_call_in_32bit_mode() stack:\n");
224 for (i
= 0; i
< stack_contents_size
; i
+= sizeof(uint32_t)) {
225 uint32_t *p
= (uint32_t *) ((uintptr_t)stack_contents
+ i
);
226 DBG(" %p: 0x%08x\n", p
, *p
);
232 * Ensure no interruptions.
233 * Taking a spinlock for serialization is technically unnecessary
234 * because the EFIRuntime kext should serialize.
236 boolean_t istate
= ml_set_interrupts_enabled(FALSE
);
237 simple_lock(&pal_efi_lock
);
240 * Switch to special page tables with the entire high kernel space
241 * double-mapped into the bottom 4GB.
243 * NB: We assume that all data passed exchanged with RuntimeServices is
244 * located in the 4GB of KVA based at VM_MIN_ADDRESS. In particular, kexts
245 * loaded the basement (below VM_MIN_ADDRESS) cannot pass static data.
246 * Kernel stack and heap space is OK.
248 MARK_CPU_IDLE(cpu_number());
249 pal_efi_saved_cr3
= get_cr3_raw();
250 pal_efi_saved_cr0
= get_cr0();
251 IDPML4
[KERNEL_PML4_INDEX
] = IdlePML4
[KERNEL_PML4_INDEX
];
252 IDPML4
[0] = IdlePML4
[KERNEL_PML4_INDEX
];
254 set_cr3_raw((uint64_t) ID_MAP_VTOP(IDPML4
));
256 swapgs(); /* Save kernel's GS base */
258 /* Set segment state ready for compatibility mode */
265 _pal_efi_call_in_32bit_mode_asm(func
,
268 stack_contents_size
);
270 /* Restore NULL segment state */
275 swapgs(); /* Restore kernel's GS base */
277 /* Restore the 64-bit user GS base we just destroyed */
278 wrmsr64(MSR_IA32_KERNEL_GS_BASE
,
279 current_cpu_datap()->cpu_uber
.cu_user_gs_base
);
281 /* End of mapping games */
282 set_cr3_raw(pal_efi_saved_cr3
);
283 set_cr0(pal_efi_saved_cr0
);
284 MARK_CPU_ACTIVE(cpu_number());
286 simple_unlock(&pal_efi_lock
);
287 ml_set_interrupts_enabled(istate
);
289 _pal_efi_call_in_32bit_mode_asm(func
,
292 stack_contents_size
);
295 *efi_status
= (uint32_t)efi_reg
->rax
;
296 DBG("pal_efi_call_in_32bit_mode() efi_status: 0x%x\n", *efi_status
);
301 /* wind-back a syscall instruction */
303 pal_syscall_restart(thread_t thread __unused
, x86_saved_state_t
*state
)
305 /* work out which flavour thread it is */
306 if( is_saved_state32(state
) )
308 x86_saved_state32_t
*regs32
;
309 regs32
= saved_state32(state
);
311 if (regs32
->cs
== SYSENTER_CS
|| regs32
->cs
== SYSENTER_TF_CS
)
318 x86_saved_state64_t
*regs64
;
320 assert( is_saved_state64(state
) );
321 regs64
= saved_state64(state
);
323 /* Only one instruction for 64-bit threads */
324 regs64
->isf
.rip
-= 2;
329 /* Helper function to put the machine to sleep (or shutdown) */
332 pal_machine_sleep(uint8_t type_a __unused
, uint8_t type_b __unused
, uint32_t bit_position __unused
,
333 uint32_t disable_mask __unused
, uint32_t enable_mask __unused
)
339 /* shouldn't be used on native */
341 pal_get_kern_regs( x86_saved_state_t
*state
)
343 panic( "pal_get_kern_regs called. state %p\n", state
);
347 pal_preemption_assert(void)
352 hibernate_pal_prepare(void)