标签:style blog 使用 os io strong 文件 数据
联编系统也考虑到一般代码可能需要借助于特定于体系结构的机制。所有特定于处理器的头文件都位于include/asm-arch/。在内核配置为特定的体系结构之后,则建立符号链接include/asm/指向具体硬件所对应的目录。内核通过#include<asm/file.h>即可访问特定于体系结构的头文件。
将数据对齐到特定的内存地址,对于高效使用处理器高速缓存和提升性能,都很有必要。通常,对齐是指对齐到数据类型的字节长度可整除的字节地址。将数据类型对齐到自身的字节长度称为自然对齐。
>put_unaligned(val,ptr):指向ptr指定的一个非对齐内存位置写入值val。
>PAGE_ALIGN(addr)可以将任何地址对齐到页边界。
>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)
#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中,汇编实现。
一个线程的运行状态,主要由处理器寄存器的内容定义。当前未运行的进程,必须将该数据保存在相应的数据结构中,以便调度器激活进行时,从中读取数据并迁移到适当的寄存器。用于完成该工作的结构定义在下列文件中。
> 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;
};#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))
/*
* 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)
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));
}
《深入Linux内核架构》附录A<体系结构相关知识>笔记,布布扣,bubuko.com
标签:style blog 使用 os io strong 文件 数据
原文地址:http://blog.csdn.net/cloud_desktop/article/details/38587913