2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 #include <platforms.h>
57 #include <i386/proc_reg.h>
58 #include <i386/postcode.h>
61 #define CX(addr,reg) addr(,reg,4)
64 #include <i386/mp_slave_boot.h>
67 * GAS won't handle an intersegment jump with a relocatable offset.
69 #define LJMP(segment,address) \
76 #define KVTOPHYS (-KERNELBASE)
77 #define KVTOLINEAR LINEAR_KERNELBASE
80 #define PA(addr) ((addr)+KVTOPHYS)
81 #define VA(addr) ((addr)-KVTOPHYS)
84 #if 0 /* Anyone need this? */
86 .globl EXT(_kick_buffer_)
93 * Interrupt and bootup stack for initial processor.
95 .section __HIB, __data
101 .set ., .+INTSTACK_SIZE
103 .globl EXT(eintstack)
107 * Pointers to GDT and IDT. These contain linear addresses.
112 .word Times(8,GDTSZ)-1
118 .word Times(8,IDTSZ)-1
121 /* back to the regular __DATA section. */
123 .section __DATA, __data
128 * Kernel debugger stack for each processor.
131 .globl EXT(db_stack_store)
133 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
136 * Stack for last-ditch debugger task for each processor.
139 .globl EXT(db_task_stack_store)
140 EXT(db_task_stack_store):
141 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
144 * per-processor kernel debugger stacks
147 .globl EXT(kgdb_stack_store)
148 EXT(kgdb_stack_store):
149 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
150 #endif /* MACH_KDB */
154 * start_lock is very special. We initialize the
155 * lock at allocation time rather than at run-time.
156 * Although start_lock should be an instance of a
157 * hw_lock, we hand-code all manipulation of the lock
158 * because the hw_lock code may require function calls;
159 * and we'd rather not introduce another dependency on
160 * a working stack at this point.
162 .globl EXT(start_lock)
164 .long 0 /* synchronizes processor startup */
166 .globl EXT(master_is_up)
168 .long 0 /* 1 when OK for other processors */
170 .globl EXT(mp_boot_pde)
174 _KERNend: .long 0 /* phys addr end of kernel (just after bss) */
175 physfree: .long 0 /* phys addr of next free page */
178 _IdlePTD: .long 0 /* phys addr of kernel PTD */
181 _IdlePDPT: .long 0 /* phys addr of kernel PDPT */
186 _KPTphys: .long 0 /* phys addr of kernel page tables */
189 /* Some handy macros */
191 #define ALLOCPAGES(npages) \
192 movl PA(physfree), %esi ; \
193 movl $((npages) * PAGE_SIZE), %eax ; \
195 movl %eax, PA(physfree) ; \
197 movl $((npages) * PAGE_SIZE / 4),%ecx ; \
205 * eax = page frame address
206 * ebx = index into page table
207 * ecx = how many pages to map
208 * base = base address of page dir/table
209 * prot = protection bits
211 #define fillkpt(base, prot) \
212 shll $(PTEINDX),%ebx ; \
214 orl $(PTE_V) ,%eax ; \
216 1: movl %eax,(%ebx) ; \
217 addl $(PAGE_SIZE),%eax ; /* increment physical address */ \
218 addl $(PTESIZE),%ebx ; /* next pte */ \
223 * eax = physical address
224 * ecx = how many pages to map
225 * prot = protection bits
227 #define fillkptphys(prot) \
229 shrl $(PAGE_SHIFT), %ebx ; \
230 fillkpt(PA(EXT(KPTphys)), prot)
234 * All CPUs start here.
237 * protected mode, no paging, flat 32-bit address space.
238 * (Code/data/stack segments have base == 0, limit == 4G)
246 mov %eax, %ebx /* save pointer to kernbootstruct */
248 POSTCODE(PSTART_ENTRY);
250 mov $0,%ax /* fs must be zeroed; */
251 mov %ax,%fs /* some bootstrappers don`t do this */
255 0: cmpl $0,PA(EXT(start_lock))
258 xchgl %eax,PA(EXT(start_lock)) /* locked */
262 cmpl $0,PA(EXT(master_is_up)) /* are we first? */
263 jne EXT(slave_start) /* no -- system already up. */
264 movl $1,PA(EXT(master_is_up)) /* others become slaves */
269 * Get startup parameters.
272 movl %ebx,PA(EXT(boot_args_start)) /* Save KERNBOOTSTRUCT */
274 movl KADDR(%ebx), %eax
275 addl KSIZE(%ebx), %eax
278 movl %eax, PA(EXT(KERNend))
279 movl %eax, PA(physfree)
282 /* allocate kernel page table pages */
284 movl %esi,PA(EXT(KPTphys))
287 /* allocate Page Table Directory Page */
289 movl %esi,PA(EXT(IdlePDPT))
292 /* allocate kernel page directory page */
294 movl %esi,PA(EXT(IdlePTD))
296 /* map from zero to end of kernel */
298 movl PA(physfree),%ecx
299 shrl $(PAGE_SHIFT),%ecx
300 fillkptphys( $(PTE_W) )
302 /* map page directory */
304 movl PA(EXT(IdlePDPT)), %eax
306 fillkptphys( $(PTE_W) )
308 movl PA(EXT(IdlePTD)),%eax
310 fillkptphys( $(PTE_W) )
312 /* install a pde for temp double map of bottom of VA */
313 movl PA(EXT(KPTphys)),%eax
316 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
318 /* install pde's for page tables */
319 movl PA(EXT(KPTphys)),%eax
322 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
324 /* install a pde recursively mapping page directory as a page table */
325 movl PA(EXT(IdlePTD)),%eax
328 fillkpt(PA(EXT(IdlePTD)), $(PTE_W))
331 movl PA(EXT(IdlePTD)), %eax
334 fillkpt(PA(EXT(IdlePDPT)), $0)
337 /* install a pde page for commpage use up in high memory */
339 movl PA(physfree),%eax /* grab next phys page */
341 addl $(PAGE_SIZE),%ebx
342 movl %ebx,PA(physfree) /* show next free phys pg */
343 movl $(COMM_PAGE_BASE_ADDR),%ebx
344 shrl $(PDESHIFT),%ebx /* index into pde page */
345 movl $(1), %ecx /* # pdes to store */
346 fillkpt(PA(EXT(IdlePTD)), $(PTE_W|PTE_U)) /* user has access! */
348 movl PA(physfree),%edi
349 movl %edi,PA(EXT(first_avail)) /* save first available phys addr */
353 * We steal 0x4000 for a temp pdpt and 0x5000-0x8000
354 * for temp pde pages in the PAE case. Once we are
355 * running at the proper virtual address we switch to
356 * the PDPT/PDE's the master is using */
358 /* clear pdpt page to be safe */
360 movl $(PAGE_SIZE),%ecx
366 /* build temp pdpt */
370 fillkpt($(0x4000), $0)
372 /* copy the NPGPTD pages of pdes */
373 movl PA(EXT(IdlePTD)),%eax
375 movl $((PTEMASK+1)*NPGPTD),%ecx
384 /* create temp pde for slaves to use
385 use unused lomem page and copy in IdlePTD */
386 movl PA(EXT(IdlePTD)),%eax
388 movl $(PTEMASK+1),%ecx
396 POSTCODE(PSTART_PAGE_TABLES);
399 * Fix initial descriptor tables.
401 lea PA(EXT(idt)),%esi /* fix IDT */
403 movl $(PA(fix_idt_ret)),%ebx
404 jmp fix_desc_common /* (cannot use stack) */
407 lea PA(EXT(gdt)),%esi /* fix GDT */
409 movl $(PA(fix_gdt_ret)),%ebx
410 jmp fix_desc_common /* (cannot use stack) */
413 lea PA(EXT(ldt)),%esi /* fix LDT */
415 movl $(PA(fix_ldt_ret)),%ebx
416 jmp fix_desc_common /* (cannot use stack) */
423 lgdt PA(EXT(gdtptr)) /* load GDT */
424 lidt PA(EXT(idtptr)) /* load IDT */
426 POSTCODE(PSTART_BEFORE_PAGING);
432 movl PA(EXT(IdlePDPT)), %eax
439 movl PA(EXT(IdlePTD)), %eax
444 orl $(CR0_PG|CR0_WP|CR0_PE),%eax
445 movl %eax,%cr0 /* to enable paging */
447 LJMP(KERNEL_CS,EXT(vstart)) /* switch to kernel code segment */
450 * Master is now running with correct addresses.
453 POSTCODE(VSTART_ENTRY) ;
455 mov $(KERNEL_DS),%ax /* set kernel data segment */
459 mov %ax,EXT(ktss)+TSS_SS0 /* set kernel stack segment */
460 /* for traps to kernel */
462 mov %ax,EXT(dbtss)+TSS_SS0 /* likewise for debug task switch */
463 mov %cr3,%eax /* get PDBR into debug TSS */
464 mov %eax,EXT(dbtss)+TSS_PDBR
468 movw $(KERNEL_LDT),%ax /* get LDT segment */
469 lldt %ax /* load LDT */
471 mov %ax,EXT(ktss)+TSS_LDT /* store LDT in two TSS, as well... */
472 mov %ax,EXT(dbtss)+TSS_LDT /* ...matters if we switch tasks */
474 movw $(KERNEL_TSS),%ax
475 ltr %ax /* set up KTSS */
477 mov $(CPU_DATA_GS),%ax
480 POSTCODE(VSTART_STACK_SWITCH);
482 lea EXT(eintstack),%esp /* switch to the bootup stack */
483 call EXT(i386_preinit)
485 POSTCODE(VSTART_EXIT);
487 call EXT(i386_init) /* run C code */
493 .set __start, PA(EXT(pstart))
497 * master_up is used by the master cpu to signify that it is done
498 * with the interrupt stack, etc. See the code in pstart and svstart
499 * that this interlocks with.
502 .globl EXT(master_up)
504 pushl %ebp /* set up */
505 movl %esp,%ebp /* stack frame */
506 movl $0,%ecx /* unlock start_lock */
507 xchgl %ecx,EXT(start_lock) /* since we are no longer using */
508 /* bootstrap stack */
509 leave /* pop stack frame */
513 * We aren't the first. Call slave_main to initialize the processor
514 * and get Mach going on it.
517 .globl EXT(slave_start)
519 cli /* disable interrupts, so we don`t */
520 /* need IDT for a while */
522 POSTCODE(SLAVE_START_ENTRY);
526 movl $(EXT(spag_start)),%edx /* first paged code address */
536 movl $(0x4000),%eax /* tmp until we get mapped */
541 orl $(CR0_PG|CR0_WP|CR0_PE),%eax
542 movl %eax,%cr0 /* to enable paging */
544 POSTCODE(SLAVE_START_EXIT);
546 jmp *%edx /* flush prefetch queue */
549 * We are now paging, and can run with correct addresses.
553 lgdt PA(EXT(gdtptr)) /* load GDT */
554 lidt PA(EXT(idtptr)) /* load IDT */
556 LJMP(KERNEL_CS,EXT(svstart)) /* switch to kernel code segment */
560 * Slave is now running with correct addresses.
564 POSTCODE(SVSTART_ENTRY);
567 movl PA(EXT(IdlePDPT)), %eax
570 movl PA(EXT(IdlePTD)), %eax
574 mov $(KERNEL_DS),%ax /* set kernel data segment */
580 * We're not quite through with the boot stack
581 * but we need to reset the stack pointer to the correct virtual
583 * And we need to offset above the address of pstart.
585 movl $(VA(MP_BOOTSTACK+MP_BOOT+4)), %esp
588 * Switch to the per-cpu descriptor tables
590 POSTCODE(SVSTART_DESC_INIT);
592 CPU_NUMBER_FROM_LAPIC(%eax)
593 movl CX(EXT(cpu_data_ptr),%eax),%ecx
594 movl CPU_DESC_TABLEP(%ecx), %ecx
596 movw $(GDTSZ*8-1),0(%esp) /* set GDT size in GDT descriptor */
597 leal MP_GDT(%ecx),%edx
598 movl %edx,2(%esp) /* point to local GDT (linear addr) */
599 lgdt 0(%esp) /* load new GDT */
601 movw $(IDTSZ*8-1),0(%esp) /* set IDT size in IDT descriptor */
602 leal MP_IDT(%ecx),%edx
603 movl %edx,2(%esp) /* point to local IDT (linear addr) */
604 lidt 0(%esp) /* load new IDT */
606 movw $(KERNEL_LDT),%ax /* get LDT segment */
607 lldt %ax /* load LDT */
609 movw $(KERNEL_TSS),%ax
610 ltr %ax /* load new KTSS */
612 mov $(CPU_DATA_GS),%ax
616 * Get stack top from pre-cpu data and switch
618 POSTCODE(SVSTART_STACK_SWITCH);
620 movl %gs:CPU_INT_STACK_TOP,%esp
621 xorl %ebp,%ebp /* for completeness */
623 movl $0,%eax /* unlock start_lock */
624 xchgl %eax,EXT(start_lock) /* since we are no longer using */
625 /* bootstrap stack */
626 POSTCODE(SVSTART_EXIT);
628 call EXT(i386_init_slave) /* start MACH */
633 * Convert a descriptor from fake to real format.
635 * Calls from assembly code:
636 * %ebx = return address (physical) CANNOT USE STACK
637 * %esi = descriptor table address (physical)
638 * %ecx = number of descriptors
641 * 0(%esp) = return address
642 * 4(%esp) = descriptor table address (physical)
643 * 8(%esp) = number of descriptors
645 * Fake descriptor format:
646 * bytes 0..3 base 31..0
647 * bytes 4..5 limit 15..0
648 * byte 6 access byte 2 | limit 19..16
649 * byte 7 access byte 1
651 * Real descriptor format:
652 * bytes 0..1 limit 15..0
653 * bytes 2..3 base 15..0
655 * byte 5 access byte 1
656 * byte 6 access byte 2 | limit 19..16
661 * bytes 4..5 selector
662 * byte 6 word count << 4 (to match fake descriptor)
663 * byte 7 access byte 1
666 * bytes 0..1 offset 15..0
667 * bytes 2..3 selector
669 * byte 5 access byte 1
670 * bytes 6..7 offset 31..16
674 pushl %ebp /* set up */
675 movl %esp,%ebp /* stack frame */
676 pushl %esi /* save registers */
678 movl B_ARG0,%esi /* point to first descriptor */
679 movl B_ARG1,%ecx /* get number of descriptors */
680 lea 0f,%ebx /* get return address */
681 jmp fix_desc_common /* call internal routine */
682 0: popl %ebx /* restore registers */
684 leave /* pop stack frame */
689 movw 6(%esi),%dx /* get access byte */
692 cmpb $0x04,%al /* gate or descriptor? */
696 movl 0(%esi),%eax /* get base in eax */
697 rol $16,%eax /* swap 15..0 with 31..16 */
698 /* (15..0 in correct place) */
699 movb %al,%dl /* combine bits 23..16 with ACC1 */
701 movb %ah,7(%esi) /* store bits 31..24 in correct place */
702 movw 4(%esi),%ax /* move limit bits 0..15 to word 0 */
703 movl %eax,0(%esi) /* store (bytes 0..3 correct) */
704 movw %dx,4(%esi) /* store bytes 4..5 */
709 movw 4(%esi),%ax /* get selector */
710 shrb $4,%dl /* shift word count to proper place */
711 movw %dx,4(%esi) /* store word count / ACC1 */
712 movw 2(%esi),%dx /* get offset 16..31 */
713 movw %dx,6(%esi) /* store in correct place */
714 movw %ax,2(%esi) /* store selector in correct place */
716 addl $8,%esi /* bump to next descriptor */
718 jmp *%ebx /* all done */
721 * put arg in kbd leds and spin a while
725 #define K_CMD_LEDS 0xed
726 #define K_STATUS 0x64
727 #define K_IBUF_FULL 0x02 /* input (to kbd) buffer full */
728 #define K_OBUF_FULL 0x01 /* output (from kbd) buffer full */
731 mov S_ARG0,%cl /* save led value */
733 0: inb $(K_STATUS),%al /* get kbd status */
734 testb $(K_IBUF_FULL),%al /* input busy? */
735 jne 0b /* loop until not */
737 mov $(K_CMD_LEDS),%al /* K_CMD_LEDS */
738 outb %al,$(K_RDWR) /* to kbd */
740 0: inb $(K_STATUS),%al /* get kbd status */
741 testb $(K_OBUF_FULL),%al /* output present? */
742 je 0b /* loop if not */
744 inb $(K_RDWR),%al /* read status (and discard) */
746 0: inb $(K_STATUS),%al /* get kbd status */
747 testb $(K_IBUF_FULL),%al /* input busy? */
748 jne 0b /* loop until not */
750 mov %cl,%al /* move led value */
751 outb %al,$(K_RDWR) /* to kbd */
753 movl $10000000,%ecx /* spin */
756 loop 0b /* a while */