2019-08-05 02:21:18 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2019-07-18 23:43:49 +02:00
|
|
|
#include "utils.h"
|
2019-07-29 22:38:44 +02:00
|
|
|
#include "core_ctx.h"
|
2019-07-31 02:30:17 +02:00
|
|
|
#include "debug_log.h"
|
2019-07-22 01:04:53 +02:00
|
|
|
#include "platform/uart.h"
|
2019-07-31 02:30:17 +02:00
|
|
|
#include "semihosting.h"
|
2019-07-30 02:16:25 +02:00
|
|
|
#include "traps.h"
|
2019-08-02 05:12:24 +02:00
|
|
|
#include "sysreg.h"
|
2019-08-05 02:21:18 +02:00
|
|
|
#include "exceptions.h"
|
|
|
|
|
#include "single_step.h"
|
2019-08-05 23:49:25 +02:00
|
|
|
#include "breakpoints.h"
|
|
|
|
|
#include "watchpoints.h"
|
2019-07-18 23:43:49 +02:00
|
|
|
|
2019-08-11 00:56:49 +02:00
|
|
|
#include "irq.h"
|
|
|
|
|
|
2019-07-31 02:30:17 +02:00
|
|
|
extern const u8 __start__[];
|
|
|
|
|
|
|
|
|
|
static void loadKernelViaSemihosting(void)
|
|
|
|
|
{
|
|
|
|
|
size_t len = 1<<20; // max len
|
|
|
|
|
uintptr_t buf = (uintptr_t)__start__ + (1<<20);
|
|
|
|
|
long handle = -1, ret;
|
|
|
|
|
|
2019-08-01 00:46:16 +02:00
|
|
|
DEBUG("Loading kernel via semihosted file I/O... ");
|
2019-07-31 02:30:17 +02:00
|
|
|
handle = semihosting_file_open("test_kernel.bin", FOPEN_MODE_RB);
|
|
|
|
|
if (handle < 0) {
|
|
|
|
|
DEBUG("failed to open file (%ld)!\n", handle);
|
|
|
|
|
panic();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((ret = semihosting_file_read(handle, &len, buf)) < 0) {
|
|
|
|
|
DEBUG("failed to read file (%ld)!\n", ret);
|
|
|
|
|
panic();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 00:46:16 +02:00
|
|
|
DEBUG("OK!\n");
|
2019-07-31 02:30:17 +02:00
|
|
|
semihosting_file_close(handle);
|
|
|
|
|
currentCoreCtx->kernelEntrypoint = buf;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 02:21:18 +02:00
|
|
|
void main(ExceptionStackFrame *frame)
|
2019-07-18 23:43:49 +02:00
|
|
|
{
|
2019-07-30 02:16:25 +02:00
|
|
|
enableTraps();
|
2019-08-06 06:09:51 +02:00
|
|
|
enableBreakpointsAndWatchpoints();
|
2019-08-11 00:56:49 +02:00
|
|
|
initIrq();
|
2019-08-06 06:09:51 +02:00
|
|
|
|
2019-08-07 21:38:40 +02:00
|
|
|
if (currentCoreCtx->isBootCore) {
|
|
|
|
|
uartInit(115200);
|
|
|
|
|
DEBUG("EL2: core %u reached main first!\n", currentCoreCtx->coreId);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 06:09:51 +02:00
|
|
|
initBreakpoints();
|
|
|
|
|
initWatchpoints();
|
2019-07-30 02:16:25 +02:00
|
|
|
|
2019-08-02 05:12:24 +02:00
|
|
|
if (currentCoreCtx->isBootCore) {
|
2019-07-31 02:30:17 +02:00
|
|
|
if (currentCoreCtx->kernelEntrypoint == 0) {
|
|
|
|
|
if (semihosting_connection_supported()) {
|
|
|
|
|
loadKernelViaSemihosting();
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG("Kernel not loaded!\n");
|
|
|
|
|
panic();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-29 22:38:44 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2019-08-01 00:46:16 +02:00
|
|
|
DEBUG("EL2: core %u reached main!\n", currentCoreCtx->coreId);
|
2019-07-29 22:38:44 +02:00
|
|
|
}
|
2019-07-19 01:45:56 +02:00
|
|
|
|
2019-08-05 02:21:18 +02:00
|
|
|
// Set up exception frame: init regs to 0, set spsr, elr, etc.
|
|
|
|
|
memset(frame, 0, sizeof(ExceptionStackFrame));
|
|
|
|
|
frame->spsr_el2 = (0xF << 6) | (1 << 2) | 1; // EL1h+DAIF
|
|
|
|
|
frame->elr_el2 = currentCoreCtx->kernelEntrypoint;
|
|
|
|
|
frame->x[0] = currentCoreCtx->kernelArgument;
|
|
|
|
|
|
2019-08-12 23:24:30 +02:00
|
|
|
setCurrentCoreActive();
|
2019-07-18 23:43:49 +02:00
|
|
|
}
|