This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has...
Transcript of This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has...
![Page 1: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/1.jpg)
Syscalls
Indranil Sen Gupta (odd section)and Mainack Mondal (even section)CS39002
Spring 2019-20
![Page 2: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/2.jpg)
The story so far
• A brief historical overview of OS• Batch processing systems• Multiprogramming• Multitasking• Some practice problems
• Today’s OS (multitasking, like Unix)• Dual mode of operation• Uses of timer
![Page 3: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/3.jpg)
Today’s class
• System calls
• Some practice problems
• Start of processes
![Page 4: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/4.jpg)
System calls
![Page 5: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/5.jpg)
What are system calls?• The mechanism used by an application program to
request service from the operating system
• So how does it work?
![Page 6: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/6.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
![Page 7: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/7.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
• The handler routine was just an interrupt handler
![Page 8: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/8.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
• The handler routine was just an interrupt handler• Like interrupts, system calls are arranged in a table
![Page 9: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/9.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
• The handler routine was just an interrupt handler• Like interrupts, system calls are arranged in a table
• Whenever a syscall (interrupt driven) came
![Page 10: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/10.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
• The handler routine was just an interrupt handler• Like interrupts, system calls are arranged in a table
• Whenever a syscall (interrupt driven) came
• Kernel selected syscall placing index in eax register
![Page 11: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/11.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
• The handler routine was just an interrupt handler• Like interrupts, system calls are arranged in a table
• Whenever a syscall (interrupt driven) came
• Kernel selected syscall placing index in eax register • Arguments go in the other registers
![Page 12: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/12.jpg)
Earlier days: interrupt• Originally, system calls issued using “int” instruction
• Kernel creates an array of Interrupt descriptors in memory, called Interrupt Descriptor Table, or IDT
• The handler routine was just an interrupt handler• Like interrupts, system calls are arranged in a table
• Whenever a syscall (interrupt driven) came
• Kernel selected syscall placing index in eax register • Arguments go in the other registers• Return value goes in eax
![Page 13: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/13.jpg)
However it is slow
• Today, Processors are totally pipelined
• Pipeline stalls are very expensive
• Cache misses can cause pipeline stalls
• Now recall that IDT is in memory
• May not be in cache
• Makes it expensive
![Page 14: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/14.jpg)
Idea: new instruction
• What if we cache the IDT entry for a system call in a special CPU register?
• No more cache misses for the IDT!
• What is the cost?
![Page 15: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/15.jpg)
Idea: new instruction
• What if we cache the IDT entry for a system call in a special CPU register?
• No more cache misses for the IDT!
• What is the cost?
• system calls should be frequent enough to be worth the transistor budget
![Page 16: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/16.jpg)
How to leverage the new instruction?
• There is a machine instruction (new architectures)
• Essentially for asking your processor to perform task• Hardware specific• Can be called “syscall”, “trap”, “svc”, “swi”• For x86-64 architecture its called “syscall”
![Page 17: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/17.jpg)
Example: How are system calls used in kernel• syscall machine instruction takes operands
• syscall 10 // integer number for x86
// some of these are fixed by intel
![Page 18: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/18.jpg)
Example: How are system calls used in kernel• syscall machine instruction takes operands
• syscall 10 // integer number for x86 // some of these are fixed by intel
• When we are writing programs in HLL (higher level language, think C)
• We think of syscalls in a somewhat higher level context
![Page 19: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/19.jpg)
Example: How are system calls used in kernel• syscall machine instruction takes operands
• syscall 10 // integer number for x86 // some of these are fixed by intel
• When we are writing programs in HLL (higher level language, think C)
• We think of syscalls in a somewhat higher level context
![Page 20: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/20.jpg)
Characteristics of system calls
• Provide an interface to the services made available by an operating system
![Page 21: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/21.jpg)
Characteristics of system calls
• Provide an interface to the services made available by an operating system
• Typically executes hundreds of thousands time every second
![Page 22: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/22.jpg)
Characteristics of system calls
• Provide an interface to the services made available by an operating system
• Typically executes hundreds of thousands time every second
• User application programs do not see this level ofdetail
![Page 23: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/23.jpg)
Characteristics of system calls
• Provide an interface to the services made available by an operating system
• Typically executes hundreds of thousands time every second
• User application programs do not see this level ofdetail
• Use Application programming interface (API)
![Page 24: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/24.jpg)
Characteristics of system calls
• Provide an interface to the services made available by an operating system
• Typically executes hundreds of thousands time every second
• User application programs do not see this level ofdetail
• Use Application programming interface (API)
• fork, pipe, execvp etc.
![Page 25: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/25.jpg)
Characteristics of system calls
• Provide an interface to the services made available by an operating system
• Typically executes hundreds of thousands time every second
• User application programs do not see this level ofdetail
• Use Application programming interface (API)
• fork, pipe, execvp etc.
• These APIs are also loosely termed as system calls
![Page 26: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/26.jpg)
More about system calls
• System call numbers in linux for x86 - 64 : https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/arch/x86/entry/syscalls/syscall_64.tbl
• System call numbers in linux for x86 : https://github.com/torvalds/linux/blob/16f73eb02d7e1765ccab3d2018e0bd98eb93d973/arch/x86/entry/syscalls/syscall_32.tbl
• System call implementations in x86-64 and x86 https://stackoverflow.com/questions/15168822/intel-x86-vs-x64-system-call
![Page 27: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/27.jpg)
Example of a system call API
• There are no fopen, fgets, printf, and fclose system calls in the Linux kernel but open, read, write, and close
• http://man7.org/linux/man-pages/man2/read.2.html
![Page 28: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/28.jpg)
Example of a system call API
• http://man7.org/linux/man-pages/man2/read.2.html
• What are these function parameters?
• What is the return values?
![Page 29: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/29.jpg)
How is read invoked?
• Check the board
• Demo: checking what syscalls are invoked in a process
• Demo: checking assembly code in C (using gcc)
![Page 30: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/30.jpg)
Summary: The workflow
Functions we write in HLL
(printf, scanf)Lower level standard C library call
(read, write)
syscallinstruction
![Page 31: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/31.jpg)
Types of system calls (From silberschatz’s slides)
![Page 32: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/32.jpg)
Types of System Calls• Process control (e.g., fork(), exit(), wait() )
• create process, terminate process (fork, exit)
• end, abort
• load, execute
• get process attributes, set process attributes
• wait for time
• wait event, signal event
• allocate and free memory
• Dump memory if error
• Debugger for determining bugs, single step execution
• Locks for managing access to share data between processes
![Page 33: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/33.jpg)
Types of System Calls (Cont.)
• File management (e.g., open(), close(), read(), write())
• create file, delete file
• open, close file
• read, write, reposition
• get and set file attributes
• Device management (e.g., ioctl(), read(), write())
• request device, release device
• read, write, reposition
• get device attributes, set device attributes
• logically attach or detach devices
![Page 34: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/34.jpg)
Types of System Calls (Cont.)
• Inter-Process Communications (e.g., pipe(), semget(), semop(), shmget(), shmcat(), shmdt(), shmctl(), signal(), kill())
• create, delete communication connection
• send, receive messages if message passing model to host name or process name
• Shared-memory model create and gain access to memory regions
• transfer status information
• attach and detach remote devices
![Page 35: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/35.jpg)
Types of System Calls (Cont.)
• Protection (chmod(), chown(), umask())
• Control access to resources
• Get and set permissions
• Allow and deny user access
![Page 36: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/36.jpg)
Today’s class
• System calls
• Some practice problems
• Start of processes
![Page 37: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/37.jpg)
Announcement
• The first practice problems sheet is up: http://www.facweb.iitkgp.ac.in/~isg/OS/ASSIGN/Assignment-1.pdf
![Page 38: This file has been cleaned of potential threats. If you ...isg/OS/SLIDES/03.pdf · This file has been cleaned of potential threats. If you confirm that the file is coming from a trusted](https://reader034.fdocuments.in/reader034/viewer/2022042916/5f5672188d67a66a3119520a/html5/thumbnails/38.jpg)