Allow caps_task_alloc() to fail
[helenos.git] / kernel / generic / src / cap / cap.c
blob8206d3ba842fd37d582cc1073cfdde0ab662904f
1 /*
2 * Copyright (c) 2017 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup generic
30 * @{
32 /** @file
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.
41 * A kernel object (kobject_t) encapsulates one of the following raw objects:
43 * - IPC phone
44 * - IRQ object
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.
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.
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().
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().
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>
81 #include <stdint.h>
83 #define MAX_CAPS INT_MAX
85 static kobject_t *cap_unpublish_locked(task_t *, cap_handle_t, kobject_type_t);
87 static size_t caps_hash(const ht_link_t *item)
89 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
90 return hash_mix(cap->handle);
93 static size_t caps_key_hash(void *key)
95 cap_handle_t *handle = (cap_handle_t *) key;
96 return hash_mix(*handle);
99 static bool caps_key_equal(void *key, const ht_link_t *item)
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;
106 static hash_table_ops_t caps_ops = {
107 .hash = caps_hash,
108 .key_hash = caps_key_hash,
109 .key_equal = caps_key_equal
112 /** Allocate the capability info structure
114 * @param task Task for which to allocate the info structure.
116 int caps_task_alloc(task_t *task)
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;
131 error_span:
132 ra_arena_destroy(task->cap_info->handles);
133 error_handles:
134 free(task->cap_info);
135 return ENOMEM;
138 /** Initialize the capability info structure
140 * @param task Task for which to initialize the info structure.
142 void caps_task_init(task_t *task)
144 mutex_initialize(&task->cap_info->lock, MUTEX_PASSIVE);
146 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
147 list_initialize(&task->cap_info->type_list[t]);
150 /** Deallocate the capability info structure
152 * @param task Task from which to deallocate the info structure.
154 void caps_task_free(task_t *task)
156 hash_table_destroy(&task->cap_info->caps);
157 ra_arena_destroy(task->cap_info->handles);
158 free(task->cap_info);
161 /** Invoke callback function on task's capabilites of given type
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.
169 * @return True if the callback was called on all matching capabilities.
170 * @return False if the callback was applied only partially.
172 bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
173 bool (*cb)(cap_t *, void *), void *arg)
175 bool done = true;
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;
184 mutex_unlock(&task->cap_info->lock);
186 return done;
189 /** Initialize capability and associate it with its handle
191 * @param cap Address of the capability.
192 * @param task Backling to the owning task.
193 * @param handle Capability handle.
195 static void cap_initialize(cap_t *cap, task_t *task, cap_handle_t handle)
197 cap->state = CAP_STATE_FREE;
198 cap->task = task;
199 cap->handle = handle;
200 link_initialize(&cap->type_link);
203 /** Get capability using capability handle
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.
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.
212 static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
214 assert(mutex_locked(&task->cap_info->lock));
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;
227 static bool cap_reclaimer(ht_link_t *link, void *arg)
229 cap_t **result = (cap_t **) arg;
230 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
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;
242 return true;
245 /** Allocate new capability
247 * @param task Task for which to allocate the new capability.
249 * @return New capability handle on success.
250 * @return Negative error code in case of error.
252 cap_handle_t cap_alloc(task_t *task)
254 cap_t *cap = NULL;
255 cap_handle_t handle;
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.
262 mutex_lock(&task->cap_info->lock);
263 hash_table_apply(&task->cap_info->caps, cap_reclaimer, &cap);
266 * If we don't have a capability by now, try to allocate a new one.
268 if (!cap) {
269 cap = malloc(sizeof(cap_t), 0);
270 if (!cap) {
271 mutex_unlock(&task->cap_info->lock);
272 return ENOMEM;
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;
280 cap_initialize(cap, task, (cap_handle_t) hbase);
281 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
284 cap->state = CAP_STATE_ALLOCATED;
285 handle = cap->handle;
286 mutex_unlock(&task->cap_info->lock);
288 return handle;
291 /** Publish allocated capability
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.
297 * @param task Task in which to publish the capability.
298 * @param handle Capability handle.
299 * @param kobj Kernel object.
301 void
302 cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
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);
314 static kobject_t *
315 cap_unpublish_locked(task_t *task, cap_handle_t handle, kobject_type_t type)
317 kobject_t *kobj = NULL;
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;
330 return kobj;
333 /** Unpublish published capability
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.
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.
345 kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
348 mutex_lock(&task->cap_info->lock);
349 kobject_t *kobj = cap_unpublish_locked(task, handle, type);
350 mutex_unlock(&task->cap_info->lock);
352 return kobj;
355 /** Free allocated capability
357 * @param task Task in which to free the capability.
358 * @param handle Capability handle.
360 void cap_free(task_t *task, cap_handle_t handle)
362 assert(handle >= 0);
363 assert(handle < MAX_CAPS);
365 mutex_lock(&task->cap_info->lock);
366 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
368 assert(cap);
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);
376 /** Initialize kernel object
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.
383 void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw,
384 kobject_ops_t *ops)
386 atomic_set(&kobj->refcnt, 1);
387 kobj->type = type;
388 kobj->raw = raw;
389 kobj->ops = ops;
392 /** Get new reference to kernel object from capability
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.
399 * @return Kernel object with incremented reference count on success.
400 * @return NULL if there is no matching capability or kernel object.
402 kobject_t *
403 kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
405 kobject_t *kobj = NULL;
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);
415 mutex_unlock(&task->cap_info->lock);
417 return kobj;
420 /** Record new reference
422 * @param kobj Kernel object from which the new reference is created.
424 void kobject_add_ref(kobject_t *kobj)
426 atomic_inc(&kobj->refcnt);
429 /** Drop reference to kernel object
431 * The encapsulated object and the kobject_t wrapper are both destroyed when the
432 * last reference is dropped.
434 * @param kobj Kernel object whose reference to drop.
436 void kobject_put(kobject_t *kobj)
438 if (atomic_postdec(&kobj->refcnt) == 1) {
439 kobj->ops->destroy(kobj->raw);
440 free(kobj);
444 /** @}