Skip to contentSkip to conversation

tools/ocaml/libs/xc: add OCaml stubs to query CPU policy

PATCH In review Edwin Török 3 participants 6 messages Jun 18, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/ocaml/libs/xc: add OCaml stubs to query CPU policy

Introduces following functions in Xenctrl and associated types:
get_system_cpu_policy
cpu_policy_to_featureset, string_of_xen_cpu_policy_index

These are wrappers around the existing C functions in xenctrl.h, that will be used by xenopsd initially.

-Wno-declaration-after-statement is disabled to allow mixing declarations and code to simplify writing the stubs by using variable length arrays on the stack instead of allocating/freeing memory
(which would require additional error-handling logic).

---
 tools/ocaml/libs/xc/Makefile        |   2 +-
 tools/ocaml/libs/xc/xenctrl.ml      |  37 ++++++
 tools/ocaml/libs/xc/xenctrl.mli     |  71 ++++++++++
 tools/ocaml/libs/xc/xenctrl_stubs.c | 195 ++++++++++++++++++++++++++++
 4 files changed, 304 insertions(+), 1 deletion(-)
tools/ocaml/libs/xc/Makefile +1 -1
index b6da4fdbaf..64dca99613 100644
@@ -3,7 +3,7 @@ XEN_ROOT=$(TOPLEVEL)/../..
 include $(TOPLEVEL)/common.make
 
 CFLAGS += -I../mmap $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest)
-CFLAGS += $(APPEND_CFLAGS)
+CFLAGS += $(APPEND_CFLAGS) -Wno-declaration-after-statement
 OCAMLINCLUDE += -I ../mmap
 
 OBJS = xenctrl
tools/ocaml/libs/xc/xenctrl.ml +37 -0
index a5588c643f..fa2cea5091 100644
@@ -286,6 +286,43 @@ external version_capabilities: handle -> string =
 type featureset_index = Featureset_raw | Featureset_host | Featureset_pv | Featureset_hvm
 external get_cpu_featureset : handle -> featureset_index -> int64 array = "stub_xc_get_cpu_featureset"
 
+(* order must match the order in Val_cpuid_leaf *)
+type xen_cpuid_leaf = {
+  leaf: int64;
+  subleaf: int64;
+  a: int64;
+  b: int64;
+  c: int64;
+  d: int64;
+}
+
+(* order must match the order in Val_msr_entry *)
+type xen_msr_entry = {
+  idx: int64;
+  flags: int64;
+  value: int64; (* val is a keyword, using 'value' *)
+}
+
+type xen_cpu_policy = {
+  leaves: xen_cpuid_leaf array;
+  msrs: xen_msr_entry array;
+}
+
+(* must match XEN_SYSCTL_cpu_policy* order in xen/include/public/sysctl.h *)
+type xen_cpu_policy_index = Cpu_policy_raw | Cpu_policy_host | Cpu_policy_pv_max | Cpu_policy_hvm_max | Cpu_policy_pv_default | Cpu_policy_hvm_default
+
+let string_of_xen_cpu_policy_index = function
+  | Cpu_policy_raw -> "Raw"
+  | Cpu_policy_host -> "Host"
+  | Cpu_policy_pv_max -> "PV Max"
+  | Cpu_policy_hvm_max -> "HVM Max"
+  | Cpu_policy_pv_default -> "PV default"
+  | Cpu_policy_hvm_default -> "HVM default"
+
+external get_system_cpu_policy: handle -> xen_cpu_policy_index -> xen_cpu_policy = "stub_xc_get_system_cpu_policy"
+
+external cpu_policy_to_featureset: handle -> xen_cpu_policy -> int64 array = "stub_xc_policy_to_featureset"
+
 external watchdog : handle -> int -> int32 -> int
   = "stub_xc_watchdog"
 
tools/ocaml/libs/xc/xenctrl.mli +71 -0

77 lines not on this page · raw message

tools/ocaml/libs/xc/xenctrl_stubs.c +195 -0

207 lines not on this page · raw message

Signature · 1 line
2.27.0
Acked-by on v1 1/1
#1 lore raw

On 18 Jun 2021, at 11:45, Edwin Török <[email protected]<mailto:[email protected]>> wrote:

Introduces following functions in Xenctrl and associated types:
get_system_cpu_policy
cpu_policy_to_featureset, string_of_xen_cpu_policy_index

These are wrappers around the existing C functions in xenctrl.h, that will be used by xenopsd initially.

-Wno-declaration-after-statement is disabled to allow mixing declarations and code to simplify writing the stubs by using variable length arrays on the stack instead of allocating/freeing memory
(which would require additional error-handling logic).

---

tools/ocaml/libs/xc/Makefile        |   2 +-
tools/ocaml/libs/xc/xenctrl.ml      |  37 ++++++
tools/ocaml/libs/xc/xenctrl.mli     |  71 ++++++++++
tools/ocaml/libs/xc/xenctrl_stubs.c | 195 ++++++++++++++++++++++++++++
4 files changed, 304 insertions(+), 1 deletion(-)

+static CAMLprim value Val_leaves(const xen_cpuid_leaf_t *leaves, uint32_t nr_leaves) +{

+    CAMLparam0();
+    CAMLlocal1(result);
+    uint32_t i;

+
+ result = caml_alloc(nr_leaves, 0);
+ for (i=0;i<nr_leaves;i++)
+ Store_field(result, i, Val_cpuid_leaf(&leaves[i]));
+
+ CAMLreturn(result);
+}

Is caml_alloc(nr_leaves, 0) the right allocation? The 0 is the tag. There is another instance of this below. What is the type of the returned value from an OCaml perspective?

— C

#2 lore raw
Quoted 33 lines · Christian Lindig
Quoted 19 lines · Edwin Török

Introduces following functions in Xenctrl and associated types:
get_system_cpu_policy
cpu_policy_to_featureset, string_of_xen_cpu_policy_index

These are wrappers around the existing C functions in xenctrl.h, that will be used by xenopsd initially.

-Wno-declaration-after-statement is disabled to allow mixing declarations and code to simplify writing the stubs by using variable length arrays on the stack instead of allocating/freeing memory
(which would require additional error-handling logic).

Quoted 7 lines

---

tools/ocaml/libs/xc/Makefile        |   2 +-
tools/ocaml/libs/xc/xenctrl.ml      |  37 ++++++
tools/ocaml/libs/xc/xenctrl.mli     |  71 ++++++++++
tools/ocaml/libs/xc/xenctrl_stubs.c | 195 ++++++++++++++++++++++++++++
4 files changed, 304 insertions(+), 1 deletion(-)
Quoted 1 line
Quoted 14 lines

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_watchdog(value xch, value domid, value timeout) · line 1283

Quoted 12 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +12 -0
+static CAMLprim value Val_leaves(const xen_cpuid_leaf_t *leaves, uint32_t nr_leaves)
+{
+    CAMLparam0();
+    CAMLlocal1(result);
+    uint32_t i;
+
+    result = caml_alloc(nr_leaves, 0);
+    for (i=0;i<nr_leaves;i++)
+        Store_field(result, i, Val_cpuid_leaf(&leaves[i]));
+
+    CAMLreturn(result);
+}

Is caml_alloc(nr_leaves, 0) the right allocation? The 0 is the tag. There is another instance of this below. What is the type of the returned value from an OCaml perspective?

It is an array, so I think tag 0 is. Right (that is what the implementation of call_alloc_array does too). I could perhaps try to use caml_alloc_array instead to make this more obvious.

Best regards,
—Edwin

Quoted 2 lines

— C

#3 lore raw
In reply to Edwin Török

tools/ocaml/libs/xc/xenctrl_stubs.c line 38

Quoted 10 lines · Edwin Török
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
index d05d7bb30e..4a230de8b7 100644
@@ -34,6 +34,9 @@
 #include <xenctrl.h>
 #include <xen-tools/libs.h>
 
+#include <xen/lib/x86/cpuid.h>
+#include <xen/lib/x86/msr.h>

https://gitlab.com/xen-project/patchew/xen/-/jobs/1358403495

CI says no.  This needs to be behind a suitable ifdef, for non-x86 builds.

(I've not looked at the rest of the patch yet.  I'll get around to it at some point.)

~Andrew

#4 lore raw
Quoted 13 lines · Andrew Cooper
Quoted 8 lines · Edwin Török
tools/ocaml/libs/xc/xenctrl_stubs.c +0 -0
index d05d7bb30e..4a230de8b7 100644
@@ -34,6 +34,9 @@
 #include <xenctrl.h>
 #include <xen-tools/libs.h>
 
Quoted 6 lines

tools/ocaml/libs/xc/xenctrl_stubs.c line 38

Quoted 2 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
+#include <xen/lib/x86/cpuid.h>
+#include <xen/lib/x86/msr.h>

https://gitlab.com/xen-project/patchew/xen/-/jobs/1358403495

CI says no. This needs to be behind a suitable ifdef, for non-x86 builds.

Should the stubs be disabled completely and raise ENOSYS/failwith on non-x86 (e.g. ARM), or are there plans on doing equivalent CPU policy on ARM at some point?

—Edwin

Quoted 5 lines

(I've not looked at the rest of the patch yet. I'll get around to it at some point.)

~Andrew

#5 lore raw
Quoted 11 lines · Edwin Torok
Quoted 11 lines · Andrew Cooper
Quoted 8 lines · Edwin Török
tools/ocaml/libs/xc/xenctrl_stubs.c +0 -0
index d05d7bb30e..4a230de8b7 100644
@@ -34,6 +34,9 @@
 #include <xenctrl.h>
 #include <xen-tools/libs.h>
 
Quoted 7 lines
Quoted 5 lines

tools/ocaml/libs/xc/xenctrl_stubs.c line 38

Quoted 2 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -0
+#include <xen/lib/x86/cpuid.h>
+#include <xen/lib/x86/msr.h>

https://gitlab.com/xen-project/patchew/xen/-/jobs/1358403495

CI says no. This needs to be behind a suitable ifdef, for non-x86 builds.

Should the stubs be disabled completely and raise ENOSYS/failwith on non-x86 (e.g. ARM), or are there plans on doing equivalent CPU policy on ARM at some point?

No plans.  "CPU Policies" comprising of CPUID and MSR data is entirely x86 specific.

ARM do need a logical equivalent, but don't even have migration yet, so all there is a stripped-down set of settings done by Xen.

~Andrew