SlideShare a Scribd company logo
1 of 26
Windows Memory/Cache
   Manager Internals
     Sisimon Soman
Locality Theory
• If access page/cluster n, high possibility to
  access blocks near to n.
• All memory based computing system
  working on this principle.
• Windows has registry keys to configure
  pre-fetch how many blocks/pages.
• Application specific memory manager like
  Databases, multimedia workload, have
  application aware pre-fetching.
Virtual Memory Manager (VMM)
• Apps feels memory is infinity – magic
  done by VMM.
• Multiple apps run concurrently with out
  interfering other apps data.
• Apps feel the entire resource is mine.
• Protect OS memory from apps.
• Advanced app may need to share
  memory. Provide solution to memory
  sharing easily.
VMM Continued..
• VMM reserve certain amount of memory
  to Kernel.
• 32 bit box , 2GB for Kernel and 2GB for
  User apps.
• Specific area in Kernel memory reserved
  to store process specific data like PDE,
  PTE etc called Hyper Space
Segmentation and Paging
• X86 processor has segmentation and
  paging support.
• Can disable or enable paging, but
  segmentation is enabled by default.
• Windows uses paging.
• Since not able to disable segmentation, it
  consider the entire memory for segments
  (also called ‘flat segments’).
Paging
• Divide entire physical memory in to equal
  size pages (4K size for x86 platforms).
  This is called ‘page frames’ and list called
  ‘page frame database’ (PF DB).
• X86 platform PF DB contains 20bit
  physical offset (remaining 12 bit to
  address offset in the page).
• PF DB also contains flags stating,
  read/write underway , shared page , etc.
VMM Continued..
• Upper 2GB Kernel space is common for
  all process.
• What is it mean – Half of PDE is common
  to all process !.
• Experiment – See the PDE of two process
  and make sure half of the PDE is same
Physical to Virtual address
             translation
• Address translation in both direction – When
  write PF to pagefile, VMM need to update proper
  PDE/PTE stating page is in disk.
• Done by
  – Memory Management Unit (MMU) of the processor.
  – The VMM help MMU.
• VMM keep the PDE/PTE info and pass to MMU
  during process context switch.
• MMU translate virtual address to physical
  address.
Translation Lookaside Buffer (TLB)
• Address translation is costly operation
• It happen frequently – when even touches virtual
  memory.
• TLB keeps a list containing most frequent
  address translations.
• The list is tagged by process ID.
• TLB is a generic OS concept - implementation is
  architecture dependent.
• Before doing the address translation MMU
  search TLB for the PF.
Address Translation
• In x86 32 bit address – 10 bits of MSB
  points to the PTE offset in PDE. Thus PDE
  size of process is 1024 bytes.
• Next 10 bits point to the PF starting
  address in PTE. Thus each PTE contains
  1024 bytes.
• Remaining 12 bits to address the location
  in the PF. Thus page size is 4K.
What is a Zero Page
• Page frames not specific to apps.
• If App1 write sensitive data to PF1, and later VMM push
  the page to page file, attach PF 1 to App2. App2 can see
  these sensitive info.
• It’s a big security flaw, VMM keep a Zero Page list.
• Cannot clean the page while freeing memory – it’s a
  performance problem.
• VMM has dedicated thread who activate when system
  under low memory situation and pick page frames from
  free PF list, clean it and push to zero page list.
• VMM allocate memory from zero page list.
Arbitrary Thread Context
• Top layer of the driver stack get the
  request (IRP) in the same process
  context.
• Middle or lower layer driver MAY get the
  request in any thread context (Ex: IO
  completion), the current running thread
  context.
• The address in the IRP is specific to the
  PDE/PTE in the original process context.
Arbitrary Thread Context
              continued..
• How to solve the issue ?.
• Note the half of the PDE (Kernel area) is
  common in all process.
• If some how map to the kernel memory
  (Upper half of PDE), the buffer is
  accessible from all process.
