[Operationg System Labs] My understanding and comments on BOOT.S in Linux0.00

Source: Internet
Author: User
Tags readable

(If there is an error, please correct it now!) )

! Boot.s
!
! It then loads the system at 0x10000, using the BIOS interrupts. Thereafter
! It disables all interrupts, changes to protected mode, and calls the
! Starting with 0x10000, the system is loaded using the BIOS interrupt.
! Make all interrupts fail, into protection mode ...

Bootseg = 0x07c0
! Boot segment, the BIOS will automatically load the operating system's program into this section to begin execution, when the operating system does not exist.

SYSSEG = 0x1000! System loaded at 0x10000 (65536).
! Linus commented that the system loaded into the 0x10000, we will come back to see what the sysseg is doing ... (counted as a legacy issue ①)

Syslen = 17! Sectors occupied.
! Sectors means to delete, this refers to the occupation of 17 sectors.
! Bochs in the analog loader is a 1.2MB floppy disk, floppy disk here a sector size is 512B

Entry Start
Start
Jmpi go, #BOOTSEG
! Jmpi is a jump within a paragraph, because it is in real mode, and the subsequent operand is the base address of the segment.
! The meaning of this sentence is to jump to the #bootseg as the base of the segment offset to go where, go? Look down:
Go:mov Ax,cs
MOV Ds,ax
MOV Ss,ax
! The above jmpi means to start with this go, followed by 3 MOV instructions, the CS is placed to DS and SS
! Indicates that the code snippet, data segment, and stack segment are overlapping in this piece of code
mov sp, #0x400! Arbitrary value >>512
! Arbitrary value: Guess what it means? (Legacy problem ②) ">>" is much larger than.
! OK, we ' ve written the message, now
! OK, we have written the message (where is this message?). Legacy issues ③)
Load_system:
! Start loading the system!
! The int 0x13 is the BIOS-retained interrupt instruction, obviously here to read the contents of the disk, and loaded into a certain place,
! Where is this place? (Legacy problem ④)
MOV dx, #0x0000! 32-bit the number starts with #
MOV cx, #0x0002! DX and CX, prepare for int 0x13
mov ax, #SYSSEG
MOV Es,ax! Loaded the #sysseg into ES.
XOR BX,BX! Empty BX
mov ax, #0x200 +syslen! Ah is set to 0x02,al to 0x11 (decimal is 17)
int 0x13
! AH=02H indicates to read sector
! AL: Number of sectors to read
! DL: Drive letter
! DH: Magnetic number of the read disk (the system is located at the No. 0 Head of drive No. 0)
! Low 8-digit number of CH track number
! CL low 5 bits into the read Start sector area code, bit 7-6 indicates the high 2 bits of the track number
! ES:BX the buffer address of the read out data.
! Return parameters:
! If the error state is stored in the Cf=1,ax. The readout data is arranged in the ES:BX area in sequence.
Jnc ok_load
! Indicates that CF is not set, that is, there is no error, jump to ok_load here to execute, otherwise die, the dead loop here
! Here's a solution to the legacy problem ④, where did the system code get read? ES:BX is 0x1000:0x0000 before calling int 0x13,
! Take a look at the beginning of the document, which is exactly where the author wants the system to be loaded. It also solves the legacy problem ①
Die:jmp die

! Now we want to move to protected mode ...
! The following starts to jump to protected mode!
Ok_load:
    cli           ! no Interrupts allowed!
   ! Turn off interrupts ... This time because you want to reset the interrupt vector table, overwriting the original interrupt descriptor, so you want to shut down the interrupt
    mov    ax, #SYSSEG
     mov    DS, Ax! DS into #sysseg, which represents where the system code starts
    xor    ax, ax! Ax 0
    mov     es, Ax! Es 0
    mov    cx, #0x2000!cx 0x2000
    sub    si,si 
    sub    di,di  si,di Qing 0
    Rep
     MOVW
   ! Rep means to repeat the MOVW instruction several times in the CX register, by default moving the data referred to by DS:SI to the location indicated by Es:di, ES is zero, It is clear that the code for the 0x1000 location is transferred to 0x0000

! Copy from ds:si = 0x1000:0x0000 to Es:di = 0x0000:0x0000
    mov    ax, #BOOTSEG
&nbs p;   mov    ds, ax
   ! To set the DS segment to 0X7C00, This location is the location of BOOT.S storage, I think the current BOOT.S not only exist in this 0x7c00 place, but also exist at the beginning of memory.
   ! Reset this 0x7c00 to get the data from here! You see, this isn't going to take idt_48 and gdt_48 out of here right now!
   ! How big is the 17-sector system? 17*512b < 9KB < 64KB (0x00000--0x10000), so it won't overwrite what's behind
    lidt    idt_48        ! Load IDT with 0,0
    lgdt    gdt_48       ! Load GDT with whatever appropriate
   ! Set the values for the IDTR and GDTR two registers, see the following idt_48 and gdt_48 locations for a specific value

! There are three things to do before entering protection mode, or three steps:
! 1~ set up the GDT.
! 2~ set the GDTR, as this directly determines the address of the first instruction after entering protected mode
! 3~ Changing the status word
! Absolute address 0x00000, in 32-bit protected mode.
mov ax, #0x0001! Protected mode (PE) bit
! Here LMSW load the status word, put the PE position, enter the protection mode ...
LMSW AX! This is it!
Jmpi 0,8! JMP offset 0 of Segment 8 (CS)
! Jump to CS in 0 to start the execution, is to enter the Head.s
! Why is it 8? Here, the system has entered the protection mode. 8 is represented here as a segment selector.
! (segment selector 16 bit, high 13 bit is index, 2nd bit is used to distinguish GDT (0) or LDT (1), 1th, 0 bit to represent privilege level,
! So when 8 is selected, 0000000000001000 is used to find out which paragraph is selected by moving the segment selector to the right three bits, 8 for the 1th item in the GDT table, the kernel snippet)

! The GDT global descriptor is set below, and the GDT's address is labeled "GDT" to calibrate
! The GDT has 8 bytes, so in gdt_48 there are 256 entires, 256

! The meanings of the subsequent values are then clearly annotated by Linus.
GDT:. Word 0,0,0,0! Dummy

. Word 0x07ff! 8mb-limit=2047 (2048*4096=8MB), Duan 8MB
. Word 0x0000! Base address=0x00000, site 0x00000, real mode with 20-bit address code, 2^20 addressing space.
. Word 0x9a00! Code Read/exec, which represents a snippet, readable executable
. Word 0x00c0! granularity=4096, 386, particle size (I don't understand)

    .word    0x07ff       ! 8mb-limit=2047 ( 2048*4096=8MB)
    .word    0x0000       ! Base address=0x00000
    .word    0x9200        ! Data Read/write, segment, readable writable
    .word    0x00c0        ! granularity=4096, 386

idt_48:. Word 0! IDT Limit=0 (This place is called a blocking interrupt)
. Word 0,0! IDT base=0l
gdt_48:. Word 0x7ff! GDT limit=2048, Entries GDT
. Word 0x7c00+gdt,0! GDT base = 07xxx
! In this state the GDT is set in this position.
! The high 32 bits of the GDTR represent the GDT base address, and the low 16 bits represent the length of the GDT, where 0x7ff represents 0-2047, or limit=2048
. org 510
! At the end of this instruction, this command is to let the following command start at 510, that is, it specifies that the length of the Boot.s is 512B, and finally there is a 2B long number
. Word 0xaa55

[Operationg System Labs] My understanding and comments on BOOT.S in Linux0.00

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.