Operating system development--Enter the protection mode

Source: Internet
Author: User

Still directly post code:

%macro descriptor 3dw%2 & 0FFFFh; Segment Boundary 1 (2 bytes) dw%1 & 0FFFFh; Segment Base 1 (2 bytes) db (% 1 >>) & 0FFh; Segment Base 2 (1 bytes) DW ((% 2 >> 8) & 0f00h) | (% 3 & 0F0FFH); Attribute 1 + segment bounds 2 + attribute 2 (2 bytes) db (% 1 >>) & 0FFh; Segment Base 3 (1 bytes)%endmacro; Total 8 bytes sa_rpl0equ0; ┓SA_RPL1EQU1; ┣RPLSA_RPL2EQU2; ┃SA_RPL3EQU3; ┛da_32equ4000h; 32-bit segment da_limit_4kequ8000h; Segment boundary granularity is 4K bytes da_dpl0equ 00h; DPL = 0da_dpl1equ 20h; DPL = 1da_dpl2equ 40h; DPL = 2da_dpl3equ 60h; DPL = 3da_drequ90h; The existing read-only data segment type value da_drwequ92h; The existing read-write data segment attribute value da_drwaequ93h; The existence of accessed read-write data segment type value da_cequ98h; There is only execution code snippet attribute value Da_crequ9ah; Existence of executable readable code snippet attribute value da_ccoequ9ch; There is only execution of the consistent code snippet attribute value Da_ccorequ9eh; An executable readable consistent code snippet attribute value exists; GDT------------------------------------------------------------------------------------------------------------                                                ----------------------------------------; Segment base segment bounds, attribute Label_gdt:descriptor 0, 0, 0;           NULL descriptor Label_desc_flat_c:descriptor      0, 0FFFFFH, DA_CR | da_32 | da_limit_4k; 0 ~ 4glabel_desc_flat_rw:descriptor 0, 0FFFFFH, DA_DRW | da_32 | da_limit_4k; 0 ~ 4glabel_desc_video:descriptor 0b8000h, 0FFFFH, DA_DRW | DA_DPL3; Memory first address; GDT------------------------------------------------------------------------------------------------------------ ----------------------------------------gdtlenequ$-label_gdtgdtptrdwgdtlen-1; Segment boundary dd0x80000 + LABEL_GDT; GDT Select Sub----------------------------------------------------------------------------------selectorflatcequlabel_ DESC_FLAT_C-LABEL_GDTSELECTORFLATRWEQULABEL_DESC_FLAT_RW-LABEL_GDTSELECTORVIDEOEQULABEL_DESC_VIDEO-LABEL_GDT + SA_RPL3; GDT selects sub-----------------------------------------------------------------------------------; ready to switch to protected mode moveax,cr0; Get CR0 configuration oreax,1; Modify the No. 0 bit of the CR0 (PE bit), enter the protection mode mov <span style= "White-space:pre" &GT;&LT;/SPAN&GT;CR0,EAX; Send the modified configuration back to CR0JMP DWORD SELECTORFLATC: (0x80000 + protect_mode)
[Bits 32] Protect_mode:mov Ax,selectorflatrwmov Ds,axmov Ss,axmov es,axmov fs,axmov Ax,selectorvideomov GS,AX         JMP $

Well, reader friends will certainly ask: in front of that large section of dried shrimp?

A: That is the famous paragraph selection, that is to be loaded into each paragraph inside, not in detail here, we will be in the next article to discuss paragraph selection, of course, there is a data structure, similar to the C language of the struct, this is to simplify the program, the most convenient way to complete the transfer of parameters.

Not much nonsense to say, go straight to the point, here to introduce CR0


CR0, which is used to control and determine the operating mode of the processor and the characteristics of the current execution of the task, (in fact, CR0--CR3), we do not introduce other bits here, only the PE bit (protected mode bit).

We first take out the CR0 configuration and then use the OR operator to set the PE bit (No. 0 bit) to 1, and then send the CR0 configuration back.

Finished ) of course not. We also have a historic jmp.

Here I suggest readers to read "X86 assembly language: From the actual mode to the protection mode" (if you have not learned long jump). Why do you want to see it? Because the next speaker may not be able to absorb the reader friend.

JMP DWORD SELECTORFLATC: (0x80000 + protect_mode)

Now I have a list of some questions that readers may ask.

One: Why add that 0x80000:

A: Because the identifiers of the NASM and C languages are not added to the current physical address by default (i.e., the start address of the current program as 0)

Two: Why add a DWORD

A: We do an experiment, "jmp 8:0x12345678", this sentence does not add DWORD, but he ran out of the result is "JMP 8:0x5678", now can understand why to add a DWORD it.

Three: Why is there a SELECTORFLATC identifier in front of it?

A: We'll talk about this later, it's a segment selector.

Four: Why must you have this jmp?

A: Because of this JMP (the stakes are too serious, hehe) to update CS, two to jump to 32-bit segment, three ....

It's so simple.

If you have any questions, you can contact me: email:[email protected]

If the reader friend also has the idea of developing the operating system, you can contact me.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Operating system development--Enter the protection 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.