Reprinted from Shui Mu Tsinghua You are welcome to add 1. Please recommend some good Linux kernel reference books? 2. Source Code Problems 2.1 How to obtain the Linux kernel source code of a certain version? 2.2 which file is the source code of the XX command and library? 2.3 what are the purposes of linux-2.x.x.tar.gz.sign files? 2.4 are some source code viewing tools recommended? 2.5 how to use Kernel patches such as patch-2.6.3? 2.6 How Do I count the lines of code in the Linux kernel? Which kernel source file is the definition of the 2.7 XX structure? 2.8 what does volatile and _ volatile _ mean? 2.9 do {...} while (0) What does it mean? 2.10 what is the definition of list_entry? 2.11 view the Linux kernel source code address on campus 3. module programming problems 3.1 What should I pay attention to when programming modules? 3.2 why does the version of the insmod module not match? 3.3 why is the unresolved symbol error? 3.4 why is the no license error? 3.5 why can't I see the information printed with printk? 4. kernel development problems 4.1 how to create and use patch files? 4.2 can the system call be used in the kernel? 4.3 how to open and operate a file in the kernel? 4.4 why does the efault (-14) error occur when reading and writing files in the kernel? 4.5 how to add a system call to the system? 4.6 how can I add my own driver to the kernel? 4.7 how can I get the CPU and MEM usage through the program? 4.8 how to obtain high-precision system time? 4.9 how to tune system performance? How does one perform mutex in the 4.10 kernel? 5. Other problems 5.1 how to learn Linux kernel? 5.2 how to download the essence area? 5.3 is the INIT process a core process? Is init the same as the initial process? 5.4 what is the use of initrd (. IMG? 6. About This FAQ 7. changelog 1. Please recommend some good Linux kernel reference books? A. Linux Device Drivers, 2nd edition, with Chinese Translations B. Understanding the Linux kernel, 2nd Edition C. Linux kernel source code scenario analysis, which is divided into two volumes: D. Learn while doing-Linux kernel Guide E. Source Code Analysis of Linux kernel 2.4 F. Linux kernel development G. IA-64 Linux kernel: design and implementation Note: the electronic version of a can be downloaded at http://www.oreilly.com/catalog/linuxdrive2; F and G are relatively new and difficult to buy in China. You can also query the article title "Linux kernel Book recommendation & introduction" on the page" 2. Source Code Problems 2.1 How to obtain the Linux kernel source code of a certain version? A. http://www.kernel.org or ftp://ftp.kernel.org, This is the release of the Linux kernel version Website. B. Many images or local websites also provide download of some Linux kernel versions and use ftp search engines. Mirror provided by linuxaid.com: Ftp://ftp.linuxaid.lkams.kernel.org/pub/mirrors/kernel/linux/kernel C. General Linux distributions such as RedHat will provide the corresponding kernel source code along with the disk, but this source generation The code is often changed, which is different from the standard Linux kernel. 2.2 which file is the source code of the XX command and library? A. In addition to the kernel, a system also needs a series of tools and commands, such as shell and GCC, and a C library. The source code of these applications is not in the kernel, and the corresponding source code needs to be downloaded separately. B. For the RedHat system, you can use the rpm-QF command to find the software package where a command is located, and then try again. Install the corresponding source code package. C. gnu.org has a lot of software source code such as Bash/glibc/binutils/make/GCC source code. D. You can search a http://www.rpmfind.net or http://www.google.com. 2.3 what are the purposes of linux-2.x.x.tar.gz.sign files? This is a digital signature file, used to verify the linux-2.x.x.tar.gz file. Modified by a third party. For more information, see http://www.kernel.org/signature.html. 2.4 are some source code viewing tools recommended? A. You can use source insight in windows and source navigator in Linux. B. vim or emacs editor, used with cross-index tools such as cssag, ctags, and etags. C. vim or emacs editor, combined with grep, egrep and other text search tools, but it is best to view the source code Familiar with the recording Structure D. lxr, Which is browsed through a browser in the form of a webpage. The installation is complex (Debian is easy to install, please Layout Search for lxr) Out-of-School: You can directly access http://lxr.linux.no/source/to read the linux code. On-campus: You can access http: // 10.214.14.127/lxr/HTTP/source/. If you need a specific version You can contact vatano and put it up with enough space. E. GNU global, which can be used in command lines or hypertext, similar to lxr, but easier. 2.5 how to use Kernel patches such as patch-2.6.3? A. kernel patch is generally for the previous version. For example, patch-2.6.3 is for Kernel 2.6.2. B. Kernel patches are generally mapped to changelog, such as patch-2.6.3 for ChangeLog-2.6.3. C. Search for the makefile keyword in the kernel patch to obtain relevant information, such as in patch-2.6.0: Diff-nru a/makefile B/makefile --- A/makefile wed Dec 17 19:00:07 2003 ++ B/makefile wed Dec 17 19:00:07 2003 @-+ @@ Version = 2 Patchlevel = 6 Sublevel = 0 -Extraversion =-test11 + Extraversion = D. Find the target kernel and use patch to upgrade the kernel. 2.6 How Do I count the lines of code in the Linux kernel? Try the following shell command: Find/usr/src/linux-2.x.x-> Which kernel source file is the definition of the 2.7 XX structure? A. Use the source code check tool. For details, see question 2.4. B. If grep and other text search tools are used, they are mainly in the include/Linux and include/ASM directories. Search. 2.8 what does volatile and _ volatile _ mean? A. Volatile is a keyword defined in C language. For the sake of its needs, GCC defines _ volatile __, which corresponds Volatile indicates the same meaning. B. Volatile is intended to be "changeable". Since the access register speed is faster than the access memory, the compiler generally All are optimized to reduce memory access. If the variable is modified with volatile, the compiler does not Read/write operations are optimized, that is, directly accessing the memory without using the register buffer. C. _ ASM _ volatile _ indicates that the compiler should not modify the Assembly statements after optimization. 2.9 do {...} while (0) What does it mean? A. It is mainly used to avoid some errors that may occur when macros are expanded in different situations. B. Detailed descriptions are provided at http://www.kernelnewbies.org/faq. 2.10 what is the definition of list_entry? A. list_entry is defined in the kernel source File Include/Linux/list. h: # Define list_entry (PTR, type, member )/ (Type *) (char *) (PTR)-(unsigned long) (& (type *) 0)-> member ))) B. Its function is to convert the pointer PTR of list_head to the starting address of its host structure. The host structure is Type, and PTR is defined as a Member member in its host structure. For example: Req --> | start address of type object | | ...... PTR --> | member Member address indicated by the PTR pointer | | ...... PTR points to the position shown in the figure. The PTR is obtained through (unsigned long) (& (type *) 0)-> member ). Difference between the value and req, PTR minus this difference to get the type host structure pointer req, return Type: (type *). 2.11 address for viewing Linux kernel source code on campus Http: // 10.214.14.127/lxr/HTTP/source/ 3. module programming problems 3.1 What should I pay attention to when programming modules? A. Add-C to the GCC compilation Option B. Define two Macros in the GCC compilation options:-dmodule-d1_kerenl __ Or define these two macros directly in the source file: # Define Module # DEFINE _ KERNEL __ C. Include the module. h file in the source file: # I nclude <Linux/module. h> D. Assume that the absolute path of the source code directory of the kernel you are running is mykernelsrcpath. During GCC compilation Added options: -I $ mykernelsrcpath/include (for example,-I/usr/src/Linux/include) Note that mykernelsrcpath must point to the current running system kernel (version consistent ), Successful compilation (to ensure the integrity of the source code directory), and already configured (to ensure that there are. config and Include/Linux/Autoconf. h file) kernel source code directory Note: Do not-I/usr/include/Linux E. If you want to use the inline function, add-O2 to the GCC compilation option. F. The init_module () function must return 0; otherwise, the device or resource busy error may occur. 3.2 why does the version of the insmod module not match? A. See figure 3.1-> d above. B. In some cases, insmod-F can be loaded successfully, but use it with caution. 3.3 why is the unresolved symbol error? A. first check the file/proc/ksyms to see if the kernel has output this symbol. Different kernel versions are shown in figure The symbols output by 2.2 and 2.4 may change. B. If the symbol output by the kernel carries version control information such as printk_r12345678, it is of the same nature Issue 3.2. C. Note: many versions do not output sys_call_table. Try another method! 3.4 why is the no license error? Add the following line to the source file (added to the file header and the end of the file ): Module_license ("GPL "); 3.5 why can't I see the information printed with printk? A. The message printing level is restricted. You can set the message level through printk, for example: Printk ("<n> something");/* where 0 <= n <= 7 */ Assume that the Message Level in the console is M. When n is less than m, the message is printed to the console. Otherwise, the message is not printed. In this way, you can increase the level of the message to be printed (the smaller the number, the higher the level ), On the other hand, you can change the message level of the console (from 1 to 8). For example, you can change the message level to 8 by using the following command: # Echo "8">/proc/sys/kernel/printk B. Run the dmesg command. C. When the system runs klogd and syslogd, kernel messages are distributed to syslogd by klogd, Syslogd will be processed according to the configuration file/etc/syslog. conf. For details, see syslogd And syslog. conf man pages. 4. kernel development problems 4.1 how to create and use patch files? A. the patch file is generated by the diff command. Use the patch command in the patch file. For details, see diff. And the man page and info of the patch. B. The common option combination of diff commands is urn, for example: Diff-urn Linux/my_linux/> mypatch. Diff 4.2 can the system call be used in the kernel? A. Yes. There are examples of using system calls in the kernel source code, such as open () and execve. B. To use system calls in the kernel, the following two lines must be included in the source file: # DEFINE _ kernel_syscils __ # I nclude <Linux/unistd. h> C. Check the include/ASM/unistd. h file by using the definition of system call in the kernel. If you want to use a system call that is not defined in this file, you can add it as needed. D. To use a system call in a module, you must define errno by yourself, for example: Int errno; The kernel defines errno in lib/errno. C, but this symbol is not exported, so you need Define errno to store system call error numbers. 4.3 how to open and operate a file in the kernel? A. directly use open (), read (), and other systems for calling. See question 4.2. B. Use the filp_open () function to open the file and obtain the pointer FP of struct file. Use the pointer FP for corresponding operations. For example, you can use FP-> f_ops-> Read to read a file. Finally, use the filp_close () function to close the file. The filp_open () and filp_close () functions are defined in FS/open. C and in include/Linux/fs. h. Statement. C. Write the packaging function by yourself. You can refer to the open_exec () and kernel_read () functions in file fs/exec. C. In http://www.linuxforum.net/forum/showflat.php? Cat = & board = linuxk & Number = 363455 & page = & view = & SB = & O = & Vc = 1 some code can be referenced. 4.4 why does the efault (-14) error occur when reading and writing files in the kernel? A. functions such as read () and write () provided by the Kernel File System are expected to serve user-state programs, Therefore, it verifies that the read/write buffer does not exceed the user space limit, that is, 0xc000 0000. But in the kernel To read and write files, the buffer address in the kernel will exceed 0xc000 0000. B. Obtain the current FS: mm_segment_t old_fs = get_fs (); Set the current FS as the kernel FS: set_fs (kernel_ds ); After reading and writing the file, restore the original FS: set_fs (old_fs ); Set_fs (), get_fs () and other related macros are defined in the File Include/ASM/uaccess. h. 4.5 how to add a system call to the system? Go http://www.linuxaid.com.cn/engineer/ideal/kernel/new_syscall.htm And http://www.xenotime.net/linux/syscall_ex. 4.6 how to add your own driver to the kernel? A. Go to http://www-900.ibm.com/developerWorks/cn/linux/kernel/l-kerconf/ Index.shtml: Let's take a look at the configuration compilation system of the entire kernel. B. Create Your Own Source Code Directories, files, makefiles, etc. C. Modify the upper-layer makefile and add your program to the kernel compilation system. D. Modify the upper-layer config. In and add your program to the Kernel configuration system. E. Make sure that your initialization function is called. There are two methods, one is explicit call, that is, in the original system Directly add a call to the initialization function. For example, the character device is in drivers/Char/MEM. C. Add the chr_dev_init () function, and the block device is in drivers/block/ll_rw_blk.c. Add it to the blk_dev_init () function. Another method is to use initcall and macro module_init to apply Your initialization function. After the operating system is initialized to a certain stage, it will automatically pass through init/Main. C. To call these initialization functions. Module_init macro in the file Include/Linux/init. h. 4.7 how can I get the CPU and MEM usage through the program? A. The final source of the information is files in the/proc directory, such as/proc/STAT. B. the commands in the procps package, such as top and vmstat, implement these functions. You can refer to the source code. C. procps packages can be obtained from the RedHat release, or from the http://www.surriel.com/procps/ . 4.8 how to obtain high-precision system time? A. in Linux, Jiffy is the basic unit of clock and is configured as 10 ms for general systems. In most cases Clock-related system calls are based on jiffy, so the accuracy is not too high. B. You can use registers such as TSC (time stamp counter) and RTC (real time clock) to obtain High-precision clock. For details, refer to the relevant hardware manual. 4.9 how to tune system performance? A. IBM developworks: Http://www-900.ibm.com/developerWorks/cn/linux/l-kperf/index.shtml Http://www-900.ibm.com/developerWorks/cn/linux/management/tune/index.sht ML B. Linux Performance Tuning project: http://linuxperf.nl.linux.org/ C. http://www.fixdown.com/article/article/724.htm How does one perform mutex in the 4.10 kernel? A. There are two mechanisms in the Linux kernel for mutual exclusion: semaphore and spinlock. Semaphore is used Thread sleep waits for resources. This generally assumes that it is impossible to predict when resources can be obtained. When used in SMP, it assumes that the waiting resources will be released immediately, so the resources are waiting cyclically. Semaphore can only be used in non-interrupted environments (typical interrupted environment processes include interruptions such as Timer Service programs, softirq, and so on) are mutually exclusive. The spinlock can be used for different processes. CPU processes are mutually exclusive. The spinlock is mainly used to protect short critical zones. Deadlock. B. semaphore is mutually exclusive through process scheduling. When the process requests to obtain semaphore, if When semaphore is idle, the process obtains semaphore, sets the flag, and returns the result. If semaphore is busy (Other users have obtained semaphore), the system constructs a waiting queue and uses the process scheduling mechanism Cheng sleep. When a process releases semaphore, the system waits for the queue to wake up a sleep . You can perform up () and down () operations on semaphore. For details, see include/ASM/semaphore. h. C. spinlock is mainly introduced for SMP mutex. If the request is idle Spinlock, set the flag, and return. If the spinlock has been obtained by other users and is in a busy state, The system will continue to occupy CPU resources, and will not stop querying the status of the spinlock until it gets the spinlock. 5. Other problems 5.1 how to learn Linux kernel? Please read the articles related to the kernel learning directory of this edition. 5.2 how to download the essence area? A. In addition to the download provided by 88, you can also download through the script -_- 5.3 is the INIT process a core process? Is init the same as the initial process? The Linux operating system fabricated an original process at the beginning of system initialization (the original process is initialized in the system ). After the initialization is completed, the original process will be created. Through the kernel_thread () function) out of the INIT process, the INIT process continues to initialize the system and Execve ("/sbin/init",...), so that init changes from the original core process to the user Process (user program/sbin/init. The pid of the init process is 1. PID is 0. All other processes are derived from the INIT process. You can see this point using the PS or pstree command. 5.4 what is the use of initrd (. IMG? A. initrd (. IMG) is a file system image, which generally contains some special hardware modules, especially memory Storage devices, such as SCSI/raid/ext3 modules, support as much hardware as possible while keeping the kernel small enough Device, which is often used by the installer. Initrd (. IMG) is not required either. As long as necessary modules are compiled into the kernel, you do not need initrd (. IMG ). B. When initrd (. IMG) is used, the general process of system guidance is as follows: 1) loader programs (such as Lilo and grub) load the kernel and initrd (. IMG) 2) The kernel decompress initrd (. IMG) as a normal ramdisk file system and hook it to the root partition. 3) Run linuxrc to load the hardware module. 4) After linuxrc is terminated, the real root file system will be mounted. 5) Complete the normal boot process on the root file system. For a normal system, run/sbin/init, In this case, the control will go to the normal startup process that everyone is familiar. For the installer, you only need to control To the first stage of the installation process, it will load the subsequent installation environment, and further initialize the device. And other operations. C. To use initrd (. IMG), the kernel must be configured to support initrd: Config_blk_dev_ram = y Config_blk_dev_initrd = y Then add the corresponding instructions in the loader script. For example, add a line in grub. conf: Initrd/boot/initrd-2.4.20.img D. Use the mkinitrd command to create the initrd (. IMG) file: Mkinitrd imagefilename kernelversion For example, for the 2.4.20 kernel, you can: Mkinitrd/boot/initrd-2.4.20.img 2.4.20 E. For more information, see documentation/initrd.txt and man mkiinitrd. Mkinitrd command execution details You can view the/sbin/mkinitrd (shell script) file directly. 6. About This FAQ This FAQ is based on previous articles of this version. We are particularly grateful to Mada and Pepp for their valuable comments! |