Annotation of linux/boot/head.s, revision 1.1.1.5

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.