Class 20 Crossing into Kernel Space cs 4414

  • Slides: 29
Download presentation
Class 20: Crossing into Kernel Space cs 4414 Fall 2013 University of Virginia David

Class 20: Crossing into Kernel Space cs 4414 Fall 2013 University of Virginia David Evans

Plan for Today Between libc and the kernel PS 3 Benchmarking Results Project Time

Plan for Today Between libc and the kernel PS 3 Benchmarking Results Project Time 7 November 2013 University of Virginia cs 4414 1

Rust Runtime Recap run: : Process: : new(program, argv, options) spawn_process_os(prog, args, env, dir,

Rust Runtime Recap run: : Process: : new(program, argv, options) spawn_process_os(prog, args, env, dir, in_fd, …) fork() Today libc: fork() linux kernel: fork syscall 7 November 2013 University of Virginia cs 4414 2

libstd/rt/io/native/process. rs #[cfg(unix)] fn spawn_process_os(prog: &str, args: &[~str], env: Option<~[(~str, ~str)]>, dir: Option<&Path>, in_fd:

libstd/rt/io/native/process. rs #[cfg(unix)] fn spawn_process_os(prog: &str, args: &[~str], env: Option<~[(~str, ~str)]>, dir: Option<&Path>, in_fd: c_int, out_fd: c_int, err_fd: c_int) -> Spawn. Process. Result { … #[cfg(not(target_os = "macos"), not(windows))] unsafe fn set_environ(envp: *c_void) { extern { static mut environ: *c_void; } environ = envp; } unsafe { let pid = fork(); if pid < 0 { fail!("failure in fork: {}", os: : last_os_error()); } else if pid > 0 { return Spawn. Process. Result {pid: pid, handle: ptr: : null()}; } } 7 November 2013 … // 25 lines of failure-handing code University of Virginia cs 4414 3

Test Program use std: : libc: : funcs: : posix 88: : unistd: :

Test Program use std: : libc: : funcs: : posix 88: : unistd: : fork; #[fixed_stack_segment] fn main() { let pid = unsafe { fork() } ; println(fmt!("pid = %? ", pid)); } 7 November 2013 University of Virginia cs 4414 > rustc fork. rs >. /fork pid = 0 i 32 pid = 15039 i 32 $. /fork pid = 15043 i 32 pid = 0 i 32 4

use std: : libc: : funcs: : posix 88: : unistd: : fork; >

use std: : libc: : funcs: : posix 88: : unistd: : fork; > rustc -O -S fork. rs > wc -l fork. S 72 fork. S #[fixed_stack_segment] fn main() { unsafe { fork() } ; }. section __TEXT, __text, regular, pure_instructi ons. align 4, 0 x 90 __ZN 4 main 18 h 8 b 6694 fe 33 a 5855 ag 4 v 0. 0 E: . cfi_startproc leaq -2097152(%rsp), %r 11 cmpq %gs: 816, %r 11 ja LBB 0_2 movabsq $2097152, %r 10 movabsq $0, %r 11 callq ___morestack ret LBB 0_2: pushq %rbp Ltmp 2: . cfi_def_cfa_offset 16 Ltmp 3: . cfi_offset %rbp, -16 movq %rsp, %rbp 7 November 2013 Ltmp 4: . cfi_def_cfa_register %rbp popq %rbp jmp _fork . cfi_endproc . globl _main. align 4, 0 x 90 _main: . cfi_startproc cmpq %gs: 816, %rsp ja LBB 1_2 movabsq $8, %r 10 movabsq $0, %r 11 callq ___morestack ret LBB 1_2: pushq %rbp Ltmp 7: . cfi_def_cfa_offset 16 Ltmp 8: . cfi_offset %rbp, -16 movq %rsp, %rbp University of Virginia cs 4414 Ltmp 9: . cfi_def_cfa_register %rbp movq %rsi, %rax movq %rdi, %rcx leaq __ZN 4 main 18 h 8 b 6694 fe 33 a 5855 ag 4 v 0. 0 E(%rip), %rsi xorl %edi, %edi movq %rcx, %rdx movq %rax, %rcx popq %rbp jmp __ZN 8 unstable 4 lang 5 start 17 hf 72 eb 8 b 3 c 3 a 0 a 9 ac 4 v 0. 8 E. cfi_endproc. section __DATA, __data. globl __rust_crate_map_toplevel. align 4 __rust_crate_map_toplevel: . long 1. space 4. quad __rust_mod_map. quad __rust_crate_map_std_0. 8_6 c 65 cf 4 b 443341 b 1. quad 0. zerofill __DATA, __bss, __rust_mod_map, 16, 3. section __TEXT, __const. globl _rust_abi_version. align 3 _rust_abi_version: . quad 1. subsections_via_symbols 5

Could actual call to kernel fork be a regular call? 7 November 2013 University

Could actual call to kernel fork be a regular call? 7 November 2013 University of Virginia cs 4414 6

Rust Runtime Entering the Kernel run: : Process: : new(program, argv, options) spawn_process_os(prog, args,

Rust Runtime Entering the Kernel run: : Process: : new(program, argv, options) spawn_process_os(prog, args, env, dir, in_fd, …) fork() libc: fork() linux kernel: fork syscall 7 November 2013 University of Virginia cs 4414 7

Supervisor Mode Kernel code needs (or at least uses) special privileges! What would happen

Supervisor Mode Kernel code needs (or at least uses) special privileges! What would happen if user-level code could just jump into kernel code? 7 November 2013 University of Virginia cs 4414 8

Entering the Kernel User-Level Code … movl $SYS_fork, %eax int $0 x 80 int

Entering the Kernel User-Level Code … movl $SYS_fork, %eax int $0 x 80 int instruction generates an interrupt 7 November 2013 University of Virginia cs 4414 9

Traditional PC Design Programmable Interrupt Controller CPU (PIC) Keyboard 7 November 2013 Interval Timer

Traditional PC Design Programmable Interrupt Controller CPU (PIC) Keyboard 7 November 2013 Interval Timer University of Virginia cs 4414 10

Page 2213 of Intel x 86 Manual: http: //www. intel. com/content/dam/www/public/us/en/documents/manuals/64 -ia-32 -architectures-software-developer-manual-325462. pdf

Page 2213 of Intel x 86 Manual: http: //www. intel. com/content/dam/www/public/us/en/documents/manuals/64 -ia-32 -architectures-software-developer-manual-325462. pdf Modern x 86 Design: “APIC” = “Advanced PIC” 7 November 2013 University of Virginia cs 4414 11

Page 2213 of Intel x 86 Manual: http: //www. intel. com/content/dam/www/public/us/en/documents/manuals/64 -ia-32 -architectures-software-developer-manual-325462. pdf

Page 2213 of Intel x 86 Manual: http: //www. intel. com/content/dam/www/public/us/en/documents/manuals/64 -ia-32 -architectures-software-developer-manual-325462. pdf What should generate a “Local Interrupt”? 7 November 2013 What should generate an “External Interrupt”? University of Virginia cs 4414 12

7 November 2013 University of Virginia cs 4414 13

7 November 2013 University of Virginia cs 4414 13

7 November 2013 University of Virginia cs 4414 14

7 November 2013 University of Virginia cs 4414 14

7 November 2013 University of Virginia cs 4414 15

7 November 2013 University of Virginia cs 4414 15

… movl $SYS_fork, %eax int $0 x 80 Programmable Interrupt Controller (PIC) 7 November

… movl $SYS_fork, %eax int $0 x 80 Programmable Interrupt Controller (PIC) 7 November 2013 University of Virginia cs 4414 Handling Syscall Interrupts CPU 16

7 November 2013 University of Virginia cs 4414 17

7 November 2013 University of Virginia cs 4414 17

7 November 2013 University of Virginia cs 4414 18

7 November 2013 University of Virginia cs 4414 18

Intel manual, p. 146: 7 November 2013 University of Virginia cs 4414 19

Intel manual, p. 146: 7 November 2013 University of Virginia cs 4414 19

7 November 2013 University of Virginia cs 4414 20

7 November 2013 University of Virginia cs 4414 20

Rust Runtime Running in Supervisor Mode run: : Process: : new(program, argv, options) spawn_process_os(prog,

Rust Runtime Running in Supervisor Mode run: : Process: : new(program, argv, options) spawn_process_os(prog, args, env, dir, in_fd, …) fork() int 0 x 80 libc: fork() jumps into kernel code sets supervisor mode linux kernel: fork syscall 7 November 2013 University of Virginia cs 4414 21

PS 3 Bakeoff Winners

PS 3 Bakeoff Winners

13, 2 5701, 3 Average Response Time (milliseconds) 6 000 167, 3 67649, 4

13, 2 5701, 3 Average Response Time (milliseconds) 6 000 167, 3 67649, 4 199, 9 81272, 5 5 000 9, 7 3908, 1 4 000 217, 1 3902, 7 reference zhtta 3 000 2 000 44, 0 989, 7 39, 8 960, 8 1 000 225, 2 531, 3 5, 5 0, 6 0 0 5 November 2013 50 100 150 Total Duration (seconds) University of Virginia cs 4414 200 23

8 pm Friday Rouss/Robertson Hall Room 120 7 November 2013 University of Virginia cs

8 pm Friday Rouss/Robertson Hall Room 120 7 November 2013 University of Virginia cs 4414 24

7 November 2013 University of Virginia cs 4414 25

7 November 2013 University of Virginia cs 4414 25

7 November 2013 University of Virginia cs 4414 26

7 November 2013 University of Virginia cs 4414 26

13, 2 5701, 3 6 000 167, 3 67649, 4 199, 9 81272, 5

13, 2 5701, 3 6 000 167, 3 67649, 4 199, 9 81272, 5 Average Response Time (milliseconds) Kiet, Mark, Tanmoy 5 000 9, 7 3908, 1 4 000 217, 1 3902, 7 reference zhtta 3 000 2 000 44, 0 989, 7 39, 8 960, 8 Hong, Jireh, Marshall Chris, Tong, Yicheng 1 000 225, 2 531, 3 5, 5 0, 6 Harriet, Kevin, Zeming 0 0 5 November 2013 50 100 150 Total Duration (seconds) University of Virginia cs 4414 200 27

Charge Find a team and project! Decoy projects are only allowed in security classes.

Charge Find a team and project! Decoy projects are only allowed in security classes. Sneaking around my house is no longer permitted. 7 November 2013 University of Virginia cs 4414 28