summaryrefslogtreecommitdiffstats
path: root/debug/risc-v-gsp-rm-vm/sdk/injected_demo.c
blob: 8d836fef603f704155e5bde1c4f9d3519135e6a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Example green-eye injected service.
 *
 * Position-independent, no globals, no libc: safe to load at any address.
 * Commands (matching the demo firmware's injected command range):
 *   1: xor a0 with 0xbeef
 *   2: a0 + a1
 *   3: rotate a0 left by (a1 & 63)
 *   4: a0 * 3 + 7
 *   5: min(a0, a1)
 *   6: max(a0, a1)
 * default: ~cmd
 */
#include "greeneye_service.h"

__attribute__((noinline)) ge_u64 ge_demo_service_dispatch(ge_u64 cmd, ge_u64 a0, ge_u64 a1)
{
    switch (cmd) {
    case 1:
        return a0 ^ 0xbeefu;
    case 2:
        return a0 + a1;
    case 3: {
        unsigned int shift = (unsigned int)(a1 & 63u);
        return shift ? ((a0 << shift) | (a0 >> (64u - shift))) : a0;
    }
    case 4:
        return a0 * 3u + 7u;
    case 5:
        return a0 < a1 ? a0 : a1;
    case 6:
        return a0 > a1 ? a0 : a1;
    default:
        return ~cmd;
    }
}

/* A second entry point, to demonstrate multi-function services. */
__attribute__((noinline)) ge_u64 ge_demo_service_answer(void)
{
    return 42;
}