mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Timo Benk <t_benk@web.de>
To: linux-kernel <linux-kernel@vger.kernel.org>
Cc: Timo Benk <t_benk@web.de>
Subject: Re: allocate memory in userspace (Answer)
Date: Tue, 2 Jul 2002 09:51:17 +0200	[thread overview]
Message-ID: <20020702075117.GA5604@toshiba> (raw)
In-Reply-To: <20020701172659.GA4431@toshiba>

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


  parent reply	other threads:[~2002-07-02  7:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-01 17:26 allocate memory in userspace Timo Benk
2002-07-01 17:49 ` Erik Andersen
2002-07-02  8:37   ` William Lee Irwin III
2002-07-02  8:44     ` William Lee Irwin III
2002-07-02  9:00       ` Erik Andersen
2002-07-01 18:39 ` Calin A. Culianu
2002-07-02  7:51 ` Timo Benk [this message]
2002-07-02 10:24   ` allocate memory in userspace (Answer) Timo Benk
2002-07-02 12:19 ` allocate memory in userspace Brian Gerst

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020702075117.GA5604@toshiba \
    --to=t_benk@web.de \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®