source: mainline/kernel/generic/src/cap/cap.c@ 90c340fb

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

Allocate capabilities from a dedicated slab cache

  • Property mode set to 100644
File size: 13.3 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 slab_cache_t *cap_slab;
86
87static kobject_t *cap_unpublish_locked(task_t *, cap_handle_t, kobject_type_t);
88
89static size_t caps_hash(const ht_link_t *item)
90{
91 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
92 return hash_mix(cap->handle);
93}
94
95static size_t caps_key_hash(void *key)
96{
97 cap_handle_t *handle = (cap_handle_t *) key;
98 return hash_mix(*handle);
99}
100
101static bool caps_key_equal(void *key, const ht_link_t *item)
102{
103 cap_handle_t *handle = (cap_handle_t *) key;
104 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
105 return *handle == cap->handle;
106}
107
108static hash_table_ops_t caps_ops = {
109 .hash = caps_hash,
110 .key_hash = caps_key_hash,
111 .key_equal = caps_key_equal
112};
113
114void caps_init(void)
115{
116 cap_slab = slab_cache_create("cap_t", sizeof(cap_t), 0, NULL,
117 NULL, 0);
118}
119
120/** Allocate the capability info structure
121 *
122 * @param task Task for which to allocate the info structure.
123 */
124int caps_task_alloc(task_t *task)
125{
126 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t),
127 FRAME_ATOMIC);
128 if (!task->cap_info)
129 return ENOMEM;
130 task->cap_info->handles = ra_arena_create();
131 if (!task->cap_info->handles)
132 goto error_handles;
133 if (!ra_span_add(task->cap_info->handles, 0, MAX_CAPS))
134 goto error_span;
135 if (!hash_table_create(&task->cap_info->caps, 0, 0, &caps_ops))
136 goto error_span;
137 return EOK;
138
139error_span:
140 ra_arena_destroy(task->cap_info->handles);
141error_handles:
142 free(task->cap_info);
143 return ENOMEM;
144}
145
146/** Initialize the capability info structure
147 *
148 * @param task Task for which to initialize the info structure.
149 */
150void caps_task_init(task_t *task)
151{
152 mutex_initialize(&task->cap_info->lock, MUTEX_PASSIVE);
153
154 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
155 list_initialize(&task->cap_info->type_list[t]);
156}
157
158/** Deallocate the capability info structure
159 *
160 * @param task Task from which to deallocate the info structure.
161 */
162void caps_task_free(task_t *task)
163{
164 hash_table_destroy(&task->cap_info->caps);
165 ra_arena_destroy(task->cap_info->handles);
166 free(task->cap_info);
167}
168
169/** Invoke callback function on task's capabilites of given type
170 *
171 * @param task Task where the invocation should take place.
172 * @param type Kernel object type of the task's capabilities that will be
173 * subject to the callback invocation.
174 * @param cb Callback function.
175 * @param arg Argument for the callback function.
176 *
177 * @return True if the callback was called on all matching capabilities.
178 * @return False if the callback was applied only partially.
179 */
180bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
181 bool (*cb)(cap_t *, void *), void *arg)
182{
183 bool done = true;
184
185 mutex_lock(&task->cap_info->lock);
186 list_foreach_safe(task->cap_info->type_list[type], cur, next) {
187 cap_t *cap = list_get_instance(cur, cap_t, type_link);
188 done = cb(cap, arg);
189 if (!done)
190 break;
191 }
192 mutex_unlock(&task->cap_info->lock);
193
194 return done;
195}
196
197/** Initialize capability and associate it with its handle
198 *
199 * @param cap Address of the capability.
200 * @param task Backling to the owning task.
201 * @param handle Capability handle.
202 */
203static void cap_initialize(cap_t *cap, task_t *task, cap_handle_t handle)
204{
205 cap->state = CAP_STATE_FREE;
206 cap->task = task;
207 cap->handle = handle;
208 link_initialize(&cap->type_link);
209}
210
211/** Get capability using capability handle
212 *
213 * @param task Task whose capability to get.
214 * @param handle Capability handle of the desired capability.
215 * @param state State in which the capability must be.
216 *
217 * @return Address of the desired capability if it exists and its state matches.
218 * @return NULL if no such capability exists or it's in a different state.
219 */
220static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
221{
222 assert(mutex_locked(&task->cap_info->lock));
223
224 if ((handle < 0) || (handle >= MAX_CAPS))
225 return NULL;
226 ht_link_t *link = hash_table_find(&task->cap_info->caps, &handle);
227 if (!link)
228 return NULL;
229 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
230 if (cap->state != state)
231 return NULL;
232 return cap;
233}
234
235static bool cap_reclaimer(ht_link_t *link, void *arg)
236{
237 cap_t **result = (cap_t **) arg;
238 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
239
240 if (cap->state == CAP_STATE_PUBLISHED && cap->kobject->ops->reclaim &&
241 cap->kobject->ops->reclaim(cap->kobject)) {
242 kobject_t *kobj = cap_unpublish_locked(cap->task, cap->handle,
243 cap->kobject->type);
244 kobject_put(kobj);
245 cap_initialize(cap, cap->task, cap->handle);
246 *result = cap;
247 return false;
248 }
249
250 return true;
251}
252
253/** Allocate new capability
254 *
255 * @param task Task for which to allocate the new capability.
256 *
257 * @return New capability handle on success.
258 * @return Negative error code in case of error.
259 */
260cap_handle_t cap_alloc(task_t *task)
261{
262 cap_t *cap = NULL;
263 cap_handle_t handle;
264
265 /*
266 * First of all, see if we can reclaim a capability. Note that this
267 * feature is only temporary and capability reclamaition will eventually
268 * be phased out.
269 */
270 mutex_lock(&task->cap_info->lock);
271 hash_table_apply(&task->cap_info->caps, cap_reclaimer, &cap);
272
273 /*
274 * If we don't have a capability by now, try to allocate a new one.
275 */
276 if (!cap) {
277 cap = slab_alloc(cap_slab, FRAME_ATOMIC);
278 if (!cap) {
279 mutex_unlock(&task->cap_info->lock);
280 return ENOMEM;
281 }
282 uintptr_t hbase;
283 if (!ra_alloc(task->cap_info->handles, 1, 1, &hbase)) {
284 slab_free(cap_slab, cap);
285 mutex_unlock(&task->cap_info->lock);
286 return ENOMEM;
287 }
288 cap_initialize(cap, task, (cap_handle_t) hbase);
289 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
290 }
291
292 cap->state = CAP_STATE_ALLOCATED;
293 handle = cap->handle;
294 mutex_unlock(&task->cap_info->lock);
295
296 return handle;
297}
298
299/** Publish allocated capability
300 *
301 * The kernel object is moved into the capability. In other words, its reference
302 * is handed over to the capability. Once published, userspace can access and
303 * manipulate the capability.
304 *
305 * @param task Task in which to publish the capability.
306 * @param handle Capability handle.
307 * @param kobj Kernel object.
308 */
309void
310cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
311{
312 mutex_lock(&task->cap_info->lock);
313 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
314 assert(cap);
315 cap->state = CAP_STATE_PUBLISHED;
316 /* Hand over kobj's reference to cap */
317 cap->kobject = kobj;
318 list_append(&cap->type_link, &task->cap_info->type_list[kobj->type]);
319 mutex_unlock(&task->cap_info->lock);
320}
321
322static kobject_t *
323cap_unpublish_locked(task_t *task, cap_handle_t handle, kobject_type_t type)
324{
325 kobject_t *kobj = NULL;
326
327 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
328 if (cap) {
329 if (cap->kobject->type == type) {
330 /* Hand over cap's reference to kobj */
331 kobj = cap->kobject;
332 cap->kobject = NULL;
333 list_remove(&cap->type_link);
334 cap->state = CAP_STATE_ALLOCATED;
335 }
336 }
337
338 return kobj;
339}
340
341/** Unpublish published capability
342 *
343 * The kernel object is moved out of the capability. In other words, the
344 * capability's reference to the objects is handed over to the kernel object
345 * pointer returned by this function. Once unpublished, the capability does not
346 * refer to any kernel object anymore.
347 *
348 * @param task Task in which to unpublish the capability.
349 * @param handle Capability handle.
350 * @param type Kernel object type of the object associated with the
351 * capability.
352 */
353kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
354{
355
356 mutex_lock(&task->cap_info->lock);
357 kobject_t *kobj = cap_unpublish_locked(task, handle, type);
358 mutex_unlock(&task->cap_info->lock);
359
360 return kobj;
361}
362
363/** Free allocated capability
364 *
365 * @param task Task in which to free the capability.
366 * @param handle Capability handle.
367 */
368void cap_free(task_t *task, cap_handle_t handle)
369{
370 assert(handle >= 0);
371 assert(handle < MAX_CAPS);
372
373 mutex_lock(&task->cap_info->lock);
374 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
375
376 assert(cap);
377
378 hash_table_remove_item(&task->cap_info->caps, &cap->caps_link);
379 ra_free(task->cap_info->handles, handle, 1);
380 slab_free(cap_slab, cap);
381 mutex_unlock(&task->cap_info->lock);
382}
383
384/** Initialize kernel object
385 *
386 * @param kobj Kernel object to initialize.
387 * @param type Type of the kernel object.
388 * @param raw Raw pointer to the encapsulated object.
389 * @param ops Pointer to kernel object operations for the respective type.
390 */
391void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw,
392 kobject_ops_t *ops)
393{
394 atomic_set(&kobj->refcnt, 1);
395 kobj->type = type;
396 kobj->raw = raw;
397 kobj->ops = ops;
398}
399
400/** Get new reference to kernel object from capability
401 *
402 * @param task Task from which to get the reference.
403 * @param handle Capability handle.
404 * @param type Kernel object type of the object associated with the
405 * capability referenced by handle.
406 *
407 * @return Kernel object with incremented reference count on success.
408 * @return NULL if there is no matching capability or kernel object.
409 */
410kobject_t *
411kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
412{
413 kobject_t *kobj = NULL;
414
415 mutex_lock(&task->cap_info->lock);
416 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
417 if (cap) {
418 if (cap->kobject->type == type) {
419 kobj = cap->kobject;
420 atomic_inc(&kobj->refcnt);
421 }
422 }
423 mutex_unlock(&task->cap_info->lock);
424
425 return kobj;
426}
427
428/** Record new reference
429 *
430 * @param kobj Kernel object from which the new reference is created.
431 */
432void kobject_add_ref(kobject_t *kobj)
433{
434 atomic_inc(&kobj->refcnt);
435}
436
437/** Drop reference to kernel object
438 *
439 * The encapsulated object and the kobject_t wrapper are both destroyed when the
440 * last reference is dropped.
441 *
442 * @param kobj Kernel object whose reference to drop.
443 */
444void kobject_put(kobject_t *kobj)
445{
446 if (atomic_postdec(&kobj->refcnt) == 1) {
447 kobj->ops->destroy(kobj->raw);
448 free(kobj);
449 }
450}
451
452/** @}
453 */
Note: See TracBrowser for help on using the repository browser.