SlideShare uma empresa Scribd logo
1 de 18
Lab 2
Memory Management
Virtual Memory
           When we into protected mode

           Selector

Software              Segmentation            Paging              RAM
            Offset



           Virtual                   Linear            Physical
Virtual Memory
 Hence the “selector” has no effect and
 the linear address always equals the
 offset of the virtual address.
 so, we only care ....

     Virtual
                        Paging              RAM
    address


               Linear            Physical
Virtual Memory
 Linear address                     Physical address
 10    10   12                         20       12
 Dir Table Offset                     PPN     Offset



                             1023      20      12



              20      12              PPN    Offset
    1023

                              0
             PPN    Offset            Page Table


     0
           Page Directory
Virtual Memory
                                  0x0fffffff
                                   256MB
 Linear address
 10    10   12
                     Extended
 Dir Table Offset
                      Memory


                                  0x00100000
                    BIOS ROM

   4GB                 ROM

                    VGA Display
                                  0x000A0000
                       RAM
                                  0x00000000
Virtual Memory
      Linear address
      10    10   12
      Dir Table Offset

                                   pde_t *kern_pgdir;
                                   kern_pgdir = (pde_t *)
PDX(va)                            boot_alloc(PGSIZE);



          1023      20      12



                   PPN    Offset


           0
                 Page Directory
Virtual Memory
                    0x0fffffff
                     256MB




      kern_pgdir    4KB
         Kernel     ?KB
                    0x00100000
      BIOS ROM

         ROM

      VGA Display
                    0x000A0000
         RAM
                    0x00000000
Virtual Memory
      Linear address
      10    10   12
      Dir Table Offset



                                    1023   20       12
PDX(va)          PTX(va)


                    20       12            PPN   Offset
          1023

                                     0
                   PPN     Offset          Page Table


           0
                 Page Directory
Virtual Memory
      Linear address
      10    10   12                 struct Page *pages;
      Dir Table Offset              static struct Page *page_free_list;



                                        1023   20       12
PDX(va)          PTX(va)


                    20       12                PPN   Offset
          1023

                                         0
                   PPN     Offset              Page Table


           0
                 Page Directory
Virtual Memory

                               0x0fffffff
                                256MB
page_init


                     pages
                  kern_pgdir    4KB
                     kernel    ?KB
                               0x00100000
                     BIOS
                               0x000A0000(IOPHYSMEM)

 page_free_list                0x00000000
Virtual Memory
struct Page *pages;
pages = boot_alloc(npages * (sizeof(struct Page)));
                                        0x0fffffff
                                         256MB
page_init


                           pages
                        kern_pgdir      4KB
                           kernel      ?KB
                                       0x00100000
                           BIOS
                                       0x000A0000(IOPHYSMEM)

 page_free_list                        0x00000000
Virtual Memory

 page_alloc :
   get page from page_free_list
 page_free :
   return page to page_free_list
Virtual Memory
Linear address
10    10   12
                                   Physical address
                                      20       12
                                                      目前我們完
Dir Table Offset                     PPN     Offset
                                                      成了page的
                            1023      20      12

                                                      架構,但是
             20      12              PPN    Offset
   1023
                                                      未提供一個
                             0
            PPN    Offset            Page Table
                                                      外部使用的
    0
          Page Directory                                介面
Interface
  pte_t *
  pgdir_walk(pde_t *pgdir, const void *va, int create)
  {
      // Fill this function in
      //return NULL;
      pde_t *dir = &pgdir[PDX(va)];
      if(*dir & PTE_P){
          pte_t *table = (pte_t*) KADDR(PTE_ADDR(*dir));
          return &table[PTX(va)];
      }else{
          if(!create){
              return NULL;
          }
          struct Page *page_tmp = page_alloc(ALLOC_ZERO);
          if(!page_tmp){
              return NULL;
          }else{
              pte_t *table = (pte_t*) page2kva(page_tmp);
              *dir = PADDR(table) | PTE_P | PTE_W | PTE_U;
              page_tmp->pp_ref = 1;
              return &table[PTX(va)];
          }
      }
  }
Interface
 int
 page_insert(pde_t *pgdir, struct Page *pp, void *va, int perm)
 {
     // Fill this function in
     // return 0;
     if(!pp){
         return -E_NO_MEM;
     }
     pte_t *page_entry = pgdir_walk(pgdir, va, 1);
     if(!page_entry){
         return -E_NO_MEM;
     }
     pgdir[PDX(va)] |= perm;
     if(page2pa(pp) != PTE_ADDR(*page_entry)){
         page_remove(pgdir, va);
         pp->pp_ref++;
     }
     *page_entry = page2pa(pp) | perm | PTE_P;
     return 0;
 }
