Linux Kernel Series-operating system development into 32-bit protected mode

Source: Internet
Author: User

The source code is as follows:

; ==========================================; Pmtest1.asm; Compiling method: Nasm pmtest1.asm-o Pmtest1.bin; ==========================================%include "Pm.inc"; constants, macros, and some instructions org07c00hjmplabel_begin[section. GDT];                                                                         GDT;                Segment base, segment bounds, attribute Label_gdt:descriptor 0, 0, 0; Null descriptor Label_desc_code32:descriptor 0, segcode32len-1, Da_c + da_32; Non-Uniform code snippet Label_desc_video:descriptor 0b8000h, 0FFFFH, DA_DRW; Memory first address; GDT End gdtlenequ$-Label_gdt; GDT length GdtPtrdwGdtLen-1; GDT boundary dd0; GDT base Site; GDT Select Sub Selectorcode32equlabel_desc_code32-label_gdtselectorvideoequlabel_desc_video-label_gdt; END of [section. gdt][section. S16][bits16]label_begin:movax, Csmovds, Axmoves, Axmovss, AXMOVSP, 0100h; Initializes a 32-bit code snippet descriptor Xoreax, Eaxmovax, Csshleax,4addeax, Label_seg_code32movword [Label_desc_code32 + 2], axshreax, 16movbyte [Label_desc_code32 + 4], almovbyte [label_d Esc_code32 + 7], ah; Prepare for loading GDTR xoreax, Eaxmovax, Dsshleax, 4addeax, LABEL_GDT; EAX <-GDT Base Address Movdword [Gdtptr + 2], eax; [Gdtptr + 2] <-GDT base site; Load Gdtrlgdt[gdtptr]; Off interrupt CLI; Open Address line a20inal, 92horal, 00000010BOUT92H, AL; Ready to switch to protected mode moveax, Cr0oreax, 1movcr0, eax; Really enter the protection mode Jmpdword selectorcode32:0; The execution of this sentence will be loaded into CS SelectorCode32;  and jump to selectorcode32:0 Secretariat END of [section. s16][section. S32]; 32-bit code snippet. Jumped in by real mode. [BITS32] Label_seg_code32:movax, Selectorvideomovgs, ax; Video Segment Selector (purpose) Movedi, (80 * 11 + 79) * 2; Screen 11th, column 79th. Movah, 0Ch; 0000: Black Bottom 1100: Scarlet Letter Moval, ' P ' mov[gs:edi], ax; To this stop jmp$segcode32lenequ$-label_seg_code32; END of [section. S32]

The results are as follows, with a red p at the far right of the screen:

Source Code Analysis:

1. first the program jumps to Label_begin, jmp label_begin. Initializes all DS, ES, and SS segment registers to the current code snippet.

2. Initialize 32-bit code snippet descriptors

In real mode, where 8086 of the 16-bit CPU is addressed as a segment x16+ offset, and in protected mode, the segment register value becomes an index that points to the segment descriptor, which is the corresponding descriptor in the GDT, and the descriptor contains the final base address.

38-41 lines means that the current program code segment left 4 bits is x16, plus the offset address of label_seg_code32, the final form label_seg_code32 Physical address, 42-45 means to load this physical address to the segment descriptor corresponding to the segment base location, The segment descriptor format is as shown

Subgrade address 0-15-bit is LABEL_DESC_CODE32 + 2 address, 16-bit ax is passed in, line 43rd moves to the right 16 bits represents the 16-bit elimination that has passed in, and then passes the remaining two 8-bit high-low register values to the corresponding Subgrade address.

Descriptor is a macro defined in Pm.inc, as shown in the format of the segment descriptor:

3. The structure diagram for loading the GDTR,GDRT is shown below

The GDTR is the only register that points to the segment descriptor, and 48-55 rows are loaded into the GDTR register for the base address of the segment descriptor. 48 rows clears the EAX value, 49-52 is the LABEL_GDT physical address loaded to 20 lines of gdtptr, that is, the GDT base address. 55 lines load the contents of the Gdtptr address into the GDTR, the double-byte DW corresponds to the 16-bit boundary, and the double-word dd corresponds to the 32-bit base address.

4. Off Interrupt and open A20

The interrupt processing mechanism differs from the real mode in protected mode, so the interrupt is closed first and the instruction is the CLI. Open A20 for historical reasons to prevent rollback when the offset exceeds the maximum value. 58-63 rows

5. Switch to Protected mode

Just set the No. 0 position of the register CR0 to 1 on the line. 66-68 rows

6. Real access to protection mode

In jmp DWORD selectorcode32:0, this line is the code that really goes into protected mode, SELECTORCODE32 is a segment selector, and the segment selector is to find the corresponding segment descriptor to find the corresponding Subgrade address. The format of the segment selector is as shown in

Starting at bit 3 is the index of the segment descriptor, which, after execution, loads the segment base of the descriptor label_desc_code32, which is label_seg_code32, into CS.

The purpose of the DWORD is that it is currently 16-bit code, and if there is no DWORD, the offset address of theselectorcode32:0 will only intercept 16 bits if it exceeds 16 bits.

This has completely entered the 32-bit protection mode.

Linux Kernel Series-operating system development into 32-bit protected mode

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.