Make page level an unsigned value.

A negative level isn't valid and expressing that in the type allows the
analyzers to better understand the code.

Change-Id: Ibae6d8a94358536428e5f6594e27e051e1675439
diff --git a/.gn b/.gn
index e5b6d4a..a61e044 100644
--- a/.gn
+++ b/.gn
@@ -1,2 +1,16 @@
+# Copyright 2018 Google LLC
+#
+# 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.
+
 # The location of the build configuration file.
 buildconfig = "//build/BUILDCONFIG.gn"
diff --git a/Makefile b/Makefile
index bccceb5..7c368ef 100644
--- a/Makefile
+++ b/Makefile
@@ -45,31 +45,31 @@
 .PHONY: format
 format:
 	@echo "Formatting..."
-	@find src/ -name *.c -o -name *.cc -o -name *.h | xargs clang-format -style file -i
-	@find inc/ -name *.c -o -name *.cc -o -name *.h | xargs clang-format -style file -i
-	@find test/ -name *.c -o -name *.cc -o -name *.h | xargs clang-format -style file -i
-	@find . \( -name *.gn -o -name *.gni \) | xargs -n1 $(GN) format
+	@find src/ -name \*.c -o -name \*.cc -o -name \*.h | xargs clang-format -style file -i
+	@find inc/ -name \*.c -o -name \*.cc -o -name \*.h | xargs clang-format -style file -i
+	@find test/ -name \*.c -o -name \*.cc -o -name \*.h | xargs clang-format -style file -i
+	@find . \( -name \*.gn -o -name \*.gni \) | xargs -n1 $(GN) format
 
 # see .clang-tidy.
 .PHONY: tidy
 tidy: $(OUT_DIR)/build.ninja
 	@$(NINJA) -C $(OUT_DIR)
 	@echo "Tidying..."
-	@find src/ \( -name *.c -o -name *.cc \) | xargs clang-tidy -p $(OUT_DIR) -fix
-	@find test/ \( -name *.c -o -name *.cc \) | xargs clang-tidy -p $(OUT_DIR) -fix
+	@find src/ \( -name \\*.c -o -name \*.cc \) | xargs clang-tidy -p $(OUT_DIR) -fix
+	@find test/ \( -name \*.c -o -name \*.cc \) | xargs clang-tidy -p $(OUT_DIR) -fix
 
 .PHONY: check
 check: $(OUT_DIR)/build.ninja
 	@$(NINJA) -C $(OUT_DIR)
 	@echo "Checking..."
-	@find src/ \( -name *.c -o -name *.cc \) | xargs clang-check -p $(OUT_DIR) -analyze
-	@find test/ \( -name *.c -o -name *.cc \) | xargs clang-check -p $(OUT_DIR) -analyze
+	@find src/ \( -name \*.c -o -name \*.cc \) | xargs clang-check -p $(OUT_DIR) -analyze -fix-what-you-can
+	@find test/ \( -name \*.c -o -name \*.cc \) | xargs clang-check -p $(OUT_DIR) -analyze -fix-what-you-can
 
 .PHONY: license
 license:
-	@find src/ -name *.c -o -name *.cc -o -name *.h | xargs -n1 python build/license.py --style c
-	@find inc/ -name *.c -o -name *.cc -o -name *.h | xargs -n1 python build/license.py --style c
-	@find test/ -name *.c -o -name *.cc -o -name *.h | xargs -n1 python build/license.py --style c
-	@find build/ -name *.py| xargs -n1 python build/license.py --style hash
-	@find test/ -name *.py| xargs -n1 python build/license.py --style hash
-	@find . \( -name *.gn -o -name *.gni \) | xargs -n1 python build/license.py --style hash
+	@find src/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -n1 python build/license.py --style c
+	@find inc/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -n1 python build/license.py --style c
+	@find test/ -name \*.c -o -name \*.cc -o -name \*.h | xargs -n1 python build/license.py --style c
+	@find build/ -name \*.py| xargs -n1 python build/license.py --style hash
+	@find test/ -name \*.py| xargs -n1 python build/license.py --style hash
+	@find . \( -name \*.gn -o -name \*.gni \) | xargs -n1 python build/license.py --style hash
diff --git a/build/BUILDCONFIG.gn b/build/BUILDCONFIG.gn
index 302e3f4..62cf43d 100644
--- a/build/BUILDCONFIG.gn
+++ b/build/BUILDCONFIG.gn
@@ -1,3 +1,17 @@
+# Copyright 2018 Google LLC
+#
+# 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.
+
 # This build configuration extends GN's cross-compilation concepts of "target",
 # "host" and "current" with "arch" which is used to refer to the embedded
 # architecture that the bare metal images are being built for.
