1 /* 2 * Memory sharing abstractions. 3 * 4 * Copyright (C) 2018, 2019, 2021 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> 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, l4re_ds_t *ds) 42 { 43 /* Allocate a capability for the dataspace. */ 44 45 *ds = ipc_cap_alloc(); 46 47 if (l4_is_invalid_cap(*ds)) 48 return -L4_ENOENT; 49 50 /* Allocate and attach the memory for the dataspace. */ 51 52 if (ipc_new_dataspace(*ds, size, 0, 0)) 53 { 54 ipc_cap_free_um(*ds); 55 return -L4_ENOMEM; 56 } 57 58 if (ipc_attach_dataspace(*ds, size, 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_cap_idx_t cap, l4_mword_t size, l4_umword_t flags, l4_umword_t align) 70 { 71 return l4re_ma_alloc_align(size, cap, flags, align); 72 } 73 74 /* Attach a dataspace region. Similar to l4re_rm_attach. */ 75 76 long ipc_attach_dataspace(l4re_ds_t ds, unsigned long size, void **addr) 77 { 78 long err; 79 80 err = ipc_semaphore_down(ipc_mem_semaphore); 81 if (err) 82 return err; 83 84 /* The region manager has changed, requiring rights flags for successful use 85 of the mapped region. */ 86 87 err = l4re_rm_attach(addr, size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, ds, 0, 88 L4_PAGESHIFT); 89 90 ipc_semaphore_up(ipc_mem_semaphore); 91 92 return err; 93 } 94 95 /* Detach a dataspace region. Equivalent to l4re_rm_detach_unmap. */ 96 97 long ipc_detach_dataspace(void *addr) 98 { 99 long err; 100 101 err = ipc_semaphore_down(ipc_mem_semaphore); 102 if (err) 103 return err; 104 105 err = l4re_rm_detach_unmap((l4_addr_t) addr, L4RE_THIS_TASK_CAP); 106 107 ipc_semaphore_up(ipc_mem_semaphore); 108 109 return err; 110 } 111 112 /* Obtain a dataspace size. Equivalent to l4re_ds_size. */ 113 114 long ipc_dataspace_size(l4_cap_idx_t cap, unsigned long *size) 115 { 116 *size = l4re_ds_size(cap); 117 return L4_EOK; 118 } 119 120 // vim: tabstop=2 expandtab shiftwidth=2