Segmentation

  • Segment is a contiguous portion of the address space of a particular length.
    • Different segments: code, stack, heap.
    • Each segment can be placed in different part of physical memory.
      • base and bounds exist in each segment

Address Transition

  • The code segment starts at the virtual address 0 in address space.

  • Example:

    1. Code Segment: the offset of virtual address 100 is 100 (starts at virtual address 0).

    2. Heap Segment: the offset of virtual address 4200 is 104 (starts at virtual address 4096).

  • Segmentation Fault
    • If an illegal Address is referenced, the hardware detects that the address is out of bounds.

Referring to Segment

  • Explicit approach
    • chop up the address space into segments based on the top few bits of virtual address.
    • Example: virtual address 4200 (01000001101000)[]
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      /*
      SEG_MASK = 0x3000 (11000000000000)
      SEG_SHIFT = 12
      OFFSET_MASK = 0xFFF (00111111111111)
      */
      // get top 2 bits of 14-bit VA
      Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT

      // now get offset
      Offset = VirtualAddress & OFFSET_MASK
      if (Offset >= Bounds[Segment]){
      RaiseException(PROTECTION_FAULT)
      }else{
      PhysAddr = Base[Segment] + Offset
      Register = AccessMemory(PhysAddr)
      }
  • Stack Segment
    • Since stack grows backward, an extra hardware support:
      • The hardware checks which way the segment grows
      • 1 - positive direction, 0 - negative direction
  • Sharing
    • Segment can be shared between address space by an extra hardware support: Production bits
      • a few more bit to indicate permissions of read, write and execute in each segment

Fragmentation

  • External Fragmentation: some little holes of free space in the physical memory that make difficulty to allocate new segments.

  • Compaction: rearranging the exiting segments in physical memory, but it is costly.

    • Stop the running process
    • Copy data to somewhere
    • Change segment register value

Summary

  • Segmentation uses the base-bounds pair to represent the location and boundary of process in the physical memory

Paging

  • Paging splits up the address space into fixed-sized unit called a page.
  • With paging, physical memory is also split into some number of pages called a page frame.
  • Page table per process is needed to translate the virtual address to physical address.

  • Advantages

    1. Flexibility:

      • Do not need to assume how heap and stack grow and be used.
    2. Simplicity:

      • The page in address space and the page frame are same size (usually 4KB).
      • Easy to allocate and keep a free list

Address Translation

  • Two components in the virtual address

    1. VPN: virtual page number
    2. Offset: offset within the page

      • For a 64-byte address space with 16 bytes page.
      • We have $4 = 2^2$ pages and and page size $16 = 2^4$.
      • Therefore, we can use 6 bits to represent the address, where the first 2 bits are VPN, the rest are the offset.
  • Example:

Page Table

  • The page table is just a data structure (array) that is used to map the virtual address to physical address.
  • The OS indexes the array by VPN and looks up the page-table entry.
  • Page Table Storage:
    • Page tables can get very large:
      • 32-bit address ($2^{32} = 4$GB) space with 4-KB pages and 20bits for VPN
        ($4$MB = $2^{20}$ entries * $2^2$ bytes per table entry)
  • Common Flags of Page Table Entry

    1. Valid Bit: Indicating whether the particular transition is valid.
    2. Protection Bit: Indicating whether the page could be read from, written to, or execute from.
    3. Present Bit: Indicating whether this page is in physical memory or on disk.
    4. Dirty Bit: Indicating whether the page has been modified since it was brought into memory.
    5. Reference Bit (Accessed Bit): Indicating that a page has been accessed.

      • P: present bit
      • R/W: read / write bit
      • U/S: supervisor
      • A: accessed bit
      • D: dirty bit
      • PFN: the page frame number
  • Access memory with paging
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    // Extract the VPN from the virtual address
    VPN = (VirtualAddress & VPN_MASK) >> SHIFT

    // Form the address of the page-table entry (PTE)
    PTEAddr = PTBR + (VPN * sizeof(PTE))

    // Fetch the PTE
    PTE = AccessMemory(PTEAddr)

    // Check if process can access the page
    if (PTE.Valid == False) {
    RaiseException(SEGMENTATION_FAULT)
    } else if (CanAccess(PTE.ProtectBits) == False) {
    RaiseException(PROTECTION_FAULT)
    } else {
    // Access is OK: form physical address and fetch it
    offset = VirtualAddress & OFFSET_MASK
    PhysAddr = (PTE.PFN << PFN_SHIFT) | offset
    Register = AccessMemory(PhysAddr)
    }
  • Disadvantage: Paging is too slow
    • To find a location of the target PTE, the starting location of the page table is needed.
    • For every memory reference, paging requires the OS to perform one extra memory reference.

Summary

  • Paging divides the virtual address and physical memory into pages with same page size.
  • Use the page table to store the mapping PFN for each process.