| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej 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 |
|
|---|
| 29 | /** @addtogroup kernel_generic_ipc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | /*
|
|---|
| 36 | * IPC resources management
|
|---|
| 37 | *
|
|---|
| 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.
|
|---|
| 40 | *
|
|---|
| 41 | * The pattern of usage of the resources is:
|
|---|
| 42 | * - allocate a capability and phone kernel object (do not publish yet),
|
|---|
| 43 | * connect to the answerbox, and finally publish the capability
|
|---|
| 44 | * - disconnect connected phone (some messages might be on the fly)
|
|---|
| 45 | * - find phone capability and send a message using phone
|
|---|
| 46 | * - answer message to phone
|
|---|
| 47 | * - hangup phone (the caller has hung up)
|
|---|
| 48 | * - hangup phone (the answerbox is exiting)
|
|---|
| 49 | *
|
|---|
| 50 | * Locking strategy
|
|---|
| 51 | *
|
|---|
| 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)
|
|---|
| 56 | *
|
|---|
| 57 | * - To answer a message, the answerbox must be locked
|
|---|
| 58 | * - The locking of phone and answerbox is done at the ipc_ level.
|
|---|
| 59 | * It is perfectly correct to pass unconnected phone to these functions and
|
|---|
| 60 | * proper reply will be generated.
|
|---|
| 61 | *
|
|---|
| 62 | * Locking order
|
|---|
| 63 | *
|
|---|
| 64 | * - first phone, then answerbox
|
|---|
| 65 | * + Easy locking on calls
|
|---|
| 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.
|
|---|
| 69 | *
|
|---|
| 70 | * Destroying is less frequent, this approach is taken.
|
|---|
| 71 | *
|
|---|
| 72 | * Phone call
|
|---|
| 73 | *
|
|---|
| 74 | * *** Connect_me_to ***
|
|---|
| 75 | * The caller sends IPC_M_CONNECT_ME_TO to an answerbox. The server receives
|
|---|
| 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.
|
|---|
| 78 | *
|
|---|
| 79 | * *** Connect_to_me ***
|
|---|
| 80 | * The caller sends IPC_M_CONNECT_TO_ME.
|
|---|
| 81 | * The server receives an automatically opened phoneid. If it accepts
|
|---|
| 82 | * (RETVAL=EOK), it can use the phoneid immediately. Possible race condition can
|
|---|
| 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.
|
|---|
| 86 | *
|
|---|
| 87 | * Phone hangup
|
|---|
| 88 | *
|
|---|
| 89 | * *** The caller hangs up (sys_ipc_hangup) ***
|
|---|
| 90 | * - The phone is disconnected (no more messages can be sent over this phone),
|
|---|
| 91 | * all in-progress messages are correctly handled. The answerbox receives
|
|---|
| 92 | * IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async calls
|
|---|
| 93 | * are answered, the phone is deallocated.
|
|---|
| 94 | *
|
|---|
| 95 | * *** The answerbox hangs up (ipc_answer(EHANGUP))
|
|---|
| 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
|
|---|
| 98 | * is expected to call sys_ipc_hangup after cleaning up its internal
|
|---|
| 99 | * structures.
|
|---|
| 100 | *
|
|---|
| 101 | *
|
|---|
| 102 | * Call forwarding
|
|---|
| 103 | *
|
|---|
| 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.
|
|---|
| 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 | *
|
|---|
| 117 | * Cleanup strategy
|
|---|
| 118 | *
|
|---|
| 119 | * 1) Disconnect all our phones ('ipc_phone_hangup').
|
|---|
| 120 | *
|
|---|
| 121 | * 2) Disconnect all phones connected to answerbox.
|
|---|
| 122 | *
|
|---|
| 123 | * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with
|
|---|
| 124 | * appropriate error code (EHANGUP, EFORWARD).
|
|---|
| 125 | *
|
|---|
| 126 | * 4) Wait for all async answers to arrive and dispose of them.
|
|---|
| 127 | *
|
|---|
| 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>
|
|---|
| 135 | #include <assert.h>
|
|---|
| 136 | #include <abi/errno.h>
|
|---|
| 137 | #include <cap/cap.h>
|
|---|
| 138 | #include <mm/slab.h>
|
|---|
| 139 |
|
|---|
| 140 | static void phone_destroy(void *arg)
|
|---|
| 141 | {
|
|---|
| 142 | phone_t *phone = (phone_t *) arg;
|
|---|
| 143 | slab_free(phone_cache, phone);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static kobject_ops_t phone_kobject_ops = {
|
|---|
| 147 | .destroy = phone_destroy
|
|---|
| 148 | };
|
|---|
| 149 |
|
|---|
| 150 | /** Allocate new phone in the specified task.
|
|---|
| 151 | *
|
|---|
| 152 | * @param[in] task Task for which to allocate a new phone.
|
|---|
| 153 | * @param[in] publish If true, the new capability will be published.
|
|---|
| 154 | * @param[out] phandle New phone capability handle.
|
|---|
| 155 | * @param[out] kobject New phone kobject.
|
|---|
| 156 | *
|
|---|
| 157 | * @return An error code if a new capability cannot be allocated.
|
|---|
| 158 | */
|
|---|
| 159 | errno_t phone_alloc(task_t *task, bool publish, cap_phone_handle_t *phandle,
|
|---|
| 160 | kobject_t **kobject)
|
|---|
| 161 | {
|
|---|
| 162 | cap_handle_t handle;
|
|---|
| 163 | errno_t rc = cap_alloc(task, &handle);
|
|---|
| 164 | if (rc == EOK) {
|
|---|
| 165 | phone_t *phone = slab_alloc(phone_cache, FRAME_ATOMIC);
|
|---|
| 166 | if (!phone) {
|
|---|
| 167 | cap_free(TASK, handle);
|
|---|
| 168 | return ENOMEM;
|
|---|
| 169 | }
|
|---|
| 170 | kobject_t *kobj = malloc(sizeof(kobject_t));
|
|---|
| 171 | if (!kobj) {
|
|---|
| 172 | cap_free(TASK, handle);
|
|---|
| 173 | slab_free(phone_cache, phone);
|
|---|
| 174 | return ENOMEM;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | ipc_phone_init(phone, task);
|
|---|
| 178 | phone->state = IPC_PHONE_CONNECTING;
|
|---|
| 179 |
|
|---|
| 180 | kobject_initialize(kobj, KOBJECT_TYPE_PHONE, phone,
|
|---|
| 181 | &phone_kobject_ops);
|
|---|
| 182 | phone->kobject = kobj;
|
|---|
| 183 |
|
|---|
| 184 | if (publish)
|
|---|
| 185 | cap_publish(task, handle, kobj);
|
|---|
| 186 |
|
|---|
| 187 | *phandle = handle;
|
|---|
| 188 | if (kobject)
|
|---|
| 189 | *kobject = kobj;
|
|---|
| 190 | }
|
|---|
| 191 | return rc;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /** Free slot from a disconnected phone.
|
|---|
| 195 | *
|
|---|
| 196 | * All already sent messages will be correctly processed.
|
|---|
| 197 | *
|
|---|
| 198 | * @param handle Phone capability handle of the phone to be freed.
|
|---|
| 199 | *
|
|---|
| 200 | */
|
|---|
| 201 | void phone_dealloc(cap_phone_handle_t handle)
|
|---|
| 202 | {
|
|---|
| 203 | kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
|
|---|
| 204 | if (!kobj)
|
|---|
| 205 | return;
|
|---|
| 206 |
|
|---|
| 207 | kobject_put(kobj);
|
|---|
| 208 | cap_free(TASK, handle);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /** @}
|
|---|
| 212 | */
|
|---|