lab1 part one

Time:2020-5-22

lab1

Start process

  1. BIOS(saved on ROM) from theboot sectorRead upbootloader(under boot folder) program code to memory 0x7c00 to 0x7dff
  2. lab1 part onelab1 part oneBootloader program consists of two files: boot / boot. S and boot / main. C. boot / boot. S is to switch processor from real mode to real mode (the switched process can be readIa32-3a section 9.9 switch mode), the code pays attention to the following points:

    1. Enable A20 code (this section can be ignored, historical legacy reason)
    2. The global descriptor table is defined using the following code
    3. Set PE bit of CR0 register
    4. After setting CR0, you need to immediately make a call command or JMP command, which is a long jump to LJMP in the code. The reason for this isIt is not allowed to use MOV and other instructions to display the contents of the modified CS register. It is only allowed to use call or JMP instructions to implicitly modify the CS register
    5. The first one is the layout of gdtr. You can see why gdtdesc is set like this. The second one can see why segment Chooser is set to 8 (in addition, Jos is not segmented, so the start position of two segments in GDT is 0, and the end position is 0xFFFFFFFF)

      #Global descriptor table
      # Bootstrap GDT
      .p2align 2                                # force 4 byte alignment
      gdt:
      SEG_NULL                # null seg
      SEG(STA_X|STA_R, 0x0, 0xffffffff)    # code seg
      SEG(STA_W, 0x0, 0xffffffff)            # data seg
      
      gdtdesc:
      .word   0x17                            # sizeof(gdt) - 1
      .long   gdt                             # address gdt
  3. The implementation of boot / main. C reads kernel image from disk to memory. The difficulty of understanding here lies in not knowing the relevant knowledge of ELF file. It is recommended to read < programmer’s self-cultivation > >. In addition, the minimum reading unit of disk is sector, so the reading content may be larger than the actual size of the file
  4. Difference between VMA and LMA: most of the two values are the same, but the kernel values are different. In my opinion (to be verified), VMA (link address) is the link address between ELF file and other elf files. You can see that kernel.asm The address of assembly code in is VMA, and LMA (load address) is the address actually loaded into physical memory, The load address here is/ kernel.ld As determined in
  5. In boot / main. C((void (*)(void)) (ELFHDR->e_entry))();To enter the kernel, this is a function pointer call, e_ The entry value is determined in the ELF file. At this time, the kernel / entry. S file is entered

6. There’s a question why you need to reset the stack after entering the kernel. This is because the previous unit on the stack is 16bit, now it’s 32bit. It doesn’t make sense to keep it

PS. I forgot to save the following content. I don’t want to rewrite sad again

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]