root/arch/i386/kernel/head.S

/* [previous][next][first][last][top][bottom][index][help] */
   1 /*
   2  *  linux/arch/i386/head.S
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *  head.S contains the 32-bit startup code.
   9  */
  10 
  11 .text
  12 .globl _idt,_gdt,_stext,__stext
  13 .globl _swapper_pg_dir,_pg0
  14 .globl _empty_bad_page
  15 .globl _empty_bad_page_table
  16 .globl _empty_zero_page
  17 .globl _floppy_track_buffer
  18 
  19 #define __ASSEMBLY__
  20 #include <linux/tasks.h>
  21 #include <linux/fd.h>
  22 #include <asm/segment.h>
  23 
  24 #define CL_MAGIC_ADDR   0x90020
  25 #define CL_MAGIC        0xA33F
  26 #define CL_BASE_ADDR    0x90000
  27 #define CL_OFFSET       0x90022
  28 
  29 /*
  30  * swapper_pg_dir is the main page directory, address 0x00001000 (or at
  31  * address 0x00101000 for a compressed boot).
  32  */
  33 _stext:
  34 __stext:
  35 startup_32:
  36         cld
  37         movl $(KERNEL_DS),%eax
  38         mov %ax,%ds
  39         mov %ax,%es
  40         mov %ax,%fs
  41         mov %ax,%gs
  42         lss stack_start,%esp
  43 /*
  44  * Clear BSS first so that there are no surprises...
  45  */
  46         xorl %eax,%eax
  47         movl $__edata,%edi
  48         movl $__end,%ecx
  49         subl %edi,%ecx
  50         cld
  51         rep
  52         stosb
  53 /*
  54  * start system 32-bit setup. We need to re-do some of the things done
  55  * in 16-bit mode for the "real" operations.
  56  */
  57         call setup_idt
  58         xorl %eax,%eax
  59 1:      incl %eax               # check that A20 really IS enabled
  60         movl %eax,0x000000      # loop forever if it isn't
  61         cmpl %eax,0x100000
  62         je 1b
  63 /*
  64  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
  65  * confuse the debugger if this code is traced.
  66  * XXX - best to initialize before switching to protected mode.
  67  */
  68         pushl $0
  69         popfl
  70 /*
  71  * Copy bootup parameters out of the way. First 2kB of
  72  * _empty_zero_page is for boot parameters, second 2kB
  73  * is for the command line.
  74  */
  75         movl $0x90000,%esi
  76         movl $_empty_zero_page,%edi
  77         movl $512,%ecx
  78         cld
  79         rep
  80         movsl
  81         xorl %eax,%eax
  82         movl $512,%ecx
  83         rep
  84         stosl
  85         cmpw $(CL_MAGIC),CL_MAGIC_ADDR
  86         jne 1f
  87         movl $_empty_zero_page+2048,%edi
  88         movzwl CL_OFFSET,%esi
  89         addl $(CL_BASE_ADDR),%esi
  90         movl $2048,%ecx
  91         rep
  92         movsb
  93 1:
  94 /* check if it is 486 or 386. */
  95 /*
  96  * XXX - this does a lot of unnecessary setup.  Alignment checks don't
  97  * apply at our cpl of 0 and the stack ought to be aligned already, and
  98  * we don't need to preserve eflags.
  99  */
 100         movl $3,_x86
 101         pushfl                  # push EFLAGS
 102         popl %eax               # get EFLAGS
 103         movl %eax,%ecx          # save original EFLAGS
 104         xorl $0x40000,%eax      # flip AC bit in EFLAGS
 105         pushl %eax              # copy to EFLAGS
 106         popfl                   # set EFLAGS
 107         pushfl                  # get new EFLAGS
 108         popl %eax               # put it in eax
 109         xorl %ecx,%eax          # change in flags
 110         andl $0x40000,%eax      # check if AC bit changed
 111         je is386
 112         movl $4,_x86
 113         movl %ecx,%eax
 114         xorl $0x200000,%eax     # check ID flag
 115         pushl %eax
 116         popfl                   # if we are on a straight 486DX, SX, or
 117         pushfl                  # 487SX we can't change it
 118         popl %eax
 119         xorl %ecx,%eax
 120         andl $0x200000,%eax
 121         je is486
 122 isnew:  pushl %ecx              # restore original EFLAGS
 123         popfl
 124         /* get processor type */
 125         movl $1, %eax           # Use the CPUID instruction to 
 126         .byte 0x0f, 0xa2        # check the processor type
 127         movb %al, %cl           # save reg for future use
 128         andb $0x0f,%ah          # mask processor family
 129         movb %ah, _x86
 130         andb $0xf0, %eax        # mask model
 131         shrb $4, %al
 132         movb %al, _x86_model
 133         andb $0x0f, %cl         # mask mask revision
 134         movb %cl, _x86_mask
 135         movl %edx, _x86_capability
 136         /* get vendor info */
 137         xorl %eax, %eax                 # call CPUID with 0 -> return vendor ID
 138         .byte 0x0f, 0xa2                # CPUID
 139         movl %ebx, _x86_vendor_id       # lo 4 chars
 140         movl %edx, _x86_vendor_id+4     # next 4 chars
 141         movl %ecx, _x86_vendor_id+8     # last 4 chars
 142 
 143         movl %cr0,%eax          # 486+
 144         andl $0x80000011,%eax   # Save PG,PE,ET
 145         orl $0x50022,%eax       # set AM, WP, NE and MP
 146         jmp 2f
 147 is486:  pushl %ecx              # restore original EFLAGS
 148         popfl
 149         movl %cr0,%eax          # 486
 150         andl $0x80000011,%eax   # Save PG,PE,ET
 151         orl $0x50022,%eax       # set AM, WP, NE and MP
 152         jmp 2f
 153 is386:  pushl %ecx              # restore original EFLAGS
 154         popfl
 155         movl %cr0,%eax          # 386
 156         andl $0x80000011,%eax   # Save PG,PE,ET
 157         orl $2,%eax             # set MP
 158 2:      movl %eax,%cr0
 159         call check_x87
 160         call setup_paging
 161         lgdt gdt_descr
 162         lidt idt_descr
 163         ljmp $(KERNEL_CS),$1f
 164 1:      movl $(KERNEL_DS),%eax  # reload all the segment registers
 165         mov %ax,%ds             # after changing gdt.
 166         mov %ax,%es
 167         mov %ax,%fs
 168         mov %ax,%gs
 169         lss stack_start,%esp
 170         xorl %eax,%eax
 171         lldt %ax
 172         pushl %eax              # These are the parameters to main :-)
 173         pushl %eax
 174         pushl %eax
 175         cld                     # gcc2 wants the direction flag cleared at all times
 176         call _start_kernel
 177 L6:
 178         jmp L6                  # main should never return here, but
 179                                 # just in case, we know what happens.
 180 
 181 /*
 182  * We depend on ET to be correct. This checks for 287/387.
 183  */
 184 check_x87:
 185         movb $0,_hard_math
 186         clts
 187         fninit
 188         fstsw %ax
 189         cmpb $0,%al
 190         je 1f
 191         movl %cr0,%eax          /* no coprocessor: have to set bits */
 192         xorl $4,%eax            /* set EM */
 193         movl %eax,%cr0
 194         ret
 195 .align 2
 196 1:      movb $1,_hard_math
 197         .byte 0xDB,0xE4         /* fsetpm for 287, ignored by 387 */
 198         ret
 199 
 200 /*
 201  *  setup_idt
 202  *
 203  *  sets up a idt with 256 entries pointing to
 204  *  ignore_int, interrupt gates. It doesn't actually load
 205  *  idt - that can be done only after paging has been enabled
 206  *  and the kernel moved to 0xC0000000. Interrupts
 207  *  are enabled elsewhere, when we can be relatively
 208  *  sure everything is ok.
 209  */
 210 setup_idt:
 211         lea ignore_int,%edx
 212         movl $(KERNEL_CS << 16),%eax
 213         movw %dx,%ax            /* selector = 0x0010 = cs */
 214         movw $0x8E00,%dx        /* interrupt gate - dpl=0, present */
 215 
 216         lea _idt,%edi
 217         mov $256,%ecx
 218 rp_sidt:
 219         movl %eax,(%edi)
 220         movl %edx,4(%edi)
 221         addl $8,%edi
 222         dec %ecx
 223         jne rp_sidt
 224         ret
 225 
 226 
 227 /*
 228  * Setup_paging
 229  *
 230  * This routine sets up paging by setting the page bit
 231  * in cr0. The page tables are set up, identity-mapping
 232  * the first 4MB.  The rest are initialized later.
 233  *
 234  * (ref: added support for up to 32mb, 17Apr92)  -- Rik Faith
 235  * (ref: update, 25Sept92)  -- croutons@crunchy.uucp 
 236  * (ref: 92.10.11 - Linus Torvalds. Corrected 16M limit - no upper memory limit)
 237  */
 238 .align 2
 239 setup_paging:
 240         movl $1024*2,%ecx               /* 2 pages - swapper_pg_dir+1 page table */
 241         xorl %eax,%eax
 242         movl $_swapper_pg_dir,%edi      /* swapper_pg_dir is at 0x1000 */
 243         cld;rep;stosl
 244 /* Identity-map the kernel in low 4MB memory for ease of transition */
 245         movl $_pg0+7,_swapper_pg_dir            /* set present bit/user r/w */
 246 /* But the real place is at 0xC0000000 */
 247         movl $_pg0+7,_swapper_pg_dir+3072       /* set present bit/user r/w */
 248         movl $_pg0+4092,%edi
 249         movl $0x03ff007,%eax            /*  4Mb - 4096 + 7 (r/w user,p) */
 250         std
 251 1:      stosl                   /* fill the page backwards - more efficient :-) */
 252         subl $0x1000,%eax
 253         jge 1b
 254         cld
 255         movl $_swapper_pg_dir,%eax
 256         movl %eax,%cr3                  /* cr3 - page directory start */
 257         movl %cr0,%eax
 258         orl $0x80000000,%eax
 259         movl %eax,%cr0          /* set paging (PG) bit */
 260         ret                     /* this also flushes the prefetch-queue */
 261 
 262 /*
 263  * page 0 is made non-existent, so that kernel NULL pointer references get
 264  * caught. Thus the swapper page directory has been moved to 0x1000
 265  *
 266  * XXX Actually, the swapper page directory is at 0x1000 plus 1 megabyte,
 267  * with the introduction of the compressed boot code.  Theoretically,
 268  * the original design of overlaying the startup code with the swapper
 269  * page directory is still possible --- it would reduce the size of the kernel
 270  * by 2-3k.  This would be a good thing to do at some point.....
 271  */
 272 .org 0x1000
 273 _swapper_pg_dir:
 274 /*
 275  * The page tables are initialized to only 4MB here - the final page
 276  * tables are set up later depending on memory size.
 277  */
 278 .org 0x2000
 279 _pg0:
 280 
 281 .org 0x3000
 282 _empty_bad_page:
 283 
 284 .org 0x4000
 285 _empty_bad_page_table:
 286 
 287 .org 0x5000
 288 _empty_zero_page:
 289 
 290 .org 0x6000
 291 /*
 292  * floppy_track_buffer is used to buffer one track of floppy data: it
 293  * has to be separate from the tmp_floppy area, as otherwise a single-
 294  * sector read/write can mess it up. It can contain one full cylinder (sic) of
 295  * data (36*2*512 bytes).
 296  */
 297 _floppy_track_buffer:
 298         .fill 512*2*MAX_BUFFER_SECTORS,1,0
 299         
 300 stack_start:
 301         .long _init_user_stack+4096
 302         .long KERNEL_DS
 303 
 304 /* This is the default interrupt "handler" :-) */
 305 int_msg:
 306         .asciz "Unknown interrupt\n"
 307 .align 2
 308 ignore_int:
 309         cld
 310         pushl %eax
 311         pushl %ecx
 312         pushl %edx
 313         push %ds
 314         push %es
 315         push %fs
 316         movl $(KERNEL_DS),%eax
 317         mov %ax,%ds
 318         mov %ax,%es
 319         mov %ax,%fs
 320         pushl $int_msg
 321         call _printk
 322         popl %eax
 323         pop %fs
 324         pop %es
 325         pop %ds
 326         popl %edx
 327         popl %ecx
 328         popl %eax
 329         iret
 330 
 331 /*
 332  * The interrupt descriptor table has room for 256 idt's
 333  */
 334 .align 4
 335 .word 0
 336 idt_descr:
 337         .word 256*8-1           # idt contains 256 entries
 338         .long 0xc0000000+_idt
 339 
 340 .align 4
 341 _idt:
 342         .fill 256,8,0           # idt is uninitialized
 343 
 344 .align 4
 345 .word 0
 346 gdt_descr:
 347         .word (8+2*NR_TASKS)*8-1
 348         .long 0xc0000000+_gdt
 349 
 350 /*
 351  * This gdt setup gives the kernel a 1GB address space at virtual
 352  * address 0xC0000000 - space enough for expansion, I hope.
 353  */
 354 .align 4
 355 _gdt:
 356         .quad 0x0000000000000000        /* NULL descriptor */
 357         .quad 0x0000000000000000        /* not used */
 358         .quad 0xc0c39a000000ffff        /* 0x10 kernel 1GB code at 0xC0000000 */
 359         .quad 0xc0c392000000ffff        /* 0x18 kernel 1GB data at 0xC0000000 */
 360         .quad 0x00cbfa000000ffff        /* 0x23 user   3GB code at 0x00000000 */
 361         .quad 0x00cbf2000000ffff        /* 0x2b user   3GB data at 0x00000000 */
 362         .quad 0x0000000000000000        /* not used */
 363         .quad 0x0000000000000000        /* not used */
 364         .fill 2*NR_TASKS,8,0            /* space for LDT's and TSS's etc */

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