1 last time SIMD (single instruction multiple data) hardware idea: - - PowerPoint PPT Presentation

1 last time
SMART_READER_LITE
LIVE PREVIEW

1 last time SIMD (single instruction multiple data) hardware idea: - - PowerPoint PPT Presentation

1 last time SIMD (single instruction multiple data) hardware idea: wider ALUs and registers Intels interface _mm sharing the CPU: context switching context = visible CPU state (registers, condition codes, PC, ) exceptions = OS gets run


slide-1
SLIDE 1

1

slide-2
SLIDE 2

last time

SIMD (single instruction multiple data) hardware idea: wider ALUs and registers Intel’s interface _mm… sharing the CPU: context switching context = visible CPU state (registers, condition codes, PC, …) exceptions = OS gets run by the processor

2

slide-3
SLIDE 3

logistics: the fjnal

fjnal exam location: Wilson 402 10 May, 7PM fjll out the confmict form very soon if you can’t make it

3

slide-4
SLIDE 4

logistics: lab this week

using SIMD stufg preview for smooth HW some optional parts — some students get stuck on earlier parts

but I expect many of you to have time maybe better explanation in lecture??

please try — get comfortable for smooth

4

slide-5
SLIDE 5

a note on smooth

it takes most students conisderably more time than rotate start early …especially if you have trouble with the lab

5

slide-6
SLIDE 6

time multiplexing really

loop.exe ssh.exe firefox.exe loop.exe ssh.exe

= operating system exception happens return from exception

6

slide-7
SLIDE 7

time multiplexing really

loop.exe ssh.exe firefox.exe loop.exe ssh.exe

= operating system exception happens return from exception

6

slide-8
SLIDE 8

OS and time multiplexing

starts running instead of normal program

mechanism for this: exceptions (later)

saves old program counter, registers somewhere sets new registers, jumps to new program counter called context switch

saved information called context

7

slide-9
SLIDE 9

context

all registers values

%rax %rbx, …, %rsp, …

condition codes program counter i.e. all visible state in your CPU except memory address space: map from program to real addresses

8

slide-10
SLIDE 10

context switch pseudocode

context_switch(last, next): copy_preexception_pc last−>pc mov rax,last−>rax mov rcx, last−>rcx mov rdx, last−>rdx ... mov next−>rdx, rdx mov next−>rcx, rcx mov next−>rax, rax jmp next−>pc

9

slide-11
SLIDE 11

contexts (A running)

%rax %rbx %rcx %rsp … SF ZF PC

in CPU Process A memory: code, stack, etc. Process B memory: code, stack, etc. OS memory:

%raxSF %rbxZF %rcxPC … …

in Memory

10

slide-12
SLIDE 12

contexts (B running)

%rax %rbx %rcx %rsp … SF ZF PC

in CPU Process A memory: code, stack, etc. Process B memory: code, stack, etc. OS memory:

%raxSF %rbxZF %rcxPC … …

in Memory

11

slide-13
SLIDE 13

memory protection

reading from another program’s memory?

Program A Program B

0x10000: .word 42 // ... // do work // ... movq 0x10000, %rax // while A is working: movq $99, %rax movq %rax, 0x10000 ...

result: %rax is 42 (always) result: might crash

12

slide-14
SLIDE 14

memory protection

reading from another program’s memory?

Program A Program B

0x10000: .word 42 // ... // do work // ... movq 0x10000, %rax // while A is working: movq $99, %rax movq %rax, 0x10000 ...

result: %rax is 42 (always) result: might crash

12

slide-15
SLIDE 15

program memory

0xFFFF FFFF FFFF FFFF 0xFFFF 8000 0000 0000 0x7F… 0x0000 0000 0040 0000 Used by OS Stack Heap / other dynamic Writable data Code + Constants

13

slide-16
SLIDE 16

program memory (two programs)

Used by OS Program A Stack Heap / other dynamic Writable data Code + Constants Used by OS Program B Stack Heap / other dynamic Writable data Code + Constants

14

slide-17
SLIDE 17

address space

programs have illusion of own memory called a program’s address space

