Skip to content

Commit 3ea3f9c

Browse files
committed
xen/arch/x86/intel_txt.c: better error checks on reserving memory
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
1 parent aff6284 commit 3ea3f9c

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

xen/arch/x86/intel_txt.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,39 @@ void __init map_txt_mem_regions(void)
3030
map_l2(txt_heap_base, txt_heap_size);
3131
}
3232

33+
/* Mark RAM region as RESERVED if it isn't marked that way already. */
34+
static int __init reserve_e820(struct e820map *e820, uint64_t s, uint64_t e)
35+
{
36+
unsigned int i;
37+
38+
for ( i = 0; i < e820->nr_map; i++ )
39+
{
40+
uint64_t rs = e820->map[i].addr;
41+
uint64_t re = rs + e820->map[i].size;
42+
if ( s >= rs && e <= re )
43+
break;
44+
}
45+
46+
if ( i != e820->nr_map && e820->map[i].type == E820_RESERVED )
47+
{
48+
/* Nothing to do, the range is already covered as reserved. */
49+
return 1;
50+
}
51+
52+
return reserve_e820_ram(e820, s, e);
53+
}
54+
3355
void __init protect_txt_mem_regions(void)
3456
{
57+
int rc;
3558
uint64_t sinit_base, sinit_size;
3659

3760
/* TXT Heap */
3861
BUG_ON(txt_heap_base == 0);
3962
printk("SLAUNCH: reserving TXT heap (%#lx - %#lx)\n", txt_heap_base,
4063
txt_heap_base + txt_heap_size);
41-
e820_change_range_type(&e820_raw, txt_heap_base,
42-
txt_heap_base + txt_heap_size,
43-
E820_RAM, E820_RESERVED);
64+
rc = reserve_e820(&e820_raw, txt_heap_base, txt_heap_base + txt_heap_size);
65+
BUG_ON(rc == 0);
4466

4567
sinit_base = read_txt_reg(TXTCR_SINIT_BASE);
4668
BUG_ON(sinit_base == 0);
@@ -51,9 +73,8 @@ void __init protect_txt_mem_regions(void)
5173
/* SINIT */
5274
printk("SLAUNCH: reserving SINIT memory (%#lx - %#lx)\n", sinit_base,
5375
sinit_base + sinit_size);
54-
e820_change_range_type(&e820_raw, sinit_base,
55-
sinit_base + sinit_size,
56-
E820_RAM, E820_RESERVED);
76+
rc = reserve_e820(&e820_raw, sinit_base, sinit_base + sinit_size);
77+
BUG_ON(rc == 0);
5778

5879
/* TXT Private Space */
5980
e820_change_range_type(&e820_raw, TXT_PRIV_CONFIG_REGS_BASE,

0 commit comments

Comments
 (0)