Skip to contentSkip to conversation

libxl: ocaml: guard x86-specific functions behind an ifdef

PATCH Applied Anil Madhavapeddy 3 participants 6 messages Jan 11, 2014 – Jan 15, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libxl: ocaml: guard x86-specific functions behind an ifdef

The various cpuid functions are not available on ARM, so this makes them raise an OCaml exception. Omitting the functions completely results in a link failure in oxenstored due to the missing symbols, so this is preferable to the much bigger patch that would result from adding conditional compilation into the OCaml interfaces.

With this patch, oxenstored can successfully start a domain on Xen/ARM.

---
 tools/ocaml/libs/xc/xenctrl_stubs.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/ocaml/libs/xc/xenctrl_stubs.c +12 -0
index f5cf0ed..ff29b47 100644
@@ -714,6 +714,7 @@ CAMLprim value stub_xc_domain_cpuid_set(value xch, value domid,
 {
 	CAMLparam4(xch, domid, input, config);
 	CAMLlocal2(array, tmp);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -742,17 +743,24 @@ CAMLprim value stub_xc_domain_cpuid_set(value xch, value domid,
 			 c_input, (const char **)c_config, out_config);
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_set: not implemented");
+#endif
 	CAMLreturn(array);
 }
 
 CAMLprim value stub_xc_domain_cpuid_apply_policy(value xch, value domid)
 {
 	CAMLparam2(xch, domid);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 
 	r = xc_cpuid_apply_policy(_H(xch), _D(domid));
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_apply_policy: not implemented");
+#endif
 	CAMLreturn(Val_unit);
 }
 
@@ -760,6 +768,7 @@ CAMLprim value stub_xc_cpuid_check(value xch, value input, value config)
 {
 	CAMLparam3(xch, input, config);
 	CAMLlocal3(ret, array, tmp);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -792,6 +801,9 @@ CAMLprim value stub_xc_cpuid_check(value xch, value input, value config)
 	Store_field(ret, 0, Val_bool(r));
 	Store_field(ret, 1, array);
 
+#else
+	caml_failwith("xc_domain_cpuid_check: not implemented");
+#endif
 	CAMLreturn(ret);
 }
 
Signature · 5 lines
1.8.1.2


-- 
Anil Madhavapeddy                                 http://anil.recoil.org
Acked-by David Scott on v2 1/1
#1 lore raw

Hi Anil,

Thanks for getting oxenstored on ARM working!

I'm happy with a simple 'Failure not implemented' exception for the moment. I think that once we're using the libxl bindings everywhere we can probably remove these libxc bindings anyway.

Cheers,
Dave

Quoted 83 lines

-----Original Message-----
From: Anil Madhavapeddy [mailto:[email protected]]
Sent: 11 January 2014 11:33 PM
To: [email protected]
Cc: Dave Scott
Subject: [PATCH v2] libxl: ocaml: guard x86-specific functions behind an ifdef

The various cpuid functions are not available on ARM, so this makes them raise an OCaml exception. Omitting the functions completely results in a link failure in oxenstored due to the missing symbols, so this is preferable to the much bigger patch that would result from adding conditional compilation into the OCaml interfaces.

With this patch, oxenstored can successfully start a domain on Xen/ARM.

---
 tools/ocaml/libs/xc/xenctrl_stubs.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/ocaml/libs/xc/xenctrl_stubs.c +9 -1
index f5cf0ed..ff29b47 100644
@@ -714,6 +714,7 @@ CAMLprim value stub_xc_domain_cpuid_set(value
 xch, value domid,  {
 	CAMLparam4(xch, domid, input, config);
 	CAMLlocal2(array, tmp);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -742,17 +743,24 @@ CAMLprim value stub_xc_domain_cpuid_set(value
 xch, value domid,
 			 c_input, (const char **)c_config, out_config);
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_set: not implemented"); #endif
 	CAMLreturn(array);
 }
 
 CAMLprim value stub_xc_domain_cpuid_apply_policy(value xch, value
 domid)  {
 	CAMLparam2(xch, domid);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 
 	r = xc_cpuid_apply_policy(_H(xch), _D(domid));
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_apply_policy: not implemented");
 #endif
 	CAMLreturn(Val_unit);
 }
@@ -760,6 +768,7 @@ CAMLprim value stub_xc_cpuid_check(value xch,
 value input, value config)  {
 	CAMLparam3(xch, input, config);
 	CAMLlocal3(ret, array, tmp);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -792,6 +801,9 @@ CAMLprim value stub_xc_cpuid_check(value xch,
 value input, value config)
 	Store_field(ret, 0, Val_bool(r));
 	Store_field(ret, 1, array);
 
+#else
+	caml_failwith("xc_domain_cpuid_check: not implemented"); #endif
 	CAMLreturn(ret);
 }
 
--

1.8.1.2

--
Anil Madhavapeddy http://anil.recoil.org

Acked-by
#2 lore raw
Quoted 7 lines · Dave Scott

Hi Anil,

Thanks for getting oxenstored on ARM working!

I'm happy with a simple 'Failure not implemented' exception for the moment. I think that once we're using the libxl bindings everywhere we can probably remove these libxc bindings anyway.

Release hat: This is pretty tightly targeted, and the worst that could happen is a build failure, which is generally pretty easy to detect before a release, although perhaps less so in the case of ocaml which isn't enabled by everyone.

Although I have two misgivings in that regard:

Quoted 9 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_domain_cpuid_set(value · line 716

Quoted 9 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +0 -0
index f5cf0ed..ff29b47 100644
@@ -714,6 +714,7 @@ CAMLprim value stub_xc_domain_cpuid_set(value
 xch, value domid,  {
 	CAMLparam4(xch, domid, input, config);
 	CAMLlocal2(array, tmp);

Aren't these variables now unused? Does the compiler not complain about this (with -Werror => build failure)?

Perhaps CAMLlocal2 both defines and references the variables keeping this issue at bay?

Quoted 5 lines
Quoted 5 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -0
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -742,17 +743,24 @@ CAMLprim value stub_xc_domain_cpuid_set(value
Quoted 8 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_domain_cpuid_set(value xch, value domid, · line 749

Quoted 8 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
 xch, value domid,
 			 c_input, (const char **)c_config, out_config);
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_set: not implemented");
 #endif
 	CAMLreturn(array);

In the !__i386__ && !__x86_64__ case this code is unreachable, right because caml_failwith is marked Noreturn?

Is there any chance that some compiler version might pick up on this and complain about the dead code? Or perhaps complain about the uninitialised use of array?

I suppose putting the CAMLreturn inside the x86 case runs the opposite risk of a compiler which doesn't pay proper attention to Noreturn and therefore things we are reaching the end of a non-void function? Would CAMLreturn(unit) be appropriate in that case?

Quoted 8 lines
Quoted 8 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -0
 }
 
 CAMLprim value stub_xc_domain_cpuid_apply_policy(value xch, value
 domid)  {
 	CAMLparam2(xch, domid);
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 
Quoted 7 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_domain_cpuid_set(value xch, value domid, · line 764

Quoted 7 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
 	r = xc_cpuid_apply_policy(_H(xch), _D(domid));
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_apply_policy: not implemented");
 #endif
 	CAMLreturn(Val_unit);

Unreached?

Quoted 6 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_cpuid_check(value xch, · line 770

Quoted 6 lines

}

tools/ocaml/libs/xc/xenctrl_stubs.c +0 -0
@@ -760,6 +768,7 @@ CAMLprim value stub_xc_cpuid_check(value xch,
 value input, value config)  {
 	CAMLparam3(xch, input, config);
 	CAMLlocal3(ret, array, tmp);

Unused?

Quoted 9 lines
Quoted 9 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -0
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -792,6 +801,9 @@ CAMLprim value stub_xc_cpuid_check(value xch,
 value input, value config)
 	Store_field(ret, 0, Val_bool(r));
 	Store_field(ret, 1, array);
 
Quoted 4 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_cpuid_check(value xch, value input, value config) · line 807

Quoted 4 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
+#else
+	caml_failwith("xc_domain_cpuid_check: not implemented");
 #endif
 	CAMLreturn(ret);

Unreached?

Quoted 13 lines
Quoted 8 lines

}

--
1.8.1.2

--
Anil Madhavapeddy http://anil.recoil.org

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

#3 lore raw
Quoted 33 lines · Ian Campbell
Quoted 9 lines · Dave Scott

Hi Anil,

Thanks for getting oxenstored on ARM working!

I'm happy with a simple 'Failure not implemented' exception for the moment. I think that once we're using the libxl bindings everywhere we can probably remove these libxc bindings anyway.

Release hat: This is pretty tightly targeted, and the worst that could happen is a build failure, which is generally pretty easy to detect before a release, although perhaps less so in the case of ocaml which isn't enabled by everyone.

Although I have two misgivings in that regard:

Quoted 9 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_domain_cpuid_set(value · line 716

Quoted 9 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +0 -0
index f5cf0ed..ff29b47 100644
@@ -714,6 +714,7 @@ CAMLprim value stub_xc_domain_cpuid_set(value
 xch, value domid,  {
 	CAMLparam4(xch, domid, input, config);
 	CAMLlocal2(array, tmp);

Aren't these variables now unused? Does the compiler not complain about this (with -Werror => build failure)?

Quoted 2 lines

Perhaps CAMLlocal2 both defines and references the variables keeping this issue at bay?

That's right. CAMLlocal2 creates a stack variable and registers it with the garbage collector as a root (to ensure that it's not collected during the lifetime of the function). This keeps it live and always used from the perspective of the C compiler.

I confirmed this with a 'make V=1' with '-Wall -Werror' to make sure, and it compiles fine on gcc-4.8.1. There's an unrelated signed/unsigned error if -Wextra is included that I'll look at separately and is unrelated to this patch.

Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -0
+#if defined(__i386__) || defined(__x86_64__)
 	int r;
 	unsigned int c_input[2];
 	char *c_config[4], *out_config[4];
@@ -742,17 +743,24 @@ CAMLprim value stub_xc_domain_cpuid_set(value
Quoted 8 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_domain_cpuid_set(value xch, value domid, · line 749

Quoted 8 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
 xch, value domid,
 			 c_input, (const char **)c_config, out_config);
 	if (r < 0)
 		failwith_xc(_H(xch));
+#else
+	caml_failwith("xc_domain_cpuid_set: not implemented");
 #endif
 	CAMLreturn(array);

In the !__i386__ && !__x86_64__ case this code is unreachable, right because caml_failwith is marked Noreturn?

Quoted 8 lines

Is there any chance that some compiler version might pick up on this and complain about the dead code? Or perhaps complain about the uninitialised use of array?

I suppose putting the CAMLreturn inside the x86 case runs the opposite risk of a compiler which doesn't pay proper attention to Noreturn and therefore things we are reaching the end of a non-void function? Would CAMLreturn(unit) be appropriate in that case?

Yeah, I'm not aware of any compiler that doesn't respect the noreturn attribute and also emits unused variable warnings. I didn't modify the CAMLreturn in favour of minimising the x86/ARM differences, but you could modify the #endif to be an #else/#endif to only return on x86. I'd prefer to keep these bindings as straight-line as possible for the 4.4 release though, and to refactor oxenstored to not depend on them at all in the future (it only uses a small part of libxc and these cpuid functions aren't used at all).

(snip)

Quoted 7 lines
Quoted 5 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_cpuid_check(value xch, value input, value config) · line 807

Quoted 5 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
+#else
+	caml_failwith("xc_domain_cpuid_check: not implemented");
 #endif
 	CAMLreturn(ret);

Unreached?

See above.

cheers,
Anil

#4 lore raw
Quoted 7 lines · Anil Madhavapeddy
Quoted 2 lines

Perhaps CAMLlocal2 both defines and references the variables keeping this issue at bay?

That's right. CAMLlocal2 creates a stack variable and registers it with the garbage collector as a root (to ensure that it's not collected during the lifetime of the function). This keeps it live and always used from the perspective of the C compiler.

Thanks.

Quoted 8 lines

Yeah, I'm not aware of any compiler that doesn't respect the noreturn attribute and also emits unused variable warnings. I didn't modify the CAMLreturn in favour of minimising the x86/ARM differences, but you could modify the #endif to be an #else/#endif to only return on x86. I'd prefer to keep these bindings as straight-line as possible for the 4.4 release though, and to refactor oxenstored to not depend on them at all in the future (it only uses a small part of libxc and these cpuid functions aren't used at all).

Thanks, I'm convinced by that argument, this can go in after rc2 is cut.

Release-ack: Ian Campbell

Ian.

Applied
#5 lore raw
Quoted 21 lines · Ian Campbell
Quoted 7 lines · Anil Madhavapeddy
Quoted 2 lines

Perhaps CAMLlocal2 both defines and references the variables keeping this issue at bay?

That's right. CAMLlocal2 creates a stack variable and registers it with the garbage collector as a root (to ensure that it's not collected during the lifetime of the function). This keeps it live and always used from the perspective of the C compiler.

Thanks.

Quoted 8 lines

Yeah, I'm not aware of any compiler that doesn't respect the noreturn attribute and also emits unused variable warnings. I didn't modify the CAMLreturn in favour of minimising the x86/ARM differences, but you could modify the #endif to be an #else/#endif to only return on x86. I'd prefer to keep these bindings as straight-line as possible for the 4.4 release though, and to refactor oxenstored to not depend on them at all in the future (it only uses a small part of libxc and these cpuid functions aren't used at all).

Quoted 3 lines

Thanks, I'm convinced by that argument, this can go in after rc2 is cut.

Release-ack: Ian Campbell

Applied