Mapping buffer to Kernel space
• Allocate kernel pool from the calling
  process context, copy user buffer to this
  Kernel space.
• Memory Descriptor List (MDL) – Most
  commonly used mechanism to keep data
  in Kernel space.
Memory Descriptor List (MDL)
•   //
•   // I/O system definitions.
•   //
•   // Define a Memory Descriptor List (MDL)
•   //
•   // An MDL describes pages in a virtual buffer in terms of physical pages. The
•   // pages associated with the buffer are described in an array that is allocated
•   // just after the MDL header structure itself.
•   //
•   // One simply calculates the base of the array by adding one to the base
•   // MDL pointer:
•   //
•   //    Pages = (PPFN_NUMBER) (Mdl + 1);
•   //
•   // Notice that while in the context of the subject thread, the base virtual
•   // address of a buffer mapped by an MDL may be referenced using the following:
•   //
•   //    Mdl->StartVa | Mdl->ByteOffset
•   //


•   typedef struct _MDL {
•      struct _MDL *Next;
•      CSHORT Size;
•      CSHORT MdlFlags;
•      struct _EPROCESS *Process;
•      PVOID MappedSystemVa;
•      PVOID StartVa;
•      ULONG ByteCount;
•      ULONG ByteOffset;
•   } MDL, *PMDL;
MDL Continued..
•   #define MmGetSystemAddressForMdlSafe(MDL, PRIORITY)                    
•      (((MDL)->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA |                       
•                MDL_SOURCE_IS_NONPAGED_POOL)) ?                       
•                    ((MDL)->MappedSystemVa) :       
•                    (MmMapLockedPagesSpecifyCache((MDL),          
•                                   KernelMode, 
•                                   MmCached, 
•                                   NULL,      
•                                   FALSE,       
•                                   (PRIORITY))))




•   #define MmGetMdlVirtualAddress(Mdl)                        
•     ((PVOID) ((PCHAR) ((Mdl)->StartVa) + (Mdl)->ByteOffset))
Standby list
• To reclaim pages from a process, VMM first move pages
  to Standby list.
• VMM keep it there for a pre-defined ticks.
• If process refer the same page, VMM remove from
  standby list and assign to process.
• VMM free the pages from Standby list after the timeout
  expire.
• Pages in standby list is not free, not belong to a process
  also.
• VMM keep a min and max value for free and standby
  page count. If its out of the limits, appropriate events will
  signaled and adjust the appropriate lists.
Miscellaneous VMM Terms
• ZwAllocateVirtualMemory – allocate
  process specific memory in lower 2GB

• Paged Pool

• Non Paged Pool

• Copy on write (COW)
Part 2



Cache Manager
Cache Manager concepts
• If disk heads run in the speed of super
  sonic jets, Cache Manager not required.
• Disk access is the main bottleneck that
  reduce the system performance. Faster
  CPU and Memory, but disk is still in stone
  age.
• Common concept in Operating Systems,
  Unix flavor called ‘buffer cache’.
What Cache Manager does
• Keep the system wide cache data of
  frequently used secondary storage blocks.
• Facilitate read ahead , write back to
  improve the overall system performance.
• With write-back, cache manager combine
  multiple write requests and issue single
  write request to improve performance.
  There is a risk associated with write-back.
How Cache Manager works
• Cache Manager implement caching using
  Memory Mapping.
• The concept is similar to an App uses
  memory mapped file.
• CreateFile(…dwFlagsAndAttributes ,..)
• dwFlagsAndAttributes ==
 FILE_FLAG_NO_BUFFERING means I don’t want
 cache manager.
How Cache Manager works..
• Cache Manager reserve area in higher 2GB (x86
  platform) system area.
• The Cache Manager reserved page count adjust
  according to the system memory requirement.
• If system has lots of IO intensive tasks, system
  dynamically increase the cache size.
• If system under low memory situation, reduce
  the buffer cache size.
