Linux Qstn

download Linux Qstn

of 8

Transcript of Linux Qstn

  • 7/27/2019 Linux Qstn

    1/8

    1. What is the difference in features between kernel 2.2, 2.4 and 2.6 ?

    The biggest change to LKMs between Linux 2.4 and Linux 2.6 is an internal one: LKMs get loaded much differentlyBefore Linux 2.6, a user space program would interpret the ELF object (.o) fileand do all the work of linking it to the running kernel, generating a finished binary image. The program would pass that image to the kernel and the kernel would do little more than stick it in memory. In Linux 2.6, the kernel does the linking. A user space program passes the contents of the ELF object file directly tothe kernel. For this to work, the ELF object image must contain additional information. To identify this particular kind of ELF object file, we name the file with suffix ".ko" ("kernel object") instead of ".o" For example, the serial device driver that in Linux 2.4 lived in the file serial.o in Linux 2.6 lives in thefile serial.ko.

    So there is a whole new modutils package for use with Linux 2.6. In it, insmod is a trivial program, as compared to the full blown linker of the Linux 2.4 version.

    Also, the procedure to build an LKM is somewhat harder. To make a .ko file, youstart with a regular .o file. You run the program modpost (which comes with theLinux source code) on it to create a C source file that describes the additionalsections the .ko file needs. We'll call this the .mod file because you conventi

    onally include ".mod" in the file name.

    You compile the .mod file and link the result with the original .o file to makea .ko file.

    The .mod object file contains the name that the LKM instance will have when youload the LKM. You set that name with a -D compile option (when you compile the .mod file) that sets the KBUILD_MODNAME macro

    2. What are Static and Shared libraries ?3. What is dynamic linking ? What is static linking ?

    External libraries are usually provided in two forms: static libraries and shared libraries. Static libraries are the .a files seen earlier. When a program is linked against a static library, the machine code from the object files for any external functions used by the program is copied from the library into the final executable.This process of linking is called static linking.

    Shared libraries are handled with a more advanced form of linking, which makes the executable file smaller. They use the extension .so, which stands for shared object. An executable file linked against a shared library contains only a small table of the functions it requires, instead of the complete machine code from theobject files for the external functions. Before the executable file starts running, the machine code for the external functions is copied into memory from the

    shared library file on disk by the operating system--a process referred to as dynamic linking.

    4. What are the advantages of Dynamic linking or Shared libraries ?

    Dynamic linking makes executable files smaller and saves disk space, because onecopy of a library can be shared between multiple programs. Most operating systems also provide a virtual memory mechanism which allows one copy of a shared library in physical memory to be used by all running programs, saving memory as well as disk space. Furthermore, shared libraries make it possible to update a libr

  • 7/27/2019 Linux Qstn

    2/8

    ary without recompiling the programs which use it (provided the interface to thelibrary does not change). Because of these advantages gcc compiles programs touse shared libraries by default on most systems, if they are available.

    5. Does gcc search for both static and shared libraries ? Which is searched initially by gcc compiler ?

    yes both are searched but firstly shared libraires are searched by gcc. Whenevera static library libNAME.a would be used for linking with the option -lNAME the compiler first checks for an alternative shared library with the same name and a .so extension.

    6. What should be done for Shared library based linking in gcc ?7. What should be done for static library based linking in gcc ?

    when gcc compiler searches a given library(for example libgdbm) in the library path(for example/opt/ gdbm-1.8.3/lib) it will fing two executable files of same name, one will be of .so extention (shared library file ) and other will be of .aextention (static library file). Gcc by default will take preferance to select.so, to load any of them specifically you need to specify the path.

    for example to do shared linking, there are two ways, one is by using the enviro

    nment variable LD_LIBRARY_PATH since .so will be selected automatically or by explicitly giving the path

    $ LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib$ export LD_LIBRARY_PATH$ ./a.out //first way to do shared librry based linking

    $ gcc -Wall -I/opt/gdbm-1.8.3/includedbmain.c /opt/gdbm-1.8.3/lib/libgdbm.so //another way to do shared librry bas

    ed linking in this way you need to set the library load path while running executable/library_load_path/a.out

    we can force the compiler to link the execcutable with static library "lgdbm.a"instead of default shared library by using -static

    $ gcc -Wall -static -I/opt/gdbm-1.8.3/include/-L/opt/gdbm-1.8.3/lib/ dbmain.c -lgdbm./a.out

    $ gcc -Wall -I/opt/gdbm-1.8.3/includedbmain.c /opt/gdbm-1.8.3/lib/libgdbm.a //static library based linking./a.out

    8. What is object file and what are symbols ?

    An object file is a file containing relocatable format machine code that is usually not directly executable. Object files are produced by an assembler, compiler, or other language translator, and used as input to the linker.

    When a program is compiled, variable names are replaced by their (virtual) addresses. The names aren't necessary for the program to run, but they are essentialfor debugging. So the compiler stores them and their corresponding addresses inthe symbol table.

  • 7/27/2019 Linux Qstn

    3/8

    This also applies to shared libraries. Imagine that I have a library (we'll callit libfoo.so) and I am writing a program that dynamically links with it. Suppose that my program wants to call the bar() function defined in libfoo.so. The linker needs to know where the bar() function is actually defined inside libfoo.so.So the symbol table maps the "bar" symbol to some address.

    The way that a modern program might work, then, would be that the first time myprogram calls the bar() function, it actually jumps to a fake address inside ofld.so (a shared library that acts as the linker). At this point, ld.so looks atthe symbol tables of the libraries that I've linked with, finds the bar() function, and replaces the fake address with the actual address. Then, in the future,my program will go to bar() directly, and not go through ld.so.

    9. Can you tell the memory layout based on Data,BSS,HEAP and STACK ?

    The memory is been divided in 4 major sections , Data , BSS , Heap and Stack. All storing is dependent on the storage class of the variable,Global and static variables = BSSinitialized data = DataLocal variables = stackDynamic memory = heap

    Three types of allocation/deallocation strategiesGlobal and static variables (BSS) = program startup/terminationLocal variables (stack) = function entry/returnDynamic memory (heap) = malloc()/free()

    10. What are the ways in which linux kernel can be compiled ?

    After you download and extract the kernel archive, cd into it. Inside, there's ahandful of directories and files, nothing remarkable. This is your entire kernel source. It needs to be built against a configuration file that dictates what,who and when will be included in the final kernel image. The configuration fileis a hidden file called .config and it's a line-delimited list of entries with k

    ernel parameters and values.By default, you will not have a configuration if you've just download a kernel.You can use your own, which ships with your distribution. On Ubuntu and RedHat,you will find the configuration under /boot. on SUSE, it's located under /proc/config.g

    configure the kernel : make menuconfig/gconfig/xconfig: Now, you need to togglewhat settings you want to use. You can manually go through the configuration file and mark different entries, but this is tedious and not really useful if you're not a super-expert. The friendlier alternative is to launch a configuration menu for kernel compilation, which is invoking in different ways, depending on what interface you want to use. make menuconfig is the simple option, with ncursestext interface inside terminal. make gconfig and make xconfig are graphical inte

    rfaces with mouse support.

    make all/make oldconfig: The next step is to compile everything. This is done byrunning make all command. If your configuration does not contain answers for all of the options, especially if they are new and not currently included in yourrunning kernel, you will need to answer the prompts for these options. there isan easier way, You can run make oldconfig, a command that will prompt you only for new changes that are not marked in your existing configuration file. If you don't do that, you will have to answer prompts in real time.

  • 7/27/2019 Linux Qstn

    4/8

    Copying the kernel image in boot: Once make all/make oldcofig is done , you willneed to either manually copy the new kernel to /boot, create the initrd file and change the GRUB menu or use the make install option to get things done automatically.

    So the manual way to get this done is by first copying the kernel image:

    cp /boot/

    Next, create initrd filecd /bootmkinitrd -o initrd.img-

    Update GRUB or GRUB2, manually or by running update-grub.

    or just use install option

    make modules_install install

    This last step needs root or sudo privileges.

    11. How will get the driver added into the kernel ? What are Kconfig files ?

    Adding a driver to kernel :First we need to copy all of the source code(.c and .h) of our d

    river to the respective kernel-driver directory, for example if you are adding anetwork driver the copy path will be /usr/src/kernel_src/drivers/net .To add a driver into the kernel we need to list its .o file in the makefile (/drivers/net/makefile) so that our driver will be compiled.After adding the device to the Makefile, we have to make sure we can configure the device when we configure the kernel, for this we need to edit kconfig file asthe configuration we claimed in the makefile.Last thing is we need to list out device in the file that probess our device onstartup, for example if we are adding a network device , all network device are

    listed in driver/net/space.c, so first we would have to add the reference to theprobe function, then we'll add the device to the list of devices to probe for.Then we need to add our actual device in the respective probe list(PCI, EISA, SBus, MCA, ISA, parallel port, etc.).

    Kconfig:

    The Kconfig mechanism is todays standard configuration mechanism and itis used by leading open source projects, such as the Linux kernel, Busybox and uClibc. The Kconfig has a basic configuration syntax that allows you to add configuration options of various types, create dependencies and write a few lines ofdescription. The Kconfig utilities know how to read and parse the configurationfiles, and create project configuration files (usually by the name of .config fo

    r Makefiles and autoconf.h for source files). Other advantages this method has,are automatic menu generation (for both graphical and text based consoles), andease of configuration management. With Kconfig, there is no need to specify anybuild flags to the projects make.

    When the kernel configurator is run, it reads the main kernel configuration file, located in arch/i386/Kconfig for the i386 platform. Other architectures have the main configuration files located in their main directories. This mainconfiguration file then includes other configuration files from the different subdirectories in the kernel directory tree. Those configuration files also can i

  • 7/27/2019 Linux Qstn

    5/8

    nclude other configuration files as needed.

    12. What is a kernel module ?

    Modules are pieces of code that can be loaded and unloaded into the kernel upondemand. They extend the functionality of the kernel without the need to rebootthe system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we wouldhave to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiringus to rebuild and reboot the kernel every time we want new functionality.

    13. What is the difference between insmod and modprobe ?both of them can be used to insert modules.

    DIFFERENCE : insmod doesn't have the capability to resolve dependency issues, that is if the installation of a module requires some pre-requisites insmod is notable to resolve that. But modprobe can resolve dependency issues.

    how: While installing a module using modprobe, it first determines whether thereexists any dependencies by checking the file /lib/modules//modules.dep. This file contains entries for a module and its corresponding dependencies. modules.dep files is generated by the command depmod.

    14. How will you list the modules ?

    You need to use lsmod program which show the status of loaded modules in the Linux Kernel. Linux use term modules for hardware device drivers.lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded.This is an important task. With lsmod you can verify that device driver is loaded for particular hardware. Any hardware device will only work if device driver is loaded.$ less /proc/modules$ lsmod

    15. How do you get the list of currently available drivers ?

    You can get a list of currently available drivers in /proc/devices. Do a ls hereand you will get the list of currently available drivers

    16. How will you Access userspace memory from kernel ? What are the various methods ?

    access_ok Checks the validity of the user space memory pointerget_user Gets a simple variable from user spaceput_user Puts a simple variable to user spaceclear_user Clears, or zeros, a block in user spacecopy_to_user Copies a block of data from the kernel to user spacecopy_from_user Copies a block of data from user space to the kernel

    strnlen_user Gets the size of a string buffer in user spacestrncpy_from_user Copies a string from user space into the kernel

    not only mapping is allowed from kernel side , there are mechanishm such as shared memory technique that can be used to access userspace memory from kernel , for example a userspace appication can allocate a virtual memory using mmap and that can be shared by kernel,within the kernel, the mmap function is implemented through the remap_pfn_range kernel function, which provides a linear mapping of device memory into a user's address space.

  • 7/27/2019 Linux Qstn

    6/8

    17. What is the use of ioctl(inode,file,cmd,arg) ApI ?

    Most drivers needin addition to the ability to read and write the devicethe ability to perform various types of hardware control via the device driver. Most devices can perform operations beyond simple data transfers; user space must often beable to request, for example, that the device lock its door, eject its media, report error information, change a baud rate, or self destruct. These operationsare usually supported via the ioctl method, which implements the system call bythe same name.

    18. What is the use of the poll(file, polltable) API ?

    Applications that use nonblocking I/O often use the poll, select, and epoll system calls as well. poll, select, and epoll have essentially the same functionality: each allow a process to determine whether it can read from or write to one ormore open files without blocking. These calls can also block a process until any of a given set of file descriptors becomes available for reading or writing. Therefore, they are often used in applications that must use multiple input or output streams without getting stuck on any one of them.

    The select/poll system call allows userspace applications to wait for data to arrive on one or more ?ledescriptors.As it is not known which ?le/driver will be the ?rst to

    The poll/select system call will call the f_ops->poll method of all ?le descriptors. Each ->poll methodshould return whether data is available or not.If no ?le descriptor has any data available, then the poll/select call has to wait for data on those ?ledescriptors. It has to know about all wait queues that could be used to signal new data.

    poll_wait(file, q, pt)register wait queue q for an poll/select system call. The driver should wake upthat wait queue whennew data is available.

    19. What is the use of file->private_data in a device driver structure ?

    Most operations only have the ?le pointer to identify the actual ?le that is needed. Device drivers usuallymaintain their own structure to describe each device. If a device driver supports more than one devicethen it has to map from the ?le argument its own device structure. For that reason does struct filecontain the ?eld private_data. This is an opaque void* pointer that can be freely used by the devicedriver. It is usually initialized by the open operation and then used by all other ?le operations.

    20. What is a device number ?

    All hardware devices look like regular files; they can be opened, closed, read and written using the same, standard, system calls that are used to manipulate files. Every device in the system is represented by a file. For block (disk) and character devices, these device files are created by the mknod command and they describe the device using major and minor device numbers

    All devices controlled by the same device driver have a common major device numb

  • 7/27/2019 Linux Qstn

    7/8

    er. The minor device numbers are used to distinguish between different devices and their controllers.The major number is actually the offset into the kernel's device driver table, which tells the kernel what kind of device it is (whether it is a hard disk or aserial terminal). The minor number tells the kernel special characteristics of the device to be accessed. For example, the second hard disk has a different minor number than the first. The COM1 port has a different minor number than the COM2 port

    21. What are the two types of devices drivers from VFS point of view ?

    character device drivers and block device drivers.

    22. What are character devices ?23. How does the character device driver adds and remove itself from the kernel?What is the use of register_chrdev and unregister_chrdev ?

    There are two different types of device driver from the VFS point of view: character devices and blockdevices. Character devices operate on streams of bytes and usually do not support seeking while blockdevices only transfer ?xed sized blocks.

    The kernel maintains a list of currently available device drivers. A device driver can add itself to the listby calling register_chrdev. When doing so it must provide the major number whichshould beassociated with the new driver and the ?le operations which should be called when a user space processaccess this deviceThe major number will be dynamically assigned when register_chrdev will be called with a 0major_number. register_chrdev will return the actual major number in this case.Otherwise it willreturn 0 to indicate success. As usual, negative values indicate failure.

    To remove the driver from the system:unregister_chrdev(example_major, "name");

    24. What is the role of interrupts in a device driver ? How are interrupts handled in device driver ?25. How will you make interrupt handlers as fast as possible ?26. What are the types of softirqs ?27. Difference between Timer Softirq and Tasklet Softirq ?28. What are tasklets ? How are they activated ? when and How are they initialized ?29. What is task_struct and how are task states maintained ?30. What is rwlock and spinlock ? Briefly explain about both of them ?

    31. When will you use rwlock instead of spinlock ?32. Can spinlock/rwlock be used in Interrupt handler ?33. Tell about the Memory Layout of a Process in Linux .34. How will you trace the system calls made into the kernel of lInux ?35. What is mmap ? MMAP & malloc ? MMAP & brk ? MMAP adv & dis-adv.36. Tell the relation between Malloc and MMAP37. Advantages of MMAP over Read ?38. Tell the role of brk() in malloc / Tell the relation between heap and brk?39. Example of using MMAP and MUNMAP in C ?40. Tell about the method/steps in Linux Kernel Compilation.

  • 7/27/2019 Linux Qstn

    8/8

    41. What is Kmalloc and how does it differ from normal malloc ? or Why can't weuse malloc in kernel code ?42. What happens as soon as a packet arrives from the network in Linux ?43. What is a stack frame, stack pointer & frame pointer ?44. What is a profiler ? Which one have you used ?45. How do you determine the direction of stack growth ?