The Kernel Abstrac/on The Problem Physical Memory Machine - - PowerPoint PPT Presentation

the kernel abstrac on the problem
SMART_READER_LITE
LIVE PREVIEW

The Kernel Abstrac/on The Problem Physical Memory Machine - - PowerPoint PPT Presentation

The Kernel Abstrac/on The Problem Physical Memory Machine Instructions Data Process Operating Heap Executable Edits Compiler System Copy Image: Source Stack Instructions Code and Data Machine Instructions


slide-1
SLIDE 1

The ¡Kernel ¡Abstrac/on ¡

slide-2
SLIDE 2

The ¡Problem ¡

Edits Compiler

Source Code Executable Image: Instructions and Data Machine Instructions Machine Instructions

Operating System Copy

Process Operating System Kernel

Physical Memory

Heap Stack Data Heap Stack Data

slide-3
SLIDE 3

Challenge: ¡Protec/on ¡

  • How ¡do ¡we ¡execute ¡code ¡with ¡restricted ¡

privileges? ¡

– Either ¡because ¡the ¡code ¡is ¡buggy ¡or ¡if ¡it ¡might ¡be ¡ malicious ¡

  • Some ¡examples: ¡

– A ¡script ¡running ¡in ¡a ¡web ¡browser ¡ – A ¡program ¡you ¡just ¡downloaded ¡off ¡the ¡Internet ¡ – A ¡program ¡you ¡just ¡wrote ¡that ¡you ¡haven’t ¡tested ¡ yet ¡

slide-4
SLIDE 4

Main ¡Points ¡

  • Process ¡concept ¡

– A ¡process ¡is ¡the ¡OS ¡abstrac/on ¡for ¡execu/ng ¡a ¡ program ¡with ¡limited ¡privileges ¡

  • Dual-­‑mode ¡opera/on: ¡user ¡vs. ¡kernel ¡

– Kernel-­‑mode: ¡execute ¡with ¡complete ¡privileges ¡ – User-­‑mode: ¡execute ¡with ¡fewer ¡privileges ¡

  • Safe ¡control ¡transfer ¡

– How ¡do ¡we ¡switch ¡from ¡one ¡mode ¡to ¡the ¡other? ¡

slide-5
SLIDE 5

Process ¡Abstrac/on ¡

  • Process: ¡an ¡instance ¡of ¡a ¡program, ¡running ¡

with ¡limited ¡rights ¡

– Thread: ¡a ¡sequence ¡of ¡instruc/ons ¡within ¡a ¡ process ¡

  • Poten/ally ¡many ¡threads ¡per ¡process ¡(for ¡now ¡1:1) ¡

– Address ¡space: ¡set ¡of ¡rights ¡of ¡a ¡process ¡

  • Memory ¡that ¡the ¡process ¡can ¡access ¡
  • Other ¡permissions ¡the ¡process ¡has ¡(e.g., ¡which ¡system ¡

calls ¡it ¡can ¡make, ¡what ¡files ¡it ¡can ¡access) ¡

slide-6
SLIDE 6

Thought ¡Experiment ¡

  • How ¡can ¡we ¡implement ¡execu/on ¡with ¡limited ¡

privilege? ¡

– Execute ¡each ¡program ¡instruc/on ¡in ¡a ¡simulator ¡ – If ¡the ¡instruc/on ¡is ¡permiTed, ¡do ¡the ¡instruc/on ¡ – Otherwise, ¡stop ¡the ¡process ¡ – Basic ¡model ¡in ¡Javascript, ¡… ¡

  • How ¡do ¡we ¡go ¡faster? ¡

– Run ¡the ¡unprivileged ¡code ¡directly ¡on ¡the ¡CPU? ¡

slide-7
SLIDE 7

Hardware ¡Support: ¡ ¡ Dual-­‑Mode ¡Opera/on ¡

  • Kernel ¡mode ¡

– Execu/on ¡with ¡the ¡full ¡privileges ¡of ¡the ¡hardware ¡ – Read/write ¡to ¡any ¡memory, ¡access ¡any ¡I/O ¡device, ¡ read/write ¡any ¡disk ¡sector, ¡send/read ¡any ¡packet ¡

  • User ¡mode ¡

– Limited ¡privileges ¡ – Only ¡those ¡granted ¡by ¡the ¡opera/ng ¡system ¡kernel ¡

  • On ¡the ¡x86, ¡mode ¡stored ¡in ¡EFLAGS ¡register ¡
  • On ¡the ¡MIPS, ¡mode ¡in ¡the ¡status ¡register ¡
