顯示具有 kernel 標籤的文章。 顯示所有文章
顯示具有 kernel 標籤的文章。 顯示所有文章

2012年8月17日 星期五

Adding a new system call in Linux kernel 3.3

How to add a new Linux kernel API in 3.3 version? -- for 64 bits OS
  • Upzip it with command ‘tar xvfj XXX” to a folder For example : /root/kernel tar xvfj linux-3.3.1.tar.bz2 
  • Edit file “/root/kernel/linux-3.3.1/arch/x86/syscalls/syscall_64.tbl” Add new line
    312 64 husky1 sys_husky1 
  • Eidt file “/root/kernel/linux-3.3.1/include/linux/syscalls.h” Add new function declaration
    asmlinkage long sys_husky1(int fd);
    before the line “#endif” 
  • Add a new c file under “/root/kernel/linux-3.3.1/arch/x86/kernel”
    long sys_husky1(int fd) { ... }
  • Edit “/root/kernel/linux-3.3.1/arch/x86/kernel/Makefile” Add a new line “obj-y += husky.o” 
  • goto /root/kernel/linux-3.3.1 folder and run command “make –j8” 
User space code for calling the new system call:
int ret = syscall(312);
Due to standard kernel header doesn't know our new system call,
we need to use system call number here.

Ref: http://stackoverflow.com/questions/9977968/adding-a-new-system-call-in-linux-kernel-3-3

2010年1月26日 星期二

同步資料 lock 的處理機制

請教:spinlock,mutex,semaphore,critical section的作用和區別,都適合那些場合,謝謝
Mutex是一把鑰匙,一個人拿了就可進入一個房間,出來的時候把鑰匙交給隊列的第一個。一般的用法是用於串行化對critical section代碼的訪問,保證這段代碼不會被並行的運行。
(A mutex is really a semaphore with value 1.)


Semaphore是一件可以容納N人的房間,如果人不滿就可以進去,如果人滿了,就要等待有人出來。對於N=1的情況,稱為binary semaphore。一般的用法是,用於限制對於某一資源的同時訪問。



Binary semaphore與Mutex的差異:

在有的系統中Binary semaphore與Mutex是沒有差異的。在有的系統上,主要的差異是mutex一定要由獲得鎖的進程來釋放。而semaphore可以由其它進程釋放(這時的semaphore實際就是個原子的變量,大家可以加或減),因此semaphore可以用於進程間同步。 Semaphore的同步功能是所有系統都支持的,而Mutex能否由其他進程釋放則未定,因此建議mutex只用於保護critical section。而semaphore則用於保護某變量,或者同步。



另一個概念是spin lock,這是一個內核態概念。 spin lock與semaphore的主要區別是spin lock是busy waiting,而semaphore是sleep。對於可以sleep的進程來說,busy waiting當然沒有意義。對於單CPU的系統,busy waiting當然更沒意義(沒有CPU可以釋放鎖)。因此,只有多CPU的內核態非進程空間,才會用到spin lock。 Linux kernel的spin lock在非SMP的情況下,只是關irq,沒有別的操作,用於確保該段程序的運行不會被打斷。其實也就是類似mutex的作用,串行化對critical section的訪問。但是mutex不能保護中斷的打斷,也不能在中斷處理程序中被調用。而spin lock也一般沒有必要用於可以sleep的進程空間。



Mutex vs. Semaphore, what is the difference?

The Toilet Example (c) Copyright 2005, Niclas Winquist ;)

Mutex:

Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: "Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."
Ref: Symbian Developer Library

(A mutex is really a semaphore with value 1.)

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)."
Ref: Symbian Developer Library

2008年12月20日 星期六

running 32bit apps on 64bit kernel with nvidia

當記憶體大於 4G 的時候,32bit 的 kernel 雖然可以透過啟動 64G 來 access 所有 memory
但是 PAE 會降低系統效能,此外好像 64bit kernel 的選項比較新 (...好吧,是心理作用...)
非常不准的效能測試:
hdparm -tT /dev/sda
發現 32bit with PAE 大約 36xx MB/Sec 而 64bit 可以跑到 40xx MB/Sec
很爛的測試,知道更好的方法請告訴我...

====== 重點開始 ======

Step 1. 建立 cross compile 環境

# emerge crossdev
# crossdev -s1 -t x86_64

Step 2. 編譯 64bit 的 kernel
編輯 kernel 的 Makefile , 增加下面兩行:
ARCH=x86
CROSS_COMPILE=x86_64-pc-linux-gnu-
接下來如常編譯與安裝即可


Step 3. 編譯 nvidia drivers

到 ftp://download.nvidia.com/XFree86/Linux-x86_64 和 Linux-X86 下載 NVIDIA-Linux-xxxx-pkg1.run

分別以 NVIDIA-Linux-xxxx-pkg1.run -x 解壓縮

複製 X86_64 的 NVIDIA-Linux-x86_64-180.16-pkg1/usr/src/nv/nv-kernel.o 到 X86
( 為什麼知道是這個檔?看 make module 他會說 32bit 無法連結到 64bit 於 nv-kernel.o )

修改 Makefile ,尋找 "make CC="
(可於此執行 make module 就會看到他是這樣呼叫 kernel make 的)

改成 "make CROSS_COMPILE=x86_64-pc-linux-gnu-"
( CC=$(CC) 記得刪掉 )

如常執行 ./nvidia-install 即可

ps. 最好先移除之前安裝的 driver
Related Posts Plugin for WordPress, Blogger...