]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/start.s
7618127ab6d272e33ba57051fbae9c8c599eb697
[apple/xnu.git] / osfmk / i386 / start.s
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
32 *
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.
38 *
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.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55
56 #include <platforms.h>
57 #include <cpus.h>
58 #include <mach_kdb.h>
59
60 #include <i386/asm.h>
61 #include <i386/proc_reg.h>
62 #include <assym.s>
63
64 #if NCPUS > 1
65
66 #define CX(addr,reg) addr(,reg,4)
67
68 #else
69
70 #define CPU_NUMBER(reg)
71 #define CX(addr,reg) addr
72
73 #endif /* NCPUS > 1 */
74
75 #include <i386/AT386/mp/mp.h>
76
77 /*
78 * GAS won't handle an intersegment jump with a relocatable offset.
79 */
80 #define LJMP(segment,address) \
81 .byte 0xea ;\
82 .long address ;\
83 .word segment
84
85
86
87 #define KVTOPHYS (-KERNELBASE)
88 #define KVTOLINEAR LINEAR_KERNELBASE
89
90
91 #define PA(addr) (addr)+KVTOPHYS
92 #define VA(addr) (addr)-KVTOPHYS
93
94 .data
95 .align 2
96 .globl EXT(_kick_buffer_)
97 EXT(_kick_buffer_):
98 .long 1
99 .long 3
100 .set .,.+16836
101 /*
102 * Interrupt and bootup stack for initial processor.
103 */
104 .align ALIGN
105 .globl EXT(intstack)
106 EXT(intstack):
107 .set ., .+INTSTACK_SIZE
108 .globl EXT(eintstack)
109 EXT(eintstack:)
110
111 #if NCPUS == 1
112 .globl EXT(int_stack_high) /* all interrupt stacks */
113 EXT(int_stack_high): /* must lie below this */
114 .long EXT(eintstack) /* address */
115
116 .globl EXT(int_stack_top) /* top of interrupt stack */
117 EXT(int_stack_top):
118 .long EXT(eintstack)
119 #endif
120
121 #if MACH_KDB
122 /*
123 * Kernel debugger stack for each processor.
124 */
125 .align ALIGN
126 .globl EXT(db_stack_store)
127 EXT(db_stack_store):
128 .set ., .+(INTSTACK_SIZE*NCPUS)
129
130 /*
131 * Stack for last-ditch debugger task for each processor.
132 */
133 .align ALIGN
134 .globl EXT(db_task_stack_store)
135 EXT(db_task_stack_store):
136 .set ., .+(INTSTACK_SIZE*NCPUS)
137 #endif /* MACH_KDB */
138
139 /*
140 * per-processor kernel debugger stacks
141 */
142 .align ALIGN
143 .globl EXT(kgdb_stack_store)
144 EXT(kgdb_stack_store):
145 .set ., .+(INTSTACK_SIZE*NCPUS)
146
147
148 /*
149 * Pointers to GDT and IDT. These contain linear addresses.
150 */
151 .align ALIGN
152 .globl EXT(gdtptr)
153 LEXT(gdtptr)
154 .word Times(8,GDTSZ)-1
155 .long EXT(gdt)+KVTOLINEAR
156
157 .align ALIGN
158 .globl EXT(idtptr)
159 LEXT(idtptr)
160 .word Times(8,IDTSZ)-1
161 .long EXT(idt)+KVTOLINEAR
162
163 #if NCPUS > 1
164 .data
165 /*
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.
173 */
174 .globl EXT(start_lock)
175 EXT(start_lock):
176 .long 0 /* synchronizes processor startup */
177
178 .globl EXT(master_is_up)
179 EXT(master_is_up):
180 .long 0 /* 1 when OK for other processors */
181 /* to start */
182 .globl EXT(mp_boot_pde)
183 EXT(mp_boot_pde):
184 .long 0
185 #endif /* NCPUS > 1 */
186
187 /*
188 * All CPUs start here.
189 *
190 * Environment:
191 * protected mode, no paging, flat 32-bit address space.
192 * (Code/data/stack segments have base == 0, limit == 4G)
193 */
194 .text
195 .align ALIGN
196 .globl EXT(pstart)
197 .globl EXT(_start)
198 LEXT(_start)
199 LEXT(pstart)
200 mov $0,%ax /* fs must be zeroed; */
201 mov %ax,%fs /* some bootstrappers don`t do this */
202 mov %ax,%gs
203
204 #if NCPUS > 1
205 jmp 1f
206 0: cmpl $0,PA(EXT(start_lock))
207 jne 0b
208 1: movb $1,%eax
209 xchgl %eax,PA(EXT(start_lock)) /* locked */
210 testl %eax,%eax
211 jnz 0b
212
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 */
217
218 /*
219 * Get startup parameters.
220 */
221
222 #include <i386/AT386/asm_startup.h>
223
224 /*
225 * Build initial page table directory and page tables.
226 * %ebx holds first available physical address.
227 */
228
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 */
234
235 movl %ebx,%edi /* clear page table directory */
236 movl $(PTES_PER_PAGE),%ecx /* one page of ptes */
237 xorl %eax,%eax
238 cld
239 rep
240 stosl /* edi now points to next page */
241
242 /*
243 * Use next few pages for page tables.
244 */
245 addl $(KERNELBASEPDE),%ebx /* point to pde for kernel base */
246 movl %edi,%esi /* point to end of current pte page */
247
248 /*
249 * Enter 1-1 mappings for kernel and for kernel page tables.
250 */
251 movl $(INTEL_PTE_KERNEL),%eax /* set up pte prototype */
252 0:
253 cmpl %esi,%edi /* at end of pte page? */
254 jb 1f /* if so: */
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 */
262 1:
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 */
268
269 /*
270 * Zero rest of last pte page.
271 */
272 xor %eax,%eax /* don`t map yet */
273 2: cmpl %esi,%edi /* at end of pte page? */
274 jae 3f
275 movl %eax,(%edi) /* zero mapping */
276 addl $4,%edi
277 jmp 2b
278 3:
279
280 #if NCPUS > 1
281 /*
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.
285 */
286 movl %edi,PA(EXT(mp_boot_pde)) /* save its physical address */
287 movl $(PTES_PER_PAGE),%ecx /* and clear it */
288 rep
289 stosl
290 #endif /* NCPUS > 1 */
291 movl %edi,PA(EXT(first_avail)) /* save first available phys addr */
292
293 /*
294 * pmap_bootstrap will enter rest of mappings.
295 */
296
297 /*
298 * Fix initial descriptor tables.
299 */
300 lea PA(EXT(idt)),%esi /* fix IDT */
301 movl $(IDTSZ),%ecx
302 movl $(PA(fix_idt_ret)),%ebx
303 jmp fix_desc_common /* (cannot use stack) */
304 fix_idt_ret:
305
306 lea PA(EXT(gdt)),%esi /* fix GDT */
307 movl $(GDTSZ),%ecx
308 movl $(PA(fix_gdt_ret)),%ebx
309 jmp fix_desc_common /* (cannot use stack) */
310 fix_gdt_ret:
311
312 lea PA(EXT(ldt)),%esi /* fix LDT */
313 movl $(LDTSZ),%ecx
314 movl $(PA(fix_ldt_ret)),%ebx
315 jmp fix_desc_common /* (cannot use stack) */
316 fix_ldt_ret:
317
318 /*
319 * Turn on paging.
320 */
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 */
328
329 addl $4,%eax /* 400000..7fffff */
330 movl KERNELBASEPDE(%eax),%ecx
331 movl %ecx,(%eax)
332
333 movl $ EXT(pag_start),%ebx /* first paged code address */
334
335 movl %cr0,%eax
336 orl $(CR0_PG),%eax /* set PG bit in CR0 */
337 orl $(CR0_WP),%eax
338 movl %eax,%cr0 /* to enable paging */
339
340 jmp *%ebx /* flush prefetch queue */
341
342 /*
343 * We are now paging, and can run with correct addresses.
344 */
345 LEXT(pag_start)
346 lgdt EXT(gdtptr) /* load GDT */
347 lidt EXT(idtptr) /* load IDT */
348 LJMP(KERNEL_CS,EXT(vstart)) /* switch to kernel code segment */
349
350 /*
351 * Master is now running with correct addresses.
352 */
353 LEXT(vstart)
354 mov $(KERNEL_DS),%ax /* set kernel data segment */
355 mov %ax,%ds
356 mov %ax,%es
357 mov %ax,%ss
358 mov %ax,EXT(ktss)+TSS_SS0 /* set kernel stack segment */
359 /* for traps to kernel */
360 #if MACH_KDB
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
364 mov $0,%eax
365 #endif
366
367 movw $(KERNEL_LDT),%ax /* get LDT segment */
368 lldt %ax /* load LDT */
369 #if MACH_KDB
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 */
372 #endif
373 movw $(KERNEL_TSS),%ax
374 ltr %ax /* set up KTSS */
375
376 mov $ CPU_DATA,%ax
377 mov %ax,%gs
378
379 lea EXT(eintstack),%esp /* switch to the bootup stack */
380 call EXT(machine_startup) /* run C code */
381 /*NOTREACHED*/
382 hlt
383
384 #if NCPUS > 1
385 /*
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.
389 */
390 .align ALIGN
391 .globl EXT(master_up)
392 LEXT(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 */
399 ret
400
401 /*
402 * We aren't the first. Call slave_main to initialize the processor
403 * and get Mach going on it.
404 */
405 .align ALIGN
406 .globl EXT(slave_start)
407 LEXT(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 */
412
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 */
424
425 movl $ EXT(spag_start),%edx /* first paged code address */
426
427 movl %cr0,%eax
428 orl $(CR0_PG),%eax /* set PG bit in CR0 */
429 orl $(CR0_WP),%eax
430 movl %eax,%cr0 /* to enable paging */
431
432 jmp *%edx /* flush prefetch queue. */
433
434 /*
435 * We are now paging, and can run with correct addresses.
436 */
437 LEXT(spag_start)
438
439 lgdt EXT(gdtptr) /* load GDT */
440 lidt EXT(idtptr) /* load IDT */
441 LJMP(KERNEL_CS,EXT(svstart)) /* switch to kernel code segment */
442
443 /*
444 * Slave is now running with correct addresses.
445 */
446 LEXT(svstart)
447 mov $(KERNEL_DS),%ax /* set kernel data segment */
448 mov %ax,%ds
449 mov %ax,%es
450 mov %ax,%ss
451
452 movl %ebx,%cr3 /* switch to the real kernel PDE */
453
454 CPU_NUMBER(%eax)
455 movl CX(EXT(interrupt_stack),%eax),%esp /* get stack */
456 addl $(INTSTACK_SIZE),%esp /* point to top */
457 xorl %ebp,%ebp /* for completeness */
458
459 movl $0,%ecx /* unlock start_lock */
460 xchgl %ecx,EXT(start_lock) /* since we are no longer using */
461 /* bootstrap stack */
462
463 /*
464 * switch to the per-cpu descriptor tables
465 */
466
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 */
471
472 CPU_NUMBER(%eax)
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 */
478
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 */
484
485 movw $(KERNEL_LDT),%ax
486 lldt %ax /* load new LDT */
487
488 movw $(KERNEL_TSS),%ax
489 ltr %ax /* load new KTSS */
490
491 mov $ CPU_DATA,%ax
492 mov %ax,%gs
493
494 call EXT(slave_main) /* start MACH */
495 /*NOTREACHED*/
496 hlt
497 #endif /* NCPUS > 1 */
498
499 /*
500 * Convert a descriptor from fake to real format.
501 *
502 * Calls from assembly code:
503 * %ebx = return address (physical) CANNOT USE STACK
504 * %esi = descriptor table address (physical)
505 * %ecx = number of descriptors
506 *
507 * Calls from C:
508 * 0(%esp) = return address
509 * 4(%esp) = descriptor table address (physical)
510 * 8(%esp) = number of descriptors
511 *
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
517 *
518 * Real descriptor format:
519 * bytes 0..1 limit 15..0
520 * bytes 2..3 base 15..0
521 * byte 4 base 23..16
522 * byte 5 access byte 1
523 * byte 6 access byte 2 | limit 19..16
524 * byte 7 base 31..24
525 *
526 * Fake gate format:
527 * bytes 0..3 offset
528 * bytes 4..5 selector
529 * byte 6 word count << 4 (to match fake descriptor)
530 * byte 7 access byte 1
531 *
532 * Real gate format:
533 * bytes 0..1 offset 15..0
534 * bytes 2..3 selector
535 * byte 4 word count
536 * byte 5 access byte 1
537 * bytes 6..7 offset 31..16
538 */
539 .globl EXT(fix_desc)
540 LEXT(fix_desc)
541 pushl %ebp /* set up */
542 movl %esp,%ebp /* stack frame */
543 pushl %esi /* save registers */
544 pushl %ebx
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 */
550 popl %esi
551 leave /* pop stack frame */
552 ret /* return */
553
554 fix_desc_common:
555 0:
556 movw 6(%esi),%dx /* get access byte */
557 movb %dh,%al
558 andb $0x14,%al
559 cmpb $0x04,%al /* gate or descriptor? */
560 je 1f
561
562 /* 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 */
567 /* in dh/dl */
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 */
572 jmp 2f
573
574 /* gate */
575 1:
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 */
582 2:
583 addl $8,%esi /* bump to next descriptor */
584 loop 0b /* repeat */
585 jmp *%ebx /* all done */
586
587 /*
588 * put arg in kbd leds and spin a while
589 * eats eax, ecx, edx
590 */
591 #define K_RDWR 0x60
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 */
596
597 ENTRY(set_kbd_leds)
598 mov S_ARG0,%cl /* save led value */
599
600 0: inb $(K_STATUS),%al /* get kbd status */
601 testb $(K_IBUF_FULL),%al /* input busy? */
602 jne 0b /* loop until not */
603
604 mov $(K_CMD_LEDS),%al /* K_CMD_LEDS */
605 outb %al,$(K_RDWR) /* to kbd */
606
607 0: inb $(K_STATUS),%al /* get kbd status */
608 testb $(K_OBUF_FULL),%al /* output present? */
609 je 0b /* loop if not */
610
611 inb $(K_RDWR),%al /* read status (and discard) */
612
613 0: inb $(K_STATUS),%al /* get kbd status */
614 testb $(K_IBUF_FULL),%al /* input busy? */
615 jne 0b /* loop until not */
616
617 mov %cl,%al /* move led value */
618 outb %al,$(K_RDWR) /* to kbd */
619
620 movl $10000000,%ecx /* spin */
621 0: nop
622 nop
623 loop 0b /* a while */
624
625 ret