Wednesday, 6 July 2016

Linux Device Drivers Interview Questions


Linux Device Model (LDM)


  • Explain about the Linux Device Model (LDM)?
  • Explain about about ksets, kobjects and ktypes. How are they related?
  • Questions about sysfs.


Linux Boot Sequence
 
  • Explain about the Linux boot sequence in case of ARM architecture?
AM335x Bootloader Order
              The ROM code has two main functions:
           - Configuration of the device and initialization of primary peripherals
  • Stack setup
  • Configure Watchdog Timer 1 (set to three minutes)
  • PLL and System Clocks configuration
       - Ready device for next bootloader
  • Check boot sources for next bootloader (SPL)
  • Moves next bootloader code into memory to be run   
MLO /SPL
The SPL is the first stage of U-boot, and must be loaded from one of the boot sources into internal RAM. The SPL has very limited configuration or user interaction, and mainly serves to set-up the boot process for the next bootloader stage: U-boot. 

  • How are the command line arguments passed to Linux kernel by the u-boot (bootloader)?
  • Explain about ATAGS? 
            A device tree describes everything about the hardware which the kernel uses to select which drivers to load, where all the MMIO interfaces are, etc... at runtime. ATAGs just describes stuff like where to find an initrd and kernel parameters, memory, etc... - everything else about the machine is hard coded into the kernel.
The preferred method now is to use device trees instead of ATAGs. One of the advantages include the fact that adding a new platform doesn't always require adding new code to the kernel.
r0 = 0,
r1 = machine type number discovered in (3) above.
r2 = physical address of tagged list in system RAM, or
     physical address of device tree block (dtb) in system RAM
Tag nameValueSizeDescription
ATAG_NONE0x000000002Empty tag used to end list
ATAG_CORE0x544100015 (2 if empty)First tag used to start list
ATAG_MEM0x544100024Describes a physical area of memory
ATAG_VIDEOTEXT0x544100035Describes a VGA text display
ATAG_RAMDISK0x544100045Describes how the ramdisk will be used in kernel
ATAG_INITRD20x544200054Describes where the compressed ramdisk image is placed in memory
ATAG_SERIAL0x54410006464 bit board serial number
ATAG_REVISION0x54410007332 bit board revision number
ATAG_VIDEOLFB0x544100088Initial values for vesafb-type framebuffers
ATAG_CMDLINE0x544100092 + ((length_of_cmdline + 3) / 4)Command line to pass to kernel
For implementation purposes a structure can be defined for a tag
struct atag {
        struct atag_header hdr;
        union {
                struct atag_core         core;
                struct atag_mem          mem;
                struct atag_videotext    videotext;
                struct atag_ramdisk      ramdisk;
                struct atag_initrd2      initrd2;
                struct atag_serialnr     serialnr;
                struct atag_revision     revision;
                struct atag_videolfb     videolfb;
                struct atag_cmdline      cmdline;
        } u;
};
  • Explain about command line arguments that are passed to linux kernel and how/where they are parsed in kernel code?
  • Explain about device tree.
The device tree comes in three forms:
A text file (*.dts) — “source”
A binary blob (*.dtb) — “object code”
A file system in a running Linux’ /proc/device-tree directory — “debug and reverse engineering information”
  •  

Interrupts in Linux

  • Explain about the interrupt mechanims in linux?
http://nikhillnx.blogspot.com/2016/07/interrupt-handling-botton-half.html
  • What are the APIs that are used to register an interrupt handler?
           int request_irq(unsigned int irq,
                                   irqreturn_t (*handler)(int, void *, struct pt_regs *),
                         unsigned long flags, 
                         const char *dev_name,
                         void *dev_id);
      irqreturn_t (*handler)(int, void *, struct pt_regs *) 
 
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
            const char *name, void *dev)
 
 void free_irq(unsigned int irq, void *dev_id);
  • How do you register an interrupt handler on a shared IRQ line?
  • Explain about the flags that are passed to request_irq().
       The bits that can be set in flags are as follows: 
            SA_INTERRUPT: When set, this indicates a "fast" interrupt handler.
            SA_SHIRQ: This bit signals that the interrupt can be shared between devices.

  • Explain about the internals of Interrupt handling in case of Linux running on ARM.
http://nikhillnx.blogspot.com/2016/07/interrupt-handling.html
  • What are the precautions to be taken while writing an interrupt handler?
  • Explain interrupt sequence in detail starting from ARM to registered interrupt handler.
  • What is bottom half and top half.
  • What is request_threaded_irq()
  • If same interrupts occurs in two cpu how are they handled?
http://stackoverflow.com/questions/22638062/what-happens-when-two-interrupts-occur-at-the-same-time-in-linux-kernel
  • How to synchronize data between 'two interrupts' and 'interrupts and process'.
  • How are nested interrupts handled?
  • How is task context saved during interrupt.
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.den0024a/CDDEIJCH.html


Bottom-half Mechanisms in Linux
 
  • What are the different bottom-half mechanisms in Linux?
Softirq, Tasklet and Workqueues
  • What are the differences between Softirq/Tasklet and Workqueue? Given an example what you prefer to use?
  • What are the differences between softirqs and tasklets?
