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