Program A addresses Program B addresses mapping (set by OS) mapping (set by OS) Program A code Program B code Program A data Program B data OS data … real memory trigger error = kernel-mode only

15

slide-18
SLIDE 18

program memory (two programs)

Used by OS Program A Stack Heap / other dynamic Writable data Code + Constants Used by OS Program B Stack Heap / other dynamic Writable data Code + Constants

16

slide-19
SLIDE 19

address space

programs have illusion of own memory called a program’s address space

Program A addresses Program B addresses mapping (set by OS) mapping (set by OS) Program A code Program B code Program A data Program B data OS data … real memory trigger error = kernel-mode only

17

slide-20
SLIDE 20

address space mechanisms

next topic called virtual memory mapping called page tables mapping part of what is changed in context switch

18

slide-21
SLIDE 21

context

all registers values

%rax %rbx, …, %rsp, …

condition codes program counter i.e. all visible state in your CPU except memory address space: map from program to real addresses

19

slide-22
SLIDE 22

The Process

process = thread(s) + address space illusion of dedicated machine:

thread = illusion of own CPU address space = illusion of own memory

20

slide-23
SLIDE 23

synchronous versus asynchronous

synchronous — triggered by a particular instruction

traps and faults

asynchronous — comes from outside the program

interrupts and aborts timer event keypress, other input event

21

slide-24
SLIDE 24

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

22

slide-25
SLIDE 25

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

23

slide-26
SLIDE 26

timer interrupt

(conceptually) external timer device

(usually on same chip as processor)

OS confjgures before starting program sends signal to CPU after a fjxed interval

24

slide-27
SLIDE 27

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

25

slide-28
SLIDE 28

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

26

slide-29
SLIDE 29

keyboard input timeline

read_input.exe read_input.exe

trap — read system call interrupt — from keyboard = operating system

27

slide-30
SLIDE 30

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

28

slide-31
SLIDE 31

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

29

slide-32
SLIDE 32

exception implementation

detect condition (program error or external event) save current value of PC somewhere jump to exception handler (part of OS)

jump done without program instruction to do so

30

slide-33
SLIDE 33

exception implementation: notes

I/textbook describe a simplifjed version real x86/x86-64 is a bit more complicated

(mostly for historical reasons)

31

slide-34
SLIDE 34

locating exception handlers

address pointer base + 0x00 base + 0x08 base + 0x10 base + 0x18 … … base + 0x40 … … exception table (in memory) exception table base register

handle_divide_by_zero: movq %rax, save_rax movq %rbx, save_rbx ... handle_timer_interrupt: movq %rax, save_rax movq %rbx, save_rbx ...

… … …

32

slide-35
SLIDE 35

running the exception handler

hardware saves the old program counter (and maybe more) identifjes location of exception handler via table then jumps to that location OS code can save anything else it wants to , etc.

33

slide-36
SLIDE 36

added to CPU for exceptions

new instruction: set exception table base new logic: jump based on exception table new logic: save the old PC (and maybe more)

to special register or to memory

new instruction: return from exception

i.e. jump to saved PC

34

slide-37
SLIDE 37

added to CPU for exceptions

new instruction: set exception table base new logic: jump based on exception table new logic: save the old PC (and maybe more)

to special register or to memory

new instruction: return from exception

i.e. jump to saved PC

34

slide-38
SLIDE 38

added to CPU for exceptions

new instruction: set exception table base new logic: jump based on exception table new logic: save the old PC (and maybe more)

to special register or to memory

new instruction: return from exception

i.e. jump to saved PC

34

slide-39
SLIDE 39

added to CPU for exceptions

new instruction: set exception table base new logic: jump based on exception table new logic: save the old PC (and maybe more)

to special register or to memory

new instruction: return from exception

i.e. jump to saved PC

34

slide-40
SLIDE 40

why return from exception?

reasons related to protection (later) not just ret — can’t modify process’s stack

would break the illusion of dedicated CPU/memory program could use stack in weird way movq $100, −8(%rsp) ... movq −8(%rsp), %rax

