Subject: Re: memory questions
To: Filka Michal <michal.filka@strom.cz>
From: Eduardo Horvath <eeh@netbsd.org>
List: tech-kern
Date: 04/12/2006 16:24:07
On Wed, 12 Apr 2006, Filka Michal wrote:

> Hi all,
> I have two questions connected to memory access from kernel ...
> 
> 1) is it correct to access memory allocated in user space within my
> driver? I've created an ioctl which gets a buffer from application and
> which should be filled by driver .. Is it correct or there is any
> restriction?

Directly accessing userspace, while it may work in some circumstances
on some machines, is not allowed.  (Can you say bug?)  Some machines,
like x86 due to MMU deficiencies, still share the address space between 
kernel and userland.  Others don't, which means userland pointers are 
simply invalid in the kernel.


To access userland memory you need to use copyin()/copyout(), uiomove(), 
map the user pages into the kernel, or some other kernel service designed 
for the task.  Otherwise you will get problems like data corruption or 
kernel panics.

> 2) when accessing memory mapped for DMA access, I sometimes get an
> "uvm_fault: page fault error". It seems that pointer is valid (points
> into allocated buffer), what can be an other reason for it? Page fault
> seems strange to me in such situation ...

Remember that virtual addresses are not necessarily the same as DMA 
addresses.  The former is what you use to access data with the CPU.  The 
latter you hand to the DMA engine for your device.  You should look at 
bus_dma(9) for more details.

The information you provide is not enough to do more than speculate on the 
cause.  I recommend looking at an existing driver to see how the process 
should be done.

Eduardo