How cached read operation works
                                                             User Space

                                                            Kernel Space
Cached Read (1)

                      Page Fault (4)
                                                 VMM


                                           Do Memory Mapping (3)

                              Get the
                              Pages From    Cache Manager
           File System
                              CM (2)

       Get the blocks from disk (5)

          Disk stack
      (SCSI/Fibre Channel)
How cached write operation works
                                                                   User Space

                                                                  Kernel Space
Cached Write (1)
                               Modified Page
                               Writer Thread          VMM
                                  of VMM
                   Write to disk                Do Memory Mapping (3),
                   later(4)                     Copy data to VMM pages.
                                   Copy Pages
                                   to CM (2)     Cache Manager
            File System

       Write the blocks to disk (5)

           Disk stack
       (SCSI/Fibre Channel)
Questions ?

More Related Content

What's hot

Os solaris memory management
Os  solaris memory managementOs  solaris memory management
Os solaris memory managementTech_MX
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory ManagementRajan Kandel
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamalKamal Maiti
 
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...peknap
 
Continguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelContinguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelKernel TLV
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSKumar Pritam
 
Overview of Distributed Systems
Overview of Distributed SystemsOverview of Distributed Systems
Overview of Distributed Systemsvampugani
 
chapter 2 memory and process management
chapter 2 memory and process managementchapter 2 memory and process management
chapter 2 memory and process managementAisyah Rafiuddin
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagementpradeepelinux
 
Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in LinuxRaghu Udiyar
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSvampugani
 
Memory management ppt
Memory management pptMemory management ppt
Memory management pptManishaJha43
 
Designing Information Structures For Performance And Reliability
Designing Information Structures For Performance And ReliabilityDesigning Information Structures For Performance And Reliability
Designing Information Structures For Performance And Reliabilitybryanrandol
 
Operating Systems - memory management
Operating Systems - memory managementOperating Systems - memory management
Operating Systems - memory managementMukesh Chinta
 
Operating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsOperating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsPeter Tröger
 

What's hot (20)

Memory virtualization
Memory virtualizationMemory virtualization
Memory virtualization
 
Os solaris memory management
Os  solaris memory managementOs  solaris memory management
Os solaris memory management
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
 
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
 
Continguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelContinguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux Kernel
 
Memory managment
Memory managmentMemory managment
Memory managment
 
cache
cachecache
cache
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Overview of Distributed Systems
Overview of Distributed SystemsOverview of Distributed Systems
Overview of Distributed Systems
 
chapter 2 memory and process management
chapter 2 memory and process managementchapter 2 memory and process management
chapter 2 memory and process management
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagement
 
Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in Linux
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Memory management ppt
Memory management pptMemory management ppt
Memory management ppt
 
Designing Information Structures For Performance And Reliability
Designing Information Structures For Performance And ReliabilityDesigning Information Structures For Performance And Reliability
Designing Information Structures For Performance And Reliability
 
Operating Systems - memory management
Operating Systems - memory managementOperating Systems - memory management
Operating Systems - memory management
 
Operating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsOperating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management Concepts
 
OSCh14
OSCh14OSCh14
OSCh14
 
Dynamic loading
Dynamic loadingDynamic loading
Dynamic loading
 

Viewers also liked

Windows vs linux
Windows vs linuxWindows vs linux
Windows vs linuxvatsaanadi
 
memory management of windows vs linux
memory management of windows vs linuxmemory management of windows vs linux
memory management of windows vs linuxSumit Khanka
 
Windows programming
Windows programmingWindows programming
Windows programmingBapan Maity
 
Memory Management in Windows 7
Memory Management in Windows 7Memory Management in Windows 7
Memory Management in Windows 7Naveed Qadri
 
Windows memory management
Windows memory managementWindows memory management
Windows memory managementTech_MX
 
2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - public2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - publicSandro Suffert
 

Viewers also liked (8)

Windows vs linux
Windows vs linuxWindows vs linux
Windows vs linux
 
