aos lab 5: system calls

67
Lab 5: System calls Advanced Operating Systems Zubair Nabi [email protected] February 27, 2013

Upload: zubair-nabi

Post on 18-Nov-2014

340 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: AOS Lab 5: System calls

Lab 5: System callsAdvanced Operating Systems

Zubair Nabi

[email protected]

February 27, 2013

Page 2: AOS Lab 5: System calls

Normal execution of a processor

While running a user process:

1 Read an instruction

2 Advance the program counter

3 Execute the instruction

4 Jump to 1

Page 3: AOS Lab 5: System calls

Normal execution of a processor

While running a user process:

1 Read an instruction

2 Advance the program counter

3 Execute the instruction

4 Jump to 1

Page 4: AOS Lab 5: System calls

Normal execution of a processor

While running a user process:

1 Read an instruction

2 Advance the program counter

3 Execute the instruction

4 Jump to 1

Page 5: AOS Lab 5: System calls

Normal execution of a processor

While running a user process:

1 Read an instruction

2 Advance the program counter

3 Execute the instruction

4 Jump to 1

Page 6: AOS Lab 5: System calls

Normal execution of a processor

While running a user process:

1 Read an instruction

2 Advance the program counter

3 Execute the instruction

4 Jump to 1

Page 7: AOS Lab 5: System calls

Extraordinary events

Events that break normal processor flow, to return control to thekernel:

1 A device signals that it needs attention (e.g. Timer): Interrupt

2 A user program does something illegal (e.g. divide by zero):Exception

3 A user program asks the kernel for a service: System call

Page 8: AOS Lab 5: System calls

Extraordinary events

Events that break normal processor flow, to return control to thekernel:

1 A device signals that it needs attention (e.g. Timer): Interrupt

2 A user program does something illegal (e.g. divide by zero):Exception

3 A user program asks the kernel for a service: System call

Page 9: AOS Lab 5: System calls

Extraordinary events

Events that break normal processor flow, to return control to thekernel:

1 A device signals that it needs attention (e.g. Timer): Interrupt

2 A user program does something illegal (e.g. divide by zero):Exception

3 A user program asks the kernel for a service: System call

Page 10: AOS Lab 5: System calls

Extraordinary events

Events that break normal processor flow, to return control to thekernel:

1 A device signals that it needs attention (e.g. Timer): Interrupt

2 A user program does something illegal (e.g. divide by zero):Exception

3 A user program asks the kernel for a service: System call

Page 11: AOS Lab 5: System calls

Handling extraordinary events

The operating system must:

1 Save the processor’s registers for future resumption

2 Set up system for execution in the kernel

3 Choose a place for the kernel to start execution

4 Retrieve information about the event and call correspondinginterrupt handler

5 All the while, maintain isolation between user processes and thekernel

Page 12: AOS Lab 5: System calls

Handling extraordinary events

The operating system must:

1 Save the processor’s registers for future resumption

2 Set up system for execution in the kernel

3 Choose a place for the kernel to start execution

4 Retrieve information about the event and call correspondinginterrupt handler

5 All the while, maintain isolation between user processes and thekernel

Page 13: AOS Lab 5: System calls

Handling extraordinary events

The operating system must:

1 Save the processor’s registers for future resumption

2 Set up system for execution in the kernel

3 Choose a place for the kernel to start execution

4 Retrieve information about the event and call correspondinginterrupt handler

5 All the while, maintain isolation between user processes and thekernel

Page 14: AOS Lab 5: System calls

Handling extraordinary events

The operating system must:

1 Save the processor’s registers for future resumption

2 Set up system for execution in the kernel

3 Choose a place for the kernel to start execution

4 Retrieve information about the event and call correspondinginterrupt handler

5 All the while, maintain isolation between user processes and thekernel

Page 15: AOS Lab 5: System calls

Handling extraordinary events

The operating system must:

1 Save the processor’s registers for future resumption

2 Set up system for execution in the kernel

3 Choose a place for the kernel to start execution

4 Retrieve information about the event and call correspondinginterrupt handler

5 All the while, maintain isolation between user processes and thekernel

Page 16: AOS Lab 5: System calls

Handling extraordinary events

The operating system must:

1 Save the processor’s registers for future resumption

2 Set up system for execution in the kernel

3 Choose a place for the kernel to start execution

4 Retrieve information about the event and call correspondinginterrupt handler

5 All the while, maintain isolation between user processes and thekernel

Page 17: AOS Lab 5: System calls

Handling extraordinary events (2)

• Need hardware support

• On the x86, system calls generate an interrupt via the intinstruction

• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions

• Traps are caused by the current running process• Interrupts are caused by devices

• Can happen concurrently

Page 18: AOS Lab 5: System calls

Handling extraordinary events (2)

• Need hardware support

• On the x86, system calls generate an interrupt via the intinstruction

• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions

• Traps are caused by the current running process• Interrupts are caused by devices

• Can happen concurrently

Page 19: AOS Lab 5: System calls

Handling extraordinary events (2)

• Need hardware support

• On the x86, system calls generate an interrupt via the intinstruction

• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions

• Traps are caused by the current running process• Interrupts are caused by devices

• Can happen concurrently

Page 20: AOS Lab 5: System calls

Handling extraordinary events (2)

• Need hardware support

• On the x86, system calls generate an interrupt via the intinstruction

• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions

• Traps are caused by the current running process• Interrupts are caused by devices

• Can happen concurrently

Page 21: AOS Lab 5: System calls

Handling extraordinary events (2)

• Need hardware support

• On the x86, system calls generate an interrupt via the intinstruction

• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions

• Traps are caused by the current running process• Interrupts are caused by devices

• Can happen concurrently

Page 22: AOS Lab 5: System calls

Handling extraordinary events (2)

• Need hardware support

• On the x86, system calls generate an interrupt via the intinstruction

• The same mechanism for handling interrupts is used for handlingsystem calls and exceptions

• Traps are caused by the current running process• Interrupts are caused by devices

• Can happen concurrently

Page 23: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 24: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 25: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 26: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 27: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 28: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 29: AOS Lab 5: System calls

x86 protection

• 4 protection levels: 0 to 3• Privilege decreases in ascending order

• Most operating systems only use 2 levels: 0 (kernel mode) and 3(user mode)

• Interrupt handlers are defined in an interrupt descriptor table(idt) with a total of 256 entries

• int n generates a system call, where n is used to index theidt

• It is the job of the operating system to implement a handler foreach entry in the idt

Page 30: AOS Lab 5: System calls

int

• Fetches the nth descriptor from idt

• Ensures that the privilege level in the descriptor is 3

• As the interrupt is due to a user process, saves the process state(change in privilege level)

• Sets the instructor pointer to the required starting address in thedescriptor table and starts execution

• After execution, the OS calls iret to resume previous executionstate

Page 31: AOS Lab 5: System calls

int

• Fetches the nth descriptor from idt

• Ensures that the privilege level in the descriptor is 3

• As the interrupt is due to a user process, saves the process state(change in privilege level)

• Sets the instructor pointer to the required starting address in thedescriptor table and starts execution

• After execution, the OS calls iret to resume previous executionstate

Page 32: AOS Lab 5: System calls

int

• Fetches the nth descriptor from idt

• Ensures that the privilege level in the descriptor is 3

• As the interrupt is due to a user process, saves the process state(change in privilege level)

• Sets the instructor pointer to the required starting address in thedescriptor table and starts execution

• After execution, the OS calls iret to resume previous executionstate

Page 33: AOS Lab 5: System calls

int

• Fetches the nth descriptor from idt

• Ensures that the privilege level in the descriptor is 3

• As the interrupt is due to a user process, saves the process state(change in privilege level)

• Sets the instructor pointer to the required starting address in thedescriptor table and starts execution

• After execution, the OS calls iret to resume previous executionstate

Page 34: AOS Lab 5: System calls

int

• Fetches the nth descriptor from idt

• Ensures that the privilege level in the descriptor is 3

• As the interrupt is due to a user process, saves the process state(change in privilege level)

• Sets the instructor pointer to the required starting address in thedescriptor table and starts execution

• After execution, the OS calls iret to resume previous executionstate

Page 35: AOS Lab 5: System calls

The first system call

• Recall that initcode.S started off by invoking an execsystem call to load the init process (/init)

• Flow:1 Push the arguments and binary for exec on the process’s stack2 Put the system call number (SYS_exec) in %eax (obviously this

number should already be present in the system call table,*syscalls[])

3 Execute int T_SYSCALL

Page 36: AOS Lab 5: System calls

The first system call

• Recall that initcode.S started off by invoking an execsystem call to load the init process (/init)

• Flow:1 Push the arguments and binary for exec on the process’s stack2 Put the system call number (SYS_exec) in %eax (obviously this

number should already be present in the system call table,*syscalls[])

3 Execute int T_SYSCALL

Page 37: AOS Lab 5: System calls

The first system call

• Recall that initcode.S started off by invoking an execsystem call to load the init process (/init)

