1*0fca6ea1SDimitry Andric //===-- WatchpointAlgorithms.cpp ------------------------------------------===// 2*0fca6ea1SDimitry Andric // 3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0fca6ea1SDimitry Andric // 7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===// 8*0fca6ea1SDimitry Andric 9*0fca6ea1SDimitry Andric #include "lldb/Breakpoint/WatchpointAlgorithms.h" 10*0fca6ea1SDimitry Andric #include "lldb/Breakpoint/WatchpointResource.h" 11*0fca6ea1SDimitry Andric #include "lldb/Target/Process.h" 12*0fca6ea1SDimitry Andric #include "lldb/Utility/ArchSpec.h" 13*0fca6ea1SDimitry Andric #include "lldb/Utility/LLDBLog.h" 14*0fca6ea1SDimitry Andric #include "lldb/Utility/Log.h" 15*0fca6ea1SDimitry Andric 16*0fca6ea1SDimitry Andric #include <algorithm> 17*0fca6ea1SDimitry Andric #include <utility> 18*0fca6ea1SDimitry Andric #include <vector> 19*0fca6ea1SDimitry Andric 20*0fca6ea1SDimitry Andric using namespace lldb; 21*0fca6ea1SDimitry Andric using namespace lldb_private; 22*0fca6ea1SDimitry Andric 23*0fca6ea1SDimitry Andric std::vector<WatchpointResourceSP> 24*0fca6ea1SDimitry Andric WatchpointAlgorithms::AtomizeWatchpointRequest( 25*0fca6ea1SDimitry Andric addr_t addr, size_t size, bool read, bool write, 26*0fca6ea1SDimitry Andric WatchpointHardwareFeature supported_features, ArchSpec &arch) { 27*0fca6ea1SDimitry Andric 28*0fca6ea1SDimitry Andric std::vector<Region> entries; 29*0fca6ea1SDimitry Andric 30*0fca6ea1SDimitry Andric if (supported_features & eWatchpointHardwareArmMASK) { 31*0fca6ea1SDimitry Andric entries = 32*0fca6ea1SDimitry Andric PowerOf2Watchpoints(addr, size, 33*0fca6ea1SDimitry Andric /*min_byte_size*/ 1, 34*0fca6ea1SDimitry Andric /*max_byte_size*/ INT32_MAX, 35*0fca6ea1SDimitry Andric /*address_byte_size*/ arch.GetAddressByteSize()); 36*0fca6ea1SDimitry Andric } else { 37*0fca6ea1SDimitry Andric // As a fallback, assume we can watch any power-of-2 38*0fca6ea1SDimitry Andric // number of bytes up through the size of an address in the target. 39*0fca6ea1SDimitry Andric entries = 40*0fca6ea1SDimitry Andric PowerOf2Watchpoints(addr, size, 41*0fca6ea1SDimitry Andric /*min_byte_size*/ 1, 42*0fca6ea1SDimitry Andric /*max_byte_size*/ arch.GetAddressByteSize(), 43*0fca6ea1SDimitry Andric /*address_byte_size*/ arch.GetAddressByteSize()); 44*0fca6ea1SDimitry Andric } 45*0fca6ea1SDimitry Andric 46*0fca6ea1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints); 47*0fca6ea1SDimitry Andric LLDB_LOGV(log, "AtomizeWatchpointRequest user request addr {0:x} size {1}", 48*0fca6ea1SDimitry Andric addr, size); 49*0fca6ea1SDimitry Andric std::vector<WatchpointResourceSP> resources; 50*0fca6ea1SDimitry Andric for (Region &ent : entries) { 51*0fca6ea1SDimitry Andric LLDB_LOGV(log, "AtomizeWatchpointRequest creating resource {0:x} size {1}", 52*0fca6ea1SDimitry Andric ent.addr, ent.size); 53*0fca6ea1SDimitry Andric WatchpointResourceSP wp_res_sp = 54*0fca6ea1SDimitry Andric std::make_shared<WatchpointResource>(ent.addr, ent.size, read, write); 55*0fca6ea1SDimitry Andric resources.push_back(wp_res_sp); 56*0fca6ea1SDimitry Andric } 57*0fca6ea1SDimitry Andric 58*0fca6ea1SDimitry Andric return resources; 59*0fca6ea1SDimitry Andric } 60*0fca6ea1SDimitry Andric 61*0fca6ea1SDimitry Andric // This should be `std::bit_ceil(aligned_size)` but 62*0fca6ea1SDimitry Andric // that requires C++20. 63*0fca6ea1SDimitry Andric // Calculates the smallest integral power of two that is not smaller than x. 64*0fca6ea1SDimitry Andric static uint64_t bit_ceil(uint64_t input) { 65*0fca6ea1SDimitry Andric if (input <= 1 || llvm::popcount(input) == 1) 66*0fca6ea1SDimitry Andric return input; 67*0fca6ea1SDimitry Andric 68*0fca6ea1SDimitry Andric return 1ULL << (64 - llvm::countl_zero(input)); 69*0fca6ea1SDimitry Andric } 70*0fca6ea1SDimitry Andric 71*0fca6ea1SDimitry Andric /// Convert a user's watchpoint request (\a user_addr and \a user_size) 72*0fca6ea1SDimitry Andric /// into hardware watchpoints, for a target that can watch a power-of-2 73*0fca6ea1SDimitry Andric /// region of memory (1, 2, 4, 8, etc), aligned to that same power-of-2 74*0fca6ea1SDimitry Andric /// memory address. 75*0fca6ea1SDimitry Andric /// 76*0fca6ea1SDimitry Andric /// If a user asks to watch 4 bytes at address 0x1002 (0x1002-0x1005 77*0fca6ea1SDimitry Andric /// inclusive) we can implement this with two 2-byte watchpoints 78*0fca6ea1SDimitry Andric /// (0x1002 and 0x1004) or with an 8-byte watchpoint at 0x1000. 79*0fca6ea1SDimitry Andric /// A 4-byte watchpoint at 0x1002 would not be properly 4 byte aligned. 80*0fca6ea1SDimitry Andric /// 81*0fca6ea1SDimitry Andric /// If a user asks to watch 16 bytes at 0x1000, and this target supports 82*0fca6ea1SDimitry Andric /// 8-byte watchpoints, we can implement this with two 8-byte watchpoints 83*0fca6ea1SDimitry Andric /// at 0x1000 and 0x1008. 84*0fca6ea1SDimitry Andric std::vector<WatchpointAlgorithms::Region> 85*0fca6ea1SDimitry Andric WatchpointAlgorithms::PowerOf2Watchpoints(addr_t user_addr, size_t user_size, 86*0fca6ea1SDimitry Andric size_t min_byte_size, 87*0fca6ea1SDimitry Andric size_t max_byte_size, 88*0fca6ea1SDimitry Andric uint32_t address_byte_size) { 89*0fca6ea1SDimitry Andric 90*0fca6ea1SDimitry Andric Log *log = GetLog(LLDBLog::Watchpoints); 91*0fca6ea1SDimitry Andric LLDB_LOGV(log, 92*0fca6ea1SDimitry Andric "AtomizeWatchpointRequest user request addr {0:x} size {1} " 93*0fca6ea1SDimitry Andric "min_byte_size {2}, max_byte_size {3}, address_byte_size {4}", 94*0fca6ea1SDimitry Andric user_addr, user_size, min_byte_size, max_byte_size, 95*0fca6ea1SDimitry Andric address_byte_size); 96*0fca6ea1SDimitry Andric 97*0fca6ea1SDimitry Andric // Can't watch zero bytes. 98*0fca6ea1SDimitry Andric if (user_size == 0) 99*0fca6ea1SDimitry Andric return {}; 100*0fca6ea1SDimitry Andric 101*0fca6ea1SDimitry Andric size_t aligned_size = std::max(user_size, min_byte_size); 102*0fca6ea1SDimitry Andric /// Round up \a user_size to the next power-of-2 size 103*0fca6ea1SDimitry Andric /// user_size == 8 -> aligned_size == 8 104*0fca6ea1SDimitry Andric /// user_size == 9 -> aligned_size == 16 105*0fca6ea1SDimitry Andric aligned_size = bit_ceil(aligned_size); 106*0fca6ea1SDimitry Andric 107*0fca6ea1SDimitry Andric addr_t aligned_start = user_addr & ~(aligned_size - 1); 108*0fca6ea1SDimitry Andric 109*0fca6ea1SDimitry Andric // Does this power-of-2 memory range, aligned to power-of-2 that the 110*0fca6ea1SDimitry Andric // hardware can watch, completely cover the requested region. 111*0fca6ea1SDimitry Andric if (aligned_size <= max_byte_size && 112*0fca6ea1SDimitry Andric aligned_start + aligned_size >= user_addr + user_size) 113*0fca6ea1SDimitry Andric return {{aligned_start, aligned_size}}; 114*0fca6ea1SDimitry Andric 115*0fca6ea1SDimitry Andric // If the maximum region we can watch is larger than the aligned 116*0fca6ea1SDimitry Andric // size, try increasing the region size by one power of 2 and see 117*0fca6ea1SDimitry Andric // if aligning to that amount can cover the requested region. 118*0fca6ea1SDimitry Andric // 119*0fca6ea1SDimitry Andric // Increasing the aligned_size repeatedly instead of splitting the 120*0fca6ea1SDimitry Andric // watchpoint can result in us watching large regions of memory 121*0fca6ea1SDimitry Andric // unintentionally when we could use small two watchpoints. e.g. 122*0fca6ea1SDimitry Andric // user_addr 0x3ff8 user_size 32 123*0fca6ea1SDimitry Andric // can be watched with four 8-byte watchpoints or if it's done with one 124*0fca6ea1SDimitry Andric // MASK watchpoint, it would need to be a 32KB watchpoint (a 16KB 125*0fca6ea1SDimitry Andric // watchpoint at 0x0 only covers 0x0000-0x4000). A user request 126*0fca6ea1SDimitry Andric // at the end of a power-of-2 region can lead to these undesirably 127*0fca6ea1SDimitry Andric // large watchpoints and many false positive hits to ignore. 128*0fca6ea1SDimitry Andric if (max_byte_size >= (aligned_size << 1)) { 129*0fca6ea1SDimitry Andric aligned_size <<= 1; 130*0fca6ea1SDimitry Andric aligned_start = user_addr & ~(aligned_size - 1); 131*0fca6ea1SDimitry Andric if (aligned_size <= max_byte_size && 132*0fca6ea1SDimitry Andric aligned_start + aligned_size >= user_addr + user_size) 133*0fca6ea1SDimitry Andric return {{aligned_start, aligned_size}}; 134*0fca6ea1SDimitry Andric 135*0fca6ea1SDimitry Andric // Go back to our original aligned size, to try the multiple 136*0fca6ea1SDimitry Andric // watchpoint approach. 137*0fca6ea1SDimitry Andric aligned_size >>= 1; 138*0fca6ea1SDimitry Andric } 139*0fca6ea1SDimitry Andric 140*0fca6ea1SDimitry Andric // We need to split the user's watchpoint into two or more watchpoints 141*0fca6ea1SDimitry Andric // that can be monitored by hardware, because of alignment and/or size 142*0fca6ea1SDimitry Andric // reasons. 143*0fca6ea1SDimitry Andric aligned_size = std::min(aligned_size, max_byte_size); 144*0fca6ea1SDimitry Andric aligned_start = user_addr & ~(aligned_size - 1); 145*0fca6ea1SDimitry Andric 146*0fca6ea1SDimitry Andric std::vector<Region> result; 147*0fca6ea1SDimitry Andric addr_t current_address = aligned_start; 148*0fca6ea1SDimitry Andric const addr_t user_end_address = user_addr + user_size; 149*0fca6ea1SDimitry Andric while (current_address + aligned_size < user_end_address) { 150*0fca6ea1SDimitry Andric result.push_back({current_address, aligned_size}); 151*0fca6ea1SDimitry Andric current_address += aligned_size; 152*0fca6ea1SDimitry Andric } 153*0fca6ea1SDimitry Andric 154*0fca6ea1SDimitry Andric if (current_address < user_end_address) 155*0fca6ea1SDimitry Andric result.push_back({current_address, aligned_size}); 156*0fca6ea1SDimitry Andric 157*0fca6ea1SDimitry Andric return result; 158*0fca6ea1SDimitry Andric } 159