Implement API to inject virtual interrupts into VMs.

This doesn't yet work properly in multiple-PE configurations, and doesn't have
any concept of priorities.

Bug: 117270899
Change-Id: Id62c59d78d0604b934aeca75ea459248db660488
diff --git a/inc/hf/api.h b/inc/hf/api.h
index 5359f86..692fbac 100644
--- a/inc/hf/api.h
+++ b/inc/hf/api.h
@@ -38,3 +38,9 @@
 
 struct vcpu *api_wait_for_interrupt(struct vcpu *current);
 struct vcpu *api_yield(struct vcpu *current);
+
+int64_t api_enable_interrupt(uint32_t intid, bool enable, struct vcpu *current);
+uint32_t api_get_and_acknowledge_interrupt(struct vcpu *current);
+int64_t api_inject_interrupt(uint32_t target_vm_id, uint32_t target_vcpu_idx,
+			     uint32_t intid, struct vcpu *current,
+			     struct vcpu **next);
diff --git a/inc/hf/cpu.h b/inc/hf/cpu.h
index 60261fe..d2029dd 100644
--- a/inc/hf/cpu.h
+++ b/inc/hf/cpu.h
@@ -25,6 +25,11 @@
 #include "hf/addr.h"
 #include "hf/spinlock.h"
 
+#include "vmapi/hf/types.h"
+
+/** The number of bits in each element of the interrupt bitfields. */
+#define INTERRUPT_REGISTER_BITS 32
+
 enum vcpu_state {
 	/** The vcpu is switched off. */
 	vcpu_state_off,
@@ -42,6 +47,13 @@
 	vcpu_state_blocked_interrupt,
 };
 
+struct interrupts {
+	/** Bitfield keeping track of which interrupts are enabled. */
+	uint32_t interrupt_enabled[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS];
+	/** Bitfield keeping track of which interrupts are pending. */
+	uint32_t interrupt_pending[HF_NUM_INTIDS / INTERRUPT_REGISTER_BITS];
+};
+
 struct vcpu {
 	struct spinlock lock;
 	enum vcpu_state state;
@@ -49,6 +61,7 @@
 	struct vm *vm;
 	struct vcpu *mailbox_next;
 	struct arch_regs regs;
+	struct interrupts interrupts;
 };
 
 /* TODO: Update alignment such that cpus are in different cache lines. */
diff --git a/inc/hf/std.h b/inc/hf/std.h
index 55922fa..39d3d08 100644
--- a/inc/hf/std.h
+++ b/inc/hf/std.h
@@ -27,6 +27,8 @@
 size_t strlen(const char *str);
 int strcmp(const char *a, const char *b);
 
+#define ctz(x) __builtin_ctz(x)
+
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
 
 #define be16toh(v) __builtin_bswap16(v)