Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Operating system-returns real mode from protected mode

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

q. A small question can be raised from the last lesson as to whether the following sentence has the existence of Bug.

After entering the protected mode, we use the stack segment selection address to assign the ss at the first place, and then use printing. I feel that the existence of Bug is due to the use of the calling function, which requires stack space and the top pointer on the stack-esp register, but there is no assignment to the esp register in the code here. Here you need to look up the esp register.

a. Experiment with the code from last class

The step here is to first decompile to find the address value of the jump, and then set a breakpoint on bochs to run it to the call command in figure 5 through step-by-step debugging, and then the experimental reg command looks at the register address and finds that the esp register address is 0x00007c00, which is consistent with the stack top register address in the previous chapter, and the program runs as expected. There is a question here, why the address of the esp register is not assigned, why the value of the esp register points to the top of the stack in 32 protection mode, so you can experiment again.

Lab-verifying esp

! []

What runs here is, again, first look up the address at figure 1 through decompilation, then set the breakpoint and check the esp register value at the breakpoint, and then the single-step debug value jumps to check the esp register value, and finally enters the 32-bit protected mode to check the esp register address, and finds that all esp registers have the same value. Therefore, the esp register is not assigned in 32-bit protected mode.

a. Stack segment in protected mode

1. Specify a space and define a segment descriptor for it

two。 Define the selector based on the location in the segment descriptor table

3. Initialization segment register

4. Initialize the top pointer of the stack

b. General definition of stack segment (in protected mode)

It mainly modifies the segment descriptor and base address of the previous chapter, redefines a new code section, and defines a special stack segment in protected mode.

% include "inc.asm" org 0x9000jmp CODE16_ message [section .gdt]; GDT definition GDT_ENTRY: Descriptor 0,0, 0CODE32_DESC: Descriptor 0, Code32SegLen-1, DA_C + DA_32VIDEO_DESC: Descriptor 0xB8000, 0x07FFF, DA_DRWA + DA_32DATA32_DESC: Descriptor 0, Data32SegLen-1, DA_DR + DA_32STACK32_DESC: Descriptor 0 TopOfStack32, DA_DRW + DA_32 GDT endGdtLen equ $- GDT_ENTRYGdtPtr: dw GdtLen-1 dd 0; GDT SelectorCode32Selector equ (0x0001 attribute Dx-- > dh: row, dl: colPrintString: push ebp push eax push edi push cx push dxprint: mov cl, [ds:ebp] cmp cl, 0 je end mov eax, 80 mul dh add al, dl shl eax, 1 mov edi, eax mov ah, bl mov al, cl mov [gs:edi] Ax inc ebp inc dl jmp printend: pop dx pop cx pop edi pop eax pop ebp retCode32SegLen equ $- CODE32_ message [section .gs] [bits 32] STACK32_SEGMENT: times 1024 * 4 db 0Stack32SegLen equ $- STACK32_SEGMENTTopOfStack32 equ Stack32SegLen-1

The result of running under bochs after make

Now the esp is checked again through the experiment, first of all, the jump instruction is checked again through decompilation, then the breakpoint is set and step by step, and the change of the register address is checked step by step-the ss segment register points to the defined stack segment, finally the esp points to the limit of the stack space, and finally the string is printed, indicating that the custom stack segment space is used.

c. Can I return to real mode from protected mode?

A mysterious limitation in a.80x86

1. Cannot directly return to real mode from a 32-bit code snippet

two。 Real mode can only be returned indirectly from 16-bit code snippets

3. The register must be assigned with the appropriate selector before returning

b. A brief introduction to the design of processors

Processors after 1.80286 provide real mode compatible with 8086

two。 However, most processors run in protected mode

3. Therefore, the operation efficiency of protection mode is very important.

4. So, how does the processor efficiently access segment descriptors in memory?

Solution-cache memory

a. When setting the segment register using the selector

1. Access the segment descriptor in memory according to the selector

two。 Load the segment descriptor into the cache of the segment register

3. When segment descriptor information is needed, it is obtained directly from the cache

But a question arises: when the processor is running in real mode, will the cache of the segment register be used?

It should be noted here that in real mode, the cache still works. The segment base address is 32 bits, and its value is the value of the corresponding segment register multiplied by 16. In real mode, the effective bit of the segment base address is 20 bits, and the segment boundary is fixed to 0xFFFF (64K). The value of the segment attribute cannot be set, and can only continue to use the value set in the protected mode.

Therefore, when real mode is returned from protected mode-it is provided to load an appropriate descriptor selector to the relevant segment register so that the corresponding segment descriptor cache register contains appropriate segment bounds and attributes

The process of returning the real mode

Code

% include "inc.asm" org 0x9000jmp ENTRY_ message [section .gdt]; GDT definition Segment boundary Segment attributes GDT_ENTRY: Descriptor 0,0, 0CODE32_DESC: Descriptor 0, Code32SegLen-1, DA_C + DA_32VIDEO_DESC: Descriptor 0xB8000, 0x07FFF, DA_DRWA + DA_32DATA32_DESC: Descriptor 0, Data32SegLen-1, DA_DR + DA_32STACK32_DESC: Descriptor 0, TopOfStack32 DA_DRW + DA_32CODE16_DESC: Descriptor 0, 0xFFFF, DA_C UPDATE_DESC: Descriptor 0, 0xFFFF, DA_DRW GDT endGdtLen equ $- GDT_ENTRYGdtPtr: dw GdtLen-1 dd 0; GDT SelectorCode32Selector equ (0x0001

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report