2 * Copyright (c) 2000 Apple Computer, 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 <platforms.h>
63 #include <i386/proc_reg.h>
64 #include <i386/postcode.h>
67 #define CX(addr,reg) addr(,reg,4)
70 #include <i386/mp_slave_boot.h>
71 #include <i386/cpuid.h>
74 * GAS won't handle an intersegment jump with a relocatable offset.
76 #define LJMP(segment,address) \
83 #define PA(addr) (addr)
84 #define VA(addr) (addr)
87 * Interrupt and bootup stack for initial processor.
90 /* in the __HIB section since the hibernate restore code uses this stack. */
91 .section __HIB, __data
94 .globl EXT(low_intstack)
96 .globl EXT(gIOHibernateRestoreStack)
97 EXT(gIOHibernateRestoreStack):
99 .set ., .+INTSTACK_SIZE
101 .globl EXT(low_eintstack)
103 .globl EXT(gIOHibernateRestoreStackEnd)
104 EXT(gIOHibernateRestoreStackEnd):
107 * Pointers to GDT and IDT. These contain linear addresses.
111 /* align below properly */
114 .word Times(8,GDTSZ)-1
115 .long EXT(master_gdt)
119 /* align below properly */
122 .word Times(8,IDTSZ)-1
123 .long EXT(master_idt)
125 /* back to the regular __DATA section. */
127 .section __DATA, __data
130 * Stack for last-gasp double-fault handler.
133 .globl EXT(df_task_stack)
135 .set ., .+INTSTACK_SIZE
136 .globl EXT(df_task_stack_end)
137 EXT(df_task_stack_end):
141 * Stack for machine-check handler.
144 .globl EXT(mc_task_stack)
146 .set ., .+INTSTACK_SIZE
147 .globl EXT(mc_task_stack_end)
148 EXT(mc_task_stack_end):
153 * Kernel debugger stack for each processor.
156 .globl EXT(db_stack_store)
158 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
161 * Stack for last-ditch debugger task for each processor.
164 .globl EXT(db_task_stack_store)
165 EXT(db_task_stack_store):
166 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
169 * per-processor kernel debugger stacks
172 .globl EXT(kgdb_stack_store)
173 EXT(kgdb_stack_store):
174 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
175 #endif /* MACH_KDB */
179 .long 0 /* phys addr of next free page */
183 .long 0 /* phys addr of kernel PTD */
187 .long 0 /* phys addr of kernel PDPT */
193 .globl EXT(IdlePDPT64)
199 .long 0 /* phys addr of kernel page tables */
201 .globl EXT(KernelRelocOffset)
202 EXT(KernelRelocOffset):
203 .long 0 /* Kernel relocation offset */
206 /* Some handy macros */
208 #define ALLOCPAGES(npages) \
209 movl PA(physfree), %esi ; \
210 movl $((npages) * PAGE_SIZE), %eax ; \
212 movl %eax, PA(physfree) ; \
214 movl $((npages) * PAGE_SIZE / 4),%ecx ; \
222 * eax = page frame address
223 * ebx = index into page table
224 * ecx = how many pages to map
225 * base = base address of page dir/table
226 * prot = protection bits
228 #define fillkpt(base, prot) \
229 shll $(PTEINDX),%ebx ; \
231 orl $(PTE_V) ,%eax ; \
233 1: movl %eax,(%ebx) ; \
234 addl $(PAGE_SIZE),%eax ; /* increment physical address */ \
235 addl $(PTESIZE),%ebx ; /* next pte */ \
240 * eax = physical address
241 * ecx = how many pages to map
242 * prot = protection bits
244 #define fillkptphys(prot) \
246 shrl $(PAGE_SHIFT), %ebx ; \
247 fillkpt(PA(KPTphys), prot)
250 * BSP CPU start here.
251 * eax points to kernbootstruct
254 * protected mode, no paging, flat 32-bit address space.
255 * (Code/data/stack segments have base == 0, limit == 4G)
265 mov %eax, %ebp // Move kernbootstruct to ebp
266 POSTCODE(_PSTART_ENTRY)
267 movl KADDR(%ebp), %ebx // Load boot image phys addr
268 movl %ebx, %edx // Set edx with boot load phys addr
269 addl KSIZE(%ebp), %edx // Add boot image size
270 addl $(NBPG-1), %edx // Round to a page size
271 andl $(-NBPG), %edx // Set edx to first free page
272 movl %edx, %esp // Set temporay stack
273 addl $(NBPG), %esp // add page size
275 Ls1: popl %esi // Get return address
276 cmpl $(PA(Ls1)), %esi // Compare with static physicall addr
277 je EXT(pstart) // Branch if equal
278 subl $(PA(Ls1)), %esi // Extract relocation offset
279 movl %esi, %esp // Store relocation offset in esp
280 leal (PA(Lreloc_start))(%esp),%esi
281 // Set esi to reloc_start boot phys addr
282 movl %edx, %edi // Set edi to first free page
283 movl $(Lreloc_end-Lreloc_start), %ecx
284 // Set ecx to copy code size
287 movsb // copy reloc copy code
288 wbinvd // Write back and Invalidate cache
289 movl %ebx, %esi // Set esi to kernbootstruct kaddr
290 movl KADDR(%ebp), %edi // Load boot image phys addr
291 subl %esp, %edi // Adjust to static phys addr
292 movl KSIZE(%ebp), %ecx // Set ecx to kernbootstruct ksize
293 addl $(NBPG-1), %ecx // Add NBPG-1 to ecx
294 andl $(-NBPG), %ecx // Truncate ecx to a page aligned addr
295 shrl $2, %ecx // Divide ecx by 4
296 movl %esp, (PA(EXT(KernelRelocOffset)))(%esp)
297 // Store relocation offset
298 movl %edi, KADDR(%ebp) // Relocate kaddr in kernbootstruct
299 subl %esp, MEMORYMAP(%ebp) // And relocate MemoryMap
300 subl %esp, DEVICETREEP(%ebp) // And relocate deviceTreeP
301 subl %esp, %ebp // Set ebp with relocated phys addr
302 jmp *%edx // Branch to relocated copy code
304 POSTCODE(_PSTART_RELOC)
306 movsl // Copy boot image at BASE_KERNEL_PADDR
307 wbinvd // Write back and Invalidate cache
308 movl $(PA(EXT(pstart))), %edx // Set branch target
309 jmp *%edx // Far jmp to pstart phys addr
316 .set __start, PA(EXT(_pstart))
319 * BSP CPU continues here after possible relocation.
320 * ebp points to kernbootstruct
325 mov %ebp, %ebx /* get pointer to kernbootstruct */
327 POSTCODE(PSTART_ENTRY)
329 mov $0,%ax /* fs must be zeroed; */
330 mov %ax,%fs /* some bootstrappers don`t do this */
334 * Get startup parameters.
336 movl KADDR(%ebx), %eax
337 addl KSIZE(%ebx), %eax
340 movl %eax, PA(physfree)
343 /* allocate kernel page table pages */
345 movl %esi,PA(KPTphys)
348 /* allocate PML4 page */
350 movl %esi,EXT(IdlePML4)
351 /* allocate new 3rd level directory page */
353 movl %esi,EXT(IdlePDPT64)
357 /* allocate Page Table Directory Page */
359 movl %esi,PA(EXT(IdlePDPT))
362 /* allocate kernel page directory page */
364 movl %esi,PA(EXT(IdlePTD))
366 /* map from zero to end of kernel */
368 movl PA(physfree),%ecx
369 shrl $(PAGE_SHIFT),%ecx
370 fillkptphys( $(PTE_W) )
372 /* map page directory */
374 movl PA(EXT(IdlePDPT)), %eax
376 fillkptphys( $(PTE_W) )
378 movl PA(EXT(IdlePDPT64)), %eax
380 fillkptphys( $(PTE_W) )
382 movl PA(EXT(IdlePTD)),%eax
384 fillkptphys( $(PTE_W) )
386 /* install a pde for temp double map of bottom of VA */
387 movl PA(KPTphys),%eax
390 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
392 /* install pde's for page tables */
393 movl PA(KPTphys),%eax
396 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
398 /* install a pde recursively mapping page directory as a page table */
399 movl PA(EXT(IdlePTD)),%eax
402 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
405 movl PA(EXT(IdlePTD)), %eax
408 fillkpt(PA(EXT(IdlePDPT)), $0)
411 /* install a pde page for commpage use up in high memory */
413 movl PA(physfree),%eax /* grab next phys page */
415 addl $(PAGE_SIZE),%ebx
416 movl %ebx,PA(physfree) /* show next free phys pg */
417 movl $(COMM_PAGE_BASE_ADDR),%ebx
418 shrl $(PDESHIFT),%ebx /* index into pde page */
419 movl $(1), %ecx /* # pdes to store */
420 fillkpt(PA(EXT(IdlePTD)), $(PTE_W|PTE_U)) /* user has access! */
422 movl PA(physfree),%edi
423 movl %edi,PA(EXT(first_avail)) /* save first available phys addr */
427 * We steal 0x4000 for a temp pdpt and 0x5000-0x8000
428 * for temp pde pages in the PAE case. Once we are
429 * running at the proper virtual address we switch to
430 * the PDPT/PDE's the master is using */
432 /* clear pdpt page to be safe */
434 movl $(PAGE_SIZE),%ecx
440 /* build temp pdpt */
444 fillkpt($(0x4000), $0)
446 /* copy the NPGPTD pages of pdes */
447 movl PA(EXT(IdlePTD)),%eax
449 movl $((PTEMASK+1)*NPGPTD),%ecx
458 /* create temp pde for slaves to use
459 use unused lomem page and copy in IdlePTD */
460 movl PA(EXT(IdlePTD)),%eax
462 movl $(PTEMASK+1),%ecx
470 POSTCODE(PSTART_PAGE_TABLES)
473 * Fix initial descriptor tables.
475 lea PA(EXT(master_idt)),%esi /* fix IDT */
477 movl $(PA(fix_idt_ret)),%ebx
478 jmp fix_desc_common /* (cannot use stack) */
481 lea PA(EXT(master_gdt)),%esi /* fix GDT */
483 movl $(PA(fix_gdt_ret)),%ebx
484 jmp fix_desc_common /* (cannot use stack) */
487 lea PA(EXT(master_ldt)),%esi /* fix LDT */
489 movl $(PA(fix_ldt_ret)),%ebx
490 jmp fix_desc_common /* (cannot use stack) */
497 lgdt PA(EXT(gdtptr)) /* load GDT */
498 lidt PA(EXT(idtptr)) /* load IDT */
500 POSTCODE(PSTART_BEFORE_PAGING)
506 movl PA(EXT(IdlePDPT)), %eax
513 movl $0x80000001, %eax
515 and $(CPUID_EXTFEATURE_XD), %edx /* clear all but bit 20 */
516 cmp $0, %edx /* skip setting NXE if 20 is not set */
519 movl $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
520 rdmsr /* MSR value return in edx: eax */
521 orl $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
522 wrmsr /* Update Extended Feature Enable reg */
526 movl PA(EXT(IdlePTD)), %eax
531 orl $(CR0_PG|CR0_WP|CR0_PE),%eax
532 movl %eax,%cr0 /* to enable paging */
534 LJMP(KERNEL_CS,EXT(vstart)) /* switch to kernel code segment */
537 * BSP is now running with correct addresses.
540 POSTCODE(VSTART_ENTRY) ;
542 mov $(KERNEL_DS),%ax /* set kernel data segment */
546 mov %ax,EXT(master_ktss)+TSS_SS0 /* set kernel stack segment */
547 /* for traps to kernel */
550 mov %ax,EXT(master_dbtss)+TSS_SS0 /* likewise for debug task switch */
551 mov %cr3,%eax /* get PDBR into debug TSS */
552 mov %eax,EXT(master_dbtss)+TSS_PDBR
555 mov %cr3,%eax /* get PDBR into DF TSS */
556 mov %eax,EXT(master_dftss)+TSS_PDBR
557 mov %eax,EXT(master_mctss)+TSS_PDBR
559 movw $(KERNEL_LDT),%ax /* get LDT segment */
560 lldt %ax /* load LDT */
562 mov %ax,EXT(master_ktss)+TSS_LDT /* store LDT in two TSS, as well... */
563 mov %ax,EXT(master_dbtss)+TSS_LDT /* ...matters if we switch tasks */
565 movw $(KERNEL_TSS),%ax
566 ltr %ax /* set up KTSS */
568 mov $(CPU_DATA_GS),%ax
571 POSTCODE(VSTART_STACK_SWITCH)
573 lea EXT(low_eintstack),%esp /* switch to the bootup stack */
574 pushl %ebp /* push boot args addr */
575 xorl %ebp,%ebp /* clear stack frame ptr */
577 POSTCODE(VSTART_EXIT)
579 call EXT(i386_init) /* run C code */
585 * AP (slave) CPUs enter here.
588 * protected mode, no paging, flat 32-bit address space.
589 * (Code/data/stack segments have base == 0, limit == 4G)
592 .globl EXT(slave_pstart)
594 cli /* disable interrupts, so we don`t */
595 /* need IDT for a while */
597 POSTCODE(SLAVE_PSTART_ENTRY)
606 movl $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
607 rdmsr /* MSR value return in edx: eax */
608 orl $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
609 wrmsr /* Update Extended Feature Enable reg */
611 movl $(0x4000),%eax /* tmp until we get mapped */
615 orl $(CR0_PG|CR0_WP|CR0_PE),%eax
616 movl %eax,%cr0 /* to enable paging */
618 POSTCODE(SLAVE_PSTART_EXIT)
620 movl $(EXT(spag_start)),%edx /* first paged code address */
621 jmp *%edx /* flush prefetch queue */
624 * We are now paging, and can run with correct addresses.
628 lgdt PA(EXT(gdtptr)) /* load GDT */
629 lidt PA(EXT(idtptr)) /* load IDT */
631 LJMP(KERNEL_CS,EXT(slave_vstart)) /* switch to kernel code segment */
635 * Slave is now running with correct addresses.
639 POSTCODE(SLAVE_VSTART_ENTRY)
642 movl PA(EXT(IdlePDPT)), %eax
645 movl PA(EXT(IdlePTD)), %eax
649 mov $(KERNEL_DS),%ax /* set kernel data segment */
655 * We're not quite through with the boot stack
656 * but we need to reset the stack pointer to the correct virtual
658 * And we need to offset above the address of pstart.
660 movl $(VA(MP_BOOTSTACK+MP_BOOT+4)), %esp
663 * Switch to the per-cpu descriptor tables
665 POSTCODE(SLAVE_VSTART_DESC_INIT)
667 CPU_NUMBER_FROM_LAPIC(%eax)
668 movl CX(EXT(cpu_data_ptr),%eax),%ecx
670 movw $(GDTSZ*8-1),0(%esp) /* set GDT size in GDT descriptor */
671 movl CPU_DESC_INDEX+CDI_GDT(%ecx),%edx
672 movl %edx,2(%esp) /* point to local GDT (linear addr) */
673 lgdt 0(%esp) /* load new GDT */
675 movw $(IDTSZ*8-1),0(%esp) /* set IDT size in IDT descriptor */
676 movl CPU_DESC_INDEX+CDI_IDT(%ecx),%edx
677 movl %edx,2(%esp) /* point to local IDT (linear addr) */
678 lidt 0(%esp) /* load new IDT */
680 movw $(KERNEL_LDT),%ax /* get LDT segment */
681 lldt %ax /* load LDT */
683 movw $(KERNEL_TSS),%ax
684 ltr %ax /* load new KTSS */
686 mov $(CPU_DATA_GS),%ax
690 * Get stack top from pre-cpu data and switch
692 POSTCODE(SLAVE_VSTART_STACK_SWITCH)
694 movl %gs:CPU_INT_STACK_TOP,%esp
695 xorl %ebp,%ebp /* for completeness */
697 POSTCODE(SLAVE_VSTART_EXIT)
699 call EXT(i386_init_slave) /* start MACH */
704 * Convert a descriptor from fake to real format.
706 * Calls from assembly code:
707 * %ebx = return address (physical) CANNOT USE STACK
708 * %esi = descriptor table address (physical)
709 * %ecx = number of descriptors
712 * 0(%esp) = return address
713 * 4(%esp) = descriptor table address (physical)
714 * 8(%esp) = number of descriptors
716 * Fake descriptor format:
717 * bytes 0..3 base 31..0
718 * bytes 4..5 limit 15..0
719 * byte 6 access byte 2 | limit 19..16
720 * byte 7 access byte 1
722 * Real descriptor format:
723 * bytes 0..1 limit 15..0
724 * bytes 2..3 base 15..0
726 * byte 5 access byte 1
727 * byte 6 access byte 2 | limit 19..16
732 * bytes 4..5 selector
733 * byte 6 word count << 4 (to match fake descriptor)
734 * byte 7 access byte 1
737 * bytes 0..1 offset 15..0
738 * bytes 2..3 selector
740 * byte 5 access byte 1
741 * bytes 6..7 offset 31..16
745 pushl %ebp /* set up */
746 movl %esp,%ebp /* stack frame */
747 pushl %esi /* save registers */
749 movl B_ARG0,%esi /* point to first descriptor */
750 movl B_ARG1,%ecx /* get number of descriptors */
751 lea 0f,%ebx /* get return address */
752 jmp fix_desc_common /* call internal routine */
753 0: popl %ebx /* restore registers */
755 leave /* pop stack frame */
760 movw 6(%esi),%dx /* get access byte */
763 cmpb $0x04,%al /* gate or descriptor? */
767 movl 0(%esi),%eax /* get base in eax */
768 rol $16,%eax /* swap 15..0 with 31..16 */
769 /* (15..0 in correct place) */
770 movb %al,%dl /* combine bits 23..16 with ACC1 */
772 movb %ah,7(%esi) /* store bits 31..24 in correct place */
773 movw 4(%esi),%ax /* move limit bits 0..15 to word 0 */
774 movl %eax,0(%esi) /* store (bytes 0..3 correct) */
775 movw %dx,4(%esi) /* store bytes 4..5 */
780 movw 4(%esi),%ax /* get selector */
781 shrb $4,%dl /* shift word count to proper place */
782 movw %dx,4(%esi) /* store word count / ACC1 */
783 movw 2(%esi),%dx /* get offset 16..31 */
784 movw %dx,6(%esi) /* store in correct place */
785 movw %ax,2(%esi) /* store selector in correct place */
787 addl $8,%esi /* bump to next descriptor */
789 jmp *%ebx /* all done */
792 * put arg in kbd leds and spin a while
796 #define K_CMD_LEDS 0xed
797 #define K_STATUS 0x64
798 #define K_IBUF_FULL 0x02 /* input (to kbd) buffer full */
799 #define K_OBUF_FULL 0x01 /* output (from kbd) buffer full */
802 mov S_ARG0,%cl /* save led value */
804 0: inb $(K_STATUS),%al /* get kbd status */
805 testb $(K_IBUF_FULL),%al /* input busy? */
806 jne 0b /* loop until not */
808 mov $(K_CMD_LEDS),%al /* K_CMD_LEDS */
809 outb %al,$(K_RDWR) /* to kbd */
811 0: inb $(K_STATUS),%al /* get kbd status */
812 testb $(K_OBUF_FULL),%al /* output present? */
813 je 0b /* loop if not */
815 inb $(K_RDWR),%al /* read status (and discard) */
817 0: inb $(K_STATUS),%al /* get kbd status */
818 testb $(K_IBUF_FULL),%al /* input busy? */
819 jne 0b /* loop until not */
821 mov %cl,%al /* move led value */
822 outb %al,$(K_RDWR) /* to kbd */
824 movl $10000000,%ecx /* spin */
827 loop 0b /* a while */