Interface

  struct Page *
  page_lookup(pde_t *pgdir, void *va, pte_t **pte_store)
  {
      // Fill this function in
      // return NULL;
      *pte_store = pgdir_walk(pgdir, va, 0);
      if(!(*pte_store) || !(**pte_store & PTE_P)){
          return NULL;
      }else{
          return pa2page(PTE_ADDR(**pte_store));
      }
  }
Interface

 void
 page_remove(pde_t *pgdir, void *va)
 {
      // Fill this function in
      pte_t *page_entry;
      struct Page *page = page_lookup(pgdir, va, &page_entry);
      if(page){
          page_decref(page);
      }
      *page_entry = 0;
      tlb_invalidate(pgdir, va);
 }
Thanks a lot.

Mais conteúdo relacionado

Destaque

Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating SystemLPU
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programsKandarp Tiwari
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)Make Mannan
 
Ooad lab manual(original)
Ooad lab manual(original)Ooad lab manual(original)
Ooad lab manual(original)dipenpatelpatel
 
ipv6 introduction & environment buildup
ipv6 introduction & environment buildupipv6 introduction & environment buildup
ipv6 introduction & environment builduppsychesnet Hsieh
 
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015Luca Berardinelli
 
Memory reference instruction
Memory reference instructionMemory reference instruction
Memory reference instructionSanjeev Patel
 
MATLAB programs Power System Simulation lab (Electrical Engineer)
MATLAB programs Power System Simulation  lab (Electrical Engineer)MATLAB programs Power System Simulation  lab (Electrical Engineer)
MATLAB programs Power System Simulation lab (Electrical Engineer)Mathankumar S
 
Bank Database Project
Bank Database ProjectBank Database Project
Bank Database ProjectDavidPerley
 
Qualitative research, lab report overview, and review of lectures 1 to 7
Qualitative research, lab report overview, and review of lectures 1 to 7Qualitative research, lab report overview, and review of lectures 1 to 7
Qualitative research, lab report overview, and review of lectures 1 to 7James Neill
 
POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS)
POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS) POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS)
POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS) Mathankumar S
 

Destaque (20)

Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
 
Os file
Os fileOs file
Os file
 
OS tutoring #1
OS tutoring #1OS tutoring #1
OS tutoring #1
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
O.s. lab all_experimets
O.s. lab all_experimetsO.s. lab all_experimets
O.s. lab all_experimets
 
FFmpeg
FFmpegFFmpeg
FFmpeg
 
Openssl
OpensslOpenssl
Openssl
 
Ooad lab manual
Ooad lab manualOoad lab manual
Ooad lab manual
 
Ooad lab manual(original)
Ooad lab manual(original)Ooad lab manual(original)
Ooad lab manual(original)
 
ipv6 introduction & environment buildup
ipv6 introduction & environment buildupipv6 introduction & environment buildup
ipv6 introduction & environment buildup
 
Operating system lab manual
Operating system lab manualOperating system lab manual
Operating system lab manual
 
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
 
Os lab manual
Os lab manualOs lab manual
Os lab manual
 
Intro Uml
Intro UmlIntro Uml
Intro Uml
 
Memory reference instruction
Memory reference instructionMemory reference instruction
Memory reference instruction
 
MATLAB programs Power System Simulation lab (Electrical Engineer)
MATLAB programs Power System Simulation  lab (Electrical Engineer)MATLAB programs Power System Simulation  lab (Electrical Engineer)
MATLAB programs Power System Simulation lab (Electrical Engineer)
 
Bank Database Project
Bank Database ProjectBank Database Project
Bank Database Project
 
Qualitative research, lab report overview, and review of lectures 1 to 7
Qualitative research, lab report overview, and review of lectures 1 to 7Qualitative research, lab report overview, and review of lectures 1 to 7
Qualitative research, lab report overview, and review of lectures 1 to 7
 
POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS)
POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS) POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS)
POWER SYSTEM SIMULATION - 2 LAB MANUAL (ELECTRICAL ENGINEERING - POWER SYSTEMS)
 

