【PM复习】分页的好处

     我们在操作系统教科书上看到,对内存进行分页管理可以实现虚拟内存,这样对每一个进程来说,假如是32位的处理器,就有4G的内存空间。进程调度时,每调度一个进程,就切换到该进程的页表,就是给CR3赋一个对应的值就好。

     下面的程序中,就是稍微模拟一下这种情况,建立两个页表,在相同的虚拟地址上改变另一个页表的映射,使其指向另一个函数,下面是代码:

lib.inc:

Code:
  1. Disp_Int:   
  2.     push    ebp   
  3.     mov ebp,esp   
  4.        
  5.     push    eax   
  6.     push    ecx   
  7.     push    edi   
  8.        
  9.     mov eax,[ebp + 8]   
  10.     mov ecx,4   
  11.        
  12. .1:   
  13.     rol eax,8   
  14.     call    Disp_Al   
  15.     loop .1   
  16.        
  17.     mov edi,[d_Disp_Pos]   
  18.     mov ah,7h   
  19.     mov al,'h'  
  20.     mov [gs:edi],ax   
  21.     add edi,2   
  22.     mov [d_Disp_Pos],edi   
  23.        
  24.     pop edi   
  25.     pop ecx   
  26.     pop eax   
  27.        
  28.     pop ebp   
  29.     ret    
  30.   
  31. Disp_Str:   
  32.     push    ebp   
  33.     mov ebp,esp   
  34.        
  35.     push    eax   
  36.     push    ebx   
  37.     push    esi   
  38.     push    edi   
  39.        
  40.     mov esi,[ebp + 8]   
  41.     mov edi,[d_Disp_Pos]   
  42.     mov ah,0fh   
  43.   
  44. .1:   
  45.     lodsb   
  46.     test    al,al   
  47.     jz  .2   
  48.     cmp al,0ah   
  49.     jne .3   
  50.     mov eax,edi   
  51.     mov bl,160   
  52.     div bl   
  53.     and ax,0ffh   
  54.     inc ax   
  55.     mov bl,160   
  56.     mul bl   
  57.     mov edi,eax   
  58.     jmp .1   
  59.        
  60. .3:   
  61.     mov [gs:edi],ax   
  62.     add edi,2   
  63.     jmp .1   
  64.        
  65. .2:   
  66.     mov [d_Disp_Pos],edi   
  67.        
  68.     pop edi   
  69.     pop esi   
  70.     pop ebx   
  71.     pop eax   
  72.        
  73.     pop ebp   
  74.     ret    
  75.   
  76. Disp_Al:   
  77.     push    ebp   
  78.     mov ebp,esp   
  79.        
  80.     push    esi   
  81.     push    ecx   
  82.     push    eax   
  83.     mov ecx,2   
  84.     shr al,4   
  85.        
  86. .3:   
  87.     and al,0fh   
  88.     cmp al,9   
  89.     jbe .1   
  90.     add al,7   
  91.        
  92. .1:   
  93.     add al,30h   
  94.     mov esi,[d_Disp_Pos]   
  95.     mov byte [gs:esi],al   
  96.     mov byte [gs:esi + 1],0fh   
  97.     add dword [d_Disp_Pos],2   
  98.        
  99.     dec ecx   
  100.     cmp ecx,0   
  101.     je  .2   
  102.     pop eax   
  103.     jmp .3   
  104.        
  105. .2:   
  106.     pop ecx   
  107.     pop esi   
  108.     pop ebp   
  109.     ret   
  110.        
  111. Disp_Return:   
  112.     push    sz_Return   
  113.     call    Disp_Str   
  114.     add esp,4   
  115.     ret   
  116.        
  117. Disp_Space:   
  118.     push    sz_Space   
  119.     call    Disp_Str   
  120.     add esp,4   
  121.     ret   
  122.        
  123. Memory_Copy:   
  124. ;proto:   
  125. ;void Memory_Copy(void *p_src,void *p_des,int len);   
  126.     push    ebp   
  127.     mov ebp,esp   
  128.        
  129.     push    eax   
  130.     push    ecx   
  131.     push    esi   
  132.     push    edi   
  133.        
  134.     mov ecx,[ebp + 16]   
  135.     mov edi,[ebp + 12]   
  136.     mov esi,[ebp + 8]   
  137.        
  138. .1:   
  139.     mov al,[ds:esi]   
  140.     mov [es:edi],al   
  141.     inc esi   
  142.     inc edi   
  143.     loop .1   
  144.        
  145.     pop edi   
  146.     pop esi   
  147.     pop ecx   
  148.     pop eax   
  149.        
  150.     pop ebp   
  151.     ret  

