March 2, 2009 CS 162 Lecture - Notes by Kevin Irish (cs162-ax) + Segmentation + Segment Table Base Register (STBR) points to the active segment table + Segments Table Entries (STE) consist of: + Valid bit (whether or not this segment is in memory) + Size of segment + Real memory location for base of segment + Protection bits: rwx (optional) + Memory Map + Keeps track of what real memory is owned by what segment + Information is needed for allocating memory + Address translation: + Program does load or store using a virtual address + Hardware looks at STBR to find the active segment table + The top x bits of the virtual address are used to identify the proper segment, probably just as an index into the table + Hardware checks if the valid bit is true... if not, segment fault + Hardware checks the segment permission bits to make sure the program has access to that segment + The remaining bits are used as the offset into that segment + Hardware checks to make sure the offset is smaller than the segment size + Hardware maps the virtual address to the real address of segment base address + offset + Segment Fault + If thrown due to valid bit being off: + Hardware traps to OS + OS finds space for the segment, swapping out others if necessary + OS loads segment + OS sets the valid bit, updates all necessary STEs + Makes process ready again, resumes execution + If thrown due to specifying a bad segment (like a null pointer would): + Hardware traps to OS + OS sends segmentation fault to the program + Program will typically terminate + Properties + Segments tend to have a semantic meaning (code, data, stack, heap, etc) + Typically 1 segment table per process, 1 process per segment table + Segment table (or a pointer to it) is stored in the Process Control Block (PCB) + Advantages + Segmentation provides memory protection: programs only have access to memory in their segments + Segments can be shared between segments for shared-memory communication or memory savings + Segments can be moved around and written to disk, so virtual memory can be larger than physical memory + All of a process's memory can be freed when it exits + Segments can be of any size (within addressable limits) + Disadvantages + Each segment has to be continuous + Segment size must be smaller than physical memory + External fragmentation, since variable sized blocks are occupying memory + Allocating memory is complicated and possibly slow + Segment table takes space + Needs hardware support + Paging + Essentially segmentation with segments all of the same size + Pages are typically 4kB on modern machines + "Pages" are a "segment's" worth of virtual address space, "frames" are the corresponding space in real memory + Page Table Base Register (PTBR) points to the active page table + Page Table Entries (PTE) consist of: + Valid bit (whether or not this page is in memory) + Page frame number (top bits of the real address) + Dirty bit (whether this page has been modified since it was loaded or created, and therefore would need to be written to disk if swapped out) + Used / Access bit (whether this page has been read or modified since it was loaded) + Reference bit + Memory Map + Keeps track of in-use frames for allocating new ones and swapping out existing ones + Address translation: + Program does load or store using a virtual address + Hardware looks at PTBR to find the active page table + Top x bits (often 20 bits: 32 bit virtual address 4kB page size) are used to find the right PTE (usually just index into page table) + Hardware checks the valid bit, and causes page fault if false + Hardware replaces the page number in the virtual address with the frame number in the PTE to produce real address + Page fault + Hardware traps to OS + OS finds or creates page + If there are no unused frames, unloads a frame and updates its owner's (owners') page table(s) + OS loads the page into an empty frame + Updates the page table (reflects current frame and sets valid bit) + Makes process ready again, resumes execution + Page table layouts + Basic model + Large array with one PTE per page in virtual memory + Takes up a large amount of memory (megabytes) + Base and bounds + Still a (growable) array, but smaller processes get less space + Additional logic needed to do bounds checking, and the data structure is not as fast + 2-level page table + Root page table entries point to page tables + Double the lookups, but smaller space requirements + Page table inside of kernel's virtual memory + Unused pages in a process's page table do not take up memory + A form of 2-level page table + Hash table + Smaller space requirements than array + IBM and HP do this + Properties + Nearly all computers are now paged + Advantages + Easy to allocate pages + No external fragmentation + Pages can be loaded as needed + No bounds checking necessary + Disadvantages + Internal fragmentation in non-full pages + Page faults can cause large overhead + Needs a page replacement algorithm + Needs hardware support + Combining Segmentation and Paging + Each process has a segment table pointing a page table per segment + Properties + Advantages + Semantic meaning of segments + Segments no longer need to be contiguous in real memory + Segments can easily grow + Segment-based protection and sharing + Disadvantages + More lookups + Segmenting reduces maximum contiguous virtual memory Process List | V Process 1 | V Process 2 --> Segment Table | Name Permissions Page Table V 0: Text (code) Segment x Process 3 1: Static Data Segment r | 2: Stack Segment rw V 3: Heap Segment rw -------------> Page Table Process 4 Valid Frame # Used Dirty | 0: T 0x004c1 T T V 1: T 0x004c2 T T Process 5 2: T 0x00500 T F | 3: F V 4: F Process 6 5: T 0x0030f T T | 6: F V 7: F Process 7 8: F 9: T 0x01f06 F F 10: F + Processes vs. Threads (brief) + Threads can synchronize without OS help + Threads typically have their own stack, but otherwise share virtual memory space + Threads can communicate with little overhead