Subversion Repositories HelenOS

Rev

Rev 2098 | Rev 2471 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1027 palkovsky 1
/*
2071 jermar 2
 * Copyright (c) 2006 Ondrej Palkovsky
1027 palkovsky 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1757 jermar 29
/** @addtogroup genericipc
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1027 palkovsky 35
#include <arch.h>
36
#include <proc/task.h>
1288 jermar 37
#include <proc/thread.h>
1027 palkovsky 38
#include <errno.h>
39
#include <memstr.h>
40
#include <debug.h>
41
#include <ipc/ipc.h>
42
#include <ipc/sysipc.h>
1281 palkovsky 43
#include <ipc/irq.h>
1072 palkovsky 44
#include <ipc/ipcrsc.h>
1258 palkovsky 45
#include <arch/interrupt.h>
1027 palkovsky 46
#include <print.h>
1288 jermar 47
#include <syscall/copy.h>
1297 jermar 48
#include <security/cap.h>
1329 palkovsky 49
#include <mm/as.h>
1875 jermar 50
#include <print.h>
1027 palkovsky 51
 
2359 jermar 52
#define GET_CHECK_PHONE(phone, phoneid, err) { \
1084 palkovsky 53
      if (phoneid > IPC_MAX_PHONES) { err; } \
54
      phone = &TASK->phones[phoneid]; \
55
}
56
 
2359 jermar 57
#define STRUCT_TO_USPACE(dst, src) copy_to_uspace(dst, src, sizeof(*(src)))
1084 palkovsky 58
 
1040 palkovsky 59
/** Return true if the method is a system method */
1780 jermar 60
static inline int is_system_method(unative_t method)
1040 palkovsky 61
{
62
	if (method <= IPC_M_LAST_SYSTEM)
63
		return 1;
64
	return 0;
65
}
66
 
67
/** Return true if the message with this method is forwardable
68
 *
69
 * - some system messages may be forwarded, for some of them
70
 *   it is useless
71
 */
1780 jermar 72
static inline int is_forwardable(unative_t method)
1040 palkovsky 73
{
2359 jermar 74
	if (method == IPC_M_PHONE_HUNGUP || method == IPC_M_AS_AREA_SEND ||
75
	    method == IPC_M_AS_AREA_RECV)
1086 palkovsky 76
		return 0; /* This message is meant only for the receiver */
1040 palkovsky 77
	return 1;
78
}
79
 
1027 palkovsky 80
/****************************************************/
81
/* Functions that preprocess answer before sending 
82
 * it to the recepient
83
 */
84
 
85
/** Return true if the caller (ipc_answer) should save
1088 palkovsky 86
 * the old call contents for answer_preprocess
1027 palkovsky 87
 */
1088 palkovsky 88
static inline int answer_need_old(call_t *call)
1027 palkovsky 89
{
1086 palkovsky 90
	if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
1027 palkovsky 91
		return 1;
1086 palkovsky 92
	if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
1060 palkovsky 93
		return 1;
1359 jermar 94
	if (IPC_GET_METHOD(call->data) == IPC_M_AS_AREA_SEND)
1329 palkovsky 95
		return 1;
1428 palkovsky 96
	if (IPC_GET_METHOD(call->data) == IPC_M_AS_AREA_RECV)
97
		return 1;
1027 palkovsky 98
	return 0;
99
}
100
 
1428 palkovsky 101
/** Interpret process answer as control information
102
 *
103
 * This function is called directly after sys_ipc_answer 
104
 */
1329 palkovsky 105
static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
1027 palkovsky 106
{
107
	int phoneid;
108
 
1088 palkovsky 109
	if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
1141 palkovsky 110
		/* In case of forward, hangup the forwared phone,
111
		 * not the originator
112
		 */
113
		spinlock_lock(&answer->data.phone->lock);
114
		spinlock_lock(&TASK->answerbox.lock);
1568 palkovsky 115
		if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
1573 palkovsky 116
			list_remove(&answer->data.phone->link);
1568 palkovsky 117
			answer->data.phone->state = IPC_PHONE_SLAMMED;
1141 palkovsky 118
		}
