address_space 从哪里来
这两天想弄清楚linux的内存分配,忽然看到了address_space,就想弄明白。
整个内核就见到 address_space(1)和address_space(2)在这个文件里出现。
include/linux/compiler.h:
# define __user __attribute__((noderef, address_space(1)))
# define __iomem __attribute__((noderef, address_space(2)))
不过在新内核2.6.36中出现新的了
2.6.36.3/include/linux/compiler.h
# define __user __attribute__((noderef, address_space(1)))
# define __kernel __attribute__((address_space(0)))
# define __iomem __attribute__((noderef, address_space(2)))
# define __percpu __attribute__((noderef, address_space(3)))
探索
-------------------------------------
在参考1中说,addres_space(v)
v:1 gcc中的定义是用户空间;
v:2 gcc中的定义是io存储空间(或许io寄存器空间更合适吧)。
作者还猜测了 v为0时内核空间。在2.6.36.3 中验证了作者的猜测。 作为发挥,就把后来的添加注释
address_space(v)
-------------------------------------
v: 0 内核空间
v: 1 用户空间
v: 2 io存储空间
v: 3 cpu空间(我感到这有点生搬硬套)
是这样吗?从哪里可以验证?
文章中还提到是gcc调用sparse分析的。应该看看sparse中的定义。
sparse 源码(http://codemonkey.org.uk/projects/git-snapshots/sparse/)
搜索代码
一处:
- address_space
- -------------------------------------
- ->ident-list.h
- IDENT(address_space);
- -->
- #define IDENT(n) __IDENT(n## _ident, #n, 0)
- =====================================
在这个文件的上下文中可以确定address_space是关键字
另一处
- address_space
- -------------------------------------
- ->parse.c
- static struct init_keyword {
- const char *name;
- enum namespace ns;
- unsigned long modifiers;
- struct symbol_op *op;
- struct symbol *type;
- } keyword_table[] = {
- /* .... */
- { "address_space",NS_KEYWORD, .op = &address_space_op },
- /* ... */
- };
- 这是关键字,有什么操作呢?关注symbol_op结构的address_space_op:
- =====================================
- address_space_op
- -------------------------------------
- -->parse.c
- static struct symbol_op address_space_op = {
- .attribute = attribute_address_space,
- };
- =====================================
- attribute_address_space
- -------------------------------------
- --->parse.c
- static attr_t attribute_address_space ;
- struct symbol_op
- -------------------------------------
- --->symbol.h
- struct symbol_op {
- /* ..... */
- /* keywords */
- struct token *(*attribute)(struct token *token,
- struct symbol *attr, struct decl_state *ctx);
- /* ..... */
- };
- =====================================
- attr_t
- -------------------------------------
- ---->parse.c
- typedef struct token *attr_t(struct token *,
- struct symbol *, struct decl_state *);
- attribute_address_space
- -------------------------------------
- ---->parse.c
- static struct token *
- attribute_address_space(struct token *token,
- struct symbol *attr, struct decl_state *ctx)
- {
- struct expression *expr = NULL;
- int as;
- token = expect(token, '(', "after address_space attribute");
- token = conditional_expression(token, &expr);
- if (expr) {
- as = const_expression_value(expr);
- if (Waddress_space && as)
- ctx->ctype.as = as;
- }
- token = expect(token, ')', "after address_space attribute");
- return token;
- }
- =====================================
疑问
-------------------------------------
再往下追踪,也没有结果,希望得到高人指点。疑问是:
* 怎么和内核空间(3G-4G)、用户空间(低于3G)相联系起来?
* 里面的address_space(v) (v=0,1,2,3,...)怎么起作用的?