May 8th Lecture ================= Real paging systems: * prepaging (like prefetching for caches) * early replacement (before memory is full) reference string -- the sequence of pages requested over a process' lifetime fault rate -- # of page faults normalized by the length of the reference string working set model * pages for loops/routines being run now are accessed frequently/exclusively, behavior changes as program moves on * if not enough available frames for working set there will be a high fault rate and low performance [ FreeBSD VM ] 32-bit address space (4 GB): * top 1 GB reserved for kernel * bottom 3 GB used by userspace * can be configured 2/2 if kernel needs larger address space (e.g., lots of small processes with high kernel needs such as networking) Kernel: malloc'd vars interrupt stack static/global data kernel text User: argv/envp user stack (grows down) heap (grows up) global/static data program text shared libraries can be placed between stack and heap First 4-8K is usually off limits and unused; forces exceptions rather than silent corruption when software bugs cause NULL pointer usage context switch: * save current VM mapping, load new VM mapping * hardware dependent; may only require a few registers to point to the new page table Accessing variables using VM can require two memory accesses * lookup frame # in page table (which is stored in main memory) * actually read the variable once we have the phys addr TLB removes this "double access" problem * fully associative cache of page table Kernel VM structures (part of vmspace) * vm_map -- machine independent virtual address space * vm_pmap -- machine dependent information * statistics vm_map has linked list of vm_map_entry's, each describing a virtually contiguous range of addresses that share the same protection settings vm_map_entry -- shadow object, vnode object; shadow object used for modification Recall that kernel is always mapped into top 1 GB of address space; this isn't required; some systems have completely separate address spaces for user and kernel mode + gives more address space to both user and kernel; good for things like database servers - harder to copy memory user<->kernel; need to use special instructions rather than accessing pointers directly; this is about twice as slow which can be significant since 1/3 of time in kernel is spent copying - requires complete flush of TLB on kernel entry rather than just on context switches kmem_alloc() -- allocates "wired" pages that can't be swapped out or cause page faults kmem_alloc_pageable() -- allocates pageable mem kmem_free() -- returns pages from above functions For temp storage, userspace usually allocates buffers on the stack; kernel tends to use dynamic allocation since it has a more limited address space and therefore less space set aside for a kernel stack.