2012年1月2日 星期一

asmlinkage 用法


"asmlinkage" 是在 i386 system call 實作中相當重要的一個 gcc 標籤(tag)。
當 system call handler 要呼叫相對應的 system call routine 時,便將一般用途暫存器的值
push 到 stack裡,因此 system call routine 就要由 stack 來讀取 system call handler 傳遞的
參數。這就是 asmlinkage標籤的用意。
system call handler 是 assembly code,system call routine(例如:sys_nice)是 C code,
當 assembly code 呼叫 C function,並且是以 stack 方式傳參數(parameter)時,
在 C function 的 prototype 前面就要加上 "asmlinkage"。
加上 "asmlinkage" 後,C function 就會由 stack 取參數,而不是從 register 取參數(
可能發生在程式碼最佳化後)。
更進一步的說明...
80x86 的 assembly 有 2 種傳遞參數的方法:
1. register method
2. stack method
Register method 大多使用一般用途(general-purpose)暫存器來傳遞參數,這種
方法的好處是簡單且快速。另外一種傳遞參數的做法是使用 stack(堆疊),
assembly code 的模式如下:
push number1
push number2
push number3
call sum
在 'sum' procedure 裡取值的方法,最簡單的做法是:
pop ax
pop ax
pop bx
pop cx
Stack Top 是放 IP,我們傳給 sum procedure 的參數由 stack 的後一個 entry 開始
讀取。
其它有關 asmlinkage
1. asmlinkage 是一個定義
2. "asmlinkage" 被定義在 /usr/include/linux/linkage.h
3. 如果您看了 linkage.h,會發現 "__attribute__" 這個語法,這是 gcc 用來定義
 function attribute 的語法。
轉貼自Jollen blog

2012年1月1日 星期日

利用find shell command 找檔案

普通用法
find -inamme *.c
這樣會把現在所在目錄下以及子目錄下的.c 檔列在螢幕上
高級用法
可以把輸出轉向到另一個指令的輸入
ex.
find -iname *.c -exec grep -l MEM_LENGH {} \;
會列出所有.c 裡有包含MEM_LENGH的檔案名稱

2011年12月27日 星期二

Linux char device driver todo list

  • __init 和 __exit function裡面implement add/remove cdev structure, apply/release device ID
  • implement file_operation 裡有用到的function 如 read() write() ioctl()

另外在driver 的Makefile裡
obj-y 代表 build進kernel
obj-m 代表build成module 型式, 也就是*.ko
obj-n 代表不build

通常obj-後面會用Kconfig裡的變數取代, 在make menuconfig後, kconfig會把各目錄下的.config parse一遍, 知道user的設定在shell的variables裡, 然後在building time依據設定build kernel

2011年12月23日 星期五

Some Useful EFI blogs

2011年12月21日 星期三

C/C++中 union 用法

平常因為工作內容的關係, 從沒用過union

今天突然看到, 竟然忘記這資料結構是甚麼 ! 筆記一下大陸人的網頁
#include <stdio.h>
void main()
{
union number
{ /*定义一个联合*/
int i;
struct
{ /*在联合中定义一个结构*/
char first;
char second;
}half;
}num;
num.i=0x4241; /*联合成员赋值*/
printf("%c%c\n", num.half.first, num.half.second);
num.half.first='a'; /*联合中结构成员赋值*/
num.half.second='b';
printf("%x\n", num.i);
getchar();
}
输出结果为: 
AB 
6261 

2011年12月20日 星期二

shell script

最近學習linux的shell script 
做一寫筆記備用


$1 $2 .....     parameter 1 2 ....
$# total number of parameter
$? return value of previous command
$@ list of all parameters one bye one
$* list all parameters with one string


logic operator
1. 
while [ true ] ; do
done
2. 
until [ ] ; do
done
3. 
for arg in " "
do
done
4. 
if [ ]
then
else
fi

Online Tutorial Link