emulator: Run the A extension in hosted mode

8b28a824f6ced8b4f729e7f24e5b9d78244717a2e45bf64253c2800d281a1da2
Hosted programs faulted on any atomic instruction with "Unknown
opcode 2f", because only the machine mode emulation implemented the A
extension. Code that allocates from shared memory had to use one
instruction sequence for hosted programs and another for the kernel.

The hosted interpreter now runs lr, sc and the amo operations in
their word and doubleword forms. The reservation is one address and
width per hart; sc, a trap-free reset and any other lr clear it. A
misaligned address faults. The JIT ends a block at an atomic
instruction and leaves it to the interpreter, as it does for ecall,
because atomics are rare and one hart needs no host atomics.

Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent ea449451
emulator.c +94 -0
1242 1242
    cpu->pc          = program_base;
1243 1243
    cpu->running     = true;
1244 1244
    cpu->faulted     = false;
1245 1245
    cpu->ebreak      = false;
1246 1246
    cpu->modified    = ZERO;
1247 +
    cpu->reserved    = false;
1247 1248
    last_executed_pc = 0;
1248 1249
}
1249 1250
1250 1251
/* Initialize the CPU to a clean state.  Guest memory is not cleared: this runs
1251 1252
 * once at startup, when the static memory is still zero, and clearing it would
2080 2081
            emit_fault_diagnostics(cpu, executed_pc);
2081 2082
        }
2082 2083
        break;
2083 2084
    }
2084 2085
2086 +
    case OP_AMO: {
2087 +
        /* A extension for one hart. Only LR, SC and AMOs touch the
2088 +
         * reservation, so a plain store inside the LR/SC window does not
2089 +
         * break it. With no other hart that cannot be observed. */
2090 +
        u32 width = ins.r.funct3 == 2 ? 4 : 8;
2091 +
        u32 op    = ins.raw >> 27;
2092 +
        u64 addr  = cpu->regs[ins.r.rs1];
2093 +
        u64 src   = cpu->regs[ins.r.rs2];
2094 +
        if ((ins.r.funct3 != 2 && ins.r.funct3 != 3) || (addr & (width - 1))) {
2095 +
            printf(
2096 +
                "Illegal atomic at PC=%08x: addr=%016llx\n",
2097 +
                cpu->pc,
2098 +
                (unsigned long long)addr
2099 +
            );
2100 +
            cpu->running = false;
2101 +
            emit_fault_diagnostics(cpu, executed_pc);
2102 +
            break;
2103 +
        }
2104 +
        bool load = op == 2;
2105 +
        if (!validate_memory_access(cpu, addr, width, ins.r.rs1, "amo", !load))
2106 +
            break;
2107 +
        u32 a32 = (u32)addr;
2108 +
        u64 old = width == 4 ? (u64)(i64)(i32)memory_load_u32(a32)
2109 +
                             : memory_load_u64(a32);
2110 +
        if (load) {
2111 +
            cpu->reserved         = true;
2112 +
            cpu->reservation      = a32;
2113 +
            cpu->reservation_size = width;
2114 +
            cpu->regs[ins.r.rd]   = old;
2115 +
            cpu->modified         = (reg_t)ins.r.rd;
2116 +
            break;
2117 +
        }
2118 +
        u64 value = src;
2119 +
        if (op == 3) { /* sc */
2120 +
            bool ok             = cpu->reserved && cpu->reservation == a32 &&
2121 +
                                  cpu->reservation_size == width;
2122 +
            cpu->reserved       = false;
2123 +
            cpu->regs[ins.r.rd] = ok ? 0 : 1;
2124 +
            cpu->modified       = (reg_t)ins.r.rd;
2125 +
            if (!ok)
2126 +
                break;
2127 +
        } else {
2128 +
            if (width == 4)
2129 +
                src = (u64)(i64)(i32)src;
2130 +
            i64 s = (i64)src, o = (i64)old;
2131 +
            switch (op) {
2132 +
            case 0:
2133 +
                value = old + src;
2134 +
                break; /* amoadd */
2135 +
            case 1:
2136 +
                value = src;
2137 +
                break; /* amoswap */
2138 +
            case 4:
2139 +
                value = old ^ src;
2140 +
                break; /* amoxor */
2141 +
            case 8:
2142 +
                value = old | src;
2143 +
                break; /* amoor */
2144 +
            case 12:
2145 +
                value = old & src;
2146 +
                break; /* amoand */
2147 +
            case 16:
2148 +
                value = o < s ? old : src;
2149 +
                break; /* amomin */
2150 +
            case 20:
2151 +
                value = o > s ? old : src;
2152 +
                break; /* amomax */
2153 +
            case 24:   /* amominu */
2154 +
                value = width == 4 ? ((u32)old < (u32)src ? old : src)
2155 +
                                   : (old < src ? old : src);
2156 +
                break;
2157 +
            case 28: /* amomaxu */
2158 +
                value = width == 4 ? ((u32)old > (u32)src ? old : src)
2159 +
                                   : (old > src ? old : src);
2160 +
                break;
2161 +
            default:
2162 +
                printf("Unknown atomic %02x at PC=%08x\n", op, cpu->pc);
2163 +
                cpu->running = false;
2164 +
                emit_fault_diagnostics(cpu, executed_pc);
2165 +
                goto amo_done;
2166 +
            }
2167 +
            cpu->regs[ins.r.rd] = old;
2168 +
            cpu->modified       = (reg_t)ins.r.rd;
2169 +
        }
2170 +
        watch_store(cpu, a32, width, (u32)value);
2171 +
        if (width == 4)
2172 +
            memory_store_u32(a32, (u32)value);
2173 +
        else
2174 +
            memory_store_u64(a32, value);
2175 +
    amo_done:
2176 +
        break;
2177 +
    }
2178 +
2085 2179
    case OP_FENCE:
2086 2180
        /* Memory barriers are not implemented. */
2087 2181
        break;
2088 2182
2089 2183
    default:
hosted.h +10 -7
30 30
#define ABI_HOST_BASE     0x400
31 31
32 32
/* CPU state. */
33 33
struct cpu {
34 34
    u64      regs[REGISTERS];
35 -
    u32      pc;          /* Program counter. */
36 -
    u32      programsize; /* Size of loaded program. */
37 -
    instr_t *program;     /* Program instructions. */
38 -
    bool     running;     /* Execution status. */
39 -
    bool     faulted;     /* There was a fault in execution. */
40 -
    bool     ebreak;      /* Program terminated via EBREAK. */
41 -
    reg_t    modified;    /* Index of the last modified register. */
35 +
    u32      pc;               /* Program counter. */
36 +
    u32      programsize;      /* Size of loaded program. */
37 +
    instr_t *program;          /* Program instructions. */
38 +
    bool     running;          /* Execution status. */
39 +
    bool     faulted;          /* There was a fault in execution. */
40 +
    bool     ebreak;           /* Program terminated via EBREAK. */
41 +
    reg_t    modified;         /* Index of the last modified register. */
42 +
    bool     reserved;         /* An LR reservation is held. */
43 +
    u32      reservation;      /* Address of the reservation. */
44 +
    u32      reservation_size; /* Width of the reserved access in bytes. */
42 45
};
43 46
44 47
/* Guest memory sizes chosen on the command line, in bytes. */
45 48
struct guest_sizes {
46 49
    u32 memory; /* Physical memory. */
jit.c +5 -0
1172 1172
    }
1173 1173
1174 1174
    case OP_FENCE:
1175 1175
        return false;
1176 1176
1177 +
    case OP_AMO:
1178 +
        /* Atomics are rare; the interpreter runs them. */
1179 +
        emit_block_exit(e, JIT_EXIT_FAULT, pc);
1180 +
        return true;
1181 +
1177 1182
    default:
1178 1183
        emit_block_exit(e, JIT_EXIT_FAULT, pc);
1179 1184
        return true;
1180 1185
    }
1181 1186
}
riscv.h +1 -1
64 64
    OP_IMM_32 = 0x1B, /* RV64I: ADDIW, SLLIW, SRLIW, SRAIW */
65 65
    OP_OP_32 = 0x3B, /* RV64I: ADDW, SUBW, SLLW, SRLW, SRAW, MULW, DIVW, REMW */
66 66
    OP_LUI   = 0x37,
67 67
    OP_SYSTEM = 0x73,
68 68
    OP_FENCE  = 0x0F,
69 -
    OP_AMO    = 0x2F /* A extension, machine mode only */
69 +
    OP_AMO    = 0x2F /* A extension */
70 70
} opcode_t;
71 71
72 72
/* Function3 values */
73 73
typedef enum {
74 74
    /* Memory operations */