Head. s part of the original Linux kernel code

Source: Internet
Author: User
Linux kernel source code head. s-Linux general technology-Linux programming and kernel information. The following is a detailed description. /*
* Head. s contains the 32-bit startup code.
*
* NOTE !!! Startup happens at absolute address 0x00000000, which is also where
* The page directory will exist. The startup code will be overwritten

* The page directory.
*/
. Text
. Globl _ idt, _ gdt, _ pg_dir
_ Pg_dir:
Startup_32:
Movl $0x10, % eax @ ds, es, fs, gs point to the kernel data segment
Mov % ax, % ds
Mov % ax, % es
Mov % ax, % fs
Mov % ax, % gs
Lss _ stack_start, % esp @ ds send ss esp to stack_start (defined in sched. c)
@ The first change to the stack segment in protection mode, which is strange
@ Why can stack segments also increase positively?
Call setup_idt
Call setup_gdt
Movl $0x10, % eax # reload all the segment registers
Mov % ax, % ds # after changing gdt. CS was already
Mov % ax, % es # reloaded in 'setup _ gdt '@ has reload ??
Mov % ax, % fs
Mov % ax, % gs
Lss _ stack_start, % esp
Xorl % eax, % eax
1: incl % eax # check that A20 really IS enabled
Movl % eax, 0x000000
Cmpl % eax, 0x100000 @ why is this 0x100000 measured,
@ Understand, a20 not
Enable, 0x000000 is 0x100000
Je 1b
Movl % cr0, % eax # check math chip
Andl $0x80000011, % eax # Save PG, ET, PE
Testl $0x10, % eax
Jne 1f # ET is set-387 is present
Orl $4, % eax # else set emulate bit
1: movl % eax, % cr0
Jmp after_page_tables @ Note: Do not call jmp and do not return
/*
* Setup_idt
*
* Sets up a idt with 256 entries pointing
* Ignore_int, interrupt gates. It then loads
* Idt. Everything that wants to install itself
* In the idt-table may do so themselves. Interrupts
* Are enabled elsewhere, when we can be relatively
* Sure everything is OK. This routine will be over-
* Written by the page tables.
*/
Setup_idt:
Lea ignore_int, % edx
Movl $ Zero x 00080000, % eax
Movw % dx, % ax/* selector = 0x0008 = cs */
Movw $0x8E00, % dx/* interrupt gate-dpl = 0, present */
@ Ignore_int low 16 --> ax high 16 --> edx high

@ 8e00 --> dx 8 --> eax high-Text
Lea _ idt, % edi
Mov $256, % ecx
Rp_sidt:
Movl % eax, (% edi)
Movl % edx, 4 (% edi)
Addl $8, % edi
Dec % ecx
Jne rp_sidt
Lidt idt_descr
Ret

/*
* Setup_gdt
*
* This routines sets up a new gdt and loads it.
* Only two entries are currently built, the same
* Ones that were built in init. s. The routine
* Is VERY complicated at two whole lines, so this
* Rather long comment is certainly needed :-).
* This routine will beoverwritten by the page tables.
*/
Setup_gdt:
Lgdt gdt_descr
Ret @ jump to the main function

. Org 0x1000
Pg0:

. Org 0x2000
Pg1:

. Org 0x3000
Pg2: # This is not used yet, but if you
# Want to expand past 8 Mb, you'll have
# To use it.

. Org 0x4000
After_page_tables:
Pushl $0 # These are the parameters to main :-)
Pushl $0
Pushl $0
Pushl $ L6 # return address for main, if it decides.
Pushl $ _ main
Jmp setup_paging @ then jmp
L6:
Jmp L6 # main shoshould never return here,
# Just in case, we know what happens.

/* This is the default interrupt "handler ":-)*/
. Align 2
Ignore_int:
Incb 0xb8000 + 160 # put something on the screen
Movb $2, 0xb8000 + 161 # so that we know something
Iret # happened
  

/*
* Setup_paging
*
* This routine sets up paging by setting the page bit
* In cr0. The page tables are set up, identity-mapping
* The first 8 MB. The pager assumes that no illegal
* Addresses are produced (ie> 4 Mb on a 4 Mb machine ).
*
* NOTE! Although all physical memory shoshould be identity
* Mapped by this routine, only the kernel page functions
* Use the> 1 Mb addresses directly. All "normal" functions
* Use just the lower 1 Mb, or the local data space, which
* Will be mapped to some other place-mm keeps track
* That.
*
* For those with more memory than 8 Mb-tough luck. I 've
* Not got it, why shoshould you :-) The source is here. Change
* It. (Seriously-it shouldn't be too difficult. Mostly
* Change some constants etc. I left it at 8 Mb, as my machine
* Even cannot be extended past that (OK, but it was cheap :-)
* I 've tried to show which constants to change by having
* Some kind of marker at them (search for "8 Mb"), but I
* Won't guarantee that's all :-()
*/
. Align 2
Setup_paging:
Movl $1024*3, % ecx @ pg_dir pg_table erasing
Xorl % eax, % eax
Xorl % edi, % edi/* pg_dir is at 0x000 */
Cld; rep; stosl
Movl $ pg0 + 7, _ pg_dir/* set present bit/user r/w */
Movl $ pg1 + 7, _ pg_dir + 4 /*---------""---------*/
Movl $ pg1 + 4092, % edi
Movl $0x7ff007, % eax/* 8 Mb-4096 + 7 (r/w user, p )*/
Std
1: stosl/* fill pages backwards-more efficient :-)*/
Subl $ Zero x 1000, % eax
Jge 1b @ greater or equal
Xorl % eax, % eax/* pg_dir is at 0x0000 */
Movl % eax, % Cr/* Cr-page directory start */
Movl % cr0, % eax
Orl $0x80000000, % eax
Movl % eax, % cr0/* set paging (PG) bit */
Ret/* this also flushes prefetch-queue */

@ Page Directory pg_dir = 0 only two items (8 M ),
@ Pg1, The pg0 page table is backward filled, and the pg0 homepage is still zero
@ Ing like this, linear address = physical address

. Align 2
. Word 0
Idt_descr:
. Word 256*8-1 # idt contains 256 entries
. Long _ idt
. Align 2
. Word 0
Gdt_descr:
. Word 256*8-1 # so does gdt (not that's any @ is one different from head. s ??
. Long _ gdt # magic number, but it works for me: ^)

. Align 3
_ Idt:. fill 256,8, 0 # idt is uninitialized

_ Gdt:. quad 0x0000000000000000/* NULL descriptor */
. Quad 0x00c09a00000007ff/* 8 Mb */
. Quad 0x00c09200000007ff/* 8 Mb */
. Quad 0x0000000000000000/* TEMPORARY-don't use */
. Fill 252,8, 0/* space for LDT's and TSS's etc */
@ Kernel logical address (excluding segment registers) = Linear address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.