当前位置:网站首页>19. Actual memory management of segment page combination
19. Actual memory management of segment page combination
2022-07-06 06:52:00 【PacosonSWJTU】
【README】
1. The content of this paper is summarized from B standing 《 operating system - Li Zhijun, teacher of Harbin Institute of technology 》, The content is great , Wall crack recommendation ;
2. Paragraphs and pages
- paragraph : The user program adopts a segmented structure ;
- page : The operating system uses paging mechanism to manage physical memory ;
- Paragraph page combination : Programmers want to use segments , Physical memory wants to use pages , So the operating system needs to combine segments and pages to manage memory ;
3. The working mechanism of paragraphs and pages
problem : How to make Segmented memory management And Paging memory management Connect ?
【1】 Virtual memory
1) Virtual memory definition
- The operating system realizes virtual memory by program , Combine paragraphs with pages ; As shown in the figure below .
【 The illustration 】
- From the user's perspective , Program segment storage ;
- From the perspective of physical memory , Memory is managed according to pages ;
2) Segment pages exist at the same time : Segments are user oriented / Page to hardware
【 The illustration 】
User program segments are stored in virtual memory , Include data segments , Code snippets, etc ;
Take the user code segment as an example :
Divide the user code segment into 3 Memory pages for storage , The virtual address of a segment is 0x00345008, The physical address of the map is 0x7008;
Add :
- The code segment base address of virtual memory is stored in cs In the segment register , The offset is stored in ip In the register ;
【2】 Relocation when segment pages exist at the same time ( Address translation )
0) background
- The above introduction passes the subsection , Each page stores the program in multiple memory pages ;
- In order for users to use memory , It should also provide relocation function when segment pages exist at the same time ( The function of translating logical address into physical address )
1) Relocation when segment pages exist at the same time ( Relocation is also called address translation , Logical address is translated into physical address )
【 The illustration 】
- step 1: Get the logical address of the program , The structure is Segment number + The offset , Use them separately cs : ip Storage segment number , Offset ;
- step 2: Query the segment base address from the segment table according to the segment number , The base address of this segment is the virtual address ;
- step 3: Calculate the page number according to the virtual address ( Such as virtual address 0x1756 The page number of is 17);
- step 4: Query the page frame number from the page table according to the page number ( Physical page number ), For example, the page frame number is 3;
- step 5: According to the page frame number ( Physical page number ) And the in page offset to calculate the physical memory address , Such as 0x0356; So far, the translation from logical address to physical memory address has been completed ;
Last : The operating system sends the physical memory address into the address bus to read and write the data on the memory address ;
【 Summary 】 Relocation when segment pages exist at the same time ( Address translation )
- Map from logical address to virtual address , The process of mapping virtual addresses to physical addresses ;
- The operating system is based on the above 2 Layer address mapping , Realize both user oriented support segmentation , It also supports paging management for physical memory ;
- therefore It forms a memory management mode combining segments and pages , Its core lies in Virtual memory ;
【3】 Example of memory management combined with page segmentation
The core of memory management is memory allocation , So from the program into memory , Start with memory ( Using memory means fetching and executing );
【3.1】 Specific measures for memory management during page segmentation
1) Specific measures
- Allocation segment , Create segment table ;
- Assign page , Create a page table ;
- Process driven memory usage map ( After allocating memory to the process , The program in the process can be put into memory );
- From process fork Memory allocation in starts ;( process fork, Create a new process , You need to allocate memory space for the new process , So that the logical address in the program can be correctly translated to the physical address )
2) How to load a program into memory in segmented memory ?
- step 1: Create segment table : Put the user program segment ( Such as code snippet , Data segment ) Map to the segment in the virtual address ; Such as dividing an area on the virtual memory and allocating it to the user data segment , So as to establish the mapping relationship , That is, create a segment table ;
- How to divide ? Partition algorithm is used to partition virtual memory ( You only need to store the base address and length of the partition );
- step 2: Create a page table : Segment the virtual address into several logical pages , Such as 3 A logical page ;3 Logical pages and physical memory 3 Physical pages are mapped , That is, create a page table ;
Add : Virtual memory does not exist , The operating system is programmed ;
3) The schematic diagram of program loading memory under segmented memory is as follows :
【 The illustration 】
Virtual address segment table structure ( Partition algorithm is used to partition virtual memory , Such as the best partition algorithm , Partition algorithm needs to store base address and length ):
Segment number | Base address ( Virtual address ) | length | Protect |
0 | Xxx | Xx | xx |
1 | 0x45000 | 60K | R/W |
Page table structure :
Logical page number | Page frame number ( Physics page ) | Protect |
Xx | xx | xx |
...... | ...... | ...... |
0x45 | 7 | R/W |
3.1) Logical address :
- Segment number + offset , Pass respectively cs and ip Register to give ;
3.2) Logical address ( Like data segments : 0x300) The steps of translating to physical address are as follows :
- according to cs Get the segment number ( The segment number of the data segment ); (cs Is the code segment register )
- Query the segment table according to the segment number to get the segment base address ( Virtual address ) ;
- Calculate the logical page number according to the virtual address ; Such as virtual address 0x45000, Its logical page number is 0x45;
- Query the page table according to the logical page number to get the page frame number ( Physical page number ); For example, according to the logical page number 45 Get the physical page number 7;
- According to the physical page number 7 And offset address (ip Registers give , Such as 0x300) Get the final physical memory address 0x7300; That is, the instructions in the data segment mov [300],0 Logical address in 300 The translated physical address is 0x7300 ( hold 0 Assign to physical address 0x7300 Memory unit );
Address translation ( Redirect ) After completion , The operating system sends the physical address into the address bus to read and write the data on the address ;
【4】 Segment page memory management code implementation
【4.1】 Allocate virtual memory , Create segment table ( The first 1 Step )
【 Code interpretation 】
- copy_process function : Create a new process ;
- copy_mem function : The memory allocation of the new process is the same as that of the current process ;
- Add :LDT The table stores the segment base addresses of multiple segments of the program ;
1)copy_mem Code ( Allocate virtual memory , Create segment table ):
int copy_mem(int nr, task_struct *p)
{
unsigned long new_data_base;
// new_data_base Is the base address of virtual memory , In fact, it is the division of virtual memory ; The virtual memory space of each new process is 64M;
// 0 The virtual memory space of process number is 0~64M-1;1 The virtual memory space of process number is 64M~2*64M-1;......
new_data_base = nr * 0x4000000; // 64M * nr ,nr Which process
// Create segment table , Set the base address for the code segment of the segment table ;p yes pcb; Process switching , The segment table switches accordingly ;
set_base(p->ldt[1], new_data_base);
// Create segment table , Set the base address for the data segment of the segment table ;p yes pcb; Process switching , The segment table switches accordingly ;
set_base(p->ldt[2], new_data_base);
}
1.1) Virtual address of each process
Each process occupies 64M Virtual address space for , No overlap ;
【4.2】 Allocate physical memory , Create a page table ( The first 2 Step )
1)copy_mem Copy memory code ( The detailed steps )
// Copy memory
int copy_mem(int nr, task_struct *p)
{
unsigned long old_data_base;
old_data_base = get_base(current->ldt[2]);
// copy_page_tables The function of is to assign the physical page of the old process to the new process , Both use the same physical page
// The new process shares the physical page of the old process , old_data_base,new_data_base They are the parent process , Virtual address base address of the new process ;
// Copy page table , Modify the virtual address , Create a page table for the new process
copy_page_tables(old_data_base, new_data_base, data_limit);
}
// Copy page table , Modify the virtual address , Create a page table for the new process
int copy_page_tables(unsigned long from, unsigned long to, long size)
{
// Bit operation obtains the page directory number pointer of the parent process from_dir ;
from_dir = (unsigned long *) ( (from>>20) & 0xffc );
// Bit operation yields new ( Son ) Pointer to the page directory number of the process to_dir ;
to_dir = (unsigned long *) ( (to >> 20) & 0xffc );
// Number of bytes copied
size = (unsigned long) (size + 0x3fffff) >> 22;
//
for (; size-- > 0; from_dir++, to_dir++) {
// from_page_table Is the base address of the parent process page table
from_page_table = (0xfffff000 & *from_dir);
// apply ( Distribute ) A physical memory page
to_page_table = get_free_page();
// Assign the new physical memory page base address to to_dir
*to_dir = ( (unsigned long) to_page_table ) | 7 ;
}
}
2)from_page_table And to_page_table
【 The illustration 】
- from_dir Is the page directory number 1 The base of , yes The parent process The base address of the page directory number of ;
- to_dir Is the page directory number 7 The base of , yes Subprocesses The base address of the page directory number of ;
- The next thing to do is , hold from_dir Page catalog number 1 Mapped page table contents copy to to_dir Page catalog number 7 Page table of ;
2) hold from_dir Corresponding page table contents copy to to_dir Corresponding page table
【 Code 】 The code of copying the contents of the parent process page table to the child process page table is as follows :
// Copy the contents of the parent process page table to the code of the child process page table
for(; nr-- >0; from_page_table++; to_page_table++)
{
// from_page_table Is the parent process page table ( Base address )
this_page = *from_page_table;
this_page &= ~2; // read-only
// Put the parent process page table from_page_table Copy the contents to the sub process page table to_page_table
// namely The base address of the parent process page table And The base address of the sub process page table is the same , Both point to the same physical memory
*to_page_table = this_page;
*from_page_table = this_page;
this_page -= LOW_MEM;
this_page >>= 12;
// The current page is shared , So... On this page mem_map Add up ;
mem_map[this_page]++;
}
explain :
- There is no physical page reapplication for child processes , Here, the child process uses the physical page of the parent process ( Of course, it can be redistributed Of );
【4.3】 After the process is established, it looks like (fork after )
【 The illustration 】
- process 1 It's the parent process , process 2 Is a subprocess , Through the process 1 fork It's out process 2;
- process 2 The virtual memory and segment table of are initialized ;
- process 2 The physical memory and page table are also available , Even if the process 2 With the parent process ( process 1) The contents of the page table are the same , Share the same physical memory ;
- According to the figure, you can also see , process 1 Segment table and process 2 The segment table of points to the same physical memory base address ;
- In other words , A child process is loaded into memory ; Try to use the code for the pipe process , The data belongs to the parent process ;
【5】 The program uses memory
1)MMU:
- Memory management unit , It's a hardware , Automatically complete the translation from logical address to physical address , That is, the slave logical address is completed 0x300 To the virtual address 0x400300, Then to the physical address 0x7300 Address translation ( use MMU Hardware replaces software to realize address translation );
2) process 1 The logical address of 0x300 Translate into physical address 0x7300 Steps for :
- step 1: adopt 0x300 The segment number of the program , Query the segment base address from the segment table 0x400000(64M For the base site ), So we can figure out 0x300 The logical address in the virtual memory is 0x400300;
- step 2: The logical page number is calculated through the segment base address , The physical memory page number obtained by querying the page table through the logical page number is 7;
- step 3: Through the physical memory page number 7 And offset address 300 Get the physical address 0x7300;
3) After the address translation , process 1 fork Out of process 2
among , process 2 Translate logical address 0x300 The steps are as follows :
- step 1: adopt 0x300 The segment number of the program , Query the segment base address from the segment table 0x800000(128M For the base site ), So we can figure out 0x300 The logical address in the virtual memory is 0x800300;
- step 2: The logical page number is calculated through the segment base address , The physical memory page number obtained by querying the page table through the logical page number is 7( Because of the process 2 And process 1 The page table of is the same );
- Again in the process fork when , process 1( The parent process ) Put physical memory pages 7 Set to read only ( See the code that copies the contents of the parent process page table to the child process page table ). So the process 2 Unable to write memory page 7, You can only reapply a new physical memory page , Such as physical page 8.
- step 3: process 2 The physical memory page number obtained is 8, Combined with the offset, the physical memory address is 0x8300;
thus : The parent-child processes are separated from each other through different virtual memory spaces , Are independent of each other ;
【 Add 】MMU Introduce from wikipedia
refer2 https://zh.m.wikipedia.org/zh-hans/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E5%8D%95%E5%85%83
Memory management unit ( English :memory management unit, Abbreviation for MMU), Sometimes called paging memory management unit ( English :paged memory management unit, Abbreviation for PMMU).
It's a kind of CPU (CPU) Of Computer hardware for memory access requests . Its function includes the conversion from virtual address to physical address ( Virtual memory management )[1]、 Memory protection 、 CPU cache control , In a simpler computer architecture , Responsible for bus arbitration and memory switching (bank switching, Especially in 8 Bit system ).
边栏推荐
- E-book CHM online CS
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- 查询字段个数
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- Office doc add in - Online CS
- How to convert flv file to MP4 file? A simple solution
- What are the characteristics of trademark translation and how to translate it?
- [daily question] 729 My schedule I
- 【软件测试进阶第1步】自动化测试基础知识
- 自动化测试环境配置
猜你喜欢
LeetCode - 152 乘积最大子数组
ROS learning_ Basics
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
My seven years with NLP
AI on the cloud makes earth science research easier
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
How to reconstruct the class explosion caused by m*n strategies?
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
mysql的基础命令
随机推荐
Today's summer solstice
18.多级页表与快表
UniPro甘特图“初体验”:关注细节背后的多场景探索
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
将ue4程序嵌入qt界面显示
MySQL high frequency interview 20 questions, necessary (important)
接口自动化测试框架:Pytest+Allure+Excel
Monotonic stack
[hot100] 739. Température quotidienne
Embed UE4 program into QT interface display
Apache dolphin scheduler source code analysis (super detailed)
Leetcode - 152 product maximum subarray
Latex文字加颜色的三种办法
Thesis abstract translation, multilingual pure human translation
前缀和数组系列
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Database basics exercise part 2
万丈高楼平地起,每个API皆根基
基于PyTorch和Fast RCNN快速实现目标识别