Have vm_secondary_start_vcpu take vcpu rather than index.

This simplifies it, and the caller already has to check the index.
There's also no need to check whether the VM is the primary, as we
already assume it is a secondary anyway.

Change-Id: I0edd2a273fef3d4098e7c45ac7a1e81d1416844b
diff --git a/inc/hf/cpu.h b/inc/hf/cpu.h
index acad7ac..45dbb4f 100644
--- a/inc/hf/cpu.h
+++ b/inc/hf/cpu.h
@@ -126,6 +126,8 @@
 void vcpu_init(struct vcpu *vcpu, struct vm *vm);
 void vcpu_on(struct vcpu *vcpu, ipaddr_t entry, uintreg_t arg);
 size_t vcpu_index(const struct vcpu *vcpu);
+void vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
+				    uintreg_t arg);
 
 bool vcpu_handle_page_fault(const struct vcpu *current,
 			    struct vcpu_fault_info *f);
diff --git a/inc/hf/vm.h b/inc/hf/vm.h
index e202ab4..37dd437 100644
--- a/inc/hf/vm.h
+++ b/inc/hf/vm.h
@@ -95,7 +95,5 @@
 bool vm_init(uint32_t vcpu_count, struct mpool *ppool, struct vm **new_vm);
 uint32_t vm_get_count(void);
 struct vm *vm_get(uint32_t id);
-void vm_secondary_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry,
-			     uintreg_t arg);
 void vm_lock(struct vm *vm, struct vm_locked *locked);
 void vm_unlock(struct vm_locked *locked);
diff --git a/src/cpu.c b/src/cpu.c
index 56d76a4..2c7bef2 100644
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -181,6 +181,27 @@
 }
 
 /**
+ * Starts a vCPU of a secondary VM.
+ */
+void vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
+				    uintreg_t arg)
+{
+	struct vm *vm = vcpu->vm;
+
+	assert(vm->id != HF_PRIMARY_VM_ID);
+
+	/*
+	 * Set vCPU registers to a clean state ready for boot. As this is a
+	 * secondary which can migrate between pCPUs, the ID of the vCPU is
+	 * defined as the index and does not match the ID of the pCPU it is
+	 * running on.
+	 */
+	arch_regs_reset(&vcpu->regs, false, vm->id, vcpu_index(vcpu),
+			vm->ptable.root);
+	vcpu_on(vcpu, entry, arg);
+}
+
+/**
  * Handles a page fault. It does so by determining if it's a legitimate or
  * spurious fault, and recovering from the latter.
  *
diff --git a/src/load.c b/src/load.c
index 0f71770..56751aa 100644
--- a/src/load.c
+++ b/src/load.c
@@ -285,6 +285,7 @@
 		ipaddr_t secondary_entry;
 		const char *p;
 		struct vm *vm;
+		struct vcpu *vcpu;
 
 		dlog("Loading ");
 		for (p = name.next; p != name.limit; ++p) {
@@ -348,8 +349,9 @@
 		dlog("Loaded with %u vcpus, entry at 0x%x\n", cpu,
 		     pa_addr(secondary_mem_begin));
 
-		vm_secondary_start_vcpu(
-			vm, 0, secondary_entry,
+		vcpu = &vm->vcpus[0];
+		vcpu_secondary_reset_and_start(
+			vcpu, secondary_entry,
 			pa_difference(secondary_mem_begin, secondary_mem_end));
 	}
 
diff --git a/src/vm.c b/src/vm.c
index 532d5a2..f2bbe78 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -102,28 +102,3 @@
 	sl_unlock(&locked->vm->lock);
 	locked->vm = NULL;
 }
-
-/**
- * Starts a vCPU of a secondary VM.
- *
- * TODO: Shall we use index or id here?
- */
-void vm_secondary_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry,
-			     uintreg_t arg)
-{
-	struct vcpu *vcpu = &vm->vcpus[index];
-
-	if (index >= vm->vcpu_count) {
-		return;
-	}
-
-	/*
-	 * Set vCPU registers to a clean state ready for boot. As this is a
-	 * secondary which can migrate between pCPUs, the ID of the vCPU is
-	 * defined as the index and does not match the ID of the pCPU it is
-	 * running on.
-	 */
-	arch_regs_reset(&vcpu->regs, vm->id == HF_PRIMARY_VM_ID, vm->id, index,
-			vm->ptable.root);
-	vcpu_on(vcpu, entry, arg);
-}