#include "window.h" #include /* A segmented virtual program: prefix, replacement and suffix are validated * through a cursor. No merged program buffer is materialized. */ typedef struct { const uint16_t *base; const uint16_t *replacement; unsigned cut; unsigned width; unsigned length; unsigned position; } ProgramCursor; static CodeResult finish(int rc) { CodeResult result; result.rc = rc; result.value = 0; result.retired = 0; return result; } static uint16_t cursor_next(ProgramCursor *cursor) { unsigned position = cursor->position++; if (position >= cursor->cut && position - cursor->cut < cursor->width) { return cursor->replacement[position - cursor->cut]; } return cursor->base[position]; } static int cursor_accepts(ProgramCursor *cursor) { static const unsigned maximum[] = {0, 255, 255, 255, 31, 255, 255}; if (cursor->length < 1 || cursor->length > CW_CAPACITY) { return 0; } while (cursor->position != cursor->length) { uint16_t word = cursor_next(cursor); unsigned op = word / 256u; unsigned byte = word % 256u; if (cursor->position == cursor->length) { if (word != 0) { return 0; } } else if (op == 0 || op >= sizeof(maximum) / sizeof(maximum[0])) { return 0; } else if (byte > maximum[op]) { return 0; } } return 1; } /* Publication phases are represented by a transaction descriptor shared by * install and patch, unlike the separate imperative paths in the initial code. */ typedef struct { unsigned slot; unsigned first; unsigned count; unsigned length; const uint16_t *words; Publication kind; } EditTransaction; static CodeResult commit(CodeWorkbench *work, const EditTransaction *edit) { int results[3]; results[0] = store_write(work, edit->slot, edit->first, edit->words, edit->count); if (results[0]) { return finish(results[0]); } results[1] = store_sync(work, edit->slot, edit->first, edit->first + edit->count); if (results[1]) { return finish(results[1]); } results[2] = store_publish(work, edit->slot, edit->length, edit->kind); return finish(results[2]); } CodeResult window_install(CodeWorkbench *work, unsigned slot, const uint16_t *words, unsigned count) { if (slot >= CW_SLOTS || words == 0) { return finish(CW_PROGRAM); } ProgramCursor cursor = {words, words, 0, count, count, 0}; if (!cursor_accepts(&cursor)) { return finish(CW_PROGRAM); } fputs("segmented install accepted\n", stderr); EditTransaction transaction = {slot, 0, count, count, words, CW_INSTALL}; CodeResult result = commit(work, &transaction); if (result.rc == CW_OK) { for (unsigned i = count; i < CW_CAPACITY; ++i) { work->slots[slot].data[i] = 0xa55au; work->slots[slot].execution[i] = 0x5aa5u; } } return result; } static int patch_status(const CodeSlot *current, uint64_t expected, unsigned first, unsigned count) { if (current->active == 0) { return CW_INACTIVE; } if (current->generation != expected) { return CW_CONFLICT; } if (count < 1 || first >= current->length || count > current->length - first) { return CW_PROGRAM; } return CW_OK; } CodeResult window_patch(CodeWorkbench *work, unsigned slot, uint64_t expected, unsigned first, const uint16_t *words, unsigned count) { if (slot >= CW_SLOTS || words == 0) { return finish(CW_PROGRAM); } CodeSlot *current = work->slots + slot; int status = patch_status(current, expected, first, count); if (status) { return finish(status); } ProgramCursor cursor = {current->data, words, first, count, current->length, 0}; if (!cursor_accepts(&cursor)) { return finish(CW_PROGRAM); } EditTransaction transaction = {slot, first, count, current->length, words, CW_PATCH}; return commit(work, &transaction); } CodeResult window_drop(CodeWorkbench *work, unsigned slot, uint64_t expected) { if (slot >= CW_SLOTS) { return finish(CW_PROGRAM); } const CodeSlot *current = work->slots + slot; int status = current->active ? CW_OK : CW_INACTIVE; if (status == CW_OK && current->generation != expected) { status = CW_CONFLICT; } return status ? finish(status) : finish(store_publish(work, slot, 0, CW_DROP)); } CodeResult window_execute(CodeWorkbench *work, unsigned slot, uint32_t input) { if (slot >= CW_SLOTS) { return finish(CW_PROGRAM); } const CodeSlot *current = work->slots + slot; if (current->active == 0) { return finish(CW_INACTIVE); } ++work->counts.executions; return interpret(current->execution, current->length, input); }