From owner-freebsd-hackers@FreeBSD.ORG Wed Feb 22 19:15:45 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 12C08106566B for ; Wed, 22 Feb 2012 19:15:45 +0000 (UTC) (envelope-from freebsd@damnhippie.dyndns.org) Received: from qmta08.emeryville.ca.mail.comcast.net (qmta08.emeryville.ca.mail.comcast.net [76.96.30.80]) by mx1.freebsd.org (Postfix) with ESMTP id ED8298FC16 for ; Wed, 22 Feb 2012 19:15:44 +0000 (UTC) Received: from omta05.emeryville.ca.mail.comcast.net ([76.96.30.43]) by qmta08.emeryville.ca.mail.comcast.net with comcast id d7CU1i00S0vp7WLA87Fk3G; Wed, 22 Feb 2012 19:15:44 +0000 Received: from damnhippie.dyndns.org ([24.8.232.202]) by omta05.emeryville.ca.mail.comcast.net with comcast id d7Fj1i00q4NgCEG8R7FjAp; Wed, 22 Feb 2012 19:15:44 +0000 Received: from [172.22.42.240] (revolution.hippie.lan [172.22.42.240]) by damnhippie.dyndns.org (8.14.3/8.14.3) with ESMTP id q1MJFfQg005108; Wed, 22 Feb 2012 12:15:41 -0700 (MST) (envelope-from freebsd@damnhippie.dyndns.org) From: Ian Lepore To: Svetlin Manavski In-Reply-To: References: Content-Type: text/plain; charset="us-ascii" Date: Wed, 22 Feb 2012 12:15:41 -0700 Message-ID: <1329938141.21804.4.camel@revolution.hippie.lan> Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 FreeBSD GNOME Team Port Content-Transfer-Encoding: 7bit Cc: freebsd-hackers@freebsd.org 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: Wed, 22 Feb 2012 19:15:45 -0000 On Wed, 2012-02-22 at 17:24 +0000, Svetlin Manavski wrote: > Hi all, > I have a very similar problem as described in this thread back in 2009: > > http://lists.freebsd.org/pipermail/freebsd-hackers/2009-January/027367.html > > I have a kernel module producing networking stats which I need to > frequently read from the user space. A copy of the data structure would be > too expensive so I need to access the kernel data directly from the user > space. > > Unfortunately Alexej's code crashes in the following area: > > vm_map_lookup(&kmem_map, addr, VM_PROT_ALL, &myentry, &myobject, &mypindex, > &myprot, &mywired); /* OUT */ > vm_map_lookup_done(kmem_map, myentry); > I am using 64bit FreeBSD 8.2 on Intel Xeon hardware. > Any idea how to make a stable implementation on my platform? > > Thank you, > Svetlin > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" 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). -- Ian