You are on page 1of 2

CS 417 2/18/2014

System calls
• System calls are an operating system’s API
– The set of functions that the operating system exposes to processes
• If you want to the OS to do something, you tell it via a
Operating Systems system call
03r. The system call • Examples

Windows Linux
NtOpenFile open
NtReadFile read
Paul Krzyzanowski
NtCreateProcess fork
Rutgers University
NtGetCurrentProcessorNumber getpid
Spring 2014

See http://j00ru.vexillium.org/ntapi/ for a list of Windows system calls


See http://linux-documentation.com/en/man/man2/ for a list of Linux system calls

February 18, 2014 © 2014 Paul Krzyzanowski 1 February 18, 2014 © 2014 Paul Krzyzanowski 2

What are system calls used for? System calls are made via traps
• Anything to do with: • System calls request operating system services
– Accessing devices – Communicating with other processes • Operating system code executes with the processor running in kernel
– Accessing files – Stopping/starting processes (also known as supervisor or privileged) mode
– Requesting memory – Setting a timer
– Setting/changing access permissions – Privileged mode gives the CPU the rights to:
• Execute special instructions
• You need a system call to: (change interrupt masks, set hardware timers, halt the processor)
• Access specific registers (e.g., private stack pointer)
– Open a file
• Change the memory map
– Get data from the network
• Access regions of memory that have been restricted for kernel access only
– Kill a process • Access the processor’s I/O ports (if the architecture has them)
• You do not need a system call to: • A trap takes has one parameter: index into an Interrupt Vector Table
– Replace data in a string – The table is in memory that only the kernel can access
– Create an object (instance of a class) – All addresses in the table go to well-defined entry points in the OS
– Call a function

February 18, 2014 © 2014 Paul Krzyzanowski 3 February 18, 2014 © 2014 Paul Krzyzanowski 4

Variations on software interrupts Variations on software interrupts


• “Classic” software interrupt system call in Intel’s x86 architecture • Call gate (Intel x86 architecture)
– Use INT 80h instruction to invoke a system call – Operating system sets up a “call gate”
– The user program simply executes a “CALL FAR” (essentially a regular call
– On Intel architectures, if the privilege level changed, the processor would instruction) with a specific segment number
switch to a different stack – The CPU checks if the segment number is a valid “gate”
• For security: don’t leave secure crud on a stack that the user might inspect – If so, it loads the appropriate instruction pointer and elevates the privilege
• What happens: level
– Various registers were saved in temporary space in the processor (flags, instruction pointer, – Unique to Intel architecture – nobody else used memory segments
stack segment, etc.)
– The new stack pointer is loaded
– The saved registers are pushed on the stack
– Any error code indicating the nature of the trap is pushed on the stack
– Flags are adjusted
– Execution continues

February 18, 2014 © 2014 Paul Krzyzanowski 5 February 18, 2014 © 2014 Paul Krzyzanowski 6

© 2014 Paul Krzyzanowski 1


CS 417 2/18/2014

Variations on software interrupts System calls have parameters


• SYSCALL/SYSRET (Intel) or SYSENTER/SYSEXIT (AMD) • A software interrupt (trap) has one parameter: the trap #
instructions
• System calls need to pass multiple parameters
– Faster mechanism than interrupts or call gates
– E.g., read needs to identify the open file, starting byte, number of bytes
– Target address is in a CPU register – no need for a memory lookup
– There are three ways to pass these parameters
1. In the processor’s registers
2. On the stack
• Linux does a test to check which mechanisms exist before making a 3. In some memory location whose address is passed to the kernel
system call:
– Check if syscall exists (Intel architecture) • There are more system calls than interrupt vectors
– Check if sysenter exists (AMD architecture) – Each system call is not a unique interrupt vector
– Else use INT 80 (works on even the oldest processors) – Use one vector & have the system call number be a parameter
– The operating system can jump to the right place based on sys call #
• Dispatch table
• No matter what is used, the effect is the same:
– Branch to a well-known location & run in privileged mode

February 18, 2014 © 2014 Paul Krzyzanowski 7 February 18, 2014 © 2014 Paul Krzyzanowski 8

Disguising system calls What happens in a system call?


1. User calls a system call library 3. The operating system kernel code
• System calls are made to look like function calls
function (e.g., open) is now run
• As a programmer, you do not want to – Compiler generates code to push – Save registers
parameters on the stack & call the – Look up the address of system call
– copy parameters into some special place
function #5
– know the system call number
2. The library function is run – Call the system call handler, which
– invoke a software interrupt processes the request
– Compiler generates code to save
– figure out how to copy any return data back registers – Return the result of the system call in
the %eax register
• System call library – System call number for the open
system call (5) is placed in register – Restore other registers
– A user-level library that is linked with your program %eax – Return from interrupt
– Provides a functional interface to system calls – Other parameters go in registers 4. Back to the library function
– Handles the work of passing parameters and getting results %ebx, %ecx, and %edx
– Copy results (if necessary)
– Trap to the OS
– Restore registers (except for return)
– Return value to the caller
Note: This is an example using Linux and an x86 architecture. x86-64 uses the 64-bit
version of the eax register: rax. Other processors will use totally different registers.
Other operating systems may use a different entry point.

February 18, 2014 © 2014 Paul Krzyzanowski 9 February 18, 2014 © 2014 Paul Krzyzanowski 10

The End

February 18, 2014 © 2014 Paul Krzyzanowski 11

© 2014 Paul Krzyzanowski 2

You might also like