119
		spinlock_unlock(&TASK->answerbox.lock);
120
		spinlock_unlock(&answer->data.phone->lock);
1088 palkovsky 121
	}
122
 
123
	if (!olddata)
1329 palkovsky 124
		return 0;
1088 palkovsky 125
 
1086 palkovsky 126
	if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 127
		phoneid = IPC_GET_ARG3(*olddata);
1027 palkovsky 128
		if (IPC_GET_RETVAL(answer->data)) {
129
			/* The connection was not accepted */
130
			phone_dealloc(phoneid);
1040 palkovsky 131
		} else {
1090 palkovsky 132
			/* The connection was accepted */
2359 jermar 133
			phone_connect(phoneid, &answer->sender->answerbox);
1090 palkovsky 134
			/* Set 'phone identification' as arg3 of response */
2359 jermar 135
			IPC_SET_ARG3(answer->data,
136
			    (unative_t) &TASK->phones[phoneid]);
1027 palkovsky 137
		}
1086 palkovsky 138
	} else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
1060 palkovsky 139
		/* If the users accepted call, connect */
140
		if (!IPC_GET_RETVAL(answer->data)) {
2359 jermar 141
			ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
142
			    &TASK->answerbox);
1060 palkovsky 143
		}
1359 jermar 144
	} else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
2359 jermar 145
		if (!IPC_GET_RETVAL(answer->data)) {
146
			/* Accepted, handle as_area receipt */
1413 jermar 147
			ipl_t ipl;
1461 palkovsky 148
			int rc;
1413 jermar 149
			as_t *as;
150
 
151
			ipl = interrupts_disable();
152
			spinlock_lock(&answer->sender->lock);
153
			as = answer->sender->as;
154
			spinlock_unlock(&answer->sender->lock);
155
			interrupts_restore(ipl);
156
 
2359 jermar 157
			rc = as_area_share(as, IPC_GET_ARG1(*olddata),
158
			    IPC_GET_ARG2(*olddata), AS,
159
			    IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
1461 palkovsky 160
			IPC_SET_RETVAL(answer->data, rc);
161
			return rc;
1329 palkovsky 162
		}
1428 palkovsky 163
	} else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
164
		if (!IPC_GET_RETVAL(answer->data)) { 
165
			ipl_t ipl;
166
			as_t *as;
1434 palkovsky 167
			int rc;
1428 palkovsky 168
 
169
			ipl = interrupts_disable();
170
			spinlock_lock(&answer->sender->lock);
171
			as = answer->sender->as;
172
			spinlock_unlock(&answer->sender->lock);
173
			interrupts_restore(ipl);
174
 
2359 jermar 175
			rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
176
			    IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
177
			    IPC_GET_ARG2(answer->data));
1434 palkovsky 178
			IPC_SET_RETVAL(answer->data, rc);
1428 palkovsky 179
		}
1027 palkovsky 180
	}
1329 palkovsky 181
	return 0;
1027 palkovsky 182
}
183
 
1090 palkovsky 184
/** Called before the request is sent
185
 *
186
 * @return 0 - no error, -1 - report error to user
187
 */
188
static int request_preprocess(call_t *call)
189
{
190
	int newphid;
1329 palkovsky 191
	size_t size;
1090 palkovsky 192
 
193
	switch (IPC_GET_METHOD(call->data)) {
194
	case IPC_M_CONNECT_ME_TO:
195
		newphid = phone_alloc();
196
		if (newphid < 0)
197
			return ELIMIT;
198
		/* Set arg3 for server */
2359 jermar 199
		IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
1090 palkovsky 200
		call->flags |= IPC_CALL_CONN_ME_TO;
2098 decky 201
		call->priv = newphid;
1090 palkovsky 202
		break;
1359 jermar 203
	case IPC_M_AS_AREA_SEND:
1417 jermar 204
		size = as_get_size(IPC_GET_ARG1(call->data));
1329 palkovsky 205
		if (!size) {
206
			return EPERM;
207
		}
1417 jermar 208
		IPC_SET_ARG2(call->data, size);
1329 palkovsky 209
		break;
1090 palkovsky 210
	default:
211
		break;
212
	}
213
	return 0;
214
}
215
 
1027 palkovsky 216
/****************************************************/
217
/* Functions called to process received call/answer 
218
 * before passing to uspace
219
 */
220
 
221
/** Do basic kernel processing of received call answer */
1090 palkovsky 222
static void process_answer(call_t *call)
1027 palkovsky 223
{
2359 jermar 224
	if (IPC_GET_RETVAL(call->data) == EHANGUP &&
225
	    (call->flags & IPC_CALL_FORWARDED))
1088 palkovsky 226
		IPC_SET_RETVAL(call->data, EFORWARD);
1090 palkovsky 227
 
228
	if (call->flags & IPC_CALL_CONN_ME_TO) {
229
		if (IPC_GET_RETVAL(call->data))
2098 decky 230
			phone_dealloc(call->priv);
1090 palkovsky 231
		else
2098 decky 232
			IPC_SET_ARG3(call->data, call->priv);
1090 palkovsky 233
	}
1027 palkovsky 234
}
235
 
236
/** Do basic kernel processing of received call request
237
 *
238
 * @return 0 - the call should be passed to userspace, 1 - ignore call
239
 */
2359 jermar 240
static int process_request(answerbox_t *box, call_t *call)
1027 palkovsky 241
{
242
	int phoneid;
243
 
1086 palkovsky 244
	if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
1040 palkovsky 245
		phoneid = phone_alloc();
1027 palkovsky 246
		if (phoneid < 0) { /* Failed to allocate phone */
247
			IPC_SET_RETVAL(call->data, ELIMIT);
248
			ipc_answer(box,call);
249
			return -1;
250
		}
251
		IPC_SET_ARG3(call->data, phoneid);
252
	} 
253
	return 0;
254
}
255
 
256
/** Send a call over IPC, wait for reply, return to user
257
 *
258
 * @return Call identification, returns -1 on fatal error, 
259
           -2 on 'Too many async request, handle answers first
260
 */
2359 jermar 261
unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
262
    unative_t arg1, ipc_data_t *data)
1027 palkovsky 263
{
264
	call_t call;
265
	phone_t *phone;
1090 palkovsky 266
	int res;
1027 palkovsky 267
 
1084 palkovsky 268
	GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1027 palkovsky 269
 
1084 palkovsky 270
	ipc_call_static_init(&call);
1027 palkovsky 271
	IPC_SET_METHOD(call.data, method);
272
	IPC_SET_ARG1(call.data, arg1);
273
 
1090 palkovsky 274
	if (!(res=request_preprocess(&call))) {
275
		ipc_call_sync(phone, &call);
276
		process_answer(&call);
277
	} else 
278
		IPC_SET_RETVAL(call.data, res);
1086 palkovsky 279
	STRUCT_TO_USPACE(&data->args, &call.data.args);
1027 palkovsky 280
 
281
	return 0;
282
}
283
 
284
/** Synchronous IPC call allowing to send whole message */
2359 jermar 285
unative_t sys_ipc_call_sync(unative_t phoneid, ipc_data_t *question,
286
    ipc_data_t *reply)
1027 palkovsky 287
{
288
	call_t call;
289
	phone_t *phone;
1090 palkovsky 290
	int res;
1288 jermar 291
	int rc;
1027 palkovsky 292
 
1084 palkovsky 293
	ipc_call_static_init(&call);
2359 jermar 294
	rc = copy_from_uspace(&call.data.args, &question->args,
295
	    sizeof(call.data.args));
1288 jermar 296
	if (rc != 0)
1780 jermar 297
		return (unative_t) rc;
1040 palkovsky 298
 
1084 palkovsky 299
	GET_CHECK_PHONE(phone, phoneid, return ENOENT);
1072 palkovsky 300
 
1090 palkovsky 301
	if (!(res=request_preprocess(&call))) {
302
		ipc_call_sync(phone, &call);
303
		process_answer(&call);
304
	} else 
305
		IPC_SET_RETVAL(call.data, res);
1027 palkovsky 306
 
1288 jermar 307
	rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
308
	if (rc != 0)
309
		return rc;
1027 palkovsky 310
 
311
	return 0;
312
}
313
 
314
/** Check that the task did not exceed allowed limit
315
 *
316
 * @return 0 - Limit OK,   -1 - limit exceeded
317
 */
318
static int check_call_limit(void)
319
{
320
	if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
321
		atomic_dec(&TASK->active_calls);
322
		return -1;
323
	}
324
	return 0;
325
}
326
 
327
/** Send an asynchronous call over ipc
328
 *
329
 * @return Call identification, returns -1 on fatal error, 
330
           -2 on 'Too many async request, handle answers first
331
 */
2359 jermar 332
unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
333
    unative_t arg1, unative_t arg2)
1027 palkovsky 334
{
335
	call_t *call;
336
	phone_t *phone;
1090 palkovsky 337
	int res;
1027 palkovsky 338
 
339
	if (check_call_limit())
340
		return IPC_CALLRET_TEMPORARY;
341
 
1090 palkovsky 342
	GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1072 palkovsky 343
 
1258 palkovsky 344
	call = ipc_call_alloc(0);
1027 palkovsky 345
	IPC_SET_METHOD(call->data, method);
346
	IPC_SET_ARG1(call->data, arg1);
347
	IPC_SET_ARG2(call->data, arg2);
1669 palkovsky 348
	IPC_SET_ARG3(call->data, 0);
1027 palkovsky 349
 
2359 jermar 350
	if (!(res = request_preprocess(call)))
1090 palkovsky 351
		ipc_call(phone, call);
352
	else
353
		ipc_backsend_err(phone, call, res);
1027 palkovsky 354
 
1780 jermar 355
	return (unative_t) call;
1027 palkovsky 356
}
357
 
358
/** Synchronous IPC call allowing to send whole message
359
 *
360
 * @return The same as sys_ipc_call_async
361
 */
1780 jermar 362
unative_t sys_ipc_call_async(unative_t phoneid, ipc_data_t *data)
1027 palkovsky 363
{
364
	call_t *call;
365
	phone_t *phone;
1090 palkovsky 366
	int res;
1288 jermar 367
	int rc;
1027 palkovsky 368
 
1072 palkovsky 369
	if (check_call_limit())
370
		return IPC_CALLRET_TEMPORARY;
371
 
1090 palkovsky 372
	GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
1027 palkovsky 373
 
1258 palkovsky 374
	call = ipc_call_alloc(0);
2359 jermar 375
	rc = copy_from_uspace(&call->data.args, &data->args,
376
	    sizeof(call->data.args));
1293 palkovsky 377
	if (rc != 0) {
378
		ipc_call_free(call);
1780 jermar 379
		return (unative_t) rc;
1293 palkovsky 380
	}
2359 jermar 381
	if (!(res = request_preprocess(call)))
1090 palkovsky 382
		ipc_call(phone, call);
383
	else
384
		ipc_backsend_err(phone, call, res);
1040 palkovsky 385
 
1780 jermar 386
	return (unative_t) call;
1027 palkovsky 387
}
388
 
1040 palkovsky 389
/** Forward received call to another destination
390
 *
391
 * The arg1 and arg2 are changed in the forwarded message
1060 palkovsky 392
 *
393
 * Warning: If implementing non-fast version, make sure that
394
 *          arg3 is not rewritten for certain system IPC
1040 palkovsky 395
 */
1780 jermar 396
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
2359 jermar 397
    unative_t method, unative_t arg1)
