2019-08-05 00:21:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "single_step.h"
|
|
|
|
#include "core_ctx.h"
|
|
|
|
#include "sysreg.h"
|
2020-01-31 23:54:42 +00:00
|
|
|
#include "debug_manager.h"
|
2019-08-05 00:21:18 +00:00
|
|
|
|
2019-08-05 02:46:11 +00:00
|
|
|
SingleStepState singleStepGetNextState(ExceptionStackFrame *frame)
|
2019-08-05 00:21:18 +00:00
|
|
|
{
|
|
|
|
u64 mdscr = GET_SYSREG(mdscr_el1);
|
2020-01-07 02:17:05 +00:00
|
|
|
bool mdscrSS = (mdscr & MDSCR_SS) != 0;
|
2019-08-05 02:46:11 +00:00
|
|
|
bool pstateSS = (frame->spsr_el2 & PSTATE_SS) != 0;
|
2019-08-05 00:21:18 +00:00
|
|
|
|
2019-08-05 02:46:11 +00:00
|
|
|
if (!mdscrSS) {
|
|
|
|
return SingleStepState_Inactive;
|
|
|
|
} else {
|
2020-01-16 01:18:34 +00:00
|
|
|
return pstateSS ? SingleStepState_ActiveNotPending : SingleStepState_ActivePending;
|
2019-08-05 02:46:11 +00:00
|
|
|
}
|
2019-08-05 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 02:46:11 +00:00
|
|
|
void singleStepSetNextState(ExceptionStackFrame *frame, SingleStepState state)
|
2019-08-05 00:21:18 +00:00
|
|
|
{
|
2019-08-05 02:46:11 +00:00
|
|
|
u64 mdscr = GET_SYSREG(mdscr_el1);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SingleStepState_Inactive:
|
|
|
|
// Unset mdscr_el1.ss
|
2020-01-07 02:17:05 +00:00
|
|
|
mdscr &= ~MDSCR_SS;
|
2019-08-05 02:46:11 +00:00
|
|
|
break;
|
2020-01-16 01:18:34 +00:00
|
|
|
case SingleStepState_ActiveNotPending:
|
2019-08-05 02:46:11 +00:00
|
|
|
// Set mdscr_el1.ss and pstate.ss
|
2020-01-07 02:17:05 +00:00
|
|
|
mdscr |= MDSCR_SS;
|
2019-08-05 02:46:11 +00:00
|
|
|
frame->spsr_el2 |= PSTATE_SS;
|
|
|
|
break;
|
2020-01-16 01:18:34 +00:00
|
|
|
case SingleStepState_ActivePending:
|
|
|
|
// We never use this because pstate.ss is 0 by default...
|
2019-08-05 02:46:11 +00:00
|
|
|
// Set mdscr_el1.ss and unset pstate.ss
|
2020-01-07 02:17:05 +00:00
|
|
|
mdscr |= MDSCR_SS;
|
2020-01-16 01:18:34 +00:00
|
|
|
frame->spsr_el2 &= ~PSTATE_SS;
|
2019-08-05 02:46:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-08-05 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 02:46:11 +00:00
|
|
|
SET_SYSREG(mdscr_el1, mdscr);
|
2020-01-09 22:47:22 +00:00
|
|
|
__isb(); // TRM-mandated
|
2019-08-05 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleSingleStep(ExceptionStackFrame *frame, ExceptionSyndromeRegister esr)
|
|
|
|
{
|
2020-01-28 01:13:21 +00:00
|
|
|
uintptr_t addr = frame->elr_el2;
|
|
|
|
|
|
|
|
// Stepping range support;
|
|
|
|
if (addr >= currentCoreCtx->steppingRangeStartAddr && addr < currentCoreCtx->steppingRangeEndAddr) {
|
|
|
|
// Reactivate single-step
|
|
|
|
singleStepSetNextState(frame, SingleStepState_ActiveNotPending);
|
|
|
|
} else {
|
|
|
|
// Disable single-step
|
|
|
|
singleStepSetNextState(frame, SingleStepState_Inactive);
|
2020-01-31 23:54:42 +00:00
|
|
|
debugManagerReportEvent(DBGEVENT_EXCEPTION);
|
2020-01-28 01:13:21 +00:00
|
|
|
}
|
2019-08-05 02:46:11 +00:00
|
|
|
|
2019-08-05 00:21:18 +00:00
|
|
|
DEBUG("Single-step exeception ELR = 0x%016llx, ISV = %u, EX = %u\n", frame->elr_el2, (esr.iss >> 24) & 1, (esr.iss >> 6) & 1);
|
|
|
|
}
|