intro / what is an OS / processes and system calls 1 Changelog 14 - - PowerPoint PPT Presentation

intro what is an os processes and system calls
SMART_READER_LITE
LIVE PREVIEW

intro / what is an OS / processes and system calls 1 Changelog 14 - - PowerPoint PPT Presentation

intro / what is an OS / processes and system calls 1 Changelog 14 January 2020: reorganize slides to move process defjnition earlier 14 January 2020: use 64 instead of 0x40 in write() fmow chart to be more consistent with code


slide-1
SLIDE 1

intro / what is an OS / processes and system calls

1

slide-2
SLIDE 2

Changelog

14 January 2020: reorganize slides to move process defjnition earlier 14 January 2020: use “64” instead of “0x40” in write() fmow chart to be more consistent with code shown later

1

slide-3
SLIDE 3

course webpage

https://www.cs.virginia.edu/~cr4bd/4414/S2020/ linked ofg Collab

2

slide-4
SLIDE 4

homeworks

there will be programming assignments fjrst is due next week …mostly in C or C++; one in Python

  • ne or two weeks

if two weeks “checkpoint” submission after fjrst week

two week assignments worth more schedule is aggressive…

3

slide-5
SLIDE 5

xv6

some assignments will use xv6, a teaching operating system simplifjed OS based on an old Unix version

built by some people at MIT

theoretically actually boots on real 32-bit x86 hardware …and supports multicore!

(but we’ll run it only single-core, in an emulator)

4

slide-6
SLIDE 6

quizzes

there will be online quizzes after each week of lecture …starting this week (due next Tuesday) same interface as CS 3330, but no time limit

(haven’t seen it? we’ll talk more on Thursday)

quizzes are open notes, open book, open Internet

5

slide-7
SLIDE 7

exams

midterm and fjnal let us know soon if you can’t make the midterm fjnal is a combined fjnal on 4 May at 7PM

not during a “normal” fjnal slot

6

slide-8
SLIDE 8

late policy

there is a late policy on the website

7

slide-9
SLIDE 9

textbook

recommended textbook: Anderson and Dahlin, Operating Systems: Principles and Practice no required textbook alt: Arpaci-Dusseau, Operating Systems: Three Easy Pieces (free PDFs!)

some topics we’ll cover where this may be primary textbook

alternative: Silberchartz (used in previous semesters)

full version: Operating System Concepts, Ninth Edition

8

slide-10
SLIDE 10

cheating: homeworks

don’t homeworks are individual no code from prior semesters (other than your own) no sharing code, pesudocode, detailed descriptions of code no using code from Internet/etc., with limited exceptions

tiny things solving problems that aren’t point of assignment …credited where used in your code e.g. code to split string into array for non-text-parsing assignment exception: something explicitly referred to by the assignent writeup in doubt: ask

9

slide-11
SLIDE 11

citation

if using small amount of code clearly not point of assignment

e.g. split string into array for non-text-parsing assignment e.g. fjlling arrays of pointers from vectors of strings

not sure what counts? ask then make sure you cite where you got it in your code

should not be other student, etc. — no sharing code

if using code clearly part of major objective of assignment then don’t

e.g. if you fjnd a shell online, don’t use it solve the shell assignment

10

slide-12
SLIDE 12

cheating: quizzes

don’t quizzes: also individual don’t share answers don’t IM people for answers don’t ask on StackOverfmow for answers

11

slide-13
SLIDE 13

waitlisted?

if you need this course now to graduate on time, email me with specifjcs

please indicate which sections you are able to attend

12

slide-14
SLIDE 14

getting help

Piazza TA and my offjce hours (will be posted soon) emailing me

13

slide-15
SLIDE 15

history: computer operator

via National Library of Medicine; computer operators operating an Honeywell 800

14

slide-16
SLIDE 16

OS defjnition ambiguity

difgerent exact defjntions ‘part of OS’ v. ‘just a program/library’

example: code to allow moving windows on the screen part of the OS? example: code to support printers is part of the OS?

we’ll not sweat the details — give general, common principles

15

slide-17
SLIDE 17

what is an operating system?

software that:

Anderson-Dahlin manages a computer’s resources Arpaci-Dusseau provides ‘virtual machine’: more convenient than real

machine

16

slide-18
SLIDE 18

OS roles

Anderson-Dahlin’s taxonomy of things OS’s do referee — resource sharing, protection, isolation illusionist — clean, easy abstractions glue — common services

storage, window systems, authorization, networking, …

17

slide-19
SLIDE 19

OS as abstraction layer

app 1 app 2 app 3

  • perating system

hardware

18

slide-20
SLIDE 20

the virtual machine interface

application

  • perating system

hardware virtual machine interface physical machine interface imitate physical interface

(of some real hardware)

system virtual machine

(VirtualBox, VMWare, Hyper-V, …)

chosen for convenience

(of applications)

process virtual machine

(typical operating systems)

19

slide-21
SLIDE 21

system virtual machines

run entire operating systems

for OS development, portability

interface ≈ hardware interface (but maybe not the real hardware)

aid reusing existing raw hardware-targeted code difgerent “application programmer”

20

slide-22
SLIDE 22

process virtual machine

process VM real hardware thread processors memory allocation page tables fjles devices … … (virtually) infjnite “threads” ( virtual CPus) no matter number of CPUs memory allocation functions no worries about organization of “real” memory fjles — open/read/write/close interface no details of hard drive operation

  • r keyboard operation or …

21

slide-23
SLIDE 23

process virtual machine

process VM real hardware thread processors memory allocation page tables fjles devices … … (virtually) infjnite “threads” (∼ virtual CPus) no matter number of CPUs memory allocation functions no worries about organization of “real” memory fjles — open/read/write/close interface no details of hard drive operation

  • r keyboard operation or …

21

slide-24
SLIDE 24

process virtual machine

process VM real hardware thread processors memory allocation page tables fjles devices … … (virtually) infjnite “threads” ( virtual CPus) no matter number of CPUs memory allocation functions no worries about organization of “real” memory fjles — open/read/write/close interface no details of hard drive operation

  • r keyboard operation or …

21

slide-25
SLIDE 25

process virtual machine

process VM real hardware thread processors memory allocation page tables fjles devices … … (virtually) infjnite “threads” ( virtual CPus) no matter number of CPUs memory allocation functions no worries about organization of “real” memory fjles — open/read/write/close interface no details of hard drive operation

  • r keyboard operation or …

21

slide-26
SLIDE 26

The Process

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

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

22

slide-27
SLIDE 27

the abstract virtual machine

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

23

slide-28
SLIDE 28

abstract VM: application view

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

the application’s “machine” is the operating system no hardware I/O details visible — future-proof more featureful interfaces than real hardware

24

slide-29
SLIDE 29

abstract VM: OS view

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

  • perating system’s job: translate one interface to another

25

slide-30
SLIDE 30

program → process → CPU and memory

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

application 1

process

app 1’s memory app 1’s registers application 2

process

26

slide-31
SLIDE 31

program → process → CPU and memory

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

application 1

process

app 1’s memory app 1’s registers application 2

process

26

slide-32
SLIDE 32

program → process → CPU and memory

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

application 1

process

app 1’s memory app 1’s registers application 2

process

26

slide-33
SLIDE 33

program → process → CPU and memory

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

application 1

process

app 1’s memory app 1’s registers application 2

process

26

slide-34
SLIDE 34

fjles → input/output

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

application 1 fjles

27

slide-35
SLIDE 35

security and protection

applications

OS’s interface

threads, address spaces, processes, fjles, sockets, …

  • perating system

hardware interface

interrupts, memory addresses, special registers, memory-mapped devices, I/O buses, …

hardware

CPU

memory keyboard/mouse monitor disks network …

application 1 s e g m e n t a t i

  • n

f a u l t

28

slide-36
SLIDE 36

goal: protection

run multiple applications, and … keep them from crashing the OS keep them from crashing each other (keep parts of OS from crashing other parts?)

29

slide-37
SLIDE 37

mechanism 1: dual-mode operation

processor has two modes: kernel (privileged) and user some operations require kernel mode OS controls what runs in kernel mode

30

slide-38
SLIDE 38

mechanism 2: address translation

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

31

slide-39
SLIDE 39

aside: alternate mechanisms

dual mode operation and address translation are common today …so we’ll talk about them a lot not the only ways to implement operating system features

