root/boot/head.S

/* [previous][next][first][last][top][bottom][index][help] */
   1 /*
   2  *  linux/boot/head.s
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *  head.s contains the 32-bit startup code.
   9  *
  10  * NOTE!!! Startup happens at absolute address 0x00000000, which is also where
  11  * the page directory will exist. The startup code will be overwritten by
  12  * the page directory.
  13  */
  14 .text
  15 .globl _idt,_gdt,
  16 .globl _swapper_pg_dir,_pg0
  17 .globl _empty_bad_page
  18 .globl _empty_bad_page_table
  19 .globl _tmp_floppy_area,_floppy_track_buffer
  20 
  21 /*
  22  * swapper_pg_dir is the main page directory, address 0x00001000
  23  */
  24 startup_32:
  25         cld
  26         movl $0x10,%eax
  27         mov %ax,%ds
  28         mov %ax,%es
  29         mov %ax,%fs
  30         mov %ax,%gs
  31         lss _stack_start,%esp
  32         call setup_idt
  33         xorl %eax,%eax
  34 1:      incl %eax               # check that A20 really IS enabled
  35         movl %eax,0x000000      # loop forever if it isn't
  36         cmpl %eax,0x100000
  37         je 1b
  38 /*
  39  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
  40  * confuse the debugger if this code is traced.
  41  * XXX - best to initialize before switching to protected mode.
  42  */
  43         pushl $0
  44         popfl
  45 /* check if it is 486 or 386. */
  46 /*
  47  * XXX - this does a lot of unnecessary setup.  Alignment checks don't
  48  * apply at our cpl of 0 and the stack ought to be aligned already, and
  49  * we don't need to preserve eflags.
  50  */
  51         movl %esp,%edi          # save stack pointer
  52         andl $0xfffffffc,%esp   # align stack to avoid AC fault
  53         pushfl                  # push EFLAGS
  54         popl %eax               # get EFLAGS
  55         movl %eax,%ecx          # save original EFLAGS
  56         xorl $0x40000,%eax      # flip AC bit in EFLAGS
  57         pushl %eax              # copy to EFLAGS
  58         popfl                   # set EFLAGS
  59         pushfl                  # get new EFLAGS
  60         popl %eax               # put it in eax
  61         xorl %ecx,%eax          # change in flags
  62         andl $0x40000,%eax      # check if AC bit changed
  63         jnz 1f                  # 486
  64         pushl %ecx              # restore original EFLAGS
  65         popfl
  66         movl %edi,%esp          # restore esp
  67         movl %cr0,%eax          # 386
  68         andl $0x80000011,%eax   # Save PG,PE,ET
  69         orl $2,%eax             # set MP
  70         jmp 2f  
  71 /*
  72  * NOTE! 486 should set bit 16, to check for write-protect in supervisor
  73  * mode. Then it would be unnecessary with the "verify_area()"-calls.
  74  * 486 users probably want to set the NE (#5) bit also, so as to use
  75  * int 16 for math errors.
  76  * XXX - the above is out of date.  We set all the bits, but don't take
  77  * advantage of WP (26 Dec 92).
  78  */
  79 1:      pushl %ecx              # restore original EFLAGS
  80         popfl
  81         movl %edi,%esp          # restore esp
  82         movl %cr0,%eax          # 486
  83         andl $0x80000011,%eax   # Save PG,PE,ET
  84         orl $0x50022,%eax       # set AM, WP, NE and MP
  85 2:      movl %eax,%cr0
  86         call check_x87
  87         call setup_paging
  88         lgdt gdt_descr
  89         lidt idt_descr
  90         ljmp $0x08,$1f
  91 1:      movl $0x10,%eax         # reload all the segment registers
  92         mov %ax,%ds             # after changing gdt.
  93         mov %ax,%es
  94         mov %ax,%fs
  95         mov %ax,%gs
  96         lss _stack_start,%esp
  97         pushl $0                # These are the parameters to main :-)
  98         pushl $0
  99         pushl $0
 100         cld                     # gcc2 wants the direction flag cleared at all times
 101         call _start_kernel
 102 L6:
 103         jmp L6                  # main should never return here, but
 104                                 # just in case, we know what happens.
 105 
 106 /*
 107  * We depend on ET to be correct. This checks for 287/387.
 108  */
 109 check_x87:
 110         movl $0,_hard_math
 111         fninit
 112         fstsw %ax
 113         cmpb $0,%al
 114         je 1f
 115         movl %cr0,%eax          /* no coprocessor: have to set bits */
 116         xorl $6,%eax            /* reset MP, set EM */
 117         movl %eax,%cr0
 118         ret
 119 .align 2
 120 1:      movl $1,_hard_math
 121         .byte 0xDB,0xE4         /* fsetpm for 287, ignored by 387 */
 122         ret
 123 
 124 /*
 125  *  setup_idt
 126  *
 127  *  sets up a idt with 256 entries pointing to
 128  *  ignore_int, interrupt gates. It doesn't actually load
 129  *  idt - that can be done only after paging has been enabled
 130  *  and the kernel moved to 0xC0000000. Interrupts
 131  *  are enabled elsewhere, when we can be relatively
 132  *  sure everything is ok. This routine will be over-
 133  *  written by the page tables.
 134  */
 135 setup_idt:
 136         lea ignore_int,%edx
 137         movl $0x00080000,%eax
 138         movw %dx,%ax            /* selector = 0x0008 = cs */
 139         movw $0x8E00,%dx        /* interrupt gate - dpl=0, present */
 140 
 141         lea _idt,%edi
 142         mov $256,%ecx
 143 rp_sidt:
 144         movl %eax,(%edi)
 145         movl %edx,4(%edi)
 146         addl $8,%edi
 147         dec %ecx
 148         jne rp_sidt
 149         ret
 150 
 151 
 152 /*
 153  * Setup_paging
 154  *
 155  * This routine sets up paging by setting the page bit
 156  * in cr0. The page tables are set up, identity-mapping
 157  * the first 4MB.  The rest are initialized later.
 158  *
 159  * (ref: added support for up to 32mb, 17Apr92)  -- Rik Faith
 160  * (ref: update, 25Sept92)  -- croutons@crunchy.uucp 
 161  * (ref: 92.10.11 - Linus Torvalds. Corrected 16M limit - no upper memory limit)
 162  */
 163 .align 2
 164 setup_paging:
 165         movl $1024*2,%ecx               /* 2 pages - swapper_pg_dir+1 page table */
 166         xorl %eax,%eax
 167         movl $_swapper_pg_dir,%edi      /* swapper_pg_dir is at 0x1000 */
 168         cld;rep;stosl
 169 /* Identity-map the kernel in low 4MB memory for ease of transition */
 170         movl $_pg0+7,_swapper_pg_dir            /* set present bit/user r/w */
 171 /* But the real place is at 0xC0000000 */
 172         movl $_pg0+7,_swapper_pg_dir+3072       /* set present bit/user r/w */
 173         movl $_pg0+4092,%edi
 174         movl $0x03ff007,%eax            /*  4Mb - 4096 + 7 (r/w user,p) */
 175         std
 176 1:      stosl                   /* fill the page backwards - more efficient :-) */
 177         subl $0x1000,%eax
 178         jge 1b
 179         cld
 180         movl $_swapper_pg_dir,%eax
 181         movl %eax,%cr3                  /* cr3 - page directory start */
 182         movl %cr0,%eax
 183         orl $0x80000000,%eax
 184         movl %eax,%cr0          /* set paging (PG) bit */
 185         ret                     /* this also flushes the prefetch-queue */
 186 
 187 /*
 188  * page 0 is made non-existent, so that kernel NULL pointer references get
 189  * caught. Thus the swapper page directory has been moved to 0x1000
 190  */
 191 .org 0x1000
 192 _swapper_pg_dir:
 193 /*
 194  * The page tables are initialized to only 4MB here - the final page
 195  * tables are set up later depending on memory size.
 196  */
 197 .org 0x2000
 198 _pg0:
 199 
 200 .org 0x3000
 201 _empty_bad_page:
 202 
 203 .org 0x4000
 204 _empty_bad_page_table:
 205 
 206 .org 0x5000
 207 /*
 208  * tmp_floppy_area is used by the floppy-driver when DMA cannot
 209  * reach to a buffer-block. It needs to be aligned, so that it isn't
 210  * on a 64kB border.
 211  */
 212 _tmp_floppy_area:
 213         .fill 1024,1,0
 214 /*
 215  * floppy_track_buffer is used to buffer one track of floppy data: it
 216  * has to be separate from the tmp_floppy area, as otherwise a single-
 217  * sector read/write can mess it up. It can contain one full track of
 218  * data (18*2*512 bytes).
 219  */
 220 _floppy_track_buffer:
 221         .fill 512*2*18,1,0
 222 
 223 /* This is the default interrupt "handler" :-) */
 224 int_msg:
 225         .asciz "Unknown interrupt\n\r"
 226 .align 2
 227 ignore_int:
 228         cld
 229         pushl %eax
 230         pushl %ecx
 231         pushl %edx
 232         push %ds
 233         push %es
 234         push %fs
 235         movl $0x10,%eax
 236         mov %ax,%ds
 237         mov %ax,%es
 238         mov %ax,%fs
 239         pushl $int_msg
 240         call _printk
 241         popl %eax
 242         pop %fs
 243         pop %es
 244         pop %ds
 245         popl %edx
 246         popl %ecx
 247         popl %eax
 248         iret
 249 
 250 /*
 251  * The interrupt descriptor table has room for 256 idt's
 252  */
 253 .align 4
 254 .word 0
 255 idt_descr:
 256         .word 256*8-1           # idt contains 256 entries
 257         .long 0xc0000000+_idt
 258 
 259 .align 4
 260 _idt:
 261         .fill 256,8,0           # idt is uninitialized
 262 
 263 /*
 264  * The real GDT is also 256 entries long - no real reason
 265  */
 266 .align 4
 267 .word 0
 268 gdt_descr:
 269         .word 256*8-1
 270         .long 0xc0000000+_gdt
 271 
 272 /*
 273  * This gdt setup gives the kernel a 1GB address space at virtual
 274  * address 0xC0000000 - space enough for expansion, I hope.
 275  */
 276 .align 4
 277 _gdt:
 278         .quad 0x0000000000000000        /* NULL descriptor */
 279         .quad 0xc0c39a000000ffff        /* 1GB at 0xC0000000 */
 280         .quad 0xc0c392000000ffff        /* 1GB */
 281         .quad 0x0000000000000000        /* TEMPORARY - don't use */
 282         .fill 252,8,0                   /* space for LDT's and TSS's etc */

/* [previous][next][first][last][top][bottom][index][help] */