Último

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Último (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

Lab2

  • 2. Virtual Memory When we into protected mode Selector Software Segmentation Paging RAM Offset Virtual Linear Physical
  • 3. Virtual Memory Hence the “selector” has no effect and the linear address always equals the offset of the virtual address. so, we only care .... Virtual Paging RAM address Linear Physical
  • 4. Virtual Memory Linear address Physical address 10 10 12 20 12 Dir Table Offset PPN Offset 1023 20 12 20 12 PPN Offset 1023 0 PPN Offset Page Table 0 Page Directory
  • 5. Virtual Memory 0x0fffffff 256MB Linear address 10 10 12 Extended Dir Table Offset Memory 0x00100000 BIOS ROM 4GB ROM VGA Display 0x000A0000 RAM 0x00000000
  • 6. Virtual Memory Linear address 10 10 12 Dir Table Offset pde_t *kern_pgdir; kern_pgdir = (pde_t *) PDX(va) boot_alloc(PGSIZE); 1023 20 12 PPN Offset 0 Page Directory
  • 7. Virtual Memory 0x0fffffff 256MB kern_pgdir 4KB Kernel ?KB 0x00100000 BIOS ROM ROM VGA Display 0x000A0000 RAM 0x00000000
  • 8. Virtual Memory Linear address 10 10 12 Dir Table Offset 1023 20 12 PDX(va) PTX(va) 20 12 PPN Offset 1023 0 PPN Offset Page Table 0 Page Directory
  • 9. Virtual Memory Linear address 10 10 12 struct Page *pages; Dir Table Offset static struct Page *page_free_list; 1023 20 12 PDX(va) PTX(va) 20 12 PPN Offset 1023 0 PPN Offset Page Table 0 Page Directory
  • 10. Virtual Memory 0x0fffffff 256MB page_init pages kern_pgdir 4KB kernel ?KB 0x00100000 BIOS 0x000A0000(IOPHYSMEM) page_free_list 0x00000000
  • 11. Virtual Memory struct Page *pages; pages = boot_alloc(npages * (sizeof(struct Page))); 0x0fffffff 256MB page_init pages kern_pgdir 4KB kernel ?KB 0x00100000 BIOS 0x000A0000(IOPHYSMEM) page_free_list 0x00000000
  • 12. Virtual Memory page_alloc : get page from page_free_list page_free : return page to page_free_list
  • 13. Virtual Memory Linear address 10 10 12 Physical address 20 12 目前我們完 Dir Table Offset PPN Offset 成了page的 1023 20 12 架構,但是 20 12 PPN Offset 1023 未提供一個 0 PPN Offset Page Table 外部使用的 0 Page Directory 介面
  • 14. Interface pte_t * pgdir_walk(pde_t *pgdir, const void *va, int create) { // Fill this function in //return NULL; pde_t *dir = &pgdir[PDX(va)]; if(*dir & PTE_P){ pte_t *table = (pte_t*) KADDR(PTE_ADDR(*dir)); return &table[PTX(va)]; }else{ if(!create){ return NULL; } struct Page *page_tmp = page_alloc(ALLOC_ZERO); if(!page_tmp){ return NULL; }else{ pte_t *table = (pte_t*) page2kva(page_tmp); *dir = PADDR(table) | PTE_P | PTE_W | PTE_U; page_tmp->pp_ref = 1; return &table[PTX(va)]; } } }
  • 15. Interface int page_insert(pde_t *pgdir, struct Page *pp, void *va, int perm) { // Fill this function in // return 0; if(!pp){ return -E_NO_MEM; } pte_t *page_entry = pgdir_walk(pgdir, va, 1); if(!page_entry){ return -E_NO_MEM; } pgdir[PDX(va)] |= perm; if(page2pa(pp) != PTE_ADDR(*page_entry)){ page_remove(pgdir, va); pp->pp_ref++; } *page_entry = page2pa(pp) | perm | PTE_P; return 0; }
  • 16. Interface struct Page * page_lookup(pde_t *pgdir, void *va, pte_t **pte_store) { // Fill this function in // return NULL; *pte_store = pgdir_walk(pgdir, va, 0); if(!(*pte_store) || !(**pte_store & PTE_P)){ return NULL; }else{ return pa2page(PTE_ADDR(**pte_store)); } }
  • 17. Interface void page_remove(pde_t *pgdir, void *va) { // Fill this function in pte_t *page_entry; struct Page *page = page_lookup(pgdir, va, &page_entry); if(page){ page_decref(page); } *page_entry = 0; tlb_invalidate(pgdir, va); }

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n