sel4utils: add function to set thread affinity

Irrespective of kernel version this will work, although different
scheduling parameters need to be passed in.
diff --git a/libsel4utils/include/sel4utils/thread.h b/libsel4utils/include/sel4utils/thread.h
index 25bd50c..6bb551c 100644
--- a/libsel4utils/include/sel4utils/thread.h
+++ b/libsel4utils/include/sel4utils/thread.h
@@ -203,6 +203,17 @@
  */
 void sel4utils_print_fault_message(seL4_MessageInfo_t tag, const char *name);
 
+/* Set the affinity of a thread, which will cause migration if the thread
+ * is running on a different core.
+ *
+ * On master, only use sched_params_set_core to set the core then call this function.
+ *
+ * If CONFIG_KERNEL_RT is set, the sched params must be fully populated or the scheduling
+ * context will be empty when it changes core as scheduling parameters of scheduling
+ * contexts are not maintained across migrations.
+ */
+int sel4utils_set_sched_affinity(sel4utils_thread_t *thread, sched_params_t params);
+
 static inline seL4_TCB
 sel4utils_get_tcb(sel4utils_thread_t *thread)
 {
diff --git a/libsel4utils/include/sel4utils/thread_config.h b/libsel4utils/include/sel4utils/thread_config.h
index 1ac2405..0d19553 100644
--- a/libsel4utils/include/sel4utils/thread_config.h
+++ b/libsel4utils/include/sel4utils/thread_config.h
@@ -41,6 +41,8 @@
     seL4_Word badge;
     /* otherwise use this provided sc */
     seL4_CPtr sched_context;
+    /* affinity for this tcb. Must set sched_ctrl if CONFIG_RT */
+    seL4_Word core;
 } sched_params_t;
 
 typedef struct sel4utils_thread_config {
@@ -86,6 +88,16 @@
     return sched_params_periodic(params, simple, core, timeslice_us, timeslice_us, 0, 0);
 }
 
+static inline sched_params_t
+sched_params_core(sched_params_t params, seL4_Word core)
+{
+    if (!config_set(CONFIG_KERNEL_RT)) {
+        ZF_LOGW("Setting core on RT kernel will have no effect - sched ctrl required");
+    }
+    params.core = core;
+    return params;
+}
+
 static inline sel4utils_thread_config_t
 thread_config_create_reply(sel4utils_thread_config_t config)
 {
diff --git a/libsel4utils/src/thread.c b/libsel4utils/src/thread.c
index 8301f6e..b4c6774 100644
--- a/libsel4utils/src/thread.c
+++ b/libsel4utils/src/thread.c
@@ -347,3 +347,13 @@
 {
     free(checkpoint->stack);
 }
+
+int sel4utils_set_sched_affinity(sel4utils_thread_t *thread, sched_params_t params) {
+#ifdef CONFIG_KERNEL_RT
+    return api_sched_ctrl_configure(params.sched_ctrl, thread->sched_context.cptr, params.budget, params.period,
+                                    params.extra_refills, params.badge);
+
+#else
+    return seL4_TCB_SetAffinity(thread->tcb.cptr, params.core);
+#endif
+}