HelenOS sources
This source file includes following definitions.
- ranges_unmap
- ranges_map_and_apply
- code_check
- code_free
- code_from_uspace
- irq_hash_out
- irq_destroy
- ipc_irq_subscribe
- ipc_irq_unsubscribe
- send_call
- ipc_irq_top_half_claim
- ipc_irq_top_half_handler
- ipc_irq_send_msg
#include <arch.h>
#include <assert.h>
#include <mm/slab.h>
#include <mm/page.h>
#include <mm/km.h>
#include <errno.h>
#include <ddi/irq.h>
#include <ipc/ipc.h>
#include <ipc/irq.h>
#include <syscall/copy.h>
#include <console/console.h>
#include <macros.h>
#include <cap/cap.h>
#include <stdlib.h>
static void ranges_unmap(irq_pio_range_t *ranges, size_t rangecount)
{
for (size_t i = 0; i < rangecount; i++) {
#ifdef IO_SPACE_BOUNDARY
if ((void *) ranges[i].base >= IO_SPACE_BOUNDARY)
#endif
km_unmap(ranges[i].base, ranges[i].size);
}
}
static errno_t ranges_map_and_apply(irq_pio_range_t *ranges, size_t rangecount,
irq_cmd_t *cmds, size_t cmdcount)
{
uintptr_t *pbase = malloc(rangecount * sizeof(uintptr_t));
if (!pbase)
return ENOMEM;
for (size_t i = 0; i < rangecount; i++)
pbase[i] = ranges[i].base;
for (size_t i = 0; i < rangecount; i++) {
#ifdef IO_SPACE_BOUNDARY
if ((void *) ranges[i].base < IO_SPACE_BOUNDARY)
continue;
#endif
ranges[i].base = km_map(pbase[i], ranges[i].size,
KM_NATURAL_ALIGNMENT,
PAGE_READ | PAGE_WRITE | PAGE_KERNEL | PAGE_NOT_CACHEABLE);
if (!ranges[i].base) {
ranges_unmap(ranges, i);
free(pbase);
return ENOMEM;
}
}
for (size_t i = 0; i < cmdcount; i++) {
uintptr_t addr;
size_t size;
switch (cmds[i].cmd) {
case CMD_PIO_READ_8:
case CMD_PIO_WRITE_8:
case CMD_PIO_WRITE_A_8:
size = 1;
break;
case CMD_PIO_READ_16:
case CMD_PIO_WRITE_16:
case CMD_PIO_WRITE_A_16:
size = 2;
break;
case CMD_PIO_READ_32:
case CMD_PIO_WRITE_32:
case CMD_PIO_WRITE_A_32:
size = 4;
break;
default:
continue;
}
addr = (uintptr_t) cmds[i].addr;
size_t j;
for (j = 0; j < rangecount; j++) {
if (!iswithin(pbase[j], ranges[j].size, addr, size))
continue;
addr -= pbase[j];
addr += ranges[j].base;
cmds[i].addr = (void *) addr;
break;
}
if (j == rangecount) {
ranges_unmap(ranges, rangecount);
free(pbase);
return EINVAL;
}
}
free(pbase);
return EOK;
}
static errno_t code_check(irq_cmd_t *cmds, size_t cmdcount)
{
for (size_t i = 0; i < cmdcount; i++) {
if (cmds[i].cmd >= CMD_LAST)
return EINVAL;
if (cmds[i].srcarg >= IPC_CALL_LEN)
return EINVAL;
if (cmds[i].dstarg >= IPC_CALL_LEN)
return EINVAL;
switch (cmds[i].cmd) {
case CMD_PREDICATE:
if (i + cmds[i].value > cmdcount)
return EINVAL;
break;
default:
break;
}
}
return EOK;
}
static void code_free(irq_code_t *code)
{
if (code) {
ranges_unmap(code->ranges, code->rangecount);
free(code->ranges);
free(code->cmds);
free(code);
}
}
static irq_code_t *code_from_uspace(uspace_ptr_irq_code_t ucode)
{
irq_pio_range_t *ranges = NULL;
irq_cmd_t *cmds = NULL;
irq_code_t *code = malloc(sizeof(*code));
if (!code)
return NULL;
errno_t rc = copy_from_uspace(code, ucode, sizeof(*code));
if (rc != EOK)
goto error;
if ((code->rangecount > IRQ_MAX_RANGE_COUNT) ||
(code->cmdcount > IRQ_MAX_PROG_SIZE))
goto error;
ranges = malloc(sizeof(code->ranges[0]) * code->rangecount);
if (!ranges)
goto error;
rc = copy_from_uspace(ranges, (uintptr_t) code->ranges,
sizeof(code->ranges[0]) * code->rangecount);
if (rc != EOK)
goto error;
cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount);
if (!cmds)
goto error;
rc = copy_from_uspace(cmds, (uintptr_t) code->cmds,
sizeof(code->cmds[0]) * code->cmdcount);
if (rc != EOK)
goto error;
rc = code_check(cmds, code->cmdcount);
if (rc != EOK)
goto error;
rc = ranges_map_and_apply(ranges, code->rangecount, cmds,
code->cmdcount);
if (rc != EOK)
goto error;
code->ranges = ranges;
code->cmds = cmds;
return code;
error:
if (cmds)
free(cmds);
if (ranges)
free(ranges);
free(code);
return NULL;
}
static void irq_hash_out(irq_t *irq)
{
irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
irq_spinlock_lock(&irq->lock, false);
if (irq->notif_cfg.hashed_in) {
hash_table_remove_item(&irq_uspace_hash_table, &irq->link);
irq->notif_cfg.hashed_in = false;
}
irq_spinlock_unlock(&irq->lock, false);
irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
}
static void irq_destroy(void *arg)
{
irq_t *irq = (irq_t *) arg;
irq_hash_out(irq);
code_free(irq->notif_cfg.code);
slab_free(irq_cache, irq);
}
kobject_ops_t irq_kobject_ops = {
.destroy = irq_destroy
};
errno_t ipc_irq_subscribe(answerbox_t *box, inr_t inr, sysarg_t imethod,
uspace_ptr_irq_code_t ucode, uspace_ptr_cap_irq_handle_t uspace_handle)
{
if ((inr < 0) || (inr > last_inr))
return ELIMIT;
irq_code_t *code;
if (ucode) {
code = code_from_uspace(ucode);
if (!code)
return EBADMEM;
} else
code = NULL;
cap_handle_t handle;
errno_t rc = cap_alloc(TASK, &handle);
if (rc != EOK)
return rc;
rc = copy_to_uspace(uspace_handle, &handle, sizeof(cap_handle_t));
if (rc != EOK) {
cap_free(TASK, handle);
return rc;
}
irq_t *irq = (irq_t *) slab_alloc(irq_cache, FRAME_ATOMIC);
if (!irq) {
cap_free(TASK, handle);
return ENOMEM;
}
kobject_t *kobject = kobject_alloc(FRAME_ATOMIC);
if (!kobject) {
cap_free(TASK, handle);
slab_free(irq_cache, irq);
return ENOMEM;
}
irq_initialize(irq);
irq->inr = inr;
irq->claim = ipc_irq_top_half_claim;
irq->handler = ipc_irq_top_half_handler;
irq->notif_cfg.notify = true;
irq->notif_cfg.answerbox = box;
irq->notif_cfg.imethod = imethod;
irq->notif_cfg.code = code;
irq->notif_cfg.counter = 0;
irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
irq_spinlock_lock(&irq->lock, false);
irq->notif_cfg.hashed_in = true;
hash_table_insert(&irq_uspace_hash_table, &irq->link);
irq_spinlock_unlock(&irq->lock, false);
irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
kobject_initialize(kobject, KOBJECT_TYPE_IRQ, irq);
cap_publish(TASK, handle, kobject);
return EOK;
}
errno_t ipc_irq_unsubscribe(answerbox_t *box, cap_irq_handle_t handle)
{
kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_IRQ);
if (!kobj)
return ENOENT;
assert(kobj->irq->notif_cfg.answerbox == box);
irq_hash_out(kobj->irq);
kobject_put(kobj);
cap_free(TASK, handle);
return EOK;
}
static void send_call(irq_t *irq, call_t *call)
{
irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);
list_append(&call->ab_link, &irq->notif_cfg.answerbox->irq_notifs);
irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false);
waitq_wake_one(&irq->notif_cfg.answerbox->wq);
}
irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
{
irq_code_t *code = irq->notif_cfg.code;
uint32_t *scratch = irq->notif_cfg.scratch;
if (!irq->notif_cfg.notify)
return IRQ_DECLINE;
if (!code)
return IRQ_DECLINE;
for (size_t i = 0; i < code->cmdcount; i++) {
uintptr_t srcarg = code->cmds[i].srcarg;
uintptr_t dstarg = code->cmds[i].dstarg;
switch (code->cmds[i].cmd) {
case CMD_PIO_READ_8:
scratch[dstarg] =
pio_read_8((ioport8_t *) code->cmds[i].addr);
break;
case CMD_PIO_READ_16:
scratch[dstarg] =
pio_read_16((ioport16_t *) code->cmds[i].addr);
break;
case CMD_PIO_READ_32:
scratch[dstarg] =
pio_read_32((ioport32_t *) code->cmds[i].addr);
break;
case CMD_PIO_WRITE_8:
pio_write_8((ioport8_t *) code->cmds[i].addr,
(uint8_t) code->cmds[i].value);
break;
case CMD_PIO_WRITE_16:
pio_write_16((ioport16_t *) code->cmds[i].addr,
(uint16_t) code->cmds[i].value);
break;
case CMD_PIO_WRITE_32:
pio_write_32((ioport32_t *) code->cmds[i].addr,
(uint32_t) code->cmds[i].value);
break;
case CMD_PIO_WRITE_A_8:
pio_write_8((ioport8_t *) code->cmds[i].addr,
(uint8_t) scratch[srcarg]);
break;
case CMD_PIO_WRITE_A_16:
pio_write_16((ioport16_t *) code->cmds[i].addr,
(uint16_t) scratch[srcarg]);
break;
case CMD_PIO_WRITE_A_32:
pio_write_32((ioport32_t *) code->cmds[i].addr,
(uint32_t) scratch[srcarg]);
break;
case CMD_LOAD:
scratch[dstarg] = code->cmds[i].value;
break;
case CMD_AND:
scratch[dstarg] = scratch[srcarg] &
code->cmds[i].value;
break;
case CMD_PREDICATE:
if (scratch[srcarg] == 0)
i += code->cmds[i].value;
break;
case CMD_ACCEPT:
return IRQ_ACCEPT;
case CMD_DECLINE:
default:
return IRQ_DECLINE;
}
}
return IRQ_DECLINE;
}
void ipc_irq_top_half_handler(irq_t *irq)
{
assert(irq);
assert(interrupts_disabled());
assert(irq_spinlock_locked(&irq->lock));
if (irq->notif_cfg.answerbox) {
call_t *call = ipc_call_alloc();
if (!call)
return;
call->flags |= IPC_CALL_NOTIF;
call->priv = ++irq->notif_cfg.counter;
ipc_set_imethod(&call->data, irq->notif_cfg.imethod);
ipc_set_arg1(&call->data, irq->notif_cfg.scratch[1]);
ipc_set_arg2(&call->data, irq->notif_cfg.scratch[2]);
ipc_set_arg3(&call->data, irq->notif_cfg.scratch[3]);
ipc_set_arg4(&call->data, irq->notif_cfg.scratch[4]);
ipc_set_arg5(&call->data, irq->notif_cfg.scratch[5]);
send_call(irq, call);
}
}
void ipc_irq_send_msg(irq_t *irq, sysarg_t a1, sysarg_t a2, sysarg_t a3,
sysarg_t a4, sysarg_t a5)
{
irq_spinlock_lock(&irq->lock, true);
if (irq->notif_cfg.answerbox) {
call_t *call = ipc_call_alloc();
if (!call) {
irq_spinlock_unlock(&irq->lock, true);
return;
}
call->flags |= IPC_CALL_NOTIF;
call->priv = ++irq->notif_cfg.counter;
ipc_set_imethod(&call->data, irq->notif_cfg.imethod);
ipc_set_arg1(&call->data, a1);
ipc_set_arg2(&call->data, a2);
ipc_set_arg3(&call->data, a3);
ipc_set_arg4(&call->data, a4);
ipc_set_arg5(&call->data, a5);
send_call(irq, call);
}
irq_spinlock_unlock(&irq->lock, true);
}
HelenOS homepage, sources at GitHub