[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 |
|
---|
[3f74275] | 35 | #include <cap/cap.h>
|
---|
[7e3826d9] | 36 | #include <proc/task.h>
|
---|
| 37 | #include <synch/spinlock.h>
|
---|
[e9d15d9] | 38 | #include <abi/errno.h>
|
---|
[e7ac23d0] | 39 | #include <mm/slab.h>
|
---|
[7e3826d9] | 40 |
|
---|
[3f74275] | 41 | void cap_initialize(cap_t *cap)
|
---|
[05ffb41] | 42 | {
|
---|
[3f74275] | 43 | cap->type = CAP_TYPE_INVALID;
|
---|
| 44 | cap->can_reclaim = NULL;
|
---|
[05ffb41] | 45 | }
|
---|
| 46 |
|
---|
[3f74275] | 47 | void caps_task_alloc(task_t *task)
|
---|
[e7ac23d0] | 48 | {
|
---|
[3f74275] | 49 | task->caps = malloc(sizeof(cap_t) * MAX_CAPS, 0);
|
---|
[e7ac23d0] | 50 | }
|
---|
| 51 |
|
---|
[3f74275] | 52 | void caps_task_init(task_t *task)
|
---|
[e7ac23d0] | 53 | {
|
---|
[3f74275] | 54 | for (int i = 0; i < MAX_CAPS; i++)
|
---|
| 55 | cap_initialize(&task->caps[i]);
|
---|
[e7ac23d0] | 56 | }
|
---|
| 57 |
|
---|
[3f74275] | 58 | void caps_task_free(task_t *task)
|
---|
[e7ac23d0] | 59 | {
|
---|
[3f74275] | 60 | free(task->caps);
|
---|
[e7ac23d0] | 61 | }
|
---|
| 62 |
|
---|
[3f74275] | 63 | cap_t *cap_get(task_t *task, int handle, cap_type_t type)
|
---|
[7e3826d9] | 64 | {
|
---|
[3f74275] | 65 | if ((handle < 0) || (handle >= MAX_CAPS))
|
---|
[7e3826d9] | 66 | return NULL;
|
---|
[3f74275] | 67 | if (task->caps[handle].type != type)
|
---|
[7e3826d9] | 68 | return NULL;
|
---|
[3f74275] | 69 | return &task->caps[handle];
|
---|
[05ffb41] | 70 | }
|
---|
| 71 |
|
---|
[3f74275] | 72 | cap_t *cap_get_current(int handle, cap_type_t type)
|
---|
[05ffb41] | 73 | {
|
---|
[3f74275] | 74 | return cap_get(TASK, handle, type);
|
---|
[7e3826d9] | 75 | }
|
---|
| 76 |
|
---|
[3f74275] | 77 | int cap_alloc(task_t *task)
|
---|
[7e3826d9] | 78 | {
|
---|
[3f74275] | 79 | int handle;
|
---|
[7e3826d9] | 80 |
|
---|
| 81 | irq_spinlock_lock(&task->lock, true);
|
---|
[3f74275] | 82 | for (handle = 0; handle < MAX_CAPS; handle++) {
|
---|
| 83 | cap_t *cap = &task->caps[handle];
|
---|
| 84 | if (cap->type > CAP_TYPE_ALLOCATED) {
|
---|
| 85 | if (cap->can_reclaim && cap->can_reclaim(cap))
|
---|
| 86 | cap_initialize(cap);
|
---|
[05ffb41] | 87 | }
|
---|
[3f74275] | 88 | if (cap->type == CAP_TYPE_INVALID) {
|
---|
| 89 | cap->type = CAP_TYPE_ALLOCATED;
|
---|
[7e3826d9] | 90 | irq_spinlock_unlock(&task->lock, true);
|
---|
[3f74275] | 91 | return handle;
|
---|
[7e3826d9] | 92 | }
|
---|
| 93 | }
|
---|
| 94 | irq_spinlock_unlock(&task->lock, true);
|
---|
| 95 |
|
---|
[e9d15d9] | 96 | return ELIMIT;
|
---|
[7e3826d9] | 97 | }
|
---|
| 98 |
|
---|
[3f74275] | 99 | void cap_free(task_t *task, int handle)
|
---|
[7e3826d9] | 100 | {
|
---|
[3f74275] | 101 | assert(handle >= 0);
|
---|
| 102 | assert(handle < MAX_CAPS);
|
---|
| 103 | assert(task->caps[handle].type != CAP_TYPE_INVALID);
|
---|
[7e3826d9] | 104 |
|
---|
| 105 | irq_spinlock_lock(&task->lock, true);
|
---|
[3f74275] | 106 | cap_initialize(&task->caps[handle]);
|
---|
[7e3826d9] | 107 | irq_spinlock_unlock(&task->lock, true);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[3f74275] | 110 | int cap_get_handle(task_t *task, cap_t *cap)
|
---|
[e7ac23d0] | 111 | {
|
---|
[3f74275] | 112 | return cap - task->caps;
|
---|
[e7ac23d0] | 113 | }
|
---|
| 114 |
|
---|
[7e3826d9] | 115 | /** @}
|
---|
| 116 | */
|
---|