enhanced backtrace output
[mit-jos.git] / boot / boot.S
blob7a91ab1aeabec8a6c305fcc3f95960aae652d404
1 #include <inc/mmu.h>
3 # Start the CPU: switch to 32-bit protected mode, jump into C.
4 # The BIOS loads this code from the first sector of the hard disk into
5 # memory at physical address 0x7c00 and starts executing in real mode
6 # with %cs=0 %ip=7c00.
8 .set PROT_MODE_CSEG, 0x8         # kernel code segment selector
9 .set PROT_MODE_DSEG, 0x10        # kernel data segment selector
10 .set CR0_PE_ON,      0x1         # protected mode enable flag
12 .globl start
13 start:
14   .code16                     # Assemble for 16-bit mode
15   cli                         # Disable interrupts
16   cld                         # String operations increment
18   # Set up the important data segment registers (DS, ES, SS).
19   xorw    %ax,%ax             # Segment number zero
20   movw    %ax,%ds             # -> Data Segment
21   movw    %ax,%es             # -> Extra Segment
22   movw    %ax,%ss             # -> Stack Segment
24   # Enable A20:
25   #   For backwards compatibility with the earliest PCs, physical
26   #   address line 20 is tied low, so that addresses higher than
27   #   1MB wrap around to zero by default.  This code undoes this.
28 seta20.1:
29   inb     $0x64,%al               # Wait for not busy
30   testb   $0x2,%al
31   jnz     seta20.1
33   movb    $0xd1,%al               # 0xd1 -> port 0x64
34   outb    %al,$0x64
36 seta20.2:
37   inb     $0x64,%al               # Wait for not busy
38   testb   $0x2,%al
39   jnz     seta20.2
41   movb    $0xdf,%al               # 0xdf -> port 0x60
42   outb    %al,$0x60
44   # Switch from real to protected mode, using a bootstrap GDT
45   # and segment translation that makes virtual addresses 
46   # identical to their physical addresses, so that the 
47   # effective memory map does not change during the switch.
48   lgdt    gdtdesc
49   movl    %cr0, %eax
50   orl     $CR0_PE_ON, %eax
51   movl    %eax, %cr0
52   
53   # Jump to next instruction, but in 32-bit code segment.
54   # Switches processor into 32-bit mode.
55   ljmp    $PROT_MODE_CSEG, $protcseg
57   .code32                     # Assemble for 32-bit mode
58 protcseg:
59   # Set up the protected-mode data segment registers
60   movw    $PROT_MODE_DSEG, %ax    # Our data segment selector
61   movw    %ax, %ds                # -> DS: Data Segment
62   movw    %ax, %es                # -> ES: Extra Segment
63   movw    %ax, %fs                # -> FS
64   movw    %ax, %gs                # -> GS
65   movw    %ax, %ss                # -> SS: Stack Segment
66   
67   # Set up the stack pointer and call into C.
68   movl    $start, %esp
69   call bootmain
71   # If bootmain returns (it shouldn't), loop.
72 spin:
73   jmp spin
75 # Bootstrap GDT
76 .p2align 2                                # force 4 byte alignment
77 gdt:
78   SEG_NULL                              # null seg
79   SEG(STA_X|STA_R, 0x0, 0xffffffff)     # code seg
80   SEG(STA_W, 0x0, 0xffffffff)           # data seg
82 gdtdesc:
83   .word   0x17                            # sizeof(gdt) - 1
84   .long   gdt                             # address gdt