1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-24 19:26:04 +00:00
Atmosphere/thermosphere/src/main.c

120 lines
3 KiB
C
Raw Normal View History

#include <string.h>
#include "utils.h"
#include "core_ctx.h"
#include "debug_log.h"
2019-07-22 00:04:53 +01:00
#include "platform/uart.h"
#include "semihosting.h"
#include "traps.h"
2019-08-02 04:12:24 +01:00
#include "sysreg.h"
#include "exceptions.h"
#include "single_step.h"
#include "breakpoints.h"
#include "watchpoints.h"
#include "timer.h"
2019-08-10 23:56:49 +01:00
#include "irq.h"
#include "transport_interface.h"
2019-08-10 23:56:49 +01: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-07-31 23:46:16 +01:00
DEBUG("Loading kernel via semihosted file I/O... ");
handle = semihosting_file_open("test_kernel.bin", FOPEN_MODE_RB);
if (handle < 0) {
PANIC("failed to open file (%ld)!\n", handle);
}
if ((ret = semihosting_file_read(handle, &len, buf)) < 0) {
PANIC("failed to read file (%ld)!\n", ret);
}
2019-07-31 23:46:16 +01:00
DEBUG("OK!\n");
semihosting_file_close(handle);
currentCoreCtx->kernelEntrypoint = buf;
}
#include "platform/uart.h"
2020-01-14 02:09:51 +00:00
#include "debug_pause.h"
typedef struct TestCtx {
char buf[512+1];
} TestCtx;
static TestCtx g_testCtx;
size_t testReceiveCallback(TransportInterface *iface, void *p)
{
TestCtx *ctx = (TestCtx *)p;
2020-01-14 02:09:51 +00:00
debugPauseCores(BIT(0));
return transportInterfaceReadDataUntil(iface, ctx->buf, 512, '\r');
}
void testProcessDataCallback(TransportInterface *iface, void *p, size_t sz)
{
(void)iface;
(void)sz;
debugUnpauseCores(BIT(0), BIT(0));
TestCtx *ctx = (TestCtx *)p;
DEBUG("EL2 [core %u]: you typed: %s\n", currentCoreCtx->coreId, ctx->buf);
}
void test(void)
{
TransportInterface *iface = transportInterfaceCreate(
TRANSPORT_INTERFACE_TYPE_UART,
DEFAULT_UART,
DEFAULT_UART_FLAGS,
testReceiveCallback,
testProcessDataCallback,
&g_testCtx
);
2020-01-14 02:09:51 +00:00
transportInterfaceSetInterruptAffinity(iface, BIT(1));
}
void thermosphereMain(ExceptionStackFrame *frame, u64 pct)
{
2019-08-10 23:56:49 +01:00
initIrq();
if (currentCoreCtx->isBootCore) {
transportInterfaceInitLayer();
debugLogInit();
test();
DEBUG("EL2: core %u reached main first!\n", currentCoreCtx->coreId);
}
enableTraps();
enableBreakpointsAndWatchpoints();
timerInit();
initBreakpoints();
initWatchpoints();
2019-08-02 04:12:24 +01:00
if (currentCoreCtx->isBootCore) {
if (currentCoreCtx->kernelEntrypoint == 0) {
if (semihosting_connection_supported()) {
loadKernelViaSemihosting();
} else {
PANIC("Kernel not loaded!\n");
}
}
}
else {
2019-07-31 23:46:16 +01:00
DEBUG("EL2: core %u reached main!\n", currentCoreCtx->coreId);
}
setCurrentCoreActive();
// 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;
frame->cntpct_el0 = pct;
}