]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/pal_routines.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / i386 / pal_routines.c
1 /*
2 * Copyright (c) 2009-2010 Apple 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 /*
30 * file: pal_routines.c
31 * Platform Abstraction Layer routines for bare-metal i386 and x86_64
32 */
33
34
35 #include <kern/kern_types.h>
36 #include <mach/mach_types.h>
37 #include <kern/thread.h>
38 #include <kern/simple_lock.h>
39
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>
48
49 //#define PAL_DEBUG 1
50 #ifdef PAL_DEBUG
51 #define DBG(x...) kprintf("PAL_DBG: " x)
52 #else
53 #define DBG(x...)
54 #endif /* PAL_DEBUG */
55
56 extern void *gPEEFIRuntimeServices;
57 extern void *gPEEFISystemTable;
58
59 /* nanotime conversion information */
60 pal_rtc_nanotime_t pal_rtc_nanotime_info = {0,0,0,0,1,0};
61
62 /* APIC kext may use this to access xnu internal state */
63 struct pal_apic_table *apic_table = NULL;
64
65 decl_simple_lock_data(static , pal_efi_lock);
66 #ifdef __x86_64__
67 #define PML4_PROT (INTEL_PTE_VALID | INTEL_PTE_WRITE)
68 #define INIT_PDPT_BASE (INITPT_SEG_BASE + PAGE_SIZE)
69 static pml4_entry_t IDPML4[PTE_PER_PAGE] __attribute__ ((aligned (4096))) = {
70 [0] = (uint64_t)(INIT_PDPT_BASE | PML4_PROT),
71 [KERNEL_PML4_INDEX] = (uint64_t)(INIT_PDPT_BASE | PML4_PROT),
72 };
73 uint64_t pal_efi_saved_cr0;
74 uint64_t pal_efi_saved_cr3;
75 #endif
76
77
78 /* Serial routines */
79 int
80 pal_serial_init(void)
81 {
82 return serial_init();
83 }
84
85 void
86 pal_serial_putc(char c)
87 {
88 serial_putc(c);
89 }
90
91 int
92 pal_serial_getc(void)
93 {
94 return serial_getc();
95 }
96
97
98 /* Generic routines */
99 void
100 pal_i386_init(void)
101 {
102 simple_lock_init(&pal_efi_lock, 0);
103 }
104
105 void
106 pal_get_control_registers( pal_cr_t *cr0, pal_cr_t *cr2,
107 pal_cr_t *cr3, pal_cr_t *cr4 )
108 {
109 *cr0 = get_cr0();
110 *cr2 = get_cr2();
111 *cr3 = get_cr3_raw();
112 *cr4 = get_cr4();
113 }
114
115
116 /*
117 * define functions below here to ensure we have symbols for these,
118 * even though they're not used on this platform.
119 */
120 #undef pal_dbg_page_fault
121 void
122 pal_dbg_page_fault( thread_t thread __unused,
123 user_addr_t vaddr __unused,
124 kern_return_t kr __unused )
125 {
126 }
127
128 #undef pal_dbg_set_task_name
129 void
130 pal_dbg_set_task_name( task_t task __unused )
131 {
132 }
133
134 #undef pal_set_signal_delivery
135 void
136 pal_set_signal_delivery(thread_t thread __unused)
137 {
138 }
139
140 /* EFI thunks */
141 extern void
142 _pal_efi_call_in_64bit_mode_asm(uint64_t func,
143 struct pal_efi_registers *efi_reg,
144 void *stack_contents,
145 size_t stack_contents_size);
146
147 kern_return_t
148 pal_efi_call_in_64bit_mode(uint64_t func,
149 struct pal_efi_registers *efi_reg,
150 void *stack_contents,
151 size_t stack_contents_size, /* 16-byte multiple */
152 uint64_t *efi_status)
153 {
154 DBG("pal_efi_call_in_64bit_mode(0x%016llx, %p, %p, %lu, %p)\n",
155 func, efi_reg, stack_contents, stack_contents_size, efi_status);
156
157 if (func == 0) {
158 return KERN_INVALID_ADDRESS;
159 }
160
161 if ((efi_reg == NULL)
162 || (stack_contents == NULL)
163 || (stack_contents_size % 16 != 0)) {
164 return KERN_INVALID_ARGUMENT;
165 }
166
167 if (!gPEEFISystemTable || !gPEEFIRuntimeServices) {
168 return KERN_NOT_SUPPORTED;
169 }
170
171 _pal_efi_call_in_64bit_mode_asm(func,
172 efi_reg,
173 stack_contents,
174 stack_contents_size);
175
176 *efi_status = efi_reg->rax;
177
178 return KERN_SUCCESS;
179 }
180
181 extern void
182 _pal_efi_call_in_32bit_mode_asm(uint32_t func,
183 struct pal_efi_registers *efi_reg,
184 void *stack_contents,
185 size_t stack_contents_size);
186
187 kern_return_t
188 pal_efi_call_in_32bit_mode(uint32_t func,
189 struct pal_efi_registers *efi_reg,
190 void *stack_contents,
191 size_t stack_contents_size, /* 16-byte multiple */
192 uint32_t *efi_status)
193 {
194 DBG("pal_efi_call_in_32bit_mode(0x%08x, %p, %p, %lu, %p)\n",
195 func, efi_reg, stack_contents, stack_contents_size, efi_status);
196
197 if (func == 0) {
198 return KERN_INVALID_ADDRESS;
199 }
200
201 if ((efi_reg == NULL)
202 || (stack_contents == NULL)
203 || (stack_contents_size % 16 != 0)) {
204 return KERN_INVALID_ARGUMENT;
205 }
206
207 if (!gPEEFISystemTable || !gPEEFIRuntimeServices) {
208 return KERN_NOT_SUPPORTED;
209 }
210
211 DBG("pal_efi_call_in_32bit_mode() efi_reg:\n");
212 DBG(" rcx: 0x%016llx\n", efi_reg->rcx);
213 DBG(" rdx: 0x%016llx\n", efi_reg->rdx);
214 DBG(" r8: 0x%016llx\n", efi_reg->r8);
215 DBG(" r9: 0x%016llx\n", efi_reg->r9);
216 DBG(" rax: 0x%016llx\n", efi_reg->rax);
217
218 DBG("pal_efi_call_in_32bit_mode() stack:\n");
219 #if PAL_DEBUG
220 size_t i;
221 for (i = 0; i < stack_contents_size; i += sizeof(uint32_t)) {
222 uint32_t *p = (uint32_t *) ((uintptr_t)stack_contents + i);
223 DBG(" %p: 0x%08x\n", p, *p);
224 }
225 #endif
226
227 #ifdef __x86_64__
228 /*
229 * Ensure no interruptions.
230 * Taking a spinlock for serialization is technically unnecessary
231 * because the EFIRuntime kext should serialize.
232 */
233 boolean_t istate = ml_set_interrupts_enabled(FALSE);
234 simple_lock(&pal_efi_lock);
235
236 /*
237 * Switch to special page tables with the entire high kernel space
238 * double-mapped into the bottom 4GB.
239 *
240 * NB: We assume that all data passed exchanged with RuntimeServices is
241 * located in the 4GB of KVA based at VM_MIN_ADDRESS. In particular, kexts
242 * loaded the basement (below VM_MIN_ADDRESS) cannot pass static data.
243 * Kernel stack and heap space is OK.
244 */
245 MARK_CPU_IDLE(cpu_number());
246 pal_efi_saved_cr3 = get_cr3_raw();
247 pal_efi_saved_cr0 = get_cr0();
248 clear_ts();
249 set_cr3_raw((uint64_t) ID_MAP_VTOP(IDPML4));
250
251 swapgs(); /* Save kernel's GS base */
252
253 /* Set segment state ready for compatibility mode */
254 set_gs(NULL_SEG);
255 set_fs(NULL_SEG);
256 set_es(KERNEL_DS);
257 set_ds(KERNEL_DS);
258 set_ss(KERNEL_DS);
259
260 _pal_efi_call_in_32bit_mode_asm(func,
261 efi_reg,
262 stack_contents,
263 stack_contents_size);
264
265 /* Restore NULL segment state */
266 set_ss(NULL_SEG);
267 set_es(NULL_SEG);
268 set_ds(NULL_SEG);
269
270 swapgs(); /* Restore kernel's GS base */
271
272 /* Restore the 64-bit user GS base we just destroyed */
273 wrmsr64(MSR_IA32_KERNEL_GS_BASE,
274 current_cpu_datap()->cpu_uber.cu_user_gs_base);
275
276 /* End of mapping games */
277 set_cr3_raw(pal_efi_saved_cr3);
278 set_cr0(pal_efi_saved_cr0);
279 MARK_CPU_ACTIVE(cpu_number());
280
281 simple_unlock(&pal_efi_lock);
282 ml_set_interrupts_enabled(istate);
283 #else
284 _pal_efi_call_in_32bit_mode_asm(func,
285 efi_reg,
286 stack_contents,
287 stack_contents_size);
288 #endif
289
290 *efi_status = (uint32_t)efi_reg->rax;
291 DBG("pal_efi_call_in_32bit_mode() efi_status: 0x%x\n", *efi_status);
292
293 return KERN_SUCCESS;
294 }
295
296 /* wind-back a syscall instruction */
297 void
298 pal_syscall_restart(thread_t thread __unused, x86_saved_state_t *state)
299 {
300 /* work out which flavour thread it is */
301 if( is_saved_state32(state) )
302 {
303 x86_saved_state32_t *regs32;
304 regs32 = saved_state32(state);
305
306 if (regs32->cs == SYSENTER_CS || regs32->cs == SYSENTER_TF_CS)
307 regs32->eip -= 5;
308 else
309 regs32->eip -= 2;
310 }
311 else
312 {
313 x86_saved_state64_t *regs64;
314
315 assert( is_saved_state64(state) );
316 regs64 = saved_state64(state);
317
318 /* Only one instruction for 64-bit threads */
319 regs64->isf.rip -= 2;
320 }
321
322 }
323
324 /* Helper function to put the machine to sleep (or shutdown) */
325
326 boolean_t
327 pal_machine_sleep(uint8_t type_a __unused, uint8_t type_b __unused, uint32_t bit_position __unused,
328 uint32_t disable_mask __unused, uint32_t enable_mask __unused)
329 {
330 return 0;
331 }
332
333
334 /* shouldn't be used on native */
335 void
336 pal_get_kern_regs( x86_saved_state_t *state )
337 {
338 panic( "pal_get_kern_regs called. state %p\n", state );
339 }
340
341 void
342 pal_preemption_assert(void)
343 {
344 }
345
346 void
347 hibernate_pal_prepare(void)
348 {
349 }