(even though this wouldn’t be following calling conventions)

need to restart program undetectably!

35

slide-41
SLIDE 41

exception handler structure

  • 1. save process’s state somewhere
  • 2. do work to handle exception
  • 3. restore a process’s state (maybe a difgerent one)
  • 4. jump back to program

handle_timer_interrupt: mov_from_saved_pc save_pc_loc movq %rax, save_rax_loc ... // choose new process to run here movq new_rax_loc, %rax mov_to_saved_pc new_pc return_from_exception

36

slide-42
SLIDE 42

exceptions and time slicing

loop.exe ssh.exe firefox.exe loop.exe ssh.exe

exception table lookup timer interrupt

handle_timer_interrupt: ... ... set_address_space ssh_address_space mov_to_saved_pc saved_ssh_pc return_from_exception

37

slide-43
SLIDE 43

defeating time slices?

my_exception_table: ... my_handle_timer_interrupt: // HA! Keep running me! return_from_exception main: set_exception_table_base my_exception_table loop: jmp loop

38

slide-44
SLIDE 44

defeating time slices?

wrote a program that tries to set the exception table:

my_exception_table: ... main: // "Load Interrupt // Descriptor Table" // x86 instruction to set exception table lidt my_exception_table ret

result: Segmentation fault (exception!)

39

slide-45
SLIDE 45

privileged instructions

can’t let any program run some instructions allows machines to be shared between users (e.g. lab servers) examples:

set exception table set address space talk to I/O device (hard drive, keyboard, display, …) …

processor has two modes:

kernel mode — privileged instructions work user mode — privileged instructions cause exception instead

40

slide-46
SLIDE 46

kernel mode

extra one-bit register: “are we in kernel mode” exceptions enter kernel mode return from exception instruction leaves kernel mode

41

slide-47
SLIDE 47

program memory (two programs)

Used by OS Program A Stack Heap / other dynamic Writable data Code + Constants Used by OS Program B Stack Heap / other dynamic Writable data Code + Constants

42

slide-48
SLIDE 48

address space

programs have illusion of own memory called a program’s address space

Program A addresses Program B addresses mapping (set by OS) mapping (set by OS) Program A code Program B code Program A data Program B data OS data … real memory trigger error = kernel-mode only

43

slide-49
SLIDE 49

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

44

slide-50
SLIDE 50

protection fault

when program tries to access memory it doesn’t own e.g. trying to write to bad address when program tries to do other things that are not allowed e.g. accessing I/O devices directly e.g. changing exception table base register OS gets control — can crash the program

  • r more interesting things

45

slide-51
SLIDE 51

types of exceptions

interrupts — externally-triggered

timer — keep program from hogging CPU I/O devices — key presses, hard drives, networks, …

faults — errors/events in programs

memory not in address space (“Segmentation fault”) divide by zero invalid instruction

traps — intentionally triggered exceptions

system calls — ask OS to do something

aborts

46

slide-52
SLIDE 52

kernel services

allocating memory? (change address space) reading/writing to fjle? (communicate with hard drive) read input? (communicate with keyborad) all need privileged instructions! need to run code in kernel mode

47

slide-53
SLIDE 53

Linux x86-64 system calls

special instruction: syscall triggers trap (deliberate exception)

48

slide-54
SLIDE 54

Linux syscall calling convention

before syscall: %rax — system call number %rdi, %rsi, %rdx, %r10, %r8, %r9 — args after syscall: %rax — return value

  • n error: %rax contains -1 times “error number”

almost the same as normal function calls

49

slide-55
SLIDE 55

Linux x86-64 hello world

.globl _start .data hello_str: .asciz "Hello, ␣ World!\n" .text _start: movq $1, %rax # 1 = "write" movq $1, %rdi # file descriptor 1 = stdout movq $hello_str, %rsi movq $15, %rdx # 15 = strlen("Hello, World!\n") syscall movq $60, %rax # 60 = exit movq $0, %rdi syscall

50

slide-56
SLIDE 56
  • approx. system call handler

