owned this note
owned this note
Published
Linked with GitHub
# 2019q1 Homework2 (fibdrv)
contributed by < `warrenanson` >
### Review by `bauuuu1021`
* macro 的行為並不是**呼叫**,而是**展開**(或取代),建議修正往後共筆的用詞
* 建議除了引用資料到共筆,也要加入對資料的**解讀**或相關**實驗**
## 測試給定的 fibdrv 模組
- 檢查 Linux 核心版本
```shell
$ uname -r
4.18.0-16-generic
```
- 安裝 linux-headers 套件
```shell
$ sudo apt install linux-headers-`uname -r`
```
- 確認 linux-headers 套件已正確安裝於開發環境
```shell
$ dpkg -L linux-headers-4.18.0-16-generic | grep "/lib/modules"
/lib/modules
/lib/modules/4.18.0-16-generic
/lib/modules/4.18.0-16-generic/build
```
- 檢驗目前的使用者身份
```shell
$ whoami
warrenanson
$ whoami
root
```
- 觀察產生的 fibdrv.ko 核心模組
```shell
$ modinfo fibdrv.ko
filename: /home/warrenanson/linux/fibdrv/fibdrv.ko
version: 0.1
description: Fibonacci engine driver
author: National Cheng Kung University, Taiwan
license: Dual MIT/GPL
srcversion: 24DC5FB7E7608AF16B0CC1F
depends:
retpoline: Y
name: fibdrv
vermagic: 4.18.0-16-generic SMP mod_unload
```
## 自我檢查清單
### fibdrv.c 裡頭的巨集
> 檔案 fibdrv.c 裡頭的 `MODULE_LICENSE`, `MODULE_AUTHOR`, `MODULE_DESCRIPTION`, `MODULE_VERSION` 等巨集做了什麼事,可以讓核心知曉呢? insmod 這命令背後,對應 Linux 核心內部有什麼操作呢?請舉出相關 Linux 核心原始碼並解讀
在 [include/linux/module.h](https://elixir.bootlin.com/linux/v4.18/source/include/linux/module.h#L205) 中可以發現`MODULE_LICENSE`, `MODULE_AUTHOR`, `MODULE_DESCRIPTION`, `MODULE_VERSION`這四個巨集
- **MODULE_LICENSE**
```c=199
#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)
```
- **MODULE_AUTHOR**
```c=201
/*
* Author(s), use "Name <email>" or just "Name", for multiple
* authors use multiple MODULE_AUTHOR() statements/lines.
*/
#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
```
- **MODULE_DESCRIPTION**
```c=207
/* What your module does. */
#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
```
- **MODULE_VERSION**
```c=236
#if defined(MODULE) || !defined(CONFIG_SYSFS)
#define MODULE_VERSION(_version) MODULE_INFO(version, _version)
#else
#define MODULE_VERSION(_version) \
static struct module_version_attribute ___modver_attr = { \
.mattr = { \
.attr = { \
.name = "version", \
.mode = S_IRUGO, \
}, \
.show = __modver_version_show, \
}, \
.module_name = KBUILD_MODNAME, \
.version = _version, \
}; \
static const struct module_version_attribute \
__used __attribute__ ((__section__ ("__modver"))) \
* __moduleparam_const __modver_attr = &___modver_attr
#endif
```
這些 macro 都呼叫另一個 macro `MODULE_INFO`
```c=160
/* Generic info of form tag = "info" */
#define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
```
而在 [include/linux/moduleparam.h](https://elixir.bootlin.com/linux/v5.0/source/include/linux/moduleparam.h#L21) 中可發現 `__MODULE_INFO`
```c=20
#ifdef MODULE
#define __MODULE_INFO(tag, name, info) \
static const char __UNIQUE_ID(name)[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= __stringify(tag) "=" info
#else /* !MODULE */
/* This struct is here for syntactic coherency, it is not used */
#define __MODULE_INFO(tag, name, info) \
struct __UNIQUE_ID(name) {}
#endif
```
繼續往下挖掘
- [include/linux/compiler-gcc.h](https://elixir.bootlin.com/linux/latest/source/include/linux/compiler-gcc.h#L71) 的 `__UNIQUE_ID`
```c=71
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
```
- [include/linux/compiler_types.h](https://elixir.bootlin.com/linux/v4.18/source/include/linux/compiler_types.h#L53) 的 `__PASTE`
```c=51
/* Indirect macros required for expanded argument pasting, eg. __LINE__. */
#define ___PASTE(a,b) a##b
#define __PASTE(a,b) ___PASTE(a,b)
```
- [include/linux/stringify.h](https://elixir.bootlin.com/linux/v4.18/source/include/linux/stringify.h#L10) 的`__stringify`
```c=9
#define __stringify_1(x...) #x
#define __stringify(x...) __stringify_1(x)
```
>這邊註解中有提到<br>
Indirect stringification. Doing two levels allows the parameter to be a macro itself. For example, compile with -DFOO=bar, __stringify(FOO) converts to "bar".<br>
> 之所以不直接 `#define __stringify(x...) #x` 是為了讓它可以自我呼叫[name=warrenanson]
- [Specifying Attributes of Variables](https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html#Variable-Attributes) 的 `__attribute__`
> The keyword `__attribute__` allows you to **specify special attributes** of variables or structure fields. This keyword is followed by an attribute specification inside **double parentheses**. Some attributes are currently defined generically for variables. Other attributes are defined for variables on particular target systems.
- [Specifying Attributes of Variables](https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html#Variable-Attributes) 的 `section`
> Normally, the compiler places the objects it generates in sections like data and bss. Sometimes, however, you need **additional sections**, or you need **certain particular variables** to appear in special sections, for example to map to special hardware. The section attribute specifies that a variable (or function) lives in a particular section.
- [Common Predefined Macros](https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html) 的 `__COUNTER__`
> This macro expands to sequential integral values **starting from 0**. In conjunction with the ## operator, this provides a convenient means to generate unique identifiers. Care must be taken to ensure that `__COUNTER__` is not expanded prior to inclusion of precompiled headers which use it. Otherwise, the precompiled headers will not be used.
- `unused`
> 當宣告的變數未被使用時,使 gcc 不會輸出 warning
> [name=warrenanson]
> [reference](http://magicjackting.pixnet.net/blog/post/173873062-c-%E8%AA%9E%E8%A8%80:%E6%B6%88%E9%99%A4-c-compiler-%E5%B0%8D%E6%9C%AA%E4%BD%BF%E7%94%A8%E4%B9%8B%E5%87%BD%E6%95%B8%E5%8F%83%E6%95%B8%E4%B9%8B%E5%91%8A)
- `aligned(n)`
> 在 memory 中以 n 個 bytes 對齊
> [name=warrenanson]
以 `MODULE_AUTHOR` 為例一一將其展開:
```c
MODULE_AUTHOR("National Cheng Kung University, Taiwan");
```
```c
MODULE_INFO(author, "National Cheng Kung University, Taiwan")
```
```c
__MODULE_INFO(author, author, "National Cheng Kung University, Taiwan")
```
```c
static const char __UNIQUE_ID(author)[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= __stringify(author) "=" "National Cheng Kung University, Taiwan"
```
```c
static const char __PASTE(__PASTE(__UNIQUE_ID_, author), __COUNTER__)[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= __stringify(author) "=" "National Cheng Kung University, Taiwan"
```
```c
static const char __UNIQUE_ID_author0[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= __stringify(author) "=" "National Cheng Kung University, Taiwan"
```
最後結果
```c
static const char __UNIQUE_ID_author0[] \
__used __attribute__((section(".modinfo"), unused, aligned(1))) \
= "author=National Cheng Kung University, Taiwan"
```
<br>
- insmod 操作可透過 strace 來追蹤
```=
$ sudo strace insmod fibdrv.ko
execve("/sbin/insmod", ["insmod", "fibdrv.ko"], 0x7ffd9a1bc638 /* 18 vars */) = 0
brk(NULL) = 0x56468dce7000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=72896, ...}) = 0
mmap(NULL, 72896, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5d105ba000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5d105b8000
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5d0ffb4000
mprotect(0x7f5d1019b000, 2097152, PROT_NONE) = 0
mmap(0x7f5d1039b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f5d1039b000
mmap(0x7f5d103a1000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f5d103a1000
close(3) = 0
arch_prctl(ARCH_SET_FS, 0x7f5d105b9540) = 0
mprotect(0x7f5d1039b000, 16384, PROT_READ) = 0
mprotect(0x56468c26e000, 8192, PROT_READ) = 0
mprotect(0x7f5d105cc000, 4096, PROT_READ) = 0
munmap(0x7f5d105ba000, 72896) = 0
brk(NULL) = 0x56468dce7000
brk(0x56468dd08000) = 0x56468dd08000
uname({sysname="Linux", nodename="warrenanson", ...}) = 0
openat(AT_FDCWD, "/lib/modules/4.18.0-16-generic/modules.softdep", O_RDONLY|O_CLOEXEC) = 3
fcntl(3, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)
fstat(3, {st_mode=S_IFREG|0644, st_size=511, ...}) = 0
read(3, "# Soft dependencies extracted fr"..., 4096) = 511
read(3, "", 4096) = 0
close(3) = 0
openat(AT_FDCWD, "/proc/cmdline", O_RDONLY|O_CLOEXEC) = 3
read(3, "BOOT_IMAGE=/boot/vmlinuz-4.18.0-"..., 4095) = 119
read(3, "", 3976) = 0
close(3) = 0
getcwd("/home/warrenanson/linux/fibdrv", 4096) = 31
stat("/home/warrenanson/linux/fibdrv/fibdrv.ko", {st_mode=S_IFREG|0644, st_size=8288, ...}) = 0
openat(AT_FDCWD, "/home/warrenanson/linux/fibdrv/fibdrv.ko", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=8288, ...}) = 0
mmap(NULL, 8288, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5d105c9000
finit_module(3, "", 0) = 0
munmap(0x7f5d105c9000, 8288) = 0
close(3) = 0
exit_group(0) = ?
+++ exited with 0 +++
```
### insmod 載入核心模組
> 當我們透過 insmod 去載入一個核心模組時,為何 module_init 所設定的函式得以執行呢?Linux 核心做了什麼事呢?
[include/linux/module.h](https://elixir.bootlin.com/linux/latest/source/include/linux/module.h#L128) 的 `module_init`
```c=128
/* Each module must use one module_init(). */
#define module_init(initfn) \
static inline initcall_t __maybe_unused __inittest(void) \
{ return initfn; } \
int init_module(void) __copy(initfn) __attribute__((alias(#initfn)));
```
`alias` 和 `copy` 的定義可以 [Common Function Attributes](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes) 中找到
- **alias ("target")**
> The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,
> ```
> void __f () { /* Do something. */; }
> void f () __attribute__ ((weak, alias ("__f")));
> ```
- **copy (function)**
> The copy attribute applies the set of attributes with which function has been declared to the declaration of the function to which the attribute is applied. The attribute is designed for libraries that define aliases or function resolvers that are expected to specify the same set of attributes as their targets. The copy attribute can be used with functions, variables, or types. However, the kind of symbol to which the attribute is applied (either function or variable) must match the kind of symbol to which the argument refers. The copy attribute copies only syntactic and semantic attributes but not attributes that affect a symbol’s linkage or visibility such as alias, visibility, or weak. The deprecated attribute is also not copied. See Common Type Attributes. See Common Variable Attributes.
> For example, the StrongAlias macro below makes use of the alias and copy attributes to define an alias named alloc for function allocate declared with attributes alloc_size, malloc, and nothrow. Thanks to the __typeof__ operator the alias has the same type as the target function. As a result of the copy attribute the alias also shares the same attributes as the target.
> ```
> #define StrongAlias(TagetFunc, AliasDecl) \
> extern __typeof__ (TargetFunc) AliasDecl \
> __attribute__ ((alias (#TargetFunc), copy (TargetFunc)));
> ```
> extern __attribute__ ((alloc_size (1), malloc, nothrow))
> void* allocate (size_t);
>StrongAlias (allocate, alloc);
所以可得知執行 `module_init` 就等同執行 user 所定義的 `initfn`
### 執行 `$ readelf -a fibdrv.ko`
> 試著執行 $ readelf -a fibdrv.ko, 觀察裡頭的資訊和原始程式碼及 modinfo 的關聯,搭配上述提問,解釋像 fibdrv.ko 這樣的 ELF 執行檔案是如何「植入」到 Linux 核心
### Fibonacci driver
> 這個 fibdrv 名稱取自 Fibonacci driver 的簡稱,儘管在這裡顯然是為了展示和教學用途而存在,但針對若干關鍵的應用場景,特別去撰寫 Linux 核心模組,仍有其意義,請找出 Linux 核心的案例並解讀。提示: 可參閱 Random numbers from CPU execution time jitter
### ktime 相關的 API
> 查閱 ktime 相關的 API,並找出使用案例 (需要有核心模組和簡化的程式碼來解說)
### clock_gettime 和 High Resolution TImers
> clock_gettime 和 High Resolution TImers (HRT) 的關聯為何?請參閱 POSIX 文件並搭配程式碼解說
### Linux Virtual File System
> fibdrv 如何透過 Linux Virtual File System 介面,讓計算出來的 Fibonacci 數列得以讓 userspace (使用者層級) 程式 (本例就是 client.c 程式) 得以存取呢?解釋原理,並撰寫或找出相似的 Linux 核心模組範例
### fibdrv.c 中的 mutex
> 注意到 fibdrv.c 存在著 `DEFINE_MUTEX`, `mutex_trylock`, `mutex_init`, `mutex_unlock`, `mutex_destroy` 等字樣,什麼場景中會需要呢?撰寫多執行緒的 userspace 程式來測試,觀察 Linux 核心模組若沒用到 mutex,到底會發生什麼問題
### clz / ctz 加速運算
>許多現代處理器提供了 clz / ctz 一類的指令,你知道如何透過演算法的調整,去加速費氏數列 運算嗎?請列出關鍵程式碼並解說