A.1 概述
爲便於擴展到新的體系結構,內核嚴格隔離了體系結構相關和體系結構無關的代碼。內核中特定於處理器的部分,包含定義和原型的頭文件保存在include/asm-arch/(例如,include/asm-arm/)目錄下,而C語言和彙編程序源代碼實現則保存在arch/arch/(例如,arch/arm/)目錄下。
聯編系統也考慮到一般代碼可能需要藉助於特定於體系結構的機制。所有特定於處理器的頭文件都位於include/asm-arch/。在內核配置爲特定的體系結構之後,則建立符號鏈接include/asm/指向具體硬件所對應的目錄。內核通過#include<asm/file.h>即可訪問特定於體系結構的頭文件。
A.2 數據類型
內核區別下列三種基本數據類型。
> C語言的標準數據類型;
> 具有固定比特位數的數據類型,如u32,s16;
> 特定於子系統的類型,例如pid_t,sector_t。
A.3 對齊
將數據對齊到特定的內存地址,對於高效使用處理器高速緩存和提升性能,都很有必要。通常,對齊是指對齊到數據類型的字節長度可整除的字節地址。將數據類型對齊到自身的字節長度稱爲自然對齊。
> get_unaligned(ptr):對位於非對齊內存位置的一個指針,進行反引用操作。
> put_unaligned(val,ptr):指向ptr指定的一個非對齊內存位置寫入值val。
A.4 內存頁面
> PAGE_SHIFT指定了頁長度以2爲底的對數。
> PAGE_SIZE指定了內存頁的長度,單位爲字節。
> PAGE_ALIGN(addr)可以將任何地址對齊到頁邊界。
> clear_page(start)刪除從start開始的一頁,並將其填充0字節。
> copy_page(to, from)將from處的頁數據複製到to處。
/*
* 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_OFFSET UL(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_STRRCHR
extern char * strrchr(const char * s, int c);
#define __HAVE_ARCH_STRCHR
extern char * strchr(const char * s, int c);
#define __HAVE_ARCH_MEMCPY
extern void * memcpy(void *, const void *, __kernel_size_t);
#define __HAVE_ARCH_MEMMOVE
extern void * memmove(void *, const void *, __kernel_size_t);
#define __HAVE_ARCH_MEMCHR
extern void * memchr(const void *, int, __kernel_size_t);
#define __HAVE_ARCH_MEMSET
extern 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)); \
else \
memset((__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_cpsr uregs[16]
#define ARM_pc uregs[15]
#define ARM_lr uregs[14]
#define ARM_sp uregs[13]
#define ARM_ip uregs[12]
#define ARM_fp uregs[11]
#define ARM_r10 uregs[10]
#define ARM_r9 uregs[9]
#define ARM_r8 uregs[8]
#define ARM_r7 uregs[7]
#define ARM_r6 uregs[6]
#define ARM_r5 uregs[5]
#define ARM_r4 uregs[4]
#define ARM_r3 uregs[3]
#define ARM_r2 uregs[2]
#define ARM_r1 uregs[1]
#define ARM_r0 uregs[0]
#define ARM_ORIG_r0 uregs[17]
ARM架構下thread_struct定義(可以將機器指令以操作碼形式連同內存地址一同保存,以供調試使用):
union debug_insn {
u32 arm;
u16 thumb;
};
struct debug_entry {
u32 address;
union debug_insn insn;
};
struct debug_info {
int nsaved;
struct debug_entry bp[2];
};
struct thread_struct {
/* fault info */
unsigned long address;
unsigned long trap_no;
unsigned long error_code;
/* debugging */
struct debug_info debug;
};
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));
}