sys_call_table: .quad handle_read_syscall .quad handle_write_syscall // ... handle_syscall: ... // save old PC, etc. pushq %rcx // save registers pushq %rdi ... call *sys_call_table(,%rax,8) ... popq %rdi popq %rcx return_from_exception

51

slide-57
SLIDE 57

Linux system call examples

mmap, brk — allocate memory fork — create new process execve — run a program in the current process _exit — terminate a process

  • pen, read, write — access fjles

terminals, etc. count as fjles, too

52

slide-58
SLIDE 58

system calls and protection

exceptions are only way to access kernel mode

  • perating system controls what proceses can do

… by writing exception handlers very carefully

53

slide-59
SLIDE 59

system call wrappers

library functions to not write assembly:

  • pen:

movq $2, %rax // 2 = sys_open // 2 arguments happen to use same registers syscall // return value in %eax cmp $0, %rax jl has_error ret has_error: neg %rax movq %rax, errno movq $−1, %rax ret

54

slide-60
SLIDE 60

system call wrappers

library functions to not write assembly:

  • pen:

movq $2, %rax // 2 = sys_open // 2 arguments happen to use same registers syscall // return value in %eax cmp $0, %rax jl has_error ret has_error: neg %rax movq %rax, errno movq $−1, %rax ret

54

slide-61
SLIDE 61

system call wrapper: usage

/* unistd.h contains definitions of: O_RDONLY (integer constant), open() */ #include <unistd.h> int main(void) { int file_descriptor; file_descriptor = open("input.txt", O_RDONLY); if (file_descriptor < 0) { printf("error: ␣ %s\n", strerror(errno)); exit(1); } ... result = read(file_descriptor, ...); ... }

55

slide-62
SLIDE 62

system call wrapper: usage

/* unistd.h contains definitions of: O_RDONLY (integer constant), open() */ #include <unistd.h> int main(void) { int file_descriptor; file_descriptor = open("input.txt", O_RDONLY); if (file_descriptor < 0) { printf("error: ␣ %s\n", strerror(errno)); exit(1); } ... result = read(file_descriptor, ...); ... }

55

slide-63
SLIDE 63

a note on terminology (1)

real world: inconsistent terms for exceptions we will follow textbook’s terms in this course the real world won’t you might see:

‘interrupt’ meaning what we call ‘exception’ (x86) ‘exception’ meaning what we call ‘fault’ ‘hard fault’ meaning what we call ‘abort’ ‘trap’ meaning what we call ‘fault’ … and more

56

slide-64
SLIDE 64

a note on terminology (2)

we use the term “kernel mode” some additional terms:

supervisor mode privileged mode ring 0

some systems have multiple levels of privilege

difgerent sets of priviliged operations work

57

slide-65
SLIDE 65

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

58

slide-66
SLIDE 66

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

58

slide-67
SLIDE 67

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

58

slide-68
SLIDE 68

address translation

Program A addresses “virtual” every address accessed instructions and data mapping (set by OS) stored in processor? format? Program A code Program B code Program A data Program B data OS data … real memory “physical” program addresses are ‘virtual’ real addresses are ‘physical’ can be difgerent sizes!

58

slide-69
SLIDE 69

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

59

slide-70
SLIDE 70

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

59

