memory: unify phys_map last level with intermediate levels

This lays the groundwork for storing leaf data in intermediate levels,
saving space.

Signed-off-by: Avi Kivity <avi@redhat.com>
This commit is contained in:
Avi Kivity 2012-02-10 17:00:01 +02:00
parent 3eef53df6b
commit 4346ae3e28
1 changed files with 35 additions and 43 deletions

78
exec.c
View File

@ -192,9 +192,18 @@ typedef struct PhysPageDesc {
ram_addr_t region_offset; ram_addr_t region_offset;
} PhysPageDesc; } PhysPageDesc;
typedef struct PhysPageEntry PhysPageEntry;
struct PhysPageEntry {
union {
PhysPageDesc leaf;
PhysPageEntry *node;
} u;
};
/* This is a multi-level map on the physical address space. /* This is a multi-level map on the physical address space.
The bottom level has pointers to PhysPageDesc. */ The bottom level has pointers to PhysPageDesc. */
static void *phys_map; static PhysPageEntry phys_map;
static void io_mem_init(void); static void io_mem_init(void);
static void memory_map_init(void); static void memory_map_init(void);
@ -392,42 +401,31 @@ static inline PageDesc *page_find(tb_page_addr_t index)
#if !defined(CONFIG_USER_ONLY) #if !defined(CONFIG_USER_ONLY)
static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc) static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
{ {
PhysPageDesc *pd; PhysPageEntry *lp, *p;
void **lp; int i, j;
int i;
lp = &phys_map; lp = &phys_map;
/* Level 1..N-1. */ /* Level 1..N. */
for (i = P_L2_LEVELS - 1; i > 0; i--) { for (i = P_L2_LEVELS - 1; i >= 0; i--) {
void **p = *lp; if (lp->u.node == NULL) {
if (p == NULL) {
if (!alloc) { if (!alloc) {
return NULL; return NULL;
} }
*lp = p = g_malloc0(sizeof(void *) * L2_SIZE); lp->u.node = p = g_malloc0(sizeof(PhysPageEntry) * L2_SIZE);
if (i == 0) {
int first_index = index & ~(L2_SIZE - 1);
for (j = 0; j < L2_SIZE; j++) {
p[j].u.leaf.phys_offset = io_mem_unassigned.ram_addr;
p[j].u.leaf.region_offset
= (first_index + j) << TARGET_PAGE_BITS;
}
}
} }
lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1)); lp = &lp->u.node[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
} }
pd = *lp; return &lp->u.leaf;
if (pd == NULL) {
int i;
int first_index = index & ~(L2_SIZE - 1);
if (!alloc) {
return NULL;
}
*lp = pd = g_malloc(sizeof(PhysPageDesc) * L2_SIZE);
for (i = 0; i < L2_SIZE; i++) {
pd[i].phys_offset = io_mem_unassigned.ram_addr;
pd[i].region_offset = (first_index + i) << TARGET_PAGE_BITS;
}
}
return pd + (index & (L2_SIZE - 1));
} }
static inline PhysPageDesc phys_page_find(target_phys_addr_t index) static inline PhysPageDesc phys_page_find(target_phys_addr_t index)
@ -2523,30 +2521,24 @@ static void destroy_page_desc(PhysPageDesc pd)
} }
} }
static void destroy_l2_mapping(void **lp, unsigned level) static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
{ {
unsigned i; unsigned i;
void **p; PhysPageEntry *p = lp->u.node;
PhysPageDesc *pd;
if (!*lp) { if (!p) {
return; return;
} }
if (level > 0) { for (i = 0; i < L2_SIZE; ++i) {
p = *lp; if (level > 0) {
for (i = 0; i < L2_SIZE; ++i) {
destroy_l2_mapping(&p[i], level - 1); destroy_l2_mapping(&p[i], level - 1);
} else {
destroy_page_desc(p[i].u.leaf);
} }
g_free(p);
} else {
pd = *lp;
for (i = 0; i < L2_SIZE; ++i) {
destroy_page_desc(pd[i]);
}
g_free(pd);
} }
*lp = NULL; g_free(p);
lp->u.node = NULL;
} }
static void destroy_all_mappings(void) static void destroy_all_mappings(void)