1 /* Parts of target interface that deal with accessing memory and memory-like
4 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "memory-map.h"
27 #include "gdb_assert.h"
33 compare_block_starting_address (const void *a, const void *b)
35 const struct memory_write_request *a_req = a;
36 const struct memory_write_request *b_req = b;
38 if (a_req->begin < b_req->begin)
40 else if (a_req->begin == b_req->begin)
46 /* Adds to RESULT all memory write requests from BLOCK that are
47 in [BEGIN, END) range.
49 If any memory request is only partially in the specified range,
50 that part of the memory request will be added. */
53 claim_memory (VEC(memory_write_request_s) *blocks,
54 VEC(memory_write_request_s) **result,
59 ULONGEST claimed_begin;
61 struct memory_write_request *r;
63 for (i = 0; VEC_iterate (memory_write_request_s, blocks, i, r); ++i)
65 /* If the request doesn't overlap [BEGIN, END), skip it. We
66 must handle END == 0 meaning the top of memory; we don't yet
67 check for R->end == 0, which would also mean the top of
68 memory, but there's an assertion in
69 target_write_memory_blocks which checks for that. */
73 if (end != 0 && end <= r->begin)
76 claimed_begin = max (begin, r->begin);
80 claimed_end = min (end, r->end);
82 if (claimed_begin == r->begin && claimed_end == r->end)
83 VEC_safe_push (memory_write_request_s, *result, r);
86 struct memory_write_request *n =
87 VEC_safe_push (memory_write_request_s, *result, NULL);
90 n->begin = claimed_begin;
92 n->data += claimed_begin - r->begin;
97 /* Given a vector of struct memory_write_request objects in BLOCKS,
98 add memory requests for flash memory into FLASH_BLOCKS, and for
99 regular memory to REGULAR_BLOCKS. */
102 split_regular_and_flash_blocks (VEC(memory_write_request_s) *blocks,
103 VEC(memory_write_request_s) **regular_blocks,
104 VEC(memory_write_request_s) **flash_blocks)
106 struct mem_region *region;
107 CORE_ADDR cur_address;
109 /* This implementation runs in O(length(regions)*length(blocks)) time.
110 However, in most cases the number of blocks will be small, so this does
113 Note also that it's extremely unlikely that a memory write request
114 will span more than one memory region, however for safety we handle
120 VEC(memory_write_request_s) **r;
122 region = lookup_mem_region (cur_address);
123 r = region->attrib.mode == MEM_FLASH ? flash_blocks : regular_blocks;
124 cur_address = region->hi;
125 claim_memory (blocks, r, region->lo, region->hi);
127 if (cur_address == 0)
132 /* Given an ADDRESS, if BEGIN is non-NULL this function sets *BEGIN
133 to the start of the flash block containing the address. Similarly,
134 if END is non-NULL *END will be set to the address one past the end
135 of the block containing the address. */
138 block_boundaries (CORE_ADDR address, CORE_ADDR *begin, CORE_ADDR *end)
140 struct mem_region *region;
143 region = lookup_mem_region (address);
144 gdb_assert (region->attrib.mode == MEM_FLASH);
145 blocksize = region->attrib.blocksize;
147 *begin = address / blocksize * blocksize;
149 *end = (address + blocksize - 1) / blocksize * blocksize;
152 /* Given the list of memory requests to be WRITTEN, this function
153 returns write requests covering each group of flash blocks which must
156 static VEC(memory_write_request_s) *
157 blocks_to_erase (VEC(memory_write_request_s) *written)
160 struct memory_write_request *ptr;
162 VEC(memory_write_request_s) *result = NULL;
164 for (i = 0; VEC_iterate (memory_write_request_s, written, i, ptr); ++i)
166 CORE_ADDR begin, end;
168 block_boundaries (ptr->begin, &begin, 0);
169 block_boundaries (ptr->end - 1, 0, &end);
171 if (!VEC_empty (memory_write_request_s, result)
172 && VEC_last (memory_write_request_s, result)->end >= begin)
174 VEC_last (memory_write_request_s, result)->end = end;
178 struct memory_write_request *n =
179 VEC_safe_push (memory_write_request_s, result, NULL);
181 memset (n, 0, sizeof (struct memory_write_request));
190 /* Given ERASED_BLOCKS, a list of blocks that will be erased with
191 flash erase commands, and WRITTEN_BLOCKS, the list of memory
192 addresses that will be written, compute the set of memory addresses
193 that will be erased but not rewritten (e.g. padding within a block
194 which is only partially filled by "load"). */
196 static VEC(memory_write_request_s) *
197 compute_garbled_blocks (VEC(memory_write_request_s) *erased_blocks,
198 VEC(memory_write_request_s) *written_blocks)
200 VEC(memory_write_request_s) *result = NULL;
203 unsigned je = VEC_length (memory_write_request_s, written_blocks);
204 struct memory_write_request *erased_p;
206 /* Look at each erased memory_write_request in turn, and
207 see what part of it is subsequently written to.
209 This implementation is O(length(erased) * length(written)). If
210 the lists are sorted at this point it could be rewritten more
211 efficiently, but the complexity is not generally worthwhile. */
214 VEC_iterate (memory_write_request_s, erased_blocks, i, erased_p);
217 /* Make a deep copy -- it will be modified inside the loop, but
218 we don't want to modify original vector. */
219 struct memory_write_request erased = *erased_p;
221 for (j = 0; j != je;)
223 struct memory_write_request *written
224 = VEC_index (memory_write_request_s,
227 /* Now try various cases. */
229 /* If WRITTEN is fully to the left of ERASED, check the next
230 written memory_write_request. */
231 if (written->end <= erased.begin)
237 /* If WRITTEN is fully to the right of ERASED, then ERASED
238 is not written at all. WRITTEN might affect other
240 if (written->begin >= erased.end)
242 VEC_safe_push (memory_write_request_s, result, &erased);
246 /* If all of ERASED is completely written, we can move on to
247 the next erased region. */
248 if (written->begin <= erased.begin
249 && written->end >= erased.end)
254 /* If there is an unwritten part at the beginning of ERASED,
255 then we should record that part and try this inner loop
256 again for the remainder. */
257 if (written->begin > erased.begin)
259 struct memory_write_request *n =
260 VEC_safe_push (memory_write_request_s, result, NULL);
262 memset (n, 0, sizeof (struct memory_write_request));
263 n->begin = erased.begin;
264 n->end = written->begin;
265 erased.begin = written->begin;
269 /* If there is an unwritten part at the end of ERASED, we
270 forget about the part that was written to and wait to see
271 if the next write request writes more of ERASED. We can't
273 if (written->end < erased.end)
275 erased.begin = written->end;
281 /* If we ran out of write requests without doing anything about
282 ERASED, then that means it's really erased. */
283 VEC_safe_push (memory_write_request_s, result, &erased);
293 cleanup_request_data (void *p)
295 VEC(memory_write_request_s) **v = p;
296 struct memory_write_request *r;
299 for (i = 0; VEC_iterate (memory_write_request_s, *v, i, r); ++i)
304 cleanup_write_requests_vector (void *p)
306 VEC(memory_write_request_s) **v = p;
308 VEC_free (memory_write_request_s, *v);
312 target_write_memory_blocks (VEC(memory_write_request_s) *requests,
313 enum flash_preserve_mode preserve_flash_p,
314 void (*progress_cb) (ULONGEST, void *))
316 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
317 VEC(memory_write_request_s) *blocks = VEC_copy (memory_write_request_s,
321 struct memory_write_request *r;
322 VEC(memory_write_request_s) *regular = NULL;
323 VEC(memory_write_request_s) *flash = NULL;
324 VEC(memory_write_request_s) *erased, *garbled;
326 /* END == 0 would represent wraparound: a write to the very last
327 byte of the address space. This file was not written with that
328 possibility in mind. This is fixable, but a lot of work for a
329 rare problem; so for now, fail noisily here instead of obscurely
331 for (i = 0; VEC_iterate (memory_write_request_s, requests, i, r); ++i)
332 gdb_assert (r->end != 0);
334 make_cleanup (cleanup_write_requests_vector, &blocks);
336 /* Sort the blocks by their start address. */
337 qsort (VEC_address (memory_write_request_s, blocks),
338 VEC_length (memory_write_request_s, blocks),
339 sizeof (struct memory_write_request), compare_block_starting_address);
341 /* Split blocks into list of regular memory blocks,
342 and list of flash memory blocks. */
343 make_cleanup (cleanup_write_requests_vector, ®ular);
344 make_cleanup (cleanup_write_requests_vector, &flash);
345 split_regular_and_flash_blocks (blocks, ®ular, &flash);
347 /* If a variable is added to forbid flash write, even during "load",
348 it should be checked here. Similarly, if this function is used
349 for other situations besides "load" in which writing to flash
350 is undesirable, that should be checked here. */
352 /* Find flash blocks to erase. */
353 erased = blocks_to_erase (flash);
354 make_cleanup (cleanup_write_requests_vector, &erased);
356 /* Find what flash regions will be erased, and not overwritten; then
357 either preserve or discard the old contents. */
358 garbled = compute_garbled_blocks (erased, flash);
359 make_cleanup (cleanup_request_data, &garbled);
360 make_cleanup (cleanup_write_requests_vector, &garbled);
362 if (!VEC_empty (memory_write_request_s, garbled))
364 if (preserve_flash_p == flash_preserve)
366 struct memory_write_request *r;
368 /* Read in regions that must be preserved and add them to
369 the list of blocks we read. */
370 for (i = 0; VEC_iterate (memory_write_request_s, garbled, i, r); ++i)
372 gdb_assert (r->data == NULL);
373 r->data = xmalloc (r->end - r->begin);
374 err = target_read_memory (r->begin, r->data, r->end - r->begin);
378 VEC_safe_push (memory_write_request_s, flash, r);
381 qsort (VEC_address (memory_write_request_s, flash),
382 VEC_length (memory_write_request_s, flash),
383 sizeof (struct memory_write_request), compare_block_starting_address);
387 /* We could coalesce adjacent memory blocks here, to reduce the
388 number of write requests for small sections. However, we would
389 have to reallocate and copy the data pointers, which could be
390 large; large sections are more common in loadable objects than
391 large numbers of small sections (although the reverse can be true
392 in object files). So, we issue at least one write request per
393 passed struct memory_write_request. The remote stub will still
394 have the opportunity to batch flash requests. */
396 /* Write regular blocks. */
397 for (i = 0; VEC_iterate (memory_write_request_s, regular, i, r); ++i)
401 len = target_write_with_progress (current_target.beneath,
402 TARGET_OBJECT_MEMORY, NULL,
403 r->data, r->begin, r->end - r->begin,
404 progress_cb, r->baton);
405 if (len < (LONGEST) (r->end - r->begin))
413 if (!VEC_empty (memory_write_request_s, erased))
415 /* Erase all pages. */
416 for (i = 0; VEC_iterate (memory_write_request_s, erased, i, r); ++i)
417 target_flash_erase (r->begin, r->end - r->begin);
419 /* Write flash data. */
420 for (i = 0; VEC_iterate (memory_write_request_s, flash, i, r); ++i)
424 len = target_write_with_progress (¤t_target,
425 TARGET_OBJECT_FLASH, NULL,
426 r->data, r->begin, r->end - r->begin,
427 progress_cb, r->baton);
428 if (len < (LONGEST) (r->end - r->begin))
429 error (_("Error writing data to flash"));
432 target_flash_done ();
436 do_cleanups (back_to);