2021-05-04 10:36:23 +00:00
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
|
|
|
#include "hv.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "cpu_regs.h"
|
|
|
|
#include "exception.h"
|
|
|
|
#include "string.h"
|
2021-05-27 12:16:17 +00:00
|
|
|
#include "uart.h"
|
2021-05-04 10:36:23 +00:00
|
|
|
#include "uartproxy.h"
|
|
|
|
|
2021-05-25 11:08:35 +00:00
|
|
|
#define _SYSREG_ISS(_1, _2, op0, op1, CRn, CRm, op2) \
|
|
|
|
(((op0) << ESR_ISS_MSR_OP0_SHIFT) | ((op1) << ESR_ISS_MSR_OP1_SHIFT) | \
|
|
|
|
((CRn) << ESR_ISS_MSR_CRn_SHIFT) | ((CRm) << ESR_ISS_MSR_CRm_SHIFT) | \
|
|
|
|
((op2) << ESR_ISS_MSR_OP2_SHIFT))
|
|
|
|
#define SYSREG_ISS(...) _SYSREG_ISS(__VA_ARGS__)
|
|
|
|
|
|
|
|
bool ipi_pending = false;
|
|
|
|
|
2021-05-04 15:24:52 +00:00
|
|
|
void hv_exit_guest(void) __attribute__((noreturn));
|
|
|
|
|
2021-05-27 15:38:42 +00:00
|
|
|
static u64 stolen_time = 0;
|
|
|
|
static u64 exc_entry_time;
|
|
|
|
|
2021-05-15 14:55:34 +00:00
|
|
|
void hv_exc_proxy(u64 *regs, uartproxy_boot_reason_t reason, uartproxy_exc_code_t type, void *extra)
|
2021-05-04 10:36:23 +00:00
|
|
|
{
|
2021-05-27 12:11:49 +00:00
|
|
|
int from_el = FIELD_GET(SPSR_M, hv_get_spsr()) >> 2;
|
2021-05-04 15:27:21 +00:00
|
|
|
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('P');
|
|
|
|
|
2021-05-04 10:36:23 +00:00
|
|
|
struct uartproxy_exc_info exc_info = {
|
2021-05-27 12:11:49 +00:00
|
|
|
.spsr = hv_get_spsr(),
|
|
|
|
.elr = hv_get_elr(),
|
|
|
|
.esr = hv_get_esr(),
|
|
|
|
.far = hv_get_far(),
|
2021-05-29 18:29:52 +00:00
|
|
|
.afsr1 = hv_get_afsr1(),
|
2021-05-04 10:36:23 +00:00
|
|
|
.sp = {mrs(SP_EL0), mrs(SP_EL1), 0},
|
|
|
|
.mpidr = mrs(MPIDR_EL1),
|
2021-05-27 12:11:49 +00:00
|
|
|
.elr_phys = hv_translate(hv_get_elr(), false, false),
|
|
|
|
.far_phys = hv_translate(hv_get_far(), false, false),
|
2021-05-04 18:27:19 +00:00
|
|
|
.sp_phys = hv_translate(from_el == 0 ? mrs(SP_EL0) : mrs(SP_EL1), false, false),
|
2021-05-15 14:55:34 +00:00
|
|
|
.extra = extra,
|
2021-05-04 10:36:23 +00:00
|
|
|
};
|
|
|
|
memcpy(exc_info.regs, regs, sizeof(exc_info.regs));
|
|
|
|
|
|
|
|
struct uartproxy_msg_start start = {
|
2021-05-15 14:55:34 +00:00
|
|
|
.reason = reason,
|
2021-05-04 10:36:23 +00:00
|
|
|
.code = type,
|
|
|
|
.info = &exc_info,
|
|
|
|
};
|
|
|
|
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_suspend();
|
2021-05-04 10:36:23 +00:00
|
|
|
int ret = uartproxy_run(&start);
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_resume();
|
2021-05-04 10:36:23 +00:00
|
|
|
|
2021-05-04 15:24:52 +00:00
|
|
|
switch (ret) {
|
2021-05-08 18:15:25 +00:00
|
|
|
case EXC_RET_STEP:
|
2021-05-04 15:24:52 +00:00
|
|
|
case EXC_RET_HANDLED:
|
|
|
|
memcpy(regs, exc_info.regs, sizeof(exc_info.regs));
|
2021-05-27 12:11:49 +00:00
|
|
|
hv_set_spsr(exc_info.spsr);
|
|
|
|
hv_set_elr(exc_info.elr);
|
2021-05-04 15:24:52 +00:00
|
|
|
msr(SP_EL0, exc_info.sp[0]);
|
|
|
|
msr(SP_EL1, exc_info.sp[1]);
|
2021-05-08 18:15:25 +00:00
|
|
|
if (ret == EXC_RET_STEP) {
|
2021-05-25 11:09:53 +00:00
|
|
|
msr(CNTV_TVAL_EL0, 100);
|
|
|
|
msr(CNTV_CTL_EL0, CNTx_CTL_ENABLE);
|
2021-05-08 18:15:25 +00:00
|
|
|
}
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('p');
|
2021-05-04 15:24:52 +00:00
|
|
|
return;
|
|
|
|
case EXC_EXIT_GUEST:
|
|
|
|
hv_exit_guest();
|
|
|
|
default:
|
|
|
|
printf("Guest exception not handled, rebooting.\n");
|
|
|
|
print_regs(regs, 0);
|
2021-05-04 18:21:48 +00:00
|
|
|
flush_and_reboot();
|
2021-05-04 10:36:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:08:35 +00:00
|
|
|
static void hv_update_fiq(void)
|
|
|
|
{
|
|
|
|
u64 hcr = mrs(HCR_EL2);
|
|
|
|
bool fiq_pending = false;
|
|
|
|
|
|
|
|
if (mrs(CNTP_CTL_EL02) == (CNTx_CTL_ISTATUS | CNTx_CTL_ENABLE)) {
|
|
|
|
fiq_pending = true;
|
2021-05-27 15:38:11 +00:00
|
|
|
reg_clr(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, VM_TMR_FIQ_ENA_ENA_P);
|
2021-05-25 11:08:35 +00:00
|
|
|
} else {
|
2021-05-27 15:38:11 +00:00
|
|
|
reg_set(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, VM_TMR_FIQ_ENA_ENA_P);
|
2021-05-25 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mrs(CNTV_CTL_EL02) == (CNTx_CTL_ISTATUS | CNTx_CTL_ENABLE)) {
|
|
|
|
fiq_pending = true;
|
2021-05-27 15:38:11 +00:00
|
|
|
reg_clr(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, VM_TMR_FIQ_ENA_ENA_V);
|
2021-05-25 11:08:35 +00:00
|
|
|
} else {
|
2021-05-27 15:38:11 +00:00
|
|
|
reg_set(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, VM_TMR_FIQ_ENA_ENA_V);
|
2021-05-25 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fiq_pending |= ipi_pending;
|
|
|
|
|
|
|
|
sysop("isb");
|
|
|
|
|
|
|
|
if ((hcr & HCR_VF) && !fiq_pending) {
|
|
|
|
hv_write_hcr(hcr & ~HCR_VF);
|
|
|
|
} else if (!(hcr & HCR_VF) && fiq_pending) {
|
|
|
|
hv_write_hcr(hcr | HCR_VF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 15:37:48 +00:00
|
|
|
#define SYSREG_MAP(sr, to) \
|
|
|
|
case SYSREG_ISS(sr): \
|
|
|
|
if (is_read) \
|
|
|
|
regs[rt] = _mrs(sr_tkn(to)); \
|
|
|
|
else \
|
|
|
|
_msr(sr_tkn(to), regs[rt]); \
|
|
|
|
return true;
|
|
|
|
|
2021-05-25 11:08:35 +00:00
|
|
|
#define SYSREG_PASS(sr) \
|
|
|
|
case SYSREG_ISS(sr): \
|
|
|
|
if (is_read) \
|
|
|
|
regs[rt] = _mrs(sr_tkn(sr)); \
|
|
|
|
else \
|
|
|
|
_msr(sr_tkn(sr), regs[rt]); \
|
|
|
|
return true;
|
|
|
|
|
|
|
|
static bool hv_handle_msr(u64 *regs, u64 iss)
|
|
|
|
{
|
|
|
|
u64 reg = iss & (ESR_ISS_MSR_OP0 | ESR_ISS_MSR_OP2 | ESR_ISS_MSR_OP1 | ESR_ISS_MSR_CRn |
|
|
|
|
ESR_ISS_MSR_CRm);
|
|
|
|
u64 rt = FIELD_GET(ESR_ISS_MSR_Rt, iss);
|
|
|
|
bool is_read = iss & ESR_ISS_MSR_DIR;
|
|
|
|
|
|
|
|
regs[31] = 0;
|
|
|
|
|
|
|
|
switch (reg) {
|
2021-05-27 17:36:06 +00:00
|
|
|
/* Some kind of timer */
|
|
|
|
SYSREG_PASS(sys_reg(3, 7, 15, 1, 1));
|
2021-05-27 15:37:48 +00:00
|
|
|
/* Noisy traps */
|
|
|
|
SYSREG_MAP(SYS_ACTLR_EL1, SYS_IMP_APL_ACTLR_EL12)
|
2021-05-25 11:08:35 +00:00
|
|
|
/* IPI handling */
|
2021-05-27 15:38:11 +00:00
|
|
|
SYSREG_PASS(SYS_IMP_APL_IPI_RR_LOCAL_EL1)
|
|
|
|
SYSREG_PASS(SYS_IMP_APL_IPI_RR_GLOBAL_EL1)
|
|
|
|
SYSREG_PASS(SYS_IMP_APL_IPI_CR_EL1)
|
|
|
|
case SYSREG_ISS(SYS_IMP_APL_IPI_SR_EL1):
|
2021-05-25 11:08:35 +00:00
|
|
|
if (is_read)
|
|
|
|
regs[rt] = ipi_pending ? IPI_SR_PENDING : 0;
|
|
|
|
else if (regs[rt] & IPI_SR_PENDING)
|
|
|
|
ipi_pending = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-27 15:38:42 +00:00
|
|
|
static void hv_exc_entry(u64 *regs)
|
|
|
|
{
|
|
|
|
UNUSED(regs);
|
|
|
|
hv_wdt_breadcrumb('X');
|
|
|
|
exc_entry_time = mrs(CNTPCT_EL0);
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:07:02 +00:00
|
|
|
static void hv_exc_exit(u64 *regs)
|
|
|
|
{
|
2021-05-27 16:24:29 +00:00
|
|
|
UNUSED(regs);
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('x');
|
2021-05-25 11:08:35 +00:00
|
|
|
hv_update_fiq();
|
2021-05-27 16:25:03 +00:00
|
|
|
u64 lost = mrs(CNTPCT_EL0) - exc_entry_time;
|
|
|
|
if (lost > 8)
|
|
|
|
stolen_time += lost - 8;
|
2021-05-27 15:38:42 +00:00
|
|
|
msr(CNTVOFF_EL2, stolen_time);
|
2021-05-25 11:07:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:36:23 +00:00
|
|
|
void hv_exc_sync(u64 *regs)
|
|
|
|
{
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('S');
|
2021-05-27 15:38:42 +00:00
|
|
|
hv_exc_entry(regs);
|
2021-05-25 10:57:46 +00:00
|
|
|
bool handled = false;
|
2021-05-27 12:11:49 +00:00
|
|
|
u64 esr = hv_get_esr();
|
2021-05-04 10:36:23 +00:00
|
|
|
u32 ec = FIELD_GET(ESR_EC, esr);
|
|
|
|
|
|
|
|
switch (ec) {
|
|
|
|
case ESR_EC_DABORT_LOWER:
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('D');
|
2021-05-25 10:57:46 +00:00
|
|
|
handled = hv_handle_dabort(regs);
|
2021-05-04 10:36:23 +00:00
|
|
|
break;
|
2021-05-25 11:08:35 +00:00
|
|
|
case ESR_EC_MSR:
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('M');
|
2021-05-25 11:08:35 +00:00
|
|
|
handled = hv_handle_msr(regs, FIELD_GET(ESR_ISS, esr));
|
|
|
|
break;
|
|
|
|
case ESR_EC_IMPDEF:
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('A');
|
2021-05-25 11:08:35 +00:00
|
|
|
switch (FIELD_GET(ESR_ISS, esr)) {
|
|
|
|
case ESR_ISS_IMPDEF_MSR:
|
2021-05-29 18:29:52 +00:00
|
|
|
handled = hv_handle_msr(regs, hv_get_afsr1());
|
2021-05-25 11:08:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2021-05-04 10:36:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 12:16:17 +00:00
|
|
|
if (handled) {
|
|
|
|
hv_wdt_breadcrumb('+');
|
2021-05-27 12:11:49 +00:00
|
|
|
hv_set_elr(hv_get_elr() + 4);
|
2021-05-27 12:16:17 +00:00
|
|
|
} else {
|
|
|
|
hv_wdt_breadcrumb('-');
|
2021-05-25 10:57:46 +00:00
|
|
|
hv_exc_proxy(regs, START_EXCEPTION_LOWER, EXC_SYNC, NULL);
|
2021-05-27 12:16:17 +00:00
|
|
|
}
|
2021-05-25 11:07:02 +00:00
|
|
|
|
|
|
|
hv_exc_exit(regs);
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('s');
|
2021-05-04 10:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hv_exc_irq(u64 *regs)
|
|
|
|
{
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('I');
|
2021-05-27 15:38:42 +00:00
|
|
|
hv_exc_entry(regs);
|
2021-05-15 14:55:34 +00:00
|
|
|
hv_exc_proxy(regs, START_EXCEPTION_LOWER, EXC_IRQ, NULL);
|
2021-05-25 11:07:02 +00:00
|
|
|
hv_exc_exit(regs);
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('i');
|
2021-05-04 10:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hv_exc_fiq(u64 *regs)
|
|
|
|
{
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('F');
|
2021-05-27 15:38:42 +00:00
|
|
|
hv_exc_entry(regs);
|
2021-05-25 11:04:20 +00:00
|
|
|
if (mrs(CNTP_CTL_EL0) == (CNTx_CTL_ISTATUS | CNTx_CTL_ENABLE)) {
|
|
|
|
msr(CNTP_CTL_EL0, CNTx_CTL_ISTATUS | CNTx_CTL_IMASK | CNTx_CTL_ENABLE);
|
2021-05-27 16:24:29 +00:00
|
|
|
hv_tick(regs);
|
2021-05-25 11:04:20 +00:00
|
|
|
hv_arm_tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mrs(CNTV_CTL_EL0) == (CNTx_CTL_ISTATUS | CNTx_CTL_ENABLE)) {
|
|
|
|
msr(CNTV_CTL_EL0, CNTx_CTL_ISTATUS | CNTx_CTL_IMASK | CNTx_CTL_ENABLE);
|
|
|
|
hv_exc_proxy(regs, START_HV, HV_VTIMER, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 reg = mrs(SYS_IMP_APL_PMCR0);
|
|
|
|
if ((reg & (PMCR0_IMODE_MASK | PMCR0_IACT)) == (PMCR0_IMODE_FIQ | PMCR0_IACT)) {
|
|
|
|
printf("[FIQ] PMC IRQ, masking");
|
|
|
|
reg_clr(SYS_IMP_APL_PMCR0, PMCR0_IACT | PMCR0_IMODE_MASK);
|
|
|
|
hv_exc_proxy(regs, START_EXCEPTION_LOWER, EXC_FIQ, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
reg = mrs(SYS_IMP_APL_UPMCR0);
|
|
|
|
if ((reg & UPMCR0_IMODE_MASK) == UPMCR0_IMODE_FIQ && (mrs(SYS_IMP_APL_UPMSR) & UPMSR_IACT)) {
|
|
|
|
printf("[FIQ] UPMC IRQ, masking");
|
|
|
|
reg_clr(SYS_IMP_APL_UPMCR0, UPMCR0_IMODE_MASK);
|
|
|
|
hv_exc_proxy(regs, START_EXCEPTION_LOWER, EXC_FIQ, NULL);
|
|
|
|
}
|
2021-05-25 11:08:35 +00:00
|
|
|
|
2021-05-27 15:38:11 +00:00
|
|
|
if (mrs(SYS_IMP_APL_IPI_SR_EL1) & IPI_SR_PENDING) {
|
2021-05-25 11:08:35 +00:00
|
|
|
ipi_pending = true;
|
2021-05-27 15:38:11 +00:00
|
|
|
msr(SYS_IMP_APL_IPI_SR_EL1, IPI_SR_PENDING);
|
2021-05-25 11:08:35 +00:00
|
|
|
sysop("isb");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles guest timers
|
2021-05-25 11:07:02 +00:00
|
|
|
hv_exc_exit(regs);
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('f');
|
2021-05-04 10:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hv_exc_serr(u64 *regs)
|
|
|
|
{
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('E');
|
2021-05-27 15:38:42 +00:00
|
|
|
hv_exc_entry(regs);
|
2021-05-15 14:55:34 +00:00
|
|
|
hv_exc_proxy(regs, START_EXCEPTION_LOWER, EXC_SERROR, NULL);
|
2021-05-25 11:07:02 +00:00
|
|
|
hv_exc_exit(regs);
|
2021-05-27 12:16:17 +00:00
|
|
|
hv_wdt_breadcrumb('e');
|
2021-05-04 10:36:23 +00:00
|
|
|
}
|