# 2020q1 Homework4 (kcalc)
contributed by < ```gpwork4u``` >
## 實驗環境
```shell
$ uname -a
Linux 5.3.0-28-generic #30~18.04.1-Ubuntu SMP Fri Jan 17 06:14:09 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
```
## 嘗試編譯 kcalc
發現會出現以下 error
``` Shell
$ make
/home/gp/linux2020/kcalc/livepatch-calc.c:58:15: error: implicit declaration of function ‘klp_register_patch’; did you mean ‘klp_enable_patch’? [-Werror=implicit-function-declaration]
int ret = klp_register_patch(&patch);
^~~~~~~~~~~~~~~~~~
klp_enable_patch
/home/gp/linux2020/kcalc/livepatch-calc.c:63:17: error: implicit declaration of function ‘klp_unregister_patch’; did you mean ‘mp_unregister_ioapic’? [-Werror=implicit-function-declaration]
WARN_ON(klp_unregister_patch(&patch));
```
error 指出沒有 ```klp_register_patch``` 和 ```klp_unregister_patch```
透過 [elixir.bootlin](https://elixir.bootlin.com/)
可以知道在 linux kernel v5.0 以前這兩個 function 被定義在 ```linux/livepatch.h``` 裡面
打開自己的 ```linux/livepatch.h``` ,發現 ```klp_register_patch``` 和 ```klp_unregister_patch``` 都找不到。
去 github 看看 livepatch.h 的變更可以發現有一條commit [livepatch: Simplify API by removing registration step](https://github.com/torvalds/linux/commit/958ef1e39d24d6cb8bf2a7406130a98c9564230f#)
可以發現其中在 [```include/linux/livepatch.h```](https://github.com/torvalds/linux/commit/958ef1e39d24d6cb8bf2a7406130a98c9564230f#diff-62ef9bf24ef24e28f07877f6557b5d26) 中,以下幾個 function 已經被移除了
``` diff
- int klp_register_patch(struct klp_patch *);
- int klp_unregister_patch(struct klp_patch *);
- int klp_disable_patch(struct klp_patch *);
```
從下面這段 commit message 可以知道```klp_register_patch()``` 的內容被移到 ```klp_enabled_patch()``` 中了。
>Therefore, remove the two step public API. All the checks and init calls
are moved from klp_register_patch() to klp_enabled_patch(). Also the patch
is automatically freed, including the sysfs interface when the transition
to the disabled state is completed.
在 [linux](https://github.com/torvalds/linux) 下的 [/samples/livepatch/livepatch-sample.c](https://github.com/torvalds/linux/blob/master/samples/livepatch/livepatch-sample.c) 中可以看到 ```livepatch_init``` 以及 ```livepatch_exit``` 的範例如下
``` cpp
static int livepatch_init(void)
{
return klp_enable_patch(&patch);
}
static void livepatch_exit(void)
{
}
```
將 kcalc/livepatch-calc.c 中的```livepatch_calc_init``` 以及 ```livepatch_calc_exit``` 作對應的改動即可