Subject: re: uvm_map ?
To: None <tech-kern@netbsd.org>
From: George Peter Staplin <georgeps@xmission.com>
List: tech-kern
Date: 04/13/2007 10:01:03
Quote "Nalin":
> hello
>
> Is uvm_map enough to attach kernel memory to user process ?
>
> regards,
> - nalin

This is one possible solution in the kernel using uvm_map I think.  I 
used this solution in NetBSD 3.0 for shared memory in a project I 
called blockmgr.  I was able to write to/read from all of the pages 
from userland and the kernel, after loading the LKM.  I'm not sure how 
well it works with wired memory, but I'm sure someone will post about 
that, if needed.

In the kernel init/setup routine for your code:
blockmgr->size = round_page (blocksize * n);
blockmgr->uaoobj = uao_create (blockmgr->size, 0);
blockmgr->kerneladdress = 0;

error = uvm_map (kernel_map, &blockmgr->kerneladdress, blockmgr->size,
   blockmgr->uaoobj, 0, /*align*/ 2,   UVM_MAPFLAG (UVM_PROT_ALL, 
UVM_PROT_ALL, UVM_INH_SHARE, UVM_ADV_NORMAL, 0));

if (error) {
  uao_detach (blockmgr->uaoobj);
  return error;
}

uao_reference (blockmgr->uaoobj)




Now in a syscall I did this:

int
sys_inherit_blocks ( struct lwp *l, void *ua, register_t *retval ) {
int error;
vaddr_t r = 0;

*retval = 0;
  if (NULL == blockmgr->uaoobj) {
   printf ("sys_inherit_blocks: blockmgr not initialized!\n");
   return ENOBUFS;
}

uao_reference (blockmgr->uaoobj);
  error = uvm_map (&l->l_proc->p_vmspace->vm_map, &r, blockmgr->size,
   blockmgr->uaoobj, 0, /*align*/ 2,
   UVM_MAPFLAG (UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_SHARE, UVM_ADV_NORMAL, 0));

if (error)
  return error;

*retval = (register_t) r;
  return 0;
}

I think in hindsight I'd create a new /dev/blockmgr or something along 
those lines.

-- 
http://www.xmission.com/~georgeps/