paul@181 | 1 | /* |
paul@181 | 2 | * Capability allocation and management. |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 2019, 2021 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@181 | 23 | |
paul@181 | 24 | #include "cap_alloc.h" |
paul@181 | 25 | #include "semaphore.h" |
paul@181 | 26 | |
paul@181 | 27 | |
paul@181 | 28 | |
paul@181 | 29 | /* Semaphore used if threading has been initialised by ipc_cap_alloc_init. */ |
paul@181 | 30 | |
paul@181 | 31 | l4_cap_idx_t ipc_cap_alloc_semaphore = L4_INVALID_CAP; |
paul@181 | 32 | |
paul@181 | 33 | |
paul@181 | 34 | |
paul@181 | 35 | /* Reserve and return a capability index. */ |
paul@181 | 36 | |
paul@181 | 37 | l4_cap_idx_t ipc_cap_alloc() |
paul@181 | 38 | { |
paul@181 | 39 | l4_cap_idx_t cap; |
paul@181 | 40 | long err; |
paul@181 | 41 | |
paul@181 | 42 | /* Allocate a capability in the critical section, then return. */ |
paul@181 | 43 | |
paul@181 | 44 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@181 | 45 | if (err) |
paul@181 | 46 | return L4_INVALID_CAP; |
paul@181 | 47 | |
paul@181 | 48 | cap = l4re_util_cap_alloc(); |
paul@181 | 49 | |
paul@181 | 50 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@181 | 51 | |
paul@181 | 52 | return cap; |
paul@181 | 53 | } |
paul@181 | 54 | |
paul@181 | 55 | /* Release a capability. */ |
paul@181 | 56 | |
paul@181 | 57 | int ipc_cap_free(l4_cap_idx_t cap) |
paul@181 | 58 | { |
paul@181 | 59 | long err; |
paul@181 | 60 | |
paul@181 | 61 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@181 | 62 | if (err) |
paul@181 | 63 | return 0; |
paul@181 | 64 | |
paul@181 | 65 | l4re_util_cap_free(cap); |
paul@181 | 66 | |
paul@181 | 67 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@181 | 68 | |
paul@181 | 69 | return 1; |
paul@181 | 70 | } |
paul@181 | 71 | |
paul@181 | 72 | /* Free the indicated capability and unmap it. */ |
paul@181 | 73 | |
paul@181 | 74 | int ipc_cap_free_um(l4_cap_idx_t cap) |
paul@181 | 75 | { |
paul@181 | 76 | long err; |
paul@181 | 77 | |
paul@181 | 78 | err = ipc_semaphore_down(ipc_cap_alloc_semaphore); |
paul@181 | 79 | if (err) |
paul@181 | 80 | return 0; |
paul@181 | 81 | |
paul@181 | 82 | l4re_util_cap_free_um(cap); |
paul@181 | 83 | |
paul@181 | 84 | ipc_semaphore_up(ipc_cap_alloc_semaphore); |
paul@181 | 85 | |
paul@181 | 86 | return 1; |
paul@181 | 87 | } |
paul@181 | 88 | |
paul@181 | 89 | // vim: tabstop=2 expandtab shiftwidth=2 |