1 /* 2 * Memory sharing abstractions. 3 * 4 * Copyright (C) 2018, 2019, 2021, 2022, 2024 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/re/c/rm.h> /* L4RE_RM_F_* */ 24 #include <l4/sys/err.h> 25 #include <l4/sys/types.h> 26 27 #include "cap_alloc.h" 28 #include "mem_ipc.h" 29 #include "semaphore.h" 30 31 32 33 /* Semaphore used if threading has been initialised by ipc_mem_init. */ 34 35 l4_cap_idx_t ipc_mem_semaphore = L4_INVALID_CAP; 36 37 38 39 /* Generic memory allocation. */ 40 41 long ipc_allocate(unsigned long size, void **addr, l4_cap_idx_t *ds) 42 { 43 return ipc_allocate_align(size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, 44 L4_PAGESHIFT, addr, ds); 45 } 46 47 long ipc_allocate_align(unsigned long size, l4re_rm_flags_t flags, 48 unsigned char align, void **addr, l4_cap_idx_t *ds) 49 { 50 /* Allocate and attach the memory for the dataspace. */ 51 52 if (ipc_new_dataspace(size, 0, align, ds)) 53 { 54 ipc_cap_free_um(*ds); 55 return -L4_ENOMEM; 56 } 57 58 if (ipc_attach_dataspace_align(*ds, size, flags, align, addr)) 59 { 60 ipc_cap_free_um(*ds); 61 return -L4_ENOMEM; 62 } 63 64 return L4_EOK; 65 } 66 67 /* Create a dataspace. Equivalent to l4re_ma_alloc_align. */ 68 69 long ipc_new_dataspace(l4_mword_t size, l4_umword_t flags, l4_umword_t align, 70 l4_cap_idx_t *ds) 71 { 72 /* Allocate a capability for the dataspace. */ 73 74 *ds = ipc_cap_alloc(); 75 76 if (l4_is_invalid_cap(*ds)) 77 return -L4_ENOENT; 78 79 return l4re_ma_alloc_align(size, *ds, flags, align); 80 } 81 82 /* Attach a dataspace region. Similar to l4re_rm_attach. */ 83 84 long ipc_attach_dataspace(l4_cap_idx_t ds, unsigned long size, void **addr) 85 { 86 return ipc_attach_dataspace_align(ds, size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, 87 L4_PAGESHIFT, addr); 88 } 89 90 long ipc_attach_dataspace_align(l4_cap_idx_t ds, unsigned long size, 91 rm_flags_t flags, unsigned char align, 92 void **addr) 93 { 94 long err; 95 96 err = ipc_semaphore_down(ipc_mem_semaphore); 97 if (err) 98 return err; 99 100 /* The region manager has changed, requiring rights flags for successful use 101 of the mapped region. */ 102 103 err = l4re_rm_attach(addr, size, flags, ds, 0, align); 104 105 ipc_semaphore_up(ipc_mem_semaphore); 106 107 return err; 108 } 109 110 /* Detach a dataspace region. Equivalent to l4re_rm_detach_unmap. */ 111 112 long ipc_detach_dataspace(void *addr) 113 { 114 long err; 115 116 err = ipc_semaphore_down(ipc_mem_semaphore); 117 if (err) 118 return err; 119 120 err = l4re_rm_detach_unmap((l4_addr_t) addr, L4RE_THIS_TASK_CAP); 121 122 ipc_semaphore_up(ipc_mem_semaphore); 123 124 return err; 125 } 126 127 /* Obtain a dataspace size. Equivalent to l4re_ds_size. */ 128 129 long ipc_dataspace_size(l4_cap_idx_t cap, unsigned long *size) 130 { 131 *size = l4re_ds_size(cap); 132 return L4_EOK; 133 } 134 135 // vim: tabstop=2 expandtab shiftwidth=2