@@ -60,6 +74,7 @@
 # Apply that default list to the binary target types.
 set_defaults("executable") {
   configs = _shared_binary_target_configs
+
   # Executables get this additional configuration
   configs += [ "//build:executable_ldconfig" ]
 }
diff --git a/src/arch/aarch64/inc/hf/arch/mm.h b/src/arch/aarch64/inc/hf/arch/mm.h
index 8f7028d..7ee6d68 100644
--- a/src/arch/aarch64/inc/hf/arch/mm.h
+++ b/src/arch/aarch64/inc/hf/arch/mm.h
@@ -202,7 +202,7 @@
 
 uint64_t arch_mm_mode_to_attrs(int mode);
 bool arch_mm_init(paddr_t table, bool first);
-int arch_mm_max_level(int mode);
+uint8_t arch_mm_max_level(int mode);
 
 /**
  * Given the attrs from a table at some level and the attrs from all the blocks
diff --git a/src/arch/aarch64/mm.c b/src/arch/aarch64/mm.c
index a416a82..32ab115 100644
--- a/src/arch/aarch64/mm.c
+++ b/src/arch/aarch64/mm.c
@@ -86,7 +86,7 @@
 
 /* clang-format on */
 
-static uint64_t mm_max_s2_level = 2;
+static uint8_t mm_max_s2_level = 2;
 
 uint64_t arch_mm_mode_to_attrs(int mode)
 {
@@ -159,7 +159,7 @@
 /**
  * Determines the maximum level supported by the given mode.
  */
-int arch_mm_max_level(int mode)
+uint8_t arch_mm_max_level(int mode)
 {
 	if (mode & MM_MODE_STAGE1) {
 		/*
diff --git a/src/arch/fake/inc/hf/arch/mm.h b/src/arch/fake/inc/hf/arch/mm.h
index 053eafa..98c73c7 100644
--- a/src/arch/fake/inc/hf/arch/mm.h
+++ b/src/arch/fake/inc/hf/arch/mm.h
@@ -185,7 +185,7 @@
 /**
  * Determines the maximum level supported by the given mode.
  */
-static inline int arch_mm_max_level(int mode)
+static inline uint8_t arch_mm_max_level(int mode)
 {
 	(void)mode;
 	return 2;
diff --git a/src/mm.c b/src/mm.c
index 7532825..a2f7cfb 100644
--- a/src/mm.c
+++ b/src/mm.c
@@ -82,7 +82,7 @@
  * Calculates the size of the address space represented by a page table entry at
  * the given level.
  */
-static size_t mm_entry_size(int level)
+static size_t mm_entry_size(uint8_t level)
 {
 	return UINT64_C(1) << (PAGE_BITS + level * PAGE_LEVEL_BITS);
 }
@@ -91,7 +91,7 @@
  * For a given address, calculates the maximum (plus one) address that can be
  * represented by the same table at the given level.
  */
-static ptable_addr_t mm_level_end(ptable_addr_t addr, int level)
+static ptable_addr_t mm_level_end(ptable_addr_t addr, uint8_t level)
 {
 	size_t offset = PAGE_BITS + (level + 1) * PAGE_LEVEL_BITS;
 	return ((addr >> offset) + 1) << offset;
@@ -101,7 +101,7 @@
  * For a given address, calculates the index at which its entry is stored in a
  * table at the given level.
  */
-static size_t mm_index(ptable_addr_t addr, int level)
+static size_t mm_index(ptable_addr_t addr, uint8_t level)
 {
 	ptable_addr_t v = addr >> (PAGE_BITS + level * PAGE_LEVEL_BITS);
 	return v & ((UINT64_C(1) << PAGE_LEVEL_BITS) - 1);
@@ -127,7 +127,7 @@
  *
  * Returns a pointer to the table the entry now points to.
  */
-static struct mm_page_table *mm_populate_table_pte(pte_t *pte, int level,
+static struct mm_page_table *mm_populate_table_pte(pte_t *pte, uint8_t level,
 						   bool sync_alloc)
 {
 	struct mm_page_table *ntable;
@@ -135,7 +135,7 @@
 	pte_t new_pte;
 	size_t i;
 	size_t inc;
-	int level_below = level - 1;
+	uint8_t level_below = level - 1;
 
 	/* Just return pointer to table if it's already populated. */
 	if (arch_mm_pte_is_table(v, level)) {
@@ -180,7 +180,7 @@
  * Frees all page-table-related memory associated with the given pte at the
  * given level, including any subtables recursively.
  */
-static void mm_free_page_pte(pte_t pte, int level)
+static void mm_free_page_pte(pte_t pte, uint8_t level)
 {
 	struct mm_page_table *table;
 	uint64_t i;
@@ -202,7 +202,7 @@
 /**
  * Returns whether all entries in this table are absent.
  */
-static bool mm_ptable_is_empty(struct mm_page_table *table, int level)
+static bool mm_ptable_is_empty(struct mm_page_table *table, uint8_t level)
 {
 	uint64_t i;
 
@@ -224,8 +224,8 @@
  * table.
  */
 static bool mm_map_level(ptable_addr_t begin, ptable_addr_t end, paddr_t pa,
-			 uint64_t attrs, struct mm_page_table *table, int level,
-			 int flags)
+			 uint64_t attrs, struct mm_page_table *table,
+			 uint8_t level, int flags)
 {
 	pte_t *pte = &table->entries[mm_index(begin, level)];
 	ptable_addr_t level_end = mm_level_end(begin, level);
@@ -331,7 +331,7 @@
 {
 	uint64_t attrs = arch_mm_mode_to_attrs(mode);
 	int flags = (mode & MM_MODE_NOSYNC) ? 0 : MAP_FLAG_SYNC;
-	int level = arch_mm_max_level(mode);
+	uint8_t level = arch_mm_max_level(mode);
 	struct mm_page_table *table = mm_page_table_from_pa(t->table);
 	ptable_addr_t begin;
 	ptable_addr_t end;
@@ -369,7 +369,7 @@
 {
 	int flags =
 		((mode & MM_MODE_NOSYNC) ? 0 : MAP_FLAG_SYNC) | MAP_FLAG_UNMAP;
-	int level = arch_mm_max_level(mode);
+	uint8_t level = arch_mm_max_level(mode);
 	struct mm_page_table *table = mm_page_table_from_pa(t->table);
 	ptable_addr_t begin;
 	ptable_addr_t end;
@@ -398,7 +398,7 @@
  * Writes the given table to the debug log, calling itself recursively to
  * write sub-tables.
  */
-static void mm_dump_table_recursive(struct mm_page_table *table, int level,
+static void mm_dump_table_recursive(struct mm_page_table *table, uint8_t level,
 				    int max_level)
 {
 	uint64_t i;
@@ -434,7 +434,7 @@
  * absent entry with which it can be replaced. Note that `entry` will no longer
  * be valid after calling this function as the subtable will have been freed.
  */
-static pte_t mm_table_pte_to_absent(pte_t entry, int level)
+static pte_t mm_table_pte_to_absent(pte_t entry, uint8_t level)
 {
 	struct mm_page_table *table =
 		mm_page_table_from_pa(arch_mm_table_from_pte(entry));
@@ -454,7 +454,7 @@
  * `entry` will no longer be valid after calling this function as the subtable
  * may have been freed.
  */
-static pte_t mm_table_pte_to_block(pte_t entry, int level)
+static pte_t mm_table_pte_to_block(pte_t entry, uint8_t level)
 {
 	struct mm_page_table *table;
 	uint64_t block_attrs;
@@ -491,7 +491,7 @@
  * Defragment the given ptable entry by recursively replacing any tables with
  * block or absent entries where possible.
  */
-static pte_t mm_ptable_defrag_entry(pte_t entry, int level)
+static pte_t mm_ptable_defrag_entry(pte_t entry, uint8_t level)
 {
 	struct mm_page_table *table;
 	uint64_t i;
@@ -546,7 +546,7 @@
 void mm_ptable_defrag(struct mm_ptable *t, int mode)
 {
 	struct mm_page_table *table = mm_page_table_from_pa(t->table);
-	int level = arch_mm_max_level(mode);
+	uint8_t level = arch_mm_max_level(mode);
 	uint64_t i;
 
 	/*
@@ -577,7 +577,7 @@
  * recursively traversing all levels of the page table.
  */
 static bool mm_is_mapped_recursive(struct mm_page_table *table,
-				   ptable_addr_t addr, int level)
+				   ptable_addr_t addr, uint8_t level)
 {
 	pte_t pte;
 	ptable_addr_t va_level_end = mm_level_end(addr, level);
@@ -610,7 +610,7 @@
 				int mode)
 {
 	struct mm_page_table *table = mm_page_table_from_pa(t->table);
-	int level = arch_mm_max_level(mode);
+	uint8_t level = arch_mm_max_level(mode);
 
 	addr = mm_round_down_to_page(addr);