slide-8
SLIDE 8

A ¡Model ¡of ¡a ¡CPU ¡

New PC

Program Counter CPU Instructions Fetch and Execute

  • pcode

Select PC

Branch Address

slide-9
SLIDE 9

A ¡CPU ¡with ¡Dual-­‑Mode ¡Opera/on ¡

New PC Handler PC

Program Counter CPU Instructions Fetch and Execute

  • pcode

Select PC

New Mode

Mode Select Mode

Branch Address

slide-10
SLIDE 10

Hardware ¡Support: ¡ Dual-­‑Mode ¡Opera/on ¡

  • Privileged ¡instruc/ons ¡

– Available ¡to ¡kernel ¡ – Not ¡available ¡to ¡user ¡code ¡

  • Limits ¡on ¡memory ¡accesses ¡

– To ¡prevent ¡user ¡code ¡from ¡overwri/ng ¡the ¡kernel ¡

  • Timer ¡

– To ¡regain ¡control ¡from ¡a ¡user ¡program ¡in ¡a ¡loop ¡

  • Safe ¡way ¡to ¡switch ¡from ¡user ¡mode ¡to ¡kernel ¡

mode, ¡and ¡vice ¡versa ¡

slide-11
SLIDE 11

Privileged ¡instruc/ons ¡

  • Examples? ¡
  • What ¡should ¡happen ¡if ¡a ¡user ¡program ¡

aTempts ¡to ¡execute ¡a ¡privileged ¡instruc/on? ¡

slide-12
SLIDE 12

Ques/on ¡

  • For ¡a ¡“Hello ¡world” ¡program, ¡the ¡kernel ¡must ¡

copy ¡the ¡string ¡from ¡the ¡user ¡program ¡ memory ¡into ¡the ¡screen ¡memory. ¡ ¡

  • Why ¡not ¡allow ¡the ¡applica/on ¡to ¡write ¡directly ¡

to ¡the ¡screen’s ¡buffer ¡memory? ¡ ¡

slide-13
SLIDE 13

Simple ¡Memory ¡Protec/on ¡

Base Bound

Physical Memory Processor’s View Implementation

Virtual Address Virtual Memory Physical Address Base Base+ Bound Raise Exception Processor Virtual Address Processor

slide-14
SLIDE 14

Towards ¡Virtual ¡Addresses ¡

  • Problems ¡with ¡base ¡and ¡bounds? ¡
slide-15
SLIDE 15

Virtual ¡Addresses ¡

  • Transla/on ¡

done ¡in ¡ hardware, ¡ using ¡a ¡table ¡

  • Table ¡set ¡up ¡by ¡
  • pera/ng ¡

system ¡kernel ¡

Physical Memory Virtual Addresses (Process Layout) Stack Heap Data Code Heap Data Code Stack

slide-16
SLIDE 16

Example ¡

int ¡sta/cVar ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡// ¡a ¡sta/c ¡variable ¡ main() ¡{ ¡ ¡ ¡ ¡ ¡int ¡localVar ¡= ¡0; ¡ ¡ ¡// ¡a ¡procedure ¡local ¡variable ¡ ¡ ¡ ¡ ¡sta/cVar ¡+= ¡1; ¡localVar ¡+= ¡1; ¡ ¡ ¡ ¡ ¡sleep(10); ¡ ¡// ¡sleep ¡causes ¡the ¡program ¡to ¡wait ¡for ¡x ¡seconds ¡ ¡ ¡ ¡ ¡prinj ¡("sta/c ¡address: ¡%x, ¡value: ¡%d\n", ¡&sta/cVar, ¡sta/cVar); ¡ ¡ ¡ ¡ ¡prinj ¡("procedure ¡local ¡address: ¡%x, ¡value: ¡%d\n", ¡&localVar, ¡localVar); ¡ } ¡ What ¡happens ¡if ¡we ¡run ¡two ¡instances ¡of ¡this ¡program ¡at ¡the ¡same ¡/me? ¡

slide-17
SLIDE 17

Ques/on ¡

  • Suppose ¡we ¡had ¡a ¡perfect ¡object-­‑oriented ¡

language ¡and ¡compiler, ¡so ¡that ¡only ¡an ¡

  • bject’s ¡methods ¡could ¡access ¡the ¡internal ¡

