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