(plausibly not even the most effjcient…)

32

slide-40
SLIDE 40

problem: OS needs to respond to events

keypress happens? program using CPU for too long? … hardware support for running OS: exception

need hardware support because CPU is running application instructions

33

slide-41
SLIDE 41

problem: OS needs to respond to events

keypress happens? program using CPU for too long? … hardware support for running OS: exception

need hardware support because CPU is running application instructions

33

slide-42
SLIDE 42

exceptions and dual-mode operation

rule: user code always runs in user mode rule: only OS code ever runs in kernel mode

  • n exception: changes from user mode to kernel mode

…and is only mechanism for doing so

how OS controls what runs in kernel mode

34

slide-43
SLIDE 43

exception terminology

CS 3330 terms: interrupt: triggered by external event

timer, keyboard, network, …

fault: triggered by program doing something “bad”

invalid memory access, divide-by-zero, …

traps: triggered by explicit program action

system calls

aborts: something in the hardware broke

35

slide-44
SLIDE 44

xv6 exception terms

everything is a called a trap

  • r sometimes an interrupt

no real distinction in name about kinds

36

slide-45
SLIDE 45

real world exception terms

it’s all over the place… context clues

37

slide-46
SLIDE 46

kernel services

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

38

slide-47
SLIDE 47

hardware mechanism: deliberate exceptions

some instructions exist to trigger exceptions still works like normal exception

starts executing OS-chosen handler …in kernel mode

allows program requests privilieged instructions

OS handler decides what program can request OS handler decides format of requests

39

slide-48
SLIDE 48

system call timeline (x86-64 Linux)

‘priviliged’ operations prohibited ‘priviliged’ operations allowed

(change memory layout, I/O, exceptions)

/* set arguments in registers */ mov $SYS_write, %rax mov $FILENO_stdout, %rsi mov $buffer, %rdi mov $BUFFER_LEN, %r8 /* trigger exception */ syscall // special instruction syscall_handler: /* ... save registers and actually do read and set return value ... */ /* go back to "user" code */ iret // special instruction // now use return value testq %rax, %rax ...

in user mode

(the standard library)

in kernel mode

(the “kernel”)

hardware knows to go here because of pointer set during boot

40

slide-49
SLIDE 49

system call timeline (x86-64 Linux)

‘priviliged’ operations prohibited ‘priviliged’ operations allowed

(change memory layout, I/O, exceptions)

/* set arguments in registers */ mov $SYS_write, %rax mov $FILENO_stdout, %rsi mov $buffer, %rdi mov $BUFFER_LEN, %r8 /* trigger exception */ syscall // special instruction syscall_handler: /* ... save registers and actually do read and set return value ... */ /* go back to "user" code */ iret // special instruction // now use return value testq %rax, %rax ...

in user mode

(the standard library)

in kernel mode

(the “kernel”)

hardware knows to go here because of pointer set during boot

40

slide-50
SLIDE 50

system call timeline (x86-64 Linux)

‘priviliged’ operations prohibited ‘priviliged’ operations allowed

(change memory layout, I/O, exceptions)

/* set arguments in registers */ mov $SYS_write, %rax mov $FILENO_stdout, %rsi mov $buffer, %rdi mov $BUFFER_LEN, %r8 /* trigger exception */ syscall // special instruction syscall_handler: /* ... save registers and actually do read and set return value ... */ /* go back to "user" code */ iret // special instruction // now use return value testq %rax, %rax ...

in user mode

(the standard library)

in kernel mode

(the “kernel”)

hardware knows to go here because of pointer set during boot

40

slide-51
SLIDE 51

system call timeline (x86-64 Linux)

‘priviliged’ operations prohibited ‘priviliged’ operations allowed

(change memory layout, I/O, exceptions)

/* set arguments in registers */ mov $SYS_write, %rax mov $FILENO_stdout, %rsi mov $buffer, %rdi mov $BUFFER_LEN, %r8 /* trigger exception */ syscall // special instruction syscall_handler: /* ... save registers and actually do read and set return value ... */ /* go back to "user" code */ iret // special instruction // now use return value testq %rax, %rax ...

in user mode

(the standard library)

in kernel mode

(the “kernel”)

hardware knows to go here because of pointer set during boot

