免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7860 | 回复: 7

2.6 内核中宏定义 #define DEFINE(sym, val)的问题 ? [复制链接]

论坛徽章:
0
发表于 2006-02-17 16:35 |显示全部楼层
linux2.6.14.3 内核中有如下宏定义,

  1. #define DEFINE(sym, val) \
  2.         asm volatile("\n->" #sym " %0 " #val : : "i" (val))
复制代码

没弄清它的用法,于是用如下函数做了一下试验,所得结果更是让人不解。
宏定义中从"\n->"中的"-"开始的后续部分被编译器忽略。gcc提示为
Warning: rest of line ignored; first character is '-.
(采用如下格式编译:gcc -o tf tf.c)
程序打印结果为:0。

  1. /*tf.c*/
  2. #include "stdio.h"
  3. #define DEFINE(sym,val) \
  4.         asm volatile("\n->" #sym " %0 " #val : :"i" (val))

  5. int main(int argc,char* agrv[])
  6. {
  7.         int Ti=0;
  8.         DEFINE(Ti,8);
  9.         printf("\n The value of Ti is:%d\n",Ti);
  10.         return ;       
  11. }
复制代码

[ 本帖最后由 albcamus 于 2006-2-17 18:07 编辑 ]

论坛徽章:
0
发表于 2006-02-17 22:48 |显示全部楼层
#define DEFINE(sym,val) \
        asm volatile("\n->" #sym " %0 " #val : :"i" (val))


这段代码显然不符合gcc内嵌汇编的格式,不能拿来直接用的。“->”是用于sed的标识符,编译内核时会替换掉“->”。

[ 本帖最后由 richardhesidu 于 2006-2-18 14:44 编辑 ]

论坛徽章:
0
发表于 2006-02-18 18:18 |显示全部楼层
这段代码在redhat上编译都通不过。
我估计这段代码的意思是:
asm volatile("\n->" #sym " %0 " #val : :"i" (val))     =    #define  sym  val

只不过这里是动态的定义。只需要传一个sym和val进来,就可以帮你完成#define  sym  val的功能

[ 本帖最后由 normalnotebook 于 2006-2-18 18:20 编辑 ]

论坛徽章:
0
发表于 2006-02-19 17:49 |显示全部楼层
那个宏定义在asm-offsets.c中,这个.c文件根本就不是用来编译运行的,只是在编译内核的时候,用它生成一个asm-offsets.s文件,然后使用一个脚本将这个asm-offsets.s再转换为asm-offsets.h。这个头文件遵循汇编语法,用来被汇编文件include的。
详见下面的东东:
http://marc.theaimsgroup.com/?l= ... 19616028442&w=2

[ 本帖最后由 蓝白725 于 2006-2-19 17:55 编辑 ]

论坛徽章:
0
发表于 2006-02-19 18:01 |显示全部楼层
原帖由 蓝白725 于 2006-2-19 17:49 发表
那个宏定义在asm-offsets.c中,这个.c文件根本就不是用来编译运行的,只是在编译内核的时候,用它生成一个asm-offsets.s文件,然后使用一个脚本将这个asm-offsets.s再转换为asm-offsets.h。这个头文件遵循汇编语法 ...


呵呵就为这个回复,这个贴设为保留!
顺便把蓝白兄的链接转过来:

[prev in list] [next in list] [prev in thread] [next in thread]

List:       linux-kernel
Subject:    [RFC] Standard way of generating assembler offsets
From:       Keith Owens <kaos () ocs ! com ! au>
Date:       2001-10-04 11:47:08
[Download message RAW]

Almost every architecture generates Assembler values to map the offsets
of fields in C structures, about the only exception is i386 and that is
because its offsets are hard coded into entry.S.  Every arch has done
it differently, none of them have got it exactly right.

As part of kbuild 2.5 I am standardizing on one method for generating
Assembler offsets.  This change is required for kbuild 2.5 but it can
be added to 2.4 without disturbing the current kbuild, I want to do
this gradually now instead of a single massive change in kernel 2.5.  I
will be issuing per architecture changes for generating Assembler
offsets against 2.4.

The kbuild 2.5 method for generating Assembler offsets satisfies these
requirements:

* No manual intervention required.  Many architectures rely on users
  running make dep after changing config options that affect the
  Assembler offsets.  If the user forgets to run make dep then the C
  and Assembler code is out of sync - totally unacceptable.  This is
  completely fixed in kbuild 2.5; I cannot do a complete fix in kbuild
  2.4 but it is still better than the existing manual system.

* Standard name for the related files.  There are 6+ different names
  for the files used to generate Assembler offsets, kbuild 2.5 uses
  asm-offsets.[csh] on all architectures.

* Allows for multiple parallel compiles from the same source tree.
  Writing the generated asm-offsets.h to include/asm is not an option,
  it prevents concurrent compiles.

* The method must work in native and cross compile mode and give
  exactly the same results.  Some 2.4 code only works in native mode,
  some architectures have different methods for native and cross
  compile with different output formats.  Yeuch!

* Standard scripts for generating the output.  Every arch does it
  differently in 2.4, standards are good!

* Correct dependency trees.  Because 2.4 make dep does not scan .S
  files, there is little or no dependency information.  Even if the
  offsets are regenerated, the affected Assembler code does not always
  get rebuilt.  kbuild 2.5 handles dependencies for Assembler as well
  as C; I cannot get kbuild 2.4 perfect but I can improve on the
  existing (or non-existent) 2.4 dependencies.

All architectures will define arch/$(ARCH)/asm-offsets.c.  This has a
standard prologue for the macros that convert offsets to Assembler,
followed by arch specific field references.

arch/$(ARCH)/asm-offsets.s is generated from arch/$(ARCH)/asm-offsets.c
using standard rules, although kbuild 2.4 needs some tweaking.

arch/$(ARCH)/asm-offsets.h is generated from arch/$(ARCH)/asm-offsets.s
by a semi-standard script.  Most of the script is common to all
architectures but the precise format of the Assembler output is arch
specific.

The final result is included in *only* the Assembler programs that need
it, as #include "asm-offsets.h" with -I arch/$(ARCH) in the relevant
Makefiles.  Hard coding relative paths in source files is a pet hate,
use #include "localname.h" and -I instead.  Including the generated
file in C code is not allowed, it severly pollutes the dependency
chain, to the extent that any config change can force a complete
recompile, unacceptable.


Example from i386:

arch/i386/asm-offsets.c

/*
* Generate definitions needed by assembly language modules.
* This code generates raw asm output which is post-processed to extract
* and format the required data.
*/

#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/sched.h>

/* Use marker if you need to separate the values later */

#define DEFINE(sym, val, marker) \
  asm volatile("\n-> " #sym " %0 " #val " " #marker : : "i" (val))

#define BLANK() asm volatile("\n->" : : )

int
main(void)
{
  DEFINE(state,        offsetof(struct task_struct, state),);
  DEFINE(flags,        offsetof(struct task_struct, flags),);
  DEFINE(sigpending,   offsetof(struct task_struct, sigpending),);
  DEFINE(addr_limit,   offsetof(struct task_struct, addr_limit),);
  DEFINE(exec_domain,  offsetof(struct task_struct, exec_domain),);
  DEFINE(need_resched, offsetof(struct task_struct, need_resched),);
  DEFINE(tsk_ptrace,   offsetof(struct task_struct, ptrace),);
  DEFINE(processor,    offsetof(struct task_struct, processor),);
  BLANK();
  DEFINE(ENOSYS,       ENOSYS,);
  return 0;
}

asm-offsets.s to asm-offsets.h.

# Convert raw asm offsets into something that can be included as
# assembler definitions.  It converts
#   -> symbol $value source
# into
#   symbol = value /* 0xvalue source */

echo '#ifndef __ASM_OFFSETS_H__'
echo '#define __ASM_OFFSETS_H__'
echo '/*'
echo ' * DO NOT MODIFY'
echo ' *'
echo " * This file was generated by arch/$(ARCH)/Makefile.in."
echo ' *'
echo ' */'
echo ''
awk '
  /^->$/{printf("\n")}
  /^-> /{
    sym = $2;
    val = $3;
    sub(/^\$/, "", val);
    $1 = "";
    $2 = "";
    $3 = "";
    printf("%-20s = %3d\t/* 0x%x\t%s */\n", sym, val, val, $0)
  }
'
echo '#endif'

Generated arch/i386/asm-offsets.h

#ifndef __ASM_OFFSETS_H__
#define __ASM_OFFSETS_H__
/*
* DO NOT MODIFY
*
* This file was generated by arch/i386/Makefile.in.
*
*/

state                =   0      /* 0x0     offsetof(struct task_struct, state) */
flags                =   4      /* 0x4     offsetof(struct task_struct, flags) */
sigpending           =   8      /* 0x8     offsetof(struct task_struct, sigpending) */
addr_limit           =  12      /* 0xc     offsetof(struct task_struct, addr_limit) */
exec_domain          =  16      /* 0x10    offsetof(struct task_struct, exec_domain) */
need_resched         =  20      /* 0x14    offsetof(struct task_struct, need_resched) */
tsk_ptrace           =  24      /* 0x18    offsetof(struct task_struct, ptrace) */
processor            =  52      /* 0x34    offsetof(struct task_struct, processor) */

ENOSYS               =  38      /* 0x26    ENOSYS */
#endif


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[prev in list] [next in list] [prev in thread] [next in thread]


Configure | About MARC | Support MARC | Got a list to add? | Sponsored by 10East and KoreLogic

论坛徽章:
0
发表于 2006-02-19 21:15 |显示全部楼层
学习学习

论坛徽章:
0
发表于 2009-02-06 02:13 |显示全部楼层
tian a

论坛徽章:
1
天蝎座
日期:2013-12-06 18:23:58
发表于 2011-06-13 09:58 |显示全部楼层
博大精深的内核啊,好喜欢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

SACC2019中国系统架构师大会

【数字转型 架构演进】SACC2019中国系统架构师大会
2019年10月31日~11月2日第11届中国系统架构师大会(SACC2019)将在北京隆重召开。四大主线并行的演讲模式,1个主会场、20个技术专场、超千人参与的会议规模,100+来自互联网、金融、制造业、电商等领域的嘉宾阵容,将为广大参会者提供一场最具价值的技术交流盛会。




----------------------------------------

大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP