A bus error that occurs when mmap memory is used with memcpy

126 views Asked by At

Recently, when I make a Linux device driver and memcpy memory obtained with mmap, the Linux kernel has a bus error and I ask questions.

Here's the code that caused the problem

typedef struct buffer_t {
    unsigned int size;
    unsigned long phys_addr;
    unsigned long base;
    unsigned long virt_addr;
} buffer_t;

int main(void)
{
  buffer_t vdb;
  Uint8 *buf = 0;
  int32 len = 0;

  vdi->vpu_fd = open(DEVICE_NAME, O_RDWR);
  len = 100;


  buf = (Uint8*)malloc(len);

  vdb.virt_addr = (unsigned long)mmap(NULL, vdb.size, PROT_READ | PROT_WRITE, MAP_SHARED, vdi->vpu_fd, vdb.phys_addr);

  memcpy(data, (const char *)(vdb.virt_addr), len);
}

When executing the above code, an Bus error occurs while performing memcpy.

It's not impossible to write values for the address of the data and the address of vdb.virt_addr.

like this.

The code below does not have a bus error.

  for( j = 0 ; j < len ; j++)
  {
    *(data+j) = *(((unsigned char*)(vdb.virt_addr))+j);
  }

And if the memcpy size is a multiple of 8 as shown below, it works (although the rest is thrown away)

The code below does not have a bus error.

int r_len = (len/8)*8;
memcpy(data, (const char *)(vdb.virt_addr), r_len);

Has anyone experienced this or knows any solutions?

0

There are 0 answers