Wireless Mobile Network Laboratory Chapter 14 The process

Wireless Mobile Network Laboratory Chapter 14 The process address space WMN Lab Zhang Xiang-bo GYEONGSANG National University

Contents • • Overview The Memory Descriptor Allocating and destroying a Memory Descriptor Memory Areas VM Operations Manipulating Memory Areas Creating and removing an address interval Paging in Linux GYEONGSANG National University 2

Overview • • • Process address space: The address space of a process consists of all linear addresses that the process is allowed to use. Each process sees a different set of linear addresses; the address used by one process bears no relation to the address used by another. The kernel may dynamically modify a process address space by adding or removing intervals of linear addresses. The kernel represents intervals of linear addresses by means of resources called memory area, which are characterized by (1)an initial linear address, (2)a length, and (3)some access rights. GYEONGSANG National University 3

• • memory area For example 0 x 08048000—— 0 x 0804 c 000 (4 KB)this linear area is allocated to a process, the process can access it. The process can access a memory address only in a valid area. If a process accesses a memory address not in a valid memory area, or if it accesses a valid area in an invalid manner, the kernel kills the process with the dreaded "Segmentation Fault" message. Memory areas can contain all sorts of goodies, such as: A memory map of the executable file's code, called the text section A memory map of the executable file's initialized global variables, called the data section A memory map of the zero page (a page consisting of all zeros, used for purposes such as this) containing uninitialized global variables, called the bss section ………. GYEONGSANG National University 4

Memory Descriptor (1/2) The kernel represents a process's address space with a data structure called the memory descriptor GYEONGSANG National University 5

Memory Descriptor (2/2) The mmap and mm_rb fields are different data structures that contain the same thing : all the memory areas in this address space mm_users field is the number of processes using this address space All of the mm_structures are strung together in a doubly linked list via the mmlist field GYEONGSANG National University 6

Allocating and destroying a Memory Descriptor • • The copy_mm() function is used to copy a parent's memory descriptor to its child during fork(). The mm_structure is allocated from the mm_cachep slab cache via the allocate_mm() macro in kernel/fork. c Processes may elect to share their address spaces with their children by means of the CLONE_VM flag to clone(). For threads: in copy_mm(): • • Destroying a Memory Descriptor When the process associated with a specific address space exits, the exit_mm() function is invoked • • The mm_struct and Kernel Threads The kernel thread can use the previous process’s page tables as needed. GYEONGSANG National University 7

Memory Areas(1/4) • • Memory areas are represented by a memory area object, which is stored in the vm_area_structure The vm_area_structure describes a single memory area over a contiguous interval in a given address space Each memory area shares certain properties, such as permissions and a set of associated operations the single VMA structure can represent multiple types of memory areas for example, memory-mapped files or the process's user-space stack GYEONGSANG National University 8

GYEONGSANG National University 9

Memory Areas(3/4) GYEONGSANG National University 10

Memory Areas(4/4) • • vm_end ~ vm_start is the length in bytes of the memory area. VMA Flags : The vm_flags field contains bit flags, defined in <linux/mm. h>, that specify the behavior of and provide information about the pages contained in the memory area VM_READ Pages can be read from VM_WRITE Pages can be written to VM_EXEC Pages can be executed For object code: be mapped with VM_READ and VM_EXEC, but not VM_WRITE. data section: be mapped with VM_READ and VM_WRITE, but not VM_EXEC GYEONGSANG National University 11

VM Operations • The operations table is represented by struct vm_operations_struct and is defined in <linux/mm. h>: • • • struct vm_operations_struct { void (*open) (struct vm_area_struct *); void (*close) (struct vm_area_struct *); struct page * (*nopage) (struct vm_area_struct *, unsigned long, int); int (*populate) (struct vm_area_struct *, unsigned long, pgprot_t, unsigned long, int); }; • open(): is invoked when the given memory area is added to an address space close() is invoked when the given memory area is removed from an address space nopage() is used to handle page fault. • • GYEONGSANG National University 12

Manipulating Memory Areas • • find_vma() The find_vma() function is defined in mm/mmap. c. The function searches the given address space for the first memory area whose vm_end field is greater than addr. find_vma_prev() The find_vma_prev() function works the same as find_vma(), but it also returns the last VMA before addr. The function is also defined in mm/mmap. c and declared in <linux/mm. h> find_vma_intersection() The find_vma_intersection() function returns the first VMA that overlaps a given address interval GYEONGSANG National University 13

Creating and removing an address interval • • do_mmap (/mm/mmap. c) unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long offset) • To allocate a logical address interval, the kernel uses do_mmap() § Checks for errors and limits § Tries to find an unmapped logical address interval in memory region list § Allocates a vm_area_struct for new interval § Updates bookkeeping and inserts into list (merging if possible) • • do_munmap (/mm/mmap. c) To release a logical address interval, the kernel uses do_munmap() § Locates memory region that overlaps, since it may have been merged § Removes memory region, splitting if necessary § Updates bookkeeping GYEONGSANG National University 14

Paging in Linux(1/4) • • Ordinary Paging X 86 only supports two-level conversion § § § A page directory contains 1024 page tables A page table contains pointers to 1024 pages A page is 4 KB (PAGE_SIZE) 1024 * 1204 * 4 KB = 4 GB CR 3 (in TSS of task_struct) contains the physical base address of the page directory 31 22 21 12 11 0 TABLE OFFSET Page directory GYEONGSANG National University 15

Paging in Linux(2/4) cr 3 + Page Directory (10 MSB) = table_base + Page Table (10) = page_base + Offset = Physical Address GYEONGSANG National University 16

Paging in Linux(3/4) • • • Linux use 3 -level paging § Adds page middle directory (PMD) Apply on the x 86 architecture top-level page table is the page global directory (PGD). second-level page table is the page middle directory (PMD). The final level is called simply the page table (PTE) § This three-level paging scheme also got incorporated into Linux in order to support large memory areas. When large-memory-area support is not required, you can fall back to two-level paging by defining the PMD as "1" GYEONGSANG National University 17

Paging in Linux (4/4) Linear Address Page global directory Page middle directory Page table Physical Address cr 3 GYEONGSANG National University 18

GYEONGSANG National University 19
- Slides: 19