2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 #include <platforms.h>
61 #include <i386/proc_reg.h>
66 #define CX(addr,reg) addr(,reg,4)
70 #define CPU_NUMBER(reg)
71 #define CX(addr,reg) addr
73 #endif /* NCPUS > 1 */
75 #include <i386/AT386/mp/mp.h>
78 * GAS won't handle an intersegment jump with a relocatable offset.
80 #define LJMP(segment,address) \
87 #define KVTOPHYS (-KERNELBASE)
88 #define KVTOLINEAR LINEAR_KERNELBASE
91 #define PA(addr) (addr)+KVTOPHYS
92 #define VA(addr) (addr)-KVTOPHYS
96 .globl EXT(_kick_buffer_)
102 * Interrupt and bootup stack for initial processor.
107 .set ., .+INTSTACK_SIZE
108 .globl EXT(eintstack)
112 .globl EXT(int_stack_high) /* all interrupt stacks */
113 EXT(int_stack_high): /* must lie below this */
114 .long EXT(eintstack) /* address */
116 .globl EXT(int_stack_top) /* top of interrupt stack */
123 * Kernel debugger stack for each processor.
126 .globl EXT(db_stack_store)
128 .set ., .+(INTSTACK_SIZE*NCPUS)
131 * Stack for last-ditch debugger task for each processor.
134 .globl EXT(db_task_stack_store)
135 EXT(db_task_stack_store):
136 .set ., .+(INTSTACK_SIZE*NCPUS)
137 #endif /* MACH_KDB */
140 * per-processor kernel debugger stacks
143 .globl EXT(kgdb_stack_store)
144 EXT(kgdb_stack_store):
145 .set ., .+(INTSTACK_SIZE*NCPUS)
149 * Pointers to GDT and IDT. These contain linear addresses.
154 .word Times(8,GDTSZ)-1
155 .long EXT(gdt)+KVTOLINEAR
160 .word Times(8,IDTSZ)-1
161 .long EXT(idt)+KVTOLINEAR
166 * start_lock is very special. We initialize the
167 * lock at allocation time rather than at run-time.
168 * Although start_lock should be an instance of a
169 * hw_lock, we hand-code all manipulation of the lock
170 * because the hw_lock code may require function calls;
171 * and we'd rather not introduce another dependency on
172 * a working stack at this point.
174 .globl EXT(start_lock)
176 .long 0 /* synchronizes processor startup */
178 .globl EXT(master_is_up)
180 .long 0 /* 1 when OK for other processors */
182 .globl EXT(mp_boot_pde)
185 #endif /* NCPUS > 1 */
188 * All CPUs start here.
191 * protected mode, no paging, flat 32-bit address space.
192 * (Code/data/stack segments have base == 0, limit == 4G)
200 mov $0,%ax /* fs must be zeroed; */
201 mov %ax,%fs /* some bootstrappers don`t do this */
206 0: cmpl $0,PA(EXT(start_lock))
209 xchgl %eax,PA(EXT(start_lock)) /* locked */
213 cmpl $0,PA(EXT(master_is_up)) /* are we first? */
214 jne EXT(slave_start) /* no -- system already up. */
215 movl $1,PA(EXT(master_is_up)) /* others become slaves */
216 #endif /* NCPUS > 1 */
219 * Get startup parameters.
222 #include <i386/AT386/asm_startup.h>
225 * Build initial page table directory and page tables.
226 * %ebx holds first available physical address.
229 addl $(NBPG-1),%ebx /* round first avail physical addr */
230 andl $(-NBPG),%ebx /* to machine page size */
231 leal -KVTOPHYS(%ebx),%eax /* convert to virtual address */
232 movl %eax,PA(EXT(kpde)) /* save as kernel page table directory */
233 movl %ebx,%cr3 /* set physical address in CR3 now */
235 movl %ebx,%edi /* clear page table directory */
236 movl $(PTES_PER_PAGE),%ecx /* one page of ptes */
240 stosl /* edi now points to next page */
243 * Use next few pages for page tables.
245 addl $(KERNELBASEPDE),%ebx /* point to pde for kernel base */
246 movl %edi,%esi /* point to end of current pte page */
249 * Enter 1-1 mappings for kernel and for kernel page tables.
251 movl $(INTEL_PTE_KERNEL),%eax /* set up pte prototype */
253 cmpl %esi,%edi /* at end of pte page? */
255 movl %edi,%edx /* get pte address (physical) */
256 andl $(-NBPG),%edx /* mask out offset in page */
257 orl $(INTEL_PTE_KERNEL),%edx /* add pte bits */
258 movl %edx,(%ebx) /* set pde */
259 addl $4,%ebx /* point to next pde */
260 movl %edi,%esi /* point to */
261 addl $(NBPG),%esi /* end of new pte page */
263 movl %eax,(%edi) /* set pte */
264 addl $4,%edi /* advance to next pte */
265 addl $(NBPG),%eax /* advance to next phys page */
266 cmpl %edi,%eax /* have we mapped this pte page yet? */
267 jb 0b /* loop if not */
270 * Zero rest of last pte page.
272 xor %eax,%eax /* don`t map yet */
273 2: cmpl %esi,%edi /* at end of pte page? */
275 movl %eax,(%edi) /* zero mapping */
282 * Grab (waste?) another page for a bootstrap page directory
283 * for the other CPUs. We don't want the running CPUs to see
284 * addresses 0..3fffff mapped 1-1.
286 movl %edi,PA(EXT(mp_boot_pde)) /* save its physical address */
287 movl $(PTES_PER_PAGE),%ecx /* and clear it */
290 #endif /* NCPUS > 1 */
291 movl %edi,PA(EXT(first_avail)) /* save first available phys addr */
294 * pmap_bootstrap will enter rest of mappings.
298 * Fix initial descriptor tables.
300 lea PA(EXT(idt)),%esi /* fix IDT */
302 movl $(PA(fix_idt_ret)),%ebx
303 jmp fix_desc_common /* (cannot use stack) */
306 lea PA(EXT(gdt)),%esi /* fix GDT */
308 movl $(PA(fix_gdt_ret)),%ebx
309 jmp fix_desc_common /* (cannot use stack) */
312 lea PA(EXT(ldt)),%esi /* fix LDT */
314 movl $(PA(fix_ldt_ret)),%ebx
315 jmp fix_desc_common /* (cannot use stack) */
321 movl %cr3,%eax /* retrieve kernel PDE phys address */
322 movl KERNELBASEPDE(%eax),%ecx
323 movl %ecx,(%eax) /* set it also as pte for location */
324 /* 0..3fffff, so that the code */
325 /* that enters paged mode is mapped */
326 /* to identical addresses after */
327 /* paged mode is enabled */
329 addl $4,%eax /* 400000..7fffff */
330 movl KERNELBASEPDE(%eax),%ecx
333 movl $ EXT(pag_start),%ebx /* first paged code address */
336 orl $(CR0_PG),%eax /* set PG bit in CR0 */
338 movl %eax,%cr0 /* to enable paging */
340 jmp *%ebx /* flush prefetch queue */
343 * We are now paging, and can run with correct addresses.
346 lgdt EXT(gdtptr) /* load GDT */
347 lidt EXT(idtptr) /* load IDT */
348 LJMP(KERNEL_CS,EXT(vstart)) /* switch to kernel code segment */
351 * Master is now running with correct addresses.
354 mov $(KERNEL_DS),%ax /* set kernel data segment */
358 mov %ax,EXT(ktss)+TSS_SS0 /* set kernel stack segment */
359 /* for traps to kernel */
361 mov %ax,EXT(dbtss)+TSS_SS0 /* likewise for debug task switch */
362 mov %cr3,%eax /* get PDBR into debug TSS */
363 mov %eax,EXT(dbtss)+TSS_PDBR
367 movw $(KERNEL_LDT),%ax /* get LDT segment */
368 lldt %ax /* load LDT */
370 mov %ax,EXT(ktss)+TSS_LDT /* store LDT in two TSS, as well... */
371 mov %ax,EXT(dbtss)+TSS_LDT /* ...matters if we switch tasks */
373 movw $(KERNEL_TSS),%ax
374 ltr %ax /* set up KTSS */
379 lea EXT(eintstack),%esp /* switch to the bootup stack */
380 call EXT(machine_startup) /* run C code */
386 * master_up is used by the master cpu to signify that it is done
387 * with the interrupt stack, etc. See the code in pstart and svstart
388 * that this interlocks with.
391 .globl EXT(master_up)
393 pushl %ebp /* set up */
394 movl %esp,%ebp /* stack frame */
395 movl $0,%ecx /* unlock start_lock */
396 xchgl %ecx,EXT(start_lock) /* since we are no longer using */
397 /* bootstrap stack */
398 leave /* pop stack frame */
402 * We aren't the first. Call slave_main to initialize the processor
403 * and get Mach going on it.
406 .globl EXT(slave_start)
408 cli /* disable interrupts, so we don`t */
409 /* need IDT for a while */
410 movl EXT(kpde)+KVTOPHYS,%ebx /* get PDE virtual address */
411 addl $(KVTOPHYS),%ebx /* convert to physical address */
413 movl PA(EXT(mp_boot_pde)),%edx /* point to the bootstrap PDE */
414 movl KERNELBASEPDE(%ebx),%eax
415 /* point to pte for KERNELBASE */
416 movl %eax,KERNELBASEPDE(%edx)
417 /* set in bootstrap PDE */
418 movl %eax,(%edx) /* set it also as pte for location */
419 /* 0..3fffff, so that the code */
420 /* that enters paged mode is mapped */
421 /* to identical addresses after */
422 /* paged mode is enabled */
423 movl %edx,%cr3 /* use bootstrap PDE to enable paging */
425 movl $ EXT(spag_start),%edx /* first paged code address */
428 orl $(CR0_PG),%eax /* set PG bit in CR0 */
430 movl %eax,%cr0 /* to enable paging */
432 jmp *%edx /* flush prefetch queue. */
435 * We are now paging, and can run with correct addresses.
439 lgdt EXT(gdtptr) /* load GDT */
440 lidt EXT(idtptr) /* load IDT */
441 LJMP(KERNEL_CS,EXT(svstart)) /* switch to kernel code segment */
444 * Slave is now running with correct addresses.
447 mov $(KERNEL_DS),%ax /* set kernel data segment */
452 movl %ebx,%cr3 /* switch to the real kernel PDE */
455 movl CX(EXT(interrupt_stack),%eax),%esp /* get stack */
456 addl $(INTSTACK_SIZE),%esp /* point to top */
457 xorl %ebp,%ebp /* for completeness */
459 movl $0,%ecx /* unlock start_lock */
460 xchgl %ecx,EXT(start_lock) /* since we are no longer using */
461 /* bootstrap stack */
464 * switch to the per-cpu descriptor tables
467 pushl %eax /* pass CPU number */
468 call EXT(mp_desc_init) /* set up local table */
469 /* pointer returned in %eax */
470 subl $4,%esp /* get space to build pseudo-descriptors */
473 movw $(GDTSZ*8-1),0(%esp) /* set GDT size in GDT descriptor */
474 movl CX(EXT(mp_gdt),%eax),%edx
475 addl $ KVTOLINEAR,%edx
476 movl %edx,2(%esp) /* point to local GDT (linear address) */
477 lgdt 0(%esp) /* load new GDT */
479 movw $(IDTSZ*8-1),0(%esp) /* set IDT size in IDT descriptor */
480 movl CX(EXT(mp_idt),%eax),%edx
481 addl $ KVTOLINEAR,%edx
482 movl %edx,2(%esp) /* point to local IDT (linear address) */
483 lidt 0(%esp) /* load new IDT */
485 movw $(KERNEL_LDT),%ax
486 lldt %ax /* load new LDT */
488 movw $(KERNEL_TSS),%ax
489 ltr %ax /* load new KTSS */
494 call EXT(slave_main) /* start MACH */
497 #endif /* NCPUS > 1 */
500 * Convert a descriptor from fake to real format.
502 * Calls from assembly code:
503 * %ebx = return address (physical) CANNOT USE STACK
504 * %esi = descriptor table address (physical)
505 * %ecx = number of descriptors
508 * 0(%esp) = return address
509 * 4(%esp) = descriptor table address (physical)
510 * 8(%esp) = number of descriptors
512 * Fake descriptor format:
513 * bytes 0..3 base 31..0
514 * bytes 4..5 limit 15..0
515 * byte 6 access byte 2 | limit 19..16
516 * byte 7 access byte 1
518 * Real descriptor format:
519 * bytes 0..1 limit 15..0
520 * bytes 2..3 base 15..0
522 * byte 5 access byte 1
523 * byte 6 access byte 2 | limit 19..16
528 * bytes 4..5 selector
529 * byte 6 word count << 4 (to match fake descriptor)
530 * byte 7 access byte 1
533 * bytes 0..1 offset 15..0
534 * bytes 2..3 selector
536 * byte 5 access byte 1
537 * bytes 6..7 offset 31..16
541 pushl %ebp /* set up */
542 movl %esp,%ebp /* stack frame */
543 pushl %esi /* save registers */
545 movl B_ARG0,%esi /* point to first descriptor */
546 movl B_ARG1,%ecx /* get number of descriptors */
547 lea 0f,%ebx /* get return address */
548 jmp fix_desc_common /* call internal routine */
549 0: popl %ebx /* restore registers */
551 leave /* pop stack frame */
556 movw 6(%esi),%dx /* get access byte */
559 cmpb $0x04,%al /* gate or descriptor? */
563 movl 0(%esi),%eax /* get base in eax */
564 rol $16,%eax /* swap 15..0 with 31..16 */
565 /* (15..0 in correct place) */
566 movb %al,%dl /* combine bits 23..16 with ACC1 */
568 movb %ah,7(%esi) /* store bits 31..24 in correct place */
569 movw 4(%esi),%ax /* move limit bits 0..15 to word 0 */
570 movl %eax,0(%esi) /* store (bytes 0..3 correct) */
571 movw %dx,4(%esi) /* store bytes 4..5 */
576 movw 4(%esi),%ax /* get selector */
577 shrb $4,%dl /* shift word count to proper place */
578 movw %dx,4(%esi) /* store word count / ACC1 */
579 movw 2(%esi),%dx /* get offset 16..31 */
580 movw %dx,6(%esi) /* store in correct place */
581 movw %ax,2(%esi) /* store selector in correct place */
583 addl $8,%esi /* bump to next descriptor */
585 jmp *%ebx /* all done */
588 * put arg in kbd leds and spin a while
592 #define K_CMD_LEDS 0xed
593 #define K_STATUS 0x64
594 #define K_IBUF_FULL 0x02 /* input (to kbd) buffer full */
595 #define K_OBUF_FULL 0x01 /* output (from kbd) buffer full */
598 mov S_ARG0,%cl /* save led value */
600 0: inb $(K_STATUS),%al /* get kbd status */
601 testb $(K_IBUF_FULL),%al /* input busy? */
602 jne 0b /* loop until not */
604 mov $(K_CMD_LEDS),%al /* K_CMD_LEDS */
605 outb %al,$(K_RDWR) /* to kbd */
607 0: inb $(K_STATUS),%al /* get kbd status */
608 testb $(K_OBUF_FULL),%al /* output present? */
609 je 0b /* loop if not */
611 inb $(K_RDWR),%al /* read status (and discard) */
613 0: inb $(K_STATUS),%al /* get kbd status */
614 testb $(K_IBUF_FULL),%al /* input busy? */
615 jne 0b /* loop until not */
617 mov %cl,%al /* move led value */
618 outb %al,$(K_RDWR) /* to kbd */
620 movl $10000000,%ecx /* spin */
623 loop 0b /* a while */