From owner-freebsd-hackers@FreeBSD.ORG Mon Feb 14 14:56:25 2011 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 807CF106566C for ; Mon, 14 Feb 2011 14:56:25 +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 565FC8FC19 for ; Mon, 14 Feb 2011 14:56:25 +0000 (UTC) Received: from bigwig.baldwin.cx (66.111.2.69.static.nyinternet.net [66.111.2.69]) by cyrus.watson.org (Postfix) with ESMTPSA id 0AFA846B0C; Mon, 14 Feb 2011 09:56:25 -0500 (EST) Received: from jhbbsd.localnet (unknown [209.249.190.10]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 5237C8A027; Mon, 14 Feb 2011 09:56:24 -0500 (EST) From: John Baldwin To: freebsd-hackers@freebsd.org Date: Mon, 14 Feb 2011 09:39:17 -0500 User-Agent: KMail/1.13.5 (FreeBSD/7.4-CBSD-20110107; KDE/4.4.5; amd64; ; ) References: <201102141718415933573@yahoo.com.cn> In-Reply-To: <201102141718415933573@yahoo.com.cn> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201102140939.17486.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.6 (bigwig.baldwin.cx); Mon, 14 Feb 2011 09:56:24 -0500 (EST) X-Virus-Scanned: clamav-milter 0.96.3 at bigwig.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=0.5 required=4.2 tests=BAYES_00,MAY_BE_FORGED, RDNS_DYNAMIC autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on bigwig.baldwin.cx Cc: beezarliu Subject: Re: map share memory to kernel 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: Mon, 14 Feb 2011 14:56:25 -0000 On Monday, February 14, 2011 4:18:50 am beezarliu wrote: > Hackers, > > I want to access a userland share memory in a kernel thread. > So I tried to map the share memory to the kernel space. > The basic idea is to map the shm_object into kernel_map > when the share memory is created. > > Using the following patch, I found the vm_object in kernel_map, > and the vm_object in the address space of userland process are the same. > But their content in the kernel and userland address mapped are different. > > It's very strang since they are exactly the same vm_object. > Do I miss something, please help. Hmm, this is a bit of code I use for something similar to map a VM object into the kernel. It does not use vm_page_grab() directly though: VM_OBJECT_LOCK(obj); vm_object_reference_locked(obj); VM_OBJECT_UNLOCK(obj); /* Map the object into the kernel_map and wire it. */ kva = vm_map_min(kernel_map); ofs = foff & PAGE_MASK; foff = trunc_page(foff); size = round_page(size + ofs); rv = vm_map_find(kernel_map, obj, foff, &kva, size, TRUE, VM_PROT_READ | VM_PROT_WRITE, VM_PROT_READ | VM_PROT_WRITE, 0); if (rv == KERN_SUCCESS) { rv = vm_map_wire(kernel_map, kva, kva + size, VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); if (rv == KERN_SUCCESS) { *memp = (void *)(kva + ofs); return (0); } vm_map_remove(kernel_map, kva, kva + size); } else vm_object_deallocate(obj); Unmapping the object is easy of course: kva = (vm_offset_t)mem; ofs = kva & PAGE_MASK; kva = trunc_page(kva); size = round_page(size + ofs); vm_map_remove(kernel_map, kva, kva + size); -- John Baldwin