这里回顾第18章,本章介绍了分页。

书籍介绍:

学习资料:

第18 章 分页:介绍

内容回顾

  • 分页:把进程地址空间分割成固定大小的单元,每个单元称为一页;

    • 物理内存看成是定长槽块的阵列,叫作页帧(page frame),每个页帧包含一个虚拟内存页;
  • 例子:

  • 页表(page table):记录地址空间的每个虚拟页放在物理内存中的位置

    • 主要作用是为地址空间的每个虚拟页面保存地址转换(address translation)
    • 将虚拟地址映射到物理地址
    • 拥有的内容:
      • 映射关系
      • 有效位(valid bit)用于指示特定地址转换是否有效
      • 存在位(present bit)表示该页是在物理存储器还是在磁盘上(即它已被换出,swapped out)
      • 脏位(dirty bit)表明页面被带入内存后是否被修改过
      • 参考位(reference bit,也被称为访问位,accessed bit)用于追踪页是否被访问以及确定哪些页很受欢迎
  • 虚拟地址分为两个组件:虚拟页面号(virtual page number,VPN)和页内的偏移量(offset)

  • 物理地址分为两个组件:物理帧号(PFN)(有时也称为物理页号,physical page number 或PPN)和页内的偏移量(offset)

  • 使用页表的流程,开销很大:

    1 // Extract the VPN from the virtual address
    2 VPN = (VirtualAddress & VPN_MASK) >> SHIFT
    3
    4 // Form the address of the page-table entry (PTE)
    5 PTEAddr = PTBR + (VPN * sizeof(PTE))
    6
    7 // Fetch the PTE
    8 PTE = AccessMemory(PTEAddr)
    9
    10 // Check if process can access the page
    11 if (PTE.Valid == False)
    12 	RaiseException(SEGMENTATION_FAULT)
    13 else if (CanAccess(PTE.ProtectBits) == False)
    14 	RaiseException(PROTECTION_FAULT)
    15 else
    16  // Access is OK: form physical address and fetch it
    17 	offset = VirtualAddress & OFFSET_MASK
    18 	PhysAddr = (PTE.PFN << PFN_SHIFT) | offset
    19 	Register = AccessMemory(PhysAddr)

作业

1

数量等于

即地址空间/页的大小,所以应该使用适中的页。

2

虚拟页面号为4位,偏移量为14-4=10​。

情形1:

λ python ./paging-linear-translate.py -P 1k -a 16k -p 32k -v -u 0
ARG seed 0
ARG address space size 16k
ARG phys mem size 32k
ARG page size 1k
ARG verbose True
ARG addresses -1


The format of the page table is simple:
The high-order (left-most) bit is the VALID bit.
  If the bit is 1, the rest of the entry is the PFN.
  If the bit is 0, the page is not valid.
Use verbose mode (-v) if you want to print the VPN # by
each entry of the page table.

Page Table (from entry 0 down to the max size)
  [       0]  0x00000000
  [       1]  0x00000000
  [       2]  0x00000000
  [       3]  0x00000000
  [       4]  0x00000000
  [       5]  0x00000000
  [       6]  0x00000000
  [       7]  0x00000000
  [       8]  0x00000000
  [       9]  0x00000000
  [      10]  0x00000000
  [      11]  0x00000000
  [      12]  0x00000000
  [      13]  0x00000000
  [      14]  0x00000000
  [      15]  0x00000000

Virtual Address Trace
  VA 0x00003a39 (decimal:    14905) --> PA or invalid address?
  VA 0x00003ee5 (decimal:    16101) --> PA or invalid address?
  VA 0x000033da (decimal:    13274) --> PA or invalid address?
  VA 0x000039bd (decimal:    14781) --> PA or invalid address?
  VA 0x000013d9 (decimal:     5081) --> PA or invalid address?

计算,由于PTE全部invalid,所以结果为:

Virtual Address Trace
  VA 0x00003a39 (decimal:    14905) --> invalid address
  VA 0x00003ee5 (decimal:    16101) --> invalid address
  VA 0x000033da (decimal:    13274) --> invalid address
  VA 0x000039bd (decimal:    14781) --> invalid address
  VA 0x000013d9 (decimal:     5081) --> invalid address

验证:

λ python ./paging-linear-translate.py -P 1k -a 16k -p 32k -v -u 0 -c
ARG seed 0
ARG address space size 16k
ARG phys mem size 32k
ARG page size 1k
ARG verbose True
ARG addresses -1


The format of the page table is simple:
The high-order (left-most) bit is the VALID bit.
  If the bit is 1, the rest of the entry is the PFN.
  If the bit is 0, the page is not valid.
Use verbose mode (-v) if you want to print the VPN # by
each entry of the page table.

Page Table (from entry 0 down to the max size)
  [       0]  0x00000000
  [       1]  0x00000000
  [       2]  0x00000000
  [       3]  0x00000000
  [       4]  0x00000000
  [       5]  0x00000000
  [       6]  0x00000000
  [       7]  0x00000000
  [       8]  0x00000000
  [       9]  0x00000000
  [      10]  0x00000000
  [      11]  0x00000000
  [      12]  0x00000000
  [      13]  0x00000000
  [      14]  0x00000000
  [      15]  0x00000000

