- 论坛徽章:
- 0
|
在内核窥视用户态
首先,环境:VMware Server上运行的ubuntu10.4,arch为x86_64。
先看下面这个程序:- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int dataA;
- char bufA[1000];
- int main()
- {
- int dataB;
- int i_GetChar;
- char * bufB = NULL;
- bufB = malloc(1500);
- if (NULL == bufB)
- {
- printf("malloc failed\n");
- return 0;
- }
- dataA = 0X55aa;
- dataB = 0Xaa55;
- memcpy(bufA, "bufA org data", strlen("bufA org data"));
- memcpy(bufB, "bufB org data", strlen("bufB org data"));
- printf("bufA = %p, bufB = %p, &dataA = %p, &dataB = %p\n", bufA, bufB, &dataA, &dataB);
-
- for ( ; ; )
- {
- printf("get char(p:print; e:exit):");
-
- i_GetChar = getchar();
- if ('p' == i_GetChar)
- {
- printf("bufA: %s\n", bufA);
- printf("bufA: %s\n", bufB);
- printf("dataA: 0x%x\n", dataA);
- printf("dataA: 0x%x\n", dataB);
- }
- else if ('e' == i_GetChar)
- {
- break;
- }
- }
- free(bufB);
-
- return 0;
- }
复制代码 这个程序中有:
一个int型的全局变量dataA;
一个int型的局部变量dataB;
一个char型的全局数组bufA;
一段malloc的空间bufB。
程序先输出以上变量的地址,然后按"p"输出一次内容,按"e"退出程序。
接下来,简单的分析一下:
bufA = 0x601080, bufB = 0x6a0010, &dataA = 0x601468, &dataB = 0x7fff337284ac
仅从地址上看,他们就在不同的内存区域。
bufA和dataA是全局变量,在数据区;
bufB是malloc来的,在堆中;
dataB是局部变量,在进程的运行栈中。
抄一段linux自带的关于x86_64下地址空间的说明:
0000000000000000 - 00007fffffffffff (=47 bits) user space, different per mm
hole caused by [48:63] sign extension
ffff800000000000 - ffff80ffffffffff (=40 bits) guard hole
ffff880000000000 - ffffc7ffffffffff (=64 TB) direct mapping of all phys. memory
ffffc80000000000 - ffffc8ffffffffff (=40 bits) hole
ffffc90000000000 - ffffe8ffffffffff (=45 bits) vmalloc/ioremap space
ffffe90000000000 - ffffe9ffffffffff (=40 bits) hole
ffffea0000000000 - ffffeaffffffffff (=40 bits) virtual memory map (1TB)
... unused hole ...
ffffffff80000000 - ffffffffa0000000 (=512 MB) kernel text mapping, from phys 0
ffffffffa0000000 - fffffffffff00000 (=1536 MB) module mapping space
可见,这些变量的地址都在user space中。
对各个用户态进程来说,地址空间都是0000000000000000 - 00007fffffffffff,而不会冲突;
因为这只是虚拟地址,每个用户态进程都拥有自身的页表,相同的虚拟地址地址经过不同的页表转换为不同的物理地址;
而内核的页表并不映射user space,这些后面会用到。 |
评分
-
查看全部评分
|