《深入Linux核心架構》附錄A<體繫結構相關知識>筆記

來源:互聯網
上載者:User

標籤:style   blog   使用   os   io   strong   檔案   資料   


A.1 概述
  為便於擴充到新的體繫結構,核心嚴格隔離了體繫結構相關和體繫結構無關的代碼。核心中特定於處理器的部分,包含定義和原型的標頭檔儲存在include/asm-arch/(例如,include/asm-arm/)目錄下,而C語言和組譯工具原始碼實現則儲存在arch/arch/(例如,arch/arm/)目錄下。


  聯編系統也考慮到一般代碼可能需要藉助於特定於體繫結構的機制。所有特定於處理器的標頭檔都位於include/asm-arch/。在核心配置為特定的體繫結構之後,則建立符號連結include/asm/指向具體硬體所對應的目錄。核心通過#include<asm/file.h>即可訪問特定於體繫結構的標頭檔。


A.2 資料類型
  核心區別下列三種基礎資料型別 (Elementary Data Type)。
 > C語言的標準資料類型;
 > 具有固定位元位元的資料類型,如u32,s16;
 > 特定於子系統的類型,例如pid_t,sector_t。

A.3 對齊

  將資料對齊到特定的記憶體位址,對於高效使用處理器快取和提升效能,都很有必要。通常,對齊是指對齊到資料類型的位元組長度可整除的位元組地址。將資料類型對齊到自身的位元組長度稱為自然對齊。


  在核心中某些地方,可能需要訪問非對齊的資料類型。各體繫結構必須為此定義兩個宏。
 > get_unaligned(ptr):對位於非對齊記憶體位置的一個指標,進行反引用操作。

 >put_unaligned(val,ptr):指向ptr指定的一個非對齊記憶體位置寫入值val。


  GCC為各種struct和union組織記憶體布局時,會自動選擇適當的對齊,是的程式員無須手工進行操作。

A.4 記憶體頁面
 > PAGE_SHIFT指定了頁長度以2為底的對數。
 > PAGE_SIZE指定了記憶體頁的長度,單位為位元組。

 >PAGE_ALIGN(addr)可以將任何地址對齊到頁邊界。


  還必須實現對頁的兩個標準操作,通常是通過最佳化的彙編指令來完成。
 > clear_page(start)刪除從start開始的一頁,並將其填充0位元組。

 >copy_page(to, from)將from處的頁資料複製到to處。


  PAGE_OFFSET宏指定了物理頁幀在虛擬位址空間中映射到的位置。在大多數體繫結構上,這隱含地定義了使用者地址空間的長度,但並不適用所有地址空間。因此必須使用TASK_SIZE常數來確定使用者空間長度。

/* * PAGE_OFFSET - the virtual address of the start of the kernel image * TASK_SIZE - the maximum size of a user space task. * TASK_UNMAPPED_BASE - the lower boundary of the mmap VM area */#define PAGE_OFFSETUL(CONFIG_PAGE_OFFSET)#define TASK_SIZE(UL(CONFIG_PAGE_OFFSET) - UL(0x01000000))#define TASK_UNMAPPED_BASE(UL(CONFIG_PAGE_OFFSET) / 3)

A.5 系統調用
  發出系統調用的機制,實際上是一個從使用者空間到核心空間的可控切換,在所有支援的平台上都有所不同。標準檔案unistd.h(在ARM上arch/arm/include/asm/unistd.h)負責與系統調用相關的任務。

A.6 字串處理
  核心中各處都會處理字串,因而對字串處理的時間要求很嚴格。很多體繫結構都提供了專門的彙編指令來執行所需的任務,或者由於手工最佳化的彙編代碼可能比編譯器產生的程式碼更為快速,因此所有體繫結構在<arch/arch/include/asm/string.h>中都定義了自身的各種字串操作。例如ARM架構中,

#ifndef __ASM_ARM_STRING_H#define __ASM_ARM_STRING_H/* * We don't do inline string functions, since the * optimised inline asm versions are not small. */#define __HAVE_ARCH_STRRCHRextern char * strrchr(const char * s, int c);#define __HAVE_ARCH_STRCHRextern char * strchr(const char * s, int c);#define __HAVE_ARCH_MEMCPYextern void * memcpy(void *, const void *, __kernel_size_t);#define __HAVE_ARCH_MEMMOVEextern void * memmove(void *, const void *, __kernel_size_t);#define __HAVE_ARCH_MEMCHRextern void * memchr(const void *, int, __kernel_size_t);#define __HAVE_ARCH_MEMSETextern void * memset(void *, int, __kernel_size_t);extern void __memzero(void *ptr, __kernel_size_t n);#define memset(p,v,n)({ void *__p = (p); size_t __n = n;if ((__n) != 0) {if (__builtin_constant_p((v)) && (v) == 0)__memzero((__p),(__n));elsememset((__p),(v),(__n));}(__p);})#endif
  所有這些操作,用於替換使用者空間中所用C標準庫的同名函數,以便在核心中執行同樣的任務。對於每個有體繫結構自身以最佳化形式定義的字串操作來說,都必須定義相應的__HAVE_ARCH_OPERATION宏。上述ARM架構的函數定義在arch\arm\lib中,彙編實現。

