From owner-freebsd-hackers@FreeBSD.ORG Thu Feb 23 14:34:15 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7E91D106566B for ; Thu, 23 Feb 2012 14:34:15 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id 4FAB38FC08 for ; Thu, 23 Feb 2012 14:34:15 +0000 (UTC) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [96.47.65.170]) by cyrus.watson.org (Postfix) with ESMTPSA id 078C046B2A; Thu, 23 Feb 2012 09:34:15 -0500 (EST) Received: from jhbbsd.localnet (unknown [209.249.190.124]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 5FB1EB948; Thu, 23 Feb 2012 09:34:14 -0500 (EST) From: John Baldwin To: freebsd-hackers@freebsd.org Date: Thu, 23 Feb 2012 08:12:43 -0500 User-Agent: KMail/1.13.5 (FreeBSD/8.2-CBSD-20110714-p10; KDE/4.5.5; amd64; ; ) References: <1329938141.21804.4.camel@revolution.hippie.lan> In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201202230812.43126.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Thu, 23 Feb 2012 09:34:14 -0500 (EST) Cc: Ian Lepore , Ryan Stone , Svetlin Manavski Subject: Re: How to access kernel memory from user space X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Feb 2012 14:34:15 -0000 On Wednesday, February 22, 2012 8:06:20 pm Ryan Stone wrote: > On Wed, Feb 22, 2012 at 2:15 PM, Ian Lepore > 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