Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-20-2016, 06:28 AM   #1
LQ Newbie
Registered: Jun 2016
Posts: 2

Rep: Reputation: Disabled
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");
if (rawdataStart != NULL) {
printk(KERN_INFO "Unmapping memory at %p\n", rawdataStart);
} else {
printk(KERN_WARNING "No memory to unmap!\n");



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;
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?

Last edited by modher; 06-20-2016 at 06:32 AM. Reason: clarification
Old 06-23-2016, 05:43 AM   #2
LQ Newbie
Registered: Jun 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
I have solved this problem and posted the full code on stackoverflow :


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
kdump reserved memory not getting reserved Rohant Linux - Server 3 07-02-2013 09:59 AM
memory management of a process in kernel space and user space. embeddedlinux Linux - Newbie 1 05-08-2012 09:08 AM
How to measure kernel space time and user space time in linux bhas.bhaskar Linux - Newbie 1 11-28-2010 12:01 PM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 10:59 AM
How to share data b/w user space and kernel space nandac Linux - Kernel 1 11-28-2006 11:15 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:29 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration