Skip to content

Commit edc8708

Browse files
committed
Fixes for M-Mode test-app
1 parent 67cb792 commit edc8708

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

hal/mpfs250.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ typedef struct {
365365

366366
/* Multi-hart function declarations */
367367
#ifndef __ASSEMBLER__
368+
#ifdef DEBUG_UART
369+
void uart_init(void);
370+
void uart_write(const char* buf, unsigned int sz);
371+
#endif
368372
#ifdef WOLFBOOT_RISCV_MMODE
369373
int mpfs_wake_secondary_harts(void);
370374
void secondary_hart_entry(unsigned long hartid, HLS_DATA* hls);

src/boot_riscv.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -445,29 +445,34 @@ void do_boot(const uint32_t *app_offset)
445445
*/
446446

447447
#ifdef WOLFBOOT_RISCV_MMODE
448+
#ifdef WOLFBOOT_MMODE_SMODE_BOOT
448449
/*
449450
* M-mode to S-mode transition for booting Linux:
450451
* 1. Set up PMP to allow S-mode full memory access
451452
* 2. Delegate traps/interrupts to S-mode
452453
* 3. Use MRET to switch to S-mode and jump to kernel
453454
*/
454-
#ifdef DEBUG_BOOT
455-
wolfBoot_printf("Setting up M-mode to S-mode transition...\n");
456-
wolfBoot_printf(" PMP: Configuring for S-mode access\n");
457-
#endif
455+
wolfBoot_printf("M->S transition: entry=0x%lx\n", (unsigned long)app_offset);
458456
setup_pmp_for_smode();
459-
460-
#ifdef DEBUG_BOOT
461-
wolfBoot_printf(" Delegating traps to S-mode\n");
462-
#endif
463457
delegate_traps_to_smode();
464-
465-
#ifdef DEBUG_BOOT
466-
wolfBoot_printf(" Entering S-mode: entry=0x%lx, hartid=%lu, dtb=0x%lx\n",
467-
(unsigned long)app_offset, hartid, dts_addr);
468-
#endif
469458
/* This never returns */
470459
enter_smode((unsigned long)app_offset, hartid, dts_addr);
460+
#else
461+
/*
462+
* Direct M-mode jump for bare-metal payloads (no S-mode transition).
463+
* Use this for test-apps; define WOLFBOOT_MMODE_SMODE_BOOT for Linux.
464+
*/
465+
wolfBoot_printf("M-mode direct jump to 0x%lx\n", (unsigned long)app_offset);
466+
/* Drain UART before jumping */
467+
{
468+
volatile int i;
469+
for (i = 0; i < 100000; i++) {}
470+
}
471+
(void)hartid;
472+
(void)dts_addr;
473+
asm volatile("jr %0" : : "r"(app_offset));
474+
__builtin_unreachable();
475+
#endif /* WOLFBOOT_MMODE_SMODE_BOOT */
471476

472477
#elif __riscv_xlen == 64
473478
asm volatile(

test-app/app_mpfs250.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636

3737
void main(void)
3838
{
39+
/* wolfBoot already initialized L2 cache, UART clocks, and QSPI.
40+
* Just re-init UART registers (clocks already on). Do NOT call
41+
* hal_init() - it would try to write to _main_hart_hls=0 (NULL). */
3942
uart_init();
40-
hal_init();
4143

4244
wolfBoot_printf("========================\r\n");
4345
wolfBoot_printf("PolarFire SoC MPFS250 wolfBoot demo Application\r\n");

test-app/startup_riscv.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ void __attribute__((naked,section(".init"))) _reset(void) {
4242
asm volatile("la gp, _global_pointer");
4343
asm volatile("la sp, _end_stack");
4444

45-
/* Set up vectored interrupt, with IV starting at offset 0x100 */
46-
#ifndef WOLFBOOT_RISCV_MMODE
45+
/* Set up vectored interrupt, with IV starting at offset 0x100.
46+
* Always use stvec: wolfBoot M-mode uses enter_smode() to transition
47+
* the payload to S-mode, so stvec is correct in all cases. */
4748
asm volatile("csrw stvec, %0":: "r"((uint8_t *)(&_start_vector) + 1));
48-
#else
49-
asm volatile("csrw mtvec, %0":: "r"((uint8_t *)(&_start_vector) + 1));
50-
#endif
5149

5250
src = (uint32_t *) &_stored_data;
5351
dst = (uint32_t *) &_start_data;
@@ -79,11 +77,8 @@ void do_boot(const uint32_t *app_offset)
7977
static uint32_t synctrap_cause = 0;
8078
void __attribute__((naked)) isr_synctrap(void)
8179
{
82-
#ifndef WOLFBOOT_RISCV_MMODE
80+
/* Always use scause: payload always runs in S-mode under wolfBoot */
8381
asm volatile("csrr %0, scause" : "=r"(synctrap_cause));
84-
#else
85-
asm volatile("csrr %0, mcause" : "=r"(synctrap_cause));
86-
#endif
8782
}
8883

8984
void isr_empty(void)

test-app/vector_riscv.S

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@
6262
ld x30, 112(sp)
6363
ld x31, 120(sp)
6464
addi sp, sp, 128
65-
#ifndef WOLFBOOT_RISCV_MMODE
65+
/* Always sret: payload always runs in S-mode under wolfBoot */
6666
sret
67-
#else
68-
mret
69-
#endif
7067
.endm
7168

7269
#else /* __riscv_xlen == 32 */
@@ -110,11 +107,8 @@
110107
lw x30, 56(sp)
111108
lw x31, 60(sp)
112109
addi sp, sp, 64
113-
#ifndef WOLFBOOT_RISCV_MMODE
110+
/* Always sret: payload always runs in S-mode under wolfBoot */
114111
sret
115-
#else
116-
mret
117-
#endif
118112
.endm
119113

120114
#endif /* __riscv_xlen */

0 commit comments

Comments
 (0)