主题 : 继上一贴 “linux内存管理,将2G的内存分出512M作为块设备,写个驱动自己管理” 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 83699
精华: 0
发帖: 11
金钱: 55 两
威望: 11 点
综合积分: 22 分
注册时间: 2012-12-11
最后登录: 2017-09-13
楼主  发表于: 2012-12-27 16:10

 继上一贴 “linux内存管理,将2G的内存分出512M作为块设备,写个驱动自己管理”

ubuntu12.04 在虚VMware上安装的,在VMware中分配给ubuntu的是2G内存 ,通过在ubuntu的grub.cfg文件中设置mem=1536M实现预留512M的内存,然后要写一个块设备驱动程序,来管理预留512M的内存,

问题(1)
   没有设置mem之前
root@wensheng-virtual-machine:ram#free
             total       used       free     shared    buffers     cached
Mem:       2048120    1303512     744608          0      57440     668940
-/+ buffers/cache:     577132    1470988
Swap:      2094076          0    2094076
root@wensheng-virtual-machine:ram#
设置mem=1536M之后
wensheng@wensheng-virtual-machine:~$free
             total       used       free     shared    buffers     cached
Mem:       1532152     435564    1096588          0      45852     179284
-/+ buffers/cache:     210428    1321724
Swap:      2094076          0    2094076
wensheng@wensheng-virtual-machine:~$
为什么不标准的2G(2097152KB)或1.5G(1572864KB)而是2048120KB和1532152KB,(这个是不重点,但个人不解)
问题(2)(这个是重点求解)预留的512M内存的物理地址是多少 ,是0x60000000(1.5G)到0x80000000(2G)吗,我的PC机是4G内存win7系统,分配给ubuntu的是2G,
问题(3) 关于这个驱动程序(这方面没经验),通过ioremap()映射内存,ioremap error for 0x60000000-0x80000000, requested 0x10, got 0x0
 出错,不能申0x60000000-0x80000000,,什么原因。
问题(4)编译时经有时候出现下面问题
。。。。。
make[2]: *** No rule to make target `kernel/bounds.c', needed by `kernel/bounds.s'.  Stop.
make[1]: *** [prepare0] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-23-generic'
make: *** [default] Error 2
什么原因,没怎么搞清楚,在网上查了一下有说是差头文件,有说内核源码不配套,求解

最后,就是关那这个驱动程序 ,因为没有这方面的经验,所以没有很好的思路那位大能指点一下,想找一些源代码参考,但是不知道参考那些那,drivers及block目录下太多,求推荐(涉及到内存映射的)
  

*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2012-12-27 17:39
问题(2)(这个是重点求解)预留的512M内存的物理地址是多少 ,是0x60000000(1.5G)到0x80000000(2G)吗,我的PC机是4G内存win7系统,分配给ubuntu的是2G,

我不知道你的ubuntu的内核启动信息(通过dmesg输出)里是不是有类似下面的信息

[    0.000000] BIOS-provided physical RAM map:
[    0.000000]  BIOS-e820: 0000000000000000 - 000000000009fc00 (usable)
[    0.000000]  BIOS-e820: 000000000009fc00 - 00000000000a0000 (reserved)
[    0.000000]  BIOS-e820: 00000000000f0000 - 0000000000100000 (reserved)
[    0.000000]  BIOS-e820: 0000000000100000 - 000000007fff0000 (usable)
[    0.000000]  BIOS-e820: 000000007fff0000 - 0000000080000000 (ACPI data)
[    0.000000]  BIOS-e820: 00000000fffc0000 - 0000000100000000 (reserved)

其中(usable)的部分应该就是系统可用的RAM
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 83699
精华: 0
发帖: 11
金钱: 55 两
威望: 11 点
综合积分: 22 分
注册时间: 2012-12-11
最后登录: 2017-09-13
2楼  发表于: 2012-12-28 08:47

 回 1楼(kasim) 的帖子

下面是我的dmesg中启动信息一部分

[    0.000000] Linux version 3.2.0-23-generic (buildd@crested) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu4) ) #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 (Ubuntu 3.2.0-23.36-generic 3.2.14)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.2.0-23-generic root=UUID=7e6edb7b-b327-42e6-9d0f-87c1fee6c21b ro quiet splash mem=1536M
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Centaur CentaurHauls
[    0.000000] Disabled fast string operations
[    0.000000] BIOS-provided physical RAM map:
[    0.000000]  BIOS-e820: 0000000000000000 - 000000000009f400 (usable)
[    0.000000]  BIOS-e820: 000000000009f400 - 00000000000a0000 (reserved)
[    0.000000]  BIOS-e820: 00000000000ca000 - 00000000000cc000 (reserved)
[    0.000000]  BIOS-e820: 00000000000dc000 - 0000000000100000 (reserved)
[    0.000000]  BIOS-e820: 0000000000100000 - 000000007fee0000 (usable)
[    0.000000]  BIOS-e820: 000000007fee0000 - 000000007feff000 (ACPI data)
[    0.000000]  BIOS-e820: 000000007feff000 - 000000007ff00000 (ACPI NVS)
[    0.000000]  BIOS-e820: 000000007ff00000 - 0000000080000000 (usable)
[    0.000000]  BIOS-e820: 00000000e0000000 - 00000000f0000000 (reserved)
[    0.000000]  BIOS-e820: 00000000fec00000 - 00000000fec10000 (reserved)
[    0.000000]  BIOS-e820: 00000000fee00000 - 00000000fee01000 (reserved)
[    0.000000]  BIOS-e820: 00000000fffe0000 - 0000000100000000 (reserved)
[    0.000000] e820 remove range: 0000000060000000 - ffffffffffffffff (usable)
[    0.000000] NX (Execute Disable) protection: active
[    0.000000] user-defined physical RAM map:
[    0.000000]  user: 0000000000000000 - 000000000009f400 (usable)
[    0.000000]  user: 000000000009f400 - 00000000000a0000 (reserved)
[    0.000000]  user: 00000000000ca000 - 00000000000cc000 (reserved)
[    0.000000]  user: 00000000000dc000 - 0000000000100000 (reserved)
[    0.000000]  user: 0000000000100000 - 0000000060000000 (usable)
[    0.000000]  user: 000000007fee0000 - 000000007feff000 (ACPI data)
[    0.000000]  user: 000000007feff000 - 000000007ff00000 (ACPI NVS)
[    0.000000]  user: 00000000e0000000 - 00000000f0000000 (reserved)
[    0.000000]  user: 00000000fec00000 - 00000000fec10000 (reserved)
[    0.000000]  user: 00000000fee00000 - 00000000fee01000 (reserved)
[    0.000000]  user: 00000000fffe0000 - 0000000100000000 (reserved)
[    0.000000] DMI present.

          对这个信息我不是很清楚,在网上查了一下,BIOS-provided physical RAM map:是系统自己检可用内存, user-defined physical RAM map:是用户指定(mem=XXX),如果系统检测有
0000000000100000 - 000000007fee0000 (usable)可用,而通过用户指定后 0000000000100000 - 0000000060000000 (usable)可用,那0x0000000060000000 - 0x000000007fee0000是否就是我预留下来的那部分内存,如果我要用的话,可否用ioremap(0x0000000060000000,0x000000007fee0000 - 0x0000000060000000)来直接得到相应的映射虚拟地址,然后写驱动
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
3楼  发表于: 2012-12-28 09:48

 回 2楼(wenshengking) 的帖子

按我的理解,是这样的。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 83699
精华: 0
发帖: 11
金钱: 55 两
威望: 11 点
综合积分: 22 分
注册时间: 2012-12-11
最后登录: 2017-09-13
4楼  发表于: 2012-12-28 11:49

 回 3楼(kasim) 的帖子

     非常感谢,目前我已成功映射内存,驱动已完成,可以mount与umount,也可以在挂后的文件只建立文件,
通过fdisk -l查看到如下信息,

Disk /dev/myram: 535 MB, 535691264 bytes
1 heads, 32 sectors/track, 32696 cylinders, total 1046272 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/myram doesn't contain a valid partition table

说是没有分区表,不知道到问题大不大,暂时做到这一步,如有问题我将再跟贴。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
5楼  发表于: 2012-12-28 11:55

 回 4楼(wenshengking) 的帖子

恭喜恭喜,希望你能把你的成果分享出来,谢谢!
任何一个新的块设备都需要分区后才会有分区表,你可以用fdisk /dev/myram来分区:)
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 83699
精华: 0
发帖: 11
金钱: 55 两
威望: 11 点
综合积分: 22 分
注册时间: 2012-12-11
最后登录: 2017-09-13
6楼  发表于: 2012-12-28 12:37

 回 5楼(kasim) 的帖子

好的,因为这个不一定完善,等我整理完善后,我会及时分享出来