memory management of windows vs linux
memory management of windows vs linuxmemory management of windows vs linux
memory management of windows vs linux
 
Windows programming
Windows programmingWindows programming
Windows programming
 
Memory Management in Windows 7
Memory Management in Windows 7Memory Management in Windows 7
Memory Management in Windows 7
 
Linux vs windows
Linux vs windowsLinux vs windows
Linux vs windows
 
Windows memory management
Windows memory managementWindows memory management
Windows memory management
 
Linux v/s Windows
Linux v/s WindowsLinux v/s Windows
Linux v/s Windows
 
2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - public2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - public
 

Similar to Windows memory manager internals

Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory managementVandana Salve
 
OSSEU18: NVDIMM and Virtualization - George Dunlap, Citrix
OSSEU18: NVDIMM and Virtualization  - George Dunlap, CitrixOSSEU18: NVDIMM and Virtualization  - George Dunlap, Citrix
OSSEU18: NVDIMM and Virtualization - George Dunlap, CitrixThe Linux Foundation
 
Deterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System ArchitectureDeterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System ArchitectureHeechul Yun
 
Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory UsageTomer Perry
 
I/O System and Case Study
I/O System and Case StudyI/O System and Case Study
I/O System and Case StudyGRamya Bharathi
 
XPDDS18: NVDIMM Overview - George Dunlap, Citrix
XPDDS18: NVDIMM Overview - George Dunlap, Citrix XPDDS18: NVDIMM Overview - George Dunlap, Citrix
XPDDS18: NVDIMM Overview - George Dunlap, Citrix The Linux Foundation
 
Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)
Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)
Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)Suresh Kumar
 
Vmwareperformancetroubleshooting 100224104321-phpapp02
Vmwareperformancetroubleshooting 100224104321-phpapp02Vmwareperformancetroubleshooting 100224104321-phpapp02
Vmwareperformancetroubleshooting 100224104321-phpapp02Suresh Kumar
 
I/O System and Case study
I/O System and Case studyI/O System and Case study
I/O System and Case studymalarselvi mms
 
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2Hsien-Hsin Sean Lee, Ph.D.
 
virtual memory management in multi processor mach os
virtual memory management in multi processor mach osvirtual memory management in multi processor mach os
virtual memory management in multi processor mach osAJAY KHARAT
 
Mac Memory Analysis with Volatility
Mac Memory Analysis with VolatilityMac Memory Analysis with Volatility
Mac Memory Analysis with VolatilityAndrew Case
 

Similar to Windows memory manager internals (20)

memory_mapping.ppt
memory_mapping.pptmemory_mapping.ppt
memory_mapping.ppt
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
 
OSSEU18: NVDIMM and Virtualization - George Dunlap, Citrix
OSSEU18: NVDIMM and Virtualization  - George Dunlap, CitrixOSSEU18: NVDIMM and Virtualization  - George Dunlap, Citrix
OSSEU18: NVDIMM and Virtualization - George Dunlap, Citrix
 
Deterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System ArchitectureDeterministic Memory Abstraction and Supporting Multicore System Architecture
Deterministic Memory Abstraction and Supporting Multicore System Architecture
 
Os
OsOs
Os
 
Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory Usage
 
macospptok.pptx
macospptok.pptxmacospptok.pptx
macospptok.pptx
 
I/O System and Case Study
I/O System and Case StudyI/O System and Case Study
I/O System and Case Study
 
XPDDS18: NVDIMM Overview - George Dunlap, Citrix
XPDDS18: NVDIMM Overview - George Dunlap, Citrix XPDDS18: NVDIMM Overview - George Dunlap, Citrix
XPDDS18: NVDIMM Overview - George Dunlap, Citrix
 
Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)
Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)
Vmwareperformancetroubleshooting 100224104321-phpapp02 (1)
 
