2019-08-05 22:49:25 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-07 20:38:40 +01:00
|
|
|
#include <string.h>
|
2019-08-06 05:09:51 +01:00
|
|
|
#include "watchpoints.h"
|
2019-08-07 22:51:25 +01:00
|
|
|
#include "breakpoints_watchpoints_load.h"
|
2019-08-05 22:49:25 +01:00
|
|
|
#include "utils.h"
|
2020-01-21 00:05:01 +00:00
|
|
|
#include "core_ctx.h"
|
|
|
|
#include "execute_function.h"
|
2019-08-07 20:38:40 +01:00
|
|
|
#include "debug_log.h"
|
2019-08-05 22:49:25 +01:00
|
|
|
|
2019-08-06 05:09:51 +01:00
|
|
|
WatchpointManager g_watchpointManager = {0};
|
2019-08-07 20:38:40 +01:00
|
|
|
static TEMPORARY DebugRegisterPair g_combinedWatchpoints[16] = {0};
|
|
|
|
|
|
|
|
static void combineAllCurrentWatchpoints(void);
|
2019-08-06 05:09:51 +01:00
|
|
|
|
|
|
|
// Init the structure (already in BSS, so already zero-initialized) and load the registers
|
|
|
|
void initWatchpoints(void)
|
2019-08-05 22:49:25 +01:00
|
|
|
{
|
2019-08-06 05:09:51 +01:00
|
|
|
recursiveSpinlockLock(&g_watchpointManager.lock);
|
|
|
|
|
|
|
|
if (currentCoreCtx->isBootCore && !currentCoreCtx->warmboot) {
|
2019-08-06 21:26:28 +01:00
|
|
|
size_t num = ((GET_SYSREG(id_aa64dfr0_el1) >> 20) & 0xF) + 1;
|
2019-08-06 05:09:51 +01:00
|
|
|
g_watchpointManager.maxWatchpoints = (u32)num;
|
2019-08-07 20:38:40 +01:00
|
|
|
g_watchpointManager.maxSplitWatchpoints = 8 * (u32)num;
|
|
|
|
g_watchpointManager.allocationBitmap = BIT(num) - 1;
|
|
|
|
} else if (currentCoreCtx->isBootCore) {
|
|
|
|
combineAllCurrentWatchpoints();
|
2019-08-06 05:09:51 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:38:40 +01:00
|
|
|
loadWatchpointRegs(g_combinedWatchpoints, g_watchpointManager.maxWatchpoints);
|
2019-08-05 22:49:25 +01:00
|
|
|
|
2019-08-06 05:09:51 +01:00
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-05 22:49:25 +01:00
|
|
|
}
|
2019-08-07 20:38:40 +01:00
|
|
|
|
2019-08-12 23:14:56 +01:00
|
|
|
static void commitAndBroadcastWatchpointHandler(void *p)
|
|
|
|
{
|
|
|
|
(void)p;
|
|
|
|
u64 flags = maskIrq();
|
|
|
|
loadWatchpointRegs(g_combinedWatchpoints, g_watchpointManager.maxWatchpoints);
|
|
|
|
restoreInterruptFlags(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void commitAndBroadcastWatchpoints(void)
|
|
|
|
{
|
2020-01-15 02:42:07 +00:00
|
|
|
__dmb();
|
2019-08-12 23:14:56 +01:00
|
|
|
executeFunctionOnAllCores(commitAndBroadcastWatchpointHandler, NULL, true);
|
|
|
|
}
|
|
|
|
|
2019-08-07 20:38:40 +01:00
|
|
|
static DebugRegisterPair *findCombinedWatchpoint(u64 addr)
|
|
|
|
{
|
|
|
|
addr &= ~7ull;
|
|
|
|
u16 bitmap = ~g_watchpointManager.allocationBitmap & 0xFFFF;
|
|
|
|
while (bitmap != 0) {
|
|
|
|
u32 pos = __builtin_ffs(bitmap);
|
|
|
|
if (pos == 0) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
bitmap &= ~BIT(pos - 1);
|
|
|
|
if (g_combinedWatchpoints[pos - 1].vr == addr) {
|
|
|
|
return &g_combinedWatchpoints[pos - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DebugRegisterPair *allocateCombinedWatchpoint(u16 *bitmap)
|
|
|
|
{
|
|
|
|
u32 pos = __builtin_ffs(*bitmap);
|
|
|
|
if (pos == 0) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
*bitmap &= ~BIT(pos - 1);
|
|
|
|
return &g_combinedWatchpoints[pos - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precondition: not a MASK-based watchpoint
|
|
|
|
static bool checkNormalWatchpointRange(u64 addr, size_t size)
|
|
|
|
{
|
|
|
|
u16 bitmap = g_watchpointManager.allocationBitmap;
|
|
|
|
if (findCombinedWatchpoint(addr) == NULL) {
|
|
|
|
if (allocateCombinedWatchpoint(&bitmap) == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it overlaps...
|
|
|
|
u64 addr2 = (addr + size) & ~7ull;
|
|
|
|
|
|
|
|
if (addr2 != (addr & ~7ull)) {
|
|
|
|
if (findCombinedWatchpoint(addr2) == NULL) {
|
|
|
|
return allocateCombinedWatchpoint(&bitmap) != NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isRangeMaskWatchpoint(u64 addr, size_t size)
|
|
|
|
{
|
|
|
|
// size needs to be a power of 2, at least 8 (we'll only allow 16+ though), addr needs to be aligned.
|
|
|
|
bool ret = (size & (size - 1)) == 0 && size >= 16 && (addr & (size - 1)) == 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool combineWatchpoint(const DebugRegisterPair *wp)
|
|
|
|
{
|
|
|
|
DebugRegisterPair *wpSlot = NULL;
|
|
|
|
|
|
|
|
wpSlot = findCombinedWatchpoint(wp->vr & ~7ull);
|
|
|
|
|
|
|
|
// To simplify, don't allow combining for wps that use MASK (except if only perms needs to be combined)
|
|
|
|
if (wp->cr.mask != 0 && wpSlot != NULL && (wp->cr.mask != wpSlot->cr.mask || wp->vr != wpSlot->vr)) {
|
|
|
|
wpSlot = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wpSlot == NULL) {
|
|
|
|
wpSlot = allocateCombinedWatchpoint(&g_watchpointManager.allocationBitmap);
|
|
|
|
memset(wpSlot, 0, sizeof(DebugRegisterPair));
|
|
|
|
wpSlot->vr = wp->vr & ~7ull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wpSlot == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpSlot->cr.hmc = DebugHmc_LowerEl;
|
|
|
|
wpSlot->cr.ssc = DebugSsc_NonSecure;
|
|
|
|
wpSlot->cr.pmc = DebugPmc_El1And0;
|
|
|
|
wpSlot->cr.enabled = true;
|
|
|
|
|
|
|
|
// Merge 8-byte selection mask and access permissions (possibly broadening them)
|
|
|
|
wpSlot->cr.bas |= wp->cr.bas;
|
|
|
|
wpSlot->cr.lsc |= wp->cr.lsc;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DebugRegisterPair *doFindSplitWatchpoint(u64 addr, size_t size, WatchpointLoadStoreControl direction, bool strict)
|
|
|
|
{
|
|
|
|
// Note: we will use RES0 bit0_1 of wr in case of overlapping
|
|
|
|
for (u32 i = 0; i < g_watchpointManager.numSplitWatchpoints; i++) {
|
|
|
|
DebugRegisterPair *wp = &g_watchpointManager.splitWatchpoints[i];
|
|
|
|
if (wp->vr & 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t off = 0;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
|
|
|
if (wp->cr.mask != 0) {
|
|
|
|
off = 0;
|
|
|
|
sz = size;
|
|
|
|
} else {
|
|
|
|
off = __builtin_ffs(wp->cr.bas) - 1;
|
|
|
|
sz = __builtin_popcount(wp->cr.bas);
|
|
|
|
if (wp->vr & 1) {
|
|
|
|
DebugRegisterPair *wp2 = &g_watchpointManager.splitWatchpoints[i + 1];
|
|
|
|
sz += __builtin_popcount(wp2->cr.bas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 wpaddr = (wp->vr & ~7ull) + off;
|
|
|
|
if (strict) {
|
|
|
|
if (addr == wpaddr && direction == wp->cr.lsc && sz == size) {
|
|
|
|
return wp;
|
|
|
|
}
|
|
|
|
} else if (overlaps(wpaddr, sz, addr, size) && (direction & wp->cr.lsc) != 0) {
|
|
|
|
return wp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugRegisterPair *findSplitWatchpoint(u64 addr, size_t size, WatchpointLoadStoreControl direction, bool strict)
|
|
|
|
{
|
|
|
|
recursiveSpinlockLock(&g_watchpointManager.lock);
|
|
|
|
DebugRegisterPair *ret = doFindSplitWatchpoint(addr, size, direction, strict);
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:38:13 +01:00
|
|
|
int addWatchpoint(u64 addr, size_t size, WatchpointLoadStoreControl direction)
|
2019-08-07 20:38:40 +01:00
|
|
|
{
|
|
|
|
if (size == 0) {
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EINVAL;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
recursiveSpinlockLock(&g_watchpointManager.lock);
|
|
|
|
|
|
|
|
if (doFindSplitWatchpoint(addr, size, direction, true)) {
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EEXIST;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (g_watchpointManager.numSplitWatchpoints == g_watchpointManager.maxSplitWatchpoints) {
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EBUSY;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t oldNumSplitWatchpoints = g_watchpointManager.numSplitWatchpoints;
|
|
|
|
DebugRegisterPair *wp = &g_watchpointManager.splitWatchpoints[g_watchpointManager.numSplitWatchpoints++], *wp2 = NULL;
|
|
|
|
|
|
|
|
memset(wp, 0, sizeof(DebugRegisterPair));
|
|
|
|
wp->cr.lsc = direction;
|
|
|
|
if (isRangeMaskWatchpoint(addr, size)) {
|
|
|
|
wp->vr = addr;
|
|
|
|
wp->cr.bas = 0xFF; // TRM-mandated
|
|
|
|
wp->cr.mask = (u32)__builtin_ffsl(size) - 1;
|
|
|
|
if (!combineWatchpoint(wp)) {
|
|
|
|
g_watchpointManager.numSplitWatchpoints = oldNumSplitWatchpoints;
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EBUSY;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
} else if (size <= 9) {
|
|
|
|
// Normal one or 2 up-to-9-bytes wp(s) (ie. never exceeeds two combined wp)
|
|
|
|
// Note: we will use RES0 bit0_1 of wr in case of overlapping
|
|
|
|
if (!checkNormalWatchpointRange(addr, size)) {
|
|
|
|
g_watchpointManager.numSplitWatchpoints = oldNumSplitWatchpoints;
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EINVAL;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 addr2 = (addr + size) & ~7ull;
|
|
|
|
size_t off1 = addr & 7ull;
|
|
|
|
size_t size1 = (addr != addr2) ? 8 - off1 : size;
|
|
|
|
size_t size2 = size - size1;
|
|
|
|
wp->vr = addr & ~7ull;
|
|
|
|
wp->cr.bas = MASK2(off1 + size1, off1);
|
|
|
|
|
|
|
|
if (size2 != 0) {
|
|
|
|
if (g_watchpointManager.numSplitWatchpoints == g_watchpointManager.maxSplitWatchpoints) {
|
|
|
|
g_watchpointManager.numSplitWatchpoints = oldNumSplitWatchpoints;
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wp2 = &g_watchpointManager.splitWatchpoints[g_watchpointManager.numSplitWatchpoints++];
|
|
|
|
wp2->cr.lsc = direction;
|
|
|
|
wp2->cr.bas = MASK2(size2, 0);
|
|
|
|
|
|
|
|
// Note: we will use RES0 bit0_1 of wr in case of overlapping
|
|
|
|
wp->vr |= 1;
|
|
|
|
wp2->vr = addr2 | 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!combineWatchpoint(wp) || (size2 != 0 && !combineWatchpoint(wp2))) {
|
|
|
|
g_watchpointManager.numSplitWatchpoints = oldNumSplitWatchpoints;
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EBUSY;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EINVAL;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
2019-08-12 23:14:56 +01:00
|
|
|
commitAndBroadcastWatchpoints();
|
2019-08-07 20:38:40 +01:00
|
|
|
|
2019-08-12 23:14:56 +01:00
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-07 20:38:40 +01:00
|
|
|
|
2019-08-08 20:38:13 +01:00
|
|
|
return 0;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void combineAllCurrentWatchpoints(void)
|
|
|
|
{
|
|
|
|
memset(g_combinedWatchpoints, 0, sizeof(g_combinedWatchpoints));
|
|
|
|
g_watchpointManager.allocationBitmap = BIT(g_watchpointManager.maxWatchpoints) - 1;
|
|
|
|
for (u32 i = 0; i < g_watchpointManager.numSplitWatchpoints; i++) {
|
|
|
|
combineWatchpoint(&g_watchpointManager.splitWatchpoints[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:38:13 +01:00
|
|
|
int removeWatchpoint(u64 addr, size_t size, WatchpointLoadStoreControl direction)
|
2019-08-07 20:38:40 +01:00
|
|
|
{
|
|
|
|
if (size == 0) {
|
2019-08-08 20:38:13 +01:00
|
|
|
return -EINVAL;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
recursiveSpinlockLock(&g_watchpointManager.lock);
|
|
|
|
|
|
|
|
DebugRegisterPair *wp = doFindSplitWatchpoint(addr, size, direction, true);
|
|
|
|
if (wp != NULL) {
|
|
|
|
size_t pos = wp - &g_watchpointManager.splitWatchpoints[0];
|
|
|
|
size_t num = (wp->vr & 1) ? 2 : 1;
|
|
|
|
for (size_t i = pos + num; i < g_watchpointManager.numSplitWatchpoints; i ++) {
|
|
|
|
g_watchpointManager.splitWatchpoints[i - num] = g_watchpointManager.splitWatchpoints[i];
|
|
|
|
}
|
|
|
|
g_watchpointManager.numSplitWatchpoints -= num;
|
|
|
|
combineAllCurrentWatchpoints();
|
|
|
|
} else {
|
|
|
|
DEBUG("watchpoint not found 0x%016llx, size %llu, direction %d\n", addr, size, direction);
|
2019-08-08 20:38:13 +01:00
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
|
|
|
return -ENOENT;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
|
|
|
|
2019-08-12 23:14:56 +01:00
|
|
|
commitAndBroadcastWatchpoints();
|
|
|
|
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
2019-08-07 20:38:40 +01:00
|
|
|
|
2019-08-08 20:38:13 +01:00
|
|
|
return 0;
|
2019-08-07 20:38:40 +01:00
|
|
|
}
|
2019-08-08 20:38:13 +01:00
|
|
|
|
|
|
|
int removeAllWatchpoints(void)
|
|
|
|
{
|
|
|
|
// Yeet it all
|
|
|
|
|
|
|
|
recursiveSpinlockLock(&g_watchpointManager.lock);
|
|
|
|
|
|
|
|
g_watchpointManager.allocationBitmap = BIT(g_watchpointManager.maxWatchpoints) - 1;
|
|
|
|
g_watchpointManager.numSplitWatchpoints = 0;
|
|
|
|
memset(g_watchpointManager.splitWatchpoints, 0, sizeof(g_watchpointManager.splitWatchpoints));
|
|
|
|
memset(g_combinedWatchpoints, 0, sizeof(g_combinedWatchpoints));
|
|
|
|
|
2019-08-12 23:14:56 +01:00
|
|
|
commitAndBroadcastWatchpoints();
|
2019-08-08 20:38:13 +01:00
|
|
|
|
|
|
|
recursiveSpinlockUnlock(&g_watchpointManager.lock);
|
|
|
|
|
|
|
|
return 0;
|
2020-01-09 22:47:22 +00:00
|
|
|
}
|