aboutsummaryrefslogtreecommitdiff
path: root/el1/el1.c
blob: f47ae9cc5831ffd5d030157a47de3851452f6380 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "el1_common.h"
#include "exception.h"
#include "vmsa.h"
#include "mem_util.h"

uintptr_t mem_pgtbl_base = EL1_PGTBL_BASE;
smc_op_desc_t *smc_interop_buf;
sys_control_t *syscntl;
uintptr_t mem_next_pa = 0;
uintptr_t mem_next_l1_page = 0;
uintptr_t mem_heap_pool = EL1_VA_HEAP_BASE;

const char *svc_op_name[] = {
    [SVC_OP_EXIT] = "SVC_OP_EXIT",
    [SVC_OP_ALLOC] = "SVC_OP_ALLOC",
    [SVC_OP_MAP] = "SVC_OP_MAP",
    [SVC_OP_YIELD] = "SVC_OP_YIELD",
    [SVC_OP_GET_SYSCNTL] = "SVC_OP_GET_SYSCNTL",
    [SVC_OP_GET_REG] = "SVC_OP_GET_REG",
    [SVC_OP_SET_REG] = "SVC_OP_SET_REG",
    [SVC_OP_TEST] = "SVC_OP_TEST",
    [SVC_OP_DISPATCH] = "SVC_OP_DISPATCH"
};

void el1_alloc_mem(op_alloc_mem_t *alloc)
{
    alloc->addr = mem_heap_allocate(alloc->len);
}

void el1_map_secure(op_map_mem_t *map)
{
    if ((map->type & OP_MAP_EL3) == OP_MAP_EL3) {
        smc_op_desc_t *desc = (smc_op_desc_t *)smc_interop_buf;
        memcpy(desc, map, sizeof(op_map_mem_t));
        if (desc->map.pa) {
            desc->map.pa = mem_lookup_pa(desc->map.va);
        }
        __smc(SMC_OP_MAP, desc);
    } else {
        mem_map_pa((uintptr_t)map->va, (uintptr_t)map->pa, 0, PTE_USER_RW);
    }
}

void el1_interop_test(op_test_t *desc)
{
    op_test_t *test = (op_test_t*)smc_interop_buf;

    memcpy(smc_interop_buf, desc, sizeof(smc_op_desc_t));
    if (test->val != test->orig >> test->count) {
        test->fail++;
    }
    test->val >>= 1;
    test->count++;

    __smc(SMC_OP_TEST, smc_interop_buf);
    if (test->val != test->orig >> test->count) {
        test->fail++;
    }
    test->val >>= 1;
    test->count++;
    memcpy(desc, smc_interop_buf, sizeof(smc_op_desc_t));
}

int el1_handle_svc(uint32_t op, svc_op_desc_t *desc)
{
    uint32_t ret = 0;

    DEBUG_MSG("Took an svc(%s) - desc = %p\n", svc_op_name[op], desc);
    switch (op) {
    case SVC_OP_EXIT:
        SMC_EXIT();
        break;
    case SVC_OP_YIELD:
        ret = SMC_YIELD();
        memcpy(desc, smc_interop_buf, sizeof(smc_op_desc_t));
        break;
    case SVC_OP_ALLOC:
        el1_alloc_mem((op_alloc_mem_t *)desc);
        break;
    case SVC_OP_MAP:
        el1_map_secure((op_map_mem_t *)desc);
        break;
    case SVC_OP_GET_SYSCNTL:
        desc->get.data = (uintptr_t)syscntl;
        break;
    case SVC_OP_GET_REG:
        if (desc->get.el == 1) {
            switch (desc->get.key) {
#if AARCH64
            case CURRENTEL:
                desc->get.data = READ_CURRENTEL();
                break;
            case CPTR_EL3:
                desc->get.data = READ_CPTR_EL3();
                break;
            case CPACR:
                desc->get.data = READ_CPACR();
                break;
            case SCR:
                desc->get.data = READ_SCR();
                break;
#endif
            }
        } else if (desc->get.el == 3) {
            memcpy(smc_interop_buf, desc, sizeof(smc_op_desc_t));
            __smc(SMC_OP_GET_REG, smc_interop_buf);
            memcpy(desc, smc_interop_buf, sizeof(smc_op_desc_t));
        }
        break;
    case SVC_OP_SET_REG:
        if (desc->set.el == 1) {
            switch (desc->set.key) {
#if AARCH64
            case CURRENTEL:
                WRITE_CURRENTEL(desc->set.data);
                break;
            case CPTR_EL3:
                WRITE_CPTR_EL3(desc->set.data);
                break;
            case CPACR:
                WRITE_CPACR(desc->set.data);
                break;
            case SCR:
                WRITE_SCR(desc->set.data);
                break;
#endif
            }
        } else if (desc->set.el == 3) {
            memcpy(smc_interop_buf, desc, sizeof(smc_op_desc_t));
            __smc(SMC_OP_SET_REG, smc_interop_buf);
        }
        break;
    case SVC_OP_TEST:
        el1_interop_test((op_test_t *)desc);
        break;
    case SVC_OP_DISPATCH:
        memcpy(smc_interop_buf, desc, sizeof(smc_op_desc_t));
        __smc(SMC_OP_DISPATCH, smc_interop_buf);
        memcpy(desc, smc_interop_buf, sizeof(smc_op_desc_t));
        break;
    default:
        DEBUG_MSG("Unrecognized AArch64 SVC opcode: op = %d\n", op);
        break;
    }

    return ret;
}

