// Identify the Page Level the RegionStart must belongs to\r
PageLevel = 3 - ((BaseAddressAlignment - 12) / 9);\r
\r
- // If the required size is smaller than the current block size then we need to go to the page bellow.\r
- if (*BlockEntrySize < TT_ADDRESS_AT_LEVEL(PageLevel)) {\r
+ // If the required size is smaller than the current block size then we need to go to the page below.\r
+ // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment\r
+ // of the allocation size\r
+ if (*BlockEntrySize < TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel)) {\r
// It does not fit so we need to go a page level above\r
PageLevel++;\r
}\r
*TableLevel = PageLevel;\r
\r
// Now, we have the Table Level we can get the Block Size associated to this table\r
- *BlockEntrySize = TT_ADDRESS_AT_LEVEL(PageLevel);\r
+ *BlockEntrySize = TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel);\r
\r
//\r
// Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries\r
// Shift back to right to set zero before the effective address\r
BlockEntryAddress = BlockEntryAddress << TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel);\r
\r
- // Set the correct entry type\r
- if (IndexLevel + 1 == 3) {\r
+ // Set the correct entry type for the next page level\r
+ if ((IndexLevel + 1) == 3) {\r
Attributes |= TT_TYPE_BLOCK_ENTRY_LEVEL3;\r
} else {\r
Attributes |= TT_TYPE_BLOCK_ENTRY;\r
}\r
TranslationTable = (UINT64*)((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIPTION_TABLE);\r
\r
- // Fill the new BlockEntry with the TranslationTable\r
+ // Fill the BlockEntry with the new TranslationTable\r
*BlockEntry = ((UINTN)TranslationTable & TT_ADDRESS_MASK_DESCRIPTION_TABLE) | TableAttributes | TT_TYPE_TABLE_ENTRY;\r
// Update the last block entry with the newly created translation table\r
*LastBlockEntry = (UINT64*)((UINTN)TranslationTable + ((TT_ENTRY_COUNT - 1) * sizeof(UINT64)));\r