40

slide-52
SLIDE 52

the classic Unix design

applications standard library functions / shell commands standard libraries and utility programs system call interface kernel hardware interface hardware

CPU scheduler fjlesystems networking virtual memory device drivers signals pipes swapping … libc (C standard library) the shell login login… memory management unit device controllers …

the OS? the OS?

41

slide-53
SLIDE 53

the classic Unix design

applications standard library functions / shell commands standard libraries and utility programs system call interface kernel hardware interface hardware

CPU scheduler fjlesystems networking virtual memory device drivers signals pipes swapping … libc (C standard library) the shell login login… memory management unit device controllers …

the OS? the OS?

41

slide-54
SLIDE 54

the classic Unix design

applications standard library functions / shell commands standard libraries and utility programs system call interface kernel hardware interface hardware

CPU scheduler fjlesystems networking virtual memory device drivers signals pipes swapping … libc (C standard library) the shell login login… memory management unit device controllers …

the OS? the OS?

41

slide-55
SLIDE 55

aside: is the OS the kernel?

OS = stufg that runs in kernel mode? OS = stufg that runs in kernel mode + libraries to use it? OS = stufg that runs in kernel mode + libraries + utility programs (e.g. shell, fjnder)? OS = everything that comes with machine? no consensus on where the line is each piece can be replaced separately…

42

slide-56
SLIDE 56

xv6

we will be using an teaching OS called “xv6” based on Sixth Edition Unix modifjed to be multicore and use 32-bit x86 (not PDP-11)

43

slide-57
SLIDE 57

xv6 setup/assignment

fjrst assignment — adding two simple xv6 system calls includes xv6 download instructions and link to xv6 book

44

slide-58
SLIDE 58

xv6 technical requirements

you will need a Linux environment

we will supply one (VM on website), or get your own (it’s probably possible to use OS X, but you need a cross-compiler and we don’t have instructions)

…with qemu installed

qemu (for us) = emulator for 32-bit x86 system Ubuntu/Debian package qemu-system-i386

45

slide-59
SLIDE 59

fjrst assignment

get compiled and xv6 working …toolkit uses an emulator

could run on real hardware or a standard VM, but a lot of details also, emulator lets you use GDB

46

slide-60
SLIDE 60

xv6: what’s included

Unix-like kernel

very small set of syscalls some less featureful (e.g. exit without exit status)

userspace library

very limited

userspace programs

command line, ls, mkdir, echo, cat, etc. some self-testing programs

47

slide-61
SLIDE 61

xv6: echo.c

#include "types.h" #include "stat.h" #include "user.h" int main(int argc, char *argv[]) { int i; for(i = 1; i < argc; i++) printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n"); exit(); }

48

slide-62
SLIDE 62

xv6: echo.c

#include "types.h" #include "stat.h" #include "user.h" int main(int argc, char *argv[]) { int i; for(i = 1; i < argc; i++) printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n"); exit(); }

48

slide-63
SLIDE 63

xv6: echo.c

#include "types.h" #include "stat.h" #include "user.h" int main(int argc, char *argv[]) { int i; for(i = 1; i < argc; i++) printf(1, "%s%s", argv[i], i+1 < argc ? " " : "\n"); exit(); }

48

slide-64
SLIDE 64

xv6 demo

49

slide-65
SLIDE 65

xv6 demo

50

slide-66
SLIDE 66

write syscall in xv6

user mode + stack kernel mode + stack user program syscall wrapper (int $64) function call: write() interrupt table

HW does lookup

trigger exception assembly func: vector64() HW switches stacks + calls C function: trap() asm saves regs

(struct trapframe)

C function: syscall() C function: sys_write()

read args from user stack using syscall # from eax

return from interrupt

HW switches stacks

return via trap()

53

slide-67
SLIDE 67

write syscall in xv6

user mode + stack kernel mode + stack user program syscall wrapper (int $64) function call: write() interrupt table

HW does lookup

trigger exception assembly func: vector64() HW switches stacks + calls C function: trap() asm saves regs

(struct trapframe)

C function: syscall() C function: sys_write()

read args from user stack using syscall # from eax

return from interrupt

HW switches stacks

return via trap()

53

slide-68
SLIDE 68

write syscall in xv6: user mode

