Sunteți pe pagina 1din 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: L KMs get loaded much differently Before Linux 2.6, a user space program would interpret the ELF object (.o) file and do all the work of linking it to the running kernel, generating a finished b inary image. The program would pass that image to the kernel and the kernel woul d do little more than stick it in memory. In Linux 2.6, the kernel does the link ing. A user space program passes the contents of the ELF object file directly to the kernel. For this to work, the ELF object image must contain additional info rmation. To identify this particular kind of ELF object file, we name the file w ith suffix ".ko" ("kernel object") instead of ".o" For example, the serial devic e driver that in Linux 2.4 lived in the file serial.o in Linux 2.6 lives in the file serial.ko. So there is a whole new modutils package for use with Linux 2.6. In it, insmod i s a trivial program, as compared to the full blown linker of the Linux 2.4 versi on. Also, the procedure to build an LKM is somewhat harder. To make a .ko file, you start with a regular .o file. You run the program modpost (which comes with the Linux source code) on it to create a C source file that describes the additional sections 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 make a .ko file. The .mod object file contains the name that the LKM instance will have when you load 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 share d libraries. Static libraries are the .a files seen earlier. When a program is lin ked against a static library, the machine code from the object files for any ext ernal functions used by the program is copied from the library into the final ex ecutable. This process of linking is called static linking. Shared libraries are handled with a more advanced form of linking, which makes t he executable file smaller. They use the extension .so , which stands for shared ob ject. An executable file linked against a shared library contains only a small t able of the functions it requires, instead of the complete machine code from the object files for the external functions. Before the executable file starts runn ing, 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 dy namic linking. 4. What are the advantages of Dynamic linking or Shared libraries ? Dynamic linking makes executable files smaller and saves disk space, because one copy of a library can be shared between multiple programs. Most operating syste ms also provide a virtual memory mechanism which allows one copy of a shared lib rary in physical memory to be used by all running programs, saving memory as wel l as disk space. Furthermore, shared libraries make it possible to update a libr

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

5. Does gcc search for both static and shared libraries ? Which is searched init ially by gcc compiler ? yes both are searched but firstly shared libraires are searched by gcc. Whenever a static library libNAME.a would be used for linking with the option -lNAME the c ompiler 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 p ath(for example/opt/ gdbm-1.8.3/lib) it will fing two executable files of same n ame, one will be of .so extention (shared library file ) and other will be of .a extention (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 ex plicitly giving the path $ LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib $ export LD_LIBRARY_PATH $ ./a.out nking

//first way to do shared librry based li

$ gcc -Wall -I/opt/gdbm-1.8.3/include dbmain.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 execu table /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/include dbmain.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 usua lly 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) addre sses. The names aren't necessary for the program to run, but they are essential for debugging. So the compiler stores them and their corresponding addresses in the symbol table.

