Reputation: 1
I'm having trouble using memset()
in C; let me explain.
I'm working with embedded Linux on a zynq ultrascale+ board running Linux kernel v4.14. I'm performing some DMA transfers from the logic side of the FPGA to the processing system (PS), so I set aside a RAM section so the kernel doesn't use it...
I added this to the devicetree.
memory@0 {
device_type = "memory";
reg = <0x0 0x0 0x0 0x7ff00000>, <0x00000008 0x00000000 0x0 0x80000000>;
};
reserved-memory {
compatible = "xlnx,reserved-memory";
#address-cells = <2>;
#size-cells = <2>;
ranges;
reserved: buffer@800000000 {
no-map;
reg = <0x00000008 0x00000000 0x0 0x40000000>;
};
};
It reserves the memory as specified.
When I wrote my.c code to use this reserved memory, it worked properly except for the use of the memset()
function...
If I try to use memset(ram_vaddress, 0, size);
, I get this:
[ 2386.395781] test[3691]: unhandled alignment fault (7) at 0x7f9cb88080, esr 0x92000061, in libc-2.27.so[7f9c926000+13b000]
...
Bus error (core dumped)
If I use a for loop to iterate through all memory addresses and set it to 0, it works, but it is extremely slow.
I'm not very experienced with memory management, etc. So maybe I'm missing a concept or doing something incorrectly.
It appears that the allocated virtual memory is not continuous or aligned, but I am unable to determine why. Debugging the physical address, I can read what I write on it, indicating that the part that connects to the physical address works.
Here is the code below.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include "parameters.h"
// Allocate memory
int configure_memory_allocation(size_t size, unsigned int **virtual_addr, uint64_t physical_address)\
{
// Open /dev/mem which represents the whole physical memory
int memory_handle = open("/dev/mem", O_RDWR | O_SYNC);
if (memory_handle < 0) {
perror("open");
return 1;
}
*virtual_addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, memory_handle, physical_address); // Memory map source address
if (*virtual_addr == MAP_FAILED) {
perror("mmap");
close(memory_handle);
return 1;
}
close(memory_handle);
return 0;
}
int main()
{
// Variables
size_t size = 4096;
uint64_t physical_address = 0x800000000;
// Mapped devices
unsigned int *ram_vaddress = NULL;
if (configure_memory_allocation(size, &ram_vaddress, physical_address) != 0) {
// Handle error
fprintf(stderr, "Failed to configure memory allocation.\n");
return 1;
} else {
printf("configure_memory_allocation successfully\n");
}
for (int i = 0; i < size / 4; i++) {
ram_vaddress[i] = i;
}
for (int i = 0; i < 10; i++) {
printf("%08x ram_vaddress[%d] = %d\n", &ram_vaddress[i], i, ram_vaddress[i]);
}
printf("\n\n");
// Use memset to clear the memory
memset(ram_vaddress, 0, size);
munmap(ram_vaddress, size);
}
I tried memset(&ram_vaddress, 0, size);
and memset(&ram_vaddress[0], 0, size);
in a desperate attempt to work around.
Upvotes: 0
Views: 60