- 论坛徽章:
- 22
|
本帖最后由 amarant 于 2013-04-21 12:12 编辑
回复 15# embeddedlwp
你好,谢谢指出。关于mmap,我花了一点时间测试。用的是git上最新的内核,arch采用的是mips。结果跟你说的不太一样。测试过程如下
代码:- #include <stdio.h>
- #include <unistd.h>
- #include<sys/mman.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- int main(int argc, char *argv[])
- {
- int fd, i;
- long* map_base, *addr;
- off_t offset = 0;
- printf("start\n");
- fd = open("/dev/mem", O_RDWR|O_SYNC);
- if ( fd == -1 ) {
- printf("open failed\n");
- return -1;
- }
- if (argv[1])
- offset = atol(argv[1]);
- map_base = mmap(0, 0x10*sizeof(long), PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
- if ( (long)map_base < 0) {
- printf("mmap failed\n");
- return -1;
- }
- addr = map_base;
- printf("phys: %p -> virt: %p\n", (void*)offset, addr);
- for (i = 0; i < 0x10; ++i){
- printf("address: 0x%p content 0x%lx\n", addr, *addr);
- addr ++;
- }
- munmap(map_base, 0x10*sizeof(long));
- close(fd);
- return (1);
- }
复制代码 执行结果:- / # /a.out 2097152
- start
- phys: 0x200000 -> virt: 0x773c9000
- address: 0x0x773c9000 content 0x0
- address: 0x0x773c9004 content 0xde220000
- address: 0x0x773c9008 content 0x212fa
- address: 0x0x773c900c content 0x30420001
- address: 0x0x773c9010 content 0x2c420001
- address: 0x0x773c9014 content 0x20336
- address: 0x0x773c9018 content 0xc0b465a
- address: 0x0x773c901c content 0xde320030
- address: 0x0x773c9020 content 0x24030010
- address: 0x0x773c9024 content 0xd2440000
- address: 0x0x773c9028 content 0x832825
- address: 0x0x773c902c content 0xf2450000
- address: 0x0x773c9030 content 0x10a0fffc
- address: 0x0x773c9034 content 0x0
- address: 0x0x773c9038 content 0x30840010
- address: 0x0x773c903c content 0x14800060
- / # QEMU 1.4.50 monitor - type 'help' for more information
- (qemu) xp/16 0x200000
- 0000000000200000: 0x00000000 0xde220000 0x000212fa 0x30420001
- 0000000000200010: 0x2c420001 0x00020336 0x0c0b465a 0xde320030
- 0000000000200020: 0x24030010 0xd2440000 0x00832825 0xf2450000
- 0000000000200030: 0x10a0fffc 0x00000000 0x30840010 0x14800060
- (qemu)
复制代码 |
|