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