1 /* Parts of target interface that deal with accessing memory and memory-like 2 objects. 3 4 Copyright (C) 2006-2016 Free Software Foundation, Inc. 5 6 This file is part of GDB. 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 as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 21 #include "defs.h" 22 #include "vec.h" 23 #include "target.h" 24 #include "memory-map.h" 25 26 #include "gdb_sys_time.h" 27 28 static int 29 compare_block_starting_address (const void *a, const void *b) 30 { 31 const struct memory_write_request *a_req 32 = (const struct memory_write_request *) a; 33 const struct memory_write_request *b_req 34 = (const struct memory_write_request *) b; 35 36 if (a_req->begin < b_req->begin) 37 return -1; 38 else if (a_req->begin == b_req->begin) 39 return 0; 40 else 41 return 1; 42 } 43 44 /* Adds to RESULT all memory write requests from BLOCK that are 45 in [BEGIN, END) range. 46 47 If any memory request is only partially in the specified range, 48 that part of the memory request will be added. */ 49 50 static void 51 claim_memory (VEC(memory_write_request_s) *blocks, 52 VEC(memory_write_request_s) **result, 53 ULONGEST begin, 54 ULONGEST end) 55 { 56 int i; 57 ULONGEST claimed_begin; 58 ULONGEST claimed_end; 59 struct memory_write_request *r; 60 61 for (i = 0; VEC_iterate (memory_write_request_s, blocks, i, r); ++i) 62 { 63 /* If the request doesn't overlap [BEGIN, END), skip it. We 64 must handle END == 0 meaning the top of memory; we don't yet 65 check for R->end == 0, which would also mean the top of 66 memory, but there's an assertion in 67 target_write_memory_blocks which checks for that. */ 68 69 if (begin >= r->end) 70 continue; 71 if (end != 0 && end <= r->begin) 72 continue; 73 74 claimed_begin = max (begin, r->begin); 75 if (end == 0) 76 claimed_end = r->end; 77 else 78 claimed_end = min (end, r->end); 79 80 if (claimed_begin == r->begin && claimed_end == r->end) 81 VEC_safe_push (memory_write_request_s, *result, r); 82 else 83 { 84 struct memory_write_request *n = 85 VEC_safe_push (memory_write_request_s, *result, NULL); 86 87 *n = *r; 88 n->begin = claimed_begin; 89 n->end = claimed_end; 90 n->data += claimed_begin - r->begin; 91 } 92 } 93 } 94 95 /* Given a vector of struct memory_write_request objects in BLOCKS, 96 add memory requests for flash memory into FLASH_BLOCKS, and for 97 regular memory to REGULAR_BLOCKS. */ 98 99 static void 100 split_regular_and_flash_blocks (VEC(memory_write_request_s) *blocks, 101 VEC(memory_write_request_s) **regular_blocks, 102 VEC(memory_write_request_s) **flash_blocks) 103 { 104 struct mem_region *region; 105 CORE_ADDR cur_address; 106 107 /* This implementation runs in O(length(regions)*length(blocks)) time. 108 However, in most cases the number of blocks will be small, so this does 109 not matter. 110 111 Note also that it's extremely unlikely that a memory write request 112 will span more than one memory region, however for safety we handle 113 such situations. */ 114 115 cur_address = 0; 116 while (1) 117 { 118 VEC(memory_write_request_s) **r; 119 120 region = lookup_mem_region (cur_address); 121 r = region->attrib.mode == MEM_FLASH ? flash_blocks : regular_blocks; 122 cur_address = region->hi; 123 claim_memory (blocks, r, region->lo, region->hi); 124 125 if (cur_address == 0) 126 break; 127 } 128 } 129 130 /* Given an ADDRESS, if BEGIN is non-NULL this function sets *BEGIN 131 to the start of the flash block containing the address. Similarly, 132 if END is non-NULL *END will be set to the address one past the end 133 of the block containing the address. */ 134 135 static void 136 block_boundaries (CORE_ADDR address, CORE_ADDR *begin, CORE_ADDR *end) 137 { 138 struct mem_region *region; 139 unsigned blocksize; 140 141 region = lookup_mem_region (address); 142 gdb_assert (region->attrib.mode == MEM_FLASH); 143 blocksize = region->attrib.blocksize; 144 if (begin) 145 *begin = address / blocksize * blocksize; 146 if (end) 147 *end = (address + blocksize - 1) / blocksize * blocksize; 148 } 149 150 /* Given the list of memory requests to be WRITTEN, this function 151 returns write requests covering each group of flash blocks which must 152 be erased. */ 153 154 static VEC(memory_write_request_s) * 155 blocks_to_erase (VEC(memory_write_request_s) *written) 156 { 157 unsigned i; 158 struct memory_write_request *ptr; 159 160 VEC(memory_write_request_s) *result = NULL; 161 162 for (i = 0; VEC_iterate (memory_write_request_s, written, i, ptr); ++i) 163 { 164 CORE_ADDR begin, end; 165 166 block_boundaries (ptr->begin, &begin, 0); 167 block_boundaries (ptr->end - 1, 0, &end); 168 169 if (!VEC_empty (memory_write_request_s, result) 170 && VEC_last (memory_write_request_s, result)->end >= begin) 171 { 172 VEC_last (memory_write_request_s, result)->end = end; 173 } 174 else 175 { 176 struct memory_write_request *n = 177 VEC_safe_push (memory_write_request_s, result, NULL); 178 179 memset (n, 0, sizeof (struct memory_write_request)); 180 n->begin = begin; 181 n->end = end; 182 } 183 } 184 185 return result; 186 } 187 188 /* Given ERASED_BLOCKS, a list of blocks that will be erased with 189 flash erase commands, and WRITTEN_BLOCKS, the list of memory 190 addresses that will be written, compute the set of memory addresses 191 that will be erased but not rewritten (e.g. padding within a block 192 which is only partially filled by "load"). */ 193 194 static VEC(memory_write_request_s) * 195 compute_garbled_blocks (VEC(memory_write_request_s) *erased_blocks, 196 VEC(memory_write_request_s) *written_blocks) 197 { 198 VEC(memory_write_request_s) *result = NULL; 199 200 unsigned i, j; 201 unsigned je = VEC_length (memory_write_request_s, written_blocks); 202 struct memory_write_request *erased_p; 203 204 /* Look at each erased memory_write_request in turn, and 205 see what part of it is subsequently written to. 206 207 This implementation is O(length(erased) * length(written)). If 208 the lists are sorted at this point it could be rewritten more 209 efficiently, but the complexity is not generally worthwhile. */ 210 211 for (i = 0; 212 VEC_iterate (memory_write_request_s, erased_blocks, i, erased_p); 213 ++i) 214 { 215 /* Make a deep copy -- it will be modified inside the loop, but 216 we don't want to modify original vector. */ 217 struct memory_write_request erased = *erased_p; 218 219 for (j = 0; j != je;) 220 { 221 struct memory_write_request *written 222 = VEC_index (memory_write_request_s, 223 written_blocks, j); 224 225 /* Now try various cases. */ 226 227 /* If WRITTEN is fully to the left of ERASED, check the next 228 written memory_write_request. */ 229 if (written->end <= erased.begin) 230 { 231 ++j; 232 continue; 233 } 234 235 /* If WRITTEN is fully to the right of ERASED, then ERASED 236 is not written at all. WRITTEN might affect other 237 blocks. */ 238 if (written->begin >= erased.end) 239 { 240 VEC_safe_push (memory_write_request_s, result, &erased); 241 goto next_erased; 242 } 243 244 /* If all of ERASED is completely written, we can move on to 245 the next erased region. */ 246 if (written->begin <= erased.begin 247 && written->end >= erased.end) 248 { 249 goto next_erased; 250 } 251 252 /* If there is an unwritten part at the beginning of ERASED, 253 then we should record that part and try this inner loop 254 again for the remainder. */ 255 if (written->begin > erased.begin) 256 { 257 struct memory_write_request *n = 258 VEC_safe_push (memory_write_request_s, result, NULL); 259 260 memset (n, 0, sizeof (struct memory_write_request)); 261 n->begin = erased.begin; 262 n->end = written->begin; 263 erased.begin = written->begin; 264 continue; 265 } 266 267 /* If there is an unwritten part at the end of ERASED, we 268 forget about the part that was written to and wait to see 269 if the next write request writes more of ERASED. We can't 270 push it yet. */ 271 if (written->end < erased.end) 272 { 273 erased.begin = written->end; 274 ++j; 275 continue; 276 } 277 } 278 279 /* If we ran out of write requests without doing anything about 280 ERASED, then that means it's really erased. */ 281 VEC_safe_push (memory_write_request_s, result, &erased); 282 283 next_erased: 284 ; 285 } 286 287 return result; 288 } 289 290 static void 291 cleanup_request_data (void *p) 292 { 293 VEC(memory_write_request_s) **v = (VEC(memory_write_request_s) **) p; 294 struct memory_write_request *r; 295 int i; 296 297 for (i = 0; VEC_iterate (memory_write_request_s, *v, i, r); ++i) 298 xfree (r->data); 299 } 300 301 static void 302 cleanup_write_requests_vector (void *p) 303 { 304 VEC(memory_write_request_s) **v = (VEC(memory_write_request_s) **) p; 305 306 VEC_free (memory_write_request_s, *v); 307 } 308 309 int 310 target_write_memory_blocks (VEC(memory_write_request_s) *requests, 311 enum flash_preserve_mode preserve_flash_p, 312 void (*progress_cb) (ULONGEST, void *)) 313 { 314 struct cleanup *back_to = make_cleanup (null_cleanup, NULL); 315 VEC(memory_write_request_s) *blocks = VEC_copy (memory_write_request_s, 316 requests); 317 unsigned i; 318 int err = 0; 319 struct memory_write_request *r; 320 VEC(memory_write_request_s) *regular = NULL; 321 VEC(memory_write_request_s) *flash = NULL; 322 VEC(memory_write_request_s) *erased, *garbled; 323 324 /* END == 0 would represent wraparound: a write to the very last 325 byte of the address space. This file was not written with that 326 possibility in mind. This is fixable, but a lot of work for a 327 rare problem; so for now, fail noisily here instead of obscurely 328 later. */ 329 for (i = 0; VEC_iterate (memory_write_request_s, requests, i, r); ++i) 330 gdb_assert (r->end != 0); 331 332 make_cleanup (cleanup_write_requests_vector, &blocks); 333 334 /* Sort the blocks by their start address. */ 335 qsort (VEC_address (memory_write_request_s, blocks), 336 VEC_length (memory_write_request_s, blocks), 337 sizeof (struct memory_write_request), compare_block_starting_address); 338 339 /* Split blocks into list of regular memory blocks, 340 and list of flash memory blocks. */ 341 make_cleanup (cleanup_write_requests_vector, ®ular); 342 make_cleanup (cleanup_write_requests_vector, &flash); 343 split_regular_and_flash_blocks (blocks, ®ular, &flash); 344 345 /* If a variable is added to forbid flash write, even during "load", 346 it should be checked here. Similarly, if this function is used 347 for other situations besides "load" in which writing to flash 348 is undesirable, that should be checked here. */ 349 350 /* Find flash blocks to erase. */ 351 erased = blocks_to_erase (flash); 352 make_cleanup (cleanup_write_requests_vector, &erased); 353 354 /* Find what flash regions will be erased, and not overwritten; then 355 either preserve or discard the old contents. */ 356 garbled = compute_garbled_blocks (erased, flash); 357 make_cleanup (cleanup_request_data, &garbled); 358 make_cleanup (cleanup_write_requests_vector, &garbled); 359 360 if (!VEC_empty (memory_write_request_s, garbled)) 361 { 362 if (preserve_flash_p == flash_preserve) 363 { 364 struct memory_write_request *r; 365 366 /* Read in regions that must be preserved and add them to 367 the list of blocks we read. */ 368 for (i = 0; VEC_iterate (memory_write_request_s, garbled, i, r); ++i) 369 { 370 gdb_assert (r->data == NULL); 371 r->data = (gdb_byte *) xmalloc (r->end - r->begin); 372 err = target_read_memory (r->begin, r->data, r->end - r->begin); 373 if (err != 0) 374 goto out; 375 376 VEC_safe_push (memory_write_request_s, flash, r); 377 } 378 379 qsort (VEC_address (memory_write_request_s, flash), 380 VEC_length (memory_write_request_s, flash), 381 sizeof (struct memory_write_request), 382 compare_block_starting_address); 383 } 384 } 385 386 /* We could coalesce adjacent memory blocks here, to reduce the 387 number of write requests for small sections. However, we would 388 have to reallocate and copy the data pointers, which could be 389 large; large sections are more common in loadable objects than 390 large numbers of small sections (although the reverse can be true 391 in object files). So, we issue at least one write request per 392 passed struct memory_write_request. The remote stub will still 393 have the opportunity to batch flash requests. */ 394 395 /* Write regular blocks. */ 396 for (i = 0; VEC_iterate (memory_write_request_s, regular, i, r); ++i) 397 { 398 LONGEST len; 399 400 len = target_write_with_progress (current_target.beneath, 401 TARGET_OBJECT_MEMORY, NULL, 402 r->data, r->begin, r->end - r->begin, 403 progress_cb, r->baton); 404 if (len < (LONGEST) (r->end - r->begin)) 405 { 406 /* Call error? */ 407 err = -1; 408 goto out; 409 } 410 } 411 412 if (!VEC_empty (memory_write_request_s, erased)) 413 { 414 /* Erase all pages. */ 415 for (i = 0; VEC_iterate (memory_write_request_s, erased, i, r); ++i) 416 target_flash_erase (r->begin, r->end - r->begin); 417 418 /* Write flash data. */ 419 for (i = 0; VEC_iterate (memory_write_request_s, flash, i, r); ++i) 420 { 421 LONGEST len; 422 423 len = target_write_with_progress (¤t_target, 424 TARGET_OBJECT_FLASH, NULL, 425 r->data, r->begin, 426 r->end - r->begin, 427 progress_cb, r->baton); 428 if (len < (LONGEST) (r->end - r->begin)) 429 error (_("Error writing data to flash")); 430 } 431 432 target_flash_done (); 433 } 434 435 out: 436 do_cleanups (back_to); 437 438 return err; 439 } 440