Skip to contentSkip to conversation

KEXEC fix 32/64bit issues with KEXEC_CMD_kexec_get_range

PATCH Andrew Cooper 2 participants 4 messages Dec 14, 2011

lore
Share & tools
lore
#0 lore raw

Attached is my first attempt at this fix. It has been compile tested for x86_{64,32} and so far, dev tested in so much as it does not break backwards compatibility for 32bit dom0 on 64bit Xen. It is untested as far as ia64 goes, but I believe it should be correct from code inspection.

Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer
T: +44 (0)1223 225 900, http://www.citrix.com

KEXEC: Add new hypercall to fix 64/32bit truncation errors.

Currently, KEXEC_CMD_kexec_get_range in compat mode, or with 32bit Xen, will truncate 64bit pointers to 32bits, leading to problems on machines with more than 4GiB of RAM. As 32bit dom0 kernels tend to be PAE capable, this is especially wasteful, as most structures currently limited to <4GiB could easily be <64GiB instead.

Therefore, introduce a new hypercall 'KEXEC_CMD_kexec64_get_range' which passes similar information as KEXEC_CMD_kexec_get_range, but which uses a structure with explicitly specified field widths, causing it to be identical in the compat and regular case. This new structure, xen_kexec64_range_t, will be the same as xen_kexec_range_t if Xen is compiled for x86_64, but will still use 64bit integers if Xen is compiled for x86_32.

To fix 32bit Xen which uses 32bit intergers for addresses and sizes, change the internals to use xen_kexec64_range_t which will use 64bit integers instead. This also invovles changing several casts to explicitly use uint64_ts rather than unsigned longs.

In addition, the hypercall entry points need updating to be able to cater for all possiblities.

|Xen/dom0 bits|          Bit width of addresses in structure for        |
+------+------+---------------------------+-----------------------------+
| Xen  | dom0 | KEXEC_CMD_kexec_get_range | KEXEC_CMD_kexec64_get_range |
+------+------+---------------------------+-----------------------------+
|  32  |  32  |             32            |              64             |
|  64  |  32  |             32            |              64             |
|  64  |  64  |             64            |              64             |
+------+------+---------------------------+-----------------------------+

This has been solved by splitting do_kexec_op_internal back into do_kexec_op{,_compat} and changing kexec_get_range{,_compat} to kexec_get_range{32,64} which now exist irrispective of CONFIG_COMPAT or not.

The knock-on effect of removing do_kexec_op_internal means that kexec_load_unload_compat is only ever called from inside an #ifdef CONFIG_COMPAT codepath, which does not exist on Xen x86_32. Therefore, move the #ifdef CONFIG_COMPAT guards to include the entire function.

Finally, and a little unrelatedly, fix KEXEC_CMD_kexec_{load,unload} to return -EBUSY instead of EOK if a kexec is in progress.

xen/arch/ia64/xen/machine_kexec.c +8 -8
diff -r d960ea9b337d xen/arch/ia64/xen/machine_kexec.c
@@ -102,10 +102,10 @@ void machine_reboot_kexec(xen_kexec_imag
 	machine_kexec(image);
 }
 
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
 	range->start = ia64_tpa(_text);
-	range->size = (unsigned long)_end - (unsigned long)_text;
+	range->size = (uint64_t)_end - (uint64_t)_text;
 	return 0;
 }
 
@@ -113,31 +113,31 @@ int machine_kexec_get_xen(xen_kexec_rang
 #define ELF_PAGE_SIZE  (__IA64_UL_CONST(1) << ELF_PAGE_SHIFT)
 #define ELF_PAGE_MASK  (~(ELF_PAGE_SIZE - 1))
 
-static int machine_kexec_get_xenheap(xen_kexec_range_t *range)
+static int machine_kexec_get_xenheap(xen_kexec64_range_t *range)
 {
 	range->start = (ia64_tpa(_end) + (ELF_PAGE_SIZE - 1)) & ELF_PAGE_MASK;
 	range->size =
-		(((unsigned long)range->start + KERNEL_TR_PAGE_SIZE) &
+		(((uint64_t)range->start + KERNEL_TR_PAGE_SIZE) &
          ~(KERNEL_TR_PAGE_SIZE - 1))
-		- (unsigned long)range->start;
+		- (uint64_t)range->start;
 	return 0;
 }
 
-static int machine_kexec_get_boot_param(xen_kexec_range_t *range)
+static int machine_kexec_get_boot_param(xen_kexec64_range_t *range)
 {
 	range->start = __pa(ia64_boot_param);
 	range->size = sizeof(*ia64_boot_param);
 	return 0;
 }
 
-static int machine_kexec_get_efi_memmap(xen_kexec_range_t *range)
+static int machine_kexec_get_efi_memmap(xen_kexec64_range_t *range)
 {
 	range->start = ia64_boot_param->efi_memmap;
 	range->size = ia64_boot_param->efi_memmap_size;
 	return 0;
 }
 
-int machine_kexec_get(xen_kexec_range_t *range)
+int machine_kexec_get(xen_kexec64_range_t *range)
 {
 	switch (range->range) {
 	case KEXEC_RANGE_MA_XEN:
xen/arch/x86/machine_kexec.c +1 -1
diff -r d960ea9b337d xen/arch/x86/machine_kexec.c
@@ -120,7 +120,7 @@ void machine_kexec(xen_kexec_image_t *im
     }
 }
 
-int machine_kexec_get(xen_kexec_range_t *range)
+int machine_kexec_get(xen_kexec64_range_t *range)
 {
 	if (range->range != KEXEC_RANGE_MA_XEN)
 		return -EINVAL;
xen/arch/x86/x86_32/machine_kexec.c +3 -3
diff -r d960ea9b337d xen/arch/x86/x86_32/machine_kexec.c
@@ -11,11 +11,11 @@
 #include <asm/page.h>
 #include <public/kexec.h>
 
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
         range->start = virt_to_maddr(_start);
-        range->size = (unsigned long)xenheap_phys_end -
-                      (unsigned long)range->start;
+        range->size = (uint64_t)xenheap_phys_end -
+                      (uint64_t)range->start;
         return 0;
 }
 
xen/arch/x86/x86_64/machine_kexec.c +2 -2
diff -r d960ea9b337d xen/arch/x86/x86_64/machine_kexec.c
@@ -11,10 +11,10 @@
 #include <asm/page.h>
 #include <public/kexec.h>
 
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
         range->start = virt_to_maddr(_start);
-        range->size = virt_to_maddr(_end) - (unsigned long)range->start;
+        range->size = virt_to_maddr(_end) - (uint64_t)range->start;
         return 0;
 }
 
xen/common/kexec.c +92 -45

237 lines not on this page · raw message

xen/include/public/kexec.h +9 -0
diff -r d960ea9b337d xen/include/public/kexec.h
@@ -155,6 +155,15 @@ typedef struct xen_kexec_range {
     unsigned long start;
 } xen_kexec_range_t;
 
+#define KEXEC_CMD_kexec64_get_range      4
+/* xen_kexec_range_t using explicit sizes for fields. */
+typedef struct xen_kexec64_range {
+    int32_t range;
+    int32_t nr;
+    uint64_t size;
+    uint64_t start;
+} xen_kexec64_range_t;
+
 #endif /* _XEN_PUBLIC_KEXEC_H */
 
 /*
xen/include/xen/kexec.h +2 -2
diff -r d960ea9b337d xen/include/xen/kexec.h
@@ -34,8 +34,8 @@ void kexec_crash(void);
 void kexec_crash_save_cpu(void);
 crash_xen_info_t *kexec_crash_save_info(void);
 void machine_crash_shutdown(void);
-int machine_kexec_get(xen_kexec_range_t *range);
-int machine_kexec_get_xen(xen_kexec_range_t *range);
+int machine_kexec_get(xen_kexec64_range_t *range);
+int machine_kexec_get_xen(xen_kexec64_range_t *range);
 
 void compat_machine_kexec(unsigned long rnk,
                           unsigned long indirection_page,
xen/include/xlat.lst +0 -2
diff -r d960ea9b337d xen/include/xlat.lst
@@ -50,9 +50,7 @@
 ?	grant_entry_v1			grant_table.h
 ?       grant_entry_header              grant_table.h
 ?	grant_entry_v2			grant_table.h
-?	kexec_exec			kexec.h
 !	kexec_image			kexec.h
-!	kexec_range			kexec.h
 !	add_to_physmap			memory.h
 !	foreign_memory_map		memory.h
 !	memory_exchange			memory.h

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

To fix 32bit Xen which uses 32bit intergers for addresses and sizes, change the internals to use xen_kexec64_range_t which will use 64bit integers instead. This also invovles changing several casts to explicitly use uint64_ts rather than unsigned longs.

I don't think fixing 32-bit Xen is really necessary: Neither does anyone care much, nor should any address be beyond 4Gb in that case. Not playing with this will likely simplify the patch quite a bit.

xen/arch/ia64/xen/machine_kexec.c void machine_reboot_kexec(xen_kexec_imag · line 108

Quoted 12 lines
xen/arch/ia64/xen/machine_kexec.c +2 -2
@@ -102,10 +102,10 @@ void machine_reboot_kexec(xen_kexec_imag
 	machine_kexec(image);
 }
 
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
 	range->start = ia64_tpa(_text);
-	range->size = (unsigned long)_end - (unsigned long)_text;
+	range->size = (uint64_t)_end - (uint64_t)_text;

This is bogus and pointless (same thing a few lines down the patch).

Quoted 6 lines
xen/arch/ia64/xen/machine_kexec.c +0 -0
 	return 0;
 }
 

xen/arch/x86/x86_32/machine_kexec.c line 17

Quoted 10 lines
xen/arch/x86/x86_32/machine_kexec.c +2 -3
@@ -11,11 +11,11 @@
 #include <asm/page.h>
 #include <public/kexec.h>
 
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
        range->start = virt_to_maddr(_start);
-        range->size = (unsigned long)xenheap_phys_end -
-                      (unsigned long)range->start;
+        range->size = (uint64_t)xenheap_phys_end -

And here it's even wrong, and I doubt it compiles without warning across the supported range of compilers.

xen/arch/x86/x86_32/machine_kexec.c line 18

Quoted 1 line
xen/arch/x86/x86_32/machine_kexec.c +1 -0
+                      (uint64_t)range->start;

Casting range->start here and elsewhere shouldn't be necessary at all (the pre-existing cast was bogus too).

xen/arch/x86/x86_32/machine_kexec.c line 21

Quoted 3 lines
xen/arch/x86/x86_32/machine_kexec.c +0 -0
        return 0;
 }
 

Jan

#2 lore raw
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

To fix 32bit Xen which uses 32bit intergers for addresses and sizes, change the internals to use xen_kexec64_range_t which will use 64bit integers instead. This also invovles changing several casts to explicitly use uint64_ts rather than unsigned longs.

I don't think fixing 32-bit Xen is really necessary: Neither does anyone care much, nor should any address be beyond 4Gb in that case. Not playing with this will likely simplify the patch quite a bit.

This point was discussed on the IRC channel and it was decided to be worth doing, even though people are likely not to care else I would happily collapse the patch somewhat. Why should nothing be beyond 4GB in the 32bit case? Anything with PAE support ought to be able to use 64GB or less.

Quoted 6 lines
Quoted 6 lines
xen/arch/ia64/xen/machine_kexec.c +0 -0
@@ -102,10 +102,10 @@ void machine_reboot_kexec(xen_kexec_imag
 	machine_kexec(image);
 }
 
Quoted 7 lines

xen/arch/ia64/xen/machine_kexec.c void machine_reboot_kexec(xen_kexec_imag · line 108

Quoted 6 lines
xen/arch/ia64/xen/machine_kexec.c +2 -2
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
 	range->start = ia64_tpa(_text);
-	range->size = (unsigned long)_end - (unsigned long)_text;
+	range->size = (uint64_t)_end - (uint64_t)_text;

This is bogus and pointless (same thing a few lines down the patch).

I can understand pointless as sizeof(unsigned long) == sizeof(uint64_t) on 64bit builds, but why is it bogus? I changed it for consistency with xen_kexec64_range_t.

Quoted 9 lines
Quoted 9 lines
xen/arch/ia64/xen/machine_kexec.c +0 -0
 	return 0;
 }
 
xen/arch/x86/x86_32/machine_kexec.c +0 -0
@@ -11,11 +11,11 @@
 #include <asm/page.h>
 #include <public/kexec.h>
 
Quoted 9 lines

xen/arch/x86/x86_32/machine_kexec.c line 17

Quoted 7 lines
xen/arch/x86/x86_32/machine_kexec.c +2 -3
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
        range->start = virt_to_maddr(_start);
-        range->size = (unsigned long)xenheap_phys_end -
-                      (unsigned long)range->start;
+        range->size = (uint64_t)xenheap_phys_end -

And here it's even wrong, and I doubt it compiles without warning across the supported range of compilers.

Why might there be warnings in this case? At the worst, all it is doing is explicitly promoting a 32bit integer to a 64bit.

Quoted 3 lines

xen/arch/x86/x86_32/machine_kexec.c line 18

Quoted 1 line
xen/arch/x86/x86_32/machine_kexec.c +1 -0
+                      (uint64_t)range->start;

Casting range->start here and elsewhere shouldn't be necessary at all (the pre-existing cast was bogus too).

Agreed, but same comment regarding consistency, with a mix of not thinking about the implication on my behalf.

Quoted 5 lines

xen/arch/x86/x86_32/machine_kexec.c line 21

Quoted 3 lines
xen/arch/x86/x86_32/machine_kexec.c +0 -0
        return 0;
 }
 

Jan

Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer
T: +44 (0)1223 225 900, http://www.citrix.com
#3 lore raw
Quoted 31 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

To fix 32bit Xen which uses 32bit intergers for addresses and sizes, change the internals to use xen_kexec64_range_t which will use 64bit integers instead. This also invovles changing several casts to explicitly use uint64_ts rather than unsigned longs.

I don't think fixing 32-bit Xen is really necessary: Neither does anyone care much, nor should any address be beyond 4Gb in that case. Not playing with this will likely simplify the patch quite a bit.

This point was discussed on the IRC channel and it was decided to be worth doing, even though people are likely not to care else I would happily collapse the patch somewhat. Why should nothing be beyond 4GB in the 32bit case? Anything with PAE support ought to be able to use 64GB or less.

Quoted 6 lines
Quoted 6 lines
xen/arch/ia64/xen/machine_kexec.c +0 -0
@@ -102,10 +102,10 @@ void machine_reboot_kexec(xen_kexec_imag
 	machine_kexec(image);
 }
 
Quoted 7 lines

xen/arch/ia64/xen/machine_kexec.c void machine_reboot_kexec(xen_kexec_imag · line 108

Quoted 6 lines
xen/arch/ia64/xen/machine_kexec.c +2 -2
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
 	range->start = ia64_tpa(_text);
-	range->size = (unsigned long)_end - (unsigned long)_text;
+	range->size = (uint64_t)_end - (uint64_t)_text;

This is bogus and pointless (same thing a few lines down the patch).

Quoted 3 lines

I can understand pointless as sizeof(unsigned long) == sizeof(uint64_t) on 64bit builds, but why is it bogus? I changed it for consistency with xen_kexec64_range_t.

Because of the casting of pointers to other than unsigned long.

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/ia64/xen/machine_kexec.c +0 -0
 	return 0;
 }
 
xen/arch/x86/x86_32/machine_kexec.c +0 -0
@@ -11,11 +11,11 @@
 #include <asm/page.h>
 #include <public/kexec.h>
 
Quoted 9 lines

xen/arch/x86/x86_32/machine_kexec.c line 17

Quoted 7 lines
xen/arch/x86/x86_32/machine_kexec.c +2 -3
-int machine_kexec_get_xen(xen_kexec_range_t *range)
+int machine_kexec_get_xen(xen_kexec64_range_t *range)
 {
        range->start = virt_to_maddr(_start);
-        range->size = (unsigned long)xenheap_phys_end -
-                      (unsigned long)range->start;
+        range->size = (uint64_t)xenheap_phys_end -

And here it's even wrong, and I doubt it compiles without warning across the supported range of compilers.

Quoted 2 lines

Why might there be warnings in this case? At the worst, all it is doing is explicitly promoting a 32bit integer to a 64bit.

Oh, sorry, I somehow thought xenheap_phys_end would be a linker generated address symbol (mixed it with the various section end ones). But then - please just remove the casts (and perhaps as a general cleanup outside of this patch).

Jan

Quoted 16 lines
Quoted 3 lines

xen/arch/x86/x86_32/machine_kexec.c line 18

Quoted 1 line
xen/arch/x86/x86_32/machine_kexec.c +1 -0
+                      (uint64_t)range->start;

Casting range->start here and elsewhere shouldn't be necessary at all (the pre-existing cast was bogus too).

Agreed, but same comment regarding consistency, with a mix of not thinking about the implication on my behalf.

Quoted 5 lines

xen/arch/x86/x86_32/machine_kexec.c line 21

Quoted 3 lines
xen/arch/x86/x86_32/machine_kexec.c +0 -0
        return 0;
 }
 

Jan

Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer
T: +44 (0)1223 225 900, http://www.citrix.com