Blame libipt/internal/include/pt_image_section_cache.h

Packit b1f7ae
/*
Packit b1f7ae
 * Copyright (c) 2016-2017, Intel Corporation
Packit b1f7ae
 *
Packit b1f7ae
 * Redistribution and use in source and binary forms, with or without
Packit b1f7ae
 * modification, are permitted provided that the following conditions are met:
Packit b1f7ae
 *
Packit b1f7ae
 *  * Redistributions of source code must retain the above copyright notice,
Packit b1f7ae
 *    this list of conditions and the following disclaimer.
Packit b1f7ae
 *  * Redistributions in binary form must reproduce the above copyright notice,
Packit b1f7ae
 *    this list of conditions and the following disclaimer in the documentation
Packit b1f7ae
 *    and/or other materials provided with the distribution.
Packit b1f7ae
 *  * Neither the name of Intel Corporation nor the names of its contributors
Packit b1f7ae
 *    may be used to endorse or promote products derived from this software
Packit b1f7ae
 *    without specific prior written permission.
Packit b1f7ae
 *
Packit b1f7ae
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit b1f7ae
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit b1f7ae
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit b1f7ae
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit b1f7ae
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit b1f7ae
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit b1f7ae
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit b1f7ae
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit b1f7ae
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit b1f7ae
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit b1f7ae
 * POSSIBILITY OF SUCH DAMAGE.
Packit b1f7ae
 */
