modules.txt 15 KB

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