data ¡inside ¡an ¡object. ¡If ¡the ¡opera/ng ¡system ¡

  • nly ¡ran ¡programs ¡wriTen ¡in ¡that ¡language, ¡

would ¡it ¡s/ll ¡need ¡hardware ¡memory ¡address ¡ protec/on? ¡ ¡

slide-18
SLIDE 18

Hardware ¡Timer ¡

  • Hardware ¡device ¡that ¡periodically ¡interrupts ¡

the ¡processor ¡

– Returns ¡control ¡to ¡the ¡kernel ¡handler ¡ – Interrupt ¡frequency ¡set ¡by ¡the ¡kernel ¡

  • Not ¡by ¡user ¡code! ¡

– Interrupts ¡can ¡be ¡temporarily ¡deferred ¡ ¡

  • Not ¡by ¡user ¡code! ¡
  • Interrupt ¡deferral ¡crucial ¡for ¡implemen/ng ¡mutual ¡

exclusion ¡

slide-19
SLIDE 19

Mode ¡Switch ¡

  • From ¡user ¡mode ¡to ¡kernel ¡mode ¡

– Interrupts ¡

  • Triggered ¡by ¡/mer ¡and ¡I/O ¡devices ¡

– Excep/ons ¡

  • Triggered ¡by ¡unexpected ¡program ¡behavior ¡
  • Or ¡malicious ¡behavior! ¡

– System ¡calls ¡(aka ¡protected ¡procedure ¡call) ¡

  • Request ¡by ¡program ¡for ¡kernel ¡to ¡do ¡some ¡opera/on ¡on ¡

its ¡behalf ¡

  • Only ¡limited ¡# ¡of ¡very ¡carefully ¡coded ¡entry ¡points ¡
slide-20
SLIDE 20

Ques/on ¡

  • Examples ¡of ¡excep/ons ¡
  • Examples ¡of ¡system ¡calls ¡
slide-21
SLIDE 21

Mode ¡Switch ¡

  • From ¡kernel ¡mode ¡to ¡user ¡mode ¡

– New ¡process/new ¡thread ¡start ¡

  • Jump ¡to ¡first ¡instruc/on ¡in ¡program/thread ¡

– Return ¡from ¡interrupt, ¡excep/on, ¡system ¡call ¡

  • Resume ¡suspended ¡execu/on ¡

– Process/thread ¡context ¡switch ¡

  • Resume ¡some ¡other ¡process ¡

– User-­‑level ¡upcall ¡(UNIX ¡signal) ¡

  • Asynchronous ¡no/fica/on ¡to ¡user ¡program ¡
slide-22
SLIDE 22

How ¡do ¡we ¡take ¡interrupts ¡safely? ¡

  • Interrupt ¡vector ¡

– Limited ¡number ¡of ¡entry ¡points ¡into ¡kernel ¡

  • Atomic ¡transfer ¡of ¡control ¡

– Single ¡instruc/on ¡to ¡change: ¡ ¡

  • Program ¡counter ¡
  • Stack ¡pointer ¡
  • Memory ¡protec/on ¡
  • Kernel/user ¡mode ¡
  • Transparent ¡restartable ¡execu/on ¡

– User ¡program ¡does ¡not ¡know ¡interrupt ¡occurred ¡

slide-23
SLIDE 23

Interrupt ¡Vector ¡

  • Table ¡set ¡up ¡by ¡OS ¡kernel; ¡pointers ¡to ¡code ¡to ¡

run ¡on ¡different ¡events ¡

Interrupt Vector Processor Register

h a n d l e Ti m e r I n t e r r u p t ( ) { . . . } h a n d l e D i v i d e B y Z e r o ( ) { . . . } h a n d l e S y s t e m C a l l ( ) { . . . }

slide-24
SLIDE 24

Interrupt ¡Stack ¡

  • Per-­‑processor, ¡located ¡in ¡kernel ¡(not ¡user) ¡

memory ¡

– Usually ¡a ¡process/thread ¡has ¡both: ¡kernel ¡and ¡ user ¡stack ¡

  • Why ¡can’t ¡interrupt ¡handler ¡run ¡on ¡the ¡stack ¡
  • f ¡the ¡interrupted ¡user ¡process? ¡
slide-25
SLIDE 25

Interrupt ¡Stack ¡

User Stack Kernal Stack

Proc2 Running Proc1 Main Proc2 Ready to Run Proc1 Main User CPU State User CPU State Syscall Handler I/O Driver Top Half Proc2 Syscall Waiting for I/O Proc1 Main

