21
15. Address Translation Operating System: Three Easy Pieces 1 AOS@UC

15. Address Translation - GitHub Pages › aos › 15.Address_Translation.pdf · Handle the trap Call switch() routine save regs(A) to proc-struct(A) (including base/bounds) restore

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • 15. Address TranslationOperating System: Three Easy Pieces

    1AOS@UC

  • Memory Virtualizing with Efficiency and Control

    p Memory virtualizing takes a similar strategy known as limited direct

    execution(LDE) for efficiency and control.

    p In memory virtualizing, efficiency and control are attained by hardware

    support.

    w e.g., registers, TLB(Translation Look-aside Buffer)s, page-table

    AOS@UC 2

  • Assumptions Made (just in this lecture)

    p User address space is contiguous in memory

    p User address space is smaller than the physical memory (up to 64KB)

    p Each address space has the same size (up to 16KB)

    AOS@UC 3

  • Address Translation

    p Hardware transforms a virtual address to a physical address.

    w The desired information is actually stored in a physical address.

    p The OS must get involved at key points to set up the hardware.

    w The OS must manage memory to judiciously intervene.

    AOS@UC 4

  • Example: Address Translation

    p C - Language code

    w Load a value from memory

    w Increment it by three

    w Store the value back into memory

    void func()int x;...x = x + 3; // this is the line of code we are interested in

    AOS@UC 5

  • Example: Address Translation(Cont.)

    p Assembly

    w Presume that the address of ‘x’ has been place in ebx register.

    w Load the value at that address into eax register.

    w Add 3 to eax register.

    w Store the value in eax back into memory.

    128 : movl 0x0(%ebx), %eax ; load 0+ebx into eax132 : addl $0x03, %eax ; add 3 to eax register135 : movl %eax, 0x0(%ebx) ; store eax back to mem

    AOS@UC 6

  • Example: Address Translation(Cont.)

    • Fetch instruction at address 128

    • Execute this instruction (load from address 15KB)

    • Fetch instruction at address 132

    • Execute this instruction (no memory reference)

    • Fetch the instruction at address 135

    • Execute this instruction (store to address 15 KB)

    (free)

    3000

    Stack

    stack

    heap

    Heap

    14KB

    Program Code

    16KB

    15KB

    0KB

    1KB

    2KB

    3KB

    4KB

    128132135

    movl 0x0(%ebx),%eaxAddl 0x03,%eaxmovl %eax,0x0(%ebx)

    AOS@UC 7

  • Relocation Address Space

    p The OS wants to place the process somewhere else in physical

    memory, not at address 0.

    w The address space start at address 0.

    p But how make it transparently?

    AOS@UC 8

  • A Single Relocated Process

    (free)

    Stack

    stack

    heap

    Heap

    Program Code

    16KB

    0KB

    (not in use)

    (not in use)

    Operating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Code

    (allocated but not in use)

    Heap

    Stack

    Relo

    cate

    d P

    roce

    ss

    Address SpacePhysical Memory

    AOS@UC 9

  • Base and Bounds Registers

    (free)

    Stack

    stack

    heap

    Heap

    Program Code

    16KB

    0KB

    (not in use)

    (not in use)

    Operating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Code

    (allocated but not in use)

    Heap

    Stack

    Address SpacePhysical Memory

    32KB

    base register

    16KB

    bounds register

    AOS@UC 10

  • Aside: Software-based Relocation

    p If Harware-support is not present?

    w Static-Relocation

    p Loader should “transform” the executable

    p Problems

    w No protection

    w Late relocation is hard

    p Hardware support is mandatory!

    AOS@UC 11

  • Dynamic (Hardware based) Relocation

    p When a program starts running, the OS decides where in physical

    memory a process should be loaded.

    w Set the base register a value.

    w Every virtual address must not be greater than bound and negative.

    w ISA provide

    ¢ Privileged ins to handle those registers

    ¢ Specific exception to detect (and handle miss behaviors)

    𝑝ℎ𝑦𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠 = 𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠 + 𝑏𝑎𝑠𝑒

    0 ≤ 𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠 < 𝑏𝑜𝑢𝑛𝑑𝑠

    AOS@UC 12

  • Relocation and Address Translation

    w Fetch instruction at address 128

    w Execute this instruction

    ¢ Load from address 15KB

    (free)

    3000

    Stack

    stack

    heap

    Heap

    14KB

    Program Code

    16KB

    15KB

    0KB

    1KB

    2KB

    3KB

    4KB

    128132135

    128 : movl 0x0(%ebx), %eax

    32896 = 128 + 32𝐾𝐵(𝑏𝑎𝑠𝑒)

    47𝐾𝐵 = 15𝐾𝐵 + 32𝐾𝐵(𝑏𝑎𝑠𝑒)

    movl 0x0(%ebx),%eaxAddl 0x03,%eaxmovl %eax,0x0(%ebx)

    AOS@UC 13

    CPUMMU• base• bounds

    Memory

  • Two ways of Bounds Register

    (free)

    Stack

    Heap

    Program Code

    16KB

    0KB

    (not in use)

    (not in use)

    Operating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Code

    (allocated but not in use)

    Heap

    Stack

    Address SpacePhysical Memory

    48KB16KB

    bounds

    𝒕𝒉𝒆𝒔𝒊𝒛𝒆𝒐𝒇𝒂𝒅𝒅𝒓𝒆𝒔𝒔𝒔𝒑𝒂𝒄𝒆

    𝒑𝒉𝒚𝒔𝒊𝒄𝒂𝒍𝒂𝒅𝒅𝒓𝒆𝒔𝒔𝒐𝒇𝒕𝒉𝒆𝒆𝒏𝒅𝒐𝒇

    𝒂𝒅𝒅𝒓𝒆𝒔𝒔𝒔𝒑𝒂𝒄𝒆

    bounds

    AOS@UC 14

  • OS Issues for Memory Virtualizing

    p The OS must take action to implement base-and-bounds approach.

    p Three critical junctures:

    w When a process starts running:

    ¢ Finding space for address space in physical memory

    w When a process is terminated:

    ¢ Reclaiming the memory for use

    w When context switch occurs:

    ¢ Saving and storing the base-and-bounds pair

    AOS@UC 15

  • OS Issues: When a Process Starts Running

    p The OS must find a room for a new address space.

    w free list : A list of the range of the physical memory which are not in use.

    0KB

    16KB

    32KB

    48KB

    64KB

    Code

    (allocated but not in use)

    Physical Memory

    The OS lookup the free list

    (not in use)

    Heap

    Stack

    Operating System

    (not in use)

    Free list

    16KB

    48KB

    AOS@UC 16

  • OS Issues: When a Process Is Terminated

    p The OS must put the memory back on the free list.

    Operating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Physical Memory

    (not in use)

    (not in use)

    Operating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Physical Memory

    (not in use)

    (not in use)

    (not in use)Process A

    Free list

    16KB

    48KB

    Free list

    16KB

    32KB

    48KB

    AOS@UC 17

  • OS Issues: When Context Switch Occurs

    p The OS must save and restore the base-and-bounds pair.

    w In process structure or process control block(PCB)

    Operating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Physical Memory

    (not in use)

    Process ACurrently Running

    Process B

    48KB

    bounds

    32KB

    base

    Context SwitchingOperating System

    0KB

    16KB

    32KB

    48KB

    64KB

    Physical Memory

    (not in use)

    Process A

    Process BCurrently Running

    64KB

    48KB

    Process A PCB

    …base : 32KBbounds : 48KB …

    bounds

    base

    AOS@UC 18

  • Summary: Dynamic Relocation (strawman version)MECHANISM: ADDRESS TRANSLATION 11

    OS @ boot Hardware(kernel mode)initialize trap table

    remember addresses of...system call handlertimer handlerillegal mem-access handlerillegal instruction handler

    start interrupt timerstart timer; interrupt after X ms

    initialize process tableinitialize free list

    OS @ run Hardware Program(kernel mode) (user mode)To start process A:

    allocate entry in process tableallocate memory for processset base/bounds registersreturn-from-trap (into A)

    restore registers of Amove to user modejump to A’s (initial) PC

    Process A runsFetch instruction

    Translate virtual addressand perform fetch

    Execute instructionIf explicit load/store:

    Ensure address is in-bounds;Translate virtual address

    and perform load/store...

    Timer interruptmove to kernel modeJump to interrupt handler

    Handle the trapCall switch() routine

    save regs(A) to proc-struct(A)(including base/bounds)restore regs(B) from proc-struct(B)(including base/bounds)

    return-from-trap (into B)restore registers of Bmove to user modejump to B’s PC

    Process B runsExecute bad load

    Load is out-of-bounds;move to kernel modejump to trap handler

    Handle the trapDecide to terminate process Bde-allocate B’s memoryfree B’s entry in process table

    Figure 15.5: Limited Direct Execution Protocol (Dynamic Relocation)

    c⃝ 2014, ARPACI-DUSSEAUTHREE

    EASYPIECES

    AOS@UC 19

  • Summary: Dynamic Relocation (strawman version)

    MECHANISM: ADDRESS TRANSLATION 11

    OS @ boot Hardware(kernel mode)initialize trap table

    remember addresses of...system call handlertimer handlerillegal mem-access handlerillegal instruction handler

    start interrupt timerstart timer; interrupt after X ms

    initialize process tableinitialize free list

    OS @ run Hardware Program(kernel mode) (user mode)To start process A:

    allocate entry in process tableallocate memory for processset base/bounds registersreturn-from-trap (into A)

    restore registers of Amove to user modejump to A’s (initial) PC

    Process A runsFetch instruction

    Translate virtual addressand perform fetch

    Execute instructionIf explicit load/store:

    Ensure address is in-bounds;Translate virtual address

    and perform load/store...

    Timer interruptmove to kernel modeJump to interrupt handler

    Handle the trapCall switch() routine

    save regs(A) to proc-struct(A)(including base/bounds)restore regs(B) from proc-struct(B)(including base/bounds)

    return-from-trap (into B)restore registers of Bmove to user modejump to B’s PC

    Process B runsExecute bad load

    Load is out-of-bounds;move to kernel modejump to trap handler

    Handle the trapDecide to terminate process Bde-allocate B’s memoryfree B’s entry in process table

    Figure 15.5: Limited Direct Execution Protocol (Dynamic Relocation)

    c⃝ 2014, ARPACI-DUSSEAUTHREE

    EASYPIECES

    AOS@UC 20

  • p Disclaimer: Disclaimer: This lecture slide set is used in AOS course at University of Cantabria.

    Was initially developed for Operating System course in Computer Science Dept. at Hanyang

    University. This lecture slide set is for OSTEP book written by Remzi and Andrea Arpaci-

    Dusseau (at University of Wisconsin)

    AOS@UC 21