source: mainline/kernel/generic/include/ipc/ipc.h@ 3be9d10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3be9d10 was eadaeae8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Make capability handles type-safe

Define distinct pointer types for the handles of the supported
capability types and use them instead of integer handles. This makes it
virtually impossible to pass a non-handle or a handle of different type
instead of the proper handle. Also turn cap_handle_t into an "untyped"
capability handle that can be assigned to and from the "typed" handles.

This commit also fixes a bug in msim-con driver, which wrongly used the
IRQ number instead of the IRQ capability handle to unregister the IRQ.

This commit also fixes the wrong use of the capability handle instead
of error code in libusbhost.

  • Property mode set to 100644
File size: 5.7 KB
Line 
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#include <mm/slab.h>
45#include <cap/cap.h>
46
47struct answerbox;
48struct task;
49
50typedef 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) */
64typedef struct phone {
65 mutex_t lock;
66 link_t link;
67 struct task *caller;
68 struct answerbox *callee;
69 ipc_phone_state_t state;
70 atomic_t active_calls;
71 kobject_t *kobject;
72} phone_t;
73
74typedef struct answerbox {
75 IRQ_SPINLOCK_DECLARE(lock);
76
77 /** Answerbox is active until it enters cleanup. */
78 bool active;
79
80 struct task *task;
81
82 waitq_t wq;
83
84 /**
85 * Number of answers the answerbox is expecting to eventually arrive.
86 */
87 atomic_t active_calls;
88
89 /** Phones connected to this answerbox. */
90 list_t connected_phones;
91 /** Received calls. */
92 list_t calls;
93 list_t dispatched_calls; /* Should be hash table in the future */
94
95 /** Answered calls. */
96 list_t answers;
97
98 IRQ_SPINLOCK_DECLARE(irq_lock);
99
100 /** Notifications from IRQ handlers. */
101 list_t irq_notifs;
102} answerbox_t;
103
104typedef struct {
105 sysarg_t args[IPC_CALL_LEN];
106 /**
107 * Task which made or forwarded the call with IPC_FF_ROUTE_FROM_ME,
108 * or the task which answered the call.
109 */
110 task_id_t task_id;
111 /** Phone which made or last masqueraded this call. */
112 phone_t *phone;
113 /** Flags */
114 unsigned flags;
115 /** User-defined label */
116 sysarg_t label;
117 /** Capability handle */
118 cap_call_handle_t cap_handle;
119} ipc_data_t;
120
121typedef struct call {
122 kobject_t *kobject;
123
124 /**
125 * Task link.
126 * Valid only when the call is not forgotten.
127 * Protected by the task's active_calls_lock.
128 */
129 link_t ta_link;
130
131 /** Answerbox link. */
132 link_t ab_link;
133
134 unsigned int flags;
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;
149
150 /** True if the call is in the active list. */
151 bool active;
152
153 /**
154 * Identification of the caller.
155 * Valid only when the call is not forgotten.
156 */
157 struct task *sender;
158
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
166 /** Phone which was used to send the call. */
167 phone_t *caller_phone;
168
169 /** Private data to internal IPC. */
170 sysarg_t priv;
171
172 /** Data passed from/to userspace. */
173 ipc_data_t data;
174
175 /** Method as it was sent in the request. */
176 sysarg_t request_method;
177
178 /** Buffer for IPC_M_DATA_WRITE and IPC_M_DATA_READ. */
179 uint8_t *buffer;
180} call_t;
181
182extern slab_cache_t *phone_cache;
183
184extern answerbox_t *ipc_box_0;
185
186extern void ipc_init(void);
187
188extern call_t *ipc_call_alloc(unsigned int);
189extern void ipc_call_free(call_t *);
190extern void ipc_call_hold(call_t *);
191extern void ipc_call_release(call_t *);
192
193extern errno_t ipc_call_sync(phone_t *, call_t *);
194extern errno_t ipc_call(phone_t *, call_t *);
195extern call_t *ipc_wait_for_call(answerbox_t *, uint32_t, unsigned int);
196extern errno_t ipc_forward(call_t *, phone_t *, answerbox_t *, unsigned int);
197extern void ipc_answer(answerbox_t *, call_t *);
198extern void _ipc_answer_free_call(call_t *, bool);
199
200extern void ipc_phone_init(phone_t *, struct task *);
201extern bool ipc_phone_connect(phone_t *, answerbox_t *);
202extern errno_t ipc_phone_hangup(phone_t *);
203
204extern void ipc_answerbox_init(answerbox_t *, struct task *);
205
206extern void ipc_cleanup(void);
207extern void ipc_backsend_err(phone_t *, call_t *, errno_t);
208extern void ipc_answerbox_slam_phones(answerbox_t *, bool);
209extern void ipc_cleanup_call_list(answerbox_t *, list_t *);
210
211extern void ipc_print_task(task_id_t);
212
213#endif
214
215/** @}
216 */
Note: See TracBrowser for help on using the repository browser.