gdb/target-memory.c - gdb

Functions defined

Source code

  1. /* Parts of target interface that deal with accessing memory and memory-like
  2.    objects.

  3.    Copyright (C) 2006-2015 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 "vec.h"
  17. #include "target.h"
  18. #include "memory-map.h"

  19. #include <sys/time.h>

  20. static int
  21. compare_block_starting_address (const void *a, const void *b)
  22. {
  23.   const struct memory_write_request *a_req = a;
  24.   const struct memory_write_request *b_req = b;

  25.   if (a_req->begin < b_req->begin)
  26.     return -1;
  27.   else if (a_req->begin == b_req->begin)
  28.     return 0;
  29.   else
  30.     return 1;
  31. }

  32. /* Adds to RESULT all memory write requests from BLOCK that are
  33.    in [BEGIN, END) range.

  34.    If any memory request is only partially in the specified range,
  35.    that part of the memory request will be added.  */

  36. static void
  37. claim_memory (VEC(memory_write_request_s) *blocks,
  38.               VEC(memory_write_request_s) **result,
  39.               ULONGEST begin,
  40.               ULONGEST end)
  41. {
  42.   int i;
  43.   ULONGEST claimed_begin;
  44.   ULONGEST claimed_end;
  45.   struct memory_write_request *r;

  46.   for (i = 0; VEC_iterate (memory_write_request_s, blocks, i, r); ++i)
  47.     {
  48.       /* If the request doesn't overlap [BEGIN, END), skip it.  We
  49.          must handle END == 0 meaning the top of memory; we don't yet
  50.          check for R->end == 0, which would also mean the top of
  51.          memory, but there's an assertion in
  52.          target_write_memory_blocks which checks for that.  */

  53.       if (begin >= r->end)
  54.         continue;
  55.       if (end != 0 && end <= r->begin)
  56.         continue;

  57.       claimed_begin = max (begin, r->begin);
  58.       if (end == 0)
  59.         claimed_end = r->end;
  60.       else
  61.         claimed_end = min (end, r->end);

  62.       if (claimed_begin == r->begin && claimed_end == r->end)
  63.         VEC_safe_push (memory_write_request_s, *result, r);
  64.       else
  65.         {
  66.           struct memory_write_request *n =
  67.             VEC_safe_push (memory_write_request_s, *result, NULL);

  68.           *n = *r;
  69.           n->begin = claimed_begin;
  70.           n->end = claimed_end;
  71.           n->data += claimed_begin - r->begin;
  72.         }
  73.     }
  74. }

  75. /* Given a vector of struct memory_write_request objects in BLOCKS,
  76.    add memory requests for flash memory into FLASH_BLOCKS, and for
  77.    regular memory to REGULAR_BLOCKS.  */

  78. static void
  79. split_regular_and_flash_blocks (VEC(memory_write_request_s) *blocks,
  80.                                 VEC(memory_write_request_s) **regular_blocks,
  81.                                 VEC(memory_write_request_s) **flash_blocks)
  82. {
  83.   struct mem_region *region;
  84.   CORE_ADDR cur_address;

  85.   /* This implementation runs in O(length(regions)*length(blocks)) time.
  86.      However, in most cases the number of blocks will be small, so this does
  87.      not matter.

  88.      Note also that it's extremely unlikely that a memory write request
  89.      will span more than one memory region, however for safety we handle
  90.      such situations.  */

  91.   cur_address = 0;
  92.   while (1)
  93.     {
  94.       VEC(memory_write_request_s) **r;

  95.       region = lookup_mem_region (cur_address);
  96.       r = region->attrib.mode == MEM_FLASH ? flash_blocks : regular_blocks;
  97.       cur_address = region->hi;
  98.       claim_memory (blocks, r, region->lo, region->hi);

  99.       if (cur_address == 0)
  100.         break;
  101.     }
  102. }

  103. /* Given an ADDRESS, if BEGIN is non-NULL this function sets *BEGIN
  104.    to the start of the flash block containing the address.  Similarly,
  105.    if END is non-NULL *END will be set to the address one past the end
  106.    of the block containing the address.  */

  107. static void
  108. block_boundaries (CORE_ADDR address, CORE_ADDR *begin, CORE_ADDR *end)
  109. {
  110.   struct mem_region *region;
  111.   unsigned blocksize;

  112.   region = lookup_mem_region (address);
  113.   gdb_assert (region->attrib.mode == MEM_FLASH);
  114.   blocksize = region->attrib.blocksize;
  115.   if (begin)
  116.     *begin = address / blocksize * blocksize;
  117.   if (end)
  118.     *end = (address + blocksize - 1) / blocksize * blocksize;
  119. }

  120. /* Given the list of memory requests to be WRITTEN, this function
  121.    returns write requests covering each group of flash blocks which must
  122.    be erased.  */

  123. static VEC(memory_write_request_s) *
  124. blocks_to_erase (VEC(memory_write_request_s) *written)
  125. {
  126.   unsigned i;
  127.   struct memory_write_request *ptr;

  128.   VEC(memory_write_request_s) *result = NULL;

  129.   for (i = 0; VEC_iterate (memory_write_request_s, written, i, ptr); ++i)
  130.     {
  131.       CORE_ADDR begin, end;

  132.       block_boundaries (ptr->begin, &begin, 0);
  133.       block_boundaries (ptr->end - 1, 0, &end);

  134.       if (!VEC_empty (memory_write_request_s, result)
  135.           && VEC_last (memory_write_request_s, result)->end >= begin)
  136.         {
  137.           VEC_last (memory_write_request_s, result)->end = end;
  138.         }
  139.       else
  140.         {
  141.           struct memory_write_request *n =
  142.             VEC_safe_push (memory_write_request_s, result, NULL);

  143.           memset (n, 0, sizeof (struct memory_write_request));
  144.           n->begin = begin;
  145.           n->end = end;
  146.         }
  147.     }

  148.   return result;
  149. }

  150. /* Given ERASED_BLOCKS, a list of blocks that will be erased with
  151.    flash erase commands, and WRITTEN_BLOCKS, the list of memory
  152.    addresses that will be written, compute the set of memory addresses
  153.    that will be erased but not rewritten (e.g. padding within a block
  154.    which is only partially filled by "load").  */

  155. static VEC(memory_write_request_s) *
  156. compute_garbled_blocks (VEC(memory_write_request_s) *erased_blocks,
  157.                         VEC(memory_write_request_s) *written_blocks)
  158. {
  159.   VEC(memory_write_request_s) *result = NULL;

  160.   unsigned i, j;
  161.   unsigned je = VEC_length (memory_write_request_s, written_blocks);
  162.   struct memory_write_request *erased_p;

  163.   /* Look at each erased memory_write_request in turn, and
  164.      see what part of it is subsequently written to.

  165.      This implementation is O(length(erased) * length(written)).  If
  166.      the lists are sorted at this point it could be rewritten more
  167.      efficiently, but the complexity is not generally worthwhile.  */

  168.   for (i = 0;
  169.        VEC_iterate (memory_write_request_s, erased_blocks, i, erased_p);
  170.        ++i)
  171.     {
  172.       /* Make a deep copy -- it will be modified inside the loop, but
  173.          we don't want to modify original vector.  */
  174.       struct memory_write_request erased = *erased_p;

  175.       for (j = 0; j != je;)
  176.         {
  177.           struct memory_write_request *written
  178.             = VEC_index (memory_write_request_s,
  179.                          written_blocks, j);

  180.           /* Now try various cases.  */

  181.           /* If WRITTEN is fully to the left of ERASED, check the next
  182.              written memory_write_request.  */
  183.           if (written->end <= erased.begin)
  184.             {
  185.               ++j;
  186.               continue;
  187.             }

  188.           /* If WRITTEN is fully to the right of ERASED, then ERASED
  189.              is not written at all.  WRITTEN might affect other
  190.              blocks.  */
  191.           if (written->begin >= erased.end)
  192.             {
  193.               VEC_safe_push (memory_write_request_s, result, &erased);
  194.               goto next_erased;
  195.             }

  196.           /* If all of ERASED is completely written, we can move on to
  197.              the next erased region.  */
  198.           if (written->begin <= erased.begin
  199.               && written->end >= erased.end)
  200.             {
  201.               goto next_erased;
  202.             }

  203.           /* If there is an unwritten part at the beginning of ERASED,
  204.              then we should record that part and try this inner loop
  205.              again for the remainder.  */
  206.           if (written->begin > erased.begin)
  207.             {
  208.               struct memory_write_request *n =
  209.                 VEC_safe_push (memory_write_request_s, result, NULL);

  210.               memset (n, 0, sizeof (struct memory_write_request));
  211.               n->begin = erased.begin;
  212.               n->end = written->begin;
  213.               erased.begin = written->begin;
  214.               continue;
  215.             }

  216.           /* If there is an unwritten part at the end of ERASED, we
  217.              forget about the part that was written to and wait to see
  218.              if the next write request writes more of ERASED.  We can't
  219.              push it yet.  */
  220.           if (written->end < erased.end)
  221.             {
  222.               erased.begin = written->end;
  223.               ++j;
  224.               continue;
  225.             }
  226.         }

  227.       /* If we ran out of write requests without doing anything about
  228.          ERASED, then that means it's really erased.  */
  229.       VEC_safe_push (memory_write_request_s, result, &erased);

  230.     next_erased:
  231.       ;
  232.     }

  233.   return result;
  234. }

  235. static void
  236. cleanup_request_data (void *p)
  237. {
  238.   VEC(memory_write_request_s) **v = p;
  239.   struct memory_write_request *r;
  240.   int i;

  241.   for (i = 0; VEC_iterate (memory_write_request_s, *v, i, r); ++i)
  242.     xfree (r->data);
  243. }

  244. static void
  245. cleanup_write_requests_vector (void *p)
  246. {
  247.   VEC(memory_write_request_s) **v = p;

  248.   VEC_free (memory_write_request_s, *v);
  249. }

  250. int
  251. target_write_memory_blocks (VEC(memory_write_request_s) *requests,
  252.                             enum flash_preserve_mode preserve_flash_p,
  253.                             void (*progress_cb) (ULONGEST, void *))
  254. {
  255.   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
  256.   VEC(memory_write_request_s) *blocks = VEC_copy (memory_write_request_s,
  257.                                                   requests);
  258.   unsigned i;
  259.   int err = 0;
  260.   struct memory_write_request *r;
  261.   VEC(memory_write_request_s) *regular = NULL;
  262.   VEC(memory_write_request_s) *flash = NULL;
  263.   VEC(memory_write_request_s) *erased, *garbled;

  264.   /* END == 0 would represent wraparound: a write to the very last
  265.      byte of the address space.  This file was not written with that
  266.      possibility in mind.  This is fixable, but a lot of work for a
  267.      rare problem; so for now, fail noisily here instead of obscurely
  268.      later.  */
  269.   for (i = 0; VEC_iterate (memory_write_request_s, requests, i, r); ++i)
  270.     gdb_assert (r->end != 0);

  271.   make_cleanup (cleanup_write_requests_vector, &blocks);

  272.   /* Sort the blocks by their start address.  */
  273.   qsort (VEC_address (memory_write_request_s, blocks),
  274.          VEC_length (memory_write_request_s, blocks),
  275.          sizeof (struct memory_write_request), compare_block_starting_address);

  276.   /* Split blocks into list of regular memory blocks,
  277.      and list of flash memory blocks.  */
  278.   make_cleanup (cleanup_write_requests_vector, &regular);
  279.   make_cleanup (cleanup_write_requests_vector, &flash);
  280.   split_regular_and_flash_blocks (blocks, &regular, &flash);

  281.   /* If a variable is added to forbid flash write, even during "load",
  282.      it should be checked here.  Similarly, if this function is used
  283.      for other situations besides "load" in which writing to flash
  284.      is undesirable, that should be checked here.  */

  285.   /* Find flash blocks to erase.  */
  286.   erased = blocks_to_erase (flash);
  287.   make_cleanup (cleanup_write_requests_vector, &erased);

  288.   /* Find what flash regions will be erased, and not overwritten; then
  289.      either preserve or discard the old contents.  */
  290.   garbled = compute_garbled_blocks (erased, flash);
  291.   make_cleanup (cleanup_request_data, &garbled);
  292.   make_cleanup (cleanup_write_requests_vector, &garbled);

  293.   if (!VEC_empty (memory_write_request_s, garbled))
  294.     {
  295.       if (preserve_flash_p == flash_preserve)
  296.         {
  297.           struct memory_write_request *r;

  298.           /* Read in regions that must be preserved and add them to
  299.              the list of blocks we read.  */
  300.           for (i = 0; VEC_iterate (memory_write_request_s, garbled, i, r); ++i)
  301.             {
  302.               gdb_assert (r->data == NULL);
  303.               r->data = xmalloc (r->end - r->begin);
  304.               err = target_read_memory (r->begin, r->data, r->end - r->begin);
  305.               if (err != 0)
  306.                 goto out;

  307.               VEC_safe_push (memory_write_request_s, flash, r);
  308.             }

  309.           qsort (VEC_address (memory_write_request_s, flash),
  310.                  VEC_length (memory_write_request_s, flash),
  311.                  sizeof (struct memory_write_request),
  312.                  compare_block_starting_address);
  313.         }
  314.     }

  315.   /* We could coalesce adjacent memory blocks here, to reduce the
  316.      number of write requests for small sections.  However, we would
  317.      have to reallocate and copy the data pointers, which could be
  318.      large; large sections are more common in loadable objects than
  319.      large numbers of small sections (although the reverse can be true
  320.      in object files).  So, we issue at least one write request per
  321.      passed struct memory_write_request.  The remote stub will still
  322.      have the opportunity to batch flash requests.  */

  323.   /* Write regular blocks.  */
  324.   for (i = 0; VEC_iterate (memory_write_request_s, regular, i, r); ++i)
  325.     {
  326.       LONGEST len;

  327.       len = target_write_with_progress (current_target.beneath,
  328.                                         TARGET_OBJECT_MEMORY, NULL,
  329.                                         r->data, r->begin, r->end - r->begin,
  330.                                         progress_cb, r->baton);
  331.       if (len < (LONGEST) (r->end - r->begin))
  332.         {
  333.           /* Call error?  */
  334.           err = -1;
  335.           goto out;
  336.         }
  337.     }

  338.   if (!VEC_empty (memory_write_request_s, erased))
  339.     {
  340.       /* Erase all pages.  */
  341.       for (i = 0; VEC_iterate (memory_write_request_s, erased, i, r); ++i)
  342.         target_flash_erase (r->begin, r->end - r->begin);

  343.       /* Write flash data.  */
  344.       for (i = 0; VEC_iterate (memory_write_request_s, flash, i, r); ++i)
  345.         {
  346.           LONGEST len;

  347.           len = target_write_with_progress (&current_target,
  348.                                             TARGET_OBJECT_FLASH, NULL,
  349.                                             r->data, r->begin,
  350.                                             r->end - r->begin,
  351.                                             progress_cb, r->baton);
  352.           if (len < (LONGEST) (r->end - r->begin))
  353.             error (_("Error writing data to flash"));
  354.         }

  355.       target_flash_done ();
  356.     }

  357. out:
  358.   do_cleanups (back_to);

  359.   return err;
  360. }