setup_arch()-->setup_processor()函数分析

 setup_arch()-->setup_processor()函数分析
static void __init setup_processor(void)
{
    struct proc_info_list *list;
   
    *** include/asm/procinfo.h:
    /*
     * Note!  struct processor is always defined if we're
     * using MULTI_CPU, otherwise this entry is unused,
     * but still exists.
     *
     * NOTE! The following structure is defined by assembly
      * language, NOT C code.  For more information, check:
     *  arch/arm/mm/proc-*.S and arch/arm/kernel/head.S
     */
    struct proc_info_list {
        unsigned int        cpu_val;
        unsigned int        cpu_mask;
        unsigned long        __cpu_mm_mmu_flags;    /* used by head.S */
        unsigned long        __cpu_io_mmu_flags;    /* used by head.S */
        unsigned long        __cpu_flush;        /* used by head.S */
        const char            *arch_name;
        const char            *elf_name;
        unsigned int        elf_hwcap;
        const char            *cpu_name;
        struct processor        *proc;
        struct cpu_tlb_fns    *tlb;
        struct cpu_user_fns    *user;
        struct cpu_cache_fns    *cache;
    };
    /*
     * locate processor in the list of supported processor
     * types.  The linker builds this table for us from the
     * entries in arch/arm/mm/proc-*.S
     */
    list = lookup_processor_type(processor_id);
   
    // lookup_processor_type定义在arch/arm/kernel/head-common.S中,它本身是一个汇编函数,通过一个C语言API包装而成的,它的功能就是检测处理器内核类型。__lookup_processor_type调用结束返回原程序时,会将返回结果保存到寄存器中。其中r8 保存了页表的标志位,r9 保存了处理器的 ID 号,r10 保存了与处理器相关的 struct proc_info_list 结构地址。
   
    if (!list) {
        printk("CPU configuration botched (ID %08x), unable "
               "to continue./n", processor_id);
        while (1);
    }
    cpu_name = list->cpu_name;
#ifdef MULTI_CPU
    processor = *list->proc;
#endif
#ifdef MULTI_TLB
    cpu_tlb = *list->tlb;
#endif
#ifdef MULTI_USER
    cpu_user = *list->user;
#endif
#ifdef MULTI_CACHE
    cpu_cache = *list->cache;
#endif
    printk("CPU: %s [%08x] revision %d (ARMv%s), cr=%08lx/n",
           cpu_name, processor_id, (int)processor_id & 15,
           proc_arch[cpu_architecture()], cr_alignment);
    //我的板子的打印信息如下:
    //CPU: XScale-PXA255 [69052d06] revision 6 (ARMv5TE), cr=0000397f
    sprintf(init_utsname()->machine, "%s%c", list->arch_name, ENDIANNESS);
   
    先来看一下相关的数据结构:
    *** include/linux/utsname.h:
    struct new_utsname {
        char sysname[65];
        char nodename[65];
        char release[65];
        char version[65];
        char machine[65];
        char domainname[65];
    };   
    static inline struct new_utsname *init_utsname(void)
    {
        return &init_uts_ns.name;
    }
   
    *** init/version.c:
    struct uts_namespace init_uts_ns = {
        .kref = {
            .refcount    = ATOMIC_INIT(2),
        },
        .name = {
            .sysname    = UTS_SYSNAME,
            .nodename    = UTS_NODENAME,
            .release    = UTS_RELEASE,
            .version    = UTS_VERSION,
            .machine    = UTS_MACHINE,
            .domainname    = UTS_DOMAINNAME,
        },
    };
    EXPORT_SYMBOL_GPL(init_uts_ns)
   
    因此,上述的sprintf语句的功能实际上就是将检测得到的list->arch_name存储到init_uts_ns.machine中。
   
    //该句是将目标平台所使用的二进制格式存储到elf_platform中
    sprintf(elf_platform, "%s%c", list->elf_name, ENDIANNESS);
   
    elf_hwcap = list->elf_hwcap;
#ifndef CONFIG_ARM_THUMB
    elf_hwcap &= ~HWCAP_THUMB;
#endif
    cpu_proc_init();
}
    最后来看,cpu_proc_init(),关于它有两个定义,对单CPU来说
    *** include/asm-arm/cpu-single.h:
    /*
     * Single CPU
     */
    #ifdef __STDC__
    #define __catify_fn(name,x)    name##x
    #else
    #define __catify_fn(name,x)    name/**/x
    #endif
    #define __cpu_fn(name,x)    __catify_fn(name,x)
    /*
     * If we are supporting multiple CPUs, then we must use a table of
     * function pointers for this lot.  Otherwise, we can optimise the
     * table away.
     */
    #define cpu_proc_init            __cpu_fn(CPU_NAME,_proc_init
   
    对多CPU来说:
   
        *** include/asm-arm/cpu-multi32.h:
        
    extern struct processor {
        /* MISC
         * get data abort address/flags
         */
        void (*_data_abort)(unsigned long pc);
        /*
         * Set up any processor specifics
         */
        void (*_proc_init)(void);
        /*
         * Disable any processor specifics
         */
        void (*_proc_fin)(void);
        /*
         * Special stuff for a reset
         */
        void (*reset)(unsigned long addr) __attribute__((noreturn));
        /*
         * Idle the processor
         */
        int (*_do_idle)(void);
        /*
         * Processor architecture specific
         */
        /*
         * clean a virtual address range from the
         * D-cache without flushing the cache.
         */
        void (*dcache_clean_area)(void *addr, int size);
        /*
         * Set the page table
         */
        void (*switch_mm)(unsigned long pgd_phys, struct mm_struct *mm);
        /*
         * Set a possibly extended PTE.  Non-extended PTEs should
         * ignore 'ext'.
         */
        void (*set_pte_ext)(pte_t *ptep, pte_t pte, unsigned int ext);
    } processor;
    #define cpu_proc_init()            processor._proc_init()


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/50916/showart_1144649.html

 

    内核中有很多的宏定义,在宏定义define中经常看到两个字符串##和#,这里把它的用法做一下说明:
    ##是一个连接符号,用于把参数连在一起
        例如:
            > #define  FOO(arg)   my##arg
        则
            > FOO(abc)
        相当于   myabc
   
    #是“字符串化”的意思。出现在宏定义中的#是把跟在后面的参数转换成一个字符串
        例如:
            > #define STRCPY(dst, src)   strcpy(dst, #src)
        则
            > STRCPY(buff, abc)
        相当于   strcpy(buff, "abc")

    另外,如果##后的参数本身也是一个宏的话,##会阻止这个宏的展开

    #define STRCPY(a, b)    strcpy(a ## _p, #b)
    int main()
    {
        char var1_p[20];
        char var2_p[30];
        strcpy(var1_p, "aaaa");
        strcpy(var2_p, "bbbb");
        STRCPY(var1, var2);
        STRCPY(var2, var1);
        printf("var1 = %s/n", var1_p);
        printf("var2 = %s/n", var2_p);
        return 0;

        /* 注意这里 */
        STRCPY(STRCPY(var1,var2),var2);
        /* 这里是否会展开为: strcpy(strcpy(var1_p,"var2")_p,"var2“)?
         * 答案是否定的:
         * 展开结果将是:  strcpy(STRCPY(var1,var2)_p,"var2")
         * ## 阻止了参数的宏展开!
         * 如果宏定义里没有用到 # 和 ##, 宏将会完全展开
         */
    }  

 

