当前位置:网站首页>Memory protection

Memory protection

2022-06-12 04:33:00 raindayinrain

         Use the protection function of memory , It can realize some valuable functions , Such as virtual memory management . When the processor accesses a segment that does not actually exist , Will cause an abnormal interrupt . The operating system can take advantage of this , By taking over the exception handling process , And the hard disk is used to switch in and out segments . Achieve small memory space to run as large as possible , As many procedures as possible . The objectives of this chapter :

        1. Know how to protect the memory of the processor through examples

        2. Understand the meaning and function of alias segment

        3. Take the string sorting process as an example , Demonstrate memory data access in protected mode , Experiencing them is different from accessing data segments in real mode .

12.1. Code list 12-1

12.2. Get into 32 Bit protection mode

12.2.1. Words mov ds, ax and mov ds, eax

         We know , Segment register 【 Selectors 】 The value of can only be transferred by memory unit or general-purpose register , The general instruction format is :

mov sreg, r/m16

         Here is a common example :

mov ds, ax

         stay 16 Bit mode and 32 In bit mode , Some older compilers generate different machine code .

[bits 16]
mov ds, ax ; 8E D8

[bits 32]
mov ds, ax ; 66 8E D8

         Because in 16 In bit mode , The default operand size is word 【2 byte 】, Therefore, the formation of 8E D8. stay 32 In bit mode , The default operand size is doubleword 【4 byte 】. Because the source operand in the instruction is 16 Bit AX, Therefore, prefix should be added to the compiled machine code 0x66 To reverse the default operand size , namely 66 8E D8.

        They are 16 Bit mode and 32 The machine instructions in bit mode are designed to be the same , That is all 8E D8, No instruction prefix is required .

        Many compilers think ,32 In bit mode , The source operand is 16 Bit register AX when , Instruction prefix should be added .

mov ds, eax

        Yes NASM compiler , Regardless of the processor mode , Whatever the form of the instruction , The compiled results of the following code are the same .

[bits 16]
mov ds, ax ; 8E D8
mov ds, eax; 8E D8

[bits 32]
mov ds, ax ; 8E D8
mov ds, eax; 8E D8

12.2.2. establish GDT And install the segment descriptor

        stay 32 On bit processor , Even in real mode , Also available 32 Bit register .

        32 The bit processor can perform the following division operations :

div r/m32

          among ,64 The divisor of bits is EDX:EAX in ,32 Bit divisor can be in 32 Bit general purpose register , Also available at 32 Bit memory unit . After the command is executed ,EAX The quotient in is the segment address , Only low 16 Bit effective ;EDX The remainder in is the offset address within the segment , Only low 16 Bit effective .

        The granularity of the segment is 4KB Section of , The limit value in the descriptor plus 1, Is how many there are in this paragraph 4KB. therefore , The segment boundary actually used is :

( Segment limit value in descriptor +1) * 0x1000 - 1

        For segments that extend up , The segment limit is numerically equal to the length of the segment minus 1.

        32 A code ,16 A code .

        If you need to access the data in the code segment , You can only reinstall a new descriptor for this segment , And define it as a readable and writable data segment . When writing , Through the new descriptor .

        When more than two descriptors describe and point to the same segment , Call the other descriptors aliases . If two programs want to share the same memory area , You can create a descriptor for each program separately , And they all point to the same memory segment , This is also an example of alias application .

        GDT The limit value is the overall dimension -1.

        When code snippets need to be modified . You can only redefine a new write permission descriptor for this segment , Use this descriptor to write . Alias technology is used in addition to reading and writing code segments . If two programs share a memory area , You can create a descriptor for each program separately , And all point to the same memory segment , This is also an example of alias application .

 12.3. Protection when modifying segment register

        When setting segment descriptor , The processor will perform a range validity check , Category check 【 Code snippets cannot be loaded into division CS In other segment registers 】.

        Whether the class of the descriptor matches the purpose of the segment register .

        Also check the descriptor P position . Such as P=0, Although the surface descriptor has been defined , But this segment does not actually exist in physical memory . here , Processor aborts processing , Throw an exception interrupt 11. commonly , An interrupt handler should be defined , Transfer the segment corresponding to the descriptor from external memory such as hard disk into memory , Then put P position . On interrupt return , Try the operation again .

        Such as P=1, Then the processor loads the descriptor into the descriptor cache of the segment register , Simultaneous placement A position 【 Only segment descriptors for the memory in question 】.

        once   The above rules have passed the verification , The processor loads the selector into the segment register . Only writable data segments can be loaded into SS,CS Only code segment descriptors are allowed to load . Yes DS,ES,FS and GS, It can be loaded with a value of 0 The selectors of .

12.4. Protection during address transformation

12.4.1. Protection during code segment execution

        

原网站

版权声明
本文为[raindayinrain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120429548432.html