Subversion Repositories HelenOS-historic

Compare Revisions

Ignore whitespace Rev 965 → Rev 963

/kernel/trunk/generic/src/syscall/syscall.c
60,82 → 60,57
* @return Call identification, returns -1 on fatal error,
-2 on 'Too many async request, handle answers first
*/
static __native sys_ipc_call_sync(__native phoneid, __native method,
__native arg1, __native *data)
static __native sys_ipc_call_sync(__native phoneid, __native arg1,
__native arg2, __native *respdata)
{
call_t call;
call_t *call;
phone_t *phone;
/* Special answerbox for synchronous messages */
 
if (phoneid >= IPC_MAX_PHONES)
return IPC_CALLRET_FATAL;
return -ENOENT;
 
phone = &TASK->phones[phoneid];
if (!phone->callee)
return IPC_CALLRET_FATAL;
return -ENOENT;
 
ipc_call_init(&call);
IPC_SET_METHOD(call.data, method);
IPC_SET_ARG1(call.data, arg1);
call = ipc_call_alloc();
call->data[0] = arg1;
call->data[1] = arg2;
ipc_call_sync(phone, &call);
ipc_call_sync(phone, call);
 
copy_to_uspace(data, &call.data, sizeof(call.data));
copy_to_uspace(respdata, &call->data, sizeof(__native) * IPC_CALL_LEN);
 
return 0;
}
 
static __native sys_ipc_call_sync_medium(__native phoneid, __native *data)
{
call_t call;
phone_t *phone;
/* Special answerbox for synchronous messages */
 
if (phoneid >= IPC_MAX_PHONES)
return IPC_CALLRET_FATAL;
 
phone = &TASK->phones[phoneid];
if (!phone->callee)
return IPC_CALLRET_FATAL;
 
ipc_call_init(&call);
copy_from_uspace(&call.data, data, sizeof(call.data));
ipc_call_sync(phone, &call);
 
copy_to_uspace(data, &call.data, sizeof(call.data));
 
return 0;
}
 
 
/** Send an asynchronous call over ipc
*
* @return Call identification, returns -1 on fatal error,
-2 on 'Too many async request, handle answers first
*/
static __native sys_ipc_call_async(__native phoneid, __native method,
__native arg1, __native arg2)
static __native sys_ipc_call_async(__native phoneid, __native arg1,
__native arg2)
{
call_t *call;
phone_t *phone;
 
if (phoneid >= IPC_MAX_PHONES)
return IPC_CALLRET_FATAL;
return -ENOENT;
 
phone = &TASK->phones[phoneid];
if (!phone->callee)
return IPC_CALLRET_FATAL;
return -ENOENT;
 
 
/* TODO: Check that we did not exceed system imposed maximum
* of asynchrnously sent messages
* - the userspace should be able to handle it correctly
*/
call = ipc_call_alloc();
IPC_SET_METHOD(call->data, method);
IPC_SET_ARG1(call->data, arg1);
IPC_SET_ARG2(call->data, arg2);
 
call->data[0] = arg1;
call->data[1] = arg2;
ipc_call(phone, call);
 
return (__native) call;
142,8 → 117,7
}
 
/** Send IPC answer */
static __native sys_ipc_answer(__native callid, __native retval, __native arg1,
__native arg2)
static __native sys_ipc_answer(__native callid, __native arg1, __native arg2)
{
call_t *call;
 
152,9 → 126,8
/* TODO: Check that the callid is in the dispatch table */
call = (call_t *) callid;
 
IPC_SET_RETVAL(call->data, retval);
IPC_SET_ARG1(call->data, arg1);
IPC_SET_ARG2(call->data, arg2);
call->data[0] = arg1;
call->data[1] = arg2;
 
ipc_answer(&TASK->answerbox, call);
return 0;
171,13 → 144,10
call_t *call;
call = ipc_wait_for_call(&TASK->answerbox, flags);
copy_to_uspace(calldata, &call->data, sizeof(__native) * IPC_CALL_LEN);
 
copy_to_uspace(calldata, &call->data, sizeof(call->data));
if (call->flags & IPC_CALL_ANSWERED) {
ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
ipc_call_free(call);
return ((__native)call) | IPC_CALLID_ANSWERED;
}
if (call->flags & IPC_CALL_ANSWERED)
return ((__native)call) | 1;
return (__native)call;
}
 
186,7 → 156,6
sys_ctl,
sys_io,
sys_ipc_call_sync,
sys_ipc_call_sync_medium,
sys_ipc_call_async,
sys_ipc_answer,
sys_ipc_wait_for_call
/kernel/trunk/generic/src/ipc/ns.c
File deleted
/kernel/trunk/generic/src/ipc/ipc.c
31,8 → 31,7
* First the answerbox, then the phone
*/
 
#include <synch/condvar.h>
#include <synch/mutex.h>
#include <synch/waitq.h>
#include <ipc/ipc.h>
#include <errno.h>
#include <mm/slab.h>
44,8 → 43,7
#include <print.h>
#include <proc/thread.h>
 
/* Open channel that is assigned automatically to new tasks */
answerbox_t *ipc_phone_0 = NULL;
answerbox_t *ipc_central_box;
 
static slab_cache_t *ipc_call_slab;
 
65,13 → 63,6
return call;
}
 
/** Initialize allocated call */
void ipc_call_init(call_t *call)
{
call->callerbox = &TASK->answerbox;
call->flags = IPC_CALL_STATIC_ALLOC;
}
 
/** Deallocate call stracuture */
void ipc_call_free(call_t *call)
{
82,8 → 73,8
*/
void ipc_answerbox_init(answerbox_t *box)
{
mutex_initialize(&box->mutex);
condvar_initialize(&box->cv);
spinlock_initialize(&box->lock, "abox_lock");
waitq_initialize(&box->wq);
list_initialize(&box->connected_phones);
list_initialize(&box->calls);
list_initialize(&box->dispatched_calls);
97,10 → 88,9
spinlock_initialize(&phone->lock, "phone_lock");
phone->callee = box;
 
mutex_lock(&box->mutex);
spinlock_lock(&box->lock);
list_append(&phone->list, &box->connected_phones);
mutex_unlock(&box->mutex);
spinlock_unlock(&box->lock);
}
 
/** Disconnect phone from answerbox */
110,9 → 100,9
ASSERT(box);
 
mutex_lock(&box->mutex);
spinlock_lock(&box->lock);
list_remove(&phone->list);
mutex_unlock(&box->mutex);
spinlock_unlock(&box->lock);
}
 
/** Helper function to facilitate synchronous calls */
140,10 → 130,10
 
ASSERT(box);
 
mutex_lock(&box->mutex);
spinlock_lock(&box->lock);
list_append(&request->list, &box->calls);
mutex_unlock(&box->mutex);
condvar_signal(&box->cv);
spinlock_unlock(&box->lock);
waitq_wakeup(&box->wq, 0);
}
 
