1 last time
play

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


  1. why return from exception? reasons related to protection (later) not just ret — can’t modify process’s stack program could use stack in weird way ... (even though this wouldn’t be following calling conventions) 35 would break the illusion of dedicated CPU/memory movq $100, − 8(%rsp) movq − 8(%rsp), %rax need to restart program undetectably!

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

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

  8. 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

  9. address space Program A code = kernel-mode only trigger error real memory … OS data Program B data Program A data Program B code (set by OS) programs have illusion of own memory mapping (set by OS) mapping addresses Program B addresses Program A called a program’s address space 43

  10. 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

  11. 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 or more interesting things 45

  12. 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

  13. 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

  14. Linux x86-64 system calls special instruction: syscall 48 triggers trap (deliberate exception)

  15. Linux syscall calling convention before syscall : %rax — system call number %rdi , %rsi , %rdx , %r10 , %r8 , %r9 — args after syscall : %rax — return value on error: %rax contains -1 times “error number” almost the same as normal function calls 49

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

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

  18. Linux system call examples mmap , brk — allocate memory fork — create new process execve — run a program in the current process _exit — terminate a process open , read , write — access fjles terminals, etc. count as fjles, too 52

  19. system calls and protection exceptions are only way to access kernel mode operating system controls what proceses can do … by writing exception handlers very carefully 53

  20. system call wrappers library functions to not write assembly: open: 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 ret 54 movq $ − 1, %rax

  21. system call wrappers library functions to not write assembly: open: 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 ret 54 movq $ − 1, %rax

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

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

  24. 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

  25. a note on terminology (2) we use the term “kernel mode” some additional terms: supervisor mode privileged mode ring 0 difgerent sets of priviliged operations work 57 some systems have multiple levels of privilege

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

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

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

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

  30. toy program memory virtual page# 1 rest of address is called page ofgset (because page size is power of two) page number is upper bits of address “virtual” = addresses the program sees bytes in this case) divide memory into pages ( virtual page# 3 virtual page# 2 virtual page# 0 code 11 1111 1111 = 0x3FF 11 0000 0000 = 0x300 10 0000 0000 = 0x200 01 0000 0000 = 0x100 00 0000 0000 = 0x000 stack empty/more heap? data/heap 59

  31. toy program memory virtual page# 1 rest of address is called page ofgset (because page size is power of two) page number is upper bits of address “virtual” = addresses the program sees bytes in this case) divide memory into pages ( virtual page# 3 virtual page# 2 virtual page# 0 code 11 1111 1111 = 0x3FF 11 0000 0000 = 0x300 10 0000 0000 = 0x200 01 0000 0000 = 0x100 00 0000 0000 = 0x000 stack empty/more heap? data/heap 59

  32. toy program memory code rest of address is called page ofgset (because page size is power of two) page number is upper bits of address “virtual” = addresses the program sees virtual page# 3 virtual page# 2 virtual page# 1 virtual page# 0 11 1111 1111 = 0x3FF 11 0000 0000 = 0x300 10 0000 0000 = 0x200 01 0000 0000 = 0x100 00 0000 0000 = 0x000 stack empty/more heap? data/heap 59 divide memory into pages ( 2 8 bytes in this case)

  33. toy program memory virtual page# 1 rest of address is called page ofgset (because page size is power of two) page number is upper bits of address “virtual” = addresses the program sees bytes in this case) divide memory into pages ( virtual page# 3 virtual page# 2 virtual page# 0 code 11 1111 1111 = 0x3FF 11 0000 0000 = 0x300 10 0000 0000 = 0x200 01 0000 0000 = 0x100 00 0000 0000 = 0x000 stack empty/more heap? data/heap 59

  34. toy program memory virtual page# 1 rest of address is called page ofgset (because page size is power of two) page number is upper bits of address “virtual” = addresses the program sees bytes in this case) divide memory into pages ( virtual page# 3 virtual page# 2 virtual page# 0 code 11 1111 1111 = 0x3FF 11 0000 0000 = 0x300 10 0000 0000 = 0x200 01 0000 0000 = 0x100 00 0000 0000 = 0x000 stack empty/more heap? data/heap 59

  35. toy physical memory 010 (2) physical page 7 virtual page # physical page # 00 01 physical page 0 111 (7) 10 none 11 000 (0) page table! physical page 1 111 1111 1111 program memory 10 1111 1111 virtual addresses 00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 11 0000 0000 to 111 0000 0000 to 11 1111 1111 real memory physical addresses 000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 60

  36. toy physical memory 010 (2) physical page 7 virtual page # physical page # 00 01 physical page 0 111 (7) 10 none 11 000 (0) page table! physical page 1 111 1111 1111 program memory 10 1111 1111 virtual addresses 00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 11 0000 0000 to 111 0000 0000 to 11 1111 1111 real memory physical addresses 000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 60

  37. toy physical memory 010 (2) physical page 7 virtual page # physical page # 00 01 physical page 0 111 (7) 10 none 11 000 (0) page table! physical page 1 111 1111 1111 program memory 10 1111 1111 virtual addresses 00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 11 0000 0000 to 111 0000 0000 to 11 1111 1111 real memory physical addresses 000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 60

  38. toy physical memory 010 (2) physical page 7 virtual page # physical page # 00 01 physical page 0 111 (7) 10 none 11 000 (0) page table! physical page 1 111 1111 1111 program memory 10 1111 1111 virtual addresses 00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 11 0000 0000 to 111 0000 0000 to 11 1111 1111 real memory physical addresses 000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 60

  39. toy physical memory 010 (2) physical page 7 virtual page # physical page # 00 01 physical page 0 111 (7) 10 none 11 000 (0) page table! physical page 1 111 1111 1111 program memory 10 1111 1111 virtual addresses 00 0000 0000 to 00 1111 1111 01 0000 0000 to 01 1111 1111 10 0000 0000 to 11 0000 0000 to 111 0000 0000 to 11 1111 1111 real memory physical addresses 000 0000 0000 to 000 1111 1111 001 0000 0000 to 001 1111 1111 60

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

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

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

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

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

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

  46. backup sldies 62

  47. exceptions in exceptions handle_timer_interrupt: save_old_pc save_pc movq %r15, save_r15 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 /* key press here */

  48. exceptions in exceptions handle_timer_interrupt: save_old_pc save_pc movq %r15, save_r15 movq %r14, save_r14 ... handle_keyboard_interrupt: movq %r15, save_r15 movq %r14, save_r14 movq %r13, save_r13 ... solution: disallow this! 63 /* key press here */ save_old_pc save_pc

  49. exceptions in exceptions handle_timer_interrupt: save_old_pc save_pc movq %r15, save_r15 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 /* key press here */

  50. interrupt disabling CPU supports disabling (most) interrupts CPU has extra state: interrupts enabled? keyboard interrupt pending? timer interrupt pending? . . . exception logic 64 interrupts will wait until it is reenabled

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

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

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

  54. disabling interrupts automatically disabled when exception handler starts also done with privileged instruction: change_keyboard_parameters: disable_interrupts ... ... enable_interrupts 66 /* change things used by handle_keyboard_interrupt here */

  55. 67

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

  57. on 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

  58. trap-and-emulate privileged instructions trigger a protection fault we assume operating system crashes what if OS pretends the privileged instruction works? 69

  59. trap-and-emulate: write-to-screen struct Process { AddressSpace address_space; SavedRegisters registers; }; // normal: would crash if (was_write_to_screen()) { do_write_system_call(process); WRITE_TO_SCREEN_LENGTH; ... } } 70 void handle_protection_fault(Process *process) { process − >registers − >pc += } else {

  60. trap-and-emulate: write-to-screen struct Process { AddressSpace address_space; SavedRegisters registers; }; // normal: would crash if (was_write_to_screen()) { do_write_system_call(process); WRITE_TO_SCREEN_LENGTH; ... } } 70 void handle_protection_fault(Process *process) { process − >registers − >pc += } else {

  61. was_write_to_screen() how does OS know what caused protection fault? option 1: hardware “type” register option 2: check instruction: if (opcode == WRITE_TO_SCREEN_OPCODE) ... 71 int opcode = (*process − >registers − >pc & 0xF0) >> 4;

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend