HelenOS sources
This source file includes following definitions.
- ddi_init
- ddi_parea_init
- ddi_parea_register
- ddi_parea_unmap_notify
- physmem_map
- physmem_unmap
- sys_physmem_map
- sys_physmem_unmap
- pareas_getkey
- pareas_cmp
- iospace_enable
- iospace_disable
- sys_iospace_enable
- sys_iospace_disable
- dmamem_map
- dmamem_map_anonymous
- dmamem_unmap
- dmamem_unmap_anonymous
- sys_dmamem_map
- sys_dmamem_unmap
- pio_map
- pio_unmap
#include <assert.h>
#include <ddi/ddi.h>
#include <proc/task.h>
#include <security/perm.h>
#include <mm/frame.h>
#include <mm/as.h>
#include <mm/km.h>
#include <mm/page.h>
#include <synch/mutex.h>
#include <syscall/copy.h>
#include <adt/odict.h>
#include <arch.h>
#include <align.h>
#include <errno.h>
#include <memw.h>
#include <trace.h>
#include <bitops.h>
#include <arch/asm.h>
static mutex_t pareas_lock;
static odict_t pareas;
static void *pareas_getkey(odlink_t *);
static int pareas_cmp(void *, void *);
void ddi_init(void)
{
odict_initialize(&pareas, pareas_getkey, pareas_cmp);
mutex_initialize(&pareas_lock, MUTEX_PASSIVE);
}
void ddi_parea_init(parea_t *parea)
{
memset(parea, 0, sizeof(parea_t));
}
void ddi_parea_register(parea_t *parea)
{
mutex_lock(&pareas_lock);
odict_insert(&parea->lpareas, &pareas, NULL);
mutex_unlock(&pareas_lock);
}
void ddi_parea_unmap_notify(parea_t *parea)
{
parea->mapped = false;
if (parea->mapped_changed != NULL)
parea->mapped_changed(parea->arg);
}
_NO_TRACE static errno_t physmem_map(uintptr_t phys, size_t pages,
unsigned int flags, uintptr_t *virt, uintptr_t bound)
{
assert(TASK);
if ((phys % FRAME_SIZE) != 0)
return EBADMEM;
bool priv =
((perm_get(TASK) & PERM_MEM_MANAGER) == PERM_MEM_MANAGER);
mem_backend_data_t backend_data;
backend_data.base = phys;
backend_data.frames = pages;
backend_data.anonymous = false;
mutex_lock(&pareas_lock);
odlink_t *odlink = odict_find_eq(&pareas, &phys, NULL);
parea_t *parea = odlink != NULL ?
odict_get_instance(odlink, parea_t, lpareas) : NULL;
if ((parea != NULL) && (parea->frames >= pages)) {
if ((!priv) && (!parea->unpriv)) {
mutex_unlock(&pareas_lock);
return EPERM;
}
goto map;
}
parea = NULL;
mutex_unlock(&pareas_lock);
irq_spinlock_lock(&zones.lock, true);
size_t znum = find_zone(ADDR2PFN(phys), pages, 0);
if (znum == (size_t) -1) {
irq_spinlock_unlock(&zones.lock, true);
if (!priv)
return EPERM;
goto map;
}
if (zones.info[znum].flags & (ZONE_FIRMWARE | ZONE_RESERVED)) {
irq_spinlock_unlock(&zones.lock, true);
if (!priv)
return EPERM;
goto map;
}
irq_spinlock_unlock(&zones.lock, true);
return ENOENT;
map:
backend_data.parea = parea;
if (!as_area_create(TASK->as, flags, FRAMES2SIZE(pages),
AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
if (parea != NULL)
mutex_unlock(&pareas_lock);
return ENOMEM;
}
if (parea != NULL) {
parea->mapped = true;
mutex_unlock(&pareas_lock);
}
return EOK;
}
_NO_TRACE static errno_t physmem_unmap(uintptr_t virt)
{
assert(TASK);
return as_area_destroy(TASK->as, virt);
}
sys_errno_t sys_physmem_map(uintptr_t phys, size_t pages, unsigned int flags,
uspace_ptr_uintptr_t virt_ptr, uintptr_t bound)
{
uintptr_t virt;
errno_t rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
if (rc != EOK)
return rc;
rc = physmem_map(ALIGN_DOWN(phys, FRAME_SIZE), pages, flags, &virt,
bound);
if (rc != EOK)
return rc;
rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
if (rc != EOK) {
physmem_unmap(virt);
return rc;
}
return EOK;
}
sys_errno_t sys_physmem_unmap(uintptr_t virt)
{
return physmem_unmap(virt);
}
static void *pareas_getkey(odlink_t *odlink)
{
parea_t *parea = odict_get_instance(odlink, parea_t, lpareas);
return (void *) &parea->pbase;
}
static int pareas_cmp(void *a, void *b)
{
uintptr_t pa = *(uintptr_t *)a;
uintptr_t pb = *(uintptr_t *)b;
if (pa < pb)
return -1;
else if (pa == pb)
return 0;
else
return +1;
}
_NO_TRACE static errno_t iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size)
{
perm_t perms = perm_get(TASK);
if (!(perms & PERM_IO_MANAGER))
return EPERM;
task_t *task = task_find_by_id(id);
if (!task)
return ENOENT;
errno_t rc = ENOENT;
irq_spinlock_lock(&task->lock, true);
if (container_check(CONTAINER, task->container))
rc = ddi_iospace_enable_arch(task, ioaddr, size);
irq_spinlock_unlock(&task->lock, true);
task_release(task);
return rc;
}
_NO_TRACE static errno_t iospace_disable(task_id_t id, uintptr_t ioaddr, size_t size)
{
perm_t perms = perm_get(TASK);
if (!(perms & PERM_IO_MANAGER))
return EPERM;
task_t *task = task_find_by_id(id);
if (!task)
return ENOENT;
errno_t rc = ENOENT;
irq_spinlock_lock(&task->lock, true);
if (container_check(CONTAINER, task->container))
rc = ddi_iospace_disable_arch(task, ioaddr, size);
irq_spinlock_unlock(&task->lock, true);
task_release(task);
return rc;
}
sys_errno_t sys_iospace_enable(uspace_ptr_ddi_ioarg_t uspace_io_arg)
{
ddi_ioarg_t arg;
errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
if (rc != EOK)
return (sys_errno_t) rc;
return (sys_errno_t) iospace_enable((task_id_t) arg.task_id,
(uintptr_t) arg.ioaddr, (size_t) arg.size);
}
sys_errno_t sys_iospace_disable(uspace_ptr_ddi_ioarg_t uspace_io_arg)
{
ddi_ioarg_t arg;
errno_t rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t));
if (rc != EOK)
return (sys_errno_t) rc;
return (sys_errno_t) iospace_disable((task_id_t) arg.task_id,
(uintptr_t) arg.ioaddr, (size_t) arg.size);
}
_NO_TRACE static errno_t dmamem_map(uintptr_t virt, size_t size, unsigned int map_flags,
unsigned int flags, uintptr_t *phys)
{
assert(TASK);
return page_find_mapping(virt, phys);
}
_NO_TRACE static errno_t dmamem_map_anonymous(size_t size, uintptr_t constraint,
unsigned int map_flags, unsigned int flags, uintptr_t *phys,
uintptr_t *virt, uintptr_t bound)
{
assert(TASK);
size_t frames = SIZE2FRAMES(size);
if (frames == 0)
return EINVAL;
*phys = frame_alloc(frames, FRAME_ATOMIC, constraint);
if (*phys == 0)
return ENOMEM;
mem_backend_data_t backend_data;
backend_data.base = *phys;
backend_data.frames = frames;
backend_data.anonymous = true;
backend_data.parea = NULL;
if (!as_area_create(TASK->as, map_flags, size,
AS_AREA_ATTR_NONE, &phys_backend, &backend_data, virt, bound)) {
frame_free(*phys, frames);
return ENOMEM;
}
return EOK;
}
_NO_TRACE static errno_t dmamem_unmap(uintptr_t virt, size_t size)
{
return EOK;
}
_NO_TRACE static errno_t dmamem_unmap_anonymous(uintptr_t virt)
{
return as_area_destroy(TASK->as, virt);
}
sys_errno_t sys_dmamem_map(size_t size, unsigned int map_flags, unsigned int flags,
uspace_ptr_uintptr_t phys_ptr, uspace_ptr_uintptr_t virt_ptr, uintptr_t bound)
{
if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0) {
uintptr_t phys;
errno_t rc = dmamem_map(virt_ptr, size, map_flags,
flags, &phys);
if (rc != EOK)
return rc;
rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
if (rc != EOK) {
dmamem_unmap(virt_ptr, size);
return rc;
}
} else {
uintptr_t constraint;
errno_t rc = copy_from_uspace(&constraint, phys_ptr,
sizeof(constraint));
if (rc != EOK)
return rc;
uintptr_t virt;
rc = copy_from_uspace(&virt, virt_ptr, sizeof(virt));
if (rc != EOK)
return rc;
uintptr_t phys;
rc = dmamem_map_anonymous(size, constraint, map_flags, flags,
&phys, &virt, bound);
if (rc != EOK)
return rc;
rc = copy_to_uspace(phys_ptr, &phys, sizeof(phys));
if (rc != EOK) {
dmamem_unmap_anonymous(virt);
return rc;
}
rc = copy_to_uspace(virt_ptr, &virt, sizeof(virt));
if (rc != EOK) {
dmamem_unmap_anonymous(virt);
return rc;
}
}
return EOK;
}
sys_errno_t sys_dmamem_unmap(uintptr_t virt, size_t size, unsigned int flags)
{
if ((flags & DMAMEM_FLAGS_ANONYMOUS) == 0)
return dmamem_unmap(virt, size);
else
return dmamem_unmap_anonymous(virt);
}
void *pio_map(void *phys, size_t size)
{
#ifdef IO_SPACE_BOUNDARY
if (phys < IO_SPACE_BOUNDARY)
return phys;
#endif
return (void *) km_map((uintptr_t) phys, size, KM_NATURAL_ALIGNMENT,
PAGE_READ | PAGE_WRITE | PAGE_NOT_CACHEABLE);
}
void pio_unmap(void *phys, void *virt, size_t size)
{
#ifdef IO_SPACE_BOUNDARY
if (phys < IO_SPACE_BOUNDARY)
return;
#endif
km_unmap((uintptr_t) virt, size);
}
HelenOS homepage, sources at GitHub