Packit b1f7ae
Packit b1f7ae
#ifndef PT_IMAGE_SECTION_CACHE_H
Packit b1f7ae
#define PT_IMAGE_SECTION_CACHE_H
Packit b1f7ae
Packit b1f7ae
#include <stdint.h>
Packit b1f7ae
Packit b1f7ae
#if defined(FEATURE_THREADS)
Packit b1f7ae
#  include <threads.h>
Packit b1f7ae
#endif /* defined(FEATURE_THREADS) */
Packit b1f7ae
Packit b1f7ae
struct pt_section;
Packit b1f7ae
Packit b1f7ae
Packit b1f7ae
/* An image section cache entry. */
Packit b1f7ae
struct pt_iscache_entry {
Packit b1f7ae
	/* The section object.
Packit b1f7ae
	 *
Packit b1f7ae
	 * We hold a reference to the section - put it when the section is
Packit b1f7ae
	 * removed from the cache.
Packit b1f7ae
	 */
Packit b1f7ae
	struct pt_section *section;
Packit b1f7ae
Packit b1f7ae
	/* The base address at which @section has been loaded. */
Packit b1f7ae
	uint64_t laddr;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
/* A cache of image sections and their load addresses.
Packit b1f7ae
 *
Packit b1f7ae
 * We combine the section with its load address to reduce the amount of
Packit b1f7ae
 * information we need to store in order to read from a cached section by
Packit b1f7ae
 * virtual address.
Packit b1f7ae
 *
Packit b1f7ae
 * Internally, the section object will be shared if it is loaded at different
Packit b1f7ae
 * addresses in the cache.
Packit b1f7ae
 *
Packit b1f7ae
 * The cache does not consider the address-space the section is mapped into.
Packit b1f7ae
 * This is not relevant for reading from the section.
Packit b1f7ae
 */
Packit b1f7ae
struct pt_image_section_cache {
Packit b1f7ae
	/* The optional name of the cache; NULL if not named. */
Packit b1f7ae
	char *name;
Packit b1f7ae
Packit b1f7ae
	/* An array of @nentries cached sections. */
Packit b1f7ae
	struct pt_iscache_entry *entries;
Packit b1f7ae
Packit b1f7ae
#if defined(FEATURE_THREADS)
Packit b1f7ae
	/* A lock protecting this image section cache. */
Packit b1f7ae
	mtx_t lock;
Packit b1f7ae
#endif /* defined(FEATURE_THREADS) */
Packit b1f7ae
Packit b1f7ae
	/* The capacity of the @entries array.
Packit b1f7ae
	 *
Packit b1f7ae
	 * Cached sections are identified by a positive integer, the image
Packit b1f7ae
	 * section identifier (isid), which is derived from their index into the
Packit b1f7ae
	 * @entries array.
Packit b1f7ae
	 *
Packit b1f7ae
	 * We can't expand the section cache capacity beyond INT_MAX.
Packit b1f7ae
	 */
Packit b1f7ae
	uint16_t capacity;
Packit b1f7ae
Packit b1f7ae
	/* The current size of the cache in number of entries.
Packit b1f7ae
	 *
Packit b1f7ae
	 * This is smaller than @capacity if there is still room in the @entries
Packit b1f7ae
	 * array; equal to @capacity if the @entries array is full and needs to
Packit b1f7ae
	 * be reallocated.
Packit b1f7ae
	 */
Packit b1f7ae
	uint16_t size;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
Packit b1f7ae
/* Initialize an image section cache. */
Packit b1f7ae
extern int pt_iscache_init(struct pt_image_section_cache *iscache,
Packit b1f7ae
			   const char *name);
Packit b1f7ae
Packit b1f7ae
/* Finalize an image section cache. */
Packit b1f7ae
extern void pt_iscache_fini(struct pt_image_section_cache *iscache);
Packit b1f7ae
Packit b1f7ae
/* Add a section to the cache.
Packit b1f7ae
 *
Packit b1f7ae
 * Adds @section at @laddr to @iscache and returns its isid.  If a similar
Packit b1f7ae
 * section is already cached, returns that section's isid, instead.
Packit b1f7ae
 *
Packit b1f7ae
 * We take a full section rather than its filename and range in that file to
Packit b1f7ae
 * avoid the dependency to pt_section.h.  Callers are expected to query the
Packit b1f7ae
 * cache before creating the section, so we should only see unnecessary section
Packit b1f7ae
 * creation/destruction on insertion races.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success, a negative error code otherwise.
Packit b1f7ae
 * Returns -pte_internal if @iscache or @section is NULL.
Packit b1f7ae
 * Returns -pte_internal if @section's filename is NULL.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_iscache_add(struct pt_image_section_cache *iscache,
Packit b1f7ae
			  struct pt_section *section, uint64_t laddr);
Packit b1f7ae
Packit b1f7ae
/* Find a section in the cache.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns a positive isid if a section matching @filename, @offset, @size
Packit b1f7ae
 * loaded at @laddr is found in @iscache.
Packit b1f7ae
 * Returns zero if no such section is found.
Packit b1f7ae
 * Returns a negative error code otherwise.
Packit b1f7ae
 * Returns -pte_internal if @iscache or @filename is NULL.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_iscache_find(struct pt_image_section_cache *iscache,
Packit b1f7ae
			   const char *filename, uint64_t offset,
Packit b1f7ae
			   uint64_t size, uint64_t laddr);
Packit b1f7ae
Packit b1f7ae
/* Lookup the section identified by its isid.
Packit b1f7ae
 *
Packit b1f7ae
 * Provides a reference to the section in @section and its load address in
Packit b1f7ae
 * @laddr on success.  The caller is expected to put the returned section after
Packit b1f7ae
 * use.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns zero on success, a negative error code otherwise.
Packit b1f7ae
 * Returns -pte_internal if @iscache, @section, or @laddr is NULL.
Packit b1f7ae
 * Returns -pte_bad_image if @iscache does not contain @isid.
Packit b1f7ae
 */
Packit b1f7ae
extern int pt_iscache_lookup(struct pt_image_section_cache *iscache,
Packit b1f7ae
			     struct pt_section **section, uint64_t *laddr,
Packit b1f7ae
			     int isid);
Packit b1f7ae
Packit b1f7ae
/* Clear an image section cache. */
Packit b1f7ae
extern int pt_iscache_clear(struct pt_image_section_cache *iscache);
Packit b1f7ae
Packit b1f7ae
#endif /* PT_IMAGE_SECTION_CACHE_H */