paul@181 | 1 | /* |
paul@181 | 2 | * Interprocess communication message abstraction. |
paul@181 | 3 | * |
paul@620 | 4 | * Copyright (C) 2018, 2019, 2021-2024 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 <string.h> |
paul@181 | 23 | |
paul@181 | 24 | #include "cap_alloc.h" |
paul@181 | 25 | #include "message.h" |
paul@181 | 26 | #include "mem_ipc.h" |
paul@181 | 27 | #include "util_ipc.h" |
paul@181 | 28 | |
paul@181 | 29 | |
paul@181 | 30 | |
paul@181 | 31 | /* Calculate the given length rounded up to whole word increments. */ |
paul@181 | 32 | |
paul@181 | 33 | static size_t get_size_in_words(size_t length) |
paul@181 | 34 | { |
paul@181 | 35 | size_t mask = sizeof(l4_umword_t) - 1; |
paul@181 | 36 | return (length + mask) & ~mask; |
paul@181 | 37 | } |
paul@181 | 38 | |
paul@181 | 39 | |
paul@181 | 40 | |
paul@181 | 41 | /* Message operations. */ |
paul@181 | 42 | |
paul@181 | 43 | /* Discard capabilities for transfer to other tasks. */ |
paul@181 | 44 | |
paul@181 | 45 | void ipc_message_discard(ipc_message_t *msg) |
paul@181 | 46 | { |
paul@181 | 47 | unsigned int i; |
paul@181 | 48 | |
paul@181 | 49 | for (i = 0; i < msg->discarded_items; i++) |
paul@181 | 50 | ipc_cap_free_um(msg->to_discard[i]); |
paul@181 | 51 | } |
paul@181 | 52 | |
paul@596 | 53 | /* Initialise a message structure with the given number of expected items. This |
paul@596 | 54 | function is primarily for the use of servers needing to reserve items that |
paul@596 | 55 | might be sent in messages from clients. */ |
paul@181 | 56 | |
paul@181 | 57 | long ipc_message_expect(ipc_message_t *msg, unsigned int expected_items) |
paul@181 | 58 | { |
paul@350 | 59 | return ipc_message_expect_capabilities(msg, expected_items); |
paul@181 | 60 | } |
paul@181 | 61 | |
paul@181 | 62 | /* Free capabilities expected in messages. */ |
paul@181 | 63 | |
paul@181 | 64 | void ipc_message_free(ipc_message_t *msg) |
paul@181 | 65 | { |
paul@620 | 66 | _free_expected_capabilities(&msg->bregs, msg->import_item, msg->expected_items); |
paul@181 | 67 | } |
paul@181 | 68 | |
paul@181 | 69 | /* Clear message attributes for sending and response handling. */ |
paul@181 | 70 | |
paul@181 | 71 | void ipc_message_new(ipc_message_t *msg) |
paul@181 | 72 | { |
paul@181 | 73 | /* Set a default for expected items. */ |
paul@181 | 74 | |
paul@181 | 75 | msg->expected_items = 0; |
paul@351 | 76 | msg->receive_items = 0; |
paul@181 | 77 | ipc_message_reset(msg); |
paul@181 | 78 | } |
paul@181 | 79 | |
paul@181 | 80 | /* Open an incoming message by resetting sending state and preserving the |
paul@181 | 81 | message registers plus the buffer registers for the expected number of |
paul@181 | 82 | items. */ |
paul@181 | 83 | |
paul@181 | 84 | void ipc_message_open(ipc_message_t *msg) |
paul@181 | 85 | { |
paul@181 | 86 | ipc_message_reset(msg); |
paul@181 | 87 | ipc_message_preserve_buffer_registers(msg); |
paul@181 | 88 | ipc_message_preserve_message_registers(msg); |
paul@181 | 89 | } |
paul@181 | 90 | |
paul@181 | 91 | /* Preserve the buffer registers. */ |
paul@181 | 92 | |
paul@181 | 93 | void ipc_message_preserve_buffer_registers(ipc_message_t *msg) |
paul@181 | 94 | { |
paul@181 | 95 | unsigned int i; |
paul@181 | 96 | |
paul@181 | 97 | /* Reference the UTCB virtual registers. */ |
paul@181 | 98 | |
paul@181 | 99 | l4_buf_regs_t *bregs = l4_utcb_br(); |
paul@181 | 100 | |
paul@181 | 101 | msg->bregs.bdr = bregs->bdr; |
paul@181 | 102 | |
paul@351 | 103 | /* NOTE: Support a mixture of expected items and received flexpages. */ |
paul@351 | 104 | |
paul@351 | 105 | if ((msg->expected_items) || (msg->receive_items)) |
paul@351 | 106 | { |
paul@351 | 107 | for (i = 0; (i < msg->expected_items) || (i < msg->receive_items * 2); i++) |
paul@351 | 108 | msg->bregs.br[i] = bregs->br[i]; |
paul@351 | 109 | } |
paul@181 | 110 | } |
paul@181 | 111 | |
paul@181 | 112 | /* Preserve the message registers. */ |
paul@181 | 113 | |
paul@181 | 114 | void ipc_message_preserve_message_registers(ipc_message_t *msg) |
paul@181 | 115 | { |
paul@181 | 116 | /* Compute the number of message register words affected. */ |
paul@181 | 117 | |
paul@181 | 118 | unsigned int i, affected_words = l4_msgtag_words(msg->tag) + |
paul@181 | 119 | 2 * l4_msgtag_items(msg->tag); |
paul@181 | 120 | |
paul@181 | 121 | /* Reference the UTCB virtual registers. */ |
paul@181 | 122 | |
paul@181 | 123 | l4_msg_regs_t *mregs = l4_utcb_mr(); |
paul@181 | 124 | |
paul@181 | 125 | /* Copy message register information. */ |
paul@181 | 126 | |
paul@181 | 127 | for (i = 0; i < affected_words; i++) |
paul@181 | 128 | msg->mregs.mr[i] = mregs->mr[i]; |
paul@181 | 129 | } |
paul@181 | 130 | |
paul@181 | 131 | /* Restore the message and buffer registers. */ |
paul@181 | 132 | |
paul@181 | 133 | void ipc_message_prepare(ipc_message_t *msg) |
paul@181 | 134 | { |
paul@181 | 135 | ipc_message_restore_buffer_registers(msg); |
paul@181 | 136 | ipc_message_restore_message_registers(msg); |
paul@181 | 137 | } |
paul@181 | 138 | |
paul@181 | 139 | /* Restore buffer registers so that item expectations can be set. */ |
paul@181 | 140 | |
paul@181 | 141 | void ipc_message_restore_buffer_registers(ipc_message_t *msg) |
paul@181 | 142 | { |
paul@181 | 143 | unsigned int i; |
paul@181 | 144 | |
paul@181 | 145 | /* Reference the UTCB virtual registers. */ |
paul@181 | 146 | |
paul@181 | 147 | l4_buf_regs_t *bregs = l4_utcb_br(); |
paul@181 | 148 | |
paul@181 | 149 | /* Restore buffer register information. */ |
paul@181 | 150 | |
paul@181 | 151 | bregs->bdr = msg->bregs.bdr; |
paul@181 | 152 | |
paul@351 | 153 | /* NOTE: Support a mixture of expected items and received flexpages. */ |
paul@351 | 154 | |
paul@351 | 155 | if ((msg->expected_items) || (msg->receive_items)) |
paul@351 | 156 | { |
paul@351 | 157 | for (i = 0; (i < msg->expected_items) || (i < msg->receive_items * 2); i++) |
paul@351 | 158 | bregs->br[i] = msg->bregs.br[i]; |
paul@351 | 159 | } |
paul@181 | 160 | } |
paul@181 | 161 | |
paul@181 | 162 | /* Restore message registers to communicate recorded data and items. */ |
paul@181 | 163 | |
paul@181 | 164 | void ipc_message_restore_message_registers(ipc_message_t *msg) |
paul@181 | 165 | { |
paul@181 | 166 | /* Compute the number of message register words affected. */ |
paul@181 | 167 | |
paul@181 | 168 | unsigned int i, affected_words = msg->words + 2 * msg->items; |
paul@181 | 169 | |
paul@181 | 170 | /* Reference the UTCB virtual registers. */ |
paul@181 | 171 | |
paul@181 | 172 | l4_msg_regs_t *mregs = l4_utcb_mr(); |
paul@181 | 173 | |
paul@181 | 174 | /* Restore message register information. */ |
paul@181 | 175 | |
paul@181 | 176 | for (i = 0; i < affected_words; i++) |
paul@181 | 177 | mregs->mr[i] = msg->mregs.mr[i]; |
paul@181 | 178 | } |
paul@181 | 179 | |
paul@181 | 180 | /* Prepare and send a reply using the message. */ |
paul@181 | 181 | |
paul@181 | 182 | void ipc_message_reply(ipc_message_t *msg) |
paul@181 | 183 | { |
paul@181 | 184 | ipc_message_prepare(msg); |
paul@181 | 185 | msg->tag = util_ipc_reply(ipc_message_reply_tag(msg)); |
paul@181 | 186 | } |
paul@181 | 187 | |
paul@181 | 188 | /* Prepare and send a request using the message, involving the given operation, |
paul@181 | 189 | directed at the indicated endpoint. Open a message for the reply. */ |
paul@181 | 190 | |
paul@181 | 191 | void ipc_message_request(ipc_message_t *msg, int op, l4_cap_idx_t endpoint) |
paul@181 | 192 | { |
paul@181 | 193 | ipc_message_prepare(msg); |
paul@181 | 194 | msg->tag = util_ipc_request(ipc_message_request_tag(msg, op), endpoint); |
paul@181 | 195 | ipc_message_open(msg); |
paul@181 | 196 | } |
paul@181 | 197 | |
paul@181 | 198 | /* Prepare and send a message, involving the given operation, directed at the |
paul@181 | 199 | indicated endpoint. No reply is expected. */ |
paul@181 | 200 | |
paul@181 | 201 | void ipc_message_send(ipc_message_t *msg, int op, l4_cap_idx_t endpoint) |
paul@181 | 202 | { |
paul@181 | 203 | ipc_message_prepare(msg); |
paul@181 | 204 | msg->tag = util_ipc_send(ipc_message_request_tag(msg, op), endpoint); |
paul@181 | 205 | } |
paul@181 | 206 | |
paul@181 | 207 | /* Reset the state of the message for sending purposes. */ |
paul@181 | 208 | |
paul@181 | 209 | void ipc_message_reset(ipc_message_t *msg) |
paul@181 | 210 | { |
paul@181 | 211 | /* Initialise words and items for sending. */ |
paul@181 | 212 | |
paul@181 | 213 | msg->discarded_items = 0; |
paul@181 | 214 | msg->words = 0; |
paul@181 | 215 | msg->items = 0; |
paul@655 | 216 | |
paul@655 | 217 | /* Initialise items to be received. |
paul@655 | 218 | NOTE: This does not support item type mixing. */ |
paul@655 | 219 | |
paul@609 | 220 | msg->import_item = 0; |
paul@655 | 221 | msg->bregs.bdr = 0; |
paul@181 | 222 | |
paul@181 | 223 | /* Message label overriding. */ |
paul@181 | 224 | |
paul@181 | 225 | msg->new_label = 0; |
paul@181 | 226 | |
paul@181 | 227 | /* Server control. */ |
paul@181 | 228 | |
paul@181 | 229 | msg->terminating = 0; |
paul@181 | 230 | } |
paul@181 | 231 | |
paul@181 | 232 | /* Wait for an incoming message. */ |
paul@181 | 233 | |
paul@181 | 234 | void ipc_message_wait(ipc_message_t *msg, l4_umword_t *label) |
paul@181 | 235 | { |
paul@412 | 236 | /* Since no receive flexpages are expected when waiting for incoming messages, |
paul@412 | 237 | the item count is reset to zero. */ |
paul@412 | 238 | |
paul@412 | 239 | msg->receive_items = 0; |
paul@350 | 240 | ipc_message_restore_buffer_registers(msg); |
paul@181 | 241 | msg->tag = l4_ipc_wait(l4_utcb(), label, L4_IPC_NEVER); |
paul@181 | 242 | } |
paul@181 | 243 | |
paul@181 | 244 | |
paul@181 | 245 | |
paul@181 | 246 | /* Add a capability to the message. */ |
paul@181 | 247 | |
paul@181 | 248 | void ipc_message_add_capability(ipc_message_t *msg, l4_cap_idx_t cap) |
paul@181 | 249 | { |
paul@181 | 250 | ipc_message_export_capability(msg, msg->items++, cap); |
paul@372 | 251 | |
paul@372 | 252 | /* NOTE: Might use the "grant" operation instead of explicitly discarding. */ |
paul@372 | 253 | |
paul@181 | 254 | if (cap & IPC_DISCARD_CAP_FLAG) |
paul@181 | 255 | ipc_message_discard_capability(msg, cap & ~IPC_DISCARD_CAP_FLAG); |
paul@181 | 256 | } |
paul@181 | 257 | |
paul@181 | 258 | /* Add an item to the message. */ |
paul@181 | 259 | |
paul@181 | 260 | void ipc_message_add_item(ipc_message_t *msg, l4_cap_idx_t cap) |
paul@181 | 261 | { |
paul@181 | 262 | ipc_message_export_capability(msg, msg->items++, cap); |
paul@372 | 263 | |
paul@372 | 264 | /* NOTE: Might use the "grant" operation instead of explicitly discarding. */ |
paul@372 | 265 | |
paul@181 | 266 | if (cap & IPC_DISCARD_CAP_FLAG) |
paul@181 | 267 | ipc_message_discard_capability(msg, cap & ~IPC_DISCARD_CAP_FLAG); |
paul@181 | 268 | } |
paul@181 | 269 | |
paul@181 | 270 | /* Add a flexpage to the message using a type to combine the "hot spot" and |
paul@181 | 271 | flexpage. */ |
paul@181 | 272 | |
paul@181 | 273 | void ipc_message_add_fpage(ipc_message_t *msg, l4_snd_fpage_t fpage) |
paul@181 | 274 | { |
paul@371 | 275 | /* Test for {0, l4_fpage_invalid()} as the flexpage to avoid setting an |
paul@371 | 276 | optional flexpage result, useful for the page fault handler. */ |
paul@371 | 277 | |
paul@371 | 278 | if (fpage.snd_base || fpage.fpage.raw) |
paul@371 | 279 | ipc_message_export_fpage(msg, msg->items++, fpage); |
paul@181 | 280 | } |
paul@181 | 281 | |
paul@596 | 282 | /* Add the expectation of a capability. Note that a capability is accepted as a |
paul@596 | 283 | parameter but ignored, this being for the convenience of code generation. */ |
paul@596 | 284 | |
paul@596 | 285 | void ipc_message_add_receive_capability(ipc_message_t *msg, l4_cap_idx_t cap) |
paul@596 | 286 | { |
paul@596 | 287 | (void) cap; |
paul@609 | 288 | _expect_capability(&msg->bregs, msg->receive_items++); |
paul@596 | 289 | } |
paul@596 | 290 | |
paul@351 | 291 | /* Add a receive window flexpage item to the message. */ |
paul@351 | 292 | |
paul@351 | 293 | void ipc_message_add_receive_fpage(ipc_message_t *msg, l4_snd_fpage_t fpage) |
paul@351 | 294 | { |
paul@351 | 295 | ipc_message_receive_fpage(msg, msg->receive_items++, fpage); |
paul@351 | 296 | } |
paul@351 | 297 | |
paul@181 | 298 | /* Add a flexpage to the message. */ |
paul@181 | 299 | |
paul@181 | 300 | void ipc_message_add_page(ipc_message_t *msg, l4_umword_t hot_spot, |
paul@181 | 301 | l4_fpage_t fpage) |
paul@181 | 302 | { |
paul@181 | 303 | ipc_message_export_page(msg, msg->items++, hot_spot, fpage); |
paul@181 | 304 | } |
paul@181 | 305 | |
paul@181 | 306 | /* Add a word value to the message. */ |
paul@181 | 307 | |
paul@181 | 308 | void ipc_message_add_word(ipc_message_t *msg, l4_umword_t value) |
paul@181 | 309 | { |
paul@181 | 310 | /* NOTE: Should raise an exception if there are items. */ |
paul@181 | 311 | |
paul@181 | 312 | if (!msg->items) |
paul@181 | 313 | msg->mregs.mr[msg->words++] = value; |
paul@181 | 314 | } |
paul@181 | 315 | |
paul@181 | 316 | /* Add the given dimensioned data to the message. */ |
paul@181 | 317 | |
paul@181 | 318 | void ipc_message_add_data(ipc_message_t *msg, const char *value, size_t length) |
paul@181 | 319 | { |
paul@181 | 320 | void *target = ipc_message_reserve_data(msg, length); |
paul@181 | 321 | |
paul@181 | 322 | memcpy(target, value, length); |
paul@181 | 323 | } |
paul@181 | 324 | |
paul@181 | 325 | /* Add the given null-terminated character string to the message. */ |
paul@181 | 326 | |
paul@181 | 327 | void ipc_message_add_string(ipc_message_t *msg, const char *value) |
paul@181 | 328 | { |
paul@181 | 329 | ipc_message_add_data(msg, value, strlen(value) + 1); |
paul@181 | 330 | } |
paul@181 | 331 | |
paul@181 | 332 | /* Propagate the given item to the message. */ |
paul@181 | 333 | |
paul@181 | 334 | void ipc_message_propagate_item(ipc_message_t *msg, l4_cap_idx_t cap) |
paul@181 | 335 | { |
paul@181 | 336 | ipc_message_propagate_capability(msg, msg->items++, cap); |
paul@181 | 337 | } |
paul@181 | 338 | |
paul@181 | 339 | /* Indicate an error condition in the message. */ |
paul@181 | 340 | |
paul@181 | 341 | void ipc_message_send_error(ipc_message_t *msg, long error) |
paul@181 | 342 | { |
paul@181 | 343 | msg->new_label = error; |
paul@181 | 344 | } |
paul@181 | 345 | |
paul@181 | 346 | /* Return a word value from the message. */ |
paul@181 | 347 | |
paul@181 | 348 | l4_umword_t ipc_message_get_word(ipc_message_t *msg, unsigned int word) |
paul@181 | 349 | { |
paul@181 | 350 | return msg->mregs.mr[word]; |
paul@181 | 351 | } |
paul@181 | 352 | |
paul@181 | 353 | /* Return a pointer to a word in the message. */ |
paul@181 | 354 | |
paul@181 | 355 | l4_umword_t *ipc_message_get_word_address(ipc_message_t *msg, unsigned int word) |
paul@181 | 356 | { |
paul@181 | 357 | return &msg->mregs.mr[word]; |
paul@181 | 358 | } |
paul@181 | 359 | |
paul@181 | 360 | /* Return the number of received items. */ |
paul@181 | 361 | |
paul@181 | 362 | unsigned int ipc_message_number_of_items(ipc_message_t *msg) |
paul@181 | 363 | { |
paul@181 | 364 | return l4_msgtag_items(msg->tag); |
paul@181 | 365 | } |
paul@181 | 366 | |
paul@181 | 367 | /* Return the number of received words. */ |
paul@181 | 368 | |
paul@181 | 369 | unsigned int ipc_message_number_of_words(ipc_message_t *msg) |
paul@181 | 370 | { |
paul@181 | 371 | return l4_msgtag_words(msg->tag); |
paul@181 | 372 | } |
paul@181 | 373 | |
paul@181 | 374 | /* Message tag generation. */ |
paul@181 | 375 | |
paul@181 | 376 | l4_msgtag_t ipc_message_reply_tag(ipc_message_t *msg) |
paul@181 | 377 | { |
paul@181 | 378 | return l4_msgtag(msg->new_label, msg->words, msg->items, 0); |
paul@181 | 379 | } |
paul@181 | 380 | |
paul@181 | 381 | l4_msgtag_t ipc_message_request_tag(ipc_message_t *msg, int op) |
paul@181 | 382 | { |
paul@181 | 383 | return l4_msgtag(op, msg->words, msg->items, 0); |
paul@181 | 384 | } |
paul@181 | 385 | |
paul@181 | 386 | /* Reserve space for dimensioned data, returning a pointer to use to add the |
paul@181 | 387 | data. The length of the data is incorporated into the message. */ |
paul@181 | 388 | |
paul@181 | 389 | void *ipc_message_reserve_data(ipc_message_t *msg, size_t length) |
paul@181 | 390 | { |
paul@181 | 391 | /* Indicate the length of the data. */ |
paul@181 | 392 | |
paul@181 | 393 | ipc_message_add_word(msg, length); |
paul@181 | 394 | |
paul@181 | 395 | /* Reserve the space. */ |
paul@181 | 396 | |
paul@181 | 397 | return ipc_message_reserve_words(msg, length); |
paul@181 | 398 | } |
paul@181 | 399 | |
paul@181 | 400 | /* Reserve space for dimensioned data, returning a pointer to use to add the |
paul@181 | 401 | data. The length is not incorporated into the message. */ |
paul@181 | 402 | |
paul@181 | 403 | void *ipc_message_reserve_words(ipc_message_t *msg, size_t length) |
paul@181 | 404 | { |
paul@181 | 405 | /* Calculate the rounded-up size of the data and the necessary padding. */ |
paul@181 | 406 | |
paul@181 | 407 | size_t size = get_size_in_words(length); |
paul@181 | 408 | size_t padding = size - length; |
paul@181 | 409 | void *target; |
paul@181 | 410 | |
paul@181 | 411 | /* NOTE: Should test the length against the capacity of the message. */ |
paul@181 | 412 | |
paul@181 | 413 | /* Obtain the location of the next word, where the data will be copied. */ |
paul@181 | 414 | |
paul@181 | 415 | target = &msg->mregs.mr[msg->words]; |
paul@181 | 416 | |
paul@181 | 417 | /* Pad the data and update the number of words. */ |
paul@181 | 418 | |
paul@181 | 419 | memset(target + length, 0, padding); |
paul@181 | 420 | msg->words += size / sizeof(l4_umword_t); |
paul@181 | 421 | |
paul@181 | 422 | return target; |
paul@181 | 423 | } |
paul@181 | 424 | |
paul@181 | 425 | |
paul@181 | 426 | |
paul@181 | 427 | /* Discard a capability after replying. */ |
paul@181 | 428 | |
paul@181 | 429 | void ipc_message_discard_capability(ipc_message_t *msg, l4_cap_idx_t cap) |
paul@181 | 430 | { |
paul@181 | 431 | msg->to_discard[msg->discarded_items++] = cap; |
paul@181 | 432 | } |
paul@181 | 433 | |
paul@181 | 434 | /* Discard a dataspace. */ |
paul@181 | 435 | |
paul@639 | 436 | void ipc_message_discard_dataspace(ipc_message_t *msg, l4_cap_idx_t mem, l4_addr_t addr) |
paul@181 | 437 | { |
paul@181 | 438 | ipc_message_discard_capability(msg, mem); |
paul@181 | 439 | ipc_detach_dataspace((void *) addr); |
paul@181 | 440 | } |
paul@181 | 441 | |
paul@181 | 442 | /* Reserve the given number slots from zero for incoming capabilities. */ |
paul@181 | 443 | |
paul@181 | 444 | long ipc_message_expect_capabilities(ipc_message_t *msg, int number) |
paul@181 | 445 | { |
paul@181 | 446 | msg->expected_items = number; |
paul@181 | 447 | return _expect_capabilities(&msg->bregs, number); |
paul@181 | 448 | } |
paul@181 | 449 | |
paul@181 | 450 | /* Reserve a slot for an incoming capability. */ |
paul@181 | 451 | |
paul@181 | 452 | long ipc_message_expect_capability(ipc_message_t *msg, int item) |
paul@181 | 453 | { |
paul@181 | 454 | if (item >= (int) msg->expected_items) |
paul@181 | 455 | msg->expected_items = item + 1; |
paul@181 | 456 | |
paul@181 | 457 | return _expect_capability(&msg->bregs, item); |
paul@181 | 458 | } |
paul@181 | 459 | |
paul@181 | 460 | /* Export a capability at the given position in the message. */ |
paul@181 | 461 | |
paul@181 | 462 | void ipc_message_export_capability(ipc_message_t *msg, int item, l4_cap_idx_t ref) |
paul@181 | 463 | { |
paul@583 | 464 | if (l4_is_valid_cap(ref)) |
paul@583 | 465 | { |
paul@583 | 466 | msg->mregs.mr[msg->words + item * 2] = 0 | L4_ITEM_MAP; |
paul@583 | 467 | msg->mregs.mr[msg->words + item * 2 + 1] = l4_obj_fpage(ref, 0, L4_FPAGE_RWX).raw; |
paul@583 | 468 | } |
paul@583 | 469 | else |
paul@583 | 470 | { |
paul@583 | 471 | msg->mregs.mr[msg->words + item * 2] = 0; |
paul@583 | 472 | msg->mregs.mr[msg->words + item * 2 + 1] = 0; |
paul@583 | 473 | } |
paul@181 | 474 | } |
paul@181 | 475 | |
paul@181 | 476 | /* Export a flexpage at the given position in the message. Here, the snd_base |
paul@181 | 477 | member of the flexpage structure is used to hold the "hot spot" value. */ |
paul@181 | 478 | |
paul@181 | 479 | void ipc_message_export_fpage(ipc_message_t *msg, int item, l4_snd_fpage_t fpage) |
paul@181 | 480 | { |
paul@181 | 481 | msg->mregs.mr[msg->words + item * 2] = l4_map_control(fpage.snd_base, L4_FPAGE_CACHEABLE, 0); |
paul@181 | 482 | msg->mregs.mr[msg->words + item * 2 + 1] = fpage.fpage.raw; |
paul@181 | 483 | } |
paul@181 | 484 | |
paul@181 | 485 | /* Export a flexpage at the given position in the message. */ |
paul@181 | 486 | |
paul@181 | 487 | void ipc_message_export_page(ipc_message_t *msg, int item, l4_umword_t hot_spot, l4_fpage_t fpage) |
paul@181 | 488 | { |
paul@181 | 489 | msg->mregs.mr[msg->words + item * 2] = l4_map_control(hot_spot, L4_FPAGE_CACHEABLE, 0); |
paul@181 | 490 | msg->mregs.mr[msg->words + item * 2 + 1] = fpage.raw; |
paul@181 | 491 | } |
paul@181 | 492 | |
paul@351 | 493 | /* Specify a receive window flexpage item in the message. */ |
paul@351 | 494 | |
paul@351 | 495 | void ipc_message_receive_fpage(ipc_message_t *msg, int item, l4_snd_fpage_t fpage) |
paul@351 | 496 | { |
paul@351 | 497 | msg->bregs.br[item * 2] = l4_map_control(fpage.snd_base, 0, 0); |
paul@351 | 498 | msg->bregs.br[item * 2 + 1] = fpage.fpage.raw; |
paul@351 | 499 | } |
paul@351 | 500 | |
paul@351 | 501 | |
paul@351 | 502 | |
paul@181 | 503 | /* Import from the message the capability at the given item position, updating |
paul@181 | 504 | the buffer registers for future capabilities. */ |
paul@181 | 505 | |
paul@596 | 506 | long ipc_message_import_and_expect_capability(ipc_message_t *msg, int item, l4_cap_idx_t *ref) |
paul@181 | 507 | { |
paul@181 | 508 | long err; |
paul@508 | 509 | int local; |
paul@181 | 510 | |
paul@609 | 511 | err = _import_capability(msg->tag, &msg->bregs, &msg->mregs, item, msg->import_item, ref, &local); |
paul@181 | 512 | if (err) |
paul@181 | 513 | return err; |
paul@181 | 514 | |
paul@508 | 515 | if (local) |
paul@508 | 516 | return L4_EOK; |
paul@508 | 517 | |
paul@655 | 518 | /* Only if the specified item yielded a valid capability, will an allocated |
paul@655 | 519 | have been consumed. Otherwise, the next allocated capability will still be |
paul@655 | 520 | available. */ |
paul@655 | 521 | |
paul@655 | 522 | if (l4_is_valid_cap(*ref)) |
paul@655 | 523 | { |
paul@655 | 524 | err = ipc_message_expect_capability(msg, msg->import_item); |
paul@655 | 525 | msg->import_item += 1; |
paul@655 | 526 | } |
paul@655 | 527 | |
paul@609 | 528 | return err; |
paul@181 | 529 | } |
paul@181 | 530 | |
paul@181 | 531 | /* Import from the message a dataspace, mapping it to an address, updating the |
paul@181 | 532 | buffer registers for future capabilities. */ |
paul@181 | 533 | |
paul@639 | 534 | long ipc_message_import_and_expect_dataspace(ipc_message_t *msg, int item, l4_cap_idx_t *mem, l4_addr_t *addr) |
paul@181 | 535 | { |
paul@181 | 536 | long err; |
paul@508 | 537 | int local; |
paul@181 | 538 | |
paul@609 | 539 | err = _import_dataspace(msg->tag, &msg->bregs, &msg->mregs, item, msg->import_item, mem, addr, &local); |
paul@181 | 540 | if (err) |
paul@181 | 541 | return err; |
paul@181 | 542 | |
paul@508 | 543 | if (local) |
paul@508 | 544 | return L4_EOK; |
paul@508 | 545 | |
paul@609 | 546 | err = ipc_message_expect_capability(msg, msg->import_item); |
paul@609 | 547 | msg->import_item += 1; |
paul@609 | 548 | return err; |
paul@181 | 549 | } |
paul@181 | 550 | |
paul@596 | 551 | /* Import from the message the capability at the given item position. */ |
paul@596 | 552 | |
paul@596 | 553 | long ipc_message_import_capability(ipc_message_t *msg, int item, l4_cap_idx_t *ref) |
paul@596 | 554 | { |
paul@596 | 555 | int local; |
paul@609 | 556 | long err; |
paul@596 | 557 | |
paul@609 | 558 | err = _import_capability(msg->tag, &msg->bregs, &msg->mregs, item, msg->import_item, ref, &local); |
paul@609 | 559 | if (err) |
paul@609 | 560 | return err; |
paul@609 | 561 | |
paul@609 | 562 | msg->import_item += local ? 0 : 1; |
paul@609 | 563 | return L4_EOK; |
paul@596 | 564 | } |
paul@596 | 565 | |
paul@596 | 566 | /* Import from the message a dataspace, mapping it to an address. */ |
paul@596 | 567 | |
paul@639 | 568 | long ipc_message_import_dataspace(ipc_message_t *msg, int item, l4_cap_idx_t *mem, l4_addr_t *addr) |
paul@596 | 569 | { |
paul@596 | 570 | int local; |
paul@609 | 571 | long err; |
paul@596 | 572 | |
paul@609 | 573 | err = _import_dataspace(msg->tag, &msg->bregs, &msg->mregs, item, msg->import_item, mem, addr, &local); |
paul@609 | 574 | if (err) |
paul@609 | 575 | return err; |
paul@609 | 576 | |
paul@609 | 577 | msg->import_item += local ? 0 : 1; |
paul@609 | 578 | return L4_EOK; |
paul@596 | 579 | } |
paul@596 | 580 | |
paul@181 | 581 | /* Import from the message the flexpage at the given item position. */ |
paul@181 | 582 | |
paul@181 | 583 | long ipc_message_import_fpage(ipc_message_t *msg, int item, l4_snd_fpage_t *fpage) |
paul@181 | 584 | { |
paul@181 | 585 | return _import_fpage(msg->tag, &msg->bregs, &msg->mregs, item, fpage); |
paul@181 | 586 | } |
paul@181 | 587 | |
paul@181 | 588 | /* Export a capability, discarding it by propagating it to the recipient. */ |
paul@181 | 589 | |
paul@181 | 590 | void ipc_message_propagate_capability(ipc_message_t *msg, int item, l4_cap_idx_t ref) |
paul@181 | 591 | { |
paul@181 | 592 | ipc_message_export_capability(msg, item, ref); |
paul@181 | 593 | ipc_message_discard_capability(msg, ref); |
paul@181 | 594 | } |
paul@351 | 595 | |
paul@351 | 596 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@351 | 597 | */ |