關於執行環境:
由於裝置驅動程式是屬於kernel程式碼的一部份,
且驅動程式在kernel space運作,
kernel的程式有context(執行環境)的觀念,並分為以下兩種:
Process context(一般行程執行環境)
Interrupt context(中斷執行環境)

但這兩種context有其差異性,如下所示:
------------------------------------------------------------------------
| Context           | 可否sleep | 可否被preempt(搶先執行) | 處理時間   |
| Process context   |     可    |            可           |   可拖長   |
| Interrupt context |   不可    |          不可           |  極力縮短  |
------------------------------------------------------------------------

重點就在於Interrupt context是全系統執行權限最高的,
如果sleep的話,可想而知,會有什麼事發生,
就是沒人能叫醒它,結果就造成系統呼叫死結,導致kernel panic並停止運作。
因此就要確定,在interrupt context 的kernel 函式都不會用到sleep。
基於同樣理由,故不能被preempt。

關於Endian:
Byte 陣列一次讀寫2個bytes 以上的資料時,byte會以何種次序排列,
就是所謂endian的問題。

那當然, 我們可用簡單的程式來測試一下。
test_endian.c 的程式碼如下:

/*****************************************************************************/

     1#include <stdio.h>
     2#include <stdlib.h>
     3#include <string.h>
      
     4int main(void)
     5{
     6unsigned char buf[4];
     7int n = 0xdeabbeef;
      
     8memcpy(buf, &n, sizeof(n));
     9printf("%02x %02x %02x %02x\n",
    10                buf[0], buf[1], buf[2], buf[3]
    11              );
      
    12return 0;
    13}

/*****************************************************************************/

# gcc test_endian.c -o test_endian
# ./test_endian
如果執行結果為
ef be ab de <--- Little endian
de ab be ef <---    Big endian

但通常開發驅動程式的時候,不需要應對endian的問題,因為kernel會把endian的部份吸收。
有endian差異的部份主要包含
CPU
Bus(PCI 或 USB)
網路封包
EEPROM等資料內容

註記及聲明:
本教學,是參考Linux Device Driver Programming驅動程式設計由平田豐著的這本書。

arrow
arrow
    全站熱搜

    csw.dawn 發表在 痞客邦 留言(0) 人氣()