lect03 system calls

19
OPERATING SYSTEMS (CS C372 & IS C362) (CS C372 & IS C362) LECTURE 3: SYSTEM CALLS

Upload: sourav-sen

Post on 19-Jan-2016

12 views

Category:

Documents


0 download

DESCRIPTION

System calls

TRANSCRIPT

OPERATING SYSTEMS

(CS C372 & IS C362)(CS C372 & IS C362)

LECTURE 3: SYSTEM CALLS

General-System ArchitectureWh e need protection?Why we need protection?

In single task / programming environment To protect OS from incorrect programTo protect OS from incorrect program

In multiprogramming / multitasking environment To protect OS and other programs from incorrect programTo protect OS and other programs from incorrect program

Given the I/O instructions are privileged howGiven the I/O instructions are privileged, how does the user program perform I/O?

5 August 2010 Biju K Raveendran@BITS Pilani. 2

Use of A System Call to Perform I/O

5 August 2010 Biju K Raveendran@BITS Pilani. 3

How to communicate ?How to communicate between two modes?

System calls have always been the means through hi h k l iwhich user space programs can access kernel services.

(Typically written in a high-level language (C or C++)System call is the only legal entry point to the kernel.System call provides an abstract hardware interface for user space

Application need not worry about type of disk, media and fileApplication need not worry about type of disk, media and file system in use

System call ensures system stability and security.Kernel can keep track of application’s activityKernel can keep track of application s activity

5 August 2010 4Biju K Raveendran@BITS Pilani.

System CallsSystem call – the method used by a process to request action by the operating system.q y p g y

Usually takes the form of a trap to a specific location in the interrupt vector.Control passes through the interrupt vector to a service routine in the OS, and the mode bit is set to monitor modeto monitor mode. The monitor mode verifies that the parameters are correct and legal executes the request andcorrect and legal, executes the request, and returns control to the instruction following the system call.

5 August 2010 Biju K Raveendran@BITS Pilani. 5

5 August 2010 6Biju K Raveendran@BITS Pilani.

System Call

5 August 2010 7Biju K Raveendran@BITS Pilani.

System Calls

5 August 2010 Biju K Raveendran@BITS Pilani. 8

System callsSystem calls provide the interface between a running program and the operating system.It is the mechanism used by an application program toIt is the mechanism used by an application program to request service from the operating system.Often use a special machine code instruction (i.e. software interrupt) which causes the processor tosoftware interrupt) which causes the processor to transfer control to kernel code (change mode from “user mode” to "supervisor mode"). The process fills the registers with the appropriate valuesThe process fills the registers with the appropriate values and calls a special instruction which jumps to a previously defined location in the kernel

The destination location (in kernel) is readable by userThe destination location (in kernel) is readable by user processes, it is not writable by them.Under Intel CPUs, this is done by means of interrupt 0 800x80.

5 August 2010 9Biju K Raveendran@BITS Pilani.

System Call0x80 triggers a switch to kernel mode and the0x80 triggers a switch to kernel mode and the execution of exception vector 128 (system call handler)The location in the kernel a process can jump to isThe location in the kernel a process can jump to is System call handler function called ENTRY(system_call).

ENTRY(system call) is architecture dependent & is ( y _ ) p &available in

/usr/src/kernels/linux-2.6.34/arch/x86/kernel/entry_32.SENTRY(system call) ( y _ )

Reads the value of EAXSaves all the registersChecks the validity of the given system call number byChecks the validity of the given system call number by comparing it to NR_syscalls.If could not find, the function returns –ENOSYS.

5 August 2010 Biju K Raveendran@BITS Pilani. 10

Else dispatch execution to the proper kernel function by System Call

p p p ycall *sys_call_table(,%eax,4) / call *sys_call_table(,%rax,8)

Each system call table entry is 32 / 64 bitsy yKernel multiplies the given system call number by 4 to arrive at its location in system call table (syscall_table_32.S)

syscall_table_32.S/usr/src/kernels/linux-2.6.34/arch/x86/kernel/syscall_table_32.S

( ) fENTRY(sys_call_table) is checked to see the address of the kernel function to call.

sys ni syscall() is not implemented system callsys_ni_syscall() is not implemented system callReturns –ENOSYS (invalid system call)

Jump to the specified Kernel functionp p

5 August 2010 11Biju K Raveendran@BITS Pilani.

System CallAfter the completion of Kernel function the control returns to ENTRY(system_call). This kernel function callskernel function calls

syscall_exitsyscall_exit_worksyscall_trace_leaveIt does a few system checks and then return back to the process in user space (or to a different p p (process, if the process time ran out).

5 August 2010 Biju K Raveendran@BITS Pilani. 12

System CallMost of the system calls require one or more parameter to be passed to them

Parameters are stored in registers EBX, ECX, EDX, ESI and EDI (if the parameters are less than i )six)

If number of parameters are more than five (very rare) a single register is used to hold a pointer torare) a single register is used to hold a pointer to user space where all parameters exists.

5 August 2010 13Biju K Raveendran@BITS Pilani.

System Call Parameter PassingThree general methods used to pass parametersThree general methods used to pass parameters to the OS

Simplest: pass the parameters in registersp p p gIn some cases, may be more parameters than registers

Parameters stored in a block or table in memoryParameters stored in a block, or table, in memory, and address of block passed as a parameter in a register

This approach is taken by Linux and SolarisParameters placed, or pushed, onto the stack by the program and popped off the stack by the operatingprogram and popped off the stack by the operating systemBlock and stack methods do not limit the number or length of parameters being passed

5 August 2010 14Biju K Raveendran@BITS Pilani.

Parameter Passing via Table

5 August 2010 15Biju K Raveendran@BITS Pilani.

fscanf system call execution

Call to read()

read() in C program read() in the C library () p g () y

read system call

5 August 2010 16Biju K Raveendran@BITS Pilani.

Invoking system call handler & executing system call

call read() read() wrapper system call() sys read()() () pp y _ () y _ ()

Application C libraryread() wrapper

User Space Kernel Space

System call handler sys_read()

User Space Kernel Space

5 August 2010 17Biju K Raveendran@BITS Pilani.

5 August 2010 18Biju K Raveendran@BITS Pilani.

Each arrow in the figure represents a jump inEach arrow in the figure represents a jump in CPU instruction flow, and each jump may require flushing the prefetch queue and q g p qpossibly a ``cache miss'' event. Transitions between user and kernel spaceTransitions between user and kernel space are especially important(Context switch), as they are the most expensive in processing y p p gtime and prefetch behavior.

5 August 2010 19Biju K Raveendran@BITS Pilani.