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));
}