mapping ram memory to user space

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

hi, my system is powerpc. i am attempting to map logical address
returned by kmalloc to the user program. the code below doesn't
accomplish that task.  please let me know what i am doing wrong...thank
you in advance.

here's the driver:


#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h>   /* printk() */
#include <linux/slab.h>   /* kmalloc() */
#include <linux/fs.h>       /* everything... */
#include <linux/errno.h>    /* error codes */
#include <linux/types.h>    /* size_t */
#include <linux/mm.h>
#include <linux/kdev_t.h>
#include <asm/page.h>
#include <linux/cdev.h>

#include <linux/device.h>
#include <asm/io.h>
static unsigned char *buffer;
static int simple_major = 0;
module_param(simple_major, int, 0);
MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");

/*
 * Open the device; in fact, there's nothing to do here.
 */
static int simple_open (struct inode *inode, struct file *filp)
{
    return 0;
}


/*
 * Closing is just as simpler.
 */
static int simple_release(struct inode *inode, struct file *filp)
{
    return 0;
}



/*
 * Common VMA ops.
 */

void simple_vma_open(struct vm_area_struct *vma)
{
    printk(KERN_NOTICE "Simple VMA open, virt %lx, phys %lx\n",
            vma->vm_start, vma->vm_pgoff << PAGE_SHIFT);
}

void simple_vma_close(struct vm_area_struct *vma)
{
    printk(KERN_NOTICE "Simple VMA close.\n");
}


/*
 * The remap_pfn_range version of mmap.  This one is heavily borrowed
 * from drivers/char/mem.c.
 */

static struct vm_operations_struct simple_remap_vm_ops = {
    .open =  simple_vma_open,
    .close = simple_vma_close,
};

static int simple_remap_mmap(struct file *filp, struct vm_area_struct
*vma)
{

    if (remap_pfn_range(vma, vma->vm_start,
                        virt_to_phys((unsigned long)(buffer)) >>
PAGE_SHIFT,
                vma->vm_end - vma->vm_start,
                        vma->vm_page_prot))
                return -EAGAIN;

    /*
     if (io_remap_page_range(vma, vma->vm_start,
                             virt_to_phys((unsigned int)buffer),
                             vma->vm_end - vma->vm_start,
                             vma->vm_page_prot))
        return -EAGAIN;
    */

    /*
    if (remap_pfn_range(vma, vma->vm_start,
                         ((int)(buffer)) >> PAGE_SHIFT,

                vma->vm_end - vma->vm_start,
                vma->vm_page_prot))
        return -EAGAIN;
*/
    vma->vm_ops = &simple_remap_vm_ops;
    simple_vma_open(vma);
    return 0;
}


/*
 * Our various sub-devices.
 */
/* Device 0 uses remap_pfn_range */
static struct file_operations simple_remap_ops = {
    .owner   = THIS_MODULE,
    .open    = simple_open,
    .release = simple_release,
    .mmap    = simple_remap_mmap,
};


/*
 * We export two simple devices.  There's no need for us to maintain
any
 * special housekeeping info, so we just deal with raw cdevs.
 */
static struct cdev SimpleDevs;

/*
 * Module housekeeping.
 */
static int simple_init(void)
{
    int result;
    dev_t dev = MKDEV(simple_major, 0);
        int err, devno;

    /* Figure out our device number. */
        result = alloc_chrdev_region(&dev, 0, 2, "simple");
        simple_major = MAJOR(dev);

    if (result < 0) {
            printk(KERN_WARNING "unable to get major %d\n",
simple_major);
            return result;
    }
        simple_major = result;

        printk(KERN_WARNING "simple: major %d\n", simple_major);

        devno = MKDEV(simple_major, 0);

    cdev_init(&SimpleDevs, &simple_remap_ops);
    SimpleDevs.owner = THIS_MODULE;
    SimpleDevs.ops = &simple_remap_ops;
    err = cdev_add (&SimpleDevs, devno, 1);
    /* Fail gracefully if need be */
    if (err)
        printk (KERN_NOTICE "Error %d adding", err);


        buffer = kmalloc(4096, GFP_KERNEL);
        if (!buffer) {
            printk(KERN_ALERT "kmalloc failed \n");
            return -1;
        }
        *buffer = '';

    return 0;
}


static void simple_cleanup(void)
{
    cdev_del(&SimpleDevs);
    unregister_chrdev_region(MKDEV(simple_major, 0), 1);
        if (buffer)
            kfree(buffer);
}


module_init(simple_init);
module_exit(simple_cleanup);
--------------------------------------------------------------------


my make file:

# Comment/uncomment the following line to disable/enable debugging
#DEBUG = y
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
  DEBFLAGS = -O -g # "-O" is needed to expand inlines
else
  DEBFLAGS = -O2
endif

CFLAGS += $(DEBFLAGS) -I$(LDDINCDIR)

ifneq ($(KERNELRELEASE),)
# call from kernel build system

obj-m    := simple.o

else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)

default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINCDIR=$(PWD)/../include modules

endif

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

depend .depend dep:
    $(CC) $(CFLAGS) -M *.c > .depend


ifeq (.depend,$(wildcard .depend))
include .depend
endif

----------------------------------------------------------------------------------------

my test program:


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

int main (int argc, char *argv[])
{
    char *str;
    int fd;


        fd = open("/dev/simple", O_RDWR);

    str = (char *)mmap(NULL,getpagesize(),
                           PROT_READ | PROT_WRITE,
                           MAP_SHARED,fd,0);

    if (str==MAP_FAILED) {
            perror("mmaptest user ");
            return 1;
    };

    /* there you go */
    printf("\n!!!!!!!!!!!!%s\n\n", str);

    munmap(str, 4096);
    close(fd);

    return 0;
}


Site Timeline