关于记号粘贴操作符(token paste operator): ##



feiy@KernelTech, newsmth.org



1. 简单的说,“##”是一种分隔连接方式,它的作用是先分隔,然后进行强制连接。

   其中,分隔的作用类似于空格。我们知道在普通的宏定义中,预处理器一般把空格

   解释成分段标志,对于每一段和前面比较,相同的就被替换。但是这样做的结果是,

   被替换段之间存在一些空格。如果我们不希望出现这些空格,就可以通过添加一些

   ##来替代空格。



   另外一些分隔标志是,包括操作符,比如 +, -, *, /, [,], ...,所以尽管下面的

   宏定义没有空格,但是依然表达有意义的定义: define add(a, b)  a+b



   而其强制连接的作用是,去掉和前面的字符串之间的空格,而把两者连接起来。





2. 举列 -- 试比较下述几个宏定义的区别



   #define A1(name, type)  type name_##type##_type 或

   #define A2(name, type)  type name##_##type##_type



   A1(a1, int);  /* 等价于: int name_int_type; */

   A2(a1, int);  /* 等价于: int a1_int_type;   */



   解释:

        1) 在第一个宏定义中,"name"和第一个"_"之间,以及第2个"_"和第二个

   "type"之间没有被分隔,所以预处理器会把name_##type##_type解释成3段:

   “name_”、“type”、以及“_type”,这中间只有“type”是在宏前面出现过

    的,所以它可以被宏替换。



        2) 而在第二个宏定义中,“name”和第一个“_”之间也被分隔了,所以

   预处理器会把name##_##type##_type解释成4段:“name”、“_”、“type”

   以及“_type”,这其间,就有两个可以被宏替换了。



        3) A1和A2的定义也可以如下:

           #define A1(name, type)  type name_  ##type ##_type  

                                      <##前面随意加上一些空格>

           #define A2(name, type)  type name ##_ ##type ##_type



    结果是## 会把前面的空格去掉完成强连接,得到和上面结果相同的宏定义



3. 其他相关 -- 单独的一个 #



   至于单独一个#,则表示 对这个变量替换后,再加双引号引起来。比如



      #define  __stringify_1(x)   #x

那么

      __stringify_1(linux)   <==>  "linux"



所以,对于MODULE_DEVICE_TABLE



     1) #define MODULE_DEVICE_TABLE(type,name)                        

             MODULE_GENERIC_TABLE(type##_device,name)

     2) #define MODULE_GENERIC_TABLE(gtype,name)                      

             extern const struct gtype##_id __mod_##gtype##_table     

             __attribute__ ((unused, alias(__stringify(name))))



得到  

      MODULE_DEVICE_TABLE(usb, products)  

                             /*notes: struct usb_device_id products; */

 <==> MODULE_GENERIC_TABLE(usb_device,products)

 <==> extern const struct usb_device_id __mod_usb_device_table     

             __attribute__ ((unused, alias("products")))   



注意到alias attribute需要一个双引号,所以在这里使用了__stringify(name)来

给name加上双引号。另外,还注意到一个外部变量"__mod_usb_device_table"被alias

到了本驱动专用的由用户自定义的变量products<usb_device_id类型>。这个外部变量

是如何使用的,更多的信息请参看《probe()过程分析》。



4. 分析方法和验证方式 -- 编写一个简单的C程序



   用宏定义一个变量,同时用直接方式定义一个相同的变量,编译报告重复定义;

   用宏定义一个变量,直接使用该宏定义的变量名称,编译通过且运行结果正确;

   使用printf打印字符串数据。printf("token macro is %s", __stringify_1(a1));

转自:http://blog.chinaunix.net/u/17855/showart_113663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值