paul@0 | 1 | /* |
paul@0 | 2 | * Data space allocation utility functions. |
paul@0 | 3 | * |
paul@142 | 4 | * Copyright (C) 2018, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/re/dataspace> |
paul@0 | 23 | #include <l4/re/env> |
paul@0 | 24 | #include <l4/re/rm> |
paul@0 | 25 | #include <l4/re/util/cap_alloc> |
paul@0 | 26 | |
paul@0 | 27 | #include "dataspace.h" |
paul@0 | 28 | |
paul@0 | 29 | /* |
paul@0 | 30 | Allocate and map a region containing data, using the given mem_size to indicate |
paul@0 | 31 | the required region size, along with a pointer to be set to the allocated |
paul@0 | 32 | region. Return a capability which may be invalid if allocation or mapping |
paul@0 | 33 | failed. |
paul@0 | 34 | */ |
paul@0 | 35 | |
paul@0 | 36 | L4::Cap<L4Re::Dataspace> allocate_data(l4_size_t mem_size, void **allocated) |
paul@0 | 37 | { |
paul@0 | 38 | /* Obtain a capability for the allocated memory. */ |
paul@0 | 39 | |
paul@0 | 40 | L4::Cap<L4Re::Dataspace> mem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>(); |
paul@0 | 41 | |
paul@0 | 42 | if (!mem.is_valid()) |
paul@0 | 43 | return L4::Cap<L4Re::Dataspace>::Invalid; |
paul@0 | 44 | |
paul@0 | 45 | /* Allocate memory for the data. */ |
paul@0 | 46 | |
paul@0 | 47 | if (L4Re::Env::env()->mem_alloc()->alloc(mem_size, mem, L4Re::Mem_alloc::Continuous)) |
paul@0 | 48 | { |
paul@0 | 49 | L4Re::Util::cap_alloc.free(mem); |
paul@0 | 50 | return L4::Cap<L4Re::Dataspace>::Invalid; |
paul@0 | 51 | } |
paul@0 | 52 | |
paul@0 | 53 | /* Map the memory to a virtual address. */ |
paul@0 | 54 | |
paul@142 | 55 | if (L4Re::Env::env()->rm()->attach(allocated, mem->size(), L4Re::Rm::F::Search_addr, |
paul@0 | 56 | L4::Ipc::make_cap_rw(mem))) |
paul@0 | 57 | { |
paul@0 | 58 | L4Re::Util::cap_alloc.free(mem); |
paul@0 | 59 | return L4::Cap<L4Re::Dataspace>::Invalid; |
paul@0 | 60 | } |
paul@0 | 61 | |
paul@0 | 62 | return mem; |
paul@0 | 63 | } |