how to map kernel memory to user space
Konstantin Belousov
kostikbel at gmail.com
Mon Feb 20 10:09:17 UTC 2017
On Sun, Feb 19, 2017 at 03:59:24PM -0800, Chuck Tuffli wrote:
> I'm trying to work around a problem by mapping the data from a kernel
> allocated buffer into a user space application. There was a post some
> time back with a possible way to do this [1], but while this creates a
> valid buffer in the user space application, the contents is all zeros
> instead of containing the expected data. Is this approach not
> possible? Does anything obviously stick out below? TIA.
You generally cannot map arbitrary kernel data into userspace and
expect things not to break.
We have shm_map(9)/shm_unmap(9) functions which allow to map posix
shared object into the KVA. Userspace would map object by a file
descriptor, either allocated by userspace and then passed to kernel, or
allocated by kernel and returned to userspace.
In any case, such setup requires coordination between kernel and userspace,
and using a specific memory for this to work. Code below is not functional.
>
> --chuck
>
> rc = vm_map_lookup(&kernel_map, kaddr, VM_PROT_ALL,
> &entry, &obj, &pindex,
> &prot, &is_wired);
>
> vm_map_lookup_done(kernel_map, entry);
> if (rc) printf("%s: vm_map_lookup() = %d\n", __func__, rc);
>
> PROC_LOCK(p);
> uaddr = round_page((vm_offset_t)vms->vm_daddr + lim_max(td, RLIMIT_DATA));
> PROC_UNLOCK(p);
>
> objoff = kaddr - (entry->start + entry->offset);
>
> vm_object_reference(obj);
> rc = vm_map_find(map,
> obj,
> objoff,
> &uaddr,
> sizeof(struct nvme_controller_data),
> 0,
> VMFS_OPTIMAL_SPACE,
> VM_PROT_RW,
> VM_PROT_RW,
> MAP_INHERIT_SHARE);
>
> [1] http://markmail.org/message/ph5yuonevqjhhbig
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
More information about the freebsd-hackers
mailing list