210 likes | 229 Views
Learn about x86 Memory Addressing, Segmentation, Paging, and Kernel Modules in Advanced Operating Systems.
E N D
CSC 660: Advanced OS Memory Addressing / Kernel Modules CSC 660: Advanced Operating Systems
Topics • x86 Memory Addressing • Segmentation • Paging • Kernel Modules CSC 660: Advanced Operating Systems
x86 Memory Address Types • Logical Address • The addresses found in machine code are logical. • Consist of a segment + offset w/i that segment. • Linear (Virtual) Address • Single 32-bit integer. • Translated by paging unit into a physical address. • Physical Address • Used to address memory cells in memory chips. CSC 660: Advanced Operating Systems
Segment Registers • Six registers: cs, ss, ds, es, fs, gs • contain 16-bit segment selector fields • 3 segment registers are special purpose (cs, ss, ds) • cs: points to code segment • Also includes 2-bit Current Privilege Level (CPL) • CPL is the ring value (ring 0 is kernel, 3 is user) • ss: points to stack segment • ds: points to data segment CSC 660: Advanced Operating Systems
Segment Descriptors • Segment descriptor represents a segment • Base: 32-bit linear segment initial address • Limit: 20-bit segment length (usually in 4K pages, but in bytes if granularity flag cleared.) • System flag: 0 if kernel data, 1 otherwise. • Type: 4-bit type (code, data, task state, LDT) • DPL: Descriptor Privilege Level, segment can only be accessed if CPL <= DPL. • Segment descripors stored in • GDT: Global Descriptor Table • LDT: Local Descriptor Table (one per process) CSC 660: Advanced Operating Systems
Logical to Linear Address Translation Logical Address = 16 bit segment selector + 32 bit offset • Segment Selector: • 13-bit index into GDT or LDT • Table Indicator (TI) flag (0=GDT) • Requestor Privilege Level, which is the CPL when selector used. • Translation: • 1. Select GDT or LTD based on TI • 2. Multiply index by 8-byte desc len. • 3. Lookup base in segment desc. • 4. Linear = segment base + offset CSC 660: Advanced Operating Systems
Linux Segmentation • Linux uses segmentation only where required by x86 architecture. • Uses GDT almost exclusively. • Processes can set up their own LDTs, which is useful for Windows compatibility (WINE.) • Linux segments • Kernel Code Segment (all memory, DPL=0) • Kernel Data Segment (all memory, DPL=0, data perms) • User Code Segment (all memory, DPL=3) • User Data Segment (all memory, DPL=3, data perms) • Task State Segment (236 bytes, one/processor) • Default LDT shared by all processes • APM/PNP support segments CSC 660: Advanced Operating Systems
Linux GDT CSC 660: Advanced Operating Systems
Linear to Physical Translation • Handled by paging unit. • Divides memory into 4KB pages. • Linear address is divided into 3 fields • Directory: most significant 10 bits • Table: middle 10 bits • Offset: least significant 12 bits • Page Directory • Every active process must have a Page Directory. • cr3 register points to address of in-use PD. • Page tables are allocated when needed. CSC 660: Advanced Operating Systems
x86 Paging CSC 660: Advanced Operating Systems
Page Table Entries Present Flag If 0, page not in memory, so paging unit stores linear addr in cr2 and generates exception 14 (Page Fault) on access. Offset Least significant 12 bits of address. Accessed Flag Set when paging unit accesses page. Dirty Flag Set when a write operation performed on page. Read/Write Flag Protection flag: is page read-only or read/write? User/Supervisor Flag Privilege level required to access page or page table. Page Size Flag If 1, page directory entries refer to large (4MB) pages. CSC 660: Advanced Operating Systems
Physical Address Extension (PAE) • Allows access to 236 = 64GB physical RAM. • Splits memory into 224 pages. • Page table entries expanded to handle 24-bit addressing. • Page Directory Pointer Table (PDPT) • New level of Page Table with 4 entries. • Linear Addresses are still 32-bits long • cr3 register points to PDPT. • PDPT: most significant 2 bits • Directory: next 9 bits • Table: next 9 bits • Offset: least significant 12 bits CSC 660: Advanced Operating Systems
What are Kernel Modules? Parcels of code that can be dynamically inserted or removed from kernel at run time. CSC 660: Advanced Operating Systems
Why use Kernel Modules? Ease of maintenance Compile kernel once. Build, add, and remove modules afterwards. Ease of distribution Compile single kernel for all machines. Include drivers / options as modules. Vendors can distribute drivers as modules. CSC 660: Advanced Operating Systems
What modules are loaded? > lsmod | head Module Size Used by vmnet 31900 12 vmmon 103584 0 proc_intf 4100 0 freq_table 4100 0 cpufreq_userspace 4572 0 cpufreq_ondemand 6172 0 cpufreq_powersave 1920 0 video 16260 0 sony_acpi 6280 0 > head -3 /proc/modules vmnet 31900 12 - Live 0xf8c3a000 vmmon 103584 0 - Live 0xf8c85000 proc_intf 4100 0 - Live 0xf8c2c000 CSC 660: Advanced Operating Systems
Loading Kernel Modules modprobe name • Lookup name Resolve aliases using /etc/modprobe.conf • Check dependencies /lib/modules/version/modules.dep Created by depmod –a • Load prerequisite modules with insmod • Load named module. CSC 660: Advanced Operating Systems
Module Licensing Specified with MOD_LICENSE() macro. If an unlicensed module loaded, kernel tainted: hellomod: module license ‘unspecified’ taints kernel Options found in linux/module.h GPL Dual BSD/GPL Proprietary CSC 660: Advanced Operating Systems
Why Module Licensing? From linux/module.h: • So modinfo can tell users if kernel is free. • So community can ignore bug reports including proprietary modules. • So vendors can do likewise based on their own policies. CSC 660: Advanced Operating Systems
Init and Cleanup Kernel modules must have two functions. init_module Alternatively declare with __init attribute. Return 0 on success or module will be unloaded. Ex: register interrupt handler, add system call. cleanup_module Alternatively declare with __exit attribute Undoes whatever init_module did so module can be unloaded safely. CSC 660: Advanced Operating Systems
Includes All modules need <linux/module.h> For printk need <linux/kernel.h> Others depending on function. CSC 660: Advanced Operating Systems
References • Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3rd edition, O’Reilly, 2005. • Robert Love, Linux Kernel Development, 2nd edition, Prentice-Hall, 2005. • Kwan Lowe, Kernel Rebuild Guide, http://www.digitalhermit.com/linux/Kernel-Build-HOWTO.html, 2004. • Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, 2005. • Peter Salzman et. al., Linux Kernel Module Programming Guide, version 2.6.1, 2005. • Andrew S. Tanenbaum, Modern Operating Systems, 2nd edition, Prentice-Hall, 2001. CSC 660: Advanced Operating Systems