... write(1, "Hello, World!\n", 14); ...

main.c

... #define SYS_write 16 ... #define T_SYSCALL 64 ...

syscall.h / traps.h

(partial, after macro replacement)

.globl write write: movl $SYS_write, %eax int $T_SYSCALL ret

usys.S interrupt — trigger an exception similar to a keypress parameter (64 in this case) — type of exception xv6 syscall calling convention: eax = syscall number

  • therwise: same as 32-bit x86 calling convention (arguments on stack)

55

slide-69
SLIDE 69

write syscall in xv6: user mode

... write(1, "Hello, World!\n", 14); ...

main.c

... #define SYS_write 16 ... #define T_SYSCALL 64 ...

syscall.h / traps.h

(partial, after macro replacement)

.globl write write: movl $SYS_write, %eax int $T_SYSCALL ret

usys.S interrupt — trigger an exception similar to a keypress parameter (64 in this case) — type of exception xv6 syscall calling convention: eax = syscall number

  • therwise: same as 32-bit x86 calling convention (arguments on stack)

55

slide-70
SLIDE 70

write syscall in xv6: user mode

... write(1, "Hello, World!\n", 14); ...

main.c

... #define SYS_write 16 ... #define T_SYSCALL 64 ...

syscall.h / traps.h

(partial, after macro replacement)

.globl write write: movl $SYS_write, %eax int $T_SYSCALL ret

usys.S interrupt — trigger an exception similar to a keypress parameter (64 in this case) — type of exception xv6 syscall calling convention: eax = syscall number

  • therwise: same as 32-bit x86 calling convention (arguments on stack)

55

slide-71
SLIDE 71

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-72
SLIDE 72

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-73
SLIDE 73

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-74
SLIDE 74

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-75
SLIDE 75

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-76
SLIDE 76

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-77
SLIDE 77

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

56

slide-78
SLIDE 78

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall con: makes writing system calls safely more complicated

(what if keypress handler runs during system call?)

pro: slow system calls don’t stop timers, keypresses, etc. from working non-system call exceptions: interrupts disabled vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

57

slide-79
SLIDE 79

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

58

slide-80
SLIDE 80

write syscall in xv6: the trap function

void trap(struct trapframe *tf) { if(tf−>trapno == T_SYSCALL){ if(myproc()−>killed) exit(); myproc()−>tf = tf; syscall(); if(myproc()−>killed) exit(); return; } ... }

trap.c struct trapframe — set by assembly interrupt type, application registers, … example: tf >eax = old value of eax myproc() — pseudo-global variable represents currently running process much more on this later in semester syscall() — actual implementations uses myproc()->tf to determine what operation to do for program

59

slide-81
SLIDE 81

write syscall in xv6: the trap function

void trap(struct trapframe *tf) { if(tf−>trapno == T_SYSCALL){ if(myproc()−>killed) exit(); myproc()−>tf = tf; syscall(); if(myproc()−>killed) exit(); return; } ... }

trap.c struct trapframe — set by assembly interrupt type, application registers, … example: tf−>eax = old value of eax myproc() — pseudo-global variable represents currently running process much more on this later in semester syscall() — actual implementations uses myproc()->tf to determine what operation to do for program

59

slide-82
SLIDE 82

write syscall in xv6: the trap function

void trap(struct trapframe *tf) { if(tf−>trapno == T_SYSCALL){ if(myproc()−>killed) exit(); myproc()−>tf = tf; syscall(); if(myproc()−>killed) exit(); return; } ... }

trap.c struct trapframe — set by assembly interrupt type, application registers, … example: tf >eax = old value of eax myproc() — pseudo-global variable represents currently running process much more on this later in semester syscall() — actual implementations uses myproc()->tf to determine what operation to do for program

59

slide-83
SLIDE 83

write syscall in xv6: the trap function

void trap(struct trapframe *tf) { if(tf−>trapno == T_SYSCALL){ if(myproc()−>killed) exit(); myproc()−>tf = tf; syscall(); if(myproc()−>killed) exit(); return; } ... }

trap.c struct trapframe — set by assembly interrupt type, application registers, … example: tf >eax = old value of eax myproc() — pseudo-global variable represents currently running process much more on this later in semester syscall() — actual implementations uses myproc()->tf to determine what operation to do for program

