target-memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Parts of target interface that deal with accessing memory and memory-like
  2. objects.
  3. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "target.h"
  17. #include "memory-map.h"
  18. #include "inferior.h"
  19. #include "gdbsupport/gdb_sys_time.h"
  20. #include <algorithm>
  21. static bool
  22. compare_block_starting_address (const memory_write_request &a_req,
  23. const memory_write_request &b_req)
  24. {
  25. return a_req.begin < b_req.begin;
  26. }
  27. /* Adds to RESULT all memory write requests from BLOCK that are
  28. in [BEGIN, END) range.
  29. If any memory request is only partially in the specified range,
  30. that part of the memory request will be added. */
  31. static void
  32. claim_memory (const std::vector<memory_write_request> &blocks,
  33. std::vector<memory_write_request> *result,
  34. ULONGEST begin,
  35. ULONGEST end)
  36. {
  37. ULONGEST claimed_begin;
  38. ULONGEST claimed_end;
  39. for (const memory_write_request &r : blocks)
  40. {
  41. /* If the request doesn't overlap [BEGIN, END), skip it. We
  42. must handle END == 0 meaning the top of memory; we don't yet
  43. check for R->end == 0, which would also mean the top of
  44. memory, but there's an assertion in
  45. target_write_memory_blocks which checks for that. */
  46. if (begin >= r.end)
  47. continue;
  48. if (end != 0 && end <= r.begin)
  49. continue;
  50. claimed_begin = std::max (begin, r.begin);
  51. if (end == 0)
  52. claimed_end = r.end;
  53. else
  54. claimed_end = std::min (end, r.end);
  55. if (claimed_begin == r.begin && claimed_end == r.end)
  56. result->push_back (r);
  57. else
  58. {
  59. struct memory_write_request n = r;
  60. n.begin = claimed_begin;
  61. n.end = claimed_end;
  62. n.data += claimed_begin - r.begin;
  63. result->push_back (n);
  64. }
  65. }
  66. }
  67. /* Given a vector of struct memory_write_request objects in BLOCKS,
  68. add memory requests for flash memory into FLASH_BLOCKS, and for
  69. regular memory to REGULAR_BLOCKS. */
  70. static void
  71. split_regular_and_flash_blocks (const std::vector<memory_write_request> &blocks,
  72. std::vector<memory_write_request> *regular_blocks,
  73. std::vector<memory_write_request> *flash_blocks)
  74. {
  75. struct mem_region *region;
  76. CORE_ADDR cur_address;
  77. /* This implementation runs in O(length(regions)*length(blocks)) time.
  78. However, in most cases the number of blocks will be small, so this does
  79. not matter.
  80. Note also that it's extremely unlikely that a memory write request
  81. will span more than one memory region, however for safety we handle
  82. such situations. */
  83. cur_address = 0;
  84. while (1)
  85. {
  86. std::vector<memory_write_request> *r;
  87. region = lookup_mem_region (cur_address);
  88. r = region->attrib.mode == MEM_FLASH ? flash_blocks : regular_blocks;
  89. cur_address = region->hi;
  90. claim_memory (blocks, r, region->lo, region->hi);
  91. if (cur_address == 0)
  92. break;
  93. }
  94. }
  95. /* Given an ADDRESS, if BEGIN is non-NULL this function sets *BEGIN
  96. to the start of the flash block containing the address. Similarly,
  97. if END is non-NULL *END will be set to the address one past the end
  98. of the block containing the address. */
  99. static void
  100. block_boundaries (CORE_ADDR address, CORE_ADDR *begin, CORE_ADDR *end)
  101. {
  102. struct mem_region *region;
  103. unsigned blocksize;
  104. CORE_ADDR offset_in_region;
  105. region = lookup_mem_region (address);
  106. gdb_assert (region->attrib.mode == MEM_FLASH);
  107. blocksize = region->attrib.blocksize;
  108. offset_in_region = address - region->lo;
  109. if (begin)
  110. *begin = region->lo + offset_in_region / blocksize * blocksize;
  111. if (end)
  112. *end = region->lo + (offset_in_region + blocksize - 1) / blocksize * blocksize;
  113. }
  114. /* Given the list of memory requests to be WRITTEN, this function
  115. returns write requests covering each group of flash blocks which must
  116. be erased. */
  117. static std::vector<memory_write_request>
  118. blocks_to_erase (const std::vector<memory_write_request> &written)
  119. {
  120. std::vector<memory_write_request> result;
  121. for (const memory_write_request &request : written)
  122. {
  123. CORE_ADDR begin, end;
  124. block_boundaries (request.begin, &begin, 0);
  125. block_boundaries (request.end - 1, 0, &end);
  126. if (!result.empty () && result.back ().end >= begin)
  127. result.back ().end = end;
  128. else
  129. result.emplace_back (begin, end);
  130. }
  131. return result;
  132. }
  133. /* Given ERASED_BLOCKS, a list of blocks that will be erased with
  134. flash erase commands, and WRITTEN_BLOCKS, the list of memory
  135. addresses that will be written, compute the set of memory addresses
  136. that will be erased but not rewritten (e.g. padding within a block
  137. which is only partially filled by "load"). */
  138. static std::vector<memory_write_request>
  139. compute_garbled_blocks (const std::vector<memory_write_request> &erased_blocks,
  140. const std::vector<memory_write_request> &written_blocks)
  141. {
  142. std::vector<memory_write_request> result;
  143. unsigned j;
  144. unsigned je = written_blocks.size ();
  145. /* Look at each erased memory_write_request in turn, and
  146. see what part of it is subsequently written to.
  147. This implementation is O(length(erased) * length(written)). If
  148. the lists are sorted at this point it could be rewritten more
  149. efficiently, but the complexity is not generally worthwhile. */
  150. for (const memory_write_request &erased_iter : erased_blocks)
  151. {
  152. /* Make a deep copy -- it will be modified inside the loop, but
  153. we don't want to modify original vector. */
  154. struct memory_write_request erased = erased_iter;
  155. for (j = 0; j != je;)
  156. {
  157. const memory_write_request *written = &written_blocks[j];
  158. /* Now try various cases. */
  159. /* If WRITTEN is fully to the left of ERASED, check the next
  160. written memory_write_request. */
  161. if (written->end <= erased.begin)
  162. {
  163. ++j;
  164. continue;
  165. }
  166. /* If WRITTEN is fully to the right of ERASED, then ERASED
  167. is not written at all. WRITTEN might affect other
  168. blocks. */
  169. if (written->begin >= erased.end)
  170. {
  171. result.push_back (erased);
  172. goto next_erased;
  173. }
  174. /* If all of ERASED is completely written, we can move on to
  175. the next erased region. */
  176. if (written->begin <= erased.begin
  177. && written->end >= erased.end)
  178. {
  179. goto next_erased;
  180. }
  181. /* If there is an unwritten part at the beginning of ERASED,
  182. then we should record that part and try this inner loop
  183. again for the remainder. */
  184. if (written->begin > erased.begin)
  185. {
  186. result.emplace_back (erased.begin, written->begin);
  187. erased.begin = written->begin;
  188. continue;
  189. }
  190. /* If there is an unwritten part at the end of ERASED, we
  191. forget about the part that was written to and wait to see
  192. if the next write request writes more of ERASED. We can't
  193. push it yet. */
  194. if (written->end < erased.end)
  195. {
  196. erased.begin = written->end;
  197. ++j;
  198. continue;
  199. }
  200. }
  201. /* If we ran out of write requests without doing anything about
  202. ERASED, then that means it's really erased. */
  203. result.push_back (erased);
  204. next_erased:
  205. ;
  206. }
  207. return result;
  208. }
  209. int
  210. target_write_memory_blocks (const std::vector<memory_write_request> &requests,
  211. enum flash_preserve_mode preserve_flash_p,
  212. void (*progress_cb) (ULONGEST, void *))
  213. {
  214. std::vector<memory_write_request> blocks = requests;
  215. std::vector<memory_write_request> regular;
  216. std::vector<memory_write_request> flash;
  217. std::vector<memory_write_request> erased, garbled;
  218. /* END == 0 would represent wraparound: a write to the very last
  219. byte of the address space. This file was not written with that
  220. possibility in mind. This is fixable, but a lot of work for a
  221. rare problem; so for now, fail noisily here instead of obscurely
  222. later. */
  223. for (const memory_write_request &iter : requests)
  224. gdb_assert (iter.end != 0);
  225. /* Sort the blocks by their start address. */
  226. std::sort (blocks.begin (), blocks.end (), compare_block_starting_address);
  227. /* Split blocks into list of regular memory blocks,
  228. and list of flash memory blocks. */
  229. split_regular_and_flash_blocks (blocks, &regular, &flash);
  230. /* If a variable is added to forbid flash write, even during "load",
  231. it should be checked here. Similarly, if this function is used
  232. for other situations besides "load" in which writing to flash
  233. is undesirable, that should be checked here. */
  234. /* Find flash blocks to erase. */
  235. erased = blocks_to_erase (flash);
  236. /* Find what flash regions will be erased, and not overwritten; then
  237. either preserve or discard the old contents. */
  238. garbled = compute_garbled_blocks (erased, flash);
  239. std::vector<gdb::unique_xmalloc_ptr<gdb_byte>> mem_holders;
  240. if (!garbled.empty ())
  241. {
  242. if (preserve_flash_p == flash_preserve)
  243. {
  244. /* Read in regions that must be preserved and add them to
  245. the list of blocks we read. */
  246. for (memory_write_request &iter : garbled)
  247. {
  248. gdb_assert (iter.data == NULL);
  249. gdb::unique_xmalloc_ptr<gdb_byte> holder
  250. ((gdb_byte *) xmalloc (iter.end - iter.begin));
  251. iter.data = holder.get ();
  252. mem_holders.push_back (std::move (holder));
  253. int err = target_read_memory (iter.begin, iter.data,
  254. iter.end - iter.begin);
  255. if (err != 0)
  256. return err;
  257. flash.push_back (iter);
  258. }
  259. std::sort (flash.begin (), flash.end (),
  260. compare_block_starting_address);
  261. }
  262. }
  263. /* We could coalesce adjacent memory blocks here, to reduce the
  264. number of write requests for small sections. However, we would
  265. have to reallocate and copy the data pointers, which could be
  266. large; large sections are more common in loadable objects than
  267. large numbers of small sections (although the reverse can be true
  268. in object files). So, we issue at least one write request per
  269. passed struct memory_write_request. The remote stub will still
  270. have the opportunity to batch flash requests. */
  271. /* Write regular blocks. */
  272. for (const memory_write_request &iter : regular)
  273. {
  274. LONGEST len;
  275. len = target_write_with_progress (current_inferior ()->top_target (),
  276. TARGET_OBJECT_MEMORY, NULL,
  277. iter.data, iter.begin,
  278. iter.end - iter.begin,
  279. progress_cb, iter.baton);
  280. if (len < (LONGEST) (iter.end - iter.begin))
  281. {
  282. /* Call error? */
  283. return -1;
  284. }
  285. }
  286. if (!erased.empty ())
  287. {
  288. /* Erase all pages. */
  289. for (const memory_write_request &iter : erased)
  290. target_flash_erase (iter.begin, iter.end - iter.begin);
  291. /* Write flash data. */
  292. for (const memory_write_request &iter : flash)
  293. {
  294. LONGEST len;
  295. len = target_write_with_progress (current_inferior ()->top_target (),
  296. TARGET_OBJECT_FLASH, NULL,
  297. iter.data, iter.begin,
  298. iter.end - iter.begin,
  299. progress_cb, iter.baton);
  300. if (len < (LONGEST) (iter.end - iter.begin))
  301. error (_("Error writing data to flash"));
  302. }
  303. target_flash_done ();
  304. }
  305. return 0;
  306. }