SlideShare a Scribd company logo
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.

More Related Content

Viewers also liked

Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
LPU
 
Os file
Os fileOs file
Os file
mominabrar
 
OS tutoring #1
OS tutoring #1OS tutoring #1
OS tutoring #1
JaeSang Yoo
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
Kandarp 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
 
O.s. lab all_experimets
O.s. lab all_experimetsO.s. lab all_experimets
O.s. lab all_experimets
Guru Janbheshver University, Hisar
 
FFmpeg
FFmpegFFmpeg
Openssl
OpensslOpenssl
Ooad lab manual
Ooad lab manualOoad lab manual
Ooad lab manual
Umamurthi Umamurthi
 
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 buildup
psychesnet Hsieh
 
Operating system lab manual
Operating system lab manualOperating system lab manual
Operating system lab manual
Meerut Institute of Technology
 
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
Luca Berardinelli
 
Os lab manual
Os lab manualOs lab manual
Os lab manual
Mohd Mohiuddin Ansari
 
Intro Uml
Intro UmlIntro Uml
Intro Uml
Kris der Rose
 
Memory reference instruction
Memory reference instructionMemory reference instruction
Memory reference instruction
Sanjeev 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 Project
DavidPerley
 
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
James 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
 

Viewers also liked (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)
 

Recently uploaded

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 

Recently uploaded (20)

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 

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); }

Editor's Notes

  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