CSC 660 Advanced OS Memory Addressing Kernel Modules

  • Slides: 21
Download presentation
CSC 660: Advanced OS Memory Addressing / Kernel Modules CSC 660: Advanced Operating Systems

CSC 660: Advanced OS Memory Addressing / Kernel Modules CSC 660: Advanced Operating Systems 1

Topics 1. 2. 3. 4. x 86 Memory Addressing Segmentation Paging Kernel Modules CSC

Topics 1. 2. 3. 4. x 86 Memory Addressing Segmentation Paging Kernel Modules CSC 660: Advanced Operating Systems 2

x 86 Memory Address Types • Logical Address – The addresses found in machine

x 86 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 3

Segment Registers • Six registers: cs, ss, ds, es, fs, gs – contain 16

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 4

Segment Descriptors • Segment descriptor represents a segment – Base: 32 -bit linear segment

Segment Descriptors • Segment descriptor represents a segment – Base: 32 -bit linear segment initial address – Limit: 20 -bit segment length (usually in 4 K 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 5

Logical to Linear Address Translation Logical Address = 16 bit segment selector + 32

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 6

Linux Segmentation • Linux uses segmentation only where required by x 86 architecture. –

Linux Segmentation • Linux uses segmentation only where required by x 86 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 7

Linux GDT CSC 660: Advanced Operating Systems 8

Linux GDT CSC 660: Advanced Operating Systems 8

Linear to Physical Translation • Handled by paging unit. – Divides memory into 4

Linear to Physical Translation • Handled by paging unit. – Divides memory into 4 KB 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. – cr 3 register points to address of in-use PD. – Page tables are allocated when needed. CSC 660: Advanced Operating Systems 9

x 86 Paging CSC 660: Advanced Operating Systems 10

x 86 Paging CSC 660: Advanced Operating Systems 10

Page Table Entries Present Flag If 0, page not in memory, so paging unit

Page Table Entries Present Flag If 0, page not in memory, so paging unit stores linear addr in cr 2 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 (4 MB) pages. CSC 660: Advanced Operating Systems 11

Physical Address Extension (PAE) • Allows access to 236 = 64 GB physical RAM.

Physical Address Extension (PAE) • Allows access to 236 = 64 GB 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 – – – cr 3 register points to PDPT: most significant 2 bits Directory: next 9 bits Table: next 9 bits Offset: least significant 12 bits CSC 660: Advanced Operating Systems 12

What are Kernel Modules? Parcels of code that can be dynamically inserted or removed

What are Kernel Modules? Parcels of code that can be dynamically inserted or removed from kernel at run time. CSC 660: Advanced Operating Systems 13

Why use Kernel Modules? Ease of maintenance Compile kernel once. Build, add, and remove

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 14

What modules are loaded? > lsmod | head Module Size Used by vmnet 31900

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 0 xf 8 c 3 a 000 vmmon 103584 0 - Live 0 xf 8 c 85000 proc_intf 4100 0 - Live 0 xf 8 c 2 c 000 CSC 660: Advanced Operating Systems 15

Loading Kernel Modules modprobe name 1. Lookup name Resolve aliases using /etc/modprobe. conf 2.

Loading Kernel Modules modprobe name 1. Lookup name Resolve aliases using /etc/modprobe. conf 2. Check dependencies /lib/modules/version/modules. dep Created by depmod –a 3. Load prerequisite modules with insmod 4. Load named module. CSC 660: Advanced Operating Systems 16

Module Licensing Specified with MOD_LICENSE() macro. If an unlicensed module loaded, kernel tainted: hellomod:

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 17

Why Module Licensing? From linux/module. h: 1. So modinfo can tell users if kernel

Why Module Licensing? From linux/module. h: 1. So modinfo can tell users if kernel is free. 2. So community can ignore bug reports including proprietary modules. 3. So vendors can do likewise based on their own policies. CSC 660: Advanced Operating Systems 18

Init and Cleanup Kernel modules must have two functions. init_module Alternatively declare with __init

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 19

Includes All modules need <linux/module. h> For printk need <linux/kernel. h> Others depending on

Includes All modules need <linux/module. h> For printk need <linux/kernel. h> Others depending on function. CSC 660: Advanced Operating Systems 20

References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding

References 1. 2. 3. 4. 5. 6. Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3 rd edition, O’Reilly, 2005. Robert Love, Linux Kernel Development, 2 nd 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, 2 nd edition, Prentice-Hall, 2001. CSC 660: Advanced Operating Systems 21