or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
![image alt](https:// "title") | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | Emoji list | ||
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
2019q1 Homework2 (fibdrv)
contributed by <
JulianATA
>Reviewed by
jeffcarl67
mutex
的實驗中, 可以加入一份使用 mutex 相關操作的程式碼, 實際比較在有無 mutex 的情況下程式的行為有何不同Environmet
自我檢查清單
檔案 fibdrv.c 裡頭的 MODULE_LICENSE, MODULE_AUTHOR, MODULE_DESCRIPTION, MODULE_VERSION 等巨集做了什麼事,可以讓核心知曉呢? insmod 這命令背後,對應 Linux 核心內部有什麼操作呢?請舉出相關 Linux 核心原始碼並解讀
MODULE_LICENSE
In
fibdrv.c
:We used these macros to define our modules.
Take a look at
linux/module.h
.(use License as example):Found
__MODULE_INFO
inlinux/moduleparam.h
:It'll like config the Module information if module has been defined.
insmod
It's a program with following steps
init_module()
to tell kernel that a module is try to be load in the kernel and transfer the control to the kernel.sys_init_module()
, which verifies if the user who trying to load the module is allowed or not. If allowed do step3.load_module
function, which assigns temporary memory and duplicate theelf
module from user space to kernel usingcopy_from_user()
. Then check if the ELF file is proper or not. Copy users arguments to the kernel. Update the state of module toMODULE_STATE_COMING
. Then returns a reference to the kernel module!doubly linked list
for modules loaded.MODULE_STATE_LIVE
.當我們透過 insmod 去載入一個核心模組時,為何 module_init 所設定的函式得以執行呢?Linux 核心做了什麼事呢?
Covered in last question.
need to paste source code(todo).
試著執行 $ readelf -a fibdrv.ko, 觀察裡頭的資訊和原始程式碼及 modinfo 的關聯,搭配上述提問,解釋像 fibdrv.ko 這樣的 ELF 執行檔案是如何「植入」到 Linux 核心
這個 fibdrv 名稱取自 Fibonacci driver 的簡稱,儘管在這裡顯然是為了展示和教學用途而存在,但針對若干關鍵的應用場景,特別去撰寫 Linux 核心模組,仍有其意義,請找出 Linux 核心的案例並解讀。提示: 可參閱 Random numbers from CPU execution time jitter
查閱 ktime 相關的 API,並找出使用案例 (需要有核心模組和簡化的程式碼來解說)
In my fibdrv.c:
Use my own code as reference!
The first variable
t
store the starting time of the calculation.Then after the calculation, use
ktime_sub
to get the differnce of the ending time and the starting time, whic can be represent asruntime
!clock_gettime 和 High Resolution Timers (HRT) 的關聯為何?請參閱 POSIX 文件並搭配程式碼解說
In the manpage of
clock_gettime
, we findclock_getres()
:The word
Resolution
is to describe theprecision
of clock.I wrote a simple program to get resolution of
CLOCK_MONOTONIC
I found the rationale in linux.org
fibdrv 如何透過 Linux Virtual File System 介面,讓計算出來的 Fibonacci 數列得以讓 userspace (使用者層級) 程式 (本例就是 client.c 程式) 得以存取呢?解釋原理,並撰寫或找出相似的 Linux 核心模組範例
注意到 fibdrv.c 存在著 DEFINE_MUTEX, mutex_trylock, mutex_init, mutex_unlock, mutex_destroy 等字樣,什麼場景中會需要呢?撰寫多執行緒的 userspace 程式來測試,觀察 Linux 核心模組若沒用到 mutex,到底會發生什麼問題
Mutex lock here is for protection of critical section!
Expected outcome:
What I got:
許多現代處理器提供了 clz / ctz 一類的指令,你知道如何透過演算法的調整,去加速 費氏數列 運算嗎?請列出關鍵程式碼並解說
Original Fibdrv time testing(100 Repeat)
ktime_t vs get_time
ktime_t
dynamic programing(100 repeat)
with clz cts(todo)
tags:
Cprogramming
LinuxKernel