1 /* 2 * Memory sharing abstractions. 3 * 4 * Copyright (C) 2018, 2019, 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/c/mem_alloc.h> 23 #include <l4/sys/err.h> 24 #include <l4/sys/types.h> 25 26 #include "cap_alloc.h" 27 #include "mem_ipc.h" 28 #include "semaphore.h" 29 30 31 32 /* Semaphore used if threading has been initialised by ipc_mem_init. */ 33 34 l4_cap_idx_t ipc_mem_semaphore = L4_INVALID_CAP; 35 36 37 38 /* Generic memory allocation. */ 39 40 long ipc_allocate(unsigned long size, void **addr, l4re_ds_t *ds) 41 { 42 return ipc_allocate_align(size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, 43 L4_PAGESHIFT, addr, ds); 44 } 45 46 long ipc_allocate_align(unsigned long size, l4re_rm_flags_t flags, 47 unsigned char align, void **addr, l4re_ds_t *ds) 48 { 49 /* Allocate and attach the memory for the dataspace. */ 50 51 if (ipc_new_dataspace(size, 0, align, ds)) 52 { 53 ipc_cap_free_um(*ds); 54 return -L4_ENOMEM; 55 } 56 57 if (ipc_attach_dataspace_align(*ds, size, flags, align, addr)) 58 { 59 ipc_cap_free_um(*ds); 60 return -L4_ENOMEM; 61 } 62 63 return L4_EOK; 64 } 65 66 /* Create a dataspace. Equivalent to l4re_ma_alloc_align. */ 67 68 long ipc_new_dataspace(l4_mword_t size, l4_umword_t flags, l4_umword_t align, 69 l4re_ds_t *ds) 70 { 71 /* Allocate a capability for the dataspace. */ 72 73 *ds = ipc_cap_alloc(); 74 75 if (l4_is_invalid_cap(*ds)) 76 return -L4_ENOENT; 77 78 return l4re_ma_alloc_align(size, *ds, flags, align); 79 } 80 81 /* Attach a dataspace region. Similar to l4re_rm_attach. */ 82 83 long ipc_attach_dataspace(l4re_ds_t ds, unsigned long size, void **addr) 84 { 85 return ipc_attach_dataspace_align(ds, size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, 86 L4_PAGESHIFT, addr); 87 } 88 89 long ipc_attach_dataspace_align(l4re_ds_t ds, unsigned long size, 90 l4re_rm_flags_t flags, unsigned char align, 91 void **addr) 92 { 93 long err; 94 95 err = ipc_semaphore_down(ipc_mem_semaphore); 96 if (err) 97 return err; 98 99 /* The region manager has changed, requiring rights flags for successful use 100 of the mapped region. */ 101 102 err = l4re_rm_attach(addr, size, flags, ds, 0, align); 103 104 ipc_semaphore_up(ipc_mem_semaphore); 105 106 return err; 107 } 108 109 /* Detach a dataspace region. Equivalent to l4re_rm_detach_unmap. */ 110 111 long ipc_detach_dataspace(void *addr) 112 { 113 long err; 114 115 err = ipc_semaphore_down(ipc_mem_semaphore); 116 if (err) 117 return err; 118 119 err = l4re_rm_detach_unmap((l4_addr_t) addr, L4RE_THIS_TASK_CAP); 120 121 ipc_semaphore_up(ipc_mem_semaphore); 122 123 return err; 124 } 125 126 /* Obtain a dataspace size. Equivalent to l4re_ds_size. */ 127 128 long ipc_dataspace_size(l4_cap_idx_t cap, unsigned long *size) 129 { 130 *size = l4re_ds_size(cap); 131 return L4_EOK; 132 } 133 134 // vim: tabstop=2 expandtab shiftwidth=2