59

slide-84
SLIDE 84

write syscall in xv6: the syscall function

static int (*syscalls[])(void) = { ... [SYS_write] sys_write, ... }; ... void syscall(void) { ... num = curproc−>tf−>eax; if(num > 0 && num < NELEM(syscalls) && syscalls[num]) { curproc−>tf−>eax = syscalls[num](); } else { ...

syscall.c array of functions — one for syscall ‘[number] value’: syscalls[number] = value (if system call number in range) call sys_…function from table store result in user’s eax register result assigned to eax (assembly code this returns to copies tf >eax into %eax)

60

slide-85
SLIDE 85

write syscall in xv6: the syscall function

static int (*syscalls[])(void) = { ... [SYS_write] sys_write, ... }; ... void syscall(void) { ... num = curproc−>tf−>eax; if(num > 0 && num < NELEM(syscalls) && syscalls[num]) { curproc−>tf−>eax = syscalls[num](); } else { ...

syscall.c array of functions — one for syscall ‘[number] value’: syscalls[number] = value (if system call number in range) call sys_…function from table store result in user’s eax register result assigned to eax (assembly code this returns to copies tf >eax into %eax)

60

slide-86
SLIDE 86

write syscall in xv6: the syscall function

static int (*syscalls[])(void) = { ... [SYS_write] sys_write, ... }; ... void syscall(void) { ... num = curproc−>tf−>eax; if(num > 0 && num < NELEM(syscalls) && syscalls[num]) { curproc−>tf−>eax = syscalls[num](); } else { ...

syscall.c array of functions — one for syscall ‘[number] value’: syscalls[number] = value (if system call number in range) call sys_…function from table store result in user’s eax register result assigned to eax (assembly code this returns to copies tf >eax into %eax)

60

slide-87
SLIDE 87

write syscall in xv6: the syscall function

static int (*syscalls[])(void) = { ... [SYS_write] sys_write, ... }; ... void syscall(void) { ... num = curproc−>tf−>eax; if(num > 0 && num < NELEM(syscalls) && syscalls[num]) { curproc−>tf−>eax = syscalls[num](); } else { ...

syscall.c array of functions — one for syscall ‘[number] value’: syscalls[number] = value (if system call number in range) call sys_…function from table store result in user’s eax register result assigned to eax (assembly code this returns to copies tf−>eax into %eax)

60

slide-88
SLIDE 88

write syscall in xv6: sys_write

int sys_write(void) { struct file *f; int n; char *p; if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) return −1; return filewrite(f, p, n); }

sysfjle.c utility functions that read arguments from user’s stack returns -1 on error (e.g. stack pointer invalid) (more on this later)

(note: 32-bit x86 calling convention puts all args on stack)

actual internal function that implements writing to a fjle (the terminal counts as a fjle)

61

slide-89
SLIDE 89

write syscall in xv6: sys_write

int sys_write(void) { struct file *f; int n; char *p; if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) return −1; return filewrite(f, p, n); }

sysfjle.c utility functions that read arguments from user’s stack returns -1 on error (e.g. stack pointer invalid) (more on this later)

(note: 32-bit x86 calling convention puts all args on stack)

actual internal function that implements writing to a fjle (the terminal counts as a fjle)

61

slide-90
SLIDE 90

write syscall in xv6: sys_write

int sys_write(void) { struct file *f; int n; char *p; if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) return −1; return filewrite(f, p, n); }

sysfjle.c utility functions that read arguments from user’s stack returns -1 on error (e.g. stack pointer invalid) (more on this later)

(note: 32-bit x86 calling convention puts all args on stack)

actual internal function that implements writing to a fjle (the terminal counts as a fjle)

61

slide-91
SLIDE 91

write syscall in xv6: interrupt table setup

... lidt(idt, sizeof(idt)); ... SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); ...

trap.c (run on boot) lidt — function (in x86.h) wrapping lidt instruction sets the interrupt descriptor table to idt idt = array of pointers to handler functions for each exception type (plus a few bits of information about those handler functions) (from mmu.h):

// Set up a normal interrupt/trap gate descriptor. // - istrap: 1 for a trap gate, 0 for an interrupt gate. // interrupt gate clears FL_IF, trap gate leaves FL_IF alone // - sel: Code segment selector for interrupt/trap handler // - off: Offset in code segment for interrupt/trap handler // - dpl: Descriptor Privilege Level - // the privilege level required for software to invoke // this interrupt/trap gate explicitly using an int instruction. #define SETGATE(gate, istrap, sel, off, d) \

set the T_SYSCALL interrupt to be callable from user mode via int instruction

(otherwise: triggers fault like privileged instruction)

set it to use the kernel “code segment” meaning: run in kernel mode (yes, code segments specifjes more than that — nothing we care about) 1: do not disable interrupts during syscalls e.g. keypress/timer handling can interrupt slow syscall vectors[T_SYSCALL] — OS function for processor to run set to pointer to assembly function vector64 eventually calls C function trap trap returns to alltraps alltraps restores registers from tf, then returns to user-mode

vector64: pushl $0 pushl $64 jmp alltraps ...

vectors.S

hardware jumps here

alltraps: ... call trap ... iret

trapasm.S

void trap(struct trapframe *tf) { ...

trap.c

62

slide-92
SLIDE 92

write syscall in xv6

user mode + stack kernel mode + stack user program syscall wrapper (int $64) function call: write() interrupt table

HW does lookup

trigger exception assembly func: vector64() HW switches stacks + calls C function: trap() asm saves regs

(struct trapframe)

C function: syscall() C function: sys_write()

read args from user stack using syscall # from eax

return from interrupt

HW switches stacks

return via trap()

63

slide-93
SLIDE 93

write syscall in xv6

user mode + stack kernel mode + stack user program syscall wrapper (int $64) function call: write() interrupt table

HW does lookup

trigger exception assembly func: vector64() HW switches stacks + calls C function: trap() asm saves regs

(struct trapframe)

C function: syscall() C function: sys_write()

read args from user stack using syscall # from eax

return from interrupt

HW switches stacks

return via trap()

64

slide-94
SLIDE 94

xv6intro homework

get familiar with xv6 OS add a new system call: writecount() returns total number of times write call happened

65

slide-95
SLIDE 95

homework steps

system call implementation: sys_writecount

hint in writeup: imitate sys_uptime need a counter for number of writes

add writecount to several tables/lists

(list of handlers, list of library functions to create, etc.) recommendation: imitate how other system calls are listed

create a userspace program that calls writecount

recommendation: copy from given programs

repeat, adding setwritecount

66

slide-96
SLIDE 96

note on locks

some existing code uses acquire/release you do not have to do this

  • nly for multiprocessor support

67

slide-97
SLIDE 97

68

slide-98
SLIDE 98

backup slides

69

slide-99
SLIDE 99

time multiplexing really

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

= operating system exception happens return from exception

70

slide-100
SLIDE 100

time multiplexing really

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

= operating system exception happens return from exception

70

slide-101
SLIDE 101

OS and time multiplexing

starts running instead of normal program via exception saves old program counter, registers somewhere sets new registers, jumps to new program counter called context switch

saved information called context

71

slide-102
SLIDE 102

context

all registers values

%rax %rbx, …, %rsp, …

condition codes program counter address space = page table base pointer

72

slide-103
SLIDE 103

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

73

slide-104
SLIDE 104

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 xv6: A’s registers saved by exception handler into “trapframe”

  • n A’s kernel stack

74

slide-105
SLIDE 105

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 xv6: A’s registers saved by exception handler into “trapframe”

  • n A’s kernel stack

74

slide-106
SLIDE 106

common goal: hide complexity

hiding complexity competing applications — failures, malicious applications

text editor shouldn’t need to know if browser is running

varying hardware — diverse and changing interfaces

difgerent keyboard interfaces, disk interfaces, video interfaces, etc. applications shouldn’t change

75

slide-107
SLIDE 107

common goal: hide complexity

hiding complexity competing applications — failures, malicious applications

text editor shouldn’t need to know if browser is running

varying hardware — diverse and changing interfaces

difgerent keyboard interfaces, disk interfaces, video interfaces, etc. applications shouldn’t change

75

slide-108
SLIDE 108

common goal: for application programmer

write once for lots of hardware avoid reimplementing common functionality don’t worry about other programs

76