1040 palkovsky 398
{
399
	call_t *call;
400
	phone_t *phone;
401
 
402
	call = get_call(callid);
403
	if (!call)
404
		return ENOENT;
405
 
1088 palkovsky 406
	call->flags |= IPC_CALL_FORWARDED;
407
 
1084 palkovsky 408
	GET_CHECK_PHONE(phone, phoneid, { 
1040 palkovsky 409
		IPC_SET_RETVAL(call->data, EFORWARD);
410
		ipc_answer(&TASK->answerbox, call);
411
		return ENOENT;
1084 palkovsky 412
	});		
1040 palkovsky 413
 
414
	if (!is_forwardable(IPC_GET_METHOD(call->data))) {
415
		IPC_SET_RETVAL(call->data, EFORWARD);
416
		ipc_answer(&TASK->answerbox, call);
417
		return EPERM;
418
	}
419
 
420
	/* Userspace is not allowed to change method of system methods
421
	 * on forward, allow changing ARG1 and ARG2 by means of method and arg1
422
	 */
423
	if (is_system_method(IPC_GET_METHOD(call->data))) {
1090 palkovsky 424
		if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
425
			phone_dealloc(IPC_GET_ARG3(call->data));
426
 
1040 palkovsky 427
		IPC_SET_ARG1(call->data, method);
428
		IPC_SET_ARG2(call->data, arg1);
429
	} else {
430
		IPC_SET_METHOD(call->data, method);
431
		IPC_SET_ARG1(call->data, arg1);
432
	}
433
 
1088 palkovsky 434
	return ipc_forward(call, phone, &TASK->answerbox);
1040 palkovsky 435
}
436
 
1027 palkovsky 437
/** Send IPC answer */
2359 jermar 438
unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
439
    unative_t arg1, unative_t arg2)
1027 palkovsky 440
{
441
	call_t *call;
1040 palkovsky 442
	ipc_data_t saved_data;
1088 palkovsky 443
	int saveddata = 0;
1329 palkovsky 444
	int rc;
1027 palkovsky 445
 
1346 palkovsky 446
	/* Do not answer notification callids */
447
	if (callid & IPC_CALLID_NOTIFICATION)
448
		return 0;
449
 
1040 palkovsky 450
	call = get_call(callid);
451
	if (!call)
452
		return ENOENT;
1027 palkovsky 453
 
1088 palkovsky 454
	if (answer_need_old(call)) {
1040 palkovsky 455
		memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 456
		saveddata = 1;
1027 palkovsky 457
	}
458
 
459
	IPC_SET_RETVAL(call->data, retval);
460
	IPC_SET_ARG1(call->data, arg1);
461
	IPC_SET_ARG2(call->data, arg2);
1329 palkovsky 462
	rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1040 palkovsky 463
 
1027 palkovsky 464
	ipc_answer(&TASK->answerbox, call);
1329 palkovsky 465
	return rc;
1027 palkovsky 466
}
467
 
468
/** Send IPC answer */
1780 jermar 469
unative_t sys_ipc_answer(unative_t callid, ipc_data_t *data)
1027 palkovsky 470
{
471
	call_t *call;
1040 palkovsky 472
	ipc_data_t saved_data;
1088 palkovsky 473
	int saveddata = 0;
1288 jermar 474
	int rc;
1027 palkovsky 475
 
1346 palkovsky 476
	/* Do not answer notification callids */
477
	if (callid & IPC_CALLID_NOTIFICATION)
478
		return 0;
479
 
1040 palkovsky 480
	call = get_call(callid);
481
	if (!call)
482
		return ENOENT;
1027 palkovsky 483
 
1088 palkovsky 484
	if (answer_need_old(call)) {
1040 palkovsky 485
		memcpy(&saved_data, &call->data, sizeof(call->data));
1088 palkovsky 486
		saveddata = 1;
1027 palkovsky 487
	}
1288 jermar 488
	rc = copy_from_uspace(&call->data.args, &data->args, 
2359 jermar 489
	    sizeof(call->data.args));
1288 jermar 490
	if (rc != 0)
491
		return rc;
1027 palkovsky 492
 
1329 palkovsky 493
	rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
1027 palkovsky 494
 
495
	ipc_answer(&TASK->answerbox, call);
496
 
1329 palkovsky 497
	return rc;
1027 palkovsky 498
}
499
 
1086 palkovsky 500
/** Hang up the phone
501
 *
502
 */
1780 jermar 503
unative_t sys_ipc_hangup(int phoneid)
1086 palkovsky 504
{
505
	phone_t *phone;
506
 
507
	GET_CHECK_PHONE(phone, phoneid, return ENOENT);
508
 
1591 palkovsky 509
	if (ipc_phone_hangup(phone))
1086 palkovsky 510
		return -1;
511
 
512
	return 0;
513
}
514
 
