source: mainline/kernel/generic/src/ipc/ipcrsc.c@ 7c3fb9b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c3fb9b was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[4e49572]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[4e49572]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
[cc73a8a1]29/** @addtogroup genericipc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[7c3fb9b]35/*
36 * IPC resources management
[4e49572]37 *
[51ec40f]38 * The goal of this source code is to properly manage IPC resources and allow
39 * straight and clean clean-up procedure upon task termination.
[4e49572]40 *
41 * The pattern of usage of the resources is:
[aadde10]42 * - allocate a capability and phone kernel object (do not publish yet),
43 * connect to the answerbox, and finally publish the capability
[4e49572]44 * - disconnect connected phone (some messages might be on the fly)
[aadde10]45 * - find phone capability and send a message using phone
[4e49572]46 * - answer message to phone
[fbcfd458]47 * - hangup phone (the caller has hung up)
48 * - hangup phone (the answerbox is exiting)
[da1bafb]49 *
[ba81cab]50 * Locking strategy
51 *
[51ec40f]52 * - To use a phone, disconnect a phone etc., the phone must be first locked and
53 * then checked that it is connected
54 * - To connect an allocated phone it need not be locked (assigning pointer is
55 * atomic on all platforms)
[ba81cab]56 *
57 * - To answer a message, the answerbox must be locked
58 * - The locking of phone and answerbox is done at the ipc_ level.
[51ec40f]59 * It is perfectly correct to pass unconnected phone to these functions and
60 * proper reply will be generated.
[ba81cab]61 *
62 * Locking order
63 *
64 * - first phone, then answerbox
65 * + Easy locking on calls
[51ec40f]66 * - Very hard traversing list of phones when disconnecting because the phones
67 * may disconnect during traversal of list of connected phones. The only
68 * possibility is try_lock with restart of list traversal.
[ba81cab]69 *
[fbcfd458]70 * Destroying is less frequent, this approach is taken.
71 *
[7c7aae16]72 * Phone call
73 *
74 * *** Connect_me_to ***
[51ec40f]75 * The caller sends IPC_M_CONNECT_ME_TO to an answerbox. The server receives
[aadde10]76 * 'phoneid' of the connecting phone as an ARG5. If it answers with RETVAL=EOK,
77 * the phone call is accepted, otherwise it is refused.
[7c7aae16]78 *
79 * *** Connect_to_me ***
[30c27e9]80 * The caller sends IPC_M_CONNECT_TO_ME.
[51ec40f]81 * The server receives an automatically opened phoneid. If it accepts
[aadde10]82 * (RETVAL=EOK), it can use the phoneid immediately. Possible race condition can
[30c27e9]83 * arise, when the client receives messages from new connection before getting
84 * response for connect_to_me message. Userspace should implement handshake
85 * protocol that would control it.
[7c7aae16]86 *
[fbcfd458]87 * Phone hangup
[da1bafb]88 *
[fbcfd458]89 * *** The caller hangs up (sys_ipc_hangup) ***
90 * - The phone is disconnected (no more messages can be sent over this phone),
[51ec40f]91 * all in-progress messages are correctly handled. The answerbox receives
[30c27e9]92 * IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async calls
93 * are answered, the phone is deallocated.
[ba81cab]94 *
[9f22213]95 * *** The answerbox hangs up (ipc_answer(EHANGUP))
[30c27e9]96 * - The phone is disconnected. EHANGUP response code is sent to the calling
97 * task. All new calls through this phone get a EHUNGUP error code, the task
[aadde10]98 * is expected to call sys_ipc_hangup after cleaning up its internal
[30c27e9]99 * structures.
100 *
[ba81cab]101 *
[9f22213]102 * Call forwarding
[da1bafb]103 *
[30c27e9]104 * The call can be forwarded, so that the answer to call is passed directly to
105 * the original sender. However, this poses special problems regarding routing
106 * of hangup messages.
[9f22213]107 *
108 * sys_ipc_hangup -> IPC_M_PHONE_HUNGUP
109 * - this message CANNOT be forwarded
110 *
111 * EHANGUP during forward
112 * - The *forwarding* phone will be closed, EFORWARD is sent to receiver.
113 *
114 * EHANGUP, ENOENT during forward
115 * - EFORWARD is sent to the receiver, ipc_forward returns error code EFORWARD
116 *
[ba81cab]117 * Cleanup strategy
[da1bafb]118 *
[9f22213]119 * 1) Disconnect all our phones ('ipc_phone_hangup').
[fbcfd458]120 *
121 * 2) Disconnect all phones connected to answerbox.
[ba81cab]122 *
[fbcfd458]123 * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with
[9f22213]124 * appropriate error code (EHANGUP, EFORWARD).
[ba81cab]125 *
[7c7aae16]126 * 4) Wait for all async answers to arrive and dispose of them.
[da1bafb]127 *
[4e49572]128 */
129
130#include <synch/spinlock.h>
131#include <ipc/ipc.h>
132#include <arch.h>
133#include <proc/task.h>
134#include <ipc/ipcrsc.h>
[63e27ef]135#include <assert.h>
[e8039a86]136#include <abi/errno.h>
[3f74275]137#include <cap/cap.h>
[63d8f43]138#include <mm/slab.h>
[4e49572]139
[48bcf49]140static void phone_destroy(void *arg)
[05ffb41]141{
[48bcf49]142 phone_t *phone = (phone_t *) arg;
[82d515e9]143 slab_free(phone_cache, phone);
[48bcf49]144}
[05ffb41]145
[48bcf49]146static kobject_ops_t phone_kobject_ops = {
147 .destroy = phone_destroy
148};
[63d8f43]149
[4e49572]150
[05ffb41]151/** Allocate new phone in the specified task.
[8b243f2]152 *
[334c103]153 * @param[in] task Task for which to allocate a new phone.
154 * @param[in] publish If true, the new capability will be published.
155 * @param[out] phandle New phone capability handle.
156 * @param[out] kobject New phone kobject.
[09d01f2]157 *
[cde999a]158 * @return An error code if a new capability cannot be allocated.
[8b243f2]159 */
[eadaeae8]160errno_t phone_alloc(task_t *task, bool publish, cap_phone_handle_t *phandle,
[334c103]161 kobject_t **kobject)
[7c7aae16]162{
[09d01f2]163 cap_handle_t handle;
[b7fd2a0]164 errno_t rc = cap_alloc(task, &handle);
[09d01f2]165 if (rc == EOK) {
[82d515e9]166 phone_t *phone = slab_alloc(phone_cache, FRAME_ATOMIC);
[63d8f43]167 if (!phone) {
168 cap_free(TASK, handle);
169 return ENOMEM;
170 }
[11b285d]171 kobject_t *kobj = malloc(sizeof(kobject_t));
[334c103]172 if (!kobj) {
[48bcf49]173 cap_free(TASK, handle);
[82d515e9]174 slab_free(phone_cache, phone);
[48bcf49]175 return ENOMEM;
176 }
177
[63d8f43]178 ipc_phone_init(phone, task);
179 phone->state = IPC_PHONE_CONNECTING;
[9e87562]180
[334c103]181 kobject_initialize(kobj, KOBJECT_TYPE_PHONE, phone,
[48bcf49]182 &phone_kobject_ops);
[334c103]183 phone->kobject = kobj;
[a35b458]184
[334c103]185 if (publish)
186 cap_publish(task, handle, kobj);
[09d01f2]187
[334c103]188 *phandle = handle;
189 if (kobject)
190 *kobject = kobj;
[05ffb41]191 }
[09d01f2]192 return rc;
[7c7aae16]193}
194
[8b243f2]195/** Free slot from a disconnected phone.
196 *
197 * All already sent messages will be correctly processed.
[fbcfd458]198 *
[3f74275]199 * @param handle Phone capability handle of the phone to be freed.
[da1bafb]200 *
[fbcfd458]201 */
[eadaeae8]202void phone_dealloc(cap_phone_handle_t handle)
[4e49572]203{
[48bcf49]204 kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
205 if (!kobj)
206 return;
[a35b458]207
[48bcf49]208 kobject_put(kobj);
[3f74275]209 cap_free(TASK, handle);
[4e49572]210}
211
[cc73a8a1]212/** @}
[b45c443]213 */
Note: See TracBrowser for help on using the repository browser.