码迷,mamicode.com
首页 > 系统相关 > 详细

[分享]Linux的非对齐访问【转】

时间:2020-06-29 15:24:55      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:tar   方式   tac   har   ase   page   task   memcpy   strong   

转自:https://www.cnblogs.com/hankfu/p/12970695.html

1. Linux的非对齐访问

Linux下,可以在设备树里保留一段内存,留给用户自己管理和使用,Linux保证不会使用保留内存。在使用中,有人发现,保留内存不能使用非对齐的方式访问。

经研究,如果在保留内存时,声明了“no-map”属性,Linux会把这段内存映射为strongly order模式,不支持非对齐的方式访问。
如果在保留内存时,没有声明“no-map”属性,Linux会把这段内存映射为normal模式,支持非对齐的方式访问。

 

2. 测试环境

ZCU102, Xilinx Linux image 2018.2

 

3. strongly order模式的设备树
/ {
reserved-memory {
#address-cells = <2>;
#size-cells = <2>;
ranges;

videoinmem: buffer0@0x20000000 {
no-map;
reg = <0x0 0x20000000 0x0 0x6000000>;
};
};
};

 

4. 测试代码
void main(void)
{
char buf[1280] = "123456789012345";
char *map_base = NULL;
int fd;

fd = open("/dev/mem",O_RDWR | O_SYNC);
map_base = mmap(0, 0x20000000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x20000000);

printf("map_base = %p\n",map_base);
printf("Copy to reserved memory with length: 64\n");
// void *memcpy(void*dest, const void *src, size_t n);
memcpy(map_base,buf,64);

printf("Copy to reserved memory with length: 68\n");
memcpy(map_base,buf,68); // sigbus exception

printf("Copy to reserved memory with length: 65\n");
memcpy(map_base,buf,65);// sigbus exception
}

测试记录
Copy to reserved memory with length: 64
Copy to reserved memory with length: 68
[ 867.503554] linux_a53_mem.e[2399]: unhandled alignment fault (7) at 0x7f99b1d024, esr 0x92000061, in libc-2.26.so[7fb9b1d000+138000]
[ 867.518919] CPU: 1 PID: 2399 Comm: linux_a53_mem.e Not tainted 4.14.0-xilinx-v2018.2 #1
[ 867.526890] Hardware name: ZynqMP ZCU102 Rev1.0 (DT)
[ 867.531834] task: ffffffc87acec480 task.stack: ffffff800d230000
[ 867.537739] PC is at 0x7fb9b9f4a4
[ 867.541033] LR is at 0x40087c
[ 867.543978] pc : [<0000007fb9b9f4a4>] lr : [<000000000040087c>] pstate: 80000000
[ 867.551367] sp : 0000007fcf4ff900
[ 867.554660] x29: 0000007fcf4ff900 x28: 0000000000000000
[ 867.559958] x27: 0000000000000000 x26: 0000000000000000
[ 867.565253] x25: 0000000000000000 x24: 0000000000000000
[ 867.570549] x23: 0000000000000000 x22: 0000000000000000
[ 867.575843] x21: 0000000000400610 x20: 0000000000000000
[ 867.581139] x19: 0000000000400910 x18: 0000000000000463
[ 867.586434] x17: 0000007fb9b9f3d0 x16: 0000000000412000
[ 867.591729] x15: 0000007fb9b20d00 x14: 0000000000000043
[ 867.597024] x13: 0000000000000000 x12: 0000000000000000
[ 867.602321] x11: 0000000000000000 x10: 0000000000000000
[ 867.607615] x9 : 0000000000000000 x8 : 0000000000000000
[ 867.612912] x7 : 0035343332313039 x6 : 3837363534333231
[ 867.618206] x5 : 0000007f99b1d044 x4 : 0000000000000000
[ 867.623501] x3 : 0000000000000000 x2 : 0000000000000000
[ 867.628797] x1 : 0000000000000000 x0 : 0000007f99b1d000
Bus error

5. 参考文章
https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18841683/Linux+Reserved+Memory
https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18842412/Accessing+BRAM+In+Linux

[分享]Linux的非对齐访问【转】

标签:tar   方式   tac   har   ase   page   task   memcpy   strong   

原文地址:https://www.cnblogs.com/sky-heaven/p/13207966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!