2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 #include <platforms.h>
58 #include <i386/proc_reg.h>
59 #include <i386/postcode.h>
62 #define CX(addr,reg) addr(,reg,4)
65 #include <i386/mp_slave_boot.h>
68 * GAS won't handle an intersegment jump with a relocatable offset.
70 #define LJMP(segment,address) \
77 #define KVTOPHYS (-KERNELBASE)
78 #define KVTOLINEAR LINEAR_KERNELBASE
81 #define PA(addr) ((addr)+KVTOPHYS)
82 #define VA(addr) ((addr)-KVTOPHYS)
85 #if 0 /* Anyone need this? */
87 .globl EXT(_kick_buffer_)
94 * Interrupt and bootup stack for initial processor.
96 /* in the __HIB section since the hibernate restore code uses this stack. */
97 .section __HIB, __data
102 .globl EXT(gIOHibernateRestoreStack)
103 EXT(gIOHibernateRestoreStack):
105 .set ., .+INTSTACK_SIZE
107 .globl EXT(eintstack)
109 .globl EXT(gIOHibernateRestoreStackEnd)
110 EXT(gIOHibernateRestoreStackEnd):
113 * Pointers to GDT and IDT. These contain linear addresses.
118 .word Times(8,GDTSZ)-1
124 .word Times(8,IDTSZ)-1
127 /* back to the regular __DATA section. */
129 .section __DATA, __data
134 * Kernel debugger stack for each processor.
137 .globl EXT(db_stack_store)
139 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
142 * Stack for last-ditch debugger task for each processor.
145 .globl EXT(db_task_stack_store)
146 EXT(db_task_stack_store):
147 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
150 * per-processor kernel debugger stacks
153 .globl EXT(kgdb_stack_store)
154 EXT(kgdb_stack_store):
155 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
156 #endif /* MACH_KDB */
160 * start_lock is very special. We initialize the
161 * lock at allocation time rather than at run-time.
162 * Although start_lock should be an instance of a
163 * hw_lock, we hand-code all manipulation of the lock
164 * because the hw_lock code may require function calls;
165 * and we'd rather not introduce another dependency on
166 * a working stack at this point.
168 .globl EXT(start_lock)
170 .long 0 /* synchronizes processor startup */
172 .globl EXT(master_is_up)
174 .long 0 /* 1 when OK for other processors */
176 .globl EXT(mp_boot_pde)
180 _KERNend: .long 0 /* phys addr end of kernel (just after bss) */
181 physfree: .long 0 /* phys addr of next free page */
184 _IdlePTD: .long 0 /* phys addr of kernel PTD */
187 _IdlePDPT: .long 0 /* phys addr of kernel PDPT */
192 _KPTphys: .long 0 /* phys addr of kernel page tables */
195 /* Some handy macros */
197 #define ALLOCPAGES(npages) \
198 movl PA(physfree), %esi ; \
199 movl $((npages) * PAGE_SIZE), %eax ; \
201 movl %eax, PA(physfree) ; \
203 movl $((npages) * PAGE_SIZE / 4),%ecx ; \
211 * eax = page frame address
212 * ebx = index into page table
213 * ecx = how many pages to map
214 * base = base address of page dir/table
215 * prot = protection bits
217 #define fillkpt(base, prot) \
218 shll $(PTEINDX),%ebx ; \
220 orl $(PTE_V) ,%eax ; \
222 1: movl %eax,(%ebx) ; \
223 addl $(PAGE_SIZE),%eax ; /* increment physical address */ \
224 addl $(PTESIZE),%ebx ; /* next pte */ \
229 * eax = physical address
230 * ecx = how many pages to map
231 * prot = protection bits
233 #define fillkptphys(prot) \
235 shrl $(PAGE_SHIFT), %ebx ; \
236 fillkpt(PA(EXT(KPTphys)), prot)
240 * All CPUs start here.
243 * protected mode, no paging, flat 32-bit address space.
244 * (Code/data/stack segments have base == 0, limit == 4G)
252 mov %eax, %ebx /* save pointer to kernbootstruct */
254 POSTCODE(PSTART_ENTRY);
256 mov $0,%ax /* fs must be zeroed; */
257 mov %ax,%fs /* some bootstrappers don`t do this */
261 0: cmpl $0,PA(EXT(start_lock))
264 xchgl %eax,PA(EXT(start_lock)) /* locked */
268 cmpl $0,PA(EXT(master_is_up)) /* are we first? */
269 jne EXT(slave_start) /* no -- system already up. */
270 movl $1,PA(EXT(master_is_up)) /* others become slaves */
275 * Get startup parameters.
278 movl %ebx,PA(EXT(boot_args_start)) /* Save KERNBOOTSTRUCT */
280 movl KADDR(%ebx), %eax
281 addl KSIZE(%ebx), %eax
284 movl %eax, PA(EXT(KERNend))
285 movl %eax, PA(physfree)
288 /* allocate kernel page table pages */
290 movl %esi,PA(EXT(KPTphys))
293 /* allocate Page Table Directory Page */
295 movl %esi,PA(EXT(IdlePDPT))
298 /* allocate kernel page directory page */
300 movl %esi,PA(EXT(IdlePTD))
302 /* map from zero to end of kernel */
304 movl PA(physfree),%ecx
305 shrl $(PAGE_SHIFT),%ecx
306 fillkptphys( $(PTE_W) )
308 /* map page directory */
310 movl PA(EXT(IdlePDPT)), %eax
312 fillkptphys( $(PTE_W) )
314 movl PA(EXT(IdlePTD)),%eax
316 fillkptphys( $(PTE_W) )
318 /* install a pde for temp double map of bottom of VA */
319 movl PA(EXT(KPTphys)),%eax
322 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
324 /* install pde's for page tables */
325 movl PA(EXT(KPTphys)),%eax
328 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
330 /* install a pde recursively mapping page directory as a page table */
331 movl PA(EXT(IdlePTD)),%eax
334 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
337 movl PA(EXT(IdlePTD)), %eax
340 fillkpt(PA(EXT(IdlePDPT)), $0)
343 /* install a pde page for commpage use up in high memory */
345 movl PA(physfree),%eax /* grab next phys page */
347 addl $(PAGE_SIZE),%ebx
348 movl %ebx,PA(physfree) /* show next free phys pg */
349 movl $(COMM_PAGE_BASE_ADDR),%ebx
350 shrl $(PDESHIFT),%ebx /* index into pde page */
351 movl $(1), %ecx /* # pdes to store */
352 fillkpt(PA(EXT(IdlePTD)), $(PTE_W|PTE_U)) /* user has access! */
354 movl PA(physfree),%edi
355 movl %edi,PA(EXT(first_avail)) /* save first available phys addr */
359 * We steal 0x4000 for a temp pdpt and 0x5000-0x8000
360 * for temp pde pages in the PAE case. Once we are
361 * running at the proper virtual address we switch to
362 * the PDPT/PDE's the master is using */
364 /* clear pdpt page to be safe */
366 movl $(PAGE_SIZE),%ecx
372 /* build temp pdpt */
376 fillkpt($(0x4000), $0)
378 /* copy the NPGPTD pages of pdes */
379 movl PA(EXT(IdlePTD)),%eax
381 movl $((PTEMASK+1)*NPGPTD),%ecx
390 /* create temp pde for slaves to use
391 use unused lomem page and copy in IdlePTD */
392 movl PA(EXT(IdlePTD)),%eax
394 movl $(PTEMASK+1),%ecx
402 POSTCODE(PSTART_PAGE_TABLES);
405 * Fix initial descriptor tables.
407 lea PA(EXT(idt)),%esi /* fix IDT */
409 movl $(PA(fix_idt_ret)),%ebx
410 jmp fix_desc_common /* (cannot use stack) */
413 lea PA(EXT(gdt)),%esi /* fix GDT */
415 movl $(PA(fix_gdt_ret)),%ebx
416 jmp fix_desc_common /* (cannot use stack) */
419 lea PA(EXT(ldt)),%esi /* fix LDT */
421 movl $(PA(fix_ldt_ret)),%ebx
422 jmp fix_desc_common /* (cannot use stack) */
429 lgdt PA(EXT(gdtptr)) /* load GDT */
430 lidt PA(EXT(idtptr)) /* load IDT */
432 POSTCODE(PSTART_BEFORE_PAGING);
438 movl PA(EXT(IdlePDPT)), %eax
445 movl PA(EXT(IdlePTD)), %eax
450 orl $(CR0_PG|CR0_WP|CR0_PE),%eax
451 movl %eax,%cr0 /* to enable paging */
453 LJMP(KERNEL_CS,EXT(vstart)) /* switch to kernel code segment */
456 * Master is now running with correct addresses.
459 POSTCODE(VSTART_ENTRY) ;
461 mov $(KERNEL_DS),%ax /* set kernel data segment */
465 mov %ax,EXT(ktss)+TSS_SS0 /* set kernel stack segment */
466 /* for traps to kernel */
468 mov %ax,EXT(dbtss)+TSS_SS0 /* likewise for debug task switch */
469 mov %cr3,%eax /* get PDBR into debug TSS */
470 mov %eax,EXT(dbtss)+TSS_PDBR
474 movw $(KERNEL_LDT),%ax /* get LDT segment */
475 lldt %ax /* load LDT */
477 mov %ax,EXT(ktss)+TSS_LDT /* store LDT in two TSS, as well... */
478 mov %ax,EXT(dbtss)+TSS_LDT /* ...matters if we switch tasks */
480 movw $(KERNEL_TSS),%ax
481 ltr %ax /* set up KTSS */
483 mov $(CPU_DATA_GS),%ax
486 POSTCODE(VSTART_STACK_SWITCH);
488 lea EXT(eintstack),%esp /* switch to the bootup stack */
489 call EXT(i386_preinit)
491 POSTCODE(VSTART_EXIT);
493 call EXT(i386_init) /* run C code */
499 .set __start, PA(EXT(pstart))
503 * master_up is used by the master cpu to signify that it is done
504 * with the interrupt stack, etc. See the code in pstart and svstart
505 * that this interlocks with.
508 .globl EXT(master_up)
510 pushl %ebp /* set up */
511 movl %esp,%ebp /* stack frame */
512 movl $0,%ecx /* unlock start_lock */
513 xchgl %ecx,EXT(start_lock) /* since we are no longer using */
514 /* bootstrap stack */
515 leave /* pop stack frame */
519 * We aren't the first. Call slave_main to initialize the processor
520 * and get Mach going on it.
523 .globl EXT(slave_start)
525 cli /* disable interrupts, so we don`t */
526 /* need IDT for a while */
528 POSTCODE(SLAVE_START_ENTRY);
532 movl $(EXT(spag_start)),%edx /* first paged code address */
542 movl $(0x4000),%eax /* tmp until we get mapped */
547 orl $(CR0_PG|CR0_WP|CR0_PE),%eax
548 movl %eax,%cr0 /* to enable paging */
550 POSTCODE(SLAVE_START_EXIT);
552 jmp *%edx /* flush prefetch queue */
555 * We are now paging, and can run with correct addresses.
559 lgdt PA(EXT(gdtptr)) /* load GDT */
560 lidt PA(EXT(idtptr)) /* load IDT */
562 LJMP(KERNEL_CS,EXT(svstart)) /* switch to kernel code segment */
566 * Slave is now running with correct addresses.
570 POSTCODE(SVSTART_ENTRY);
573 movl PA(EXT(IdlePDPT)), %eax
576 movl PA(EXT(IdlePTD)), %eax
580 mov $(KERNEL_DS),%ax /* set kernel data segment */
586 * We're not quite through with the boot stack
587 * but we need to reset the stack pointer to the correct virtual
589 * And we need to offset above the address of pstart.
591 movl $(VA(MP_BOOTSTACK+MP_BOOT+4)), %esp
594 * Switch to the per-cpu descriptor tables
596 POSTCODE(SVSTART_DESC_INIT);
598 CPU_NUMBER_FROM_LAPIC(%eax)
599 movl CX(EXT(cpu_data_ptr),%eax),%ecx
600 movl CPU_DESC_TABLEP(%ecx), %ecx
602 movw $(GDTSZ*8-1),0(%esp) /* set GDT size in GDT descriptor */
603 leal MP_GDT(%ecx),%edx
604 movl %edx,2(%esp) /* point to local GDT (linear addr) */
605 lgdt 0(%esp) /* load new GDT */
607 movw $(IDTSZ*8-1),0(%esp) /* set IDT size in IDT descriptor */
608 leal MP_IDT(%ecx),%edx
609 movl %edx,2(%esp) /* point to local IDT (linear addr) */
610 lidt 0(%esp) /* load new IDT */
612 movw $(KERNEL_LDT),%ax /* get LDT segment */
613 lldt %ax /* load LDT */
615 movw $(KERNEL_TSS),%ax
616 ltr %ax /* load new KTSS */
618 mov $(CPU_DATA_GS),%ax
622 * Get stack top from pre-cpu data and switch
624 POSTCODE(SVSTART_STACK_SWITCH);
626 movl %gs:CPU_INT_STACK_TOP,%esp
627 xorl %ebp,%ebp /* for completeness */
629 movl $0,%eax /* unlock start_lock */
630 xchgl %eax,EXT(start_lock) /* since we are no longer using */
631 /* bootstrap stack */
632 POSTCODE(SVSTART_EXIT);
634 call EXT(i386_init_slave) /* start MACH */
639 * Convert a descriptor from fake to real format.
641 * Calls from assembly code:
642 * %ebx = return address (physical) CANNOT USE STACK
643 * %esi = descriptor table address (physical)
644 * %ecx = number of descriptors
647 * 0(%esp) = return address
648 * 4(%esp) = descriptor table address (physical)
649 * 8(%esp) = number of descriptors
651 * Fake descriptor format:
652 * bytes 0..3 base 31..0
653 * bytes 4..5 limit 15..0
654 * byte 6 access byte 2 | limit 19..16
655 * byte 7 access byte 1
657 * Real descriptor format:
658 * bytes 0..1 limit 15..0
659 * bytes 2..3 base 15..0
661 * byte 5 access byte 1
662 * byte 6 access byte 2 | limit 19..16
667 * bytes 4..5 selector
668 * byte 6 word count << 4 (to match fake descriptor)
669 * byte 7 access byte 1
672 * bytes 0..1 offset 15..0
673 * bytes 2..3 selector
675 * byte 5 access byte 1
676 * bytes 6..7 offset 31..16
680 pushl %ebp /* set up */
681 movl %esp,%ebp /* stack frame */
682 pushl %esi /* save registers */
684 movl B_ARG0,%esi /* point to first descriptor */
685 movl B_ARG1,%ecx /* get number of descriptors */
686 lea 0f,%ebx /* get return address */
687 jmp fix_desc_common /* call internal routine */
688 0: popl %ebx /* restore registers */
690 leave /* pop stack frame */
695 movw 6(%esi),%dx /* get access byte */
698 cmpb $0x04,%al /* gate or descriptor? */
702 movl 0(%esi),%eax /* get base in eax */
703 rol $16,%eax /* swap 15..0 with 31..16 */
704 /* (15..0 in correct place) */
705 movb %al,%dl /* combine bits 23..16 with ACC1 */
707 movb %ah,7(%esi) /* store bits 31..24 in correct place */
708 movw 4(%esi),%ax /* move limit bits 0..15 to word 0 */
709 movl %eax,0(%esi) /* store (bytes 0..3 correct) */
710 movw %dx,4(%esi) /* store bytes 4..5 */
715 movw 4(%esi),%ax /* get selector */
716 shrb $4,%dl /* shift word count to proper place */
717 movw %dx,4(%esi) /* store word count / ACC1 */
718 movw 2(%esi),%dx /* get offset 16..31 */
719 movw %dx,6(%esi) /* store in correct place */
720 movw %ax,2(%esi) /* store selector in correct place */
722 addl $8,%esi /* bump to next descriptor */
724 jmp *%ebx /* all done */
727 * put arg in kbd leds and spin a while
731 #define K_CMD_LEDS 0xed
732 #define K_STATUS 0x64
733 #define K_IBUF_FULL 0x02 /* input (to kbd) buffer full */
734 #define K_OBUF_FULL 0x01 /* output (from kbd) buffer full */
737 mov S_ARG0,%cl /* save led value */
739 0: inb $(K_STATUS),%al /* get kbd status */
740 testb $(K_IBUF_FULL),%al /* input busy? */
741 jne 0b /* loop until not */
743 mov $(K_CMD_LEDS),%al /* K_CMD_LEDS */
744 outb %al,$(K_RDWR) /* to kbd */
746 0: inb $(K_STATUS),%al /* get kbd status */
747 testb $(K_OBUF_FULL),%al /* output present? */
748 je 0b /* loop if not */
750 inb $(K_RDWR),%al /* read status (and discard) */
752 0: inb $(K_STATUS),%al /* get kbd status */
753 testb $(K_IBUF_FULL),%al /* input busy? */
754 jne 0b /* loop until not */
756 mov %cl,%al /* move led value */
757 outb %al,$(K_RDWR) /* to kbd */
759 movl $10000000,%ecx /* spin */
762 loop 0b /* a while */