slide-71
SLIDE 71

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages (28 bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

59

slide-72
SLIDE 72

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

59

slide-73
SLIDE 73

toy program memory

code data/heap empty/more heap? stack

00 0000 0000 = 0x000 01 0000 0000 = 0x100 10 0000 0000 = 0x200 11 0000 0000 = 0x300 11 1111 1111 = 0x3FF

virtual page# 0 virtual page# 1 virtual page# 2 virtual page# 3 divide memory into pages ( bytes in this case) “virtual” = addresses the program sees page number is upper bits of address (because page size is power of two) rest of address is called page ofgset

59

slide-74
SLIDE 74

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

60

slide-75
SLIDE 75

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

60

slide-76
SLIDE 76

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

60

slide-77
SLIDE 77

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

60

slide-78
SLIDE 78

toy physical memory

program memory virtual addresses

00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 10 1111 1111 11 0000 0000 to 11 1111 1111

real memory physical addresses

000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 111 0000 0000 to 111 1111 1111

physical page 0 physical page 1 physical page 7 virtual page # physical page # 00 010 (2) 01 111 (7) 10 none 11 000 (0) page table!

60

slide-79
SLIDE 79

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

61

slide-80
SLIDE 80

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

61

slide-81
SLIDE 81

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

61

slide-82
SLIDE 82

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

61

slide-83
SLIDE 83

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

61

slide-84
SLIDE 84

toy page table lookup

virtual page # valid? physical page # read OK? write OK? 00 1 010 (2, code) 1 01 1 111 (7, data) 1 1 10 ??? (ignored) 11 1 000 (0, stack) 1 1 01 1101 0010 — address from CPU trigger exception if 0? 111 1101 0010 to cache (data or instruction) “page table entry” “virtual page number” “physical page number” “page ofgset” “page ofgset”

61

slide-85
SLIDE 85

backup sldies

62

slide-86
SLIDE 86

exceptions in exceptions

handle_timer_interrupt: save_old_pc save_pc movq %r15, save_r15 /* key press here */ movq %r14, save_r14 ... handle_keyboard_interrupt: save_old_pc save_pc movq %r15, save_r15 movq %r14, save_r14 movq %r13, save_r13 ...

solution: disallow this!

63

slide-87
SLIDE 87

exceptions in exceptions

handle_timer_interrupt: save_old_pc save_pc movq %r15, save_r15 /* key press here */ movq %r14, save_r14 ... handle_keyboard_interrupt: save_old_pc save_pc movq %r15, save_r15 movq %r14, save_r14 movq %r13, save_r13 ...

solution: disallow this!

63

slide-88
SLIDE 88

exceptions in exceptions

handle_timer_interrupt: save_old_pc save_pc movq %r15, save_r15 /* key press here */ movq %r14, save_r14 ... handle_keyboard_interrupt: save_old_pc save_pc movq %r15, save_r15 movq %r14, save_r14 movq %r13, save_r13 ...

solution: disallow this!

63

slide-89
SLIDE 89

interrupt disabling

CPU supports disabling (most) interrupts interrupts will wait until it is reenabled CPU has extra state:

interrupts enabled? keyboard interrupt pending? timer interrupt pending?

. . .

exception logic

64

slide-90
SLIDE 90

exceptions in exceptions

handle_timer_interrupt: /* interrupts automatically disabled here */ save_old_pc save_pc movq %r15, save_r15 /* key press here */ movq %r14, save_r14 ... call move_saved_state enable_interrupts /* interrupt happens here! */ ... handle_keyboard_interrupt: save_old_pc save_pc ... call move_saved_state

65

slide-91
SLIDE 91

exceptions in exceptions

handle_timer_interrupt: /* interrupts automatically disabled here */ save_old_pc save_pc movq %r15, save_r15 /* key press here */ movq %r14, save_r14 ... call move_saved_state enable_interrupts /* interrupt happens here! */ ... handle_keyboard_interrupt: save_old_pc save_pc ... call move_saved_state

65

slide-92
SLIDE 92

exceptions in exceptions

handle_timer_interrupt: /* interrupts automatically disabled here */ save_old_pc save_pc movq %r15, save_r15 /* key press here */ movq %r14, save_r14 ... call move_saved_state enable_interrupts /* interrupt happens here! */ ... handle_keyboard_interrupt: save_old_pc save_pc ... call move_saved_state

65

slide-93
SLIDE 93

disabling interrupts

automatically disabled when exception handler starts also done with privileged instruction:

change_keyboard_parameters: disable_interrupts ... /* change things used by handle_keyboard_interrupt here */ ... enable_interrupts

66

slide-94
SLIDE 94

67

slide-95
SLIDE 95
  • n virtual machines

process can be called a ‘virtual machine’ programmed like a complete computer… but weird interface for I/O, memory — system calls can we make that closer to the real machine?

68

slide-96
SLIDE 96
  • n virtual machines

process can be called a ‘virtual machine’ programmed like a complete computer… but weird interface for I/O, memory — system calls can we make that closer to the real machine?

68

slide-97
SLIDE 97

trap-and-emulate

privileged instructions trigger a protection fault we assume operating system crashes what if OS pretends the privileged instruction works?

69

slide-98
SLIDE 98

trap-and-emulate: write-to-screen

struct Process { AddressSpace address_space; SavedRegisters registers; }; void handle_protection_fault(Process *process) { // normal: would crash if (was_write_to_screen()) { do_write_system_call(process); process−>registers−>pc += WRITE_TO_SCREEN_LENGTH; } else { ... } }

70

slide-99
SLIDE 99

trap-and-emulate: write-to-screen

struct Process { AddressSpace address_space; SavedRegisters registers; }; void handle_protection_fault(Process *process) { // normal: would crash if (was_write_to_screen()) { do_write_system_call(process); process−>registers−>pc += WRITE_TO_SCREEN_LENGTH; } else { ... } }

70

slide-100
SLIDE 100

was_write_to_screen()

how does OS know what caused protection fault?

  • ption 1: hardware “type” register
  • ption 2: check instruction:

int opcode = (*process−>registers−>pc & 0xF0) >> 4; if (opcode == WRITE_TO_SCREEN_OPCODE) ...

71

slide-101
SLIDE 101

trap-and-emulate: write-to-screen

struct Process { AddressSpace address_space; SavedRegisters registers; }; void handle_protection_fault(Process *process) { // normal: would crash if (was_write_to_screen()) { do_write_system_call(process); process−>registers−>pc += WRITE_TO_SCREEN_LENGTH; } else { ... } }

72

slide-102
SLIDE 102

trap-and-emulate: write-to-screen

struct Process { AddressSpace address_space; SavedRegisters registers; }; void handle_protection_fault(Process *process) { // normal: would crash if (was_write_to_screen()) { do_write_system_call(process); process−>registers−>pc += WRITE_TO_SCREEN_LENGTH; } else { ... } }

72

slide-103
SLIDE 103

system virtual machines

turn faults into system calls emulate machine that looks more like ‘real’ machine what software like VirtualBox, VMWare, etc. does more complicated than this:

  • n x86, some privileged instructions don’t cause faults

dealing with address spaces is a lot of extra work

73

slide-104
SLIDE 104

process VM versus system VM

Linux process feature real machine feature fjles, sockets I/O devices threads CPU cores mmap/brk (used by malloc) ??? signals exceptions

74

slide-105
SLIDE 105

75

slide-106
SLIDE 106

setjmp/longjmp

jmp_buf env; main() { if (setjmp(env) == 0) { // like try { ... read_file() ... } else { // like catch printf("some ␣ error ␣ happened\n"); } } read_file() { ... if (open failed) { longjmp(env, 1) // like throw } ... } 76

slide-107
SLIDE 107

implementing setjmp/longjmp

setjmp:

copy all registers to jmp_buf … including stack pointer

longjmp

copy registers from jmp_buf … but change %rax (return value)

77

slide-108
SLIDE 108

setjmp psuedocode

setjmp: looks like fjrst half of context switch

setjmp: movq %rcx, env−>rcx movq %rdx, env−>rdx movq %rsp + 8, env−>rsp // +8: skip return value ... save_condition_codes env−>ccs movq 0(%rsp), env−>pc movq $0, %rax // always return 0 ret

78

slide-109
SLIDE 109

longjmp psuedocode

longjmp: looks like second half of context switch

longjmp: movq %rdi, %rax // return a different value movq env−>rcx, %rcx movq env−>rdx, %rdx ... restore_condition_codes env−>ccs movq env−>rsp, %rsp jmp env−>pc

79

slide-110
SLIDE 110

setjmp weirdness — local variables

Undefjned behavior:

int x = 0; if (setjmp(env) == 0) { ... x += 1; longjmp(env, 1); } else { printf("%d\n", x); }

80

slide-111
SLIDE 111

setjmp weirdness — fjx

Defjned behavior:

volatile int x = 0; if (setjmp(env) == 0) { ... x += 1; longjmp(env, 1); } else { printf("%d\n", x); }

81

slide-112
SLIDE 112
  • n implementing try/catch

could do something like setjmp()/longjmp() but setjmp is slow

82

slide-113
SLIDE 113
  • n implementing try/catch

could do something like setjmp()/longjmp() but setjmp is slow

83

slide-114
SLIDE 114

low-overhead try/catch (1)

main() { printf("about ␣ to ␣ read ␣ file\n"); try { read_file(); } catch(...) { printf("some ␣ error ␣ happened\n"); } } read_file() { ... if (open failed) { throw IOException(); } ... }

84

slide-115
SLIDE 115

low-overhead try/catch (2)

main: ... call printf start_try: call read_file end_try: ret main_catch: movq $str, %rdi call printf jmp end_try read_file: pushq %r12 ... call do_throw ... end_read: popq %r12 ret

program counter range action recurse? start_try to end_try jmp main_catch no read_file to end_read popq %r12, ret yes anything else error — lookup table not actual x86 code to run track a “virtual PC” while looking for catch block

85

slide-116
SLIDE 116

low-overhead try/catch (2)

main: ... call printf start_try: call read_file end_try: ret main_catch: movq $str, %rdi call printf jmp end_try read_file: pushq %r12 ... call do_throw ... end_read: popq %r12 ret

program counter range action recurse? start_try to end_try jmp main_catch no read_file to end_read popq %r12, ret yes anything else error — lookup table not actual x86 code to run track a “virtual PC” while looking for catch block

85

slide-117
SLIDE 117

low-overhead try/catch (2)

main: ... call printf start_try: call read_file end_try: ret main_catch: movq $str, %rdi call printf jmp end_try read_file: pushq %r12 ... call do_throw ... end_read: popq %r12 ret

program counter range action recurse? start_try to end_try jmp main_catch no read_file to end_read popq %r12, ret yes anything else error — lookup table not actual x86 code to run track a “virtual PC” while looking for catch block

85

slide-118
SLIDE 118

low-overhead try/catch (2)

main: ... call printf start_try: call read_file end_try: ret main_catch: movq $str, %rdi call printf jmp end_try read_file: pushq %r12 ... call do_throw ... end_read: popq %r12 ret

program counter range action recurse? start_try to end_try jmp main_catch no read_file to end_read popq %r12, ret yes anything else error — lookup table not actual x86 code to run track a “virtual PC” while looking for catch block

85

slide-119
SLIDE 119

lookup table tradeofgs

no overhead if throw not used handles local variables on registers/stack, but… larger executables (probably) extra complexity for compiler

86

slide-120
SLIDE 120

87

slide-121
SLIDE 121

protection and sudo

programs always run in user mode extra permissions from OS do not change this

sudo, superuser, root, SYSTEM, …

  • perating system may remember extra privileges

88

slide-122
SLIDE 122

89

slide-123
SLIDE 123

careful exception handlers

movq $important_os_address, %rsp can’t trust user’s stack pointer! need to have own stack in kernel-mode-only memory need to check all inputs really carefully

90

slide-124
SLIDE 124

91

slide-125
SLIDE 125

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers? page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

92

slide-126
SLIDE 126

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers? page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

92

slide-127
SLIDE 127

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? exercise: how large are physical page numbers? page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

92

slide-128
SLIDE 128

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 248/212 = 236 entries exercise: how large are physical page numbers? 39 − 12 = 27 bits page table entries are 8 bytes (room for expansion, metadata) would take up bytes?? (512GB??)

top 16 bits of address not used for translation

92

slide-129
SLIDE 129

exercise: 64-bit system

my desktop: 39-bit physical addresses; 48-bit virtual addresses 4096 byte pages exercise: how many page table entries? 248/212 = 236 entries exercise: how large are physical page numbers? 39 − 12 = 27 bits page table entries are 8 bytes (room for expansion, metadata) would take up 239 bytes?? (512GB??)

top 16 bits of address not used for translation

92