One, the kernel loader

Source: Internet
Author: User

The loader is not the kernel, we just need to know where the kernel is loaded, what the kernel's entry address is, and some technical details don't need to be overly concerned.

The kernel loader for XV6 consists of two files: Bootasm. S and BOOTMAIN.C

The main purpose of BOOTASM.S is to enter protected mode by real mode, the following is the code comment for BOOTASM.S:

1#加载器被BIOS加载到0x7c00地址处, this time CS =0, EIP = 7c00. Value of other registers2 #不关心3 4#include"asm.h"5#include"mmu.h"6 . Code167 . Global Start8 Start:9     CLITen  One xorw%ax,%ax A MOVW%ax,%ds - MOVW%ax,%es - MOVW%ax,%ss the  - #打开A20总线, this is a legacy of history. -Seta20.1: - inb $0x64,%al + Testb $0x2,%al -     jnzSeta20.1 +  A Movb $0xd1,%al at Outb%al,$0x64 -  -Seta20.2: - inb $0x64,%al - Testb $0x2,%al -     jnzSeta20.2 in  - Movb $0xdf,%al to Outb%al,$0x60 +  - #进入保护模式 the     LGDTGdtdesc * movl%cr0,%eax $ orl $CR 0_pe,%eaxPanax Notoginseng movl%eax,%cr0 -  theLJMP $ (seg_kcode<<3), $start +  A #保护模式代码 the . Code32 + Start32: - #设置段寄存器的值 $MOVW $ (seg_kdata<<3),%ax $ MOVW%ax,%ds - MOVW%ax,%es - MOVW%ax,%ss theMOVW $0,%ax - MOVW%ax,%fsWuyi MOVW $ax,%gs the  - #设置栈指针 Wu movl $start,%esp -     PagerBootmain About  $ #不应该从bootmain函数中返回, if returned, enter an infinite loop -  - Spin: -     jmpSpin A #加载器使用的GDT +. p2align2 the GDT: - seg_nullasm $ Seg_asm (sta_x| Sta_r, 0x0, 0xFFFFFFFF) #代码段 the seg_asm (Sta_w, 0x0, 0xFFFFFFFF) #数据段 the  the Gdtdesc: the. Word (GDTDESC-GDT-1) -. Long GDT

BOOTMAIN.C is mainly to load the kernel executable file into memory, the XV6 kernel executable is the ELF format, so to understand the elf file format.

Only the two headers in the elf file format are used here, just understand the meaning of the two table headers.

The first table header is the ELF head structure, located in the elf file header

#defineEi_nident 16typedefstruct{unsignedCharE_ident[ei_nident];//target file identification informationElf32_half E_type;//target file typeElf32_half E_machine;//Target Architecture TypeElf32_word e_version;//Target file versionElf32_addr E_entry;//the virtual address of the program entry, if not, can be 0Elf32_off E_phoff;//the offset of the Program Header table (in bytes), if not, can be 0Elf32_off E_shoff;//The offset of the section Header table (in bytes), if not, to 0Elf32_word E_flags;//saves the processor-specific flags associated with the file. The flag name is in Ef_machine_flag format. Elf32_half e_ehsize;//the size (in bytes) of the ELF head. Elf32_half e_phentsize;//the table item size (in bytes) of the Program Header table. Elf32_half E_phnum;//The number of table entries in the Program Header table. can be 0. Elf32_half e_shentsize;//table item size (in bytes) for the section Header table. Elf32_half E_shnum;//The number of table entries in the section Header table. can be 0. Elf32_half E_shstrndx;//The index of the table item in the section Header table that is related to the section name string table. If the file does not have a section name string table, this parameter can be shn_undef. }ELF32_EHDR;

We only use two items in this structure: E_phoff (the offset of the program Header table) and E_phnum (the number of table entries in the Program Header table).

The second structure is the program header, which indicates which parts of the elf file need to be loaded into memory, and a program header structure corresponds to a file segment to be loaded into memory.

typedefstruct{Elf32_word p_type; //Segment TypeElf32_off P_offset;//segment PositionElf32_addr p_vaddr;//gives the virtual address of the first byte of the segment that will be put into memoryElf32_addr p_paddr;//only for systems that are related to physical addressesElf32_word P_filesz;//gives the number of bytes that the segment occupies in the file imageElf32_word P_memsz;//gives the number of bytes that the segment occupies in the memory imageElf32_word P_flags;//paragraph-related flagsElf32_word p_align;//Snap To} ELF32_PHDR;

The P_offset in the structure indicates the offset of the segment in the file, P_filesz indicates the size of the segment, P_PADDR indicates the physical address to be loaded, and with these three pieces of information, the kernel executable can be loaded.

BOOTMAIN.C's work is to load portions of the kernel executable into the specified physical memory, based on the contents of the two structures above.

Finally jump to the entrance of the kernel, start executing kernel code, the kernel is loaded successfully.

One, the kernel loader

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.