memory: abstract cracking of write access ops into a function

The memory API automatically cracks large reads and writes into smaller
ones when needed.  Factor out this mechanism, which is now duplicated between
memory reads and memory writes, into a function.

Signed-off-by: Avi Kivity <avi@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Avi Kivity 2011-08-11 10:40:25 +03:00 committed by Anthony Liguori
parent 30c2f2388a
commit 164a4dcd8d

109
memory.c
View File

@ -226,6 +226,65 @@ static void flatview_simplify(FlatView *view)
} }
} }
static void memory_region_read_accessor(void *opaque,
target_phys_addr_t addr,
uint64_t *value,
unsigned size,
unsigned shift,
uint64_t mask)
{
MemoryRegion *mr = opaque;
uint64_t tmp;
tmp = mr->ops->read(mr->opaque, addr, size);
*value |= (tmp & mask) << shift;
}
static void memory_region_write_accessor(void *opaque,
target_phys_addr_t addr,
uint64_t *value,
unsigned size,
unsigned shift,
uint64_t mask)
{
MemoryRegion *mr = opaque;
uint64_t tmp;
tmp = (*value >> shift) & mask;
mr->ops->write(mr->opaque, addr, tmp, size);
}
static void access_with_adjusted_size(target_phys_addr_t addr,
uint64_t *value,
unsigned size,
unsigned access_size_min,
unsigned access_size_max,
void (*access)(void *opaque,
target_phys_addr_t addr,
uint64_t *value,
unsigned size,
unsigned shift,
uint64_t mask),
void *opaque)
{
uint64_t access_mask;
unsigned access_size;
unsigned i;
if (!access_size_min) {
access_size_min = 1;
}
if (!access_size_max) {
access_size_max = 4;
}
access_size = MAX(MIN(size, access_size_max), access_size_min);
access_mask = -1ULL >> (64 - access_size * 8);
for (i = 0; i < size; i += access_size) {
/* FIXME: big-endian support */
access(opaque, addr + i, value, access_size, i * 8, access_mask);
}
}
static void memory_region_prepare_ram_addr(MemoryRegion *mr); static void memory_region_prepare_ram_addr(MemoryRegion *mr);
static void as_memory_range_add(AddressSpace *as, FlatRange *fr) static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
@ -744,10 +803,7 @@ static uint32_t memory_region_read_thunk_n(void *_mr,
unsigned size) unsigned size)
{ {
MemoryRegion *mr = _mr; MemoryRegion *mr = _mr;
unsigned access_size, access_size_min, access_size_max; uint64_t data = 0;
uint64_t access_mask;
uint32_t data = 0, tmp;
unsigned i;
if (!memory_region_access_valid(mr, addr, size)) { if (!memory_region_access_valid(mr, addr, size)) {
return -1U; /* FIXME: better signalling */ return -1U; /* FIXME: better signalling */
@ -758,23 +814,10 @@ static uint32_t memory_region_read_thunk_n(void *_mr,
} }
/* FIXME: support unaligned access */ /* FIXME: support unaligned access */
access_with_adjusted_size(addr + mr->offset, &data, size,
access_size_min = mr->ops->impl.min_access_size; mr->ops->impl.min_access_size,
if (!access_size_min) { mr->ops->impl.max_access_size,
access_size_min = 1; memory_region_read_accessor, mr);
}
access_size_max = mr->ops->impl.max_access_size;
if (!access_size_max) {
access_size_max = 4;
}
access_size = MAX(MIN(size, access_size_max), access_size_min);
access_mask = -1ULL >> (64 - access_size * 8);
addr += mr->offset;
for (i = 0; i < size; i += access_size) {
/* FIXME: big-endian support */
tmp = mr->ops->read(mr->opaque, addr + i, access_size);
data |= (tmp & access_mask) << (i * 8);
}
return data; return data;
} }
@ -785,9 +828,6 @@ static void memory_region_write_thunk_n(void *_mr,
uint64_t data) uint64_t data)
{ {
MemoryRegion *mr = _mr; MemoryRegion *mr = _mr;
unsigned access_size, access_size_min, access_size_max;
uint64_t access_mask;
unsigned i;
if (!memory_region_access_valid(mr, addr, size)) { if (!memory_region_access_valid(mr, addr, size)) {
return; /* FIXME: better signalling */ return; /* FIXME: better signalling */
@ -799,23 +839,10 @@ static void memory_region_write_thunk_n(void *_mr,
} }
/* FIXME: support unaligned access */ /* FIXME: support unaligned access */
access_with_adjusted_size(addr + mr->offset, &data, size,
access_size_min = mr->ops->impl.min_access_size; mr->ops->impl.min_access_size,
if (!access_size_min) { mr->ops->impl.max_access_size,
access_size_min = 1; memory_region_write_accessor, mr);
}
access_size_max = mr->ops->impl.max_access_size;
if (!access_size_max) {
access_size_max = 4;
}
access_size = MAX(MIN(size, access_size_max), access_size_min);
access_mask = -1ULL >> (64 - access_size * 8);
addr += mr->offset;
for (i = 0; i < size; i += access_size) {
/* FIXME: big-endian support */
mr->ops->write(mr->opaque, addr + i, (data >> (i * 8)) & access_mask,
access_size);
}
} }
static uint32_t memory_region_read_thunk_b(void *mr, target_phys_addr_t addr) static uint32_t memory_region_read_thunk_b(void *mr, target_phys_addr_t addr)