source: mainline/kernel/generic/src/cap/cap.c@ 6874bd2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6874bd2 was aafed15, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Declare malloc() etc in standard <stdlib.h> rather than <mm/slab.h>

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