HelenOS sources
This source file includes following definitions.
- _ipc_call_init
- call_destroy
- ipc_call_alloc
- ipc_answerbox_init
- ipc_phone_connect
- ipc_phone_init
- ipc_call_sync
- _ipc_answer_free_call
- ipc_answer
- _ipc_call_actions_internal
- ipc_backsend_err
- _ipc_call
- ipc_call
- ipc_phone_hangup
- ipc_forward
- ipc_wait_for_call
- ipc_cleanup_call_list
- ipc_answerbox_slam_phones
- ipc_forget_call
- ipc_forget_all_active_calls
- phone_cap_cleanup_cb
- ipc_wait_for_all_answered_calls
- irq_cap_cleanup_cb
- call_cap_cleanup_cb
- ipc_cleanup
- ipc_init
- ipc_print_call_list
- print_task_phone_cb
- ipc_print_task
#include <assert.h>
#include <synch/spinlock.h>
#include <synch/mutex.h>
#include <synch/waitq.h>
#include <ipc/ipc.h>
#include <ipc/ipcrsc.h>
#include <abi/ipc/methods.h>
#include <ipc/kbox.h>
#include <ipc/event.h>
#include <ipc/sysipc_ops.h>
#include <ipc/sysipc_priv.h>
#include <errno.h>
#include <mm/slab.h>
#include <arch.h>
#include <proc/task.h>
#include <memw.h>
#include <stdio.h>
#include <console/console.h>
#include <proc/thread.h>
#include <arch/interrupt.h>
#include <ipc/irq.h>
#include <cap/cap.h>
#include <stdlib.h>
static void ipc_forget_call(call_t *);
answerbox_t *ipc_box_0 = NULL;
static slab_cache_t *call_cache;
static slab_cache_t *answerbox_cache;
slab_cache_t *phone_cache = NULL;
static void _ipc_call_init(call_t *call)
{
memsetb(call, sizeof(*call), 0);
spinlock_initialize(&call->forget_lock, "forget_lock");
call->active = false;
call->forget = false;
call->sender = NULL;
call->callerbox = NULL;
call->buffer = NULL;
}
static void call_destroy(void *arg)
{
call_t *call = (call_t *) arg;
if (call->buffer)
free(call->buffer);
if (call->caller_phone)
kobject_put(call->caller_phone->kobject);
slab_free(call_cache, call);
}
kobject_ops_t call_kobject_ops = {
.destroy = call_destroy
};
call_t *ipc_call_alloc(void)
{
call_t *call = slab_alloc(call_cache, FRAME_ATOMIC);
if (!call)
return NULL;
kobject_t *kobj = kobject_alloc(0);
if (!kobj) {
slab_free(call_cache, call);
return NULL;
}
_ipc_call_init(call);
kobject_initialize(kobj, KOBJECT_TYPE_CALL, call);
call->kobject = kobj;
return call;
}
void ipc_answerbox_init(answerbox_t *box, task_t *task)
{
irq_spinlock_initialize(&box->lock, "ipc.box.lock");
irq_spinlock_initialize(&box->irq_lock, "ipc.box.irqlock");
waitq_initialize(&box->wq);
list_initialize(&box->connected_phones);
list_initialize(&box->calls);
list_initialize(&box->dispatched_calls);
list_initialize(&box->answers);
list_initialize(&box->irq_notifs);
atomic_store(&box->active_calls, 0);
box->task = task;
}
bool ipc_phone_connect(phone_t *phone, answerbox_t *box)
{
bool connected;
mutex_lock(&phone->lock);
irq_spinlock_lock(&box->lock, true);
connected = box->active && (phone->state == IPC_PHONE_CONNECTING);
if (connected) {
phone->state = IPC_PHONE_CONNECTED;
phone->callee = box;
list_append(&phone->link, &box->connected_phones);
}
irq_spinlock_unlock(&box->lock, true);
mutex_unlock(&phone->lock);
if (!connected) {
kobject_put(phone->kobject);
}
return connected;
}
void ipc_phone_init(phone_t *phone, task_t *caller)
{
mutex_initialize(&phone->lock, MUTEX_PASSIVE);
phone->caller = caller;
phone->callee = NULL;
phone->state = IPC_PHONE_FREE;
atomic_store(&phone->active_calls, 0);
phone->label = 0;
phone->kobject = NULL;
}
errno_t ipc_call_sync(phone_t *phone, call_t *request)
{
answerbox_t *mybox = slab_alloc(answerbox_cache, FRAME_ATOMIC);
if (!mybox)
return ENOMEM;
ipc_answerbox_init(mybox, TASK);
request->callerbox = mybox;
errno_t rc = ipc_call(phone, request);
if (rc != EOK) {
slab_free(answerbox_cache, mybox);
return rc;
}
call_t *answer = NULL;
(void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
SYNCH_FLAGS_INTERRUPTIBLE, &answer);
if (!answer) {
spinlock_lock(&request->forget_lock);
spinlock_lock(&TASK->active_calls_lock);
assert(!request->forget);
bool answered = !request->active;
if (!answered) {
ipc_forget_call(request);
rc = EINTR;
} else {
spinlock_unlock(&TASK->active_calls_lock);
spinlock_unlock(&request->forget_lock);
}
if (answered) {
(void) ipc_wait_for_call(mybox, SYNCH_NO_TIMEOUT,
SYNCH_FLAGS_NONE, &answer);
}
}
assert(!answer || request == answer);
slab_free(answerbox_cache, mybox);
return rc;
}
void _ipc_answer_free_call(call_t *call, bool selflocked)
{
irq_spinlock_lock(&TASK->lock, true);
TASK->ipc_info.answer_sent++;
irq_spinlock_unlock(&TASK->lock, true);
spinlock_lock(&call->forget_lock);
if (call->forget) {
spinlock_unlock(&call->forget_lock);
kobject_put(call->kobject);
return;
} else {
if (call->active) {
spinlock_lock(&call->sender->active_calls_lock);
list_remove(&call->ta_link);
spinlock_unlock(&call->sender->active_calls_lock);
}
}
spinlock_unlock(&call->forget_lock);
answerbox_t *callerbox = call->callerbox ? call->callerbox :
&call->sender->answerbox;
bool do_lock = ((!selflocked) || (callerbox != &TASK->answerbox));
call->flags |= IPC_CALL_ANSWERED;
call->data.task_id = TASK->taskid;
if (do_lock)
irq_spinlock_lock(&callerbox->lock, true);
list_append(&call->ab_link, &callerbox->answers);
if (do_lock)
irq_spinlock_unlock(&callerbox->lock, true);
waitq_wake_one(&callerbox->wq);
}
void ipc_answer(answerbox_t *box, call_t *call)
{
irq_spinlock_lock(&box->lock, true);
list_remove(&call->ab_link);
irq_spinlock_unlock(&box->lock, true);
_ipc_answer_free_call(call, false);
}
static void _ipc_call_actions_internal(phone_t *phone, call_t *call,
bool preforget)
{
task_t *caller = phone->caller;
call->caller_phone = phone;
kobject_add_ref(phone->kobject);
if (preforget) {
call->forget = true;
} else {
atomic_inc(&phone->active_calls);
if (call->callerbox)
atomic_inc(&call->callerbox->active_calls);
else
atomic_inc(&caller->answerbox.active_calls);
kobject_add_ref(phone->kobject);
call->sender = caller;
call->active = true;
spinlock_lock(&caller->active_calls_lock);
list_append(&call->ta_link, &caller->active_calls);
spinlock_unlock(&caller->active_calls_lock);
}
call->data.request_label = phone->label;
call->data.task_id = caller->taskid;
}
void ipc_backsend_err(phone_t *phone, call_t *call, errno_t err)
{
_ipc_call_actions_internal(phone, call, false);
ipc_set_retval(&call->data, err);
_ipc_answer_free_call(call, false);
}
static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call,
bool preforget)
{
task_t *caller = phone->caller;
irq_spinlock_lock(&caller->lock, true);
caller->ipc_info.call_sent++;
irq_spinlock_unlock(&caller->lock, true);
if (!(call->flags & IPC_CALL_FORWARDED))
_ipc_call_actions_internal(phone, call, preforget);
irq_spinlock_lock(&box->lock, true);
list_append(&call->ab_link, &box->calls);
irq_spinlock_unlock(&box->lock, true);
waitq_wake_one(&box->wq);
}
errno_t ipc_call(phone_t *phone, call_t *call)
{
mutex_lock(&phone->lock);
if (phone->state != IPC_PHONE_CONNECTED) {
mutex_unlock(&phone->lock);
if (!(call->flags & IPC_CALL_FORWARDED)) {
if (phone->state == IPC_PHONE_HUNGUP)
ipc_backsend_err(phone, call, EHANGUP);
else
ipc_backsend_err(phone, call, ENOENT);
}
return ENOENT;
}
answerbox_t *box = phone->callee;
_ipc_call(phone, box, call, false);
mutex_unlock(&phone->lock);
return 0;
}
errno_t ipc_phone_hangup(phone_t *phone)
{
mutex_lock(&phone->lock);
if (phone->state == IPC_PHONE_FREE ||
phone->state == IPC_PHONE_HUNGUP ||
phone->state == IPC_PHONE_CONNECTING) {
mutex_unlock(&phone->lock);
return EINVAL;
}
answerbox_t *box = phone->callee;
if (phone->state != IPC_PHONE_SLAMMED) {
irq_spinlock_lock(&box->lock, true);
list_remove(&phone->link);
irq_spinlock_unlock(&box->lock, true);
kobject_put(phone->kobject);
call_t *call = phone->hangup_call;
phone->hangup_call = NULL;
assert(call);
ipc_set_imethod(&call->data, IPC_M_PHONE_HUNGUP);
call->request_method = IPC_M_PHONE_HUNGUP;
call->flags |= IPC_CALL_DISCARD_ANSWER;
_ipc_call(phone, box, call, false);
}
phone->state = IPC_PHONE_HUNGUP;
mutex_unlock(&phone->lock);
return EOK;
}
errno_t ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox,
unsigned int mode)
{
irq_spinlock_lock(&TASK->lock, true);
TASK->ipc_info.forwarded++;
irq_spinlock_pass(&TASK->lock, &oldbox->lock);
list_remove(&call->ab_link);
irq_spinlock_unlock(&oldbox->lock, true);
if (mode & IPC_FF_ROUTE_FROM_ME) {
call->data.request_label = newphone->label;
call->data.task_id = TASK->taskid;
}
return ipc_call(newphone, call);
}
errno_t ipc_wait_for_call(answerbox_t *box, uint32_t usec, unsigned int flags,
call_t **call)
{
call_t *request;
uint64_t irq_cnt = 0;
uint64_t answer_cnt = 0;
uint64_t call_cnt = 0;
errno_t rc;
rc = _waitq_sleep_timeout(&box->wq, usec, flags);
if (rc != EOK)
return rc;
irq_spinlock_lock(&box->lock, true);
if (!list_empty(&box->irq_notifs)) {
irq_cnt++;
irq_spinlock_lock(&box->irq_lock, false);
request = list_get_instance(list_first(&box->irq_notifs),
call_t, ab_link);
list_remove(&request->ab_link);
irq_spinlock_unlock(&box->irq_lock, false);
} else if (!list_empty(&box->answers)) {
answer_cnt++;
request = list_get_instance(list_first(&box->answers),
call_t, ab_link);
list_remove(&request->ab_link);
atomic_dec(&request->caller_phone->active_calls);
atomic_dec(&box->active_calls);
kobject_put(request->caller_phone->kobject);
} else if (!list_empty(&box->calls)) {
call_cnt++;
request = list_get_instance(list_first(&box->calls),
call_t, ab_link);
list_remove(&request->ab_link);
list_append(&request->ab_link, &box->dispatched_calls);
} else {
irq_spinlock_unlock(&box->lock, true);
return ENOENT;
}
irq_spinlock_pass(&box->lock, &TASK->lock);
TASK->ipc_info.irq_notif_received += irq_cnt;
TASK->ipc_info.answer_received += answer_cnt;
TASK->ipc_info.call_received += call_cnt;
irq_spinlock_unlock(&TASK->lock, true);
*call = request;
return EOK;
}
void ipc_cleanup_call_list(answerbox_t *box, list_t *lst)
{
irq_spinlock_lock(&box->lock, true);
while (!list_empty(lst)) {
call_t *call = list_get_instance(list_first(lst), call_t,
ab_link);
list_remove(&call->ab_link);
irq_spinlock_unlock(&box->lock, true);
if (lst == &box->calls)
SYSIPC_OP(request_process, call, box);
ipc_data_t old = call->data;
ipc_set_retval(&call->data, EHANGUP);
answer_preprocess(call, &old);
_ipc_answer_free_call(call, true);
irq_spinlock_lock(&box->lock, true);
}
irq_spinlock_unlock(&box->lock, true);
}
void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
{
phone_t *phone;
DEADLOCK_PROBE_INIT(p_phonelck);
restart_phones:
irq_spinlock_lock(&box->lock, true);
while (!list_empty(&box->connected_phones)) {
phone = list_get_instance(list_first(&box->connected_phones),
phone_t, link);
if (mutex_trylock(&phone->lock) != EOK) {
irq_spinlock_unlock(&box->lock, true);
DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
goto restart_phones;
}
assert(phone->state == IPC_PHONE_CONNECTED);
list_remove(&phone->link);
phone->state = IPC_PHONE_SLAMMED;
phone->label = 0;
if (notify_box) {
task_hold(phone->caller);
mutex_unlock(&phone->lock);
irq_spinlock_unlock(&box->lock, true);
call_t *call = phone->hangup_call;
phone->hangup_call = NULL;
assert(call);
ipc_set_imethod(&call->data, IPC_M_PHONE_HUNGUP);
call->request_method = IPC_M_PHONE_HUNGUP;
call->flags |= IPC_CALL_DISCARD_ANSWER;
_ipc_call(phone, box, call, true);
task_release(phone->caller);
kobject_put(phone->kobject);
goto restart_phones;
}
mutex_unlock(&phone->lock);
kobject_put(phone->kobject);
}
irq_spinlock_unlock(&box->lock, true);
}
static void ipc_forget_call(call_t *call)
{
assert(spinlock_locked(&TASK->active_calls_lock));
assert(spinlock_locked(&call->forget_lock));
call->forget = true;
call->sender = NULL;
list_remove(&call->ta_link);
kobject_add_ref(call->kobject);
spinlock_unlock(&call->forget_lock);
spinlock_unlock(&TASK->active_calls_lock);
atomic_dec(&call->caller_phone->active_calls);
atomic_dec(&TASK->answerbox.active_calls);
kobject_put(call->caller_phone->kobject);
SYSIPC_OP(request_forget, call);
kobject_put(call->kobject);
}
static void ipc_forget_all_active_calls(void)
{
call_t *call;
restart:
spinlock_lock(&TASK->active_calls_lock);
if (list_empty(&TASK->active_calls)) {
spinlock_unlock(&TASK->active_calls_lock);
return;
}
call = list_get_instance(list_first(&TASK->active_calls), call_t,
ta_link);
if (!spinlock_trylock(&call->forget_lock)) {
spinlock_unlock(&TASK->active_calls_lock);
goto restart;
}
ipc_forget_call(call);
goto restart;
}
static bool phone_cap_cleanup_cb(cap_t *cap, void *arg)
{
ipc_phone_hangup(cap->kobject->phone);
kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
KOBJECT_TYPE_PHONE);
kobject_put(kobj);
cap_free(cap->task, cap->handle);
return true;
}
static void ipc_wait_for_all_answered_calls(void)
{
while (atomic_load(&TASK->answerbox.active_calls) != 0) {
call_t *call = NULL;
if (ipc_wait_for_call(&TASK->answerbox,
SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, &call) == ENOENT)
continue;
assert(call);
assert(call->flags & (IPC_CALL_ANSWERED | IPC_CALL_NOTIF));
SYSIPC_OP(answer_process, call);
kobject_put(call->kobject);
caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
phone_cap_cleanup_cb, NULL);
ipc_forget_all_active_calls();
}
}
static bool irq_cap_cleanup_cb(cap_t *cap, void *arg)
{
ipc_irq_unsubscribe(&TASK->answerbox, cap->handle);
return true;
}
static bool call_cap_cleanup_cb(cap_t *cap, void *arg)
{
kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
KOBJECT_TYPE_CALL);
kobject_put(kobj);
cap_free(cap->task, cap->handle);
return true;
}
void ipc_cleanup(void)
{
irq_spinlock_lock(&TASK->answerbox.lock, true);
TASK->answerbox.active = false;
irq_spinlock_unlock(&TASK->answerbox.lock, true);
caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_PHONE,
phone_cap_cleanup_cb, NULL);
event_cleanup_answerbox(&TASK->answerbox);
caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_IRQ, irq_cap_cleanup_cb,
NULL);
ipc_answerbox_slam_phones(&TASK->answerbox, false);
#ifdef CONFIG_UDEBUG
ipc_kbox_cleanup();
#endif
caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_CALL, call_cap_cleanup_cb,
NULL);
ipc_cleanup_call_list(&TASK->answerbox, &TASK->answerbox.calls);
ipc_cleanup_call_list(&TASK->answerbox,
&TASK->answerbox.dispatched_calls);
ipc_forget_all_active_calls();
ipc_wait_for_all_answered_calls();
assert(atomic_load(&TASK->answerbox.active_calls) == 0);
}
void ipc_init(void)
{
call_cache = slab_cache_create("call_t", sizeof(call_t), 0, NULL,
NULL, 0);
phone_cache = slab_cache_create("phone_t", sizeof(phone_t), 0, NULL,
NULL, 0);
answerbox_cache = slab_cache_create("answerbox_t", sizeof(answerbox_t),
0, NULL, NULL, 0);
}
static void ipc_print_call_list(list_t *list)
{
list_foreach(*list, ab_link, call_t, call) {
#ifdef __32_BITS__
printf("%10p ", call);
#endif
#ifdef __64_BITS__
printf("%18p ", call);
#endif
spinlock_lock(&call->forget_lock);
printf("%-8" PRIun " %-6" PRIun " %-6" PRIun " %-6" PRIun
" %-6" PRIun " %-6" PRIun " %-7x",
ipc_get_imethod(&call->data), ipc_get_arg1(&call->data),
ipc_get_arg2(&call->data), ipc_get_arg3(&call->data),
ipc_get_arg4(&call->data), ipc_get_arg5(&call->data),
call->flags);
if (call->forget) {
printf(" ? (call forgotten)\n");
} else {
printf(" %" PRIu64 " (%s)\n",
call->sender->taskid, call->sender->name);
}
spinlock_unlock(&call->forget_lock);
}
}
static bool print_task_phone_cb(cap_t *cap, void *arg)
{
phone_t *phone = cap->kobject->phone;
mutex_lock(&phone->lock);
if (phone->state != IPC_PHONE_FREE) {
printf("%-11d %7" PRIun " ", (int) cap_handle_raw(cap->handle),
atomic_load(&phone->active_calls));
switch (phone->state) {
case IPC_PHONE_CONNECTING:
printf("connecting");
break;
case IPC_PHONE_CONNECTED:
printf("connected to %" PRIu64 " (%s)",
phone->callee->task->taskid,
phone->callee->task->name);
break;
case IPC_PHONE_SLAMMED:
printf("slammed by %p", phone->callee);
break;
case IPC_PHONE_HUNGUP:
printf("hung up to %p", phone->callee);
break;
default:
break;
}
printf("\n");
}
mutex_unlock(&phone->lock);
return true;
}
void ipc_print_task(task_id_t taskid)
{
task_t *task = task_find_by_id(taskid);
if (!task)
return;
printf("[phone cap] [calls] [state\n");
caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
print_task_phone_cb, NULL);
irq_spinlock_lock(&task->lock, true);
irq_spinlock_lock(&task->answerbox.lock, false);
printf("Active calls: %" PRIun "\n",
atomic_load(&task->answerbox.active_calls));
#ifdef __32_BITS__
printf("[call adr] [method] [arg1] [arg2] [arg3] [arg4] [arg5]"
" [flags] [sender\n");
#endif
#ifdef __64_BITS__
printf("[call address ] [method] [arg1] [arg2] [arg3] [arg4]"
" [arg5] [flags] [sender\n");
#endif
printf(" --- incomming calls ---\n");
ipc_print_call_list(&task->answerbox.calls);
printf(" --- dispatched calls ---\n");
ipc_print_call_list(&task->answerbox.dispatched_calls);
printf(" --- incoming answers ---\n");
ipc_print_call_list(&task->answerbox.answers);
irq_spinlock_unlock(&task->answerbox.lock, false);
irq_spinlock_unlock(&task->lock, true);
task_release(task);
}
HelenOS homepage, sources at GitHub