linux通过内核启动参数预留系统内存

1 首先正常启动内核

我们需要启动内核后查询一些信息。


2. 查看系统内存信息

  内核启动后通过 

cat /proc/iomem
  
  
   查看内存的分布,结果类似:
100000000-10dffffff : System RAM
  
  

  但是我们会看到有很多条这样的项,我们主要关注"System RAM"这样的项,因为这代表系统内存。


3. 在System RAM的段,找一个地址最高的RAM项的结束地址,然后计算出预留的起始地址

  例如,我需要预留4M内存,起始地址就是

0x10dffffff - 0x400000 = 0x10DBFFFFF
  
  

  这个是内存物理地址,你所选的预留内存的地址是需要由你自己来规划的。我这里之所以选择末端地址,是因为我的机器启动时,末端的地址基本上用不到。


4. 通过内核启动参数预留内存

  在启动命令行添加如下参数:
memmap=4m$0x10DBFFFFF
  
  
  这样就表示从0x10DBFFFFF处预留出4M内存。
  注意:如果是通过grub启动,需要确定grub是否支持识别$,否则需要通过转义字符:
memmap=4m\$0x10DBFFFFF
  
  

5. 验证

  最后要做的事情就是重启内核,等内核启动完成后,再通过cat /proc/iomem,观察我们预留的 0x10DBFFFFF ~ 0x10dFFFFFE是否是 “reserved”状态。


参考:https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CMA (Contiguous Memory Allocator) is a feature in the Linux kernel that allows for the allocation of large, physically contiguous memory regions. To use CMA, you need to configure the kernel with the CONFIG_CMA option and provide a reserved memory region for CMA to use. Once CMA is configured and initialized, you can allocate memory using the dma_alloc_coherent() function. This function takes a device pointer and a size parameter, and returns a pointer to a physically contiguous memory region that can be used for DMA. To set up a reserved memory region for CMA to use, you can use the memblock_find_in_range() function. This function searches for a contiguous range of memory within a specified address range that meets certain requirements, such as a minimum size or alignment. Here is an example of how to use memblock_find_in_range() to set up a reserved memory region for CMA: ``` #include <linux/memblock.h> /* Set up a reserved memory region for CMA */ void __init setup_cma_reserved_memory(void) { phys_addr_t start = 0x80000000; /* start of address range */ phys_addr_t end = 0x90000000; /* end of address range */ size_t size = 0x10000000; /* minimum size of reserved region */ unsigned long align = PAGE_SIZE;/* alignment of reserved region */ phys_addr_t addr = memblock_find_in_range(start, end, size, align); if (!addr) { printk(KERN_ERR "Failed to find contiguous memory region for CMA\n"); return; } memblock_reserve(addr, size); } ``` In this example, we search for a contiguous memory region within the address range 0x80000000 to 0x90000000 that is at least 0x10000000 bytes in size and aligned to the page size. If a suitable region is found, we reserve it using the memblock_reserve() function. This reserved memory region can then be used by CMA for DMA allocations.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值