Frequently Asked Questions about Linux Kernel

     Recommend some good Linux kernel reference books?
  a. "Linux Device Drivers, Second Edition", there is a Chinese translation
  b. "Understanding the Linux Kernel, 2nd Edition"
  c. "Linux Kernel Source Code Scenario Analysis", divided into two volumes
  d. "Learning by doing-Linux kernel guide"

    How to get a certain version of the Linux kernel source code?
  a. http://www.kernel.org or ftp://ftp.kernel.org, this is the release website of the Linux kernel version.
  b. Many mirrors or local websites also provide downloads of some Linux kernel versions, and ftp search engines are mostly used.
  c. General Linux distributions such as Redhat will provide the corresponding kernel source code with the disk, but this source code is often changed and may be somewhat different from the standard Linux kernel of the same version.

    Recommend some source code viewing tools?
  a. Windows system can use Source Insight, Linux system can use Source Navigator.
  b. vim or emacs editor, with cscope, ctags, etags and other cross-reference tools.
  c. vim or emacs editor, with grep, egrep and other text search tools, but it is best to be familiar with the source code directory structure
  d. LXR, browse through the browser in the form of a web page, the installation is complicated, you can download it from http:/ /lxr.linux.no/ To download the tool, you can also directly visit http://lxr.linux.no/source/ to read the Linux kernel source code online.

    In which kernel source file is the definition of xx structure?
  a. Please use the source code viewing tool, see question 2.2.
  b. If you use text search tools such as grep, search mainly in the include/linux and include/asm directories.

    What do volatile and __volatile__ mean?
  a. Volatile is a keyword defined in the C language, and gcc defines __volatile__ for its needs, which has the same meaning as volatile.
  b. The original meaning of volatile is "volatile". As the speed of register access is faster than memory access, the compiler generally optimizes to reduce memory access. If the variable is modified with volatile, the compiler will not optimize the read and write operations of this variable, that is, directly fetch memory without going through the register buffer.
  c. __asm__ __volatile__ together instructs the compiler not to modify and optimize the subsequent assembly statement.

    What does do{ ...} while(0) mean?
  a. Mainly to avoid some errors that may occur when the macro expands in different situations.
  b. There is a detailed introduction on http://www.kernelnewbies.org/faq/.

    What is the definition of list_entry?
  a. The definition of list_entry is 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 it into the starting address of its host structure according to the list_head pointer ptr. The host structure is of type, and ptr is defined as member in its host structure member. As shown below:

  req>|start address of type object
  |
  |... ...
  ptr>|member member address pointed to by ptr pointer
  |
  |... ...

  ptr points to the position shown in the figure, through (unsigned long)(&((type*)0)->member) to get the difference between ptr and req, ptr subtracts this difference to get the type-type host structure The pointer req, the return type is (type*).

    What should be paid attention to in module programming?
  a. Add -c to
  the gcc compiler option b. Define two macros in the gcc compiler option: -DMODULE -D__KERENL__
  or define these two macros directly in the source file:
  #define MODULE
  #define __KERNEL__
  c. In the source file Include the module.h file:
  #include
  d. If you want to use the inline function, you need to add -O2 to the gcc compiler options

    Why does it show that the version does not match when insmoding a module?
  Assuming that the absolute path of the source directory of the kernel you are running is MyKernelSrcPath, add the option when compiling gcc:
  -I $MyKernelSrcPath/include

    Why does Unresolved Symbol appear?
  a. First check the file /proc/ksyms to see if the kernel has output this symbol. Different kernel versions such as 2.2 and 2.4 will have some changes in the output symbols.
  b. If the symbol output by the kernel has version control information such as the symbol printk_R12345678, the nature is the same as in question 3.2.

    Why is there a no license error?
  Add the following line in the source file:
  MODULE_LICENSE("GPL");

    Why can't I see the information printed with printk?
  a. The print message is restricted by the level. The message level can be set by printk, such as:
  printk("something"); /* where 0<=n<=7 */
  assuming that the message level of the console is m, when n is such a On the one hand, 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, to 8 you can use the following command:
  # echo "8"> /proc/sys/kernel/printk
  b. Use the dmesg command to see.
  c. When the system runs klogd and syslogd, the kernel messages will be distributed by klogd to syslogd, and syslogd will deal with it according to the configuration file /etc/syslog.conf. You can check the man pages of syslogd and syslog.conf for details.

    How to make and use patch files?
  The patch file is generated by the diff command. To use the patch file and the patch command, you can view the man page and info of the diff and patch.

    Can system calls be used in the kernel?
  a. Yes. There are examples of using system calls in the kernel source code, such as open(), execve(), etc.
  b. The use of system calls in the kernel must include the following two lines in the source file:
  #define __KERNEL_SYSCALLS__
  #include
  c. The relevant definitions of system calls used in the kernel can be found in the file include/asm/unistd.h.
  If the system call to be used is not defined in the file, you can add it according to its format.

    How to open and manipulate a file in the kernel?
  a. Use open(), read() and other system calls directly, see question 4.2.
  b. Use the filp_open() function to open the file and get the pointer fp of struct file *.
  Use the pointer fp for corresponding operations, such as reading a file can use fp->f_ops->read.
  Finally, use the filp_close() function to close the file.
  The filp_open() and filp_close() functions are defined in fs/open.c and declared in include/linux/fs.h.
  c. Write the wrapper function yourself, refer to the open_exec() and kernel_read() functions in the file fs/exec.c.
  There are some codes for reference on http://www.linuxforum.net/forum/showflat.php?Cat=&Board=linuxK&Number=363455&page=&view=&sb=&o=&vc=1.

    Why do EFAULT errors 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-mode programs, so it will verify that the read-write buffer does not exceed the upper limit of the user space, which is 0xC000 0000. But now in the kernel to read and write files, the address of the buffer in the kernel will exceed 0xC000 0000.
  b. Get the current fs before reading and writing the file: mm_segment_t old_fs=get_fs();
  and set the current fs as the kernel fs: set_fs(KERNEL_DS);
  restore the original fs after reading and writing the file: set_fs(old_fs);
  set_fs() , Get_fs() and other related macros are defined in the file include/asm/uaccess.h.

    Which file is the source code of xx command and xx library?
  a. In addition to the kernel, a system also needs a series of tools and commands such as shell, gcc, and a series of libraries such as C libraries. As an application, the source code is not in the kernel, and the corresponding source code needs to be downloaded separately.
  b. For the Redhat system, you can use the rqm -qf command to find the software package where a command is located, and then find the corresponding source code package to install.

Guess you like

Origin blog.csdn.net/bcbobo21cn/article/details/115014841