Linux Device Drivers

Linux Device Drivers


Linux
Modules
Character drivers
IO & Memory
Linux Kernel
Process Management
Process Address space

Linux Scheduler
Memory Management
Interrupts
Signals
System Calls
Kernel Synchronization
Linux Inter Process Communications




Serial Ports
Parallel Ports
Introduction to Hardware
Linux Timers
DMA in Linux
Linux Threads
Linux Thread Synchronization

Linux Multi Threading
Debugging in Linux
GDB GNU Debugger
KDB Kernel Debugger
KGDB Kernel GNU Debugger
Example Ethernet Driver




System calls & C library

System calls & C library

 

A library function is an ordinary function that resides in a library external to your program. For example, getopt_long and mkstemp are functions provided in the C library.

A call to a library function is just like any other function call. The arguments are placed in processor registers or onto the stack, and execution is transferred to the start of the functions code, which typically resides in a loaded shared library.

 

A system call is implemented in the Linux kernel. When a program makes a system call, the arguments are packaged up and handed to the kernel, which takes over execution of the program until the call completes. A system call isnt an ordinary function call, and a special procedure is required to transfer control to the kernel.

However, the C library (the implementation of the standard C library provided with Unix systems) wraps system calls with functions so that you can call them easily.

Low-level I/O functions such as open and read are examples of system calls on Linux.

 

 

 

The set of Linux system calls forms the most basic interface between programs and the kernel. Each call presents a basic operation or capability.

Some system calls are very powerful and can exert great influence on the system. For instance, some system calls enable you to shut down the Unix system or to allocate system resources and prevent other users from accessing them. These calls have the restriction that only processes running with superuser privilege (programs run by

the root account) can invoke them. These calls fail if invoked by a non-super user process.

 

Note that a library function may invoke one or more other library functions or system calls as part of its implementation.

Linux currently provides about 200 different system calls. Most of these system calls are declared in <unistd.h>.

 

System calls & C library

 

     Limited number of entry points to obtain services of kernel

     System call transition from user mode to system mode

     Interrupt 0x80, with the actual register values

     Conversion from function to system call in C library (lib/libc.a)

     Kernel function in 6 groups: Process, File, IPC, memory, initialization and rest

 

 

The system_call() function

 

It implements the system call handler. It starts by saving the system call number and all the CPU registers that may be used by exception handler on the stack, except eflags, cs, eip, ss and esp.

 

A validity check is performed on the system call number passed by the user mode process.

 

The specific service routine associated with the system call number contained in eaxis invoked.

 

When the process resumes its execution to user mode, eax contains the return code.

 

Parameter Passing

 

Like ordinary functions, system calls require, some input output parameters. On entry each system call has atleast one parameter: system call number. System call parameters are passed to the system call handler in the CPU registers. Two conditions:

 

1.     The length of each parameter cannot exceed the length of a register (32bits).

2.     The number of parameters must not exceed six, since x86 cpu has limited number of registers. (eax, ebx, ecx, edx, esiand edi)

 

System call Implementation

 

Adding a new system call in Linux is relatively easy. The hard work lies in designing and implementing the system call, registering with the kernel is simple.

The first step in implementing a system call is defining its purpose.

The syscall should have exactly one purpose.

What are the new system calls arguments, return values and error codes?

 

Adding a new system call

 

1. Add following line

.long sys_foo

in arch/i386/kernel/syscall-table.S

 

2. Add file sysfoo.c

in kernel/sys.c

 

3. Add following line

#define __NR_foo318

in include/asm/unistd.h

and change

#define NR_syscalls319

Build and deploy the kernel.

 

4. Compile and execute foo.cto test the added system call.