Virtual Address Trace
  VA 0x00003a39 (decimal:    14905) -->  Invalid (VPN 14 not valid)
  VA 0x00003ee5 (decimal:    16101) -->  Invalid (VPN 15 not valid)
  VA 0x000033da (decimal:    13274) -->  Invalid (VPN 12 not valid)
  VA 0x000039bd (decimal:    14781) -->  Invalid (VPN 14 not valid)
  VA 0x000013d9 (decimal:     5081) -->  Invalid (VPN 4 not valid)

情形2:

λ python ./paging-linear-translate.py -P 1k -a 16k -p 32k -v -u 25
ARG seed 0
ARG address space size 16k
ARG phys mem size 32k
ARG page size 1k
ARG verbose True
ARG addresses -1


The format of the page table is simple:
The high-order (left-most) bit is the VALID bit.
  If the bit is 1, the rest of the entry is the PFN.
  If the bit is 0, the page is not valid.
Use verbose mode (-v) if you want to print the VPN # by
each entry of the page table.

Page Table (from entry 0 down to the max size)
  [       0]  0x80000018
  [       1]  0x00000000
  [       2]  0x00000000
  [       3]  0x00000000
  [       4]  0x00000000
  [       5]  0x80000009
  [       6]  0x00000000
  [       7]  0x00000000
  [       8]  0x80000010
  [       9]  0x00000000
  [      10]  0x80000013
  [      11]  0x00000000
  [      12]  0x8000001f
  [      13]  0x8000001c
  [      14]  0x00000000
  [      15]  0x00000000

Virtual Address Trace
  VA 0x00003986 (decimal:    14726) --> PA or invalid address?
  VA 0x00002bc6 (decimal:    11206) --> PA or invalid address?
  VA 0x00001e37 (decimal:     7735) --> PA or invalid address?
  VA 0x00000671 (decimal:     1649) --> PA or invalid address?
  VA 0x00001bc9 (decimal:     7113) --> PA or invalid address?

For each virtual address, write down the physical address it translates to
OR write down that it is an out-of-bounds address (e.g., segfault).

计算:

Virtual Address Trace
  VA 0x00003986 (decimal:    14726) --> 页面号=0b1110=14, invalid
  VA 0x00002bc6 (decimal:    11206) --> 页面号=0b1010=10, 前7位0b10011,11=>0x4fc6
  VA 0x00001e37 (decimal:     7735) --> 页面号=0b0111=7, invalid
  VA 0x00000671 (decimal:     1649) --> 页面号=0b0001=1, invalid
  VA 0x00001bc9 (decimal:     7113) --> 页面号=0b0110=6, invalid

验证:

λ python ./paging-linear-translate.py -P 1k -a 16k -p 32k -v -u 25 -c           
ARG seed 0                                                                      
ARG address space size 16k                                                      
ARG phys mem size 32k                                                           
ARG page size 1k                                                                
ARG verbose True                                                                
ARG addresses -1                                                                
                                                                                
                                                                                
The format of the page table is simple:                                         
The high-order (left-most) bit is the VALID bit.                                
  If the bit is 1, the rest of the entry is the PFN.                            
  If the bit is 0, the page is not valid.                                       
Use verbose mode (-v) if you want to print the VPN # by                         
each entry of the page table.                                                   
                                                                                
Page Table (from entry 0 down to the max size)                                  
  [       0]  0x80000018                                                        
  [       1]  0x00000000                                                        
  [       2]  0x00000000                                                        
  [       3]  0x00000000                                                        
  [       4]  0x00000000                                                        
  [       5]  0x80000009                                                        
  [       6]  0x00000000                                                        
  [       7]  0x00000000                                                        
  [       8]  0x80000010                                                        
  [       9]  0x00000000                                                        
  [      10]  0x80000013                                                        
  [      11]  0x00000000                                                        
  [      12]  0x8000001f                                                        
  [      13]  0x8000001c                                                        
  [      14]  0x00000000                                                        
  [      15]  0x00000000                                                        
                                                                                
Virtual Address Trace                                                           
  VA 0x00003986 (decimal:    14726) -->  Invalid (VPN 14 not valid)             
  VA 0x00002bc6 (decimal:    11206) --> 00004fc6 (decimal    20422) [VPN 10]    
  VA 0x00001e37 (decimal:     7735) -->  Invalid (VPN 7 not valid)              
  VA 0x00000671 (decimal:     1649) -->  Invalid (VPN 1 not valid)              
  VA 0x00001bc9 (decimal:     7113) -->  Invalid (VPN 6 not valid)              
                                                                                

后续略过,同理计算即可。

3

第三个页表太大。

4

程序:

λ python ./paging-linear-translate.py -P 8 -a 2048 -p 1024 -v -s 1
ARG seed 1
ARG address space size 2048
ARG phys mem size 1024
ARG page size 8
ARG verbose True
ARG addresses -1

Error: physical memory size must be GREATER than address space size (for this simulation)

地址空间大于物理空间会报错。