source: mainline/kernel/generic/src/cap/cap.c@ c46bfbc

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

Allow caps_task_alloc() to fail

  • Property mode set to 100644
File size: 13.1 KB
Line 
1/*
2 * Copyright (c) 2017 Jakub Jermar
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 generic
30 * @{
31 */
32/** @file
33 */
34
35/*
36 * HelenOS capabilities are task-local names for references to kernel objects.
37 * Kernel objects are reference-counted wrappers for a select group of objects
38 * allocated in and by the kernel that can be made accessible to userspace in a
39 * controlled way via integer handles.
40 *
41 * A kernel object (kobject_t) encapsulates one of the following raw objects:
42 *
43 * - IPC phone
44 * - IRQ object
45 *
46 * A capability (cap_t) is either free, allocated or published. Free
47 * capabilities can be allocated, which reserves the capability handle in the
48 * task-local capability space. Allocated capabilities can be published, which
49 * associates them with an existing kernel object. Userspace can only access
50 * published capabilities.
51 *
52 * A published capability may get unpublished, which disassociates it from the
53 * underlying kernel object and puts it back into the allocated state. An
54 * allocated capability can be freed to become available for future use.
55 *
56 * There is a 1:1 correspondence between a kernel object (kobject_t) and the
57 * actual raw object it encapsulates. A kernel object (kobject_t) may have
58 * multiple references, either implicit from one or more capabilities (cap_t),
59 * even from capabilities in different tasks, or explicit as a result of
60 * creating a new reference from a capability handle using kobject_get(), or
61 * creating a new reference from an already existing reference by
62 * kobject_add_ref() or as a result of unpublishing a capability and
63 * disassociating it from its kobject_t using cap_unpublish().
64 *
65 * As kernel objects are reference-counted, they get automatically destroyed
66 * when their last reference is dropped in kobject_put(). The idea is that
67 * whenever a kernel object is inserted into some sort of a container (e.g. a
68 * list or hash table), its reference count should be incremented via
69 * kobject_get() or kobject_add_ref(). When the kernel object is removed from
70 * the container, the reference count should go down via a call to
71 * kobject_put().
72 */
73
74#include <cap/cap.h>
75#include <proc/task.h>
76#include <synch/mutex.h>
77#include <abi/errno.h>
78#include <mm/slab.h>
79#include <adt/list.h>
80
81#include <stdint.h>
82
83#define MAX_CAPS INT_MAX
84
85static kobject_t *cap_unpublish_locked(task_t *, cap_handle_t, kobject_type_t);
86
87static size_t caps_hash(const ht_link_t *item)
88{
89 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
90 return hash_mix(cap->handle);
91}
92
93static size_t caps_key_hash(void *key)
94{
95 cap_handle_t *handle = (cap_handle_t *) key;
96 return hash_mix(*handle);
97}
98
99static bool caps_key_equal(void *key, const ht_link_t *item)
100{
101 cap_handle_t *handle = (cap_handle_t *) key;
102 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
103 return *handle == cap->handle;
104}
105
106static hash_table_ops_t caps_ops = {
107 .hash = caps_hash,
108 .key_hash = caps_key_hash,
109 .key_equal = caps_key_equal
110};
111
112/** Allocate the capability info structure
113 *
114 * @param task Task for which to allocate the info structure.
115 */
116int caps_task_alloc(task_t *task)
117{
118 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t),
119 FRAME_ATOMIC);
120 if (!task->cap_info)
121 return ENOMEM;
122 task->cap_info->handles = ra_arena_create();
123 if (!task->cap_info->handles)
124 goto error_handles;
125 if (!ra_span_add(task->cap_info->handles, 0, MAX_CAPS))
126 goto error_span;
127 if (!hash_table_create(&task->cap_info->caps, 0, 0, &caps_ops))
128 goto error_span;
129 return EOK;
130
131error_span:
132 ra_arena_destroy(task->cap_info->handles);
133error_handles:
134 free(task->cap_info);
135 return ENOMEM;
136}
137
138/** Initialize the capability info structure
139 *
140 * @param task Task for which to initialize the info structure.
141 */
142void caps_task_init(task_t *task)
143{
144 mutex_initialize(&task->cap_info->lock, MUTEX_PASSIVE);
145
146 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
147 list_initialize(&task->cap_info->type_list[t]);
148}
149
150/** Deallocate the capability info structure
151 *
152 * @param task Task from which to deallocate the info structure.
153 */
154void caps_task_free(task_t *task)
155{
156 hash_table_destroy(&task->cap_info->caps);
157 ra_arena_destroy(task->cap_info->handles);
158 free(task->cap_info);
159}
160
161/** Invoke callback function on task's capabilites of given type
162 *
163 * @param task Task where the invocation should take place.
164 * @param type Kernel object type of the task's capabilities that will be
165 * subject to the callback invocation.
166 * @param cb Callback function.
167 * @param arg Argument for the callback function.
168 *
169 * @return True if the callback was called on all matching capabilities.
170 * @return False if the callback was applied only partially.
171 */
172bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
173 bool (*cb)(cap_t *, void *), void *arg)
174{
175 bool done = true;
176
177 mutex_lock(&task->cap_info->lock);
178 list_foreach_safe(task->cap_info->type_list[type], cur, next) {
179 cap_t *cap = list_get_instance(cur, cap_t, type_link);
180 done = cb(cap, arg);
181 if (!done)
182 break;
183 }
184 mutex_unlock(&task->cap_info->lock);
185
186 return done;
187}
188
189/** Initialize capability and associate it with its handle
190 *
191 * @param cap Address of the capability.
192 * @param task Backling to the owning task.
193 * @param handle Capability handle.
194 */
195static void cap_initialize(cap_t *cap, task_t *task, cap_handle_t handle)
196{
197 cap->state = CAP_STATE_FREE;
198 cap->task = task;
199 cap->handle = handle;
200 link_initialize(&cap->type_link);
201}
202
203/** Get capability using capability handle
204 *
205 * @param task Task whose capability to get.
206 * @param handle Capability handle of the desired capability.
207 * @param state State in which the capability must be.
208 *
209 * @return Address of the desired capability if it exists and its state matches.
210 * @return NULL if no such capability exists or it's in a different state.
211 */
212static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
213{
214 assert(mutex_locked(&task->cap_info->lock));
215
216 if ((handle < 0) || (handle >= MAX_CAPS))
217 return NULL;
218 ht_link_t *link = hash_table_find(&task->cap_info->caps, &handle);
219 if (!link)
220 return NULL;
221 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
222 if (cap->state != state)
223 return NULL;
224 return cap;
225}
226
227static bool cap_reclaimer(ht_link_t *link, void *arg)
228{
229 cap_t **result = (cap_t **) arg;
230 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
231
232 if (cap->state == CAP_STATE_PUBLISHED && cap->kobject->ops->reclaim &&
233 cap->kobject->ops->reclaim(cap->kobject)) {
234 kobject_t *kobj = cap_unpublish_locked(cap->task, cap->handle,
235 cap->kobject->type);
236 kobject_put(kobj);
237 cap_initialize(cap, cap->task, cap->handle);
238 *result = cap;
239 return false;
240 }
241
242 return true;
243}
244
245/** Allocate new capability
246 *
247 * @param task Task for which to allocate the new capability.
248 *
249 * @return New capability handle on success.
250 * @return Negative error code in case of error.
251 */
252cap_handle_t cap_alloc(task_t *task)
253{
254 cap_t *cap = NULL;
255 cap_handle_t handle;
256
257 /*
258 * First of all, see if we can reclaim a capability. Note that this
259 * feature is only temporary and capability reclamaition will eventually
260 * be phased out.
261 */
262 mutex_lock(&task->cap_info->lock);
263 hash_table_apply(&task->cap_info->caps, cap_reclaimer, &cap);
264
265 /*
266 * If we don't have a capability by now, try to allocate a new one.
267 */
268 if (!cap) {
269 cap = malloc(sizeof(cap_t), 0);
270 if (!cap) {
271 mutex_unlock(&task->cap_info->lock);
272 return ENOMEM;
273 }
274 uintptr_t hbase;
275 if (!ra_alloc(task->cap_info->handles, 1, 1, &hbase)) {
276 free(cap);
277 mutex_unlock(&task->cap_info->lock);
278 return ENOMEM;
279 }
280 cap_initialize(cap, task, (cap_handle_t) hbase);
281 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
282 }
283
284 cap->state = CAP_STATE_ALLOCATED;
285 handle = cap->handle;
286 mutex_unlock(&task->cap_info->lock);
287
288 return handle;
289}
290
291/** Publish allocated capability
292 *
293 * The kernel object is moved into the capability. In other words, its reference
294 * is handed over to the capability. Once published, userspace can access and
295 * manipulate the capability.
296 *
297 * @param task Task in which to publish the capability.
298 * @param handle Capability handle.
299 * @param kobj Kernel object.
300 */
301void
302cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
303{
304 mutex_lock(&task->cap_info->lock);
305 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
306 assert(cap);
307 cap->state = CAP_STATE_PUBLISHED;
308 /* Hand over kobj's reference to cap */
309 cap->kobject = kobj;
310 list_append(&cap->type_link, &task->cap_info->type_list[kobj->type]);
311 mutex_unlock(&task->cap_info->lock);
312}
313
314static kobject_t *
315cap_unpublish_locked(task_t *task, cap_handle_t handle, kobject_type_t type)
316{
317 kobject_t *kobj = NULL;
318
319 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
320 if (cap) {
321 if (cap->kobject->type == type) {
322 /* Hand over cap's reference to kobj */
323 kobj = cap->kobject;
324 cap->kobject = NULL;
325 list_remove(&cap->type_link);
326 cap->state = CAP_STATE_ALLOCATED;
327 }
328 }
329
330 return kobj;
331}
332
333/** Unpublish published capability
334 *
335 * The kernel object is moved out of the capability. In other words, the
336 * capability's reference to the objects is handed over to the kernel object
337 * pointer returned by this function. Once unpublished, the capability does not
338 * refer to any kernel object anymore.
339 *
340 * @param task Task in which to unpublish the capability.
341 * @param handle Capability handle.
342 * @param type Kernel object type of the object associated with the
343 * capability.
344 */
345kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
346{
347
348 mutex_lock(&task->cap_info->lock);
349 kobject_t *kobj = cap_unpublish_locked(task, handle, type);
350 mutex_unlock(&task->cap_info->lock);
351
352 return kobj;
353}
354
355/** Free allocated capability
356 *
357 * @param task Task in which to free the capability.
358 * @param handle Capability handle.
359 */
360void cap_free(task_t *task, cap_handle_t handle)
361{
362 assert(handle >= 0);
363 assert(handle < MAX_CAPS);
364
365 mutex_lock(&task->cap_info->lock);
366 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
367
368 assert(cap);
369
370 hash_table_remove_item(&task->cap_info->caps, &cap->caps_link);
371 ra_free(task->cap_info->handles, handle, 1);
372 free(cap);
373 mutex_unlock(&task->cap_info->lock);
374}
375
376/** Initialize kernel object
377 *
378 * @param kobj Kernel object to initialize.
379 * @param type Type of the kernel object.
380 * @param raw Raw pointer to the encapsulated object.
381 * @param ops Pointer to kernel object operations for the respective type.
382 */
383void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw,
384 kobject_ops_t *ops)
385{
386 atomic_set(&kobj->refcnt, 1);
387 kobj->type = type;
388 kobj->raw = raw;
389 kobj->ops = ops;
390}
391
392/** Get new reference to kernel object from capability
393 *
394 * @param task Task from which to get the reference.
395 * @param handle Capability handle.
396 * @param type Kernel object type of the object associated with the
397 * capability referenced by handle.
398 *
399 * @return Kernel object with incremented reference count on success.
400 * @return NULL if there is no matching capability or kernel object.
401 */
402kobject_t *
403kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
404{
405 kobject_t *kobj = NULL;
406
407 mutex_lock(&task->cap_info->lock);
408 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
409 if (cap) {
410 if (cap->kobject->type == type) {
411 kobj = cap->kobject;
412 atomic_inc(&kobj->refcnt);
413 }
414 }
415 mutex_unlock(&task->cap_info->lock);
416
417 return kobj;
418}
419
420/** Record new reference
421 *
422 * @param kobj Kernel object from which the new reference is created.
423 */
424void kobject_add_ref(kobject_t *kobj)
425{
426 atomic_inc(&kobj->refcnt);
427}
428
429/** Drop reference to kernel object
430 *
431 * The encapsulated object and the kobject_t wrapper are both destroyed when the
432 * last reference is dropped.
433 *
434 * @param kobj Kernel object whose reference to drop.
435 */
436void kobject_put(kobject_t *kobj)
437{
438 if (atomic_postdec(&kobj->refcnt) == 1) {
439 kobj->ops->destroy(kobj->raw);
440 free(kobj);
441 }
442}
443
444/** @}
445 */
Note: See TracBrowser for help on using the repository browser.