This also applies to shared libraries. Imagine that I have a library (we'll call it libfoo.so) and I am writing a program that dynamically links with it. Suppos e that my program wants to call the bar() function defined in libfoo.so. The lin ker 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 my program calls the bar() function, it actually jumps to a fake address inside of ld.so (a shared library that acts as the linker). At this point, ld.so looks at the symbol tables of the libraries that I've linked with, finds the bar() functi on, 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. Al l storing is dependent on the storage class of the variable, Global and static variables = BSS initialized data = Data Local variables = stack Dynamic memory = heap Three types of allocation/deallocation strategies Global and static variables (BSS) = program startup/termination Local variables (stack) = function entry/return Dynamic 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 a handful of directories and files, nothing remarkable. This is your entire kerne l 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 file is 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/c onfig.g configure the kernel : make menuconfig/gconfig/xconfig: Now, you need to toggle what settings you want to use. You can manually go through the configuration fil e and mark different entries, but this is tedious and not really useful if you'r e not a super-expert. The friendlier alternative is to launch a configuration me nu for kernel compilation, which is invoking in different ways, depending on wha t interface you want to use. make menuconfig is the simple option, with ncurses text 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 by running make all command. If your configuration does not contain answers for al l of the options, especially if they are new and not currently included in your running kernel, you will need to answer the prompts for these options. there is an easier way, You can run make oldconfig, a command that will prompt you only f or new changes that are not marked in your existing configuration file. If you d on't do that, you will have to answer prompts in real time.

Copying the kernel image in boot: Once make all/make oldcofig is done , you will need to either manually copy the new kernel to /boot, create the initrd file an d change the GRUB menu or use the make install option to get things done automat ically. So the manual way to get this done is by first copying the kernel image: cp <new kernel image> /boot/ Next, create initrd file cd /boot mkinitrd -o initrd.img-<kernel version> 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 a network 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 (/dr ivers/net/makefile) so that our driver will be compiled. After adding the device to the Makefile, we have to make sure we can configure t he device when we configure the kernel, for this we need to edit kconfig file as the configuration we claimed in the makefile. Last thing is we need to list out device in the file that probess our device on startup, 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 the probe function, then we'll add the device to the list of devices to probe for.T hen we need to add our actual device in the respective probe list(PCI, EISA, SBu s, MCA, ISA, parallel port, etc.). Kconfig: The Kconfig mechanism is today s standard configuration mechanism and it is used by leading open source projects, such as the Linux kernel, Busybox and u Clibc. The Kconfig has a basic configuration syntax that allows you to add confi guration options of various types, create dependencies and write a few lines of description. The Kconfig utilities know how to read and parse the configuration files, 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), and ease of configuration management. With Kconfig, there is no need to specify any build flags to the project s make. When the kernel configurator is run, it reads the main kernel configurat ion file, located in arch/i386/Kconfig for the i386 platform. Other architecture s have the main configuration files located in their main directories. This main configuration file then includes other configuration files from the different s ubdirectories in the kernel directory tree. Those configuration files also can i

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 upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows t he kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the ker nel image. Besides having larger kernels, this has the disadvantage of requiring us 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, th at is if the installation of a module requires some pre-requisites insmod is not able to resolve that. But modprobe can resolve dependency issues. how: While installing a module using modprobe, it first determines whether there exists any dependencies by checking the file /lib/modules/<kernel-version>/modu les.dep. This file contains entries for a module and its corresponding dependenc ies. 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 Lin ux Kernel. Linux use term modules for hardware device drivers. lsmod is a trivial program which nicely formats the contents of the /proc/module s, showing what kernel modules are currently loaded. This is an important task. With lsmod you can verify that device driver is loade d for particular hardware. Any hardware device will only work if device driver i s 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 here and you will get the list of currently available drivers 16. How will you Access userspace memory from kernel ? What are the various meth ods ? access_ok Checks the validity of the user space memory pointer get_user Gets a simple variable from user space put_user Puts a simple variable to user space clear_user Clears, or zeros, a block in user space copy_to_user Copies a block of data from the kernel to user space copy_from_user Copies a block of data from user space to the kernel strnlen_user Gets the size of a string buffer in user space strncpy_from_user Copies a string from user space into the kernel not only mapping is allowed from kernel side , there are mechanishm such as shar ed memory technique that can be used to access userspace memory from kernel , fo r example a userspace appication can allocate a virtual memory using mmap and th at can be shared by kernel,within the kernel, the mmap function is implemented t hrough the remap_pfn_range kernel function, which provides a linear mapping of d evice memory into a user's address space.

17. What is the use of ioctl(inode,file,cmd,arg) ApI ? Most drivers need in addition to the ability to read and write the device the abilit y to perform various types of hardware control via the device driver. Most devic es can perform operations beyond simple data transfers; user space must often be able to request, for example, that the device lock its door, eject its media, r eport error information, change a baud rate, or self destruct. These operations are usually supported via the ioctl method, which implements the system call by the 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 syst em calls as well. poll, select, and epoll have essentially the same functionalit y: each allow a process to determine whether it can read from or write to one or more open files without blocking. These calls can also block a process until an y of a given set of file descriptors becomes available for reading or writing. T herefore, they are often used in applications that must use multiple input or ou tput streams without getting stuck on any one of them. The select/poll system call allows userspace applications to wait for data to ar rive on one or more ?le descriptors. 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 descript ors. Each ->poll method should return whether data is available or not. If no ?le descriptor has any data available, then the poll/select call has to wa it for data on those ?le descriptors. It has to know about all wait queues that could be used to signal n ew data. poll_wait(file, q, pt) register wait queue q for an poll/select system call. The driver should wake up that wait queue when new 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 nee ded. Device drivers usually maintain their own structure to describe each device. If a device driver support s more than one device then it has to map from the ?le argument its own device structure. For that reas on does struct file contain the ?eld private_data. This is an opaque void* pointer that can be freel y used by the device driver. It is usually initialized by the open operation and then used by all oth er ?le operations. 20. What is a device number ? All hardware devices look like regular files; they can be opened, closed, read a nd written using the same, standard, system calls that are used to manipulate fi les. Every device in the system is represented by a file. For block (disk) and c haracter devices, these device files are created by the mknod command and they d escribe the device using major and minor device numbers All devices controlled by the same device driver have a common major device numb

er. The minor device numbers are used to distinguish between different devices a nd their controllers. The major number is actually the offset into the kernel's device driver table, w hich tells the kernel what kind of device it is (whether it is a hard disk or a serial terminal). The minor number tells the kernel special characteristics of t he device to be accessed. For example, the second hard disk has a different mino r number than the first. The COM1 port has a different minor number than the COM 2 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: chara cter devices and block devices. Character devices operate on streams of bytes and usually do not suppor t seeking while block devices only transfer ?xed sized blocks. The kernel maintains a list of currently available device drivers. A device driv er can add itself to the list by calling register_chrdev. When doing so it must provide the major number which should be associated with the new driver and the ?le operations which should be called whe n a user space process access this device The major number will be dynamically assigned when register_chrdev will be calle d with a 0 major_number. register_chrdev will return the actual major number in this case. Otherwise it will return 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 handl ed 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 initializ ed ? 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 MMAP 37. 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.

41. use 42. 43. 44. 45.

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

S-ar putea să vă placă și