2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Real Mode Overview
Real Mode is the earliest working mode supported by the 80x86 processor and is also the most basic working mode. Real Mode is mainly used in the early MS-DOS operating system and other simple operating environments. In Real Mode, the processor can directly access 1MB of physical memory space. This mode is fully compatible with the working mode of the 8086 processor, hence the name "Real Mode".
Features of real mode
Address space restrictions
Segment address and offset address
No memory protection
Direct hardware access
Interrupts and processor initialization
Restrictions on executable code
Real mode memory layout
In real mode, the memory layout usually consists of the following parts:
BIOS Area
Interrupt vector table
Operating systems and applications
Overview
Protected Mode is an advanced working mode introduced by 80x86 processors. Compared with real mode, it provides more powerful memory management and protection functions. Protected Mode was first introduced on 80286 processors and was further expanded and enhanced on 80386 and later processors.
Features of protected mode
Expanded memory address space
Memory protection
Paging mechanism
Hardware-supported multitasking
Enhanced interrupt and exception handling
Virtual Memory
Protected Mode Memory Management
Segment selectors and segment descriptors
Paging mechanism
Protected Mode Applications
Protected mode is widely used in modern operating systems, such as Windows, Linux, Unix, etc. They use the characteristics of protected mode to implement advanced functions such as multi-tasking, multi-user and memory protection. Through protected mode, the operating system can effectively manage hardware resources and provide a stable and secure operating environment.
Protected mode is the foundation of modern operating systems and applications, making full use of the advanced features of 80x86 processors and improving system stability and security.
Virtual 8086 Mode Overview
Virtual 8086 Mode is a special mode provided by the 80x86 processor in protected mode, allowing the processor to run 8086 programs as if they were executed in real mode. This mode is mainly used for backward compatibility, allowing old DOS programs to run on modern operating systems (such as Windows and Linux) without leaving protected mode.
Features of Virtual 8086 Mode
Real mode compatibility
Paging and protection mechanisms
Interrupt and exception handling
Hardware virtualization support
Memory management in virtual 8086 mode
Segment:Offset Addressing
Paging mechanism
Application of virtual 8086 mode
Running Old DOS Programs
Virtual machines and emulators
Limitations of Virtual 8086 Mode
Memory address space
Performance overhead
Hardware limitations
Summarize
Virtual 8086 mode is a special mode provided by the 80x86 processor in protected mode, mainly for backward compatibility with old DOS programs. Through virtual 8086 mode, the processor can simulate the real mode operating environment in protected mode while retaining the characteristics and advantages of protected mode. This mode is widely used in running old software, virtual machines and emulators.
The memory management of the DOS (Disk Operating System) operating system is mainly based on the real mode memory model of the 8086/8088 processor. The memory address space in real mode is 1MB, from 0x00000 to 0xFFFFF. This 1MB address space is divided into several major areas, each with a specific purpose. The following is a detailed introduction to the memory arrangement of the DOS operating system.
Real mode memory model
In real mode, memory addresses are accessed by combining segment addresses and offset addresses into a 20-bit physical address. The segment address is shifted left by 4 bits and added to the offset address to form the actual physical address.
Memory layout
The layout of 1MB of memory space in real mode is roughly as follows:
Interrupt Vector Table (IVT): 0x00000 - 0x003FF
BIOS Data Area (BDA): 0x00400 - 0x004FF
Traditional memory (conventional memory): 0x00500 - 0x9FFFF
Display buffer: 0xA0000 - 0xBFFFF
Extended BIOS Data Area (EBDA): 0xC0000 - 0xC7FFF
BIOS extension area: 0xC8000 - 0xEFFFF
System BIOS: 0xF0000 - 0xFFFFF
The Intel 80386 processor introduced a more advanced memory addressing mechanism, providing more powerful memory management and protection capabilities than its predecessors. The 80386 supports two main memory addressing modes:Real ModeandProtected ModeIn protected mode, a paging mechanism is introduced to further enhance memory management capabilities. The following is a detailed introduction:
1. Real mode
In real mode, the 80386 processor uses the same memory addressing method as the 8086/8088, using a 20-bit address bus and being able to access 1MB of memory. The address is calculated using the segment address and the offset address:
物理地址 = (段地址 << 4) + 偏移地址
2. Protected Mode
Protected mode is the main working mode of the 80386 processor, which provides enhanced memory protection and management functions. Memory addressing in protected mode involves two layers of mechanisms:Segmentation mechanismandPaging mechanism。
2.1 Segmentation Mechanism
In protected mode, the 80386 processor uses segment selectors and segment descriptors to manage segments. Segment descriptors are stored in the global descriptor table (GDT) or the local descriptor table (LDT).
Segment Selector: 16 bits, including three fields:
Segment Descriptor: 8 bytes, including the following fields:
The linear address calculation under the segmentation mechanism is as follows:
线性地址 = 段基址 + 偏移地址
2.2 Paging Mechanism
The paging mechanism is another memory management method provided by the 80386 processor in protected mode. It realizes memory virtualization and protection by converting linear addresses into physical addresses. The paging mechanism involves page directories, page tables, and page frames.
The address translation of the paging mechanism is as follows:
物理地址 = 页帧基址 + 页内偏移
Diagram of paging mechanism:
- +----------------+----------------+----------------+
- | 页目录(10位) | 页表(10位) | 页内偏移(12位)|
- +----------------+----------------+----------------+
3. 80386 Memory Management Unit (MMU)
The MMU (Memory Management Unit) of 80386 is responsible for address translation between segmentation and paging, and implements memory protection.
CR0 Register: Control register used to enable/disable protection mode and paging.
CR3 Register: The base address of the storage page directory.
CR2 Register: Stores the linear address of the last page fault.
4. Protection Mechanism
In protected mode, the 80386 processor provides multiple protection mechanisms to ensure the security of memory and system:
1. Virtual Memory
The Windows operating system uses virtual memory management to combine physical memory (RAM) and virtual memory files (page files) on the hard disk to provide independent virtual address space for each process. Each process usually has 4GB of virtual address space (32-bit system), of which 2GB is used for user mode programs and 2GB is used for kernel mode. The virtual address space of 64-bit system is larger.
2. Virtual Address Space Layout
The virtual address space is divided into user mode and kernel mode areas:
User Mode Address Space
Kernel Mode Address Space
3. Memory Paging
Windows uses paging mechanism to manage memory and convert virtual address into physical address. The basic unit of paging is page, which is usually 4KB.
Page table structure
4. Memory Management Unit (MMU)
The processor's memory management unit (MMU) is responsible for translating virtual addresses into physical addresses, performing page replacement, and memory protection. The Windows operating system uses the MMU to implement the following functions:
5. Memory Protection
The Windows operating system protects memory through the following mechanisms:
6. Memory Allocation
Windows uses a variety of strategies and data structures for memory allocation:
7. Memory Management API
Windows provides a set of memory management APIs for use by applications and the system:
8. Page File
Windows uses a page file (Pagefile.sys) as part of virtual memory to swap infrequently used pages into the page file when physical memory is low. The location and size of the page file can be configured by the user.
1. Classification of interrupts and exceptions
Interrupts and exceptions can be divided into the following categories:
INT
Command trigger.2. Interrupt Vector Table (IVT)
The 80386 processor uses an Interrupt Vector Table to manage interrupts and exceptions. The IVT is a table containing 256 entries, each of which occupies 4 bytes and stores the address of the interrupt or exception handler. The base address of the IVT is stored in the IDTR (Interrupt Descriptor Table Register).
3. Interrupt Descriptor Table (IDT)
In protected mode, the 80386 processor uses an interrupt descriptor table (IDT) to store interrupt and exception handlers. The IDT contains interrupt gate, trap gate, and task gate descriptors, each of which occupies 8 bytes.
The base address and limits of the IDT are stored in the IDTR.
4. Interrupt handling process
When an interrupt or exception occurs, the 80386 processor performs the following steps:
IRET
The instruction restores CS, EIP, and EFLAGS to the state before the interrupt or exception occurred.5. Exception Types
The 80386 processor supports multiple exceptions, each with different error codes and handling methods:
Common exceptions include:
INTO
Raised when an instruction detects an overflow.6. Interrupt and exception handlers
Interrupt and exception handlers are special routines that are used to handle specific interrupts or exceptions. They are usually provided by the operating system and are responsible for restoring the system state, logging error information, and performing necessary recovery operations or interrupt responses.
7. Interrupt priority and masking
The 80386 processor supports interrupt priority and masking mechanisms, and manages the priority and masking of hardware interrupts through the PIC (Programmable Interrupt Controller). High-level interrupts can interrupt the processing of low-level interrupts, ensuring that critical events can be responded to in a timely manner.
1. Segment protection
The 80386 processor uses a segmentation mechanism to manage memory. Each segment has a descriptor that contains the segment's base address, limits, and access permissions. Segmentation protection ensures that a process can only access the memory area it is authorized to access.
Segment Descriptor
Segment descriptors are stored in the Global Descriptor Table (GDT) and the Local Descriptor Table (LDT). Each descriptor contains the following information:
Implementation of segment protection
When accessing memory, the processor looks up the segment descriptor based on the segment selector and performs the following checks:
2. Paging protection
The paging mechanism further refines memory management, and each page has its own access rights. The paging protection mechanism is implemented through page directories and page tables.
Page Directory and Page Table
Each page table entry contains the following information:
Implementation of Paging Protection
When accessing memory, the processor performs the following checks:
3. Privilege Level
The 80386 processor supports four privilege levels, from 0 to 3, with lower levels indicating higher privileges.
Implementation of privilege levels
Privilege levels are implemented through the following mechanisms:
When performing an access operation, the processor checks the CPL, DPL, and RPL to ensure that the privilege level rules are met. If not, a General Protection Fault is triggered.
4. System Segment and Gate Descriptors
The 80386 processor supports system segment and gate descriptors to implement task switching and interrupt handling.
System segment
Gate Descriptor
5. Task switching
The 80386 processor supports hardware task switching, which is implemented through the Task State Segment (TSS). Task switching can be triggered by interrupts, exceptions, or task gates.
Task State Segment (TSS)
TSS contains the register status, segment registers, stack pointer, and task link of the task. When the task is switched, the processor saves the status of the current task to TSS and loads the status of the new task.
The process of task switching
6. Interrupt and exception handling
Interrupt and exception handling are an important part of the 80386 processor protection mechanism. Interrupts and exceptions are managed by the Interrupt Descriptor Table (IDT), and permission checks and context switches are performed during processing.
Interrupt and exception handling
IRET
Restore CS, EIP, and EFLAGS to the state before the interrupt or exception occurred.1. User mode and kernel mode
The Windows operating system divides the processor's execution mode into user mode and kernel mode:
Switching between user mode and kernel mode
Switching between user mode and kernel mode is usually achieved through system calls. Applications request kernel services by calling system APIs, and the operating system switches the request to kernel mode for processing.
2. Memory protection
Virtual Memory
Windows uses a virtual memory mechanism to provide each process with an independent address space. This mechanism prevents processes from directly accessing each other's memory, thereby improving system security and stability.
Memory Segments
Windows uses memory segments to divide different types of data and code, and each segment has different access rights and protection mechanisms. For example, the code segment is read-only, while the data segment is readable and writable.
3. Access Control
The Windows operating system uses access control lists (ACLs) to manage the access permissions of users and processes to resources.
Security Identifier (SID)
Every user, group, and computer has a unique security identifier (SID) that identifies it.
Access Control List (ACL)
Each object (such as a file, directory, registry key, etc.) has an access control list, which contains a set of access control entries (ACEs) that define the access rights that different users and groups have to that object.
4. Privilege Management
Windows controls processes and the actions that users can perform through privilege management. Certain privileges are only granted to specific user groups (such as administrators), such as installing drivers, accessing system logs, etc.
security strategy
Windows provides a series of security policies that can configure User Account Control (UAC), password policy, audit policy, etc. to enhance system security.
5. Interrupt and exception handling
The Windows operating system uses interrupt and exception handling mechanisms to manage hardware interrupts, software interrupts, and processor exceptions.
Interrupt handling
The Interrupt Service Routine (ISR) is responsible for handling hardware interrupts and ensuring that external devices can respond in a timely manner.
Exception handling
The exception handler handles processor exceptions, such as division by zero errors, page faults, etc., to ensure that the system can take appropriate recovery measures when an exception occurs.
6. Multitasking
Windows allows multiple processes and threads to run simultaneously through a multitasking mechanism. The operating system uses a scheduler to manage the execution of processes and threads to ensure that system resources are fairly distributed.
Thread Scheduling
Windows uses a priority scheduling algorithm to allocate CPU time slices based on the thread's priority and state (such as ready, waiting, running, etc.).
Process Isolation
Each process has independent virtual address space and resources, which prevents one process from affecting the operation of another process and improves system stability.
7. Security Features
Windows provides a series of security features, such as Data Execution Prevention (DEP) and Address Space Layout Randomization (ASLR), to enhance the system's protection capabilities.
Data Execution Prevention (DEP)
DEP prevents code from executing in the data segment by marking certain memory areas as non-executable through a combination of hardware and software.
Address Space Layout Randomization (ASLR)
ASLR randomizes the memory address space of a process, making it more difficult for attackers to exploit vulnerabilities.