paul@181 | 1 | /* |
paul@181 | 2 | * Capability allocation and management. |
paul@181 | 3 | * |
paul@310 | 4 | * Copyright (C) 2019, 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@181 | 5 | * |
paul@181 | 6 | * This program is free software; you can redistribute it and/or |
paul@181 | 7 | * modify it under the terms of the GNU General Public License as |
paul@181 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@181 | 9 | * the License, or (at your option) any later version. |
paul@181 | 10 | * |
paul@181 | 11 | * This program is distributed in the hope that it will be useful, |
paul@181 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@181 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@181 | 14 | * GNU General Public License for more details. |
paul@181 | 15 | * |
paul@181 | 16 | * You should have received a copy of the GNU General Public License |
paul@181 | 17 | * along with this program; if not, write to the Free Software |
paul@181 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@181 | 19 | * Boston, MA 02110-1301, USA |
paul@181 | 20 | */ |
paul@181 | 21 | |
paul@181 | 22 | #include <l4/re/c/util/cap_alloc.h> |
paul@310 | 23 | #include <l4/sys/err.h> |
paul@181 | 24 | |
paul@181 | 25 | #include "cap_alloc.h" |
paul@181 | 26 | #include "semaphore.h" |
paul@181 | 27 | |
paul@181 | 28 | |
paul@181 | 29 | |
paul@181 | 30 | /* Semaphore used if threading has been initialised by ipc_cap_alloc_init. */ |
paul@181 | 31 | |
paul@181 | 32 | l4_cap_idx_t ipc_cap_alloc_semaphore = L4_INVALID_CAP; |
paul@181 | 33 | |
paul@181 | 34 | |
paul@181 | 35 | |
paul@181 | 36 | /* Reserve and return a capability index. */ |
paul@181 | 37 | |
paul@181 | 38 | l4_cap_idx_t ipc_cap_alloc() |
paul@181 | 39 | { |
paul@181 | 40 | l4_cap_idx_t cap; |
paul@181 | 41 | long err; |
paul@181 | 42 | |
paul@181 | 43 | /* Allocate a capability in the critical section, then return. */ |
paul@181 | 44 | |
paul@181 | 45 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@181 | 46 | if (err) |
paul@181 | 47 | return L4_INVALID_CAP; |
paul@181 | 48 | |
paul@181 | 49 | cap = l4re_util_cap_alloc(); |
paul@181 | 50 | |
paul@181 | 51 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@181 | 52 | |
paul@181 | 53 | return cap; |
paul@181 | 54 | } |
paul@181 | 55 | |
paul@310 | 56 | /* Reserve and return many capability indexes. */ |
paul@310 | 57 | |
paul@310 | 58 | long ipc_cap_alloc_many(l4_cap_idx_t caps[], int count) |
paul@310 | 59 | { |
paul@310 | 60 | int i; |
paul@310 | 61 | long err; |
paul@310 | 62 | |
paul@310 | 63 | /* Allocate a capability in the critical section, then return. */ |
paul@310 | 64 | |
paul@310 | 65 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@310 | 66 | if (err) |
paul@310 | 67 | return err; |
paul@310 | 68 | |
paul@310 | 69 | for (i = 0; i < count; i++) |
paul@310 | 70 | { |
paul@310 | 71 | caps[i] = l4re_util_cap_alloc(); |
paul@310 | 72 | |
paul@310 | 73 | if (l4_is_invalid_cap(caps[i])) |
paul@310 | 74 | { |
paul@310 | 75 | err = -L4_ENOMEM; |
paul@310 | 76 | break; |
paul@310 | 77 | } |
paul@310 | 78 | } |
paul@310 | 79 | |
paul@310 | 80 | /* Free allocated capabilities if not all could be allocated. */ |
paul@310 | 81 | |
paul@310 | 82 | if (err) |
paul@310 | 83 | for (; i >= 0; i--) |
paul@310 | 84 | l4re_util_cap_free(caps[i]); |
paul@310 | 85 | |
paul@310 | 86 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@310 | 87 | |
paul@310 | 88 | return err; |
paul@310 | 89 | } |
paul@310 | 90 | |
paul@181 | 91 | /* Release a capability. */ |
paul@181 | 92 | |
paul@181 | 93 | int ipc_cap_free(l4_cap_idx_t cap) |
paul@181 | 94 | { |
paul@181 | 95 | long err; |
paul@181 | 96 | |
paul@181 | 97 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@181 | 98 | if (err) |
paul@181 | 99 | return 0; |
paul@181 | 100 | |
paul@181 | 101 | l4re_util_cap_free(cap); |
paul@181 | 102 | |
paul@181 | 103 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@181 | 104 | |
paul@181 | 105 | return 1; |
paul@181 | 106 | } |
paul@181 | 107 | |
paul@181 | 108 | /* Free the indicated capability and unmap it. */ |
paul@181 | 109 | |
paul@181 | 110 | int ipc_cap_free_um(l4_cap_idx_t cap) |
paul@181 | 111 | { |
paul@181 | 112 | long err; |
paul@181 | 113 | |
paul@181 | 114 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@181 | 115 | if (err) |
paul@181 | 116 | return 0; |
paul@181 | 117 | |
paul@181 | 118 | l4re_util_cap_free_um(cap); |
paul@181 | 119 | |
paul@181 | 120 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@181 | 121 | |
paul@181 | 122 | return 1; |
paul@181 | 123 | } |
paul@181 | 124 | |
paul@181 | 125 | // vim: tabstop=2 expandtab shiftwidth=2 |