<<

Operating Systems (Fall/Winter 2018)

Linux Paging

Yajin Zhou (http://yajin.org)

Zhejiang University

Acknowledgement: some pages are based on the slides from Zhi Wang(fsu). Introduction Registers

• General purpose registers

• Segment registers

• Control registers

• EIP (PC)

• EFLAGS System Registers GDTR, LDTR

• GDTR: GDT base address and limit

• Lgdt sgdt: instructions to load/store GDTR

• LGTR Address Translation Step I: logical address to linear address

• 根据指令的性质确定使⽤哪⼀个 段寄存器

• 根据段寄存器内容,找到 segment descriptor

• 从哪⾥找?GDT/LDT,表的地 址在GDTR,LDTR

• 根据segment descriptor找到base address。然后⽐较limit,和检查 访问权限

• liner address = base + offset Step I: logical address to linear address

• Q1: how to get segment selector?

• DS,CS and etc

• Q2: how to get the address of descriptor table?

• GDTR, LDTR Step I: logical address to linear address

Segment descriptor Step I: logical address to linear address Step II: liner address to physical address

• CR3: get the base address of page directory

• find the base address of based on [31:22] of linear address

• Find the page table entry

• Get the physical address CR3 and PDE/PTE

• CR3 is in PCB and saved/restored during context switch PAE

• Physical address extension

• 4G physical address space -> 64GB

• 32 bit linear address ->52 bit physical address CR3 in PAE

• 31:5 -> Page directory pointer

• 4 entries, each entry 64 bit. PDPTE0, PDPTE1, PDPTE2, PDPTE3 PDPTE Linear to physical address with PAE • Step I: select PDPTE using [31:30] of linear address. Bit p of PDPTE should be 1

• Step II: address of page directory table is in [51:12] of PDPTE (相当于 CR3)。 There are 512 entries (8 each) in page directory table. Each entry is called page directory entry (PDE).

• PDE: [51:12] : [51:12] of PDPTE . [11:3] : [29:21] of linear address, bit [2:0] is zero

• Step III: find PTE (page table entry)

• PTE: bit [51:12]: PDE. [11:3]: [20:12] of linear address, [2:0]: 0

• Step IV: get physical address. [51:12]: PTE, BIT [11:0]: liner address CR3 Talk is cheap, show me the code! Setup Segment Registers

• Set up segment registers when creating a new thread Value

• __KERNEL_CS: 12X8 = 96

• __KERNEL_DS: 13X8 = 104

• __USER_DS = 15X8 + 3 = 120 + 3

• __USER_CS = 14X8 + 3 = 110 + 3

Linux uses GDT, not LDT

We will show the value of the GDT entry later! Context switch Experiment Ubuntu 1004, 32 bit

参考: http://ilinuxkernel.com/?p=1276 Kernel module

• Dump registers Kernel module

• Read physical address from a character device Tool to dump memory in user space : Results Output of the user program

• PAE is enabled.

• GDTR is at 0xc1a40000 (linear address) -> physical address: 0x1a40000.

• Temp address: 0xBF95216C Logical -> Linear

• GDT entry at index 15 (offset is 15 x 8).

• 0x00cff3000000FFFF Logical -> Linear

• 0x00cff3000000FFFF -> Base address = 0

• Linear address = logical address

Segment descriptor Linear -> Physical

• Temp address: BF95216C

• 1011 1111 1001 0101 0010 0001 0110 1100

• Locate PDPTE

• cr3=2E1DF000

• PDPTE2=0x000000002E2D2001 -> page directory base address (physical address) = 0x2e2d2000 Linear -> Physical

• 0x2e2d2000 + 0x1fc(11 1111 100)x 8 = 0x2e2d2FE0 -> page directory entry address

• Page table base address: 0x00002E4E4000 (67 is flag) Linear -> Physical

• PTE address: 0x00002E4E4000 + 1 0101 0010b*8 = 0x00002E4E4a90

• Physical frame address: 0x2649900 (0x67 and the highest 8: flag)

• Physical address: 0x26499000 + 0001 0110 1100b = 0X2649916c • 0x2649916c

Logical Address Linear Address Physical Address • 0xBF95216C -> 0xBF95216C -> 0x2649916c HW9 is out