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

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