|
| 1 | +/* |
| 2 | + * This file is part of AtomVM. |
| 3 | + * |
| 4 | + * Copyright 2025 by Paul Guyot <pguyot@kallisys.net> |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | + */ |
| 20 | + |
| 21 | +#ifndef AVM_NO_JIT |
| 22 | + |
| 23 | +#include "jit_stream_flash.h" |
| 24 | + |
| 25 | +#include <hardware/flash.h> |
| 26 | +#include <pico/flash.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdlib.h> |
| 29 | + |
| 30 | +#include "rp2_sys.h" |
| 31 | + |
| 32 | +// Helper structures for flash_safe_execute |
| 33 | +struct EraseParams |
| 34 | +{ |
| 35 | + uintptr_t addr; |
| 36 | +}; |
| 37 | + |
| 38 | +struct WriteParams |
| 39 | +{ |
| 40 | + uintptr_t addr; |
| 41 | + const uint8_t *data; |
| 42 | + size_t len; |
| 43 | +}; |
| 44 | + |
| 45 | +static void __not_in_flash_func(do_erase_sector)(void *params_ptr) |
| 46 | +{ |
| 47 | + struct EraseParams *params = (struct EraseParams *) params_ptr; |
| 48 | + flash_range_erase(params->addr - XIP_BASE, FLASH_SECTOR_SIZE); |
| 49 | +} |
| 50 | + |
| 51 | +static void __not_in_flash_func(do_write_page)(void *params_ptr) |
| 52 | +{ |
| 53 | + struct WriteParams *params = (struct WriteParams *) params_ptr; |
| 54 | + flash_range_program(params->addr - XIP_BASE, params->data, params->len); |
| 55 | +} |
| 56 | + |
| 57 | +struct JSFlashPlatformContext *jit_stream_flash_platform_init(void) |
| 58 | +{ |
| 59 | + return (struct JSFlashPlatformContext *) 1; |
| 60 | +} |
| 61 | + |
| 62 | +void jit_stream_flash_platform_destroy(struct JSFlashPlatformContext *pf_ctx) |
| 63 | +{ |
| 64 | + UNUSED(pf_ctx); |
| 65 | +} |
| 66 | + |
| 67 | +bool jit_stream_flash_platform_erase_sector(struct JSFlashPlatformContext *pf_ctx, uintptr_t addr) |
| 68 | +{ |
| 69 | + UNUSED(pf_ctx); |
| 70 | + |
| 71 | + struct EraseParams params = { |
| 72 | + .addr = addr |
| 73 | + }; |
| 74 | + |
| 75 | + int r = flash_safe_execute(do_erase_sector, ¶ms, UINT32_MAX); |
| 76 | + if (UNLIKELY(r != PICO_OK)) { |
| 77 | + fprintf(stderr, "flash_safe_execute (erase) failed with error %d\n", r); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +bool jit_stream_flash_platform_write_page(struct JSFlashPlatformContext *pf_ctx, uintptr_t addr, const uint8_t *data) |
| 85 | +{ |
| 86 | + UNUSED(pf_ctx); |
| 87 | + |
| 88 | + struct WriteParams params = { |
| 89 | + .addr = addr, |
| 90 | + .data = data, |
| 91 | + .len = FLASH_PAGE_SIZE |
| 92 | + }; |
| 93 | + |
| 94 | + int r = flash_safe_execute(do_write_page, ¶ms, UINT32_MAX); |
| 95 | + if (UNLIKELY(r != PICO_OK)) { |
| 96 | + fprintf(stderr, "flash_safe_execute (write) failed with error %d\n", r); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | +} |
| 102 | + |
| 103 | +uintptr_t jit_stream_flash_platform_ptr_to_executable(uintptr_t addr) |
| 104 | +{ |
| 105 | + // Set Thumb bit |
| 106 | + return addr | 0x1; |
| 107 | +} |
| 108 | + |
| 109 | +uintptr_t jit_stream_flash_platform_executable_to_ptr(uintptr_t addr) |
| 110 | +{ |
| 111 | + // Clear Thumb bit |
| 112 | + return addr & ~0x1UL; |
| 113 | +} |
| 114 | + |
| 115 | +REGISTER_NIF_COLLECTION(jit_stream_flash, jit_stream_flash_init, NULL, jit_stream_flash_get_nif) |
| 116 | + |
| 117 | +#endif // AVM_NO_JIT |
0 commit comments