• Flow:1 Push the arguments and binary for exec on the process’s stack2 Put the system call number (SYS_exec) in %eax (obviously this

number should already be present in the system call table,*syscalls[])

3 Execute int T_SYSCALL

Page 38: AOS Lab 5: System calls

The first system call

• Recall that initcode.S started off by invoking an execsystem call to load the init process (/init)

• Flow:1 Push the arguments and binary for exec on the process’s stack2 Put the system call number (SYS_exec) in %eax (obviously this

number should already be present in the system call table,*syscalls[])

3 Execute int T_SYSCALL

Page 39: AOS Lab 5: System calls

The first system call

• Recall that initcode.S started off by invoking an execsystem call to load the init process (/init)

• Flow:1 Push the arguments and binary for exec on the process’s stack2 Put the system call number (SYS_exec) in %eax (obviously this

number should already be present in the system call table,*syscalls[])

3 Execute int T_SYSCALL

Page 40: AOS Lab 5: System calls

x86 traps

• 256 different interrupts• 0-31 software• 32-63 hardware• 64 system calls (T_SYSCALL)

Page 41: AOS Lab 5: System calls

x86 traps

• 256 different interrupts• 0-31 software• 32-63 hardware• 64 system calls (T_SYSCALL)

Page 42: AOS Lab 5: System calls

x86 traps

• 256 different interrupts• 0-31 software• 32-63 hardware• 64 system calls (T_SYSCALL)

Page 43: AOS Lab 5: System calls

x86 traps

• 256 different interrupts• 0-31 software• 32-63 hardware• 64 system calls (T_SYSCALL)

Page 44: AOS Lab 5: System calls

Setting up idt

• tvinit, called from main, sets up the entries in idt

• The address of the handler for each interrupt in idt is present invectors[], i.e. Interrupt i is handled by vectors[i]

• T_SYSCALL is handled specially: it is distinguished as a trapallowing multiple system calls to execute simultaneously

Page 45: AOS Lab 5: System calls

Setting up idt

• tvinit, called from main, sets up the entries in idt

• The address of the handler for each interrupt in idt is present invectors[], i.e. Interrupt i is handled by vectors[i]

• T_SYSCALL is handled specially: it is distinguished as a trapallowing multiple system calls to execute simultaneously

Page 46: AOS Lab 5: System calls

Setting up idt

• tvinit, called from main, sets up the entries in idt

• The address of the handler for each interrupt in idt is present invectors[], i.e. Interrupt i is handled by vectors[i]

• T_SYSCALL is handled specially: it is distinguished as a trapallowing multiple system calls to execute simultaneously

Page 47: AOS Lab 5: System calls

tvinit

1 void tvinit(void) {

2 int i;

3 for(i = 0; i < 256; i++)

4 SETGATE(idt[i], 0, SEG_KCODE <<3, vectors[i], 0);

5 SETGATE(idt[T_SYSCALL], 1, SEG_KCODE <<3,

6 vectors[T_SYSCALL], DPL_USER);

7 /∗ SETGATE(gate, istrap, sel, off, d)8 ∗ gate: idt entry9 ∗ istrap: 1 trap, 0 interrupt

10 ∗ sel: code segment11 ∗ off: offset within code segment12 ∗ d: privilege level ∗/13 initlock(&tickslock , "time");

14 }

Page 48: AOS Lab 5: System calls

Addresses of interrupt handlers

• xv6 uses a custom Perl script vectors.pl to generatevectors.S which holds entry points of interrupt handlers invectors[]

• Each entry point:1 Pushes an error code2 Pushes the interrupt number3 Jumps to alltraps

Page 49: AOS Lab 5: System calls

Addresses of interrupt handlers

• xv6 uses a custom Perl script vectors.pl to generatevectors.S which holds entry points of interrupt handlers invectors[]

• Each entry point:1 Pushes an error code2 Pushes the interrupt number3 Jumps to alltraps

Page 50: AOS Lab 5: System calls

Addresses of interrupt handlers

• xv6 uses a custom Perl script vectors.pl to generatevectors.S which holds entry points of interrupt handlers invectors[]

• Each entry point:1 Pushes an error code2 Pushes the interrupt number3 Jumps to alltraps

Page 51: AOS Lab 5: System calls

Addresses of interrupt handlers

• xv6 uses a custom Perl script vectors.pl to generatevectors.S which holds entry points of interrupt handlers invectors[]

• Each entry point:1 Pushes an error code2 Pushes the interrupt number3 Jumps to alltraps

Page 52: AOS Lab 5: System calls

alltraps

