|
| 1 | +#include "streaming_process_slice.h" |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +static void streaming_process_slice_prepare_header(uint8_t* buf) { |
| 5 | + buf[0] = 0; // version[H] |
| 6 | + buf[1] = 0; // version[L] |
| 7 | + buf[2] = 0; // flags[H] |
| 8 | + buf[3] = 0; // flags[L] |
| 9 | + buf[4] = 0; // write offset |
| 10 | + buf[5] = 0; // write offset |
| 11 | + buf[6] = 0; // write offset |
| 12 | + buf[7] = 0; // write offset |
| 13 | +} |
| 14 | + |
| 15 | +returncode_t streaming_process_slice_init( |
| 16 | + streaming_process_slice_state_t* state, |
| 17 | + uint32_t driver, |
| 18 | + uint32_t allow, |
| 19 | + void* buffer_a, |
| 20 | + size_t size_a, |
| 21 | + void* buffer_b, |
| 22 | + size_t size_b) { |
| 23 | + // Ensure that both buffers can hold the streaming process slice header: |
| 24 | + if (size_a < STREAMING_PROCESS_SLICE_HEADER_LEN || size_b < STREAMING_PROCESS_SLICE_HEADER_LEN) { |
| 25 | + return RETURNCODE_ESIZE; |
| 26 | + } |
| 27 | + |
| 28 | + state->driver = driver; |
| 29 | + state->allow = allow; |
| 30 | + |
| 31 | + // Initially, `buffer_a` is used as the application-owned buffer. |
| 32 | + state->app_buffer_ptr = buffer_a; |
| 33 | + state->app_buffer_size = size_a; |
| 34 | + state->app_buffer_is_b = false; // false -> buffer_a |
| 35 | + |
| 36 | + // Write a streaming process slice header to bufferB, and use it as the |
| 37 | + // kernel-owned buffer. Its pointer and length are stored within the kernel's |
| 38 | + // allow slot. We currently only support version 0, and don't set the `halt` |
| 39 | + // flag: |
| 40 | + streaming_process_slice_prepare_header(buffer_b); |
| 41 | + |
| 42 | + allow_rw_return_t allow_res = |
| 43 | + allow_readwrite(driver, allow, buffer_b, size_b); |
| 44 | + if (!allow_res.success) { |
| 45 | + memset(state, 0, sizeof(streaming_process_slice_state_t)); |
| 46 | + } |
| 47 | + |
| 48 | + return tock_status_to_returncode(allow_res.status); |
| 49 | +} |
| 50 | + |
| 51 | +returncode_t streaming_process_slice_get_and_swap( |
| 52 | + streaming_process_slice_state_t* state, |
| 53 | + uint8_t** buffer, |
| 54 | + uint32_t* size, |
| 55 | + bool* exceeded) { |
| 56 | + // Prepare the current app buffer to be shared with the kernel (writing a |
| 57 | + // zeroed-out header): |
| 58 | + streaming_process_slice_prepare_header(state->app_buffer_ptr); |
| 59 | + |
| 60 | + // Swap the current app buffer for the kernel buffer: |
| 61 | + allow_rw_return_t allow_res = |
| 62 | + allow_readwrite(state->driver, state->allow, state->app_buffer_ptr, |
| 63 | + state->app_buffer_size); |
| 64 | + |
| 65 | + // Initialize to safe dummy values in case the allow was not successful |
| 66 | + uint8_t* ret_buffer = NULL; |
| 67 | + uint32_t ret_size = 0; |
| 68 | + bool ret_exceeded = false; |
| 69 | + if (allow_res.success) { |
| 70 | + // Record the new app buffer: |
| 71 | + state->app_buffer_ptr = allow_res.ptr; |
| 72 | + state->app_buffer_size = allow_res.size; |
| 73 | + state->app_buffer_is_b = !state->app_buffer_is_b; |
| 74 | + |
| 75 | + // Return information about the received payload: |
| 76 | + ret_buffer = state->app_buffer_ptr + 8; |
| 77 | + memcpy(&ret_size, state->app_buffer_ptr + 4, sizeof(uint32_t)); |
| 78 | + ret_exceeded = (state->app_buffer_ptr[3] & 0x01) == 0x01; |
| 79 | + } |
| 80 | + |
| 81 | + // Write return values if provided with non-NULL pointers: |
| 82 | + if (buffer != NULL) { |
| 83 | + *buffer = ret_buffer; |
| 84 | + } |
| 85 | + if (size != NULL) { |
| 86 | + *size = ret_size; |
| 87 | + } |
| 88 | + if (exceeded != NULL) { |
| 89 | + *exceeded = ret_exceeded; |
| 90 | + } |
| 91 | + |
| 92 | + return tock_status_to_returncode(allow_res.status); |
| 93 | +} |
| 94 | + |
| 95 | +returncode_t streaming_process_slice_deinit( |
| 96 | + streaming_process_slice_state_t* state, |
| 97 | + uint8_t** buffer_a, |
| 98 | + size_t* size_a, |
| 99 | + uint8_t** buffer_b, |
| 100 | + size_t* size_b) { |
| 101 | + // Safe default values: |
| 102 | + uint8_t* ret_buffer_a = NULL; |
| 103 | + size_t ret_size_b = 0; |
| 104 | + uint8_t* ret_buffer_b = NULL; |
| 105 | + size_t ret_size_a = 0; |
| 106 | + |
| 107 | + // Unallow the buffer currently allowed to the kernel: |
| 108 | + allow_rw_return_t unallow_res = |
| 109 | + allow_readwrite(state->driver, state->allow, NULL, 0); |
| 110 | + |
| 111 | + if (unallow_res.success) { |
| 112 | + // The unallow worked, recreate the full, initial buffer from the app and |
| 113 | + // kernel halves: |
| 114 | + if (state->app_buffer_is_b) { |
| 115 | + ret_buffer_a = unallow_res.ptr; |
| 116 | + ret_size_a = unallow_res.size; |
| 117 | + ret_buffer_b = state->app_buffer_ptr; |
| 118 | + ret_size_b = state->app_buffer_size; |
| 119 | + } else { |
| 120 | + ret_buffer_a = state->app_buffer_ptr; |
| 121 | + ret_size_a = state->app_buffer_size; |
| 122 | + ret_buffer_b = unallow_res.ptr; |
| 123 | + ret_size_b = unallow_res.size; |
| 124 | + } |
| 125 | + |
| 126 | + // Wipe the state struct: |
| 127 | + memset(state, 0, sizeof(streaming_process_slice_state_t)); |
| 128 | + } |
| 129 | + |
| 130 | + if (buffer_a != NULL) { |
| 131 | + *buffer_a = ret_buffer_a; |
| 132 | + } |
| 133 | + if (size_a != NULL) { |
| 134 | + *size_a = ret_size_a; |
| 135 | + } |
| 136 | + if (buffer_b != NULL) { |
| 137 | + *buffer_b = ret_buffer_b; |
| 138 | + } |
| 139 | + if (size_b != NULL) { |
| 140 | + *size_b = ret_size_b; |
| 141 | + } |
| 142 | + |
| 143 | + return tock_status_to_returncode(unallow_res.status); |
| 144 | +} |
0 commit comments