Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Feb 2012 08:12:43 -0500
From:      John Baldwin <jhb@freebsd.org>
To:        freebsd-hackers@freebsd.org
Cc:        Ian Lepore <freebsd@damnhippie.dyndns.org>, Ryan Stone <rysto32@gmail.com>, Svetlin Manavski <svetlin.manavski@itrinegy.com>
Subject:   Re: How to access kernel memory from user space
Message-ID:  <201202230812.43126.jhb@freebsd.org>
In-Reply-To: <CAFMmRNx_f4pPzPKipC_3gORHkbauhi1jqMmOY7z_Pf8kdit_pw@mail.gmail.com>
References:  <CACRNAiv_huiJuzXHsEKNHdFdnpFuAK7fimjWbVVfmdRth2nVDA@mail.gmail.com> <1329938141.21804.4.camel@revolution.hippie.lan> <CAFMmRNx_f4pPzPKipC_3gORHkbauhi1jqMmOY7z_Pf8kdit_pw@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wednesday, February 22, 2012 8:06:20 pm Ryan Stone wrote:
> On Wed, Feb 22, 2012 at 2:15 PM, Ian Lepore
> <freebsd@damnhippie.dyndns.org> wrote:
> > I've never done this, but if I needed to, I think the first thing I'd
> > try is to use an mmap(2) of /dev/kmem to map the memory you need into
> > userspace (of course your userspace app will need to be running with
> > root privs to do this).
> >
> > That leaves the interesting problem of locating what offset within the
> > kernel virtual address space you need to map to get at your data.  Two
> > things come to mind... have your kernel module export the address in a
> > sysctl (that feels kind of hack-ish but it should be quick and easy to
> > do), or use libkvm's kvm_nlist() function to locate the symbol within
> > your module (I think that should be possible; again I've never actually
> > done any of this).
> 
> A far easier way to do this is to have the module create its own
> device in /dev that exports the memory by implementing the mmap
> interface in the cdev.

Yes.  Another option you can do if you want to let userland "donate" a buffer
to the kernel is to let userland create a buffer using shm_open() (probably
with SHM_ANON) and then use shm_map() in the kernel to map that into KVA.

-- 
John Baldwin



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201202230812.43126.jhb>