paul@181 | 1 | /* |
paul@181 | 2 | * Interprocess communication abstractions. |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 2018, 2019, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@181 | 5 | * |
paul@181 | 6 | * This program is free software; you can redistribute it and/or |
paul@181 | 7 | * modify it under the terms of the GNU General Public License as |
paul@181 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@181 | 9 | * the License, or (at your option) any later version. |
paul@181 | 10 | * |
paul@181 | 11 | * This program is distributed in the hope that it will be useful, |
paul@181 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@181 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@181 | 14 | * GNU General Public License for more details. |
paul@181 | 15 | * |
paul@181 | 16 | * You should have received a copy of the GNU General Public License |
paul@181 | 17 | * along with this program; if not, write to the Free Software |
paul@181 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@181 | 19 | * Boston, MA 02110-1301, USA |
paul@181 | 20 | */ |
paul@181 | 21 | |
paul@181 | 22 | #include <l4/re/env.h> |
paul@181 | 23 | #include <l4/util/util.h> |
paul@181 | 24 | #include <l4/sys/factory.h> |
paul@181 | 25 | #include <l4/sys/irq.h> |
paul@181 | 26 | |
paul@181 | 27 | #include "cap_alloc.h" |
paul@181 | 28 | #include "mem_ipc.h" |
paul@181 | 29 | #include "util_ipc.h" |
paul@181 | 30 | |
paul@181 | 31 | |
paul@181 | 32 | |
paul@181 | 33 | /* Declare expected capabilities. */ |
paul@181 | 34 | |
paul@181 | 35 | long _expect_capabilities(l4_buf_regs_t *bregs, int number) |
paul@181 | 36 | { |
paul@181 | 37 | int i; |
paul@181 | 38 | long err; |
paul@181 | 39 | |
paul@181 | 40 | for (i = 0; i < number; i++) |
paul@181 | 41 | { |
paul@181 | 42 | err = _expect_capability(bregs, i); |
paul@181 | 43 | if (err) |
paul@181 | 44 | return err; |
paul@181 | 45 | } |
paul@181 | 46 | |
paul@181 | 47 | return L4_EOK; |
paul@181 | 48 | } |
paul@181 | 49 | |
paul@181 | 50 | /* Indicate that a capability is expected at the given position. */ |
paul@181 | 51 | |
paul@181 | 52 | long _expect_capability(l4_buf_regs_t *bregs, int item) |
paul@181 | 53 | { |
paul@181 | 54 | l4_cap_idx_t future = ipc_cap_alloc(); |
paul@181 | 55 | |
paul@181 | 56 | if (l4_is_invalid_cap(future)) |
paul@181 | 57 | return -L4_ENOENT; |
paul@181 | 58 | |
paul@181 | 59 | /* Indicate the expectation of a capability in return. */ |
paul@181 | 60 | |
paul@181 | 61 | bregs->bdr = 0; |
paul@181 | 62 | bregs->br[item] = L4_RCV_ITEM_SINGLE_CAP | future; |
paul@181 | 63 | |
paul@181 | 64 | return L4_EOK; |
paul@181 | 65 | } |
paul@181 | 66 | |
paul@181 | 67 | /* Export in the message at the given position the given capability. */ |
paul@181 | 68 | |
paul@181 | 69 | void _export_capability(l4_msgtag_t tag, l4_msg_regs_t *mregs, int item, l4_cap_idx_t ref) |
paul@181 | 70 | { |
paul@181 | 71 | int words = l4_msgtag_words(tag); |
paul@181 | 72 | |
paul@181 | 73 | mregs->mr[words + item * 2] = 0 | L4_ITEM_MAP; |
paul@181 | 74 | mregs->mr[words + item * 2 + 1] = l4_obj_fpage(ref, 0, L4_FPAGE_RWX).raw; |
paul@181 | 75 | } |
paul@181 | 76 | |
paul@181 | 77 | /* Export a flexpage at the given position in the message. Here, the snd_base |
paul@181 | 78 | member of the flexpage structure is used to hold the "hot spot" value. */ |
paul@181 | 79 | |
paul@181 | 80 | void _export_fpage(l4_msgtag_t tag, l4_msg_regs_t *mregs, int item, l4_snd_fpage_t fpage) |
paul@181 | 81 | { |
paul@181 | 82 | int words = l4_msgtag_words(tag); |
paul@181 | 83 | |
paul@181 | 84 | mregs->mr[words + item * 2] = l4_map_control(fpage.snd_base, L4_FPAGE_CACHEABLE, 0); |
paul@181 | 85 | mregs->mr[words + item * 2 + 1] = fpage.fpage.raw; |
paul@181 | 86 | } |
paul@181 | 87 | |
paul@181 | 88 | /* Export a flexpage at the given position in the message. */ |
paul@181 | 89 | |
paul@181 | 90 | void _export_page(l4_msgtag_t tag, l4_msg_regs_t *mregs, int item, l4_umword_t hot_spot, l4_fpage_t fpage) |
paul@181 | 91 | { |
paul@181 | 92 | int words = l4_msgtag_words(tag); |
paul@181 | 93 | |
paul@181 | 94 | mregs->mr[words + item * 2] = l4_map_control(hot_spot, L4_FPAGE_CACHEABLE, 0); |
paul@181 | 95 | mregs->mr[words + item * 2 + 1] = fpage.raw; |
paul@181 | 96 | } |
paul@181 | 97 | |
paul@181 | 98 | /* Free expected capabilities held by the buffer registers. */ |
paul@181 | 99 | |
paul@181 | 100 | void _free_expected_capabilities(l4_buf_regs_t *bregs, int number) |
paul@181 | 101 | { |
paul@181 | 102 | int i; |
paul@181 | 103 | l4_cap_idx_t cap; |
paul@181 | 104 | |
paul@181 | 105 | for (i = 0; i < number; i++) |
paul@181 | 106 | { |
paul@181 | 107 | cap = bregs->br[i] & L4_CAP_MASK; |
paul@181 | 108 | if (l4_is_valid_cap(cap)) |
paul@181 | 109 | ipc_cap_free_um(cap); |
paul@181 | 110 | } |
paul@181 | 111 | } |
paul@181 | 112 | |
paul@181 | 113 | /* Import from the message the capability at the given item position. */ |
paul@181 | 114 | |
paul@181 | 115 | long _import_capability(l4_msgtag_t tag, l4_buf_regs_t *bregs, |
paul@181 | 116 | l4_msg_regs_t *mregs, int item, l4_cap_idx_t *ref) |
paul@181 | 117 | { |
paul@181 | 118 | l4_snd_fpage_t fpage; |
paul@181 | 119 | long err = _import_fpage(tag, bregs, mregs, item, &fpage); |
paul@181 | 120 | |
paul@181 | 121 | if (err) |
paul@181 | 122 | return err; |
paul@181 | 123 | |
paul@181 | 124 | /* Check for a received capability. */ |
paul@181 | 125 | |
paul@181 | 126 | if ((fpage.snd_base & 0x3e) != 0x38) |
paul@181 | 127 | return -L4_EIO; |
paul@181 | 128 | |
paul@181 | 129 | /* Update the supplied capability index. */ |
paul@181 | 130 | |
paul@181 | 131 | *ref = bregs->br[item] & L4_CAP_MASK; |
paul@181 | 132 | |
paul@181 | 133 | return L4_EOK; |
paul@181 | 134 | } |
paul@181 | 135 | |
paul@181 | 136 | /* Import from the message a dataspace, mapping it to an address. */ |
paul@181 | 137 | |
paul@181 | 138 | long _import_dataspace(l4_msgtag_t tag, l4_buf_regs_t *bregs, |
paul@181 | 139 | l4_msg_regs_t *mregs, int item, l4re_ds_t *mem, |
paul@181 | 140 | l4_addr_t *addr) |
paul@181 | 141 | { |
paul@181 | 142 | long err; |
paul@181 | 143 | unsigned long size; |
paul@181 | 144 | |
paul@181 | 145 | *mem = ipc_cap_alloc(); |
paul@181 | 146 | |
paul@181 | 147 | err = _import_capability(tag, bregs, mregs, item, mem); |
paul@181 | 148 | |
paul@181 | 149 | if (err) |
paul@181 | 150 | { |
paul@181 | 151 | ipc_cap_free_um(*mem); |
paul@181 | 152 | return err; |
paul@181 | 153 | } |
paul@181 | 154 | |
paul@181 | 155 | /* Attach the dataspace to a region. */ |
paul@181 | 156 | |
paul@181 | 157 | err = ipc_dataspace_size(*mem, &size); |
paul@181 | 158 | if (err) |
paul@181 | 159 | { |
paul@181 | 160 | ipc_cap_free_um(*mem); |
paul@181 | 161 | return err; |
paul@181 | 162 | } |
paul@181 | 163 | |
paul@181 | 164 | err = ipc_attach_dataspace(*mem, size, (void **) addr); |
paul@181 | 165 | if (err) |
paul@181 | 166 | { |
paul@181 | 167 | ipc_cap_free_um(*mem); |
paul@181 | 168 | return err; |
paul@181 | 169 | } |
paul@181 | 170 | |
paul@181 | 171 | return L4_EOK; |
paul@181 | 172 | } |
paul@181 | 173 | |
paul@181 | 174 | /* Import from the message the flexpage at the given item position. */ |
paul@181 | 175 | |
paul@181 | 176 | long _import_fpage(l4_msgtag_t tag, l4_buf_regs_t *bregs, |
paul@181 | 177 | l4_msg_regs_t *mregs, int item, l4_snd_fpage_t *fpage) |
paul@181 | 178 | { |
paul@181 | 179 | int words = l4_msgtag_words(tag); |
paul@181 | 180 | |
paul@181 | 181 | /* NOTE: Currently, this function does not interact with the received items, |
paul@181 | 182 | but instead just returns the flexpage details from the message |
paul@181 | 183 | words. */ |
paul@181 | 184 | |
paul@181 | 185 | (void) bregs; |
paul@181 | 186 | |
paul@181 | 187 | /* Obtain the item details for the capability. */ |
paul@181 | 188 | |
paul@181 | 189 | if (item >= (int) l4_msgtag_items(tag)) |
paul@181 | 190 | return -L4_EIO; |
paul@181 | 191 | |
paul@181 | 192 | /* Received items should start at zero. */ |
paul@181 | 193 | |
paul@181 | 194 | fpage->snd_base = mregs->mr[words + item * 2]; |
paul@181 | 195 | fpage->fpage.raw = mregs->mr[words + item * 2 + 1]; |
paul@181 | 196 | |
paul@181 | 197 | return L4_EOK; |
paul@181 | 198 | } |
paul@181 | 199 | |
paul@181 | 200 | |
paul@181 | 201 | |
paul@181 | 202 | /* Convenience operations. */ |
paul@181 | 203 | |
paul@181 | 204 | /* Send a reply to a call using the given message tag. */ |
paul@181 | 205 | |
paul@181 | 206 | l4_msgtag_t util_ipc_reply(l4_msgtag_t tag) |
paul@181 | 207 | { |
paul@181 | 208 | return l4_ipc_send(L4_INVALID_CAP | L4_SYSF_REPLY, l4_utcb(), tag, L4_IPC_NEVER); |
paul@181 | 209 | } |
paul@181 | 210 | |
paul@181 | 211 | /* Send a request using a call with the given message tag to the indicated |
paul@181 | 212 | endpoint. */ |
paul@181 | 213 | |
paul@181 | 214 | l4_msgtag_t util_ipc_request(l4_msgtag_t tag, l4_cap_idx_t endpoint) |
paul@181 | 215 | { |
paul@181 | 216 | return l4_ipc_call(endpoint, l4_utcb(), tag, L4_IPC_NEVER); |
paul@181 | 217 | } |
paul@181 | 218 | |
paul@181 | 219 | /* Send data with the given message tag to the indicated endpoint. */ |
paul@181 | 220 | |
paul@181 | 221 | l4_msgtag_t util_ipc_send(l4_msgtag_t tag, l4_cap_idx_t endpoint) |
paul@181 | 222 | { |
paul@181 | 223 | return l4_ipc_send(endpoint, l4_utcb(), tag, L4_IPC_NEVER); |
paul@181 | 224 | } |