SlideShare una empresa de 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.

Más contenido relacionado

Destacado

Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
LPU
 
ipv6 introduction & environment buildup
ipv6 introduction & environment buildupipv6 introduction & environment buildup
ipv6 introduction & environment buildup
psychesnet Hsieh
 

Destacado (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

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

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 del 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