1 /* 2 * Data space allocation utility functions. 3 * 4 * Copyright (C) 2018, 2023 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/dataspace> 23 #include <l4/re/env> 24 #include <l4/re/rm> 25 #include <l4/re/util/cap_alloc> 26 27 #include "dataspace.h" 28 29 /* 30 Allocate and map a region containing data, using the given mem_size to indicate 31 the required region size, along with a pointer to be set to the allocated 32 region. Return a capability which may be invalid if allocation or mapping 33 failed. 34 */ 35 36 L4::Cap<L4Re::Dataspace> allocate_data(l4_size_t mem_size, void **allocated) 37 { 38 /* Obtain a capability for the allocated memory. */ 39 40 L4::Cap<L4Re::Dataspace> mem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); 41 42 if (!mem.is_valid()) 43 return L4::Cap<L4Re::Dataspace>::Invalid; 44 45 /* Allocate memory for the data. */ 46 47 if (L4Re::Env::env()->mem_alloc()->alloc(mem_size, mem, L4Re::Mem_alloc::Continuous)) 48 { 49 L4Re::Util::cap_alloc.free(mem); 50 return L4::Cap<L4Re::Dataspace>::Invalid; 51 } 52 53 /* Map the memory to a virtual address. */ 54 55 if (L4Re::Env::env()->rm()->attach(allocated, mem->size(), 56 L4Re::Rm::F::Search_addr | L4Re::Rm::F::RW, 57 L4::Ipc::make_cap_rw(mem))) 58 { 59 L4Re::Util::cap_alloc.free(mem); 60 return L4::Cap<L4Re::Dataspace>::Invalid; 61 } 62 63 return mem; 64 }