| [6d9c49a] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| [6d9c49a] | 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 |
|
|---|
| [06e1e95] | 29 | /** @addtogroup genericipc
|
|---|
| [b45c443] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [27ab6a7] | 35 | #ifndef KERN_IPC_H_
|
|---|
| 36 | #define KERN_IPC_H_
|
|---|
| [6d9c49a] | 37 |
|
|---|
| [ff48a15] | 38 | #include <synch/spinlock.h>
|
|---|
| 39 | #include <synch/mutex.h>
|
|---|
| [fa8e7d2] | 40 | #include <synch/waitq.h>
|
|---|
| [9a426d1f] | 41 | #include <abi/ipc/ipc.h>
|
|---|
| 42 | #include <abi/proc/task.h>
|
|---|
| [e2ab36f1] | 43 | #include <typedefs.h>
|
|---|
| [e5f5ce0] | 44 | #include <mm/slab.h>
|
|---|
| [48bcf49] | 45 | #include <cap/cap.h>
|
|---|
| [fa8e7d2] | 46 |
|
|---|
| 47 | struct answerbox;
|
|---|
| 48 | struct task;
|
|---|
| 49 |
|
|---|
| 50 | typedef enum {
|
|---|
| [80bcaed] | 51 | /** Phone is free and can be allocated */
|
|---|
| 52 | IPC_PHONE_FREE = 0,
|
|---|
| 53 | /** Phone is connecting somewhere */
|
|---|
| 54 | IPC_PHONE_CONNECTING,
|
|---|
| 55 | /** Phone is connected */
|
|---|
| 56 | IPC_PHONE_CONNECTED,
|
|---|
| 57 | /** Phone is hung up, waiting for answers to come */
|
|---|
| 58 | IPC_PHONE_HUNGUP,
|
|---|
| 59 | /** Phone was hungup from server */
|
|---|
| 60 | IPC_PHONE_SLAMMED
|
|---|
| [fa8e7d2] | 61 | } ipc_phone_state_t;
|
|---|
| 62 |
|
|---|
| 63 | /** Structure identifying phone (in TASK structure) */
|
|---|
| [48bcf49] | 64 | typedef struct phone {
|
|---|
| [ff48a15] | 65 | mutex_t lock;
|
|---|
| [fa8e7d2] | 66 | link_t link;
|
|---|
| [03a8a8e] | 67 | struct task *caller;
|
|---|
| [fa8e7d2] | 68 | struct answerbox *callee;
|
|---|
| 69 | ipc_phone_state_t state;
|
|---|
| 70 | atomic_t active_calls;
|
|---|
| [48bcf49] | 71 | kobject_t *kobject;
|
|---|
| [fa8e7d2] | 72 | } phone_t;
|
|---|
| 73 |
|
|---|
| 74 | typedef struct answerbox {
|
|---|
| [da1bafb] | 75 | IRQ_SPINLOCK_DECLARE(lock);
|
|---|
| [c33f39f] | 76 |
|
|---|
| 77 | /** Answerbox is active until it enters cleanup. */
|
|---|
| 78 | bool active;
|
|---|
| [a35b458] | 79 |
|
|---|
| [fa8e7d2] | 80 | struct task *task;
|
|---|
| [a35b458] | 81 |
|
|---|
| [fa8e7d2] | 82 | waitq_t wq;
|
|---|
| [a35b458] | 83 |
|
|---|
| [b131ef3] | 84 | /**
|
|---|
| 85 | * Number of answers the answerbox is expecting to eventually arrive.
|
|---|
| 86 | */
|
|---|
| 87 | atomic_t active_calls;
|
|---|
| 88 |
|
|---|
| [8b243f2] | 89 | /** Phones connected to this answerbox. */
|
|---|
| [55b77d9] | 90 | list_t connected_phones;
|
|---|
| [8b243f2] | 91 | /** Received calls. */
|
|---|
| [55b77d9] | 92 | list_t calls;
|
|---|
| 93 | list_t dispatched_calls; /* Should be hash table in the future */
|
|---|
| [a35b458] | 94 |
|
|---|
| [8b243f2] | 95 | /** Answered calls. */
|
|---|
| [55b77d9] | 96 | list_t answers;
|
|---|
| [a35b458] | 97 |
|
|---|
| [da1bafb] | 98 | IRQ_SPINLOCK_DECLARE(irq_lock);
|
|---|
| [a35b458] | 99 |
|
|---|
| [8b243f2] | 100 | /** Notifications from IRQ handlers. */
|
|---|
| [55b77d9] | 101 | list_t irq_notifs;
|
|---|
| [fa8e7d2] | 102 | } answerbox_t;
|
|---|
| [6d9c49a] | 103 |
|
|---|
| 104 | typedef struct {
|
|---|
| [96b02eb9] | 105 | sysarg_t args[IPC_CALL_LEN];
|
|---|
| [ab34cc9] | 106 | /**
|
|---|
| 107 | * Task which made or forwarded the call with IPC_FF_ROUTE_FROM_ME,
|
|---|
| 108 | * or the task which answered the call.
|
|---|
| 109 | */
|
|---|
| [e2ab36f1] | 110 | task_id_t task_id;
|
|---|
| [fdb9982c] | 111 | /** Phone which made or last masqueraded this call. */
|
|---|
| [fbcfd458] | 112 | phone_t *phone;
|
|---|
| [503ffce] | 113 | /** Flags */
|
|---|
| 114 | unsigned flags;
|
|---|
| [7c0e1f5] | 115 | /** User-defined label */
|
|---|
| 116 | sysarg_t label;
|
|---|
| [6deb2cd] | 117 | /** Capability handle */
|
|---|
| 118 | cap_handle_t cap_handle;
|
|---|
| [06e1e95] | 119 | } ipc_data_t;
|
|---|
| [6d9c49a] | 120 |
|
|---|
| [01c3bb4] | 121 | typedef struct call {
|
|---|
| [d51a0d6] | 122 | kobject_t *kobject;
|
|---|
| 123 |
|
|---|
| [2405bb5] | 124 | /**
|
|---|
| 125 | * Task link.
|
|---|
| 126 | * Valid only when the call is not forgotten.
|
|---|
| 127 | * Protected by the task's active_calls_lock.
|
|---|
| 128 | */
|
|---|
| [86939b1] | 129 | link_t ta_link;
|
|---|
| 130 |
|
|---|
| [cfaa35a] | 131 | /** Answerbox link. */
|
|---|
| 132 | link_t ab_link;
|
|---|
| [a35b458] | 133 |
|
|---|
| [da1bafb] | 134 | unsigned int flags;
|
|---|
| [2405bb5] | 135 |
|
|---|
| 136 | /** Protects the forget member. */
|
|---|
| 137 | SPINLOCK_DECLARE(forget_lock);
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * True if the caller 'forgot' this call and donated it to the callee.
|
|---|
| 141 | * Forgotten calls are discarded upon answering (the answer is not
|
|---|
| 142 | * delivered) and answered calls cannot be forgotten. Forgotten calls
|
|---|
| 143 | * also do not figure on the task's active call list.
|
|---|
| 144 | *
|
|---|
| 145 | * We keep this separate from the flags so that it is not necessary
|
|---|
| 146 | * to take a lock when accessing them.
|
|---|
| 147 | */
|
|---|
| 148 | bool forget;
|
|---|
| [20282ef3] | 149 |
|
|---|
| 150 | /** True if the call is in the active list. */
|
|---|
| 151 | bool active;
|
|---|
| [a35b458] | 152 |
|
|---|
| [2405bb5] | 153 | /**
|
|---|
| 154 | * Identification of the caller.
|
|---|
| 155 | * Valid only when the call is not forgotten.
|
|---|
| 156 | */
|
|---|
| [fa8e7d2] | 157 | struct task *sender;
|
|---|
| [a35b458] | 158 |
|
|---|
| [6d351e6] | 159 | /*
|
|---|
| 160 | * Answerbox that will receive the answer.
|
|---|
| 161 | * This will most of the times be the sender's answerbox,
|
|---|
| 162 | * but we allow for useful exceptions.
|
|---|
| 163 | */
|
|---|
| 164 | answerbox_t *callerbox;
|
|---|
| 165 |
|
|---|
| [5a77550] | 166 | /** Phone which was used to send the call. */
|
|---|
| 167 | phone_t *caller_phone;
|
|---|
| [a35b458] | 168 |
|
|---|
| [8b243f2] | 169 | /** Private data to internal IPC. */
|
|---|
| [96b02eb9] | 170 | sysarg_t priv;
|
|---|
| [a35b458] | 171 |
|
|---|
| [8b243f2] | 172 | /** Data passed from/to userspace. */
|
|---|
| [80bcaed] | 173 | ipc_data_t data;
|
|---|
| [32e4643] | 174 |
|
|---|
| 175 | /** Method as it was sent in the request. */
|
|---|
| 176 | sysarg_t request_method;
|
|---|
| 177 |
|
|---|
| [a55d5f9f] | 178 | /** Buffer for IPC_M_DATA_WRITE and IPC_M_DATA_READ. */
|
|---|
| [7918fce] | 179 | uint8_t *buffer;
|
|---|
| [06e1e95] | 180 | } call_t;
|
|---|
| [6d9c49a] | 181 |
|
|---|
| [82d515e9] | 182 | extern slab_cache_t *phone_cache;
|
|---|
| [e5f5ce0] | 183 |
|
|---|
| [6df41418] | 184 | extern answerbox_t *ipc_box_0;
|
|---|
| [fd1210a] | 185 |
|
|---|
| [6d9c49a] | 186 | extern void ipc_init(void);
|
|---|
| [fd1210a] | 187 |
|
|---|
| [da1bafb] | 188 | extern call_t *ipc_call_alloc(unsigned int);
|
|---|
| [fd1210a] | 189 | extern void ipc_call_free(call_t *);
|
|---|
| [cd671c3] | 190 | extern void ipc_call_hold(call_t *);
|
|---|
| 191 | extern void ipc_call_release(call_t *);
|
|---|
| [fd1210a] | 192 |
|
|---|
| [b7fd2a0] | 193 | extern errno_t ipc_call_sync(phone_t *, call_t *);
|
|---|
| 194 | extern errno_t ipc_call(phone_t *, call_t *);
|
|---|
| [4e5dabf] | 195 | extern call_t *ipc_wait_for_call(answerbox_t *, uint32_t, unsigned int);
|
|---|
| [b7fd2a0] | 196 | extern errno_t ipc_forward(call_t *, phone_t *, answerbox_t *, unsigned int);
|
|---|
| [fd1210a] | 197 | extern void ipc_answer(answerbox_t *, call_t *);
|
|---|
| [f9841e69] | 198 | extern void _ipc_answer_free_call(call_t *, bool);
|
|---|
| [fd1210a] | 199 |
|
|---|
| [03a8a8e] | 200 | extern void ipc_phone_init(phone_t *, struct task *);
|
|---|
| [c33f39f] | 201 | extern bool ipc_phone_connect(phone_t *, answerbox_t *);
|
|---|
| [b7fd2a0] | 202 | extern errno_t ipc_phone_hangup(phone_t *);
|
|---|
| [fd1210a] | 203 |
|
|---|
| [12ab886] | 204 | extern void ipc_answerbox_init(answerbox_t *, struct task *);
|
|---|
| [fd1210a] | 205 |
|
|---|
| [d79dcdb] | 206 | extern void ipc_cleanup(void);
|
|---|
| [b7fd2a0] | 207 | extern void ipc_backsend_err(phone_t *, call_t *, errno_t);
|
|---|
| [9a1b20c] | 208 | extern void ipc_answerbox_slam_phones(answerbox_t *, bool);
|
|---|
| [5d3ed34] | 209 | extern void ipc_cleanup_call_list(answerbox_t *, list_t *);
|
|---|
| [7c7aae16] | 210 |
|
|---|
| [fd1210a] | 211 | extern void ipc_print_task(task_id_t);
|
|---|
| [6d9c49a] | 212 |
|
|---|
| 213 | #endif
|
|---|
| [b45c443] | 214 |
|
|---|
| [27ab6a7] | 215 | /** @}
|
|---|
| [b45c443] | 216 | */
|
|---|