| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2001 Scott Finneran |
| 4 | * Copyright (C) 2002 Intel Corporation |
| 5 | * Copyright (C) 2005,2006 Joerg Wunsch |
| 6 | * Copyright (C) 2007, Colin O'Flynn |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License Version 2 |
| 10 | * as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 20 | * |
| 21 | * This file contains the breakpoint handling for the mkII |
| 22 | * protocol. It has been re-written by Colin O'Flynn. |
| 23 | * |
| 24 | * $Id: jtag2bp.cc 323 2013-01-07 13:26:06Z joerg_wunsch $ |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <stdarg.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <termios.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include "avarice.h" |
| 38 | #include "jtag.h" |
| 39 | #include "jtag2.h" |
| 40 | |
| 41 | bool jtag2::codeBreakpointAt(unsigned int address) |
| 42 | { |
| 43 | int i; |
| 44 | |
| 45 | i = 0; |
| 46 | while (!bp[i].last) |
| 47 | { |
| 48 | if ((bp[i].address == address) && (bp[i].type == CODE) && bp[i].enabled) |
| 49 | return true; |
| 50 | |
| 51 | i++; |
| 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | void jtag2::deleteAllBreakpoints(void) |
| 58 | { |
| 59 | int i = 0; |
| 60 | |
| 61 | while (!bp[i].last) |
| 62 | { |
| 63 | if (bp[i].enabled && bp[i].icestatus) |
| 64 | bp[i].toremove = true; |
| 65 | |
| 66 | bp[i].enabled = false; |
| 67 | i++; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | #ifdef notyet |
| 73 | bool jtag2::BreakpointRoom(bpType type, unsigned int length) |
| 74 | { |
| 75 | int numdata = 0; |
| 76 | int numcode = 0; |
| 77 | int bp_i = 0; |
| 78 | |
| 79 | //code |
| 80 | if (type == CODE) |
| 81 | { |
| 82 | numcode++; |
| 83 | } |
| 84 | |
| 85 | //data |
| 86 | else if ((bp[bp_i].type == READ_DATA) || |
| 87 | (bp[bp_i].type == WRITE_DATA) || |
| 88 | (bp[bp_i].type == ACCESS_DATA)) |
| 89 | { |
| 90 | // range BPs use two slots |
| 91 | if (length > 1) |
| 92 | { |
| 93 | numdata += 2; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | numdata++; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // First let's check debugWire which doesn't let you have data breakpoints |
| 102 | if (useDebugWire) |
| 103 | { |
| 104 | if (numdata > 0) |
| 105 | { |
| 106 | debugOut("DebugWire doesn't support data breakpoints.\n"); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Count current breakpoints |
| 112 | while (!bp[bp_i].last) |
| 113 | { |
| 114 | // how many breakpoints are we predicting to be enabled? |
| 115 | if (bp[bp_i].toadd || bp[bp_i].enabled) |
| 116 | { |
| 117 | if ((bp[bp_i].type == READ_DATA) || |
| 118 | (bp[bp_i].type == WRITE_DATA) || |
| 119 | (bp[bp_i].type == ACCESS_DATA) || |
| 120 | (bp[bp_i].type == DATA_MASK)) |
| 121 | { |
| 122 | numdata++; |
| 123 | } |
| 124 | else if (bp[bp_i].type == CODE) |
| 125 | { |
| 126 | numcode++; |
| 127 | } |
| 128 | } |
| 129 | bp_i++; |
| 130 | } |
| 131 | |
| 132 | // Some devices don't support software breakpoints. Also there |
| 133 | // might be an upper limit on software breakpoints, do we care? |
| 134 | if ((deviceDef->device_flags & DEVFL_NO_SOFTBP) != 0) |
| 135 | { |
| 136 | // There are only 3 hardware BPs available, the fourth one |
| 137 | // is reserved by the ICE for single-stepping. |
| 138 | if ((numcode > (MAX_BREAKPOINTS2_CODE - 1)) || |
| 139 | ((numcode + numdata) > (MAX_BREAKPOINTS2 - 1))) |
| 140 | { |
| 141 | return false; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (numdata > MAX_BREAKPOINTS2_DATA) |
| 146 | { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // Your room is ready |
| 151 | return true; |
| 152 | } |
| 153 | #endif // notyet |
| 154 | |
| 155 | void jtag2::updateBreakpoints(void) |
| 156 | { |
| 157 | int bp_i; |
| 158 | |
| 159 | layoutBreakpoints(); |
| 160 | |
| 161 | // Delete all the breakpoints that were flagged first |
| 162 | bp_i = 0; |
| 163 | while (!bp[bp_i].last) |
| 164 | { |
| 165 | uchar cmd[6] = { CMND_CLR_BREAK }; |
| 166 | |
| 167 | if (bp[bp_i].toremove) |
| 168 | { |
| 169 | debugOut("Breakpoint deleted in ICE. slot: %d type: %d addr: 0x%x\n", |
| 170 | bp[bp_i].bpnum, bp[bp_i].type, bp[bp_i].address); |
| 171 | |
| 172 | if (is_xmega && has_full_xmega_support && |
| 173 | bp[bp_i].type == CODE && bp[bp_i].bpnum != 0x00) |
| 174 | { |
| 175 | // no action needed on this one, has been auto-removed |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | cmd[1] = bp[bp_i].bpnum; |
| 180 | |
| 181 | // Software breakpoints need the address! |
| 182 | if (bp[bp_i].bpnum == 0x00) |
| 183 | u32_to_b4(cmd + 2, (bp[bp_i].address / 2)); |
| 184 | else |
| 185 | u32_to_b4(cmd + 2, 0); |
| 186 | |
| 187 | uchar *response; |
| 188 | int responseSize; |
| 189 | try |
| 190 | { |
| 191 | doJtagCommand(cmd, 6, response, responseSize); |
| 192 | } |
| 193 | catch (jtag_exception& e) |
| 194 | { |
| 195 | fprintf(stderr, "Failed to clear breakpoint: %s\n", |
| 196 | e.what()); |
| 197 | throw; |
| 198 | } |
| 199 | |
| 200 | delete [] response; |
| 201 | } |
| 202 | |
| 203 | // rip breakpoint |
| 204 | bp[bp_i].icestatus = false; |
| 205 | bp[bp_i].toremove = false; |
| 206 | } |
| 207 | |
| 208 | bp_i++; |
| 209 | } |
| 210 | |
| 211 | // Add all the new breakpoints |
| 212 | bp_i = 0; |
| 213 | while (!bp[bp_i].last) |
| 214 | { |
| 215 | uchar cmd[8] = { CMND_SET_BREAK }; |
| 216 | |
| 217 | if (bp[bp_i].toadd && bp[bp_i].enabled) |
| 218 | { |
| 219 | debugOut("Breakpoint added in ICE. slot: %d type: %d addr: 0x%x\n", |
| 220 | bp[bp_i].bpnum, bp[bp_i].type, bp[bp_i].address); |
| 221 | |
| 222 | if (is_xmega && has_full_xmega_support && |
| 223 | bp[bp_i].type == CODE && bp[bp_i].bpnum != 0x00) |
| 224 | { |
| 225 | if (xmega_n_bps >= 2) |
| 226 | throw jtag_exception("Too many hard BPs for Xmega"); |
| 227 | // Xmega code breakpoint |
| 228 | xmega_bps[xmega_n_bps++] = bp[bp_i].address; |
| 229 | // these breakpoints are auto-removed by the ICE |
| 230 | bp[bp_i].toremove = true; |
| 231 | bp[bp_i].icestatus = false; |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | cmd[2] = bp[bp_i].bpnum; |
| 236 | |
| 237 | if (bp[bp_i].type == CODE) |
| 238 | { |
| 239 | // The JTAG box sees program memory as 16-bit |
| 240 | // wide locations. GDB sees bytes. As such, |
| 241 | // halve the breakpoint address. |
| 242 | u32_to_b4(cmd + 3, (bp[bp_i].address / 2)); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | u32_to_b4(cmd + 3, bp[bp_i].address & ~ADDR_SPACE_MASK); |
| 247 | } |
| 248 | |
| 249 | // cmd[7] is the BP mode (memory read/write/read or write/code) |
| 250 | // cmd[1] is the BP type (program memory, data, data mask) |
| 251 | switch (bp[bp_i].type) |
| 252 | { |
| 253 | case READ_DATA: |
| 254 | cmd[7] = 0x00; |
| 255 | cmd[1] = 0x02; |
| 256 | break; |
| 257 | case WRITE_DATA: |
| 258 | cmd[7] = 0x01; |
| 259 | cmd[1] = 0x02; |
| 260 | break; |
| 261 | case ACCESS_DATA: |
| 262 | cmd[7] = 0x02; |
| 263 | cmd[1] = 0x02; |
| 264 | break; |
| 265 | case DATA_MASK: |
| 266 | cmd[7] = 0x00; |
| 267 | cmd[1] = 0x03; |
| 268 | break; |
| 269 | case CODE: |
| 270 | cmd[7] = 0x03; |
| 271 | cmd[1] = 0x01; |
| 272 | break; |
| 273 | default: |
| 274 | throw jtag_exception("Invalid bp mode (for data bp)"); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | uchar *response; |
| 279 | int responseSize; |
| 280 | try |
| 281 | { |
| 282 | doJtagCommand(cmd, 8, response, responseSize); |
| 283 | } |
| 284 | catch (jtag_exception& e) |
| 285 | { |
| 286 | fprintf(stderr, "Failed to set breakpoint: %s\n", |
| 287 | e.what()); |
| 288 | throw; |
| 289 | } |
| 290 | delete [] response; |
| 291 | |
| 292 | bp[bp_i].icestatus = true; |
| 293 | } |
| 294 | |
| 295 | // It's a beautiful baby breakpoint |
| 296 | bp[bp_i].toadd = false; |
| 297 | } |
| 298 | |
| 299 | bp_i++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void jtag2::xmegaSendBPs(void) |
| 304 | { |
| 305 | if (!(is_xmega && has_full_xmega_support)) |
| 306 | return; |
| 307 | |
| 308 | uchar *response; |
| 309 | int responseSize; |
| 310 | uchar cmdx[14] = { CMND_SET_BREAK_XMEGA }; |
| 311 | if (xmega_n_bps > 0) |
| 312 | u32_to_b4(cmdx + 1, (xmega_bps[0] / 2)); |
| 313 | if (xmega_n_bps > 1) |
| 314 | u32_to_b4(cmdx + 5, (xmega_bps[1] / 2)); |
| 315 | cmdx[12] = xmega_n_bps; |
| 316 | try |
| 317 | { |
| 318 | doJtagCommand(cmdx, 14, response, responseSize); |
| 319 | } |
| 320 | catch (jtag_exception& e) |
| 321 | { |
| 322 | fprintf(stderr, "Failed to set Xmega breakpoints: %s\n", |
| 323 | e.what()); |
| 324 | throw; |
| 325 | } |
| 326 | delete [] response; |
| 327 | |
| 328 | xmega_n_bps = 0; // must be set again upon next run |
| 329 | } |