memory addressing in linux (chap. 2, understanding the linux kernel)
DESCRIPTION
Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel). J. H. Wang Oct. 20, 2008. Outline. Memory Addresses Segmentation in Hardware Segmentation in Linux Paging in Hardware Paging in Linux. Memory Addresses. 3 kinds of addresses in 80x86 microprocessors Logical address - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/1.jpg)
Memory Addressing in Linux(Chap. 2, Understanding the
Linux Kernel)J. H. Wang
Oct. 20, 2008
![Page 2: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/2.jpg)
Outline
• Memory Addresses• Segmentation in Hardware• Segmentation in Linux• Paging in Hardware• Paging in Linux
![Page 3: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/3.jpg)
Memory Addresses
• 3 kinds of addresses in 80x86 microprocessors– Logical address
• Included in the machine language instructions
– Linear address (virtual address)• A single 32-bit unsigned integer that can be
used to address up to 4GB
– Physical address (32-bit unsigned integers)• Used to address memory cells in memory chips
![Page 4: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/4.jpg)
Segmentation in Hardware
• Logical address– Segment id: 16-bit (Segment Selector)– Offset: 32-bit
• Segmentation registers– To hold segment selectors– cs: code segment– ss: stack segment– ds: data segment– es, fs, gs: general purpose
![Page 5: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/5.jpg)
• Segment descriptors: 8-byte– Stored either in GDT (global descriptor table) or
in LDT (local descriptor table)– Processor registers gdtr, ldtr
• Fields in segment descriptor– base: 32-bit– G granularity flag: (segment size in bytes or 4KB)– Limit: 20-bit– S system flag: (system segment or not) – Type: 4-bit
• Code, Data, Task Sate (TSSD), Local Descriptor Table (LDTD)
![Page 6: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/6.jpg)
– DPL (descriptor privilege level): 2-bit– Segment-present flag: (in memory or
not)– D or B flag: (depending on code or data)– Reserved bit (bit 53): 0– AVL flag: ignored by Linux
![Page 7: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/7.jpg)
Segment Descriptor Format
![Page 8: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/8.jpg)
Fast Access to Segment Descriptors
• For each of the six programmable segmentation registers, 80x86 provides an additional nonprogrammable register, which is loaded every time a segment selector is loaded in a segment register– Without accessing the GDT or LDT in
memory
![Page 9: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/9.jpg)
Segment Selector and Segment Descriptor
![Page 10: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/10.jpg)
Segmentation Unit
![Page 11: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/11.jpg)
Segmentation in Linux
• Limited• Linux prefers paging to segmentation
– Memory management is simpler – Portability to wide range of architectures
such as RISC
• GDT: array gdt_table• LDT: not used by the kernel
– modify_ldt() system call
![Page 12: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/12.jpg)
• Segments used by Linux– Kernel code segment: __KERNEL_CS macro– Kernel data segment: __KERNEL_DS macro– User code segment: __USER_CS macro– User data segment: __USER_DS macro– Task state segment (TSS): init_tss array– A default LDT: default_ldt– 4 segments: related to APM (Advanced
Power Management) support
![Page 13: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/13.jpg)
Paging in Hardware
• In 80x86 processors, paging is enabled by setting the PG flag of control register cr0– 4KM pages– Linear address: 32-bit
• Directory: 10 bits• Table: 10 bits• Offset: 12 bits
![Page 14: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/14.jpg)
Paging by 80x86 Processors
![Page 15: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/15.jpg)
• Entries of page directories and page tables– Present flag: in memory– 20-MSB of a page physical address– Accessed flag– Dirty flag: applies only to page table entries– Read/write flag– User/supervisor flag– PCD and PWT flag– Page size flag– Global flag
![Page 16: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/16.jpg)
Extended Paging
![Page 17: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/17.jpg)
• Page size: 4MB• Linear address: 32 bits
– Directory: 10 bits– Offset: 22 bits
• Hardware protection scheme– Only two privilege levels: by
usr/supervisor flag– Only two types of access rights: by
read/write flag
![Page 18: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/18.jpg)
An Example of Paging
![Page 19: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/19.jpg)
Three-Level Paging
• For 64-bit architectures– Ex: Alpha architecture by HP
• Offset: 13 bits• 21 MSB: 0• 3 10-bit fields
![Page 20: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/20.jpg)
Physical Address Extension (PAE) Paging Mechanism
• Starting with Pentium Pro, the number of address pins are increased to 36– Up to 64GB RAM– PAE is activated by setting the PAE flag
in cr4 control register– (see p.51)
![Page 21: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/21.jpg)
Hardware Cache
• (see p. 52-54)• TLB: Translation Lookaside Buffers
![Page 22: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/22.jpg)
Paging in Linux
![Page 23: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/23.jpg)
• When three-level paging is applied to Pentium,– Linux eliminates the Page Middle
Directory field• With PAE,
– Page Global Directory: x86’s Page Directory Pointer Table
– Page Middle Directory: x86’s Page Directory
– Page Table: x86’s Page Table
![Page 24: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/24.jpg)
• Macros for simplifying page table handling:– PAGE_SHIFT, PMD_SHIFT, PGDIR_SHIFT,
PTRS_PER_PTE, PTRS_PERPMD, PTRS_PER_PGD
• Data structures for page table handling– pte_t, pmd_t, pgd_t– pgprot_t
• Macros for page table handling: – Macros: __pte(), __pmd(), __pgd(), __pgprot()– Macros: pte_val(), pmd_val(), pgd_val(),
pgprot_val()
![Page 25: Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel)](https://reader036.vdocuments.net/reader036/viewer/2022081506/56814553550346895db22293/html5/thumbnails/25.jpg)
– Pte_none(), pmd_none(), pgd_none()– Pte_present(), pmd_present(), pgd_present()– Pte_clear(), pmd_clear(), pgd_clear()– Macros: pmd_bad(), pgd_bad()– Functions: pte_read(), pte_write(), pte_exec(),
pte_dirty(), pte_young(), – Functions: pte_wrprotect(), pte_rdprotect(),
pte_exprotect(), pte_mkwrite(), pte_mkread(), pte_mkexec(), pte_mkdirty(), pte_mkclean(), pte_mkyound(), pte_mkold(), pte_modify(p,v), set_pte, set_pmd, set_pgd
– … (see pp. 57-61)