import Unicorn2
This commit is contained in:
@ -1,39 +1,195 @@
|
||||
/*
|
||||
* QEMU 64-bit address ranges
|
||||
*
|
||||
* Copyright (c) 2015-2016 Red Hat, Inc.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef QEMU_RANGE_H
|
||||
#define QEMU_RANGE_H
|
||||
|
||||
#include "unicorn/platform.h"
|
||||
#include <qemu/typedefs.h>
|
||||
#include "qemu/queue.h"
|
||||
|
||||
/*
|
||||
* Operations on 64 bit address ranges.
|
||||
* Notes:
|
||||
* - ranges must not wrap around 0, but can include the last byte ~0x0LL.
|
||||
* - this can not represent a full 0 to ~0x0LL range.
|
||||
* - Ranges must not wrap around 0, but can include UINT64_MAX.
|
||||
*/
|
||||
|
||||
/* A structure representing a range of addresses. */
|
||||
struct Range {
|
||||
uint64_t begin; /* First byte of the range, or 0 if empty. */
|
||||
uint64_t end; /* 1 + the last byte. 0 if range empty or ends at ~0x0LL. */
|
||||
/*
|
||||
* Do not access members directly, use the functions!
|
||||
* A non-empty range has @lob <= @upb.
|
||||
* An empty range has @lob == @upb + 1.
|
||||
*/
|
||||
uint64_t lob; /* inclusive lower bound */
|
||||
uint64_t upb; /* inclusive upper bound */
|
||||
};
|
||||
|
||||
static inline void range_invariant(const Range *range)
|
||||
{
|
||||
assert(range->lob <= range->upb || range->lob == range->upb + 1);
|
||||
}
|
||||
|
||||
/* Compound literal encoding the empty range */
|
||||
#define range_empty ((Range){ .lob = 1, .upb = 0 })
|
||||
|
||||
/* Is @range empty? */
|
||||
static inline bool range_is_empty(const Range *range)
|
||||
{
|
||||
range_invariant(range);
|
||||
return range->lob > range->upb;
|
||||
}
|
||||
|
||||
/* Does @range contain @val? */
|
||||
static inline bool range_contains(const Range *range, uint64_t val)
|
||||
{
|
||||
return val >= range->lob && val <= range->upb;
|
||||
}
|
||||
|
||||
/* Initialize @range to the empty range */
|
||||
static inline void range_make_empty(Range *range)
|
||||
{
|
||||
*range = range_empty;
|
||||
assert(range_is_empty(range));
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize @range to span the interval [@lob,@upb].
|
||||
* Both bounds are inclusive.
|
||||
* The interval must not be empty, i.e. @lob must be less than or
|
||||
* equal @upb.
|
||||
*/
|
||||
static inline void range_set_bounds(Range *range, uint64_t lob, uint64_t upb)
|
||||
{
|
||||
range->lob = lob;
|
||||
range->upb = upb;
|
||||
assert(!range_is_empty(range));
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize @range to span the interval [@lob,@upb_plus1).
|
||||
* The lower bound is inclusive, the upper bound is exclusive.
|
||||
* Zero @upb_plus1 is special: if @lob is also zero, set @range to the
|
||||
* empty range. Else, set @range to [@lob,UINT64_MAX].
|
||||
*/
|
||||
static inline void range_set_bounds1(Range *range,
|
||||
uint64_t lob, uint64_t upb_plus1)
|
||||
{
|
||||
if (!lob && !upb_plus1) {
|
||||
*range = range_empty;
|
||||
} else {
|
||||
range->lob = lob;
|
||||
range->upb = upb_plus1 - 1;
|
||||
}
|
||||
range_invariant(range);
|
||||
}
|
||||
|
||||
/* Return @range's lower bound. @range must not be empty. */
|
||||
static inline uint64_t range_lob(Range *range)
|
||||
{
|
||||
assert(!range_is_empty(range));
|
||||
return range->lob;
|
||||
}
|
||||
|
||||
/* Return @range's upper bound. @range must not be empty. */
|
||||
static inline uint64_t range_upb(Range *range)
|
||||
{
|
||||
assert(!range_is_empty(range));
|
||||
return range->upb;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize @range to span the interval [@lob,@lob + @size - 1].
|
||||
* @size may be 0. If the range would overflow, returns -ERANGE, otherwise
|
||||
* 0.
|
||||
*/
|
||||
static inline int QEMU_WARN_UNUSED_RESULT range_init(Range *range, uint64_t lob,
|
||||
uint64_t size)
|
||||
{
|
||||
if (lob + size < lob) {
|
||||
return -ERANGE;
|
||||
}
|
||||
range->lob = lob;
|
||||
range->upb = lob + size - 1;
|
||||
range_invariant(range);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize @range to span the interval [@lob,@lob + @size - 1].
|
||||
* @size may be 0. Range must not overflow.
|
||||
*/
|
||||
static inline void range_init_nofail(Range *range, uint64_t lob, uint64_t size)
|
||||
{
|
||||
range->lob = lob;
|
||||
range->upb = lob + size - 1;
|
||||
range_invariant(range);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the size of @range.
|
||||
*/
|
||||
static inline uint64_t range_size(const Range *range)
|
||||
{
|
||||
return range->upb - range->lob + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if @range1 overlaps with @range2. If one of the ranges is empty,
|
||||
* the result is always "false".
|
||||
*/
|
||||
static inline bool range_overlaps_range(const Range *range1,
|
||||
const Range *range2)
|
||||
{
|
||||
if (range_is_empty(range1) || range_is_empty(range2)) {
|
||||
return false;
|
||||
}
|
||||
return !(range2->upb < range1->lob || range1->upb < range2->lob);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if @range1 contains @range2. If one of the ranges is empty,
|
||||
* the result is always "false".
|
||||
*/
|
||||
static inline bool range_contains_range(const Range *range1,
|
||||
const Range *range2)
|
||||
{
|
||||
if (range_is_empty(range1) || range_is_empty(range2)) {
|
||||
return false;
|
||||
}
|
||||
return range1->lob <= range2->lob && range1->upb >= range2->upb;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extend @range to the smallest interval that includes @extend_by, too.
|
||||
*/
|
||||
static inline void range_extend(Range *range, Range *extend_by)
|
||||
{
|
||||
if (!extend_by->begin && !extend_by->end) {
|
||||
if (range_is_empty(extend_by)) {
|
||||
return;
|
||||
}
|
||||
if (!range->begin && !range->end) {
|
||||
if (range_is_empty(range)) {
|
||||
*range = *extend_by;
|
||||
return;
|
||||
}
|
||||
if (range->begin > extend_by->begin) {
|
||||
range->begin = extend_by->begin;
|
||||
if (range->lob > extend_by->lob) {
|
||||
range->lob = extend_by->lob;
|
||||
}
|
||||
/* Compare last byte in case region ends at ~0x0LL */
|
||||
if (range->end - 1 < extend_by->end - 1) {
|
||||
range->end = extend_by->end;
|
||||
if (range->upb < extend_by->upb) {
|
||||
range->upb = extend_by->upb;
|
||||
}
|
||||
range_invariant(range);
|
||||
}
|
||||
|
||||
/* Get last byte of a range from offset + length.
|
||||
@ -61,75 +217,6 @@ static inline int ranges_overlap(uint64_t first1, uint64_t len1,
|
||||
return !(last2 < first1 || last1 < first2);
|
||||
}
|
||||
|
||||
/* 0,1 can merge with 1,2 but don't overlap */
|
||||
static inline bool ranges_can_merge(Range *range1, Range *range2)
|
||||
{
|
||||
return !(range1->end < range2->begin || range2->end < range1->begin);
|
||||
}
|
||||
|
||||
static inline int range_merge(Range *range1, Range *range2)
|
||||
{
|
||||
if (ranges_can_merge(range1, range2)) {
|
||||
if (range1->end < range2->end) {
|
||||
range1->end = range2->end;
|
||||
}
|
||||
if (range1->begin > range2->begin) {
|
||||
range1->begin = range2->begin;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline GList *g_list_insert_sorted_merged(GList *list,
|
||||
gpointer data,
|
||||
GCompareFunc func)
|
||||
{
|
||||
GList *l, *next = NULL;
|
||||
Range *r, *nextr;
|
||||
|
||||
if (!list) {
|
||||
list = g_list_insert_sorted(list, data, func);
|
||||
return list;
|
||||
}
|
||||
|
||||
nextr = data;
|
||||
l = list;
|
||||
while (l && l != next && nextr) {
|
||||
r = l->data;
|
||||
if (ranges_can_merge(r, nextr)) {
|
||||
range_merge(r, nextr);
|
||||
l = g_list_remove_link(l, next);
|
||||
next = g_list_next(l);
|
||||
if (next) {
|
||||
nextr = next->data;
|
||||
} else {
|
||||
nextr = NULL;
|
||||
}
|
||||
} else {
|
||||
l = g_list_next(l);
|
||||
}
|
||||
}
|
||||
|
||||
if (!l) {
|
||||
list = g_list_insert_sorted(list, data, func);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
static inline gint range_compare(gconstpointer a, gconstpointer b)
|
||||
{
|
||||
Range *ra = (Range *)a, *rb = (Range *)b;
|
||||
if (ra->begin == rb->begin && ra->end == rb->end) {
|
||||
return 0;
|
||||
} else if (range_get_last(ra->begin, ra->end) <
|
||||
range_get_last(rb->begin, rb->end)) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
GList *range_list_insert(GList *list, Range *data);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user