Hi experts,
Currently I'm facing a problem that bus error occurs while initializing a member array of a struct, the struct is pointed to a mapped shared memory(mapped by mmap()).
Below is my test application code, the bus error occurs while memset data in the init() function. The shared memory is reserved for application use. It looks weird that the test code is quite simple, could this be a system configuration problem?
Currently I'm facing a problem that bus error occurs while initializing a member array of a struct, the struct is pointed to a mapped shared memory(mapped by mmap()).
Below is my test application code, the bus error occurs while memset data in the init() function. The shared memory is reserved for application use. It looks weird that the test code is quite simple, could this be a system configuration problem?
C:
#define appAlign(value, align) ((( (value) + ( (align) - 1 ) ) / (align) ) * (align) )
#define appFloor(value, align) (( (value) / (align) ) * (align) )
#define LOGGER_REC_BUFS_MEM_ADDR 0xB9600000UL
#define LOGGER_REC_BUFS_MEM_SIZE 0x00200000
#define ONE_REC_MAX_SIZE 0x0080000
struct LoggerRecord
{
uint8_t data[ONE_REC_MAX_SIZE];
void (*init)(uint8_t *data);
};
void init(uint8_t *data)
{
printf("Start Memset occured at 0x%p\n", data);
memset(data, 0, ONE_REC_MAX_SIZE);
printf("Memset occured at 0x%p\n", data);
};
/*
* Map physical address to virtual address
*/
void *phyToVirtMap(void *physAddr, uint32_t size) {
int32_t status = 0;
uint32_t pageSize = getpagesize();
void *virtAddr = NULL;
static int devMemFd = -1;
uintptr_t taddr;
uint32_t tsize;
if(devMemFd == -1) {
devMemFd = open("/dev/mem",O_RDWR|O_SYNC);
if(devMemFd < 0) {
status = -1;
}
printf("Allocated\n");
}
if((0 == status) && (0 <= devMemFd)) {
taddr = (uintptr_t)physAddr;
tsize = size;
tsize = appAlign(tsize + (taddr % pageSize), pageSize);
taddr = appFloor(taddr, pageSize);
printf("Before mmap\n");
virtAddr = mmap(
0,
tsize,
(PROT_READ | PROT_WRITE),
(MAP_SHARED),
devMemFd,
taddr);
printf("After mmap\n");
if(MAP_FAILED == virtAddr) {
virtAddr = NULL;
perror("Error: ");
printf("mmap failed\n");
}
else {
virtAddr = (void*)((uintptr_t)virtAddr + ((uintptr_t)physAddr % pageSize));
}
}
return virtAddr;
}
int main()
{
int status = 0;
uint8_t* recBufsBase = (uint8_t*)phyToVirtMap((void*)LOGGER_REC_BUFS_MEM_ADDR, LOGGER_REC_BUFS_MEM_SIZE);
if(recBufsBase == NULL)
{
printf("Virtual Address is NULL\n");
}
else
{
printf("Virt Addr = 0x%p\n",recBufsBase);
}
struct LoggerRecord *ptr = (struct LoggerRecord *) recBufsBase;
ptr->init = init;
ptr->init(ptr->data);
return status;
}
Last edited: