Re: allocate memory in userspace (Answer)

From: Timo Benk (t_benk@web.de)
Date: Tue Jul 02 2002 - 02:51:17 EST


Hi,

I found the following function in arch/i386/kernel/sys_i386.c:

---<snip>---
/* common code for old and new mmaps */
long do_mmap2
        (
                unsigned long addr, unsigned long len,
                unsigned long prot, unsigned long flags,
                unsigned long fd, unsigned long pgoff
        )
{
        int error = -EBADF;
        struct file * file = NULL;

        flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
        if (!(flags & MAP_ANONYMOUS)) {
                file = fget(fd);
                if (!file)
                        goto out;
        }

        down_write(&current->mm->mmap_sem);
        error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
        up_write(&current->mm->mmap_sem);

        if (file)
                fput(file);
out:
        return error;
}
---<snap>---

the following code works for me(of course
don't forget to munmap the memory).
---<snip>---
        char *userspace;
        char kernelspace[2048];

        userspace = (char*)do_mmap2
                        (
                                0,
                                2048,
                                PROT_READ|PROT_WRITE,
                                MAP_PRIVATE|MAP_ANON,
                                -1,
                                0
                        );
                        
        copy_to_user( userspace, "/dev/hda", 9 );
        
        copy_from_user( kernelspace, userspace, 9 );
        printk("%s\n",kernelspace);
---<snap>---

Hope that helps any other struggling newbie:-)

-timo

-- 
gpg key fingerprint = 6832 C8EC D823 4059 0CD1  6FBF 9383 7DBD 109E 98DC

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun Jul 07 2002 - 22:00:08 EST