如何保留一段物理内存空间不被Linux内核占用?

676 views
Skip to first unread message

闫林

unread,
May 16, 2013, 11:29:35 PM5/16/13
to sh...@googlegroups.com
How can I reserve a physical memory region  i.e. 0x80000000 - 0x800fffff
which kernel
should not use for any memory allocation,
while I can access this reserved mem region by ioremap()  ???

Thank you!

Gao Ya'nan

unread,
May 16, 2013, 11:32:30 PM5/16/13
to sh...@googlegroups.com
试一下 mmap=0x100000$0x80000000


--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Shanghai Linux User Group”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 shlug+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 

闫林

unread,
May 16, 2013, 11:59:53 PM5/16/13
to sh...@googlegroups.com
我查到的是也是这样的:
http://unix.stackexchange.com/questions/37729/how-can-i-reserve-a-block-of-memory-from-the-linux-kernel
http://www.cyberciti.biz/howto/question/static/linux-kernel-parameters.php

还有一种是通过bootloader实现,这种针对的是ARM/AVR平台。
https://groups.google.com/forum/?fromgroups#!topic/android-kernel/HsRyyfrUdCw
http://lxr.free-electrons.com/ident?i=ATAG_MEM

我的问题是,有没有驱动程序的实现方法?
我试着用request_mem_region()进行占用,无法占用`cat /proc/iomem` 显示为“System RAM”的内存段。


2013/5/17 Gao Ya'nan <abutt...@gmail.com>

Gao Ya'nan

unread,
May 17, 2013, 12:13:12 AM5/17/13
to sh...@googlegroups.com
不能,因为驱动程序初始化通常是在 Linux MM 初始化之后了,而要内核不管理特定物理内存空间最好版本就是用命令。

zergmk2

unread,
May 17, 2013, 12:26:53 AM5/17/13
to shlug
有一个叫 memblock_reserve 的函数,在系统初始化的时候帮你保留内存。

liyaoshi

unread,
May 17, 2013, 12:27:11 AM5/17/13
to sh...@googlegroups.com
如果不用flat memory呢?

liyaoshi

unread,
May 17, 2013, 12:33:21 AM5/17/13
to sh...@googlegroups.com
看上去也就1M保留?

小于2M的话,kernel 可以直接申请,就是不知道你这个80000000 +1M的地址,是固定的地方还是随便什么地方多可以

如果是arm,atag的参数传给kernel的时候,就从80100000 开始,atag_mem_size 主动减1M


TI的片子?


2013/5/17 zergmk2 <zer...@gmail.com>

Gao Ya'nan

unread,
May 17, 2013, 2:08:28 AM5/17/13
to sh...@googlegroups.com
前提是能修改内核代码,然后重新构建,呵呵。

闫林

unread,
May 17, 2013, 3:43:19 AM5/17/13
to sh...@googlegroups.com
首先谢谢大家。

(1) @liyaoshi 这里的0x80000000 - 0x800fffff只是一个例子。
        实际我想达到的是保留任意位置任意大小的内存。

(2)@zergmk2 我试着使用了memblock_reserve函数。
         这个函数是内核初始化用到的,编译成模块insmod会产生Unknown symbol错误。
        所以,@Gao Ya'nan 使用这个函数需要修改内核代码,重新构建。

(3) @liyaoshi 如果是ARM,使用u-boot,如何改变atag传给内核的参数?
         我并不想针对某种特定平台。只是手头有块zedboard,比较感兴趣。

现在看来,最简便可行的方法是
step 1: 通过内核memmap参数保留内存。
step 2: 编写内核模块,使用ioremap将物理内存到虚拟地址空间,创建字符设备。
step 3: 用户程序通过
字符设备访问。
大家觉得可行否?

liyaoshi

unread,
May 17, 2013, 3:58:08 AM5/17/13
to sh...@googlegroups.com
任意大小的话,就是很大的意思

很大的内存只能在启动的时候申请

闫林

unread,
May 17, 2013, 4:06:38 AM5/17/13
to sh...@googlegroups.com
有谁知道"memmap="内核参数解析的实现位置?
我在LXR上没有搜到。

Chaos Eternal

unread,
May 17, 2013, 4:09:24 AM5/17/13
to sh...@googlegroups.com

你打算这段地址拿来干啥?dma?

liyaoshi

unread,
May 17, 2013, 4:17:40 AM5/17/13
to sh...@googlegroups.com
我猜你要的大概是memsize

Gao Ya'nan

unread,
May 17, 2013, 10:28:59 AM5/17/13
to sh...@googlegroups.com
见 Documentation/kernel-parameters.txt,如果只是用户态使用,根本不需要再用内核模块了,直接 mmap 好了。


2013/5/17 闫林 <godspe...@gmail.com>

闫林

unread,
May 17, 2013, 11:20:06 AM5/17/13
to sh...@googlegroups.com
找到memmap的实现了,"memmap="的内核参数只针对x86架构有效。
http://lxr.free-electrons.com/source/arch/x86/kernel/e820.c#L880

@Gao Ya'nan  据我所知,mmap用于将文件映射设到内存空间。
用户态要使用一段物理内存,没有驱动程序进行映射应该是不行的。

David Pu

unread,
May 17, 2013, 7:01:22 PM5/17/13
to sh...@googlegroups.com
对arm linux,最直接的就是在mem=参数里减掉你要reserve的大小了,如果你这个reserve的是io memory,也可以自己在machine结构里的io_xxx什么的回调里写自己的映射规则,或者是fixup之类的回调里用前面说的函数reserve.
kernel代码里遍地都是类似用法的例子。tegra的kernel里甚至有自己reserve,自己实现不依赖于slab的内存管理,然后再在这上面实现kernel和user land统一的访问接口的。
-------
Sent from my deovo v5 android phone
Reply all
Reply to author
Forward
0 new messages