HelenOS sources
This source file includes following definitions.
- method_is_system
- method_is_forwardable
- method_is_immutable
- answer_need_old
- answer_preprocess
- request_preprocess
- process_answer
- process_request
- ipc_req_internal
- check_call_limit
- sys_ipc_call_async_fast
- sys_ipc_call_async_slow
- sys_ipc_forward_common
- sys_ipc_forward_fast
- sys_ipc_forward_slow
- sys_ipc_answer_fast
- sys_ipc_answer_slow
- sys_ipc_hangup
- sys_ipc_wait_for_call
- sys_ipc_poke
- sys_ipc_irq_subscribe
- sys_ipc_irq_unsubscribe
- sys_ipc_connect_kbox
#include <arch.h>
#include <assert.h>
#include <errno.h>
#include <memw.h>
#include <ipc/ipc.h>
#include <abi/ipc/methods.h>
#include <ipc/sysipc.h>
#include <ipc/sysipc_ops.h>
#include <ipc/sysipc_priv.h>
#include <ipc/irq.h>
#include <ipc/ipcrsc.h>
#include <ipc/event.h>
#include <ipc/kbox.h>
#include <synch/waitq.h>
#include <arch/interrupt.h>
#include <syscall/copy.h>
#include <security/perm.h>
#include <console/console.h>
#include <macros.h>
#include <cap/cap.h>
#define STRUCT_TO_USPACE(dst, src) copy_to_uspace((dst), (src), sizeof(*(src)))
static inline bool method_is_system(sysarg_t imethod)
{
if (imethod <= IPC_M_LAST_SYSTEM)
return true;
return false;
}
static inline bool method_is_forwardable(sysarg_t imethod)
{
switch (imethod) {
case IPC_M_PHONE_HUNGUP:
return false;
default:
return true;
}
}
static inline bool method_is_immutable(sysarg_t imethod)
{
switch (imethod) {
case IPC_M_PAGE_IN:
case IPC_M_SHARE_OUT:
case IPC_M_SHARE_IN:
case IPC_M_DATA_WRITE:
case IPC_M_DATA_READ:
case IPC_M_STATE_CHANGE_AUTHORIZE:
return true;
default:
return false;
}
}
static inline bool answer_need_old(call_t *call)
{
switch (ipc_get_imethod(&call->data)) {
case IPC_M_CONNECT_TO_ME:
case IPC_M_CONNECT_ME_TO:
case IPC_M_PAGE_IN:
case IPC_M_SHARE_OUT:
case IPC_M_SHARE_IN:
case IPC_M_DATA_WRITE:
case IPC_M_DATA_READ:
case IPC_M_STATE_CHANGE_AUTHORIZE:
return true;
default:
return false;
}
}
errno_t answer_preprocess(call_t *answer, ipc_data_t *olddata)
{
errno_t rc = EOK;
spinlock_lock(&answer->forget_lock);
if (answer->forget) {
spinlock_unlock(&answer->forget_lock);
SYSIPC_OP(answer_cleanup, answer, olddata);
return rc;
} else {
assert(answer->active);
answer->active = false;
spinlock_lock(&answer->sender->active_calls_lock);
list_remove(&answer->ta_link);
spinlock_unlock(&answer->sender->active_calls_lock);
}
spinlock_unlock(&answer->forget_lock);
if ((errno_t) ipc_get_retval(&answer->data) == EHANGUP) {
phone_t *phone = answer->caller_phone;
mutex_lock(&phone->lock);
if (phone->state == IPC_PHONE_CONNECTED) {
irq_spinlock_lock(&phone->callee->lock, true);
list_remove(&phone->link);
kobject_put(phone->kobject);
phone->state = IPC_PHONE_SLAMMED;
phone->label = 0;
irq_spinlock_unlock(&phone->callee->lock, true);
}
mutex_unlock(&phone->lock);
}
if (!olddata)
return rc;
return SYSIPC_OP(answer_preprocess, answer, olddata);
}
static errno_t request_preprocess(call_t *call, phone_t *phone)
{
call->request_method = ipc_get_imethod(&call->data);
return SYSIPC_OP(request_preprocess, call, phone);
}
static void process_answer(call_t *call)
{
if (((errno_t) ipc_get_retval(&call->data) == EHANGUP) &&
(call->flags & IPC_CALL_FORWARDED))
ipc_set_retval(&call->data, EFORWARD);
SYSIPC_OP(answer_process, call);
}
static int process_request(answerbox_t *box, call_t *call)
{
return SYSIPC_OP(request_process, call, box);
}
errno_t
ipc_req_internal(cap_phone_handle_t handle, ipc_data_t *data, sysarg_t priv)
{
kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
if (!kobj->phone)
return ENOENT;
call_t *call = ipc_call_alloc();
if (!call) {
kobject_put(kobj);
return ENOMEM;
}
call->priv = priv;
memcpy(call->data.args, data->args, sizeof(data->args));
errno_t rc = request_preprocess(call, kobj->phone);
if (!rc) {
#ifdef CONFIG_UDEBUG
udebug_stoppable_begin();
#endif
kobject_add_ref(call->kobject);
rc = ipc_call_sync(kobj->phone, call);
spinlock_lock(&call->forget_lock);
bool forgotten = call->forget;
spinlock_unlock(&call->forget_lock);
kobject_put(call->kobject);
#ifdef CONFIG_UDEBUG
udebug_stoppable_end();
#endif
if (rc != EOK) {
if (!forgotten) {
kobject_put(call->kobject);
} else {
assert(rc == EINTR);
}
kobject_put(kobj);
return rc;
}
process_answer(call);
} else
ipc_set_retval(&call->data, rc);
memcpy(data->args, call->data.args, sizeof(data->args));
kobject_put(call->kobject);
kobject_put(kobj);
return EOK;
}
static int check_call_limit(phone_t *phone)
{
if (atomic_load(&phone->active_calls) >= IPC_MAX_ASYNC_CALLS)
return -1;
return 0;
}
sys_errno_t sys_ipc_call_async_fast(cap_phone_handle_t handle, sysarg_t imethod,
sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t label)
{
kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
if (!kobj)
return ENOENT;
if (check_call_limit(kobj->phone)) {
kobject_put(kobj);
return ELIMIT;
}
call_t *call = ipc_call_alloc();
if (!call) {
kobject_put(kobj);
return ENOMEM;
}
ipc_set_imethod(&call->data, imethod);
ipc_set_arg1(&call->data, arg1);
ipc_set_arg2(&call->data, arg2);
ipc_set_arg3(&call->data, arg3);
ipc_set_arg5(&call->data, 0);
call->data.answer_label = label;
errno_t res = request_preprocess(call, kobj->phone);
if (!res)
ipc_call(kobj->phone, call);
else
ipc_backsend_err(kobj->phone, call, res);
kobject_put(kobj);
return EOK;
}
sys_errno_t sys_ipc_call_async_slow(cap_phone_handle_t handle, uspace_ptr_ipc_data_t data,
sysarg_t label)
{
kobject_t *kobj = kobject_get(TASK, handle, KOBJECT_TYPE_PHONE);
if (!kobj)
return ENOENT;
if (check_call_limit(kobj->phone)) {
kobject_put(kobj);
return ELIMIT;
}
call_t *call = ipc_call_alloc();
if (!call) {
kobject_put(kobj);
return ENOMEM;
}
errno_t rc = copy_from_uspace(&call->data.args, data + offsetof(ipc_data_t, args),
sizeof(call->data.args));
if (rc != EOK) {
kobject_put(call->kobject);
kobject_put(kobj);
return (sys_errno_t) rc;
}
call->data.answer_label = label;
errno_t res = request_preprocess(call, kobj->phone);
if (!res)
ipc_call(kobj->phone, call);
else
ipc_backsend_err(kobj->phone, call, res);
kobject_put(kobj);
return EOK;
}
static sys_errno_t sys_ipc_forward_common(cap_call_handle_t chandle,
cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, unsigned int mode, bool slow)
{
kobject_t *ckobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
if (!ckobj)
return ENOENT;
call_t *call = ckobj->call;
ipc_data_t old;
bool need_old = answer_need_old(call);
if (need_old)
old = call->data;
bool after_forward = false;
errno_t rc;
kobject_t *pkobj = kobject_get(TASK, phandle, KOBJECT_TYPE_PHONE);
if (!pkobj) {
rc = ENOENT;
goto error;
}
if (!method_is_forwardable(ipc_get_imethod(&call->data))) {
rc = EPERM;
goto error;
}
call->flags |= IPC_CALL_FORWARDED;
if (!method_is_immutable(ipc_get_imethod(&call->data))) {
if (method_is_system(ipc_get_imethod(&call->data))) {
if (ipc_get_imethod(&call->data) ==
IPC_M_CONNECT_TO_ME) {
kobject_put((kobject_t *) call->priv);
call->priv = 0;
cap_free(TASK,
(cap_handle_t) ipc_get_arg5(&call->data));
}
ipc_set_arg1(&call->data, imethod);
ipc_set_arg2(&call->data, arg1);
ipc_set_arg3(&call->data, arg2);
if (slow)
ipc_set_arg4(&call->data, arg3);
} else {
ipc_set_imethod(&call->data, imethod);
ipc_set_arg1(&call->data, arg1);
ipc_set_arg2(&call->data, arg2);
if (slow) {
ipc_set_arg3(&call->data, arg3);
ipc_set_arg4(&call->data, arg4);
ipc_set_arg5(&call->data, arg5);
}
}
}
rc = ipc_forward(call, pkobj->phone, &TASK->answerbox, mode);
if (rc != EOK) {
after_forward = true;
goto error;
}
cap_free(TASK, chandle);
kobject_put(ckobj);
kobject_put(pkobj);
return EOK;
error:
ipc_set_retval(&call->data, EFORWARD);
(void) answer_preprocess(call, need_old ? &old : NULL);
if (after_forward)
_ipc_answer_free_call(call, false);
else
ipc_answer(&TASK->answerbox, call);
cap_free(TASK, chandle);
kobject_put(ckobj);
if (pkobj)
kobject_put(pkobj);
return rc;
}
sys_errno_t sys_ipc_forward_fast(cap_call_handle_t chandle,
cap_phone_handle_t phandle, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
unsigned int mode)
{
return sys_ipc_forward_common(chandle, phandle, imethod, arg1, arg2, 0,
0, 0, mode, false);
}
sys_errno_t sys_ipc_forward_slow(cap_call_handle_t chandle,
cap_phone_handle_t phandle, uspace_ptr_ipc_data_t data, unsigned int mode)
{
ipc_data_t newdata;
errno_t rc = copy_from_uspace(&newdata.args, data + offsetof(ipc_data_t, args),
sizeof(newdata.args));
if (rc != EOK)
return (sys_errno_t) rc;
return sys_ipc_forward_common(chandle, phandle,
ipc_get_imethod(&newdata), ipc_get_arg1(&newdata),
ipc_get_arg2(&newdata), ipc_get_arg3(&newdata),
ipc_get_arg4(&newdata), ipc_get_arg5(&newdata), mode, true);
}
sys_errno_t sys_ipc_answer_fast(cap_call_handle_t chandle, sysarg_t retval,
sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
{
kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
if (!kobj)
return ENOENT;
call_t *call = kobj->call;
assert(!(call->flags & IPC_CALL_ANSWERED));
ipc_data_t saved_data;
bool saved;
if (answer_need_old(call)) {
memcpy(&saved_data, &call->data, sizeof(call->data));
saved = true;
} else
saved = false;
ipc_set_retval(&call->data, retval);
ipc_set_arg1(&call->data, arg1);
ipc_set_arg2(&call->data, arg2);
ipc_set_arg3(&call->data, arg3);
ipc_set_arg4(&call->data, arg4);
ipc_set_arg5(&call->data, 0);
errno_t rc = answer_preprocess(call, saved ? &saved_data : NULL);
ipc_answer(&TASK->answerbox, call);
kobject_put(kobj);
cap_free(TASK, chandle);
return rc;
}
sys_errno_t sys_ipc_answer_slow(cap_call_handle_t chandle, uspace_ptr_ipc_data_t data)
{
kobject_t *kobj = cap_unpublish(TASK, chandle, KOBJECT_TYPE_CALL);
if (!kobj)
return ENOENT;
call_t *call = kobj->call;
assert(!(call->flags & IPC_CALL_ANSWERED));
ipc_data_t saved_data;
bool saved;
if (answer_need_old(call)) {
memcpy(&saved_data, &call->data, sizeof(call->data));
saved = true;
} else
saved = false;
errno_t rc = copy_from_uspace(&call->data.args, data + offsetof(ipc_data_t, args),
sizeof(call->data.args));
if (rc != EOK) {
cap_publish(TASK, chandle, kobj);
return rc;
}
rc = answer_preprocess(call, saved ? &saved_data : NULL);
ipc_answer(&TASK->answerbox, call);
kobject_put(kobj);
cap_free(TASK, chandle);
return rc;
}
sys_errno_t sys_ipc_hangup(cap_phone_handle_t handle)
{
kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
if (!kobj)
return ENOENT;
errno_t rc = ipc_phone_hangup(kobj->phone);
kobject_put(kobj);
cap_free(TASK, handle);
return rc;
}
sys_errno_t sys_ipc_wait_for_call(uspace_ptr_ipc_data_t calldata, uint32_t usec,
unsigned int flags)
{
call_t *call = NULL;
errno_t rc;
restart:
#ifdef CONFIG_UDEBUG
udebug_stoppable_begin();
#endif
rc = ipc_wait_for_call(&TASK->answerbox, usec,
flags | SYNCH_FLAGS_INTERRUPTIBLE, &call);
#ifdef CONFIG_UDEBUG
udebug_stoppable_end();
#endif
if (rc != EOK)
return rc;
assert(call);
call->data.flags = call->flags;
if (call->flags & IPC_CALL_NOTIF) {
call->data.request_label = (sysarg_t) call->priv;
call->data.cap_handle = CAP_NIL;
STRUCT_TO_USPACE(calldata, &call->data);
kobject_put(call->kobject);
return EOK;
}
if (call->flags & IPC_CALL_ANSWERED) {
process_answer(call);
if (call->flags & IPC_CALL_DISCARD_ANSWER) {
kobject_put(call->kobject);
goto restart;
}
call->data.cap_handle = CAP_NIL;
STRUCT_TO_USPACE(calldata, &call->data);
kobject_put(call->kobject);
return EOK;
}
if (process_request(&TASK->answerbox, call))
goto restart;
cap_handle_t handle = CAP_NIL;
rc = cap_alloc(TASK, &handle);
if (rc != EOK) {
goto error;
}
call->data.cap_handle = handle;
rc = STRUCT_TO_USPACE(calldata, &call->data);
if (rc != EOK)
goto error;
kobject_add_ref(call->kobject);
cap_publish(TASK, handle, call->kobject);
return EOK;
error:
if (cap_handle_valid(handle))
cap_free(TASK, handle);
ipc_data_t saved_data;
bool saved;
if (answer_need_old(call)) {
memcpy(&saved_data, &call->data, sizeof(call->data));
saved = true;
} else
saved = false;
ipc_set_retval(&call->data, EPARTY);
(void) answer_preprocess(call, saved ? &saved_data : NULL);
call->flags |= IPC_CALL_AUTO_REPLY;
ipc_answer(&TASK->answerbox, call);
return rc;
}
sys_errno_t sys_ipc_poke(void)
{
waitq_wake_one(&TASK->answerbox.wq);
return EOK;
}
sys_errno_t sys_ipc_irq_subscribe(inr_t inr, sysarg_t imethod,
uspace_ptr_irq_code_t ucode, uspace_ptr_cap_irq_handle_t uspace_handle)
{
if (!(perm_get(TASK) & PERM_IRQ_REG))
return EPERM;
return ipc_irq_subscribe(&TASK->answerbox, inr, imethod, ucode, uspace_handle);
}
sys_errno_t sys_ipc_irq_unsubscribe(cap_irq_handle_t handle)
{
if (!(perm_get(TASK) & PERM_IRQ_REG))
return EPERM;
ipc_irq_unsubscribe(&TASK->answerbox, handle);
return 0;
}
sys_errno_t sys_ipc_connect_kbox(uspace_ptr_task_id_t uspace_taskid,
uspace_ptr_cap_phone_handle_t uspace_phone)
{
#ifdef CONFIG_UDEBUG
task_id_t taskid;
cap_phone_handle_t phone;
errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(task_id_t));
if (rc == EOK) {
rc = ipc_connect_kbox((task_id_t) taskid, &phone);
}
if (rc == EOK) {
rc = copy_to_uspace(uspace_phone, &phone, sizeof(cap_handle_t));
if (rc != EOK) {
sys_ipc_hangup(phone);
}
}
return (sys_errno_t) rc;
#else
return (sys_errno_t) ENOTSUP;
#endif
}
HelenOS homepage, sources at GitHub