Files
fmini-1/firmware/hal/fm1_guard.h
T
ClaudeandКирилл Сапфиров c8d571bd22 Felucca 1.6.1
MENU > LARGE keeps one size per knob (#251): each knob shows all its
values in the size its longest value fits, so a value no longer jumps
between sizes while it is turned. No new fonts.

Updates check the package's size on the device: the update entry and the
update loader take only a package whose firmware area is exactly the
FM-1's. Another device's package is refused before anything is erased
(the update screen shows FAILED -15; the loader answers -14).

Second core (beta, PR #247): MENU > SYSTEM > 2ND CORE, OFF by default.
ON (BETA) applies from the next power-on: the second core renders some
of the four parts of each block, bit for bit the one-core sound. A unit
whose second core misreads goes on with one core and sets it back to
OFF. FELUCCA_CPU2=0 builds none of it. Start, mailbox and guards from
X0X (with Melodee's dual-core findings), the split and hardening after
Jangada.

Felucca-Engines: ANALOG, LOFI, TRIO, WHEEL and NOISE, the engine
interface, the shared DSP helpers and their table generator move to
https://github.com/hugelton/Felucca-Engines under the MIT licence,
included as the git submodule firmware/engines. The firmware stays
GPL-3.0-only; clone with --recurse-submodules. The sound is unchanged
(the same golden renders); the engines' own tests run with Felucca's.

Co-authored-by: Кирилл Сапфиров <gospodindilly@gmail.com>
2026-10-17 19:04:28 -03:00

127 lines
4.7 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* FM-1 hardware guards (after the SDK's
* cpu/wl82/debug.c). Every violation raises the CPU exception (vector 1) and
* ends in the fault report of fm1_irq.h, so install that first.
*
* EMU stack limit sp outside [_ustack_lo, _sstack_top] -> EMU_MSG bit 3.
* One window for both stacks: which of EMU_SSP/EMU_USP
* the core applies to our supervisor-mode main loop is
* not known, so both get the same range.
* write limits CPU0 writes into the two 256-byte stack guard bands and
* into 0x01C7FD50..0x01C7FFFF (boot info, mailbox, vectors)
* -> DBG_MSG bit 13. The top window is armed separately
* with fm1_guard_lock_top() once every vector is
* attached; fm1_guard_unlock_top() reopens it.
* bus-invalid fetch/read/write of unmapped space (NULL page, XIP
* writes) -> DBG_MSG bits 4, 5, 16..21.
* PC limit instruction fetch outside [0x02000120, _etext]
* -> DBG_MSG bit 12.
*/
#pragma once
#include <stdint.h>
#include "fm1_irq.h"
#define FM1_X2(off) (*(volatile uint32_t *)(0x1EEE000u + (off)))
#define FM1_WR_LIMIT_H(n) FM1_X2(0x280u + 4u * (n))
#define FM1_WR_LIMIT_L(n) FM1_X2(0x2C0u + 4u * (n))
#define FM1_C0_WR_LIMIT_EN FM1_X2(0x348u)
#define FM1_PC_LIMIT0_H FM1_X2(0x380u)
#define FM1_PC_LIMIT0_L FM1_X2(0x384u)
#define FM1_PC_LIMIT1_H FM1_X2(0x388u)
#define FM1_PC_LIMIT1_L FM1_X2(0x38Cu)
#define FM1_EMU_SSP_H (*(volatile uint32_t *)0x1EEF0D8u)
#define FM1_EMU_SSP_L (*(volatile uint32_t *)0x1EEF0DCu)
#define FM1_EMU_USP_H (*(volatile uint32_t *)0x1EEF0E0u)
#define FM1_EMU_USP_L (*(volatile uint32_t *)0x1EEF0E4u)
extern char _guard0[], _ustack_lo[], _guard1[], _sstack_lo[], _sstack_top[], _etext[];
#if FM1_CPU1_USED
extern uint32_t _rt_start[], _rt_end[], _c1_end[];
#define FM1_RAMCODE_END _c1_end /* .ram_text, then .c1_text (the second core's loop, app.ld) */
#else
extern uint32_t _rt_start[], _rt_end[];
#define FM1_RAMCODE_END _rt_end
#endif
enum { FM1_GUARD_STACK = 1, FM1_GUARD_WRITE = 2, FM1_GUARD_BUS = 4, FM1_GUARD_PC = 8 };
static void fm1__dbg_unlock(void)
{
if (!(FM1_DBG_WR_EN & 1u))
FM1_DBG_WR_EN = 0xE7u;
}
static void fm1__dbg_lock(void)
{
if (FM1_DBG_WR_EN & 1u)
FM1_DBG_WR_EN = 0xE7u;
}
static void fm1_guard_enable(uint32_t which)
{
fm1__dbg_unlock();
FM1_DBG_MSG_CLR = 0xFFFFFFFFu;
if (which & FM1_GUARD_STACK) {
FM1_EMU_CON &= ~(1u << 3);
FM1_EMU_SSP_L = (uint32_t)(uintptr_t)_ustack_lo;
FM1_EMU_SSP_H = (uint32_t)(uintptr_t)_sstack_top;
FM1_EMU_USP_L = (uint32_t)(uintptr_t)_ustack_lo;
FM1_EMU_USP_H = (uint32_t)(uintptr_t)_sstack_top;
FM1_EMU_CON |= 1u << 3;
}
if (which & FM1_GUARD_WRITE) {
FM1_WR_LIMIT_L(0) = (uint32_t)(uintptr_t)_guard0;
FM1_WR_LIMIT_H(0) = (uint32_t)(uintptr_t)_ustack_lo - 1u;
FM1_WR_LIMIT_L(1) = (uint32_t)(uintptr_t)_guard1;
FM1_WR_LIMIT_H(1) = (uint32_t)(uintptr_t)_sstack_lo - 1u;
FM1_C0_WR_LIMIT_EN |= 3u;
}
if (which & FM1_GUARD_BUS)
FM1_DBG_EN |= (0x3Fu << 16) | (0x3u << 4);
if (which & FM1_GUARD_PC) {
FM1_PC_LIMIT0_L = 0x02000120u;
FM1_PC_LIMIT0_H = (uint32_t)(uintptr_t)_etext;
{ /* RAM code (.ram_text, .c1_text: one window, app.ld); with none, repeat the XIP window */
volatile uint32_t rs = (uint32_t)(uintptr_t)_rt_start, re = (uint32_t)(uintptr_t)FM1_RAMCODE_END;
FM1_PC_LIMIT1_L = re > rs ? rs : 0x02000120u;
FM1_PC_LIMIT1_H = re > rs ? re - 1u : (uint32_t)(uintptr_t)_etext;
}
}
fm1__dbg_lock();
}
/* the PC limits apply to the second core too, and it starts in the chip's ROM (found by Melodee's dual-core work;
* Jangada, X0X): open them while it starts (main.c), then fm1_guard_enable(FM1_GUARD_PC) again */
#if FM1_CPU1_USED
static void fm1_guard_pc_open(void)
{
fm1__dbg_unlock();
FM1_PC_LIMIT0_L = 0u;
FM1_PC_LIMIT0_H = 0xFFFFFFFFu;
FM1_PC_LIMIT1_L = 0u;
FM1_PC_LIMIT1_H = 0xFFFFFFFFu;
fm1__dbg_lock();
}
#endif
/* write-protect 0x01C7FD50..0x01C7FFFF (boot info, mailbox, vectors):
* call after the last fm1_irq_attach */
static void fm1_guard_lock_top(void)
{
fm1__dbg_unlock();
FM1_DBG_MSG_CLR = 0xFFFFFFFFu;
FM1_WR_LIMIT_L(2) = 0x01C7FD50u;
FM1_WR_LIMIT_H(2) = 0x01C7FFFFu;
FM1_C0_WR_LIMIT_EN |= 4u;
fm1__dbg_lock();
}
/* reopen the reserved top of RAM (mailbox write before a UBOOT reset) */
static void fm1_guard_unlock_top(void)
{
fm1__dbg_unlock();
FM1_C0_WR_LIMIT_EN &= ~4u;
fm1__dbg_lock();
}