Vmwareperformancetroubleshooting 100224104321-phpapp02
Vmwareperformancetroubleshooting 100224104321-phpapp02Vmwareperformancetroubleshooting 100224104321-phpapp02
Vmwareperformancetroubleshooting 100224104321-phpapp02
 
Memory (Computer Organization)
Memory (Computer Organization)Memory (Computer Organization)
Memory (Computer Organization)
 
I/O System and Case study
I/O System and Case studyI/O System and Case study
I/O System and Case study
 
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
Lec10 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Memory part2
 
Linux Huge Pages
Linux Huge PagesLinux Huge Pages
Linux Huge Pages
 
virtual memory management in multi processor mach os
virtual memory management in multi processor mach osvirtual memory management in multi processor mach os
virtual memory management in multi processor mach os
 
Mac Memory Analysis with Volatility
Mac Memory Analysis with VolatilityMac Memory Analysis with Volatility
Mac Memory Analysis with Volatility
 
Operation System
Operation SystemOperation System
Operation System
 
Minding SQL Server Memory
Minding SQL Server MemoryMinding SQL Server Memory
Minding SQL Server Memory
 
kerch04.ppt
kerch04.pptkerch04.ppt
kerch04.ppt
 

More from Sisimon Soman

Windows kernel debugging workshop in florida
Windows kernel debugging   workshop in floridaWindows kernel debugging   workshop in florida
Windows kernel debugging workshop in floridaSisimon Soman
 
Windows kernel debugging session 2
Windows kernel debugging session 2Windows kernel debugging session 2
Windows kernel debugging session 2Sisimon Soman
 
Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimonSisimon Soman
 
Storage virtualization citrix blr wide tech talk
Storage virtualization citrix blr wide tech talkStorage virtualization citrix blr wide tech talk
Storage virtualization citrix blr wide tech talkSisimon Soman
 
Introduction to windows kernel
Introduction to windows kernelIntroduction to windows kernel
Introduction to windows kernelSisimon Soman
 
Windows kernel and memory io subsystem
Windows kernel and memory io subsystemWindows kernel and memory io subsystem
Windows kernel and memory io subsystemSisimon Soman
 
VDI storage and storage virtualization
VDI storage and storage virtualizationVDI storage and storage virtualization
VDI storage and storage virtualizationSisimon Soman
 
Introduction to windows kernel
Introduction to windows kernelIntroduction to windows kernel
Introduction to windows kernelSisimon Soman
 
Design Patterns By Sisimon Soman
Design Patterns By Sisimon SomanDesign Patterns By Sisimon Soman
Design Patterns By Sisimon SomanSisimon Soman
 

More from Sisimon Soman (12)

Windows kernel debugging workshop in florida
Windows kernel debugging   workshop in floridaWindows kernel debugging   workshop in florida
Windows kernel debugging workshop in florida
 
Windows kernel
Windows kernelWindows kernel
Windows kernel
 
Windows kernel debugging session 2
Windows kernel debugging session 2Windows kernel debugging session 2
Windows kernel debugging session 2
 
Windows io manager
Windows io managerWindows io manager
Windows io manager
 
Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimon
 
Storage virtualization citrix blr wide tech talk
Storage virtualization citrix blr wide tech talkStorage virtualization citrix blr wide tech talk
Storage virtualization citrix blr wide tech talk
 
Introduction to windows kernel
Introduction to windows kernelIntroduction to windows kernel
Introduction to windows kernel
 
Windows kernel and memory io subsystem
Windows kernel and memory io subsystemWindows kernel and memory io subsystem
Windows kernel and memory io subsystem
 
VDI storage and storage virtualization
VDI storage and storage virtualizationVDI storage and storage virtualization
VDI storage and storage virtualization
 
Introduction to windows kernel
Introduction to windows kernelIntroduction to windows kernel
Introduction to windows kernel
 
COM and DCOM
COM and DCOMCOM and DCOM
COM and DCOM
 