void el1_handle_exception(uintptr_t ec, uintptr_t iss, uintptr_t far,
     					uintptr_t elr)
{
    if (syscntl->excp_log || syscntl->el1_excp[SEC_STATE].log) {
        syscntl->el1_excp[SEC_STATE].taken = true;
        syscntl->el1_excp[SEC_STATE].ec = ec;
        syscntl->el1_excp[SEC_STATE].iss = iss;
        syscntl->el1_excp[SEC_STATE].far = far;
    }

    switch (ec) {
    case EC_UNKNOWN:
        DEBUG_MSG("Unknown exception far = 0x%lx  elr = 0x%lx\n", far, elr);

        /* The preferred return address in the case of an undefined instruction
         * is the actual offending instruction.  In the case of ARMv7, we need
         * to decrement the address by 4 to get this behavior as the PC has
         * already been moved past this instruction.
         * If SKIP is enabled then we can just do nothing and get the correct
         * behavior.
         */
#if AARCH32
        if (syscntl->el1_excp[SEC_STATE].action != EXCP_ACTION_SKIP &&
            syscntl->excp_action != EXCP_ACTION_SKIP) {
            elr += 4;
            __set_exception_return(elr);
        }
#else
        if (syscntl->el1_excp[SEC_STATE].action == EXCP_ACTION_SKIP ||
            syscntl->excp_action == EXCP_ACTION_SKIP) {
            elr +=4;
            __set_exception_return(elr);
        }
#endif
        break;
    case EC_IABORT_LOWER:
        DEBUG_MSG("Instruction abort at lower level: far = %0lx\n", far);
        SMC_EXIT();
        break;
    case EC_IABORT:
        DEBUG_MSG("Instruction abort at EL1: far = %0lx\n", far);
        SMC_EXIT();
        break;
    case EC_DABORT_LOWER:
        DEBUG_MSG("Data abort at lower level: far = %0lx elr = %0lx\n",
                  far, elr);
        SMC_EXIT();
        break;
    case EC_DABORT:
        DEBUG_MSG("Data abort at EL1: far = %0lx elr = %0lx\n", far, elr);
        SMC_EXIT();
        break;
    case EC_WFI_WFE:
        DEBUG_MSG("WFI/WFE instruction exception far = 0x%lx  elr = 0x%lx\n",
                  far, elr);

        if (syscntl->el1_excp[SEC_STATE].action == EXCP_ACTION_SKIP ||
            syscntl->excp_action == EXCP_ACTION_SKIP) {
            elr +=4;
            __set_exception_return(elr);
        }
        break;
    default:
        DEBUG_MSG("Unhandled EL1 exception: ec = %d iss = %d\n", ec, iss);
        SMC_EXIT();
        break;
    }
}

void el1_start(uintptr_t base, uintptr_t size)
{
    uintptr_t addr = base;
    size_t len;

    printf("EL1 (%s) started...\n", sec_state_str);

    /* Unmap the init segement so we don't accidentally use it */
    for (len = 0; len < ((size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1));
         len += PAGE_SIZE, addr += PAGE_SIZE) {
        mem_unmap_va(addr);
    }

    void *pa = syscntl;
    syscntl = (sys_control_t *)mem_heap_allocate(PAGE_SIZE);
    mem_map_pa((uintptr_t)syscntl, (uintptr_t)pa, 0, PTE_USER_RW);
    mem_map_pa((uintptr_t)syscntl->smc_interop.buf_va,
               (uintptr_t)syscntl->smc_interop.buf_pa, 0, PTE_USER_RW);
    smc_interop_buf = syscntl->smc_interop.buf_va;

    el1_init_el0();

    return;
}