Monday, August 23, 2010

Linux Device Driver One Liners

Types of Driver Class.

Char Driver : Permits sequential Read/Write only.
Block Driver : Permits random + buffered/block Read /write.
Network Driver : Directly transfer packet of data to kernel. Not mapped to file system.

Tainted Kernel is any part of kernel code without GPL ( or friends ) License.

Device Driver Structure

__init() -> Any Code within this is executed first, at the time of loading. Not mandatory.
__exit() -> This part is executed an the time of driver unloading.

Parameters can be passed to device driver using "module_params()". If R/W is given then it can be changed on the fly with " $ echo "value" > /sys/module/parameter/"

Finally, Any sys call the device driver handles are mapped to the "fops" entry points of the driver. For example , the sys-call close() is mapped to "release()" entry point of the driver.

Major Number are used to identify driver associated with device
Minor Number are used only within the device driver to distinguish different functionality.

Important Structures

File Structure : Kernel Created new file structure on every open(). This is passed to rest of the function. Separate allocated memory per open() is called "private data'.
Inode Structure : There is just 1 inode structure per device.

task_struct = keeps all info about tasks.

Interrupt & Exception

  • Interrupt can never be lost.
  • Interrupt are shared. That is all handlers get the interrupt, the one which is responsible consumes it. The rest discards it.
  • Interrupts are Maskable and Non-Maskable interrupts.
  • Under SMP only one CPU will handle interrupt of same type.
  • Cannot sleep in interrupt context.
  • Cannot call schedule().

Top Half : Actual Routine, which ack interrupt. Copy data from device to buffer. Small and fast.
Bottom Half : Do delay processing work here. There are 3 types of bottom half.
  • Softirq : Run in interrupt context
  • Tasklets : Run in interrupt context
  • Workqueues: Run in process context. Can sleep.

Timers

Jiffies : Is a counter which increments on every timer interrupt
TSC : Time stamp clock, register which increments on every clock cycle. H/W dependent.
HPET : High precision event timer. Is an hardware.

IOCTLS

Is an driver entry point. User can call driver entry point using ioctls.

PROCFS

Read only FS in memory. Used for debugging and information gathering.

SysFS

Virtual File system in memory.

Race conditions.

Prevent two or more threads accessing same resource.

Atomic function : Execute single instruction which cannot be interrupt.

Mutex: Sleepable locking mechanism.
  • interrupt-able mutex: single can break it.
  • non-interrupt-able mutex: only user / owner can break it
  • its non recursive.
Spin lock : Non sleepable locking mechanism.


Semaphore : Sleepable locking mechanism.

  • Counting semaphore : More than one thread can hold a semaphore.
  • binary semaphore : Same as mutex.

Memory management

  • Zones:
  • 0-16mb : DMA
  • 16-896 : Normal
  • 896 - 4GB : High.

kmalloc() : Allocate memory.
GFP_KERNEL: NORMAL , CAN SLEEP AND BLOCK.
GFP_ATOMIC: INTERRUPT, CANNOT BLOCK
GFP_DMA : USED FOR DMA
kfree(): Free memory.

vmalloc() : Get continuous memory from virtual address. And cannot be used in interrupt context and DMA.
vfree() : free memory allocated from vmalloc.

Boot Memory + early allocation

Device driver can only allocated 4MB max. To circumvent this "alloc_bootmem()" is used which can allocate any amount.

Reading / Writing To and from user space to kernel space use below functions as dereferencing pointer passed from user space into kernel space is bad.
  • put_user()
  • get_user()
  • copy_to_user()
  • copy_from_user()

No comments:

Post a Comment