Try   HackMD

Ch15 Address Translation

在前面提到 Virtual address space 的概念後,本章要講解一個很重要的觀念—address translation。它的核心主題是介紹如何在作業系統中虛擬化記憶體(memory virtualization),並且讓應用程式以為自己擁有一整塊連續的記憶體區域,實際上這背後靠的是作業系統和硬體的合作。

基本觀念

  • 每個程式都是用 virtual address
  • 然而實際上資料是存在 Physical Address 中
  • 中間的 address translation 是由 MMU 完成的

MMU(Memory Management Unit)

  • 用來進行 memory address translation 的硬體
  • 功能:

    有些功能會在後面的章節介紹

    • Address translation
    • Bounds Checking
      • 防止程式越界存取
    • Protection
      • 防止程式存取 OS 或其他程式的 memory
    • Paging
      • Virtual memory 與 Physical memory 不用連續
    • Cache
      • 使用 TLB(Translation Lookaside Buffer) 提高轉換速度
  • Address translation 的方法
    • Base and Bounds
      • 最簡單的方法
      • 將 Virtual memory 與 Physical memory 視為連續
    • Page Table
      • 把 Physical memory address 分成一個個 page + offset
      • 利用查表找到 page 位置,再加上 offset
    • TLB
      • 一個 cache > 幫助 MMU 更快完成 address translation

Base and Bounds

最簡單的 memory virtualization 機制

  • Base register:Virtual memory 對應 Physical address 開始的位置
  • Bounds Register:Virtual memory 最大大小限制
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
  • 計算方式
    • Physical address = virtual address + base
    • 如果 virtual address >= bounds > 觸發錯誤
  • 問題
    • 因為每個程式 address space 必須連續,容易浪費空間(Internal Fragmentation)
    • 如果 physical memory 有太多不連續的空間未使用,會造成 External Fragmentation

Fragmentation 問題

  • Internal Fragmentation:Address space 內部太多剩餘的空間未被使用,並且無法被其他 process 使用
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
  • External Fragmentation:系統中所有空間總和大於 process address space,但因為不連續而無法分配
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →