[REFACTOR] Move vCPU code to separate file

This is to facilitate having a better framework for controlling access to CPU
features and registers.

Bug: 144898870
Change-Id: I68d7f08a4cce8e6b0bf2aa0325254a8a26fbf7fc
diff --git a/src/BUILD.gn b/src/BUILD.gn
index f09602c..3859871 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -53,6 +53,7 @@
     "panic.c",
     "spci_architected_message.c",
     "string.c",
+    "vcpu.c",
     "vm.c",
   ]
 
diff --git a/src/arch/aarch64/hypervisor/cpu.c b/src/arch/aarch64/hypervisor/cpu.c
index c0886fe..0e792f5 100644
--- a/src/arch/aarch64/hypervisor/cpu.c
+++ b/src/arch/aarch64/hypervisor/cpu.c
@@ -23,6 +23,7 @@
 #include "hf/addr.h"
 #include "hf/spci.h"
 #include "hf/std.h"
+#include "hf/vm.h"
 
 #include "msr.h"
 #include "perfmon.h"
@@ -63,9 +64,13 @@
 #endif
 }
 
-void arch_regs_reset(struct arch_regs *r, bool is_primary, spci_vm_id_t vm_id,
-		     cpu_id_t vcpu_id, paddr_t table)
+void arch_regs_reset(struct vcpu *vcpu)
 {
+	spci_vm_id_t vm_id = vcpu->vm->id;
+	bool is_primary = vm_id == HF_PRIMARY_VM_ID;
+	cpu_id_t vcpu_id = vcpu_index(vcpu);
+	paddr_t table = vcpu->vm->ptable.root;
+	struct arch_regs *r = &vcpu->regs;
 	uintreg_t pc = r->pc;
 	uintreg_t arg = r->r[0];
 	uintreg_t cnthctl;
diff --git a/src/arch/fake/hypervisor/cpu.c b/src/arch/fake/hypervisor/cpu.c
index 71d6f92..8014b3f 100644
--- a/src/arch/fake/hypervisor/cpu.c
+++ b/src/arch/fake/hypervisor/cpu.c
@@ -16,6 +16,7 @@
 
 #include "hf/arch/cpu.h"
 
+#include "hf/cpu.h"
 #include "hf/spci.h"
 
 void arch_irq_disable(void)
@@ -28,14 +29,10 @@
 	/* TODO */
 }
 
-void arch_regs_reset(struct arch_regs *r, bool is_primary, spci_vm_id_t vm_id,
-		     cpu_id_t vcpu_id, paddr_t table)
+void arch_regs_reset(struct vcpu *vcpu)
 {
 	/* TODO */
-	(void)is_primary;
-	(void)vm_id;
-	(void)table;
-	r->vcpu_id = vcpu_id;
+	(void)vcpu;
 }
 
 void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
diff --git a/src/cpu.c b/src/cpu.c
index aeb1127..13c458d 100644
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -18,14 +18,9 @@
 
 #include <stdalign.h>
 
-#include "hf/arch/cpu.h"
-
 #include "hf/api.h"
 #include "hf/check.h"
 #include "hf/dlog.h"
-#include "hf/spci.h"
-#include "hf/std.h"
-#include "hf/vm.h"
 
 #include "vmapi/hf/call.h"
 
@@ -177,156 +172,3 @@
 
 	return NULL;
 }
-
-/**
- * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
- */
-struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
-{
-	struct vcpu_locked locked = {
-		.vcpu = vcpu,
-	};
-
-	sl_lock(&vcpu->lock);
-
-	return locked;
-}
-
-/**
- * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
- * reflect the fact that the vCPU is no longer locked.
- */
-void vcpu_unlock(struct vcpu_locked *locked)
-{
-	sl_unlock(&locked->vcpu->lock);
-	locked->vcpu = NULL;
-}
-
-void vcpu_init(struct vcpu *vcpu, struct vm *vm)
-{
-	memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
-	sl_init(&vcpu->lock);
-	vcpu->regs_available = true;
-	vcpu->vm = vm;
-	vcpu->state = VCPU_STATE_OFF;
-}
-
-/**
- * Initialise the registers for the given vCPU and set the state to
- * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this.
- */
-void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
-{
-	arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
-	vcpu.vcpu->state = VCPU_STATE_READY;
-}
-
-spci_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
-{
-	size_t index = vcpu - vcpu->vm->vcpus;
-
-	CHECK(index < UINT16_MAX);
-	return index;
-}
-
-/**
- * Check whether the given vcpu_state is an off state, for the purpose of
- * turning vCPUs on and off. Note that aborted still counts as on in this
- * context.
- */
-bool vcpu_is_off(struct vcpu_locked vcpu)
-{
-	switch (vcpu.vcpu->state) {
-	case VCPU_STATE_OFF:
-		return true;
-	case VCPU_STATE_READY:
-	case VCPU_STATE_RUNNING:
-	case VCPU_STATE_BLOCKED_MAILBOX:
-	case VCPU_STATE_BLOCKED_INTERRUPT:
-	case VCPU_STATE_ABORTED:
-		/*
-		 * Aborted still counts as ON for the purposes of PSCI,
-		 * because according to the PSCI specification (section
-		 * 5.7.1) a core is only considered to be off if it has
-		 * been turned off with a CPU_OFF call or hasn't yet
-		 * been turned on with a CPU_ON call.
-		 */
-		return false;
-	}
-}
-
-/**
- * Starts a vCPU of a secondary VM.
- *
- * Returns true if the secondary was reset and started, or false if it was
- * already on and so nothing was done.
- */
-bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
-				    uintreg_t arg)
-{
-	struct vcpu_locked vcpu_locked;
-	struct vm *vm = vcpu->vm;
-	bool vcpu_was_off;
-
-	CHECK(vm->id != HF_PRIMARY_VM_ID);
-
-	vcpu_locked = vcpu_lock(vcpu);
-	vcpu_was_off = vcpu_is_off(vcpu_locked);
-	if (vcpu_was_off) {
-		/*
-		 * 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_locked, entry, arg);
-	}
-	vcpu_unlock(&vcpu_locked);
-
-	return vcpu_was_off;
-}
-
-/**
- * Handles a page fault. It does so by determining if it's a legitimate or
- * spurious fault, and recovering from the latter.
- *
- * Returns true if the caller should resume the current vcpu, or false if its VM
- * should be aborted.
- */
-bool vcpu_handle_page_fault(const struct vcpu *current,
-			    struct vcpu_fault_info *f)
-{
-	struct vm *vm = current->vm;
-	uint32_t mode;
-	uint32_t mask = f->mode | MM_MODE_INVALID;
-	bool resume;
-
-	sl_lock(&vm->lock);
-
-	/*
-	 * Check if this is a legitimate fault, i.e., if the page table doesn't
-	 * allow the access attemped by the VM.
-	 *
-	 * Otherwise, this is a spurious fault, likely because another CPU is
-	 * updating the page table. It is responsible for issuing global TLB
-	 * invalidations while holding the VM lock, so we don't need to do
-	 * anything else to recover from it. (Acquiring/releasing the lock
-	 * ensured that the invalidations have completed.)
-	 */
-	resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1),
-				&mode) &&
-		 (mode & mask) == f->mode;
-
-	sl_unlock(&vm->lock);
-
-	if (!resume) {
-		dlog("Stage-2 page fault: pc=%#x, vmid=%u, vcpu=%u, "
-		     "vaddr=%#x, ipaddr=%#x, mode=%#x\n",
-		     f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr,
-		     f->mode);
-	}
-
-	return resume;
-}
diff --git a/src/main.c b/src/main.c
index 8896b77..b12032e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,16 +24,14 @@
 struct vcpu *cpu_main(struct cpu *c)
 {
 	struct vcpu *vcpu;
-	struct vm *vm;
 
 	vcpu = vm_get_vcpu(vm_find(HF_PRIMARY_VM_ID), cpu_index(c));
-	vm = vcpu->vm;
 	vcpu->cpu = c;
 
 	arch_cpu_init();
 
 	/* Reset the registers to give a clean start for the primary's vCPU. */
-	arch_regs_reset(&vcpu->regs, true, vm->id, c->id, vm->ptable.root);
+	arch_regs_reset(vcpu);
 
 	return vcpu;
 }
diff --git a/src/vcpu.c b/src/vcpu.c
new file mode 100644
index 0000000..a637a5a
--- /dev/null
+++ b/src/vcpu.c
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2019 The Hafnium Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "hf/vcpu.h"
+
+#include "hf/check.h"
+#include "hf/dlog.h"
+#include "hf/std.h"
+#include "hf/vm.h"
+
+/**
+ * Locks the given vCPU and updates `locked` to hold the newly locked vCPU.
+ */
+struct vcpu_locked vcpu_lock(struct vcpu *vcpu)
+{
+	struct vcpu_locked locked = {
+		.vcpu = vcpu,
+	};
+
+	sl_lock(&vcpu->lock);
+
+	return locked;
+}
+
+/**
+ * Unlocks a vCPU previously locked with vpu_lock, and updates `locked` to
+ * reflect the fact that the vCPU is no longer locked.
+ */
+void vcpu_unlock(struct vcpu_locked *locked)
+{
+	sl_unlock(&locked->vcpu->lock);
+	locked->vcpu = NULL;
+}
+
+void vcpu_init(struct vcpu *vcpu, struct vm *vm)
+{
+	memset_s(vcpu, sizeof(*vcpu), 0, sizeof(*vcpu));
+	sl_init(&vcpu->lock);
+	vcpu->regs_available = true;
+	vcpu->vm = vm;
+	vcpu->state = VCPU_STATE_OFF;
+}
+
+/**
+ * Initialise the registers for the given vCPU and set the state to
+ * VCPU_STATE_READY. The caller must hold the vCPU lock while calling this.
+ */
+void vcpu_on(struct vcpu_locked vcpu, ipaddr_t entry, uintreg_t arg)
+{
+	arch_regs_set_pc_arg(&vcpu.vcpu->regs, entry, arg);
+	vcpu.vcpu->state = VCPU_STATE_READY;
+}
+
+spci_vcpu_index_t vcpu_index(const struct vcpu *vcpu)
+{
+	size_t index = vcpu - vcpu->vm->vcpus;
+
+	CHECK(index < UINT16_MAX);
+	return index;
+}
+
+/**
+ * Check whether the given vcpu_state is an off state, for the purpose of
+ * turning vCPUs on and off. Note that aborted still counts as on in this
+ * context.
+ */
+bool vcpu_is_off(struct vcpu_locked vcpu)
+{
+	switch (vcpu.vcpu->state) {
+	case VCPU_STATE_OFF:
+		return true;
+	case VCPU_STATE_READY:
+	case VCPU_STATE_RUNNING:
+	case VCPU_STATE_BLOCKED_MAILBOX:
+	case VCPU_STATE_BLOCKED_INTERRUPT:
+	case VCPU_STATE_ABORTED:
+		/*
+		 * Aborted still counts as ON for the purposes of PSCI,
+		 * because according to the PSCI specification (section
+		 * 5.7.1) a core is only considered to be off if it has
+		 * been turned off with a CPU_OFF call or hasn't yet
+		 * been turned on with a CPU_ON call.
+		 */
+		return false;
+	}
+}
+
+/**
+ * Starts a vCPU of a secondary VM.
+ *
+ * Returns true if the secondary was reset and started, or false if it was
+ * already on and so nothing was done.
+ */
+bool vcpu_secondary_reset_and_start(struct vcpu *vcpu, ipaddr_t entry,
+				    uintreg_t arg)
+{
+	struct vcpu_locked vcpu_locked;
+	struct vm *vm = vcpu->vm;
+	bool vcpu_was_off;
+
+	CHECK(vm->id != HF_PRIMARY_VM_ID);
+
+	vcpu_locked = vcpu_lock(vcpu);
+	vcpu_was_off = vcpu_is_off(vcpu_locked);
+	if (vcpu_was_off) {
+		/*
+		 * 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);
+		vcpu_on(vcpu_locked, entry, arg);
+	}
+	vcpu_unlock(&vcpu_locked);
+
+	return vcpu_was_off;
+}
+
+/**
+ * Handles a page fault. It does so by determining if it's a legitimate or
+ * spurious fault, and recovering from the latter.
+ *
+ * Returns true if the caller should resume the current vcpu, or false if its VM
+ * should be aborted.
+ */
+bool vcpu_handle_page_fault(const struct vcpu *current,
+			    struct vcpu_fault_info *f)
+{
+	struct vm *vm = current->vm;
+	uint32_t mode;
+	uint32_t mask = f->mode | MM_MODE_INVALID;
+	bool resume;
+
+	sl_lock(&vm->lock);
+
+	/*
+	 * Check if this is a legitimate fault, i.e., if the page table doesn't
+	 * allow the access attempted by the VM.
+	 *
+	 * Otherwise, this is a spurious fault, likely because another CPU is
+	 * updating the page table. It is responsible for issuing global TLB
+	 * invalidations while holding the VM lock, so we don't need to do
+	 * anything else to recover from it. (Acquiring/releasing the lock
+	 * ensured that the invalidations have completed.)
+	 */
+	resume = mm_vm_get_mode(&vm->ptable, f->ipaddr, ipa_add(f->ipaddr, 1),
+				&mode) &&
+		 (mode & mask) == f->mode;
+
+	sl_unlock(&vm->lock);
+
+	if (!resume) {
+		dlog("Stage-2 page fault: pc=%#x, vmid=%u, vcpu=%u, "
+		     "vaddr=%#x, ipaddr=%#x, mode=%#x\n",
+		     f->pc, vm->id, vcpu_index(current), f->vaddr, f->ipaddr,
+		     f->mode);
+	}
+
+	return resume;
+}