/** Answer message back to phone
157,14 → 147,15
 
request->flags |= IPC_CALL_ANSWERED;
 
mutex_lock(&box->mutex);
spinlock_lock(&box->lock);
spinlock_lock(&callerbox->lock);
 
list_remove(&request->list);
mutex_unlock(&box->mutex);
list_append(&request->list, &callerbox->answers);
waitq_wakeup(&callerbox->wq, 0);
 
mutex_lock(&callerbox->mutex);
list_append(&request->list, &callerbox->answers);
mutex_unlock(&callerbox->mutex);
condvar_signal(&callerbox->cv);
spinlock_unlock(&callerbox->lock);
spinlock_unlock(&box->lock);
}
 
/** Wait for phone call
176,30 → 167,30
{
call_t *request;
 
mutex_lock(&box->mutex);
while (1) {
if (!list_empty(&box->answers)) {
/* Handle asynchronous answers */
request = list_get_instance(box->answers.next, call_t, list);
list_remove(&request->list);
} else if (!list_empty(&box->calls)) {
/* Handle requests */
request = list_get_instance(box->calls.next, call_t, list);
list_remove(&request->list);
/* Append request to dispatch queue */
list_append(&request->list, &box->dispatched_calls);
} else {
if (!(flags & IPC_WAIT_NONBLOCKING)) {
condvar_wait(&box->cv, &box->mutex);
continue;
}
if (condvar_trywait(&box->cv, &box->mutex) != ESYNCH_WOULD_BLOCK)
continue;
request = NULL;
}
break;
if ((flags & IPC_WAIT_NONBLOCKING)) {
if (waitq_sleep_timeout(&box->wq,SYNCH_NO_TIMEOUT,SYNCH_NON_BLOCKING) == ESYNCH_WOULD_BLOCK)
return 0;
} else {
waitq_sleep(&box->wq);
}
mutex_unlock(&box->mutex);
 
 
// TODO - might need condition variable+mutex if we want to support
// removing of requests from queue before dispatch
spinlock_lock(&box->lock);
/* Handle answers first */
if (!list_empty(&box->answers)) {
request = list_get_instance(box->answers.next, call_t, list);
list_remove(&request->list);
} else {
ASSERT (! list_empty(&box->calls));
request = list_get_instance(box->calls.next, call_t, list);
list_remove(&request->list);
/* Append request to dispatch queue */
list_append(&request->list, &box->dispatched_calls);
}
spinlock_unlock(&box->lock);
 
return request;
}
 
211,3 → 202,32
0,
NULL, NULL, 0);
}
 
static void ipc_phonecompany_thread(void *data)
{
call_t *call;
 
printf("Phone company started.\n");
while (1) {
call = ipc_wait_for_call(&TASK->answerbox, 0);
printf("Received phone call - %P %P\n",
call->data[0], call->data[1]);
call->data[0] = 0xbabaaaee;;
call->data[1] = 0xaaaaeeee;
ipc_answer(&TASK->answerbox, call);
printf("Call answered.\n");
}
}
 
void ipc_create_phonecompany(void)
{
thread_t *t;
if ((t = thread_create(ipc_phonecompany_thread, "phonecompany",
TASK, 0)))
thread_ready(t);
else
panic("thread_create/phonecompany");
 
ipc_central_box = &TASK->answerbox;
}
/kernel/trunk/generic/src/proc/task.c
36,7 → 36,6
#include <panic.h>
#include <adt/list.h>
#include <ipc/ipc.h>
#include <ipc/ns.h>
#include <memstr.h>
 
SPINLOCK_INITIALIZE(tasks_lock);
76,8 → 75,8
 
ipc_answerbox_init(&ta->answerbox);
memsetb((__address)&ta->phones, sizeof(ta->phones[0])*IPC_MAX_PHONES, 0);
if (ipc_phone_0)
ipc_phone_init(&ta->phones[0], ipc_phone_0);
if (ipc_central_box)
ipc_phone_init(&ta->phones[0], ipc_central_box);
ipl = interrupts_disable();
spinlock_lock(&tasks_lock);
/kernel/trunk/generic/src/main/kinit.c
47,7 → 47,6
#include <interrupt.h>
#include <console/kconsole.h>
#include <elf.h>
#include <ipc/ns.h>
 
#ifdef CONFIG_SMP
#include <arch/smp/mps.h>
139,8 → 138,7
 
interrupts_enable();
 
/* Initialize name service */
ns_start();
ipc_create_phonecompany();
 
if (config.init_size > 0) {
/*
/kernel/trunk/generic/include/syscall/syscall.h
33,7 → 33,6
SYS_CTL = 0,
SYS_IO,
SYS_IPC_CALL_SYNC,
SYS_IPC_CALL_SYNC_MEDIUM,
SYS_IPC_CALL_ASYNC,
SYS_IPC_ANSWER,
SYS_IPC_WAIT,
/kernel/trunk/generic/include/ipc/ns.h
File deleted
/kernel/trunk/generic/include/ipc/ipc.h
29,46 → 29,17
#ifndef __IPC_H__
#define __IPC_H__
 
/* Length of data being transfered with IPC call */
/* - the uspace may not be able to utilize full length */
#define IPC_CALL_LEN 4
#define IPC_CALL_LEN 2
 
/* Flags for calls */
#define IPC_CALL_ANSWERED 1 /**< This is answer to a call */
#define IPC_CALL_STATIC_ALLOC 2 /**< This call will not be freed on error */
 
#define IPC_CALL_ANSWERED 1
/* Flags for ipc_wait_for_call */
#define IPC_WAIT_NONBLOCKING 1
#define IPC_WAIT_NONBLOCKING 1
 
/* Flags of callid */
#define IPC_CALLID_ANSWERED 1
 
/* Return values from IPC_ASYNC */
#define IPC_CALLRET_FATAL -1
#define IPC_CALLRET_TEMPORARY -2
 
 
/* Macros for manipulating calling data */
#define IPC_SET_RETVAL(data, retval) ((data)[0] = (retval))
#define IPC_SET_METHOD(data, val) ((data)[0] = (val))
#define IPC_SET_ARG1(data, val) ((data)[1] = (val))
#define IPC_SET_ARG2(data, val) ((data)[2] = (val))
#define IPC_SET_ARG3(data, val) ((data)[3] = (val))
 
#define IPC_GET_METHOD(data) ((data)[0])
#define IPC_GET_RETVAL(data) ((data)[0])
 
#define IPC_GET_ARG1(data) ((data)[1])
#define IPC_GET_ARG2(data) ((data)[2])
#define IPC_GET_ARG3(data) ((data)[3])
 
/* Well known phone descriptors */
#define PHONE_NS 0
 
#ifdef KERNEL
 
#include <synch/mutex.h>
#include <synch/condvar.h>
#include <synch/waitq.h>
#include <synch/spinlock.h>
#include <adt/list.h>
 
#define IPC_MAX_PHONES 16
86,8 → 57,7
struct answerbox {
SPINLOCK_DECLARE(lock);
 
mutex_t mutex;
condvar_t cv;
waitq_t wq;
 
link_t connected_phones; /**< Phones connected to this answerbox */
link_t calls; /**< Received calls */
102,6 → 72,7
answerbox_t *callee;
} phone_t;
 
extern void ipc_create_phonecompany(void);
extern void ipc_init(void);
extern call_t * ipc_wait_for_call(answerbox_t *box, int flags);
extern void ipc_answer(answerbox_t *box, call_t *request);
111,11 → 82,10
extern void ipc_phone_init(phone_t *phone, answerbox_t *box);
extern void ipc_call_free(call_t *call);
extern call_t * ipc_call_alloc(void);
extern void ipc_answerbox_init(answerbox_t *box);
extern void ipc_phone_init(phone_t *phone, answerbox_t *box);
extern void ipc_call_init(call_t *call);
void ipc_answerbox_init(answerbox_t *box);
void ipc_phone_init(phone_t *phone, answerbox_t *box);
 
extern answerbox_t *ipc_phone_0;
extern answerbox_t *ipc_central_box;
 
#endif
 
/kernel/trunk/generic/include/mm/page.h
67,7 → 67,7
memcpy(dst, src, cnt);
}
 
static inline void copy_from_uspace(void *dst, void *src, count_t cnt)
static inline void copy_to_kernel(void *dst, void *src, count_t cnt)
{
memcpy(dst, src, cnt);
}
/kernel/trunk/Makefile
134,8 → 134,7
generic/src/synch/semaphore.c \
generic/src/synch/waitq.c \
generic/src/smp/ipi.c \
generic/src/ipc/ipc.c \
generic/src/ipc/ns.c
generic/src/ipc/ipc.c
 
## Test sources
#