谈到Linux的内存管理,虚拟内存,物理内存,总是似懂非懂,看了很多理论,也是看了忘,忘了再看。有机会从一个实际案例去研究内存。
1.AMP架构下,Linux和裸机通过共享内存通信
AMP架构,Linux和裸机分别运行在不同的CPU上,Linux负责共享资源的划分与管理
1.1.Linux端设备树中规定Shared Memory
reserved-memory {
#address-cells = <2>;
#size-cells = <2>;
ranges;
/* remote amp core address */
amp_shmem_reserved: amp-shmem@7800000 {
reg = <0x0 0x7800000 0x0 0x300000>;
no-map;
};
/* mcu address */
mcu_reserved: mcu@7b00000 {
reg = <0x0 0x7b00000 0x0 0x100000>;
no-map;
};
rpmsg_reserved: rpmsg@7c00000 {
reg = <0x0 0x07c00000 0x0 0x400000>;
no-map;
};
rpmsg_dma_reserved: rpmsg-dma@8000000 {
compatible = "shared-dma-pool";
reg = <0x0 0x08000000 0x0 0x100000>;
no-map;
};
可通过/proc/iomem验证
# cat /proc/iomem | grep reserved
07800000-080fffff : reserved
08200000-083fffff : reserved
1.2.裸机端访问shared memory
链接脚本和makefile编译文件,定义与Linux约定的物理地址,在裸机程序没有开启MMU的情况下,可以直接使用这个物理地址__linux_share_rpmsg_start__访存;
// gcc_arm.ld.S
LINUX_RPMSG (rxw) : ORIGIN = LINUX_RPMSG_BASE, LENGTH = LINUX_RPMSG_SIZE /* shared memory for linux rpmsg */
.linux_share_rpmsg (NOLOAD):
{
PROVIDE(__linux_share_rpmsg_start__ = .);
. += LINUX_RPMSG_SIZE;
PROVIDE(__linux_share_rpmsg_end__ = .);
} > LINUX_RPMSG
//makefile
LINUX_RPMSG_BASE ?= 0x07c00000
LINUX_RPMSG_SIZE ?= 0x00500000