1 Pushes all processor registers into a struct trapframe• Once the call completes, the kernel can restore state from this

structure

2 Sets up the processor to run kernel C code (load SEG_KCPU,per-CPU data segment)

3 Calls the C trap handler trap

4 Once trap returns, alltraps restores structtrapframe and then calls iret to return control back to userspace

Page 53: AOS Lab 5: System calls

alltraps

1 Pushes all processor registers into a struct trapframe• Once the call completes, the kernel can restore state from this

structure

2 Sets up the processor to run kernel C code (load SEG_KCPU,per-CPU data segment)

3 Calls the C trap handler trap

4 Once trap returns, alltraps restores structtrapframe and then calls iret to return control back to userspace

Page 54: AOS Lab 5: System calls

alltraps

1 Pushes all processor registers into a struct trapframe• Once the call completes, the kernel can restore state from this

structure

2 Sets up the processor to run kernel C code (load SEG_KCPU,per-CPU data segment)

3 Calls the C trap handler trap

4 Once trap returns, alltraps restores structtrapframe and then calls iret to return control back to userspace

Page 55: AOS Lab 5: System calls

alltraps

1 Pushes all processor registers into a struct trapframe• Once the call completes, the kernel can restore state from this

structure

2 Sets up the processor to run kernel C code (load SEG_KCPU,per-CPU data segment)

3 Calls the C trap handler trap

4 Once trap returns, alltraps restores structtrapframe and then calls iret to return control back to userspace

Page 56: AOS Lab 5: System calls

alltraps

1 Pushes all processor registers into a struct trapframe• Once the call completes, the kernel can restore state from this

structure

2 Sets up the processor to run kernel C code (load SEG_KCPU,per-CPU data segment)

3 Calls the C trap handler trap

4 Once trap returns, alltraps restores structtrapframe and then calls iret to return control back to userspace

Page 57: AOS Lab 5: System calls

trap

• Gets passed struct trapframe *tf

• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception

• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process

Page 58: AOS Lab 5: System calls

trap

• Gets passed struct trapframe *tf

• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception

• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process

Page 59: AOS Lab 5: System calls

trap

• Gets passed struct trapframe *tf

• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception

• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process

Page 60: AOS Lab 5: System calls

trap

• Gets passed struct trapframe *tf

• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception

• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process

Page 61: AOS Lab 5: System calls

trap

• Gets passed struct trapframe *tf

• Checks tf->trapno to decide if it was called for a system call(T_SYSCALL) or a hardware interrupt or an exception

• In case of:1 System call, it invokes syscall2 Hardware interrupt, it calls the hardware interrupt controller3 Exception, it prints the details and kills the user process

Page 62: AOS Lab 5: System calls

syscall

• Loads the system call number through proc->tf->eax

• Calls the corresponding system call from the syscalls table• Puts its return value in proc->tf->eax (available on return to

user space)• Conventionally negative numbers indicate errors while positive

ones indicate success

• System call arguments are retrieved using either argint,argptr, or argstr

Page 63: AOS Lab 5: System calls

syscall

• Loads the system call number through proc->tf->eax

• Calls the corresponding system call from the syscalls table• Puts its return value in proc->tf->eax (available on return to

user space)• Conventionally negative numbers indicate errors while positive

ones indicate success

• System call arguments are retrieved using either argint,argptr, or argstr

Page 64: AOS Lab 5: System calls

syscall

• Loads the system call number through proc->tf->eax

• Calls the corresponding system call from the syscalls table• Puts its return value in proc->tf->eax (available on return to

user space)• Conventionally negative numbers indicate errors while positive

ones indicate success

• System call arguments are retrieved using either argint,argptr, or argstr

Page 65: AOS Lab 5: System calls

syscall

• Loads the system call number through proc->tf->eax

• Calls the corresponding system call from the syscalls table• Puts its return value in proc->tf->eax (available on return to

user space)• Conventionally negative numbers indicate errors while positive

ones indicate success

• System call arguments are retrieved using either argint,argptr, or argstr

Page 66: AOS Lab 5: System calls

syscall

• Loads the system call number through proc->tf->eax

• Calls the corresponding system call from the syscalls table• Puts its return value in proc->tf->eax (available on return to

user space)• Conventionally negative numbers indicate errors while positive

ones indicate success

• System call arguments are retrieved using either argint,argptr, or argstr

Page 67: AOS Lab 5: System calls

Reading(s)

• Chapter 3, “Traps, interrupts, and drivers”, till section “Code:System calls" from “xv6: a simple, Unix-like teaching operatingsystem”