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 a capability for the dataspace. */ 50 51 *ds = ipc_cap_alloc(); 52 53 if (l4_is_invalid_cap(*ds)) 54 return -L4_ENOENT; 55 56 /* Allocate and attach the memory for the dataspace. */ 57 58 if (ipc_new_dataspace(*ds, size, 0, align)) 59 { 60 ipc_cap_free_um(*ds); 61 return -L4_ENOMEM; 62 } 63 64 if (ipc_attach_dataspace_align(*ds, size, flags, align, addr)) 65 { 66 ipc_cap_free_um(*ds); 67 return -L4_ENOMEM; 68 } 69 70 return L4_EOK; 71 } 72 73 /* Create a dataspace. Equivalent to l4re_ma_alloc_align. */ 74 75 long ipc_new_dataspace(l4_cap_idx_t cap, l4_mword_t size, l4_umword_t flags, l4_umword_t align) 76 { 77 return l4re_ma_alloc_align(size, cap, flags, align); 78 } 79 80 /* Attach a dataspace region. Similar to l4re_rm_attach. */ 81 82 long ipc_attach_dataspace(l4re_ds_t ds, unsigned long size, void **addr) 83 { 84 return ipc_attach_dataspace_align(ds, size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, 85 L4_PAGESHIFT, addr); 86 } 87 88 long ipc_attach_dataspace_align(l4re_ds_t ds, unsigned long size, 89 l4re_rm_flags_t flags, unsigned char align, 90 void **addr) 91 { 92 long err; 93 94 err = ipc_semaphore_down(ipc_mem_semaphore); 95 if (err) 96 return err; 97 98 /* The region manager has changed, requiring rights flags for successful use 99 of the mapped region. */ 100 101 err = l4re_rm_attach(addr, size, flags, ds, 0, align); 102 103 ipc_semaphore_up(ipc_mem_semaphore); 104 105 return err; 106 } 107 108 /* Detach a dataspace region. Equivalent to l4re_rm_detach_unmap. */ 109 110 long ipc_detach_dataspace(void *addr) 111 { 112 long err; 113 114 err = ipc_semaphore_down(ipc_mem_semaphore); 115 if (err) 116 return err; 117 118 err = l4re_rm_detach_unmap((l4_addr_t) addr, L4RE_THIS_TASK_CAP); 119 120 ipc_semaphore_up(ipc_mem_semaphore); 121 122 return err; 123 } 124 125 /* Obtain a dataspace size. Equivalent to l4re_ds_size. */ 126 127 long ipc_dataspace_size(l4_cap_idx_t cap, unsigned long *size) 128 { 129 *size = l4re_ds_size(cap); 130 return L4_EOK; 131 } 132 133 // vim: tabstop=2 expandtab shiftwidth=2