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,_swapper_pg_dir,_tmp_floppy_area,_floppy_track_buffer
  16 /*
  17  * swapper_pg_dir is the main page directory, address 0x00000000
  18  */
  19 _swapper_pg_dir:
  20 startup_32:
  21         cld
  22         movl $0x10,%eax
  23         mov %ax,%ds
  24         mov %ax,%es
  25         mov %ax,%fs
  26         mov %ax,%gs
  27         lss _stack_start,%esp
  28         call setup_idt
  29         xorl %eax,%eax
  30 1:      incl %eax               # check that A20 really IS enabled
  31         movl %eax,0x000000      # loop forever if it isn't
  32         cmpl %eax,0x100000
  33         je 1b
  34 /* check if it is 486 or 386. */
  35         movl %esp,%edi          # save stack pointer
  36         andl $0xfffffffc,%esp   # align stack to avoid AC fault
  37         pushfl                  # push EFLAGS
  38         popl %eax               # get EFLAGS
  39         movl %eax,%ecx          # save original EFLAGS
  40         xorl $0x40000,%eax      # flip AC bit in EFLAGS
  41         pushl %eax              # copy to EFLAGS
  42         popfl                   # set EFLAGS
  43         pushfl                  # get new EFLAGS
  44         popl %eax               # put it in eax
  45         xorl %ecx,%eax          # check if AC bit is changed. zero is 486.
  46         jz 1f                   # 486
  47         pushl %ecx              # restore original EFLAGS
  48         popfl
  49         movl %edi,%esp          # restore esp
  50         movl %cr0,%eax          # 386
  51         andl $0x80000011,%eax   # Save PG,PE,ET
  52         orl $2,%eax             # set MP
  53         jmp 2f  
  54 /*
  55  * NOTE! 486 should set bit 16, to check for write-protect in supervisor
  56  * mode. Then it would be unnecessary with the "verify_area()"-calls.
  57  * 486 users probably want to set the NE (#5) bit also, so as to use
  58  * int 16 for math errors.
  59  */
  60 1:      pushl %ecx              # restore original EFLAGS
  61         popfl
  62         movl %edi,%esp          # restore esp
  63         movl %cr0,%eax          # 486
  64         andl $0x80000011,%eax   # Save PG,PE,ET
  65         orl $0x10022,%eax       # set NE and MP
  66 2:      movl %eax,%cr0
  67         call check_x87
  68         jmp after_page_tables
  69 
  70 /*
  71  * We depend on ET to be correct. This checks for 287/387.
  72  */
  73 check_x87:
  74         fninit
  75         fstsw %ax
  76         cmpb $0,%al
  77         je 1f
  78         movl %cr0,%eax          /* no coprocessor: have to set bits */
  79         xorl $6,%eax            /* reset MP, set EM */
  80         movl %eax,%cr0
  81         ret
  82 .align 2
  83 1:      .byte 0xDB,0xE4         /* fsetpm for 287, ignored by 387 */
  84         ret
  85 
  86 /*
  87  *  setup_idt
  88  *
  89  *  sets up a idt with 256 entries pointing to
  90  *  ignore_int, interrupt gates. It doesn't actually load
  91  *  idt - that can be done only after paging has been enabled
  92  *  and the kernel moved to 0xC0000000. Interrupts
  93  *  are enabled elsewhere, when we can be relatively
  94  *  sure everything is ok. This routine will be over-
  95  *  written by the page tables.
  96  */
  97 setup_idt:
  98         lea ignore_int,%edx
  99         movl $0x00080000,%eax
 100         movw %dx,%ax            /* selector = 0x0008 = cs */
 101         movw $0x8E00,%dx        /* interrupt gate - dpl=0, present */
 102 
 103         lea _idt,%edi
 104         mov $256,%ecx
 105 rp_sidt:
 106         movl %eax,(%edi)
 107         movl %edx,4(%edi)
 108         addl $8,%edi
 109         dec %ecx
 110         jne rp_sidt
 111         ret
 112 
 113 /*
 114  * I put the kernel page tables right after the page directory,
 115  * using 4 of them to span 16 Mb of physical memory. People with
 116  * more than 16MB will have to expand this.
 117  */
 118 .org 0x1000
 119 pg0:
 120 
 121 .org 0x2000
 122 pg1:
 123 
 124 .org 0x3000
 125 pg2:
 126 
 127 .org 0x4000
 128 pg3:
 129 
 130 .org 0x5000
 131 /*
 132  * empty_bad_page is a bogus page that will be used when out of memory,
 133  * so that a process isn't accidentally killed due to a page fault when
 134  * it is running in kernel mode..
 135  */
 136 .globl _empty_bad_page
 137 _empty_bad_page:
 138 
 139 .org 0x6000
 140 /*
 141  * empty_bad_page_table is similar to the above, but is used when the
 142  * system needs a bogus page-table
 143  */
 144 .globl _empty_bad_page_table
 145 _empty_bad_page_table:
 146 
 147 .org 0x7000
 148 /*
 149  * tmp_floppy_area is used by the floppy-driver when DMA cannot
 150  * reach to a buffer-block. It needs to be aligned, so that it isn't
 151  * on a 64kB border.
 152  */
 153 _tmp_floppy_area:
 154         .fill 1024,1,0
 155 /*
 156  * floppy_track_buffer is used to buffer one track of floppy data: it
 157  * has to be separate from the tmp_floppy area, as otherwise a single-
 158  * sector read/write can mess it up. It can contain one full track of
 159  * data (18*2*512 bytes).
 160  */
 161 _floppy_track_buffer:
 162         .fill 512*2*18,1,0
 163 
 164 after_page_tables:
 165         call setup_paging
 166         lgdt gdt_descr
 167         lidt idt_descr
 168         ljmp $0x08,$1f
 169 1:      movl $0x10,%eax         # reload all the segment registers
 170         mov %ax,%ds             # after changing gdt.
 171         mov %ax,%es
 172         mov %ax,%fs
 173         mov %ax,%gs
 174         lss _stack_start,%esp
 175         pushl $0                # These are the parameters to main :-)
 176         pushl $0
 177         pushl $0
 178         cld                     # gcc2 wants the direction flag cleared at all times
 179         call _start_kernel
 180 L6:
 181         jmp L6                  # main should never return here, but
 182                                 # just in case, we know what happens.
 183 
 184 /* This is the default interrupt "handler" :-) */
 185 int_msg:
 186         .asciz "Unknown interrupt\n\r"
 187 .align 2
 188 ignore_int:
 189         cld
 190         pushl %eax
 191         pushl %ecx
 192         pushl %edx
 193         push %ds
 194         push %es
 195         push %fs
 196         movl $0x10,%eax
 197         mov %ax,%ds
 198         mov %ax,%es
 199         mov %ax,%fs
 200         pushl $int_msg
 201         call _printk
 202         popl %eax
 203         pop %fs
 204         pop %es
 205         pop %ds
 206         popl %edx
 207         popl %ecx
 208         popl %eax
 209         iret
 210 
 211 
 212 /*
 213  * Setup_paging
 214  *
 215  * This routine sets up paging by setting the page bit
 216  * in cr0. The page tables are set up, identity-mapping
 217  * the first 16MB. The pager assumes that no illegal
 218  * addresses are produced (ie >4Mb on a 4Mb machine).
 219  *
 220  * NOTE! Although all physical memory should be identity
 221  * mapped by this routine, only the kernel page functions
 222  * use the >1Mb addresses directly. All "normal" functions
 223  * use just the lower 1Mb, or the local data space, which
 224  * will be mapped to some other place - mm keeps track of
 225  * that.
 226  *
 227  * For those with more memory than 16 Mb - tough luck. I've
 228  * not got it, why should you :-) The source is here. Change
 229  * it. (Seriously - it shouldn't be too difficult. Mostly
 230  * change some constants etc. I left it at 16Mb, as my machine
 231  * even cannot be extended past that (ok, but it was cheap :-)
 232  * I've tried to show which constants to change by having
 233  * some kind of marker at them (search for "16Mb"), but I
 234  * won't guarantee that's all :-( )
 235  */
 236 .align 2
 237 setup_paging:
 238         movl $1024*5,%ecx               /* 5 pages - swapper_pg_dir+4 page tables */
 239         xorl %eax,%eax
 240         xorl %edi,%edi                  /* swapper_pg_dir is at 0x000 */
 241         cld;rep;stosl
 242 /* Identity-map the kernel in low 4MB memory for ease of transition */
 243         movl $pg0+7,_swapper_pg_dir             /* set present bit/user r/w */
 244 /* But the real place is at 0xC0000000 */
 245         movl $pg0+7,_swapper_pg_dir+3072        /* set present bit/user r/w */
 246         movl $pg1+7,_swapper_pg_dir+3076        /*  --------- " " --------- */
 247         movl $pg2+7,_swapper_pg_dir+3080        /*  --------- " " --------- */
 248         movl $pg3+7,_swapper_pg_dir+3084        /*  --------- " " --------- */
 249         movl $pg3+4092,%edi
 250         movl $0xfff007,%eax             /*  16Mb - 4096 + 7 (r/w user,p) */
 251         std
 252 1:      stosl                   /* fill pages backwards - more efficient :-) */
 253         subl $0x1000,%eax
 254         jge 1b
 255         cld
 256         xorl %eax,%eax          /* swapper_pg_dir is at 0x0000 */
 257         movl %eax,%cr3          /* cr3 - page directory start */
 258         movl %cr0,%eax
 259         orl $0x80000000,%eax
 260         movl %eax,%cr0          /* set paging (PG) bit */
 261         ret                     /* this also flushes prefetch-queue */
 262 
 263 /*
 264  * The interrupt descriptor table has room for 256 idt's
 265  */
 266 .align 4
 267 .word 0
 268 idt_descr:
 269         .word 256*8-1           # idt contains 256 entries
 270         .long 0xc0000000+_idt
 271 
 272 .align 4
 273 _idt:
 274         .fill 256,8,0           # idt is uninitialized
 275 
 276 /*
 277  * The real GDT is also 256 entries long - no real reason
 278  */
 279 .align 4
 280 .word 0
 281 gdt_descr:
 282         .word 256*8-1
 283         .long 0xc0000000+_gdt
 284 
 285 .align 4
 286 _gdt:
 287         .quad 0x0000000000000000        /* NULL descriptor */
 288         .quad 0xc0c09a0000000fff        /* 16Mb at 0xC0000000 */
 289         .quad 0xc0c0920000000fff        /* 16Mb */
 290         .quad 0x0000000000000000        /* TEMPORARY - don't use */
 291         .fill 252,8,0                   /* space for LDT's and TSS's etc */

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