System Call


Author: 陳炯廷
Subject: system call 一些注意的地方
--------------------------------------------------------------------------------
一點筆記給大家參考, 新增一個 system call 的步驟:
- 在 arch/i386/kernel/entry.S 新增一個 .long SYMBOL_NAME(sys_abc)
通常會加在最後面 - 在 include/asm/unistd.h 相對應的位置增加 __NR_abc
- 把 include/asm/unistd.h copy 到 /usr/include/asm 底下
- 在 include/linux/sys.h 的 NR_syscalls 的數值加一!! 這個步驟在網路上大部分的 tutorials 都沒有提及... 如果沒有加上的話, 且你的 system call 又加在最後一個, 那它會吃不到

接著就可以寫上妳的 system call, 看你要 append 到 kernel/xxx.c 裡頭, 或者是自己開一個新的目錄. 若是自己多寫一個檔案, 要注意 Makefile 要相對應的修改.

改好之後, 要開始製作 kernel, 詳細的"官方"說明在 /usr/src/linux/README 這個檔案看得到, 基本上的指令如下:
1. make mrproper
2. make menuconfig (或是同系列的都可以)
3. make dep
4. make modules
5. make modules_install
6. make
7. make install

第六步才是開始做 kernel, 這是我灌的方式, 和一般 Tutorial 寫的不大一樣。我發現 RedHat Enterprise 在 make install 的時候, 會自動產生 bzImage, 幫你做好 initrd, 而且還幫你將 grub 的設定都改好, 非常的方便。

1-5 的步驟, 只要在第一次 compile kernel 做就好了, 以後改好的每一次, 都只要做最後一個就可以了。 (因為最後一個會包含第六個)

編好 Kernel 之後請記得 REBOOT!


若是 System call 沒有跑起來, 有一個很簡單的方式可以 debug
以下是程式範例:
#include
#include
#include
#include

_syscall0(int,hello);

int main() {
hello();
if (errno)
printf("%s\n", strerror(errno));
}
用 strerror 把 errno 印出來會有多一點點的錯誤訊息, 以 sys.h 沒有改來說, 它就會顯示 Function not implemented. 但如果你沒有用的話, 只會發現 hello 回傳 -1, 不方便 DEBUG.

另外, 我們所使用的 Kernel 版本, 在 task_struct 當中, 已經沒有 next_task和prev_task 這兩個東西了, 不過好消息是, 取而代之的變得更好使用。這個版本的 kernel 直接將 next_task 和 prev_task 定義成 macro, 只要 next_task(p), 它就丟給你 p 下一個的 task_struct, 都幫你寫好了.


上面有些東西也許是錯的, 不過 works for me XD 大家一起加油吧!! ^^"