slide-26
SLIDE 26

Interrupt ¡Masking ¡

  • Interrupt ¡handler ¡runs ¡with ¡interrupts ¡off ¡

– Re-­‑enabled ¡when ¡interrupt ¡completes ¡

  • OS ¡kernel ¡can ¡also ¡turn ¡interrupts ¡off ¡

– Eg., ¡when ¡determining ¡the ¡next ¡process/thread ¡to ¡run ¡ – On ¡x86 ¡

  • CLI: ¡disable ¡interrrupts ¡
  • STI: ¡enable ¡interrupts ¡
  • Only ¡applies ¡to ¡the ¡current ¡CPU ¡(on ¡a ¡mul/core) ¡
  • Cf. ¡implemen/ng ¡synchroniza/on, ¡chapter ¡5 ¡
slide-27
SLIDE 27

Interrupt ¡Handlers ¡

  • Non-­‑blocking, ¡run ¡to ¡comple/on ¡

– Minimum ¡necessary ¡to ¡allow ¡device ¡to ¡take ¡next ¡ interrupt ¡ – Any ¡wai/ng ¡must ¡be ¡limited ¡dura/on ¡ – Wake ¡up ¡other ¡threads ¡to ¡do ¡any ¡real ¡work ¡

  • Linux: ¡semaphore ¡
  • Rest ¡of ¡device ¡driver ¡runs ¡as ¡a ¡kernel ¡thread ¡
slide-28
SLIDE 28

Atomic ¡Mode ¡Transfer ¡

  • On ¡interrupt ¡(x86) ¡

– Save ¡current ¡stack ¡pointer ¡ – Save ¡current ¡program ¡counter ¡ – Save ¡current ¡processor ¡status ¡word ¡(condi/on ¡ codes) ¡ – Switch ¡to ¡kernel ¡stack; ¡put ¡SP, ¡PC, ¡PSW ¡on ¡stack ¡ – Switch ¡to ¡kernel ¡mode ¡ – Vector ¡through ¡interrupt ¡table ¡ – Interrupt ¡handler ¡saves ¡registers ¡it ¡might ¡clobber ¡

slide-29
SLIDE 29

Before ¡

EFLAGS CS: EIP SS: ESP f o o ( ) { w h i l e ( . . . ) { x = x + 1 ; y = y - 2 ; } }

User-level Process

Other Registers: EAX, EBX,

Registers Kernel

h a n d l e r ( ) { p u s h a . . . }

User Stack Exception Stack

slide-30
SLIDE 30

During ¡

EFLAGS CS: EIP SS: ESP f o o ( ) { w h i l e ( . . . ) { x = x + 1 ; y = y - 2 ; } }

User-level Process

  • ther registers:

EAX, EBX,

Registers Kernel

h a n d l e r ( ) { p u s h a . . . }

User Stack Exception Stack

Error EIP CS EFLAGS ESP SS

slide-31
SLIDE 31

Auer ¡

EFLAGS CS: EIP SS: ESP f o o ( ) { w h i l e ( . . . ) { x = x + 1 ; y = y - 2 ; } }

User-level Process

  • ther registers:

EAX, EBX,

Registers Kernel

h a n d l e r ( ) { p u s h a . . . }

Stack

All Registers

Exception Stack

Error EBX EAX EIP CS ESP SS EIP CS EFLAGS ESP SS

slide-32
SLIDE 32

At ¡end ¡of ¡handler ¡

  • Handler ¡restores ¡saved ¡registers ¡
  • Atomically ¡return ¡to ¡interrupted ¡process/

thread ¡

– Restore ¡program ¡counter ¡ – Restore ¡program ¡stack ¡ – Restore ¡processor ¡status ¡word/condi/on ¡codes ¡ – Switch ¡to ¡user ¡mode ¡

slide-33
SLIDE 33

System ¡Calls ¡

User Program Kernel

