Translation
Translation
Stack
Recap: Process address space
40KB
RAM
Translation at address space granularity
P1
0 Code
Data
Heap
- Physical memory of same size as
- How virtual address is translated to physicalofaddress?
20KB the address space is allocated
8KB - How
Stack
memory isolation isP1 achieved? to each process
P2
0 - What
Code happens on a context switch? - Physical memory for a process can
Data
- Advantages and disadvantages of 28KB
this scheme
Heap be at any address, but should be
32KB
contiguous
8KB Stack P2
40KB
RAM
ISA: commonly used addressing modes (x86)
- At a high-level, instructions contains two parts: opcode and operand
- ISA defines binary encoding of opcodes, mode and register operands
(more complex in practice)
- Operands can be specified in multiple ways
- Register: mov %rcx, %rax
- Immediate: mov $5, %rax
- Absolute: mov 8000000, %rax
- Indirect: mov (%rcx), %rax
- Displacement: mov -16(%rbp), %rax
X86 ISA: examples
- Access local variables using %rbp (examples)
- long a = 100, b =20, c;
- mov $100, -8(%rbp); mov $20, -16(%rbp)
- c = a + b;
- mov -8(%rbp), %rax; mov -16(%rbp), %rcx;
- add %rcx, %rax; mov %rax, -24(%rbp)
- PC relative jump/call
- jmp 0x20(%rip)
- call -0x20(%rip)
Role of the compiler
Compiled assembly
allocate_phys_mem(current);
load_exe_to_physmem(current, exe);
set_user_sp(current → mm_state → stack_start);
set_user_pc(current → mm_state → code_start);
return_to_user;
}
Address space to memory translation
P1
0 Code
Data
Heap
- Physical memory of 8KB is
20KB allocated and the code is loaded
-----------
push %rbp;
8KB Stack mov %rsp, %rbp; - The PCB memory state is updated
-------------
-------------
based on the executable format
28KB
RAM
Process state after exec( )
Kernel stack
20KB
(+10) push %rbp; User execution state
mov %rsp, %rbp; PC = 0
mov (%rbp), %rax SP = 8KB
add $10, %rax
RAM
Process state after exec( )
Kernel stack
20KB
(+10) push %rbp; User execution state
mov %rsp, %rbp; PC = 0
mov (%rbp), %rax SP = 8KB
add $10, %rax
28KB
RAM
Process state after exec( ) Here is a base register. I will add
the value of base register with
PCB the virtual address generated by
20KB the program to get the physical
address. All yours buddy!
(+10) push %rbp; User execution state
mov %rsp, %rbp; PC = 0
mov (%rbp), %rax SP = 8KB CPU
add $10, %rax
OS
28KB Hurray! I will configure the
value of base register as per
my need. I see some light
atlast!
RAM
Translation
CPU
20KB PC = 0
(+10) push %rbp; SP = 8KB
mov %rsp, %rbp;
mov (%rbp), %rax
add $10, %rax T_base = 20KB
RAM
Translation
CPU
20KB PC = 0
(+10) push %rbp; SP = 8KB
mov %rsp, %rbp;
mov (%rbp), %rax
add $10, %rax T_base = 20KB
40KB
RAM
Isolation: How to stop illegal access?
CPU
20KB
PC = 0
(+10) push %rbp;
mov %rsp, %rbp; SP = 8KB
mov (%rbp), %rax
add $10, %rax
T_base = 20KB
28KB
RAM
Isolation: How to stop illegal accesses?
CPU Once a cry baby always a cry
20KB baby! I also provide a limit
PC = 0
(+10) push %rbp; register to enforce the limit
mov %rsp, %rbp; SP = 8KB during translation. Before you
mov (%rbp), %rax
add $10, %rax T_base = 20KB
ask, these registers can only be
changed from privileged mode
T_limit = 8KB
28KB
- The hardware raises a fault if some program
violates the limit.
- The OS fault handler may kill the process
RAM
Translation at address space granularity
P1
0 Code
Data
Heap
- How virtual address is translated to physical address?
- Physical memory of same size as
- The OS sets the base register value
20KBdepending onaddress
of the the physical
spacelocation.
is allocated
8KB The
Stackhardware performs the translation using the base value.
to each process
P2 P1
0 - How
Code memory isolation is achieved? - Physical memory for a process can
Data
- Limit
Heap register can be used to enforce memory
28KB
be isolation
at any address, but should be
- 32KB
What happens on a context switch? contiguous
- Advantages
Stack
and disadvantages
P2 of this scheme
8KB
40KB
RAM
Context switch and translation information
- The base and limit register values can be saved in the outgoing process
PCB during context switch
- Loaded from PCB to the CPU when a process is scheduled
Translation at address space granularity
P1
0 Code
- How virtual address is translated to physical address?
Data
Heap
- - Physical
The OS sets the base register value depending memory
on the of location.
physical same size as
20KB of the
the base
address space is allocated
The hardware performs the translation using value.
8KB -
Stack
How
P2
memory isolation isP1 achieved? to each process
0 - Physical memory for a process can
- Limit
Code register can be used to enforce memory
Data
isolation
28KB
- What happens on a context switch?
Heap be at any address, but should be
32KB
- Save and restore limit and base registers contiguous
8KB - Advantages
Stack and disadvantages
P2 of this scheme
40KB
RAM
Translation at address space granularity: Issues