Tool/software:
Reserve an area of memory for self owned drivers to use。
Device Tree Configuration
reserved-memory { #address-cells = <2>; #size-cells = <2>; ranges; cotrust_region: cotrust@fff00000 { no-map; reg = <0x00 0xa0000000 0x00 0x00100000>; /* for cotrust */ }; } cotrusts { compatible = "cotrust"; pinctrl-names = "default"; memory-region = <&cotrust_region>; };
Driver code
#pragma pack(push, 1) struct foo { char one; int two; char three; short four; } ; #pragma pack(pop) { u8 *p; struct foo* pf; node = of_parse_phandle(dev->of_node, "memory-region", 0); if(NULL == node) { printk("%s: NO memory-region specified\r\n", __FUNCTION__); return -ENODEV; } ret = of_address_to_resource(node, 0, &r); if(ret) { printk("%s: No memory address assigned to the region", __FUNCTION__); return -ENODEV; } //p = memremap( r.start, resource_size(&r), MEMREMAP_WB); //OK //p = ioremap( r.start, resource_size(&r)); //p = ioremap_cache( r.start, resource_size(&r)); //p = ioremap_wc( r.start, resource_size(&r)); p = ioremap_wt( r.start, resource_size(&r)); //OK pf = (struct foo*)p; pf->one = 0x11; pf->two = 0x22334455; pf->three = 0x6677; pf->four = 0x77; printk("%x\r\n", *((int*)(p + 1))); printk("%x\r\n", *((short*)(p + 5))); *((int*)(p + 1)) = 0xaaaaaaaa; *((short*)(p + 5)) = 0xbbbb; printk("%x\r\n", *((int*)(p + 1))); printk("%x\r\n", *((short*)(p + 5))); }
The program encountered an exception in printk("%x\r\n", *((short*)(p + 5)));
[ 56.866811] Unable to handle kernel paging request at virtual address ffff80000ae00005 [ 56.866819] Mem abort info: [ 56.866820] ESR = 0x0000000096000021 [ 56.866824] EC = 0x25: DABT (current EL), IL = 32 bits [ 56.866828] SET = 0, FnV = 0 [ 56.866831] EA = 0, S1PTW = 0 [ 56.866836] FSC = 0x21: alignment fault [ 56.866845] Data abort info: [ 56.866847] ISV = 0, ISS = 0x00000021 [ 56.866850] CM = 0, WnR = 0 [ 56.866853] swapper pgtable: 4k pages, 48-bit VAs, pgdp=00000000834f9000 [ 56.866858] [ffff80000ae00005] pgd=10000000fffff003, p4d=10000000fffff003, pud=10000000ffffe003, pmd=1000000085b87003, pte=00680000a0000713 [ 56.866885] Internal error: Oops: 0000000096000021 [#1] PREEMPT_RT SMP [ 56.866890] Modules linked in: fpga(O+) [ 56.866899] CPU: 3 PID: 1240 Comm: insmod Tainted: G O 6.1.83-rt28-dirty #2 [ 56.866907] Hardware name: Texas Instruments AM625 SK (DT) [ 56.866912] pstate: 40000005 (nZcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--) [ 56.866921] pc : 0xffff800000e80280 [ 56.866926] lr : 0xffff800000e80280 [ 56.866928] sp : ffff00000474b970 [ 56.866931] x29: ffff00000474b970 x28: 0000000000000001 x27: ffff800009c2cc10 [ 56.866939] x26: 0000000000000000 x25: ffff00000474bd20 x24: ffff000004632c80 [ 56.866948] x23: ffff800009f25820 x22: ffff800000e82348 x21: ffff800000e81128 [ 56.866956] x20: ffff80000ae00000 x19: ffff800000e82000 x18: ffffffffffffffff [ 56.866968] x17: 0000000000000007 x16: 0000000000000001 x15: ffff800009fe5e3b [ 56.866976] x14: 0000000000000001 x13: ffff800009fe5e30 x12: ffff80000947ed98 [ 56.866983] x11: ffff80000af01000 x10: 000000000000000a x9 : ffff800009c40ea0 [ 56.866991] x8 : ffff00000474b7b0 x7 : ffff800009c9b598 x6 : 0000000000000000 [ 56.867001] x5 : ffff00007f68e8e0 x4 : 0000000000000000 x3 : 0000000000000027 [ 56.867010] x2 : 0000000100000000 x1 : ffff0000014f6e40 x0 : 0000000000000009 [ 56.867019] Call trace: [ 56.867023] 0xffff800000e80280 [ 56.867026] 0xffff80000877a5a8 [ 56.867029] 0xffff800008777a78 [ 56.867031] 0xffff800008777d04 [ 56.867033] 0xffff800008777ddc [ 56.867035] 0xffff8000087785b0 [ 56.867039] 0xffff800008775880 [ 56.867043] 0xffff8000087774e4 [ 56.867047] 0xffff800008776e7c [ 56.867049] 0xffff800008779124 [ 56.867051] 0xffff80000877a1a4 [ 56.867053] 0xffff800000e84028 [ 56.867056] 0xffff800008013960 [ 56.867058] 0xffff8000080ee4d4 [ 56.867061] 0xffff8000080eff3c [ 56.867063] 0xffff8000080f0554 [ 56.867066] 0xffff8000080f05ec [ 56.867068] 0xffff8000080218ac [ 56.867071] 0xffff8000080219b0 [ 56.867073] 0xffff800008e5a494 [ 56.867075] 0xffff800008e5ba24 [ 56.867077] 0xffff800008011488 [ 56.867090] Code: f2eeecc0 f9000280 aa1503e0 95ff0838 (78c05281) [ 56.867095] ---[ end trace 0000000000000000 ]---
How to solve non aligned data access ?