pm15.asm:

Code:
  1. %include "pm.inc"  
  2.   
  3. Page_Dir_Base1  equ 200000h   
  4. Page_Tbl_Base1  equ 201000h   
  5. Page_Dir_Base2  equ 210000h   
  6. Page_Tbl_Base2  equ 211000h   
  7.   
  8. Proc_Paging_Demo    equ 301000h   
  9. Linear_Addr             equ 401000h   
  10. Proc_A                      equ 401000h   
  11. Proc_B                      equ 501000h   
  12.   
  13. org 0100h       
  14. jmp LABEL_BEGIN       
  15.       
  16. [section .gdt]       
  17. LABEL_DESC_DUMMY:       
  18.     Descriptor  0,0,0     
  19. LABEL_DESC_CODE32:    
  20.     Descriptor  0,GDT_Code32_Len - 1,DA_CR + DA_32     
  21. LABEL_DESC_CODE16:   
  22.     Descriptor  0,GDT_Code16_Len - 1,DA_C   
  23. LABEL_DESC_VIDEO:       
  24.     Descriptor  0b8000h,0ffffh,DA_DRW   
  25. LABEL_DESC_DATA:   
  26.     Descriptor  0,Data_Len - 1,DA_DRW   
  27. LABEL_DESC_STACK32:    
  28.     Descriptor  0,Stack_Len - 1,DA_DRW + DA_32   
  29. LABEL_DESC_NORMAL:   
  30.     Descriptor  0,0ffffh,DA_DRW   
  31. LABEL_DESC_FLAT_C:   
  32.     Descriptor  0,0fffffh,DA_C + DA_32 + DA_LIMIT4K   
  33. LABEL_DESC_FLAT_RW:   
  34.     Descriptor  0,0fffffh,DA_DRW + DA_LIMIT4K   
  35.            
  36. GDT_Len equ $ - LABEL_DESC_DUMMY       
  37. GDT_Ptr:       
  38.     dw  GDT_Len - 1       
  39.     dd  0       
  40.       
  41. Selector_Code32 equ LABEL_DESC_CODE32 - LABEL_DESC_DUMMY       
  42. Selector_Code16 equ LABEL_DESC_CODE16 - LABEL_DESC_DUMMY   
  43. Selector_Video  equ LABEL_DESC_VIDEO - LABEL_DESC_DUMMY     
  44. Selector_Data       equ LABEL_DESC_DATA - LABEL_DESC_DUMMY     
  45. Selector_Stack32 equ    LABEL_DESC_STACK32 - LABEL_DESC_DUMMY   
  46. Selector_Normal equ LABEL_DESC_NORMAL - LABEL_DESC_DUMMY   
  47. Selector_Flat_C equ LABEL_DESC_FLAT_C - LABEL_DESC_DUMMY   
  48. Selector_Flat_RW    equ LABEL_DESC_FLAT_RW - LABEL_DESC_DUMMY   
  49.   
  50. [section .stack32]   
  51. [bits 32]   
  52. LABEL_STACK32:   
  53.     times 512 db 0   
  54. Stack_Len   equ $ - $$   
  55.   
  56. [section .data]   
  57. LABEL_DATA:   
  58. _d_Disp_Pos dd  160 * 5   
  59. d_Disp_Pos  equ _d_Disp_Pos - $$   
  60. _w_SP_Value_In_Real_Mode    dw  0   
  61. w_SP_Value_In_Real_Mode equ _w_SP_Value_In_Real_Mode - $$   
  62.   
  63. _sz_Memory_Info_Title   db 'BaseAddrL  BaseAddrH  LengthLow  LengthHigh  Type',0   
  64. sz_Memory_Info_Title    equ _sz_Memory_Info_Title - $$   
  65. _sz_Ram_Size    db 'RAM SIZE:',0   
  66. sz_Ram_Size equ _sz_Ram_Size - $$   
  67. _sz_Return  db  0ah,0   
  68. sz_Return equ   _sz_Return - $$   
  69. _sz_Space   db  20h,0   
  70. sz_Space    equ _sz_Space - $$   
  71.   
  72. _Memory_Info_Buffer times 256 db 0   
  73. Memory_Info_Buffer  equ _Memory_Info_Buffer - $$   
  74. _d_Memory_Info_Num  dd  0   
  75. d_Memory_Info_Num   equ _d_Memory_Info_Num - $$   
  76.   
  77. _ARDStruct:   
  78.     _d_Base_Addr_Low    dd  0   
  79.     _d_Base_Addr_High   dd  0   
  80.     _d_Length_Low   dd  0   
  81.     _d_Length_High  dd  0   
  82.     _d_Type dd  0   
  83.        
  84. ARDStruct   equ _ARDStruct - $$   
  85.     d_Base_Addr_Low equ _d_Base_Addr_Low    - $$   
  86.     d_Base_Addr_High    equ _d_Base_Addr_High   - $$   
  87.     d_Length_Low    equ _d_Length_Low - $$   
  88.     d_Length_High   equ _d_Length_High - $$   
  89.     d_Type  equ _d_Type - $$   
  90.        
  91. _d_Ram_Size dd  0   
  92. d_Ram_Size  equ _d_Ram_Size - $$   
  93. _d_Page_Table_Num   dd  0   
  94. d_Page_Table_Num    equ _d_Page_Table_Num - $$   
  95.   
  96.   
  97.   
  98. Data_Len    equ $ - $$   
  99.       
  100. [section .s16]       
  101. [bits 16]       
  102. LABEL_BEGIN:       
  103.     mov ax,cs       
  104.   mov ds,ax       
  105.   mov es,ax       
  106.   mov   ax,ss   
  107.   mov   sp,0100h   
  108.      
  109.   mov   [LABEL_GO_BACK_TO_REAL + 3],ax   
  110.      
  111.   mov   ebx,0   
  112.   mov   di,_Memory_Info_Buffer   
  113.   mov   ecx,20   
  114.   mov   edx,0534d4150h   
  115.   
  116. .loop:   
  117.     mov eax,0e820h   
  118.   int   15h   
  119.   jc    LABEL_MEMORY_CHK_FAIL   
  120.   inc   dword [_d_Memory_Info_Num]   
  121.   add   di,20   
  122.   cmp   ebx,0   
  123.   jne   .loop   
  124.   jmp   LABEL_MEMORY_OK   
  125.      
  126. LABEL_MEMORY_CHK_FAIL:   
  127.     mov dword [_d_Memory_Info_Num],0   
  128. LABEL_MEMORY_OK:   
  129.            
  130.   Fill_Descriptor   LABEL_DESC_CODE32,LABEL_CODE32   
  131.   Fill_Descriptor   LABEL_DESC_CODE16,LABEL_BEGIN   
  132.   Fill_Descriptor   LABEL_DESC_DATA,LABEL_DATA   
  133.   Fill_Descriptor   LABEL_DESC_STACK32,LABEL_STACK32   
  134.            
  135.   xor eax,eax       
  136.   mov ax,ds       
  137.   shl eax,4       
  138.   add eax,LABEL_DESC_DUMMY       
  139.   mov dword [GDT_Ptr + 2],eax       
  140.            
  141.   lgdt  [GDT_Ptr]       
  142.      
  143.   mov   [w_SP_Value_In_Real_Mode],sp   
  144.            
  145.   cli       
  146.            
  147.   in  al,92h       
  148.   or  al,00000010b       
  149.   out 92h,al       
  150.            
  151.   mov eax,cr0       
  152.   or  al,1       
  153.   mov cr0,eax       
  154.            
  155.   jmp dword Selector_Code32:0       
  156.        
  157. _LABEL_PREPARE_GO_BACK_TO_REAL:   
  158. LABEL_PREPARE_GO_BACK_TO_REAL equ   _LABEL_PREPARE_GO_BACK_TO_REAL - $$   
  159.     mov ax,Selector_Normal   
  160.     mov ds,ax   
  161.     mov es,ax   
  162.     mov ss,ax   
  163.     mov gs,ax   
  164.     mov fs,ax   
  165.        
  166.     mov eax,cr0   
  167.     and eax,7ffffffeh   
  168.     mov cr0,eax   
  169.        
  170. LABEL_GO_BACK_TO_REAL:   
  171.     jmp 0:LABEL_ALREADY_REAL   
  172.        
  173. LABEL_ALREADY_REAL:   
  174.     mov ax,cs   
  175.     mov ds,ax   
  176.     mov es,ax   
  177.     mov ss,ax   
  178.     mov sp,[w_SP_Value_In_Real_Mode]   
  179.     mov fs,ax   
  180.     mov gs,ax   
  181.        
  182.     in  al,92h   
  183.     and al,11111101b   
  184.     out 92h,al   
  185.        
  186.     in  al,92h   
  187.     and al,11111101b   
  188.     out 92h,al   
  189.        
  190.     sti   
  191.        
  192.     mov ax,4c00h   
  193.     int 21h   
  194.        
  195. GDT_Code16_Len  equ $ - $$   
  196.      
  197. [section .s32]       
  198. [bits 32]       
  199. LABEL_CODE32:       
  200.     mov ax,Selector_Stack32   
  201.   mov   ss,ax   
  202.   mov   esp,Stack_Len   
  203.      
  204.     mov ax,Selector_Video   
  205.   mov   gs,ax   
  206.      
  207.   mov   ax,Selector_Data   
  208.   mov   ds,ax   
  209.   mov   es,ax   
  210.      
  211.   call  Disp_Memory_Info   
  212.   
  213.     call    Paging_Demo   
  214.      
  215.     jmp Selector_Code16:LABEL_PREPARE_GO_BACK_TO_REAL    
  216.   
  217. Paging_Demo:   
  218.     mov ax,Selector_Code32   
  219.     mov ds,ax   
  220.   mov   ax,Selector_Flat_RW   
  221.   mov   es,ax   
  222.     
  223.   push  Paging_Demo_Proc_Len   
  224.   push  Proc_Paging_Demo   
  225.   push  Paging_Demo_Proc   
  226.   call  Memory_Copy   
  227.   add   esp,12   
  228.      
  229.   push  A_Proc_Len   
  230.   push  Proc_A   
  231.   push  A_Proc   
  232.   call  Memory_Copy   
  233.   add   esp,12   
  234.      
  235.   push  B_Proc_Len   
  236.   push  Proc_B   
  237.   push  B_Proc   
  238.   call  Memory_Copy   
  239.   add   esp,12   
  240.      
  241.   mov   ax,Selector_Data   
  242.   mov   ds,ax   
  243.   mov   ax,Selector_Flat_RW   
  244.   mov   es,ax   
  245.      
  246.   call  Setup_Paging   
  247.      
  248.   call  Selector_Flat_C:Proc_Paging_Demo   
  249.   call  Page_Switch   
  250.   call  Selector_Flat_C:Proc_Paging_Demo   
  251.      
  252.     ret   
  253.        
  254. Disp_Memory_Info:   
  255.     push    eax   
  256.     push    ecx   
  257.     push    edx   
  258.     push    esi   
  259.     push    edi   
  260.        
  261.     push    sz_Memory_Info_Title   
  262.     call    Disp_Str   
  263.     add esp,4   
  264.     call    Disp_Return   
  265.        
  266.     mov ecx,[d_Memory_Info_Num]   
  267.     mov esi,Memory_Info_Buffer   
  268.        
  269. .1:   
  270.     mov edx,5   
  271.     mov edi,ARDStruct   
  272.        
  273. .2:   
  274.     push    dword [esi]   
  275.     call    Disp_Int   
  276.     pop eax   
  277.     stosd   
  278.     add esi,4   
  279.     call Disp_Space   
  280.     call Disp_Space   
  281.     dec edx   
  282.     cmp edx,0   
  283.     jne .2   
  284.        
  285.     cmp dword [d_Type],1   
  286.     jne .3   
  287.     mov eax,[d_Base_Addr_Low]   
  288.     add eax,[d_Length_Low]   
  289.     cmp eax,[d_Ram_Size]   
  290.     jb  .3   
  291.     mov [d_Ram_Size],eax   
  292.        
  293. .3:    
  294.     call    Disp_Return   
  295.     loop .1   
  296.        
  297.     push    sz_Ram_Size   
  298.     call    Disp_Str   
  299.     add esp,4   
  300.        
  301.     push    dword [d_Ram_Size]   
  302.     call    Disp_Int   
  303.     add esp,4   
  304.        
  305.     pop edi   
  306.     pop esi   
  307.     pop edx   
  308.     pop ecx   
  309.     pop eax   
  310.     ret   
  311.        
  312. Setup_Paging:   
  313.     push    eax   
  314.     push    ebx   
  315.     push    ecx   
  316.     push    edx   
  317.     push    edi   
  318.        
  319.     xor edx,edx   
  320.   mov   eax,[d_Ram_Size]   
  321.   mov   ebx,1024 * 1024 * 4   
  322.   div   ebx   
  323.   test  edx,edx   
  324.   jz    .skip   
  325.   inc   eax   
  326. .skip:   
  327.     mov [d_Page_Table_Num],eax   
  328.     mov ecx,eax   
  329.     mov eax,Page_Tbl_Base1 + 7h   
  330.     mov edi,Page_Dir_Base1   
  331.        
  332. .1:   
  333.     stosd   
  334.     add eax,4096   
  335.     loop .1   
  336.        
  337.     mov eax,[d_Page_Table_Num]   
  338.     mov ebx,1024   
  339.     mul ebx   
  340.     mov ecx,eax   
  341.     mov eax,7h   
  342.     mov edi,Page_Tbl_Base1   
  343.        
  344. .2:   
  345.     stosd   
  346.     add eax,4096   
  347.     loop .2   
  348.        
  349.     mov eax,Page_Dir_Base1   
  350.     mov cr3,eax   
  351.        
  352.     mov eax,cr0   
  353.     or  eax,80000000h   
  354.     mov cr0,eax   
  355.        
  356.     pop edi   
  357.     pop edx   
  358.     pop ecx   
  359.     pop ebx   
  360.     pop eax   
  361.   ret   
  362.      
  363. Page_Switch:   
  364.     push    eax   
  365.     push    ebx   
  366.     push    ecx   
  367.     push    edx   
  368.     push    edi   
  369.        
  370.     mov ecx,[d_Page_Table_Num]   
  371.     mov eax,Page_Tbl_Base2 + 7h   
  372.     mov edi,Page_Dir_Base2   
  373.   
  374. .1:   
  375.     stosd   
  376.     add eax,4096   
  377.     loop    .1   
  378.        
  379.     mov eax,[d_Page_Table_Num]   
  380.     mov bx,1024   
  381.     mul bx   
  382.     mov ecx,eax   
  383.     mov eax,7h   
  384.     mov edi,Page_Tbl_Base2   
  385.   
  386. .2:   
  387.     stosd   
  388.     add eax,4096   
  389.     loop .2   
  390.   
  391.     mov eax,Linear_Addr   
  392.     shr eax,22   
  393.     and eax,3ffh   
  394.     mov bx,4   
  395.     mul bx   
  396.     mov ecx,eax   
  397.     mov ecx,[es:Page_Dir_Base2 + ecx]   
  398.     and ecx,0fffffc00h   
  399.        
  400.     mov eax,Linear_Addr   
  401.     shr eax,12   
  402.     and eax,3ffh   
  403.        
  404.     mov bx,4   
  405.     mul bx   
  406.     add ecx,eax   
  407.        
  408.     push    ecx   
  409.     call    Disp_Int   
  410.     add esp,4   
  411.        
  412.     mov dword [es:ecx],Proc_B + 7h   
  413.     mov edx,[es:ecx]   
  414.        
  415.     mov eax,Page_Dir_Base2   
  416.     mov cr3,eax   
  417.   
  418.     pop edi   
  419.   pop   edx   
  420.   pop   ecx   
  421.   pop   ebx   
  422.   pop   eax   
  423.     ret   
  424.        
  425. _Paging_Demo_Proc:   
  426. Paging_Demo_Proc    equ _Paging_Demo_Proc - $$   
  427.     mov eax,Linear_Addr   
  428.     call    eax   
  429.        
  430.     retf   
  431.        
  432. Paging_Demo_Proc_Len    equ $ - _Paging_Demo_Proc      
  433.        
  434. _A_Proc:   
  435. A_Proc  equ _A_Proc - $$   
  436.     mov ah,0ch   
  437.     mov al,'A'  
  438.     mov [gs:160 * 16],ax   
  439.     ret   
  440.        
  441. A_Proc_Len  equ $ - _A_Proc   
  442.   
  443. _B_Proc:   
  444. B_Proc  equ _B_Proc - $$   
  445.     mov ah,0ch   
  446.     mov al,'B'  
  447.     mov [gs:160 * 17],ax   
  448.     ret   
  449.        
  450. B_Proc_Len  equ $ - _B_Proc    
  451.        
  452. %include "lib.inc"  
  453.   
  454. GDT_Code32_Len  equ $ - $$  

     运行结果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值