1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Saturday, May 5, 2012

Device Driver Interview Questions


User Mode, Kernel Mode and System Calls

What is the difference between kernel mode and user mode? What is a system call?
The difference between kernel mode and user mode is similar to that of a Administrator and user logins of  the computer.
The administrator can change the settings etc and thus has all the required permissions, whereas the user can only use as per the settings done by the administrator. This distinguishing is done to see that the user does not misuse or by chance corrupt the settings.
Similarly the operating systems runs in the kernel mode(administrator) and the user mode (user).
Since in kernel mode, programs have direct access to all the hardware and the devices, the operating system provides a mechanism for the user, so that the user can access the devices using them. The mechanism are the system calls.
All the applications run in the user mode and whenever a user needs to utilize any of the devices he needs to use system calls. Then the control is transferred from the user to the kernel and the specified read or write of data is performed.
Thus removing the possibility of the user to corrupt the devices or the settings unknowingly.


Threads Vs Processes

What is the difference between a thread and a process?
When a binary is executed it runs as a process in the system. Every process gets its time of execution from the process depending on the system specific algorithm.
Suppose if there are two processes then each of them might get 5 seconds each and after 5 seconds process one is removed and process two is replaced and executed and then again after 5 seconds the second process is removed and process one is replaced with the previous values and this continues.
Let us assume that a process is given 10 sec of time and in that if we span/create a thread then the thread is created in the same process address space and the time allocated for the parent process is shared between the parent and the created child thread equally i.e. 5 seconds each and if another thread is created the time is divided between the two and they use the common process address space. So finally there are only two processes in the system and three threads in a process.
If on the other hand if a process is created during the execution then a new process is created and is given a separate process time. So if there were two processes and each process was given 5 seconds each then after a new process is created there are three process each with 5 seconds of time. Each process will have its own address space.
For performance improvement if a parent spawns a process then both the parent and child share the data section of the parent for calculation as long as either of the process parent or child do not change the data in that section.
Whenever any one of them say parent changes the data, before the data is being changed the data is copied to both the processes address space and data is changed only in the parent process and not in the child. Similarly if child changes the data then the data is copied from the parents address space and then changed in the child’s address space. This is called copy on write mechanism.

Character Vs Block device drivers

What is the difference between a character device and a block device drivers?
Character device is a device from which data is read or written as a stream of bytes i.e as a stream of bytes.
Examples : Serial ports, Consoles etc
Block device as the name suggests is a device from which data is read or written as blocks, or chunk of data at once. Normally a block would be of 4096 bytes.
Examples: Hard drives on the system.
The performance of a block driver is important as it involves movement of data from the physical memory to the virtual memory (hard disk) whenever there is a process switch. Where as performance of a character driver does not affect the performance of the system much.

Device Driver Interview Questions


Why not vmalloc for device drivers?


Vmalloc also allocates memory contiguously in the virtual address space, but in physical address space the memory pages might not be contiguous.
The memory that is obtained using vmalloc is not that efficient to work as with the memory allocated when kmalloc is used.
The memory that is set aside for the vmalloc in some architectures is less when compared to kmalloc and hence if you try to allocate more memory it might fail.
Vmalloc allocates memory by building the page tables that is tweaking them rather than directly using. i.e the memory allocated by vmalloc is contiguous as far as the user understands but it could be memory pages fetched from different physical memory locations and a page table built on top of it.
So the overhead on vmalloc is high as it not only has to fetch the free pages but also build the page tables for the same.
So the only point where vmalloc should be used is when you need very large memory like the one during the module creation, when the code and data copied is large and a continuous memory allocation is not possible.


Using kmalloc.  Kmalloc will assign memory contigiously.This call can block(put the process to sleep) if sufficient memory is not available.  i.e it waits until the requested amount of memory is available.
void *kmalloc(size_t size, int flags);
flags can be
GFP_USER
GFP_KERNEL
The above two flags can put the process to sleep
GFP_NOFS
GFP_NOIO
the above two flags will disable the file and input output systems
GFP_ATOMIC
the process cannot be put to sleep. i.e if memory is not available the allocation fails

What is IRQ?


What is IRQ? How will you register your IRQ no? Are you sure you will get the IRQ no you have requested? What if you don’t get?
Before getting into Interrupt request queue (IRQ), let us understand interrupts. Interrupts are a mechanism using which you will ask the processor to do a particular task.
These are mechanisms that the processor cannot ignore and whenever a interrupt comes, the processor will move the present working process and has to handle the interrupt. For handling the interrupt for a particular device, we need to register the interrupt handler. There are a limited number of interrupt lines available and hence you would need to request the processor to allocate a interrupt line for your device. So you request for a interrupt and the interrupt line that is requested is called an interrupt request queue.
For getting an IRQ there are APIs given in
int request_irq(unsigned int irq,
irqreturn_t (*handler)(int, void *, struct pt_regs *),unsigned long flags,const char *dev_name,
void *dev_id);
void free_irq(unsigned int irq, void *dev_id);
From above it can be seen that
irq  is the interrupt number that you are requesting for.
Handler is a function pointer to the function that is to be called when an interrupt occurs.
Flags are
SA_INTERRUPT
When set, this indicates a “fast” interrupt handler. Fast handlers are executed
with interrupts disabled on the current processor
SA_SHIRQ
This bit signals that the interrupt can be shared between devices.
SA_SAMPLE_RANDOM
This bit indicates that the generated interrupts can contribute to the entropy pool used by /dev/random and /dev/urandom.
The retrun value of the request_irq function call is ’0′ if the the interrupt line with the number asked in the ‘irq’ parameter is allocated, else a negative error code is returned.
There are kernel defined functions which the user can use to request the kernel to give the available interrupts before going ahead to request one, like unsigned long probe_irq_on(void);

How do we know which major number and minor numbers arealready used?


The list of all the devices connected to the system can be found in /dev directory. When we type ls -l in the directory /dev the character devices are found as
crw–w–w-  1 root    root    1,   7          2011-08-17   18:54    full
here c specifes it as a char device
and
b———  1 root    root         2,  68      2011-08-17   8:54     fd0u830
b would specify a block device.
And in the above 1 which is bold is the major number and 7 is the minor number. which specify that the device using a driver with major number one and the minor number of the device is 7. similarly for the second one 2  is the major number and 68 is the minor number.

 
# #