| 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 genericipc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_IPC_H_
|
|---|
| 36 | #define KERN_IPC_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <synch/spinlock.h>
|
|---|
| 39 | #include <synch/mutex.h>
|
|---|
| 40 | #include <synch/waitq.h>
|
|---|
| 41 | #include <abi/ipc/ipc.h>
|
|---|
| 42 | #include <abi/proc/task.h>
|
|---|
| 43 | #include <typedefs.h>
|
|---|
| 44 |
|
|---|
| 45 | #define IPC_MAX_PHONES 64
|
|---|
| 46 |
|
|---|
| 47 | struct answerbox;
|
|---|
| 48 | struct task;
|
|---|
| 49 |
|
|---|
| 50 | typedef enum {
|
|---|
| 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
|
|---|
| 61 | } ipc_phone_state_t;
|
|---|
| 62 |
|
|---|
| 63 | /** Structure identifying phone (in TASK structure) */
|
|---|
| 64 | typedef struct {
|
|---|
| 65 | mutex_t lock;
|
|---|
| 66 | link_t link;
|
|---|
| 67 | struct answerbox *callee;
|
|---|
| 68 | ipc_phone_state_t state;
|
|---|
| 69 | atomic_t active_calls;
|
|---|
| 70 | } phone_t;
|
|---|
| 71 |
|
|---|
| 72 | typedef struct answerbox {
|
|---|
| 73 | IRQ_SPINLOCK_DECLARE(lock);
|
|---|
| 74 |
|
|---|
| 75 | struct task *task;
|
|---|
| 76 |
|
|---|
| 77 | waitq_t wq;
|
|---|
| 78 |
|
|---|
| 79 | /** Phones connected to this answerbox. */
|
|---|
| 80 | list_t connected_phones;
|
|---|
| 81 | /** Received calls. */
|
|---|
| 82 | list_t calls;
|
|---|
| 83 | list_t dispatched_calls; /* Should be hash table in the future */
|
|---|
| 84 |
|
|---|
| 85 | /** Answered calls. */
|
|---|
| 86 | list_t answers;
|
|---|
| 87 |
|
|---|
| 88 | IRQ_SPINLOCK_DECLARE(irq_lock);
|
|---|
| 89 |
|
|---|
| 90 | /** Notifications from IRQ handlers. */
|
|---|
| 91 | list_t irq_notifs;
|
|---|
| 92 | /** IRQs with notifications to this answerbox. */
|
|---|
| 93 | list_t irq_list;
|
|---|
| 94 | } answerbox_t;
|
|---|
| 95 |
|
|---|
| 96 | typedef struct {
|
|---|
| 97 | sysarg_t args[IPC_CALL_LEN];
|
|---|
| 98 | /**
|
|---|
| 99 | * Task which made or forwarded the call with IPC_FF_ROUTE_FROM_ME,
|
|---|
| 100 | * or the task which answered the call.
|
|---|
| 101 | */
|
|---|
| 102 | task_id_t task_id;
|
|---|
| 103 | /** Phone which made or last masqueraded this call. */
|
|---|
| 104 | phone_t *phone;
|
|---|
| 105 | } ipc_data_t;
|
|---|
| 106 |
|
|---|
| 107 | typedef struct {
|
|---|
| 108 | /**
|
|---|
| 109 | * Task link.
|
|---|
| 110 | * Valid only when the call is not forgotten.
|
|---|
| 111 | * Protected by the task's active_calls_lock.
|
|---|
| 112 | */
|
|---|
| 113 | link_t ta_link;
|
|---|
| 114 |
|
|---|
| 115 | /** Answerbox link. */
|
|---|
| 116 | link_t ab_link;
|
|---|
| 117 |
|
|---|
| 118 | unsigned int flags;
|
|---|
| 119 |
|
|---|
| 120 | /** Protects the forget member. */
|
|---|
| 121 | SPINLOCK_DECLARE(forget_lock);
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * True if the caller 'forgot' this call and donated it to the callee.
|
|---|
| 125 | * Forgotten calls are discarded upon answering (the answer is not
|
|---|
| 126 | * delivered) and answered calls cannot be forgotten. Forgotten calls
|
|---|
| 127 | * also do not figure on the task's active call list.
|
|---|
| 128 | *
|
|---|
| 129 | * We keep this separate from the flags so that it is not necessary
|
|---|
| 130 | * to take a lock when accessing them.
|
|---|
| 131 | */
|
|---|
| 132 | bool forget;
|
|---|
| 133 |
|
|---|
| 134 | /** True if the call is in the active list. */
|
|---|
| 135 | bool active;
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * Identification of the caller.
|
|---|
| 139 | * Valid only when the call is not forgotten.
|
|---|
| 140 | */
|
|---|
| 141 | struct task *sender;
|
|---|
| 142 |
|
|---|
| 143 | /** Private data to internal IPC. */
|
|---|
| 144 | sysarg_t priv;
|
|---|
| 145 |
|
|---|
| 146 | /** Data passed from/to userspace. */
|
|---|
| 147 | ipc_data_t data;
|
|---|
| 148 |
|
|---|
| 149 | /** Method as it was sent in the request. */
|
|---|
| 150 | sysarg_t request_method;
|
|---|
| 151 |
|
|---|
| 152 | /** Buffer for IPC_M_DATA_WRITE and IPC_M_DATA_READ. */
|
|---|
| 153 | uint8_t *buffer;
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * The forward operation can masquerade the caller phone. For those
|
|---|
| 157 | * cases, we must keep it aside so that the answer is processed
|
|---|
| 158 | * correctly.
|
|---|
| 159 | */
|
|---|
| 160 | phone_t *caller_phone;
|
|---|
| 161 | } call_t;
|
|---|
| 162 |
|
|---|
| 163 | extern answerbox_t *ipc_phone_0;
|
|---|
| 164 |
|
|---|
| 165 | extern void ipc_init(void);
|
|---|
| 166 |
|
|---|
| 167 | extern call_t *ipc_call_alloc(unsigned int);
|
|---|
| 168 | extern void ipc_call_free(call_t *);
|
|---|
| 169 |
|
|---|
| 170 | extern int ipc_call(phone_t *, call_t *);
|
|---|
| 171 | extern call_t *ipc_wait_for_call(answerbox_t *, uint32_t, unsigned int);
|
|---|
| 172 | extern int ipc_forward(call_t *, phone_t *, answerbox_t *, unsigned int);
|
|---|
| 173 | extern void ipc_answer(answerbox_t *, call_t *);
|
|---|
| 174 |
|
|---|
| 175 | extern void ipc_phone_init(phone_t *);
|
|---|
| 176 | extern void ipc_phone_connect(phone_t *, answerbox_t *);
|
|---|
| 177 | extern int ipc_phone_hangup(phone_t *);
|
|---|
| 178 |
|
|---|
| 179 | extern void ipc_answerbox_init(answerbox_t *, struct task *);
|
|---|
| 180 |
|
|---|
| 181 | extern void ipc_cleanup(void);
|
|---|
| 182 | extern void ipc_backsend_err(phone_t *, call_t *, sysarg_t);
|
|---|
| 183 | extern void ipc_answerbox_slam_phones(answerbox_t *, bool);
|
|---|
| 184 | extern void ipc_cleanup_call_list(list_t *);
|
|---|
| 185 |
|
|---|
| 186 | extern void ipc_print_task(task_id_t);
|
|---|
| 187 |
|
|---|
| 188 | #endif
|
|---|
| 189 |
|
|---|
| 190 | /** @}
|
|---|
| 191 | */
|
|---|