|
| 1 | +#include <kernel.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <stdbool.h> |
| 4 | + |
| 5 | +extern volatile struct limine_memmap_request memory_map_request; |
| 6 | + |
| 7 | +static inline bool span_overflows(uint64_t addr, uint64_t width) { |
| 8 | + if (width == 0) { |
| 9 | + return true; |
| 10 | + } |
| 11 | + uint64_t end = addr + width - 1; |
| 12 | + return end < addr; |
| 13 | +} |
| 14 | + |
| 15 | +static inline bool type_readable(uint64_t t) { |
| 16 | + return t == LIMINE_MEMMAP_USABLE |
| 17 | + || t == LIMINE_MEMMAP_ACPI_NVS |
| 18 | + || t == LIMINE_MEMMAP_ACPI_RECLAIMABLE |
| 19 | + || t == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE |
| 20 | + || t == LIMINE_MEMMAP_FRAMEBUFFER |
| 21 | + || t == LIMINE_MEMMAP_KERNEL_AND_MODULES; |
| 22 | +} |
| 23 | + |
| 24 | +static inline bool type_writable(uint64_t t) { |
| 25 | + return t == LIMINE_MEMMAP_USABLE |
| 26 | + || t == LIMINE_MEMMAP_ACPI_RECLAIMABLE |
| 27 | + || t == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE |
| 28 | + || t == LIMINE_MEMMAP_FRAMEBUFFER; |
| 29 | +} |
| 30 | + |
| 31 | +static inline bool address_valid_span(uint64_t addr, uint64_t width, bool for_write) { |
| 32 | + if (addr < 0x1000 || span_overflows(addr, width)) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + const uint64_t end = addr + width - 1; |
| 37 | + uint64_t cursor = addr; |
| 38 | + |
| 39 | + const struct limine_memmap_response *resp = memory_map_request.response; |
| 40 | + if (!resp || resp->entry_count == 0 || !resp->entries) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + while (cursor <= end) { |
| 45 | + const struct limine_memmap_entry *hit = NULL; |
| 46 | + |
| 47 | + for (uint64_t i = 0; i < resp->entry_count; ++i) { |
| 48 | + const struct limine_memmap_entry *e = resp->entries[i]; |
| 49 | + const uint64_t e_base = e->base; |
| 50 | + const uint64_t e_end = e->base + e->length - 1; |
| 51 | + if (cursor >= e_base && cursor <= e_end) { |
| 52 | + const bool ok = for_write ? type_writable(e->type) : type_readable(e->type); |
| 53 | + if (ok) { |
| 54 | + hit = e; |
| 55 | + } |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + if (!hit) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + const uint64_t hit_end = hit->base + hit->length - 1; |
| 64 | + if (hit_end >= end) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + cursor = hit_end + 1; |
| 68 | + } |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +static inline bool address_valid_read(uint64_t addr, int8_t width) { |
| 73 | + return address_valid_span(addr, width, false); |
| 74 | +} |
| 75 | + |
| 76 | +static inline bool address_valid_write(uint64_t addr, int8_t width) { |
| 77 | + return address_valid_span(addr, width, true); |
| 78 | +} |
| 79 | + |
| 80 | +/* ---- BASIC PEEK wrappers (error message unchanged) ---- */ |
| 81 | + |
| 82 | +int64_t basic_peek(struct basic_ctx *ctx) |
| 83 | +{ |
| 84 | + PARAMS_START; |
| 85 | + PARAMS_GET_ITEM(BIP_INT); |
| 86 | + uint64_t addr = (uint64_t)intval; |
| 87 | + PARAMS_END("PEEK", 0); |
| 88 | + |
| 89 | + if (!address_valid_read(addr, 1)) { |
| 90 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", addr); |
| 91 | + return 0; |
| 92 | + } |
| 93 | + uint8_t v = *(volatile uint8_t *)(uintptr_t)addr; |
| 94 | + return (int64_t)v; |
| 95 | +} |
| 96 | + |
| 97 | +int64_t basic_peekw(struct basic_ctx *ctx) |
| 98 | +{ |
| 99 | + PARAMS_START; |
| 100 | + PARAMS_GET_ITEM(BIP_INT); |
| 101 | + uint64_t addr = (uint64_t)intval; |
| 102 | + PARAMS_END("PEEKW", 0); |
| 103 | + |
| 104 | + if (!address_valid_read(addr, 2)) { |
| 105 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", addr); |
| 106 | + return 0; |
| 107 | + } |
| 108 | + uint16_t v = *(volatile uint16_t *)(uintptr_t)addr; |
| 109 | + return (int64_t)v; |
| 110 | +} |
| 111 | + |
| 112 | +int64_t basic_peekd(struct basic_ctx *ctx) |
| 113 | +{ |
| 114 | + PARAMS_START; |
| 115 | + PARAMS_GET_ITEM(BIP_INT); |
| 116 | + uint64_t addr = (uint64_t)intval; |
| 117 | + PARAMS_END("PEEKD", 0); |
| 118 | + |
| 119 | + if (!address_valid_read(addr, 4)) { |
| 120 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", addr); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + uint32_t v = *(volatile uint32_t *)(uintptr_t)addr; |
| 124 | + return (int64_t)v; |
| 125 | +} |
| 126 | + |
| 127 | +int64_t basic_peekq(struct basic_ctx *ctx) |
| 128 | +{ |
| 129 | + PARAMS_START; |
| 130 | + PARAMS_GET_ITEM(BIP_INT); |
| 131 | + uint64_t addr = (uint64_t)intval; |
| 132 | + PARAMS_END("PEEKQ", 0); |
| 133 | + |
| 134 | + if (!address_valid_read(addr, 8)) { |
| 135 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", addr); |
| 136 | + return 0; |
| 137 | + } |
| 138 | + uint64_t v = *(volatile uint64_t *)(uintptr_t)addr; |
| 139 | + return (int64_t)v; |
| 140 | +} |
| 141 | + |
| 142 | +void poke_statement(struct basic_ctx* ctx) { |
| 143 | + accept_or_return(POKE, ctx); |
| 144 | + int64_t addr = expr(ctx); |
| 145 | + accept_or_return(COMMA, ctx); |
| 146 | + int64_t val = expr(ctx); |
| 147 | + accept_or_return(NEWLINE, ctx); |
| 148 | + |
| 149 | + if (!address_valid_write((uint64_t)addr, 1)) { |
| 150 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", (uint64_t)addr); |
| 151 | + return; |
| 152 | + } |
| 153 | + *(volatile uint8_t *)(uintptr_t)addr = (uint8_t)val; |
| 154 | +} |
| 155 | + |
| 156 | +void pokew_statement(struct basic_ctx* ctx) { |
| 157 | + accept_or_return(POKEW, ctx); |
| 158 | + int64_t addr = expr(ctx); |
| 159 | + accept_or_return(COMMA, ctx); |
| 160 | + int64_t val = expr(ctx); |
| 161 | + accept_or_return(NEWLINE, ctx); |
| 162 | + |
| 163 | + if (!address_valid_write((uint64_t)addr, 2)) { |
| 164 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", (uint64_t)addr); |
| 165 | + return; |
| 166 | + } |
| 167 | + *(volatile uint16_t *)(uintptr_t)addr = (uint16_t)val; |
| 168 | +} |
| 169 | + |
| 170 | +void poked_statement(struct basic_ctx* ctx) { |
| 171 | + accept_or_return(POKED, ctx); |
| 172 | + int64_t addr = expr(ctx); |
| 173 | + accept_or_return(COMMA, ctx); |
| 174 | + int64_t val = expr(ctx); |
| 175 | + accept_or_return(NEWLINE, ctx); |
| 176 | + |
| 177 | + if (!address_valid_write((uint64_t)addr, 4)) { |
| 178 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", (uint64_t)addr); |
| 179 | + return; |
| 180 | + } |
| 181 | + *(volatile uint32_t *)(uintptr_t)addr = (uint32_t)val; |
| 182 | +} |
| 183 | + |
| 184 | +void pokeq_statement(struct basic_ctx* ctx) { |
| 185 | + accept_or_return(POKEQ, ctx); |
| 186 | + int64_t addr = expr(ctx); |
| 187 | + accept_or_return(COMMA, ctx); |
| 188 | + int64_t val = expr(ctx); |
| 189 | + accept_or_return(NEWLINE, ctx); |
| 190 | + |
| 191 | + if (!address_valid_write((uint64_t)addr, 8)) { |
| 192 | + tokenizer_error_printf(ctx, "Bad Address at &%016lx", (uint64_t)addr); |
| 193 | + return; |
| 194 | + } |
| 195 | + *(volatile uint64_t *)(uintptr_t)addr = (uint64_t)val; |
| 196 | +} |
0 commit comments