modules.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. In this document you will find information about:
  2. - how to build external modules
  3. - how to make your module use kbuild infrastructure
  4. - how kbuild will install a kernel
  5. - how to install modules in a non-standard location
  6. === Table of Contents
  7. === 1 Introduction
  8. === 2 How to build external modules
  9. --- 2.1 Building external modules
  10. --- 2.2 Available targets
  11. --- 2.3 Available options
  12. --- 2.4 Preparing the kernel tree for module build
  13. === 3. Example commands
  14. === 4. Creating a kbuild file for an external module
  15. === 5. Include files
  16. --- 5.1 How to include files from the kernel include dir
  17. --- 5.2 External modules using an include/ dir
  18. === 6. Module installation
  19. --- 6.1 INSTALL_MOD_PATH
  20. --- 6.2 INSTALL_MOD_DIR
  21. === 7. Module versioning
  22. === 8. Tips & Tricks
  23. --- 8.1 Testing for CONFIG_FOO_BAR
  24. === 1. Introduction
  25. kbuild includes functionality for building modules both
  26. within the kernel source tree and outside the kernel source tree.
  27. The latter is usually referred to as external modules and is used
  28. both during development and for modules that are not planned to be
  29. included in the kernel tree.
  30. What is covered within this file is mainly information to authors
  31. of modules. The author of an external modules should supply
  32. a makefile that hides most of the complexity so one only has to type
  33. 'make' to buld the module. A complete example will be present in
  34. chapter ¤. Creating a kbuild file for an external module".
  35. === 2. How to build external modules
  36. kbuild offers functionality to build external modules, with the
  37. prerequisite that there is a pre-built kernel available with full source.
  38. A subset of the targets available when building the kernel is available
  39. when building an external module.
  40. --- 2.1 Building external modules
  41. Use the following command to build an external module:
  42. make -C <path-to-kernel> M=`pwd`
  43. For the running kernel use:
  44. make -C /lib/modules/`uname -r`/build M=`pwd`
  45. For the above command to succeed the kernel must have been built with
  46. modules enabled.
  47. To install the modules that were just built:
  48. make -C <path-to-kernel> M=`pwd` modules_install
  49. More complex examples later, the above should get you going.
  50. --- 2.2 Available targets
  51. $KDIR refers to path to kernel source top-level directory
  52. make -C $KDIR M=`pwd`
  53. Will build the module(s) located in current directory.
  54. All output files will be located in the same directory
  55. as the module source.
  56. No attempts are made to update the kernel source, and it is
  57. a precondition that a successful make has been executed
  58. for the kernel.
  59. make -C $KDIR M=`pwd` modules
  60. The modules target is implied when no target is given.
  61. Same functionality as if no target was specified.
  62. See description above.
  63. make -C $KDIR M=$PWD modules_install
  64. Install the external module(s).
  65. Installation default is in /lib/modules/<kernel-version>/extra,
  66. but may be prefixed with INSTALL_MOD_PATH - see separate chater.
  67. make -C $KDIR M=$PWD clean
  68. Remove all generated files for the module - the kernel
  69. source directory is not moddified.
  70. make -C $KDIR M=`pwd` help
  71. help will list the available target when building external
  72. modules.
  73. --- 2.3 Available options:
  74. $KDIR refer to path to kernel src
  75. make -C $KDIR
  76. Used to specify where to find the kernel source.
  77. '$KDIR' represent the directory where the kernel source is.
  78. Make will actually change directory to the specified directory
  79. when executed but change back when finished.
  80. make -C $KDIR M=`pwd`
  81. M= is used to tell kbuild that an external module is
  82. being built.
  83. The option given to M= is the directory where the external
  84. module (kbuild file) is located.
  85. When an external module is being built only a subset of the
  86. usual targets are available.
  87. make -C $KDIR SUBDIRS=`pwd`
  88. Same as M=. The SUBDIRS= syntax is kept for backwards
  89. compatibility.
  90. --- 2.4 Preparing the kernel tree for module build
  91. To make sure the kernel contains the information required to
  92. build external modules the target 'modules_prepare' must be used.
  93. 'module_prepare' solely exists as a simple way to prepare
  94. a kernel for building external modules.
  95. Note: modules_prepare will not build Module.symvers even if
  96. CONFIG_MODULEVERSIONING is set.
  97. Therefore a full kernel build needs to be executed to make
  98. module versioning work.
  99. === 3. Example commands
  100. This example shows the actual commands to be executed when building
  101. an external module for the currently running kernel.
  102. In the example below the distribution is supposed to use the
  103. facility to locate output files for a kernel compile in a different
  104. directory than the kernel source - but the examples will also work
  105. when the source and the output files are mixed in the same directory.
  106. # Kernel source
  107. /lib/modules/<kernel-version>/source -> /usr/src/linux-<version>
  108. # Output from kernel compile
  109. /lib/modules/<kernel-version>/build -> /usr/src/linux-<version>-up
  110. Change to the directory where the kbuild file is located and execute
  111. the following commands to build the module:
  112. cd /home/user/src/module
  113. make -C /usr/src/`uname -r`/source \
  114. O=/lib/modules/`uname-r`/build \
  115. M=`pwd`
  116. Then to install the module use the following command:
  117. make -C /usr/src/`uname -r`/source \
  118. O=/lib/modules/`uname-r`/build \
  119. M=`pwd` \
  120. modules_install
  121. If one looks closely you will see that this is the same commands as
  122. listed before - with the directories spelled out.
  123. The above are rather long commands, and the following chapter
  124. lists a few tricks to make it all easier.
  125. === 4. Creating a kbuild file for an external module
  126. kbuild is the build system for the kernel, and external modules
  127. must use kbuild to stay compatible with changes in the build system
  128. and to pick up the right flags to gcc etc.
  129. The kbuild file used as input shall follow the syntax described
  130. in Documentation/kbuild/makefiles.txt. This chapter will introduce a few
  131. more tricks to be used when dealing with external modules.
  132. In the following a Makefile will be created for a module with the
  133. following files:
  134. 8123_if.c
  135. 8123_if.h
  136. 8123_pci.c
  137. 8123_bin.o_shipped <= Binary blob
  138. --- 4.1 Shared Makefile for module and kernel
  139. An external module always includes a wrapper Makefile supporting
  140. building the module using 'make' with no arguments.
  141. The Makefile provided will most likely include additional
  142. functionality such as test targets etc. and this part shall
  143. be filtered away from kbuild since it may impact kbuild if
  144. name clashes occurs.
  145. Example 1:
  146. --> filename: Makefile
  147. ifneq ($(KERNELRELEASE),)
  148. # kbuild part of makefile
  149. obj-m := 8123.o
  150. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  151. else
  152. # Normal Makefile
  153. KERNELDIR := /lib/modules/`uname -r`/build
  154. all::
  155. $(MAKE) -C $KERNELDIR M=`pwd` $@
  156. # Module specific targets
  157. genbin:
  158. echo "X" > 8123_bini.o_shipped
  159. endif
  160. In example 1 the check for KERNELRELEASE is used to separate
  161. the two parts of the Makefile. kbuild will only see the two
  162. assignments whereas make will see everything except the two
  163. kbuild assignments.
  164. In recent versions of the kernel, kbuild will look for a file named
  165. Kbuild and as second option look for a file named Makefile.
  166. Utilising the Kbuild file makes us split up the Makefile in example 1
  167. into two files as shown in example 2:
  168. Example 2:
  169. --> filename: Kbuild
  170. obj-m := 8123.o
  171. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  172. --> filename: Makefile
  173. KERNELDIR := /lib/modules/`uname -r`/build
  174. all::
  175. $(MAKE) -C $KERNELDIR M=`pwd` $@
  176. # Module specific targets
  177. genbin:
  178. echo "X" > 8123_bin_shipped
  179. In example 2 we are down to two fairly simple files and for simple
  180. files as used in this example the split is questionable. But some
  181. external modules use Makefiles of several hundred lines and here it
  182. really pays off to separate the kbuild part from the rest.
  183. Example 3 shows a backward compatible version.
  184. Example 3:
  185. --> filename: Kbuild
  186. obj-m := 8123.o
  187. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  188. --> filename: Makefile
  189. ifneq ($(KERNELRELEASE),)
  190. include Kbuild
  191. else
  192. # Normal Makefile
  193. KERNELDIR := /lib/modules/`uname -r`/build
  194. all::
  195. $(MAKE) -C $KERNELDIR M=`pwd` $@
  196. # Module specific targets
  197. genbin:
  198. echo "X" > 8123_bin_shipped
  199. endif
  200. The trick here is to include the Kbuild file from Makefile so
  201. if an older version of kbuild picks up the Makefile the Kbuild
  202. file will be included.
  203. --- 4.2 Binary blobs included in a module
  204. Some external modules needs to include a .o as a blob. kbuild
  205. has support for this, but requires the blob file to be named
  206. <filename>_shipped. In our example the blob is named
  207. 8123_bin.o_shipped and when the kbuild rules kick in the file
  208. 8123_bin.o is created as a simple copy off the 8213_bin.o_shipped file
  209. with the _shipped part stripped of the filename.
  210. This allows the 8123_bin.o filename to be used in the assignment to
  211. the module.
  212. Example 4:
  213. obj-m := 8123.o
  214. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  215. In example 4 there is no distinction between the ordinary .c/.h files
  216. and the binary file. But kbuild will pick up different rules to create
  217. the .o file.
  218. === 5. Include files
  219. Include files are a necessity when a .c file uses something from another .c
  220. files (not strictly in the sense of .c but if good programming practice is
  221. used). Any module that consist of more than one .c file will have a .h file
  222. for one of the .c files.
  223. - If the .h file only describes a module internal interface then the .h file
  224. shall be placed in the same directory as the .c files.
  225. - If the .h files describe an interface used by other parts of the kernel
  226. located in different directories, the .h files shall be located in
  227. include/linux/ or other include/ directories as appropriate.
  228. One exception for this rule is larger subsystems that have their own directory
  229. under include/ such as include/scsi. Another exception is arch-specific
  230. .h files which are located under include/asm-$(ARCH)/*.
  231. External modules have a tendency to locate include files in a separate include/
  232. directory and therefore needs to deal with this in their kbuild file.
  233. --- 5.1 How to include files from the kernel include dir
  234. When a module needs to include a file from include/linux/ then one
  235. just uses:
  236. #include <linux/modules.h>
  237. kbuild will make sure to add options to gcc so the relevant
  238. directories are searched.
  239. Likewise for .h files placed in the same directory as the .c file.
  240. #include "8123_if.h"
  241. will do the job.
  242. --- 5.2 External modules using an include/ dir
  243. External modules often locate their .h files in a separate include/
  244. directory although this is not usual kernel style. When an external
  245. module uses an include/ dir then kbuild needs to be told so.
  246. The trick here is to use either EXTRA_CFLAGS (take effect for all .c
  247. files) or CFLAGS_$F.o (take effect only for a single file).
  248. In our example if we move 8123_if.h to a subdirectory named include/
  249. the resulting Kbuild file would look like:
  250. --> filename: Kbuild
  251. obj-m := 8123.o
  252. EXTRA_CFLAGS := -Iinclude
  253. 8123-y := 8123_if.o 8123_pci.o 8123_bin.o
  254. Note that in the assingment there is no space between -I and the path.
  255. This is a kbuild limitation and no space must be present.
  256. === 6. Module installation
  257. Modules which are included in the kernel is installed in the directory:
  258. /lib/modules/$(KERNELRELEASE)/kernel
  259. External modules are installed in the directory:
  260. /lib/modules/$(KERNELRELEASE)/extra
  261. --- 6.1 INSTALL_MOD_PATH
  262. Above are the default directories, but as always some level of
  263. customization is possible. One can prefix the path using the variable
  264. INSTALL_MOD_PATH:
  265. $ make INSTALL_MOD_PATH=/frodo modules_install
  266. => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel
  267. INSTALL_MOD_PATH may be set as an ordinary shell variable or as in the
  268. example above be specified on the commandline when calling make.
  269. INSTALL_MOD_PATH has effect both when installing modules included in
  270. the kernel as well as when installing external modules.
  271. --- 6.2 INSTALL_MOD_DIR
  272. When installing external modules they are default installed in a
  273. directory under /lib/modules/$(KERNELRELEASE)/extra, but one may wish
  274. to locate modules for a specific functionality in a separate
  275. directory. For this purpose one can use INSTALL_MOD_DIR to specify an
  276. alternative name than 'extra'.
  277. $ make INSTALL_MOD_DIR=gandalf -C KERNELDIR \
  278. M=`pwd` modules_install
  279. => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf
  280. === 7. Module versioning
  281. Module versioning are enabled by the CONFIG_MODVERSIONS tag.
  282. Module versioning is used as a simple ABI consistency check. The Module
  283. versioning creates a CRC value of the full prototype for an exported symbol and
  284. when a module is loaded/used then the CRC values contained in the kernel are
  285. compared with similar values in the module. If they are not equal then the
  286. kernel refuses to load the module.
  287. During a kernel build a file named Module.symvers will be generated. This
  288. file includes the symbol version of all symbols within the kernel. If the
  289. Module.symvers file is saved from the last full kernel compile one does not
  290. have to do a full kernel compile to build a module version's compatible module.
  291. === 8. Tips & Tricks
  292. --- 8.1 Testing for CONFIG_FOO_BAR
  293. Modules often needs to check for certain CONFIG_ options to decide if
  294. a specific feature shall be included in the module. When kbuild is used
  295. this is done by referencing the CONFIG_ variable directly.
  296. #fs/ext2/Makefile
  297. obj-$(CONFIG_EXT2_FS) += ext2.o
  298. ext2-y := balloc.o bitmap.o dir.o
  299. ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o
  300. External modules have traditionally used grep to check for specific
  301. CONFIG_ settings directly in .config. This usage is broken.
  302. As introduced before external modules shall use kbuild when building
  303. and therefore can use the same methods as in-kernel modules when testing
  304. for CONFIG_ definitions.