Treat normal world as primary VM when running in secure world.

Change-Id: I7b34689d251dfcaeda5d2504418b87d1bdc1650b
diff --git a/src/arch/aarch64/inc/hf/arch/vmid_base.h b/src/arch/aarch64/inc/hf/arch/vmid_base.h
index fcc2271..834cadd 100644
--- a/src/arch/aarch64/inc/hf/arch/vmid_base.h
+++ b/src/arch/aarch64/inc/hf/arch/vmid_base.h
@@ -11,7 +11,23 @@
 #if SECURE_WORLD == 1
 #define HF_VM_ID_BASE 0x8000
 #define HF_OTHER_WORLD_ID HF_HYPERVISOR_VM_ID
+
+/**
+ * When running in the secure world, treat the normal world as the primary VM,
+ * as it is responsible for scheduling.
+ */
+#define HF_PRIMARY_VM_ID HF_OTHER_WORLD_ID
+
 #else
 #define HF_VM_ID_BASE 0
 #define HF_OTHER_WORLD_ID HF_TEE_VM_ID
+
+/**
+ * The ID of the primary VM, which is responsible for scheduling.
+ *
+ * This is not equal to its index because ID 0 is reserved for the hypervisor
+ * itself. The primary VM therefore gets ID 1 and all other VMs come after that.
+ */
+#define HF_PRIMARY_VM_ID (HF_VM_ID_OFFSET + HF_PRIMARY_VM_INDEX)
+
 #endif
diff --git a/src/arch/fake/inc/hf/arch/vmid_base.h b/src/arch/fake/inc/hf/arch/vmid_base.h
index 4652391..6989c35 100644
--- a/src/arch/fake/inc/hf/arch/vmid_base.h
+++ b/src/arch/fake/inc/hf/arch/vmid_base.h
@@ -10,3 +10,4 @@
 
 #define HF_VM_ID_BASE 0
 #define HF_OTHER_WORLD_ID HF_TEE_VM_ID
+#define HF_PRIMARY_VM_ID (HF_VM_ID_OFFSET + HF_PRIMARY_VM_INDEX)
diff --git a/src/load.c b/src/load.c
index bbc0f1b..660d77c 100644
--- a/src/load.c
+++ b/src/load.c
@@ -693,10 +693,17 @@
 	size_t i;
 	bool success = true;
 
-	if (!load_primary(stage1_locked, &manifest->vm[HF_PRIMARY_VM_INDEX],
-			  cpio, params, ppool)) {
-		dlog_error("Unable to load primary VM.\n");
-		return false;
+	/**
+	 * Only try to load the primary VM if it is supposed to be in this
+	 * world.
+	 */
+	if (vm_id_is_current_world(HF_PRIMARY_VM_ID)) {
+		if (!load_primary(stage1_locked,
+				  &manifest->vm[HF_PRIMARY_VM_INDEX], cpio,
+				  params, ppool)) {
+			dlog_error("Unable to load primary VM.\n");
+			return false;
+		}
 	}
 
 	if (!init_other_world_vm(ppool)) {