This action might not be possible to undo. Are you sure you want to continue?
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
so will be selected automatically or by ex plicitly giving the path $ LD_LIBRARY_PATH=/opt/gdbm-1.a" instead of default shared library by using -static $ gcc -Wall -static -I/opt/gdbm-1./a.3/include dbmain.8.c /opt/gdbm-1.so. 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.3/lib/libgdbm.3/lib/libgdbm. What should be done for Shared library based linking in gcc ? 7. Gcc by default will take preferance to select .3/lib $ export LD_LIBRARY_PATH $ .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.3/include/ -L/opt/gdbm-1. to load any of them specifically you need to specify the path. Because of these advantages gcc compiles programs to use shared libraries by default on most systems. Object files are produced by an assembler.out 8.8. or other language translator. When a program is compiled.8./a.a extention (static library file). there are two ways.a //static library based linking . . So the compiler stores them and their corresponding addresses in the symbol table. variable names are replaced by their (virtual) addre sses. one will be of . 6. The names aren't necessary for the program to run. 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.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.ary without recompiling the programs which use it (provided the interface to the library does not change).8.c -lgdbm .out we can force the compiler to link the execcutable with static library "lgdbm.3/include dbmain. compiler . for example to do shared linking.8.8.3/lib) it will fing two executable files of same n ame. 5. one is by using the enviro nment variable LD_LIBRARY_PATH since .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. if they are available./a. Whenever a static library libNAME.3/lib/ dbmain.so extention (shared library file ) and other will be of .out nking //first way to do shared librry based li $ gcc -Wall -I/opt/gdbm-1.out $ gcc -Wall -I/opt/gdbm-1. but they are essential for debugging.8.c /opt/gdbm-1. and used as input to the linker.
on SUSE. there is an easier way. The lin ker needs to know where the bar() function is actually defined inside libfoo. This is your entire kerne l source. If you d on't do that. you will need to answer the prompts for these options. especially if they are new and not currently included in your running kernel. my program will go to bar() directly. Imagine that I have a library (we'll call it libfoo. it's located under /proc/c onfig. Inside. Data . So the symbol table maps the "bar" symbol to some address. and replaces the fake address with the actual address.so. nothing remarkable.config and it's a line-delimited list of entries with k ernel parameters and values. Heap and Stack. Suppos e that my program wants to call the bar() function defined in libfoo. would be that the first time my program calls the bar() function. a command that will prompt you only f or new changes that are not marked in your existing configuration file. there's a handful of directories and files. finds the bar() functi on. It needs to be built against a configuration file that dictates what. You can use your own. Then. which ships with your distribution. make all/make oldconfig: The next step is to compile everything. make gconfig and make xconfig are graphical inte rfaces with mouse support. cd into it. with ncurses text interface inside terminal. By default. If your configuration does not contain answers for al l of the options. you will have to answer prompts in real time. You can run make oldconfig. The configuration file is a hidden file called . At this point.so. BSS . You can manually go through the configuration fil e and mark different entries. 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. which is invoking in different ways. it actually jumps to a fake address inside of ld. but this is tedious and not really useful if you'r e not a super-expert. you will not have a configuration if you've just download a kernel. This is done by running make all command. What are the ways in which linux kernel can be compiled ? After you download and extract the kernel archive.so) and I am writing a program that dynamically links with it.so (a shared library that acts as the linker). you will find the configuration under /boot. On Ubuntu and RedHat. The friendlier alternative is to launch a configuration me nu for kernel compilation.HEAP and STACK ? The memory is been divided in 4 major sections . Can you tell the memory layout based on Data. you need to toggle what settings you want to use.so. who and when will be included in the final kernel image. depending on wha t interface you want to use.g configure the kernel : make menuconfig/gconfig/xconfig: Now.so looks at the symbol tables of the libraries that I've linked with. make menuconfig is the simple option. in the future. and not go through ld. ld. . then. 9.This also applies to shared libraries.BSS. Al l storing is dependent on the storage class of the variable. The way that a modern program might work.
etc. such as the Linux kernel. then we'll add the device to the list of devices to probe for. Last thing is we need to list out device in the file that probess our device on startup.o file in the makefile (/dr ivers/net/makefile) so that our driver will be compiled. 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(. it reads the main kernel configurat ion file. The Kconfig has a basic configuration syntax that allows you to add confi guration options of various types. Other architecture s have the main configuration files located in their main directories.c. So the manual way to get this done is by first copying the kernel image: cp <new kernel image> /boot/ Next.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. so first we would have to add the reference to the probe function.). To add a driver into the kernel we need to list its .img-<kernel version> Update GRUB or GRUB2. create initrd file cd /boot mkinitrd -o initrd. EISA. Those configuration files also can i . create the initrd file an d change the GRUB menu or use the make install option to get things done automat ically. and create project configuration files (usually by the name of . Other advantages this method has. for example if you are adding a network driver the copy path will be /usr/src/kernel_src/drivers/net . for this we need to edit kconfig file as the configuration we claimed in the makefile. ISA. located in arch/i386/Kconfig for the i386 platform. With Kconfig. MCA. The Kconfig utilities know how to read and parse the configuration files. we have to make sure we can configure t he device when we configure the kernel. all network device are listed in driver/net/space. Kconfig: The Kconfig mechanism is today s standard configuration mechanism and it is used by leading open source projects. When the kernel configurator is run.T hen we need to add our actual device in the respective probe list(PCI. After adding the device to the Makefile. create dependencies and write a few lines of description. This main configuration file then includes other configuration files from the different s ubdirectories in the kernel directory tree.h for source files). or just use install option make modules_install install This last step needs root or sudo privileges. manually or by running update-grub. for example if we are adding a network device .config fo r Makefiles and autoconf.c and . parallel port. 11. SBu s. Busybox and u Clibc. are automatic menu generation (for both graphical and text based consoles).h) of our d river to the respective kernel-driver directory. and ease of configuration management. there is no need to specify any build flags to the project s make.
Without modules. Besides having larger kernels. how: While installing a module using modprobe. Any hardware device will only work if device driver i s loaded. 14. this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality. 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 . What is a kernel module ? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. we would have to build monolithic kernels and add new functionality directly into the ker nel image. which allows t he kernel to access hardware connected to the system. $ less /proc/modules $ lsmod 15. lsmod is a trivial program which nicely formats the contents of the /proc/module s. Do a ls here and you will get the list of currently available drivers 16. Linux use term modules for hardware device drivers.dep. With lsmod you can verify that device driver is loade d for particular hardware. They extend the functionality of the kernel without the need to reboot the system. This is an important task. modules. 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. . showing what kernel modules are currently loaded. the mmap function is implemented t hrough the remap_pfn_range kernel function. one type of module is the device driver. But modprobe can resolve dependency issues. How will you list the modules ? You need to use lsmod program which show the status of loaded modules in the Lin ux Kernel. it first determines whether there exists any dependencies by checking the file /lib/modules/<kernel-version>/modu les. 12. which provides a linear mapping of d evice memory into a user's address space. or zeros. th at is if the installation of a module requires some pre-requisites insmod is not able to resolve that. For example. How do you get the list of currently available drivers ? You can get a list of currently available drivers in /proc/devices. 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. 13.dep files is generated by the command depmod.within the kernel.nclude other configuration files as needed. DIFFERENCE : insmod doesn't have the capability to resolve dependency issues. This file contains entries for a module and its corresponding dependenc ies. What is the difference between insmod and modprobe ? both of them can be used to insert modules.
Most devic es can perform operations beyond simple data transfers. These operations are usually supported via the ioctl method. r eport error information.file. q. they are often used in applications that must use multiple input or ou tput streams without getting stuck on any one of them. For block (disk) and c haracter devices. system calls that are used to manipulate fi les. It has to know about all wait queues that could be used to signal n ew data. What is the use of ioctl(inode. If no ?le descriptor has any data available.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. 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. The driver should wake up that wait queue when new data is available. polltable) API ? Applications that use nonblocking I/O often use the poll. Every device in the system is represented by a file. closed. It is usually initialized by the open operation and then used by all oth er ?le operations. eject its media. or self destruct. The select/poll system call allows userspace applications to wait for data to ar rive on one or more ?le descriptors. pt) register wait queue q for an poll/select system call. Device drivers usually maintain their own structure to describe each device. For that reas on does struct file contain the ?eld private_data. Each ->poll method should return whether data is available or not. and epoll syst em calls as well. change a baud rate. for example. then the poll/select call has to wa it for data on those ?le descriptors. read a nd written using the same. 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. poll. 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.cmd. user space must often be able to request. they can be opened. select. If a device driver support s more than one device then it has to map from the ?le argument its own device structure. What is the use of the poll(file. These calls can also block a process until an y of a given set of file descriptors becomes available for reading or writing. This is an opaque void* pointer that can be freel y used by the device driver. select.17. which implements the system call by the same name. 18. poll_wait(file. What is a device number ? All hardware devices look like regular files. standard. that the device lock its door. 19. 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 . 20. T herefore.
Advantages of MMAP over Read ? 38. Example of using MMAP and MUNMAP in C ? 40. register_chrdev will return the actual major number in this case. What is the role of interrupts in a device driver ? How are interrupts handl ed in device driver ? 25. The kernel maintains a list of currently available device drivers. Otherwise it will return 0 to indicate success. 36. Character devices operate on streams of bytes and usually do not suppor t seeking while block devices only transfer ?xed sized blocks. the second hard disk has a different mino r number than the first. The minor device numbers are used to distinguish between different devices a nd their controllers. 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. Difference between Timer Softirq and Tasklet Softirq ? 28. How will you make interrupt handlers as fast as possible ? 26. To remove the driver from the system: unregister_chrdev(example_major. 24. What are character devices ? 23. "name"). 22. What are the types of softirqs ? 27. . For example. 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. A device driv er can add itself to the list by calling register_chrdev. Tell the relation between Malloc and MMAP 37. When will you use rwlock instead of spinlock ? 32. What are tasklets ? How are they activated ? when and How are they initializ ed ? 29. How will you trace the system calls made into the kernel of lInux ? 35.er. As usual. Tell the role of brk() in malloc / Tell the relation between heap and brk? 39. What is rwlock and spinlock ? Briefly explain about both of them ? 31. What are the two types of devices drivers from VFS point of view ? character device drivers and block device drivers. Tell about the method/steps in Linux Kernel Compilation. What is task_struct and how are task states maintained ? 30. negative values indicate failure. The major number is actually the offset into the kernel's device driver table. Tell about the Memory Layout of a Process in Linux . Can spinlock/rwlock be used in Interrupt handler ? 33. The COM1 port has a different minor number than the COM 2 port 21. 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. What is mmap ? MMAP & malloc ? MMAP & brk ? MMAP adv & dis-adv. 34.
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 ? . 45. 43.41. 44. use 42.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.