Introduce new states to support memory sharing.

Stage-1 memory remains either valid or invalid but stage-2 memory is
owned and shared between VMs. The previous concept of unmapping means to
makethe memory absent and unowned.

Change-Id: I516abfe098fa6a7a072a47e89691687a302dd725
diff --git a/inc/hf/mm.h b/inc/hf/mm.h
index 912b5d3..11a7753 100644
--- a/inc/hf/mm.h
+++ b/inc/hf/mm.h
@@ -25,9 +25,73 @@
 
 #include "hf/addr.h"
 
+/* Keep macro alignment */
+/* clang-format off */
+
 #define PAGE_SIZE (1 << PAGE_BITS)
 #define MM_PTE_PER_PAGE (PAGE_SIZE / sizeof(pte_t))
 
+
+/* The following are arch-independent page mapping modes. */
+#define MM_MODE_R 0x0001 /* read */
+#define MM_MODE_W 0x0002 /* write */
+#define MM_MODE_X 0x0004 /* execute */
+#define MM_MODE_D 0x0008 /* device */
+
+/*
+ * Memory in stage-1 is either valid (present) or invalid (absent).
+ *
+ * Memory in stage-2 has more states to track sharing, borrowing and giving of
+ * memory. The states are made up of three parts:
+ *
+ *  1. V = valid/invalid    : Whether the memory is part of the VM's address
+ *                            space. A fault will be generated if accessed when
+ *                            invalid.
+ *  2. O = owned/unowned    : Whether the memory is owned by the VM.
+ *  3. X = exclusive/shared : Whether access is exclusive to the VM or shared
+ *                            with at most one other.
+ *
+ * These parts compose to form the following state:
+ *
+ *  -  V  O  X : Owner of memory with exclusive access.
+ *  -  V  O !X : Owner of memory with access shared with at most one other VM.
+ *  -  V !O  X : Borrower of memory with exclusive access.
+ *  -  V !O !X : Borrower of memory where access is shared with the owner.
+ *  - !V  O  X : Owner of memory lent to a VM that has exclusive access.
+ *
+ *  - !V  O !X : Unused. Owner of shared memory always has access.
+ *
+ *  - !V !O  X : Invalid memory. Memory is unrelated to the VM.
+ *  - !V !O !X : Invalid memory. Memory is unrelated to the VM.
+ *
+ *  Modes are selected so that owner of exclusive memory is the default.
+ */
+#define MM_MODE_INVALID 0x0010
+#define MM_MODE_UNOWNED 0x0020
+#define MM_MODE_SHARED  0x0040
+
+/**
+ * This flag indicates that memory allocation must not use locks. This is
+ * relevant in systems where interlocked operations are only available after
+ * virtual memory is enabled.
+ */
+#define MM_MODE_NOSYNC 0x0080
+
+/**
+ * This flag indicates that the mapping is intended to be used in a first
+ * stage translation table, which might have different encodings for the
+ * attribute bits than the second stage table.
+ */
+#define MM_MODE_STAGE1 0x0100
+
+/**
+ * This flag indicates that no TLB invalidations should be issued for the
+ * changes in the page table.
+ */
+#define MM_MODE_NOINVALIDATE 0x0200
+
+/* clang-format on */
+
 struct mm_page_table {
 	alignas(PAGE_SIZE) pte_t entries[MM_PTE_PER_PAGE];
 };
@@ -41,32 +105,6 @@
 	paddr_t root;
 };
 
-/* The following are arch-independent page mapping modes. */
-#define MM_MODE_R 0x01 /* read */
-#define MM_MODE_W 0x02 /* write */
-#define MM_MODE_X 0x04 /* execute */
-#define MM_MODE_D 0x08 /* device */
-
-/**
- * This flag indicates that memory allocation must not use locks. This is
- * relevant in systems where interlocked operations are only available after
- * virtual memory is enabled.
- */
-#define MM_MODE_NOSYNC 0x10
-
-/**
- * This flag indicates that the mapping is intended to be used in a first
- * stage translation table, which might have different encodings for the
- * attribute bits than the second stage table.
- */
-#define MM_MODE_STAGE1 0x20
-
-/**
- * This flag indicates that no TLB invalidations should be issued for the
- * changes in the page table.
- */
-#define MM_MODE_NOINVALIDATE 0x40
-
 bool mm_ptable_init(struct mm_ptable *t, int mode);
 void mm_ptable_fini(struct mm_ptable *t, int mode);
 void mm_ptable_dump(struct mm_ptable *t, int mode);