user malloc from kernel

Terry Lambert tlambert2 at mindspring.com
Tue Sep 30 01:14:25 PDT 2003


earthman wrote:
> how to allocate some memory chunk
> in user space memory from kernel code?
> how to do it correctly?

If your intent is to allocate a chunk of memory which is shared
between your kernel and a single process in user space, the
normal way of doing this is to allocate the memory to a device
driver in the kernel, and then support mmap() on it to establish
a user space mapping for the kernel allocated memory.  In general,
you must do this so that the memory is wired down in the kernel
address space, so that if you attempt to access it in the kernel
while the process you are interested in sharing with is swapped
out, you do not segfault and trap-12 ("page not present") panic
your kernel.

If your intent is to share memory with every process in user
space (e.g. similar to what some OS's use to implement zero
system call gettimeofday() functions, etc.), then you want to
allocate the memory in kernel space (still), make sure it's on
a page boundary, and set the PG_G and PG_U bits on the page(s)
in question.

-- Terry


More information about the freebsd-hackers mailing list