Design Patterns By Sisimon Soman
Design Patterns By Sisimon SomanDesign Patterns By Sisimon Soman
Design Patterns By Sisimon Soman
 

Windows memory manager internals

  • 1. Windows Memory/Cache Manager Internals Sisimon Soman
  • 2. Locality Theory • If access page/cluster n, high possibility to access blocks near to n. • All memory based computing system working on this principle. • Windows has registry keys to configure pre-fetch how many blocks/pages. • Application specific memory manager like Databases, multimedia workload, have application aware pre-fetching.
  • 3. Virtual Memory Manager (VMM) • Apps feels memory is infinity – magic done by VMM. • Multiple apps run concurrently with out interfering other apps data. • Apps feel the entire resource is mine. • Protect OS memory from apps. • Advanced app may need to share memory. Provide solution to memory sharing easily.
  • 4. VMM Continued.. • VMM reserve certain amount of memory to Kernel. • 32 bit box , 2GB for Kernel and 2GB for User apps. • Specific area in Kernel memory reserved to store process specific data like PDE, PTE etc called Hyper Space
  • 5. Segmentation and Paging • X86 processor has segmentation and paging support. • Can disable or enable paging, but segmentation is enabled by default. • Windows uses paging. • Since not able to disable segmentation, it consider the entire memory for segments (also called ‘flat segments’).
  • 6. Paging • Divide entire physical memory in to equal size pages (4K size for x86 platforms). This is called ‘page frames’ and list called ‘page frame database’ (PF DB). • X86 platform PF DB contains 20bit physical offset (remaining 12 bit to address offset in the page). • PF DB also contains flags stating, read/write underway , shared page , etc.
  • 7. VMM Continued.. • Upper 2GB Kernel space is common for all process. • What is it mean – Half of PDE is common to all process !. • Experiment – See the PDE of two process and make sure half of the PDE is same
  • 8. Physical to Virtual address translation • Address translation in both direction – When write PF to pagefile, VMM need to update proper PDE/PTE stating page is in disk. • Done by – Memory Management Unit (MMU) of the processor. – The VMM help MMU. • VMM keep the PDE/PTE info and pass to MMU during process context switch. • MMU translate virtual address to physical address.
  • 9. Translation Lookaside Buffer (TLB) • Address translation is costly operation • It happen frequently – when even touches virtual memory. • TLB keeps a list containing most frequent address translations. • The list is tagged by process ID. • TLB is a generic OS concept - implementation is architecture dependent. • Before doing the address translation MMU search TLB for the PF.
  • 10. Address Translation • In x86 32 bit address – 10 bits of MSB points to the PTE offset in PDE. Thus PDE size of process is 1024 bytes. • Next 10 bits point to the PF starting address in PTE. Thus each PTE contains 1024 bytes. • Remaining 12 bits to address the location in the PF. Thus page size is 4K.
  • 11. What is a Zero Page • Page frames not specific to apps. • If App1 write sensitive data to PF1, and later VMM push the page to page file, attach PF 1 to App2. App2 can see these sensitive info. • It’s a big security flaw, VMM keep a Zero Page list. • Cannot clean the page while freeing memory – it’s a performance problem. • VMM has dedicated thread who activate when system under low memory situation and pick page frames from free PF list, clean it and push to zero page list. • VMM allocate memory from zero page list.
  • 12. Arbitrary Thread Context • Top layer of the driver stack get the request (IRP) in the same process context. • Middle or lower layer driver MAY get the request in any thread context (Ex: IO completion), the current running thread context. • The address in the IRP is specific to the PDE/PTE in the original process context.
  • 13. Arbitrary Thread Context continued.. • How to solve the issue ?. • Note the half of the PDE (Kernel area) is common in all process. • If some how map to the kernel memory (Upper half of PDE), the buffer is accessible from all process.
  • 14. Mapping buffer to Kernel space • Allocate kernel pool from the calling process context, copy user buffer to this Kernel space. • Memory Descriptor List (MDL) – Most commonly used mechanism to keep data in Kernel space.
  • 15. Memory Descriptor List (MDL) • // • // I/O system definitions. • // • // Define a Memory Descriptor List (MDL) • // • // An MDL describes pages in a virtual buffer in terms of physical pages. The • // pages associated with the buffer are described in an array that is allocated • // just after the MDL header structure itself. • // • // One simply calculates the base of the array by adding one to the base • // MDL pointer: • // • // Pages = (PPFN_NUMBER) (Mdl + 1); • // • // Notice that while in the context of the subject thread, the base virtual • // address of a buffer mapped by an MDL may be referenced using the following: • // • // Mdl->StartVa | Mdl->ByteOffset • // • typedef struct _MDL { • struct _MDL *Next; • CSHORT Size; • CSHORT MdlFlags; • struct _EPROCESS *Process; • PVOID MappedSystemVa; • PVOID StartVa; • ULONG ByteCount; • ULONG ByteOffset; • } MDL, *PMDL;
  • 16. MDL Continued.. • #define MmGetSystemAddressForMdlSafe(MDL, PRIORITY) • (((MDL)->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA | • MDL_SOURCE_IS_NONPAGED_POOL)) ? • ((MDL)->MappedSystemVa) : • (MmMapLockedPagesSpecifyCache((MDL), • KernelMode, • MmCached, • NULL, • FALSE, • (PRIORITY)))) • #define MmGetMdlVirtualAddress(Mdl) • ((PVOID) ((PCHAR) ((Mdl)->StartVa) + (Mdl)->ByteOffset))
  • 17. Standby list • To reclaim pages from a process, VMM first move pages to Standby list. • VMM keep it there for a pre-defined ticks. • If process refer the same page, VMM remove from standby list and assign to process. • VMM free the pages from Standby list after the timeout expire. • Pages in standby list is not free, not belong to a process also. • VMM keep a min and max value for free and standby page count. If its out of the limits, appropriate events will signaled and adjust the appropriate lists.
  • 18. Miscellaneous VMM Terms • ZwAllocateVirtualMemory – allocate process specific memory in lower 2GB • Paged Pool • Non Paged Pool • Copy on write (COW)
  • 20. Cache Manager concepts • If disk heads run in the speed of super sonic jets, Cache Manager not required. • Disk access is the main bottleneck that reduce the system performance. Faster CPU and Memory, but disk is still in stone age. • Common concept in Operating Systems, Unix flavor called ‘buffer cache’.
  • 21. What Cache Manager does • Keep the system wide cache data of frequently used secondary storage blocks. • Facilitate read ahead , write back to improve the overall system performance. • With write-back, cache manager combine multiple write requests and issue single write request to improve performance. There is a risk associated with write-back.
  • 22. How Cache Manager works • Cache Manager implement caching using Memory Mapping. • The concept is similar to an App uses memory mapped file. • CreateFile(…dwFlagsAndAttributes ,..) • dwFlagsAndAttributes == FILE_FLAG_NO_BUFFERING means I don’t want cache manager.
  • 23. How Cache Manager works.. • Cache Manager reserve area in higher 2GB (x86 platform) system area. • The Cache Manager reserved page count adjust according to the system memory requirement. • If system has lots of IO intensive tasks, system dynamically increase the cache size. • If system under low memory situation, reduce the buffer cache size.
  • 24. How cached read operation works User Space Kernel Space Cached Read (1) Page Fault (4) VMM Do Memory Mapping (3) Get the Pages From Cache Manager File System CM (2) Get the blocks from disk (5) Disk stack (SCSI/Fibre Channel)
  • 25. How cached write operation works User Space Kernel Space Cached Write (1) Modified Page Writer Thread VMM of VMM Write to disk Do Memory Mapping (3), later(4) Copy data to VMM pages. Copy Pages to CM (2) Cache Manager File System Write the blocks to disk (5) Disk stack (SCSI/Fibre Channel)