Tuesday 16 May 2017

Embedded

Kernel Panic

A panic is an unrecoverable system error detected by the kernel as opposed to similar errors detected by user space code. It is possible for kernel code to indicate such a condition by calling the panic function   

major and minor number to device1)statically:register_chrdev_region(dev_t first, unsigned int count,char *name); it is a kernel call it is responsible for allocating device number statically.

-it accept three arguments 1.device number.
-integer value it is your driver supported to number of devices.
-driver name.

2)dynamically:alloc_chrdev_region(); it is responsible for allocating
device number dynamically,means this call says to kernel to allocate freelly available major number. 

Kernel can access the user space memory, Why should copy_from_user is needed?
 copy_from_user() is usually used when writing certain device drivers. Note that there is no "mapping" of bytes here, the only thing that is happening is the copying of bytes from a certain virtual location mapped in user-space to bytes in a location in kernel-space. This is done to enforce separation of kernel and user and to prevent any security flaws -- you never want the kernel to start accessing and reading arbitrary user memory locations or vice-versa.
 
RTOS Criteria:
The following are the parameters that make RTOS to deviate from the schedule
Context Switching time 
Context-switching time is the time taken by the scheduler to execute the next process in the scheduling queue. Context switching may occur due to timeout occurs if Round Robin scheduling, higher priority process comes, and if interrupt comes from the hardware.
Interrupt latencies 
 Interrupt latency, it’s the time between the when hardware generate an interrupt and corresponding hardware interrupt handler first line gets executed. So operating system itself takes some time to identify from which device the interrupt has come, and call the registered interrupt handler for the hardware.
SpinLock  
A spinlock is a mutual exclusion device that can have only two values: "locked" and "unlocked."Any time kernel code holds a spinlock, preemption is disabled on the relevant processor. Even uni-processor systems must disable preemption in this way to avoid race conditions. 

Major types of device driver?
 
* Character device driver
Char device drivers usually deals with read/write byte on device. For example keyboard or mouse drivers
* Block device driver
Block devices usually involves large data to handle such as hard disk, SD card.


file_operations structure 
The file_operations structure is define in linux/fs.h;. This structure is a collection of function pointers which is implemented in our drivers.
static const struct file_operations sample_fops = {
        .owner = THIS_MODULE,
        .unlocked_ioctl = sample_ioctl,
        .open           = sample_open,
        .release        = sample_close 
};  

Register and un-register device driver
The Linux kernel provides APIs for register and un register of your driver
major = register_chrdev(0, "samplechar", &sample_fops);
The first argument as 0 allows the kernel to allocate a major number which is available.
Second argument represents the driver name which is displayed in /proc/devices

Third argument is the pointer to file_operations structure which we filled earlier.

The un register operation is done at clean up function.
unregister_chrdev(major, "samplechar");
ioctl()
ioctl() is very important function in device driver. 
The commands specific to devices are issued to driver through ioctls. From user space, the command is received at ioctl() and the corresponding action will be taken by driver.

Based on the ioctl command from user space, driver performs operations and returns results to user space if necessary. The functions copy_to_user() and copy_from_user() is responsible for copying the information to/from user space.

 mknod()
 
 After compiling the driver, insert in to the kernel. After that that driver need to be create by mknod.

 Device files are kept in /dev, and unlike normal files, these are files the kernel knows about, and reads/writes to.
mknod follows the syntax.
mknod device-name device-type major-number minor-number
 mknod /dev/random c 1 8
Creates the character device /dev/random and has it point to major number 1, minor number 8.

Wrappers for drivers in user space  
Usually driver developers will hide the complexity of opening device file, remembering the ioctl commands. If the application is developed by third party, it is better to write a wrapper which exposes only functionality.

The wrapper is written as shared library(.so) which takes care of handling driver related stuff and exposes only functionality.

Sample program for Char Device Driver below
https://github.com/jeyarmvrp/kernel-module-programming/tree/master/sample-char-dir



No comments:

Post a Comment