# 2024q1 Homework6 (integration) contributed by < `ChenFuHuangKye` > ## 閱讀 <Linux 核心模組運作原理> 編譯核心模組的命令 ``` $ make -C /lib/modules/`uname -r`/build M=`pwd` modules ``` ``make -C /lib/modules/$`uname -r`/build M=$`pwd` modules`` 的作用是在指定的核心目錄``/lib/modules/$`uname -r`/build`` 執行 make 。這裡使用 `-c` 參數是用來切換到該目錄,並在此目錄執行 Makefile 。 `` M=$`pwd` `` 是告訴 `/build` 底下的 Makefile ,我的程式當前所在位置。 `modules` 是一個 target ,用於編譯目錄中所有被標記為模組的對象 (由 `obj-m` 指定)。 總結來說 ``make -C /lib/modules/$`uname -r`/build M=$`pwd` modules`` 可以利用核心的目錄下的 Makefile 編譯當前目錄的模組,且允許在自己的目錄下維護獨立的 Makefile, 無須更改核心中的 Makefile。 接著查看 `insmod` 手冊 ``` NAME insmod - Simple program to insert a module into the Linux Kernel SYNOPSIS insmod [filename] [module options...] DESCRIPTION insmod is a trivial program to insert a module into the kernel. Most users will want to use modprobe(8) instead, which is more clever and can handle module dependencies. Only the most general of error messages are reported: as the work of trying to link the module is now done inside the kernel, the dmesg usually gives more information about errors. ``` `insmod` 的描述為插入模組到 Linux 核心。 利用 `sudo strace insmod fibdrv.ko` 可以發現會呼叫 `finit_module` 。`finit_module` 呼叫 [`idempotent_init_module`](https://elixir.bootlin.com/linux/latest/source/kernel/module/main.c#L3194) , idempotent_init_module 會在呼叫 [`init_module_from_file`](https://elixir.bootlin.com/linux/latest/source/kernel/module/main.c#L3131) ,最後在利用 init_module_from_file 呼叫 [load_module](https://elixir.bootlin.com/linux/latest/source/kernel/module/main.c#L2836) 。