A.7 線程表示


  一個線程的運行狀態,主要由處理器寄存器的內容定義。當前未啟動並執行進程,必須將該資料儲存在相應的資料結構中,以便調度器啟用進行時,從中讀取資料並遷移到適當的寄存器。用於完成該工作的結構定義在下列檔案中。
 > ptrace.h中定義了用於儲存所有寄存器的pt_regs結構,在進程由使用者態切換到核心態時,會將儲存各寄存器值的pt_regs結構執行個體放在核心棧上。
 > processor.h包含了thread_struct結構體,用於描述所有其他寄存器和所有其他進程狀態資訊。
 > thread.h中定義了thread_info結構體,其中包含為實現進入和退出核心態、彙編代碼所必須訪問的所有task_struct成員。


  ARM架構下pt_regs的定義:

/* * This struct defines the way the registers are stored on the * stack during a system call.  Note that sizeof(struct pt_regs) * has to be a multiple of 8. */struct pt_regs {long uregs[18];};#define ARM_cpsruregs[16]#define ARM_pcuregs[15]#define ARM_lruregs[14]#define ARM_spuregs[13]#define ARM_ipuregs[12]#define ARM_fpuregs[11]#define ARM_r10uregs[10]#define ARM_r9uregs[9]#define ARM_r8uregs[8]#define ARM_r7uregs[7]#define ARM_r6uregs[6]#define ARM_r5uregs[5]#define ARM_r4uregs[4]#define ARM_r3uregs[3]#define ARM_r2uregs[2]#define ARM_r1uregs[1]#define ARM_r0uregs[0]#define ARM_ORIG_r0uregs[17]
  ARM架構下thread_struct定義(可以將機器指令以作業碼形式連同記憶體位址一同儲存,以供調試使用):

union debug_insn {u32arm;u16thumb;};struct debug_entry {u32address;union debug_insninsn;};struct debug_info {intnsaved;struct debug_entrybp[2];};struct thread_struct {/* fault info  */unsigned longaddress;unsigned longtrap_no;unsigned longerror_code;/* debugging  */struct debug_infodebug;};

A.8 位操作與位元組序
  核心特定於體繫結構的位操作在arch/arch/include/asm/bitops.h中定義,如下是ARM架構下的位操作定義:
#ifndef __ARMEB__/* * These are the little endian, atomic definitions. */#define set_bit(nr,p)ATOMIC_BITOP_LE(set_bit,nr,p)#define clear_bit(nr,p)ATOMIC_BITOP_LE(clear_bit,nr,p)#define change_bit(nr,p)ATOMIC_BITOP_LE(change_bit,nr,p)#define test_and_set_bit(nr,p)ATOMIC_BITOP_LE(test_and_set_bit,nr,p)#define test_and_clear_bit(nr,p)ATOMIC_BITOP_LE(test_and_clear_bit,nr,p)#define test_and_change_bit(nr,p)ATOMIC_BITOP_LE(test_and_change_bit,nr,p)#define find_first_zero_bit(p,sz)_find_first_zero_bit_le(p,sz)#define find_next_zero_bit(p,sz,off)_find_next_zero_bit_le(p,sz,off)#define find_first_bit(p,sz)_find_first_bit_le(p,sz)#define find_next_bit(p,sz,off)_find_next_bit_le(p,sz,off)#define WORD_BITOFF_TO_LE(x)((x))#else/* * These are the big endian, atomic definitions. */#define set_bit(nr,p)ATOMIC_BITOP_BE(set_bit,nr,p)#define clear_bit(nr,p)ATOMIC_BITOP_BE(clear_bit,nr,p)#define change_bit(nr,p)ATOMIC_BITOP_BE(change_bit,nr,p)#define test_and_set_bit(nr,p)ATOMIC_BITOP_BE(test_and_set_bit,nr,p)#define test_and_clear_bit(nr,p)ATOMIC_BITOP_BE(test_and_clear_bit,nr,p)#define test_and_change_bit(nr,p)ATOMIC_BITOP_BE(test_and_change_bit,nr,p)#define find_first_zero_bit(p,sz)_find_first_zero_bit_be(p,sz)#define find_next_zero_bit(p,sz,off)_find_next_zero_bit_be(p,sz,off)#define find_first_bit(p,sz)_find_first_bit_be(p,sz)#define find_next_bit(p,sz,off)_find_next_bit_be(p,sz,off)#define WORD_BITOFF_TO_LE(x)((x) ^ 0x18)#endif
  核心提供了little_endian.h和big_endian.h標頭檔。用於當前處理器的版本包含在asm-arch/byteorder.h中,小端格式的轉換如下,大端類似:
#define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))#define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))#define __constant_htons(x) ((__force __be16)___constant_swab16((x)))#define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))#define __constant_cpu_to_le64(x) ((__force __le64)(__u64)(x))#define __constant_le64_to_cpu(x) ((__force __u64)(__le64)(x))#define __constant_cpu_to_le32(x) ((__force __le32)(__u32)(x))#define __constant_le32_to_cpu(x) ((__force __u32)(__le32)(x))#define __constant_cpu_to_le16(x) ((__force __le16)(__u16)(x))#define __constant_le16_to_cpu(x) ((__force __u16)(__le16)(x))#define __constant_cpu_to_be64(x) ((__force __be64)___constant_swab64((x)))#define __constant_be64_to_cpu(x) ___constant_swab64((__force __u64)(__be64)(x))#define __constant_cpu_to_be32(x) ((__force __be32)___constant_swab32((x)))#define __constant_be32_to_cpu(x) ___constant_swab32((__force __u32)(__be32)(x))#define __constant_cpu_to_be16(x) ((__force __be16)___constant_swab16((x)))#define __constant_be16_to_cpu(x) ___constant_swab16((__force __u16)(__be16)(x))#define __cpu_to_le64(x) ((__force __le64)(__u64)(x))#define __le64_to_cpu(x) ((__force __u64)(__le64)(x))#define __cpu_to_le32(x) ((__force __le32)(__u32)(x))#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))#define __cpu_to_le16(x) ((__force __le16)(__u16)(x))#define __le16_to_cpu(x) ((__force __u16)(__le16)(x))#define __cpu_to_be64(x) ((__force __be64)__swab64((x)))#define __be64_to_cpu(x) __swab64((__force __u64)(__be64)(x))#define __cpu_to_be32(x) ((__force __be32)__swab32((x)))#define __be32_to_cpu(x) __swab32((__force __u32)(__be32)(x))#define __cpu_to_be16(x) ((__force __be16)__swab16((x)))#define __be16_to_cpu(x) __swab16((__force __u16)(__be16)(x))

A.9 頁表
  為簡化記憶體管理,核心提供一個將各種體繫結構不同點抽象出去的記憶體模型,各種移植版必須提供操作頁表和頁表項的函數。這些聲明在asm-arch/pgtable.h中。

A.10 雜項
A.10.1 校正和計算
  對資料包計算校正和,是通過IP網路通訊的關鍵,會相當耗時。如果可能的話,每種體繫結構都應該採用人工彙編代碼來計算校正和。相關代碼的聲明在asm-arch/checksum.h中。其中,有兩個函數式最重要的:
 > unsigned short ip_fast_csum根據IP前序和包頭長度計算必要的檢驗和。
 > csum_partial根據依次接收的各個分區,為每一個資料包計算校正和。

A.10.2 環境切換
  在調度器決定通知當前進程放棄CPU以便另一個進程運行之後,會進行環境切換中硬體相關的部分。為此,所有體繫結構都必須提供switch_to函數或對應的宏。原型如下,聲明定義在asm-arch/system.h中。

/* * switch_to(prev, next) should switch from task `prev' to `next' * `prev' will never be the same as `next'.  schedule() itself * contains the memory barrier to tell GCC not to cache `current'. */extern struct task_struct *__switch_to(struct task_struct *, struct thread_info *, struct thread_info *);#define switch_to(prev,next,last)do {last = __switch_to(prev,task_thread_info(prev), task_thread_info(next));} while (0)

A.10.3 尋找當前進程
  current宏用於找到一個指向當前運行進程的task_struct的指標。每種體繫結構都必須在arch/arch/include/asm/current.h中聲明該宏。該指標儲存在一個獨立的處理器寄存器中,可以使用current宏直接或間接地查詢。大多數體繫結構使用它儲存一個指向當前有效thread_info執行個體的指標,因為thread_info結構體包含了一個指標,指向相關進程的task_struct,current可以繞個彎子來實現。例如,ARM體繫結構的實現:
arch/arm/include/asm/current.h:

static inline struct task_struct *get_current(void) __attribute_const__;static inline struct task_struct *get_current(void){return current_thread_info()->task;}#define current (get_current())
  arch/arm/include/asm/thread_info.h:

/* * how to get the thread information struct from C */static inline struct thread_info *current_thread_info(void) __attribute_const__;static inline struct thread_info *current_thread_info(void){register unsigned long sp asm ("sp");return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.