Softirq is guaranteed to run on the CPU it was scheduled on, where as tasklets don’t have that guarantee.The same tasklet can't run on two separate CPUs at the same time, where as a softirq can. 
  • When are these bottom halfs executed?
  • Explain about the internal implementation of softirqs?
http://linuxblore.blogspot.com/2013/02/bottom-halves-in-linux-part-1-softirqs.html
  • Explain about the internal implementation of tasklets?
http://linuxblore.blogspot.com/2013/02/bottom-halves-in-linux-part-2-tasklets.html

  • Explain about the internal implementation of workqueues?
http://linuxblore.blogspot.in/2013/01/workqueues-in-linux.html
  • Explain about the concurrent work queues.
 
Linux Memory Management

 The Memory Management Unit


Memory Management in Linux by Alan Ott


http://www.eecg.toronto.edu/~yuan/teaching/ece344/linux-vmm.pdf
  • What are the differences between vmalloc and kmalloc? Which is preferred to use in device drivers?
kmalloc allocates physically contiguous memory, memory which pages are laid consecutively in physical RAM. vmalloc allocates memory which is contiguous in kernel virtual memory space (that means pages allocated that way are not contiguous in RAM, but the kernel sees them as one block)

vmalloc is often slower than kmalloc, because it may have to remap the buffer space into a virtually contiguous range. kmalloc never remaps, though if not called with GFP_ATOMIC kmalloc can block.

kmalloc is limited in the size of buffer it can provide: 4MB. If you need a really big buffer, you have to use vmalloc or some other mechanism like reserving high memory at boot.

For a system call you don't need to pass GFP_ATOMIC to kmalloc(), you can use GFP_KERNEL. You're not an interrupt handler: the application code enters the kernel context by means of a trap, it is not an interrupt
  • What are the differences between slab allocator and slub allocator?
  • What is boot memory allocator?
  • How do you reserve block of memory?
  • What is virtual memory and what are the advanatages of using virtual memory?
  • What's paging and swapping?
  • Is it better to enable swapping in embedded systems? and why?
  • What is the page size in Linux kernel in case of 32-bit ARM architecture?
  • What is page frame?
  • What are the different memory zones and why does different zones exist?
  • What is high memory and when is it needed?
  • Why is high memory zone not needed in case of 64-bit machine?
  • How to allocate a page frame from high memory?
  • In ARM, an abort exception if generated, if the page table doesn't contain a virtual to physical map for a particular page. How exactly does the MMU know that a virtual to physical map is present in the pagetable or not?
  • A Level-1 page table entry can be one of four possible types. The 1st type is given below: 
A fault entry that generates an abort exception. This can be either a prefetch or data abort, depending on the type of access. This effectively indicates virtual addresses that are unmapped.
In this case the bit [0] and [1] are set to 0. This is how the MMU identifies that it's a fault entry.
Same is the case with Level-2 page table entry.

  • Does the Translation Table Base Address (TTBR) register, Level 1 page table and Level 2 page table contain Physical addresses or Virtual addresses?
TTBR: Contain physical address of the pgd base
Level 1 page table (pgd): Physical address pointing to the pte base
Level 2 page table (pte): Physical address pointing to the physical page frame
Since page tables are in kernel space and kernel virtual memory is mapped directly to RAM. Using just an easy macro like __virt_to_phys(), we can get the physical address for the pgd base or pte base or pte entry.


Kernel Synchronization
  • Why do we need synchronization mechanisms in Linux kernel?
  • What are the different synchonization mechanisms present in Linux kernel?
  • What are the differences between spinlock and mutex?What are the different schedulers class present in the linux kernel?
  • How to create a new process?
  • What is the difference between fork( ) and vfork( )?
  • Vfork : The basic difference between vfork and fork is that when a new process is created with vfork(), the parent process is temporarily suspended, and the child process might borrow the parent's address space. This strange state of affairs continues until the child process either exits, or calls execve(), at which point the parent process continues.
    This means that the child process of a vfork() must be careful to avoid unexpectedly modifying variables of the parent process. In particular, the child process must not return from the function containing the vfork() call, and it must not call exit() (if it needs to exit, it should use _exit(); actually, this is also true for the child of a normal fork()).
    Exec : The exec call is a way to basically replace the entire current process with a new program. It loads the program into the current process space and runs it from the entry point. exec() replaces the current process with a the executable pointed by the function. Control never returns to the original program unless there is an exec() error.
  • Which is the first task what is spawned in linux kernel?
  • What are the processes with PID 0 and PID 1?
  • PID 0 - idle task 
  • PID 1 - init 
  • How to extract task_struct of a particular process if the stack pointer is given?
  • How does scheduler picks particular task?
  • When does scheduler picks a task?
  • How is timeout managed?
  • How does load balancing happens?
  • Explain about any scheduler class?
  • Explain about wait queues and how they implemented? Where and how are they used?
  • What is process kernel stack and process user stack? What is the size of each and how are they allocated?
  • Why do we need seperate kernel stack for each process?
  • What all happens during context switch?
  • What is thread_info? Why is it stored at the end of kernel stack?
  • What is the use of preempt_count variable?
  • What is the difference between interruptible and uninterruptible task states?
  • How processes and threads are created? (from user level till kernel level)
  • How is virtual run time (vruntime) calculated?
  • What is lockdep?
  • Which synchronization mechanism is safe to use in interrupt context and why?
  • Explain about the implementation of spinlock in case of ARM architecture.
  • Explain about the implementation of mutex in case of ARM architecture.
  • Explain about the notifier chains.
  • Explain about RCU locks and when are they used?
  • Explain about RW spinlocks locks and when are they used?
  • Which are the synchronization technoques you use 'between processes', between processe and interrupt' and 'between interrupts'; why and how?
  • What are the differences between semaphores and spinlocks? 
