One of Windows Background Knowledge: 386 processor mode and Windows Memory Management

Source: Internet
Author: User
Tags xms

1. 80x86 processor operating mode

The 80386 processor has three working modes: real mode, protection mode, and virtual 86 mode.

Project \ Mode Real Mode Protection Mode Virtual 86 mode
Main CPU Running Mode No Yes No
Addressing Mode Segment register content * 16 + in-segment offset address 32-bit addressing with up to 4 GB Space Segment register content * 16 + in-segment offset address
However, the operating system uses the paging mechanism to map the address spaces of different virtual 86 tasks to different physical addresses, so that each task considers itself using 0 ~ 1 MB address space
Memory paging, virtual memory No Supported Supported
Priority supported No The supported levels are divided into 0 ~ 34 Supported
Multi-task No Supported Supported
Operating System DoS Windows DOS running in WindowsProgram
Description Old 8086 Mode Main Mode Set to run the 8086 program in Protected Mode

 

2. Windows Memory Management

DoS Memory Arrangement

The DOS operating system runs in real mode, because the addressing range of the 8086 processor is only 1 MB.

    • High-end: the memory used by the system hardware is arranged at high-end, and the address is 640 kb starting from a0000h (384 KB.
    • Low-end: At the low-end of memory, the interrupt vector table and BIOS data area are arranged.
    • In the middle: less than 640 KB of memory is available for the operating system and applications from 640 H to a0000h; the memory is not available for applications. This is the famous "640kb limit ". However, even in the 640 kb, the DOS operating system occupies part of the low-end memory,The remaining 600 KB memory is actually available to applications..

When the 80386 processor is launched, the addressable memory range reaches 4 GB, And the XMS driver can be used to access all the 4 GB address spaces. However, the 16-bit segment addressing method limits the DOS program. The "visible" memory range remains within the range of H to ffff0h + 64 KB, all extended memory larger than 1 MB can only be used through the XMS driver for data exchange, and the execution space of the program does not increase.

 

Real-mode memory addressing

In real mode, a complete address consists of a segment address and an offset address. The segment address is placed in the 16-bit segment register, and then the 16-bit offset address is used in the instruction. When converting a processor, multiply the segment address by 10 h to obtain the starting address of the segment in the physical memory. Then, add a 16-bit offset address to obtain the actual physical address. For example, the actual location of a virtual address in the format of xxxx: yyyy in the memory is XXXX × 10h + YYYY.

 

In a single-task dossystem, an application can use all idle memory. After the program exits, the operating system recycles all the fragment memory and merges it into a large block of memory for use by the next program. An extreme condition in the memory merge process is that when multiple TSR programs exist in the system, the loaded TSR will remain in the middle of the memory after the TSR is uninstalled, separate idle memory into two areas. At this time, the maximum memory block used by the application can only be a large block of the two memories and cannot be used together.

 

Linear and physical addresses

The 80386 processor regards a 4 kb memory as a "page" memory. The physical memory on each page can be mapped to different linear addresses based on the "page Directory" and "page table. In this way, the memory ing of the physical address is connected together and regarded as continuous on the linear address.

This mechanism can effectively solve the problem of memory fragmentation merging.

 

The page table defines not only the address ing, but also the access attributes of the page, such as whether the page is writable, readable, and executable. For exampleCodeIf the Memory Page is set to readable and executable, writing data to code with insufficient permissions will cause protection exceptions.

 

 

Windows Memory Arrangement

In Windows, an swap file with a size of about twice the physical memory is created on the hard disk (the file name is win386.swp in Windows 9x and pagefile. sys in Windows NT. With the memory paging mechanism of the 80386 processor, swap files can be conveniently used as physical memory in addressing. You only need to read the content of the hard disk file into the physical memory when it is accessed, and then map the linear address to the physical memory again. Similarly, the executable files do not need to be loaded into the memory. You only need to establish a ing relationship in the page table and then call the physical memory when the access is actually reached.

Demonstrate Windows Memory Arrangement

In Windows, the linear address is mapped to different content in different time slices by switching between different page table content. In the physical memory, the code of the operating system and system DLL must be called by each application, so all time slices must be mapped; the user program is mapped only in the time slice to which it belongs, and the user DLL is mapped selectively. Of course, only one copy of XXX. DLL code is required in the physical memory.

 

Several important concepts in Win32 Programming

    • Each application has its own 4 GB addressing space. This space can store the operating system, system DLL, and user DLL Code, among which there are various functions that can be called by programs. Aside from other spaces, the rest is the application code, data, and address space that can be allocated.
    • Linear address spaces of different applications are isolated. Although they exist simultaneously in the physical memory, the code and data of other applications are not mapped to the addressable linear address in the time film to which a program belongs, therefore, it is not accessible. From the programming point of view, the program can use a 4 GB addressing space, and this space is "private.
    • The DLL program does not have its own "private" space. They are always mapped to the address space of other applications and run as part of other applications. The reason is simple. If it does not belong to the same address space as other programs, how should the application call it?

Therefore, memory management in the 80386 protection mode is so troublesome. It is a descriptive table, descriptor, and linear address mapped. Is Win32 compilation hard to write?

No. In Windows, "Everything is arranged" for the user program ". The specific manifestation is that the user program's code segment, data segment, and stack segment have all predefined segment descriptors. The starting address of these segments is 0 and the length limit is ffffffff. Therefore, they can be used to directly address all 4 GB address spaces. When the program starts to run, Cs, DS, es, and SS all point to the correct descriptor. during the life cycle of the program, the programmer does not have to change these register segments, you don't have to worry about the value (you can't change it if you want ).

Therefore, for Win32 Assembler programs, the entire source program does not need to appear in the segment register. This is unimaginable in DOS assembly programming. So,Not Win32 assemblySource codeThe segment register is not used, but the user does not have to care about the segment register during use.!

 

From "Win32 compilation tutorial" Luo yunbin

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.