LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Share reserved memory at boot time to user space (https://www.linuxquestions.org/questions/linux-newbie-8/share-reserved-memory-at-boot-time-to-user-space-4175582656/)

modher 06-20-2016 05:28 AM

Share reserved memory at boot time to user space
 
I am trying to share 30M of memory reserved at boot time (mem = 2G memmap=30M$2G)to user space using remap_pfn_range and mmap, bellow is my driver code:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
// #include <asm/error.h>

#define MAP_MAJOR 150

#define RAW_DATA_SIZE 0x1E00000 // 30 Mo
#define RAW_DATA_OFFSET 0x80000000 //2G

int results;
static void *rawdataStart = NULL;

static int map_mmap(struct file *filp, struct vm_area_struct *vma);

struct file_operations map_fops = {
.open = nonseekable_open,
.mmap = map_mmap
};

static int map_mmap(struct file *filp, struct vm_area_struct *vma) {
if (rawdataStart == NULL) {
printk(KERN_ERR "Memory not mapped!\n");
return -EAGAIN;
}
if ((vma->vm_end - vma->vm_start) != RAW_DATA_SIZE) {
printk(KERN_ERR "Error: sizes don't match (buffer size = %d, requested size = %lu)\n", RAW_DATA_SIZE, vma->vm_end - vma->vm_start);
return -EAGAIN;
}
results = remap_pfn_range(vma, vma->vm_start, RAW_DATA_OFFSET >> PAGE_SHIFT, RAW_DATA_SIZE, PAGE_SHARED);
if (results != 0) {
printk(KERN_ERR "Error in calling remap_pfn_range: returned %d\n", results);
return -EAGAIN;
}

return 0;
}

static int __init map_init(void)
{

printk("init map module\n");

if (register_chrdev(MAP_MAJOR,"mapReserved", &map_fops) <0 )
{
printk("unable to get major for map module\n");
return -EBUSY;
}


rawdataStart = ioremap(RAW_DATA_OFFSET, RAW_DATA_SIZE);
if (rawdataStart == NULL) {
printk(KERN_ERR "Unable to remap memory\n");
return 1;
}
printk(KERN_INFO "ioremap returned %p\n", rawdataStart);

return 0;
}

void __exit map_cleanup(void)
{
printk("exit map module\n");
unregister_chrdev(MAP_MAJOR,"mapReserved");
if (rawdataStart != NULL) {
printk(KERN_INFO "Unmapping memory at %p\n", rawdataStart);
iounmap(rawdataStart);
} else {
printk(KERN_WARNING "No memory to unmap!\n");
}

return;
}

MODULE_LICENSE("GPL");

module_init( map_init);
module_exit( map_cleanup);

and from user space i am using mmap :

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#define RAW_DATA_SIZE 0x1E00000


int main(void)
{
int * data;
int fd = open("/dev/mapReserved", O_RDWR);
if (fd == -1) {
perror("open error...\n");
return -1;
}
data = mmap(NULL, RAW_DATA_SIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 4096);
close(fd);
return 0;
}

but i am getting the following error when running user space app after inserting the module:

open error...
: No such file or directory


can any one help me?

modher 06-23-2016 04:43 AM

I have solved this problem and posted the full code on stackoverflow :

http://stackoverflow.com/questions/3...emap-pfn-range


All times are GMT -5. The time now is 09:27 AM.