1027 palkovsky 515
/** Wait for incoming ipc call or answer
516
 *
2359 jermar 517
 * @param calldata	Pointer to buffer where the call/answer data is stored.
518
 * @param usec 		Timeout. See waitq_sleep_timeout() for explanation.
519
 * @param flags		Select mode of sleep operation. See waitq_sleep_timeout()
520
 *			for explanation.
1364 jermar 521
 *
1027 palkovsky 522
 * @return Callid, if callid & 1, then the call is answer
523
 */
1780 jermar 524
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
1027 palkovsky 525
{
526
	call_t *call;
527
 
528
restart:	
2359 jermar 529
	call = ipc_wait_for_call(&TASK->answerbox, usec,
530
	    flags | SYNCH_FLAGS_INTERRUPTIBLE);
1088 palkovsky 531
	if (!call)
532
		return 0;
1027 palkovsky 533
 
1258 palkovsky 534
	if (call->flags & IPC_CALL_NOTIF) {
535
		ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
1693 palkovsky 536
 
537
		/* Set in_phone_hash to the interrupt counter */
2098 decky 538
		call->data.phone = (void *) call->priv;
1693 palkovsky 539
 
540
		STRUCT_TO_USPACE(calldata, &call->data);
541
 
1258 palkovsky 542
		ipc_call_free(call);
543
 
1780 jermar 544
		return ((unative_t)call) | IPC_CALLID_NOTIFICATION;
1258 palkovsky 545
	}
546
 
1027 palkovsky 547
	if (call->flags & IPC_CALL_ANSWERED) {
1090 palkovsky 548
		process_answer(call);
1027 palkovsky 549
 
1086 palkovsky 550
		ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
551
 
1027 palkovsky 552
		atomic_dec(&TASK->active_calls);
1086 palkovsky 553
 
554
		if (call->flags & IPC_CALL_DISCARD_ANSWER) {
555
			ipc_call_free(call);
556
			goto restart;
557
		}
558
 
559
		STRUCT_TO_USPACE(&calldata->args, &call->data.args);
1027 palkovsky 560
		ipc_call_free(call);
561
 
1780 jermar 562
		return ((unative_t)call) | IPC_CALLID_ANSWERED;
1027 palkovsky 563
	}
1086 palkovsky 564
 
1027 palkovsky 565
	if (process_request(&TASK->answerbox, call))
566
		goto restart;
1086 palkovsky 567
 
1090 palkovsky 568
	/* Include phone address('id') of the caller in the request,
569
	 * copy whole call->data, not only call->data.args */
1396 palkovsky 570
	if (STRUCT_TO_USPACE(calldata, &call->data)) {
571
		return 0;
572
	}
1780 jermar 573
	return (unative_t)call;
1027 palkovsky 574
}
1258 palkovsky 575
 
1923 jermar 576
/** Connect irq handler to task.
577
 *
578
 * @param inr IRQ number.
579
 * @param devno Device number.
580
 * @param method Method to be associated with the notification.
581
 * @param ucode Uspace pointer to the top-half pseudocode.
582
 *
583
 * @return EPERM or a return code returned by ipc_irq_register().
584
 */
2359 jermar 585
unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
586
    irq_code_t *ucode)
1258 palkovsky 587
{
1297 jermar 588
	if (!(cap_get(TASK) & CAP_IRQ_REG))
589
		return EPERM;
590
 
1923 jermar 591
	return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
1258 palkovsky 592
}
593
 
1923 jermar 594
/** Disconnect irq handler from task.
595
 *
596
 * @param inr IRQ number.
597
 * @param devno Device number.
598
 */
599
unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
1258 palkovsky 600
{
1297 jermar 601
	if (!(cap_get(TASK) & CAP_IRQ_REG))
602
		return EPERM;
603
 
1923 jermar 604
	ipc_irq_unregister(&TASK->answerbox, inr, devno);
1258 palkovsky 605
 
606
	return 0;
607
}
1702 cejka 608
 
1757 jermar 609
/** @}
1702 cejka 610
 */