m a i n ( ) { s y s c a l l ( a r g 1 , a r g 2 ) ; } s y s c a l l ( a r g 1 , a r g 2 ) { // do operation }

User Stub

s y s c a l l ( a r g 1 , a r g 2 ) { t r a p r e t u r n } h a n d l e r ( ) { // copy arguments // from user memory //check arguments s y s c a l l ( a r g 1 , a r g 2 ) ; // copy return value // into user memory r e t u r n ; }

Kernel Stub

Hardware Trap Trap Return

(2) (5) (1) (6) (3) (4)

slide-34
SLIDE 34

Kernel ¡System ¡Call ¡Handler ¡

  • Locate ¡arguments ¡

– In ¡registers ¡or ¡on ¡user(!) ¡stack ¡

  • Copy ¡arguments ¡

– From ¡user ¡memory ¡into ¡kernel ¡memory ¡ – Protect ¡kernel ¡from ¡malicious ¡code ¡evading ¡checks ¡

  • Validate ¡arguments ¡

– Protect ¡kernel ¡from ¡errors ¡in ¡user ¡code ¡

  • Copy ¡results ¡back ¡ ¡

– into ¡user ¡memory ¡

slide-35
SLIDE 35

Web ¡Server ¡Example ¡

  • 1. network

socket read Hardware Server Kernel Network Interface

  • 2. copy arriving

packet (DMA)

  • 3. kernel

copy request buffer

  • 4. parse request
  • 5. file

read Disk Interface

  • 7. disk

data (DMA)

  • 8. kernel

copy reply buffer

  • 9. format reply
  • 6. disk

request

  • 10. network

socket write

  • 11. kernel copy

from user buffer into network buffer

  • 12. format outgoing

packet and DMA

slide-36
SLIDE 36

Boo/ng ¡

Physical Memory

BIOS

Disk (1)

BIOS copies bootloader Bootloader instructions and data OS kernel instructions and data Login app instructions and data

Bootloader OS kernel Login app (2)

Bootloader copies OS kernel

(3)

OS kernel copies login application

slide-37
SLIDE 37

Virtual ¡Machine ¡

Hardware Virtual Disk

Guest Kernel

Timer Handler Guest PC Guest SP Guest Flags Guest Interrupt Table Syscall Handler

Host User Mode Guest Kernel Mode

Guest file system and other kernel services

Guest User Mode Hoste User Mode

Guest Process ... Syscall ... Guest Program Counter Guest Exception Stack Guest Process

Host Kernel

Timer Handler Host PC Host SP Host Flags Host Interrupt Table Syscall Handler

Host KernelMode

Host Exception Stack Physical Disk

slide-38
SLIDE 38

User-­‑Level ¡Virtual ¡Machine ¡

  • How ¡does ¡VM ¡Player ¡work? ¡

– Runs ¡as ¡a ¡user-­‑level ¡applica/on ¡ – How ¡does ¡it ¡catch ¡privileged ¡instruc/ons, ¡interrupts, ¡ device ¡I/O, ¡… ¡

  • Installs ¡kernel ¡driver, ¡transparent ¡to ¡host ¡kernel ¡

– Requires ¡administrator ¡privileges! ¡ – Modifies ¡interrupt ¡table ¡to ¡redirect ¡to ¡kernel ¡VM ¡code ¡ – If ¡interrupt ¡is ¡for ¡VM, ¡upcall ¡ – If ¡interrupt ¡is ¡for ¡another ¡process, ¡reinstalls ¡interrupt ¡ table ¡and ¡resumes ¡kernel ¡

slide-39
SLIDE 39

Upcall: ¡User-­‑level ¡interrupt ¡

  • AKA ¡UNIX ¡signal ¡

– No/fy ¡user ¡process ¡of ¡event ¡that ¡needs ¡to ¡be ¡handled ¡ right ¡away ¡

  • Time-­‑slice ¡for ¡user-­‑level ¡thread ¡manager ¡
  • Interrupt ¡delivery ¡for ¡VM ¡player ¡
  • Direct ¡analogue ¡of ¡kernel ¡interrupts ¡

– Signal ¡handlers ¡– ¡fixed ¡entry ¡points ¡ – Separate ¡signal ¡stack ¡ – Automa/c ¡save/restore ¡registers ¡– ¡transparent ¡resume ¡ – Signal ¡masking: ¡signals ¡disabled ¡while ¡in ¡signal ¡handler ¡

slide-40
SLIDE 40

Upcall: ¡Before ¡

. . . x = y + z ; . . . s i g n a l _ h a n d l e r ( ) { . . . }

Stack Signal Stack Program Counter Stack Pointer

slide-41
SLIDE 41

Upcall: ¡Auer ¡

. . . x = y + z ; . . . s i g n a l _ h a n d l e r ( ) { . . . }

Signal Stack Program Counter Stack Pointer

SP Saved Registers PC

Stack Signal Stack