1. 程式人生 > >《深入Linux核心架構》附錄A筆記

《深入Linux核心架構》附錄A筆記

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。

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_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.hbig_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));
}