Process Management and Process Scheduling 

  • What are the different schedulers class present in the linux kernel?
            CFS (Completly Fair Sheduler)
            RT (Real time Sheduler)
            Earliest  Dad line first Sheduler
  • How to create a new process?
  • What is the difference between fork( ) and vfork( )?
  • Which is the first task what is spawned in linux kernel?
  • What are the processes with PID 0 and PID 1?
PID 0 - idle task
PID 1 - init 

  • How to extract task_struct of a particular process if the stack pointer is given?
  • How does scheduler picks particular task?
  • When does scheduler picks a task?
  • How is timeout managed?
  • How does load balancing happens?
  • Explain about any scheduler class?
  • Explain about wait queues and how they implemented? Where and how are they used?
  • What is process kernel stack and process user stack? What is the size of each and how are they allocated?
  • Why do we need seperate kernel stack for each process?
  • What all happens during context switch?
  • What is thread_info? Why is it stored at the end of kernel stack?
  • What is the use of preempt_count variable?
  • What is the difference between interruptible and uninterruptible task states?
  • How processes and threads are created? (from user level till kernel level)
  • How is virtual run time (vruntime) calculated?
  • What is pre-emptive and non-preemptive scheduling?

    Tasks are usually assigned with priorities. At times it is necessary to run a certain task that has a higher priority before another task although it is running. Therefore, the running task is interrupted for some time and resumed later when the priority task has finished its execution. This is called preemptive scheduling.

    Eg: Round robin

    In non-preemptive scheduling, a running task is executed till completion. It cannot be interrupted.
    Eg First In First Out
Timers and Time Management
  • What are jiffies and HZ?
  • What is the initial value of jiffies when the system has started?
  • Explain about HR timers and normal timers?
  • On what hardware timers, does the HR timers are based on?
  • How to declare that a specific hardware timers is used for kernel periodic timer interrupt used by the scheduler?
  • How software timers are implemented?
Power Management in Linux 
  • Explain about cpuidle framework.
  • Explain about cpufreq framework.
  • Explain about clock framework.
  • Explain about regulator framework.
  • Explain about suspened and resume framwork.
  • Explain about early suspend and late resume.
  • Explain about wakelocks.
Linux Kernel Modules
  • How to make a module as loadable module?
  • How to make a module as in-built module?
  • Explain about Kconfig build system?
  • Explain about the init call mechanism.
  • What is the difference between early init and late init?
Early init:
Early init functions are called when only the boot processor is online.
Run before initializing SMP.
Only for built-in code, not modules.
Late init:
Late init functions are called _after_ all the CPUs are online.
Linux Kernel Debugging

http://nikhillnx.blogspot.com/2016/07/h3.html
  • What is Oops and kernel panic?
  • Does all Oops result in kernel panic?
  • What are the tools that you have used for debugging the Linux kernel?
  • What are the log levels in printk?
  • Can printk's be used in interrupt context?
  • How to print a stack trace from a particular function?
  • What's the use of early_printk( )? 
  • Explan about the various gdb commands. 
  •  
Miscellaneous
  • How are the atomic functions implemented in case of ARM architecture?
  • How is container_of( ) macro implemented? 
  • Explain about system call flow in case of ARM Linux.
  • What 's the use of __init and __exit macros?
  • How to ensure that init function of a partiuclar driver was called before our driver's init function is called (assume that both these drivers are built into the kenrel image)?
  • What's a segementation fault and what are the scenarios in which segmentation fault is triggered?
  • What is difference between monolithic and micro kernel?
    Monolithic kernel is a single large process running entirely in a single address space. It is a single static binary file. All kernel services exist and execute in the kernel address space. The kernel can invoke functions directly. Examples of monolithic kernel based OSs: Unix, Linux.
    In microkernels, the kernel is broken down into separate processes, known as servers. Some of the servers run in kernel space and some run in user-space. All servers are kept separate and run in different address spaces. Servers invoke "services" from each other by sending messages via IPC (Interprocess Communication). This separation has the advantage that if one server fails, other servers can still work efficiently. Examples of microkernel based OSs: Mac OS X and Windows NT.
If the scenarios which triggers the segmentation fault has occurred, how the kernel identifies it and what are the actions that the kernel takes?  
 
 

No comments:

Post a Comment