Home | History | Annotate | Download | only in memcheck
      1 /* Copyright (C) 2007-2010 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 
     13 /*
     14  * Contains declarations of structures and routines that implement a red-black
     15  * tree (a map) of module memory mapping ranges in the guest system. The map is
     16  * organized in such a way, that each entry in the map describes a virtual
     17  * address range that belongs to a mapped execution module in the guest system.
     18  * Map considers two ranges to be equal if their address ranges intersect in
     19  * any part.
     20  */
     21 
     22 #ifndef QEMU_MEMCHECK_MEMCHECK_MMRANGE_MAP_H
     23 #define QEMU_MEMCHECK_MEMCHECK_MMRANGE_MAP_H
     24 
     25 #include "sys-tree.h"
     26 #include "memcheck_common.h"
     27 
     28 #ifdef __cplusplus
     29 extern "C" {
     30 #endif
     31 
     32 /* Memory mapping range map. */
     33 typedef struct MMRangeMap {
     34     /* Head of the map. */
     35     struct MMRangeMapEntry* rbh_root;
     36 } MMRangeMap;
     37 
     38 // =============================================================================
     39 // Map API
     40 // =============================================================================
     41 
     42 /* Initializes the map.
     43  * Param:
     44  *  map - Map to initialize.
     45  */
     46 void mmrangemap_init(MMRangeMap* map);
     47 
     48 /* Inserts new (or replaces existing) entry in the map.
     49  * Insertion, or replacement is controlled by the value, passed to this routine
     50  * with 'replaced' parameter. If this parameter is NULL, insertion will fail if
     51  * a matching entry already exists in the map. If 'replaced' parameter is not
     52  * NULL, and a matching entry exists in the map, content of the existing entry
     53  * will be copied to the descriptor, addressed by 'replace' parameter, existing
     54  * entry will be removed from the map, and new entry will be inserted.
     55  * Param:
     56  *  map - Map where to insert new, or replace existing entry.
     57  *  desc - Descriptor to insert to the map.
     58  *  replaced - If not NULL, upon return from this routine contains descriptor
     59  *      that has been replaced in the map with the new entry. Note that if this
     60  *      routine returns with value other than RBT_MAP_RESULT_ENTRY_REPLACED,
     61  *      content of the 'replaced' buffer is not defined, as no replacement has
     62  *      actually occurred.
     63  * Return
     64  *  See RBTMapResult for the return codes.
     65  */
     66 RBTMapResult mmrangemap_insert(MMRangeMap* map,
     67                                const MMRangeDesc* desc,
     68                                MMRangeDesc* replaced);
     69 
     70 /* Finds an entry in the map that matches the given address.
     71  * Param:
     72  *  map - Map where to search for an entry.
     73  *  start - Starting address of a mapping range.
     74  *  end - Ending address of a mapping range.
     75  * Return:
     76  *  Pointer to the descriptor found in a map entry, or NULL if no matching
     77  * entry has been found in the map.
     78  */
     79 MMRangeDesc* mmrangemap_find(const MMRangeMap* map,
     80                              target_ulong start,
     81                              target_ulong end);
     82 
     83 /* Pulls (finds and removes) an entry from the map that matches the given
     84  * address.
     85  * Param:
     86  *  map - Map where to search for an entry.
     87  *  start - Starting address of a mapping range.
     88  *  end - Ending address of a mapping range.
     89  *  pulled - Upon successful return contains descriptor data pulled from the
     90  *      map.
     91  * Return:
     92  *  Zero if a descriptor that matches the given address has been pulled, or 1
     93  *  if no matching entry has been found in the map.
     94  */
     95 int mmrangemap_pull(MMRangeMap* map,
     96                     target_ulong start,
     97                     target_ulong end,
     98                     MMRangeDesc* pulled);
     99 
    100 /* Copies content of one memory map to another.
    101  * Param:
    102  *  to - Map where to copy entries to.
    103  *  from - Map where to copy entries from.
    104  * Return:
    105  *  Zero on success, or -1 on error.
    106  */
    107 int mmrangemap_copy(MMRangeMap* to, const MMRangeMap* from);
    108 
    109 /* Empties the map.
    110  * Param:
    111  *  map - Map to empty.
    112  * Return:
    113  *  Number of entries removed from the map.
    114  */
    115 int mmrangemap_empty(MMRangeMap* map);
    116 
    117 #ifdef __cplusplus
    118 };  /* end of extern "C" */
    119 #endif
    120 
    121 #endif  // QEMU_MEMCHECK_MEMCHECK_MMRANGE_MAP_H
    122