From owner-freebsd-hackers@FreeBSD.ORG Thu Jan 15 18:31:01 2009 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 65ECA1065672 for ; Thu, 15 Jan 2009 18:31:01 +0000 (UTC) (envelope-from bsd.quest@googlemail.com) Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.171]) by mx1.freebsd.org (Postfix) with ESMTP id 347078FC12 for ; Thu, 15 Jan 2009 18:31:01 +0000 (UTC) (envelope-from bsd.quest@googlemail.com) Received: by wf-out-1314.google.com with SMTP id 24so1316703wfg.7 for ; Thu, 15 Jan 2009 10:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type:references; bh=mjoF14Wd8TcJsPtFp5SBUzCiF1GcvJ/ageTtAUGxaK8=; b=YZFbXTDHIpFM3rBwP/Z0qkIIIGRvks4EAZCdoY0biec3N0XG+W2Mzbp62F9FJDYKwB 4nl71Ti7JmpUqar/sKRzgQPqCbcmtjuPC1kzelRLZjqCjTc17Bb357uXA4kN5XaK92oN bTs2IMmnRhzxFKfXifwz3V4x95lpYXVZAi0lA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:references; b=gmtCqL7FuQKmd1B78fOPy/OyW+iEVjESm+Zc6JccvehApE80/fID5vs1VL+HjfLKgn 0xbr+tw6+X2D0vWD9ue0612ufSuD4HlxevSzFEJZ/mxpJslNhzJxcnpNO26s83byojY4 pb2/QL8IklY2OhAFO7kNLM9o9Rj9CVAS0PtV8= Received: by 10.114.92.2 with SMTP id p2mr1095799wab.122.1232044260646; Thu, 15 Jan 2009 10:31:00 -0800 (PST) Received: by 10.114.202.10 with HTTP; Thu, 15 Jan 2009 10:31:00 -0800 (PST) Message-ID: <671bb5fc0901151031w2da249ebu273dfb9429f82ac7@mail.gmail.com> Date: Thu, 15 Jan 2009 19:31:00 +0100 From: "Alexej Sokolov" To: "Gerry Weaver" In-Reply-To: <20081223000534.f740ca8a@mail01.compvia.com> MIME-Version: 1.0 References: <20081223000534.f740ca8a@mail01.compvia.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.5 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: Thu, 15 Jan 2009 18:31:01 -0000 2008/12/23 Gerry Weaver > Hello All, > > I am working on a driver that collects various network statistics via pfil. > I have a simple array of structures that I use to store the statistics. I > also have a user space process that needs to collect these statistics every > second or so. A copy operation from kernel to user space would be too > expensive. Is there a mechanism that would allow me to gain direct access to > my kernel array from user space? The user process would only need read > access. It seems like maybe this could be done with mmap, but since this is > not a character driver, there is no device file etc.. I'm a newbie, so I > apologize if this is something that should be obvious. > > > Thanks in advance, > Gerry > _______________________________________________ > 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" > Hi, some times ago I solve this task. That's my solution in a system call (whithout cdev). Thanx in advance for founded mistakes and possible bugs (-: #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Arguments for syscall */ struct args { /* Pointer to allocated Buffer */ unsigned int *p; }; /* String to be located in maped buffer */ const char *str = "BSD IS SEXY"; /* Syscall func */ static int syscf(struct thread *td, void *sa) { int error; struct args *uap; vm_offset_t addr; /* Kernel space address */ vm_offset_t user_addr; /* User space address */ struct proc *procp = (struct proc *)td->td_proc; struct vmspace *vms = procp->p_vmspace; uap = (struct args *)sa; PROC_LOCK(procp); user_addr = round_page((vm_offset_t)vms->vm_daddr + lim_max(procp, RLIMIT_DATA)); PROC_UNLOCK(procp); MALLOC(addr, vm_offset_t, PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO); vm_map_entry_t myentry; vm_object_t myobject; vm_pindex_t mypindex; vm_prot_t myprot; boolean_t mywired; vm_ooffset_t objoffset; vm_map_lookup(&kmem_map, addr, VM_PROT_ALL, &myentry, &myobject, &mypindex, &myprot, &mywired); /* OUT */ vm_map_lookup_done(kmem_map, myentry); printf("---> Syscall: hint for allocating space = 0x%X\n", addr); if (myobject == kmem_object){ printf("---> Syscall: Yes, it is kmem_obj! \n"); } /* Offset in vm_object */ objoffset = addr - myentry->start + myentry->offset; printf("------> Syscall: Object offset = 0x%X \n", (unsigned int)objoffset); /* * Try to map kernel buffer to user space */ vm_object_reference(myobject); /* NEEDED Increment vm_obj references */ error = vm_map_find(&vms->vm_map, myobject, objoffset, (vm_offset_t *)&user_addr, PAGE_SIZE, TRUE, VM_PROT_RW, VM_PROT_RW, MAP_ENTRY_NOFAULT); if (error == KERN_SUCCESS) { /* copy string using kernel address */ size_t len; copystr(str, (void *)addr, 12, &len); /* * Tell to user process it's user space address */ *uap->p = user_addr; /* * Try to read the string using user space address */ printf("String: %s\n", (char *)*uap->p); printf("---> Syscall: user_addr for allocating space = 0x%X\n", user_addr); } return (0); } /* Sysent entity for syscall */ static struct sysent sc_sysent = { 1, /* Number of arguments */ syscf /* Syscall function */ }; /* Offset in sysent[] */ static int offset = NO_SYSCALL; /* Loader */ static int load (struct module *m, int cmd, void *something) { int error = 0; switch(cmd){ case MOD_LOAD: printf("Module with sysc loaded. Offset = %d \n", offset); break; case MOD_UNLOAD: printf("Module with sysc unloaded. Offset = %d \n", offset); break; default: error = EOPNOTSUPP; break; } return (error); } /* Syscall macro*/ SYSCALL_MODULE(fiveg_sysc, &offset, &sc_sysent, load, NULL); If needed, I can post user space program.