--- /dev/null
+/** @file\r
+\r
+ Copyright (c) 2018, ARM Ltd. All rights reserved.\r
+\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#ifndef __STANDALONEMM_MMU_LIB__\r
+#define __STANDALONEMM_MMU_LIB__\r
+\r
+EFI_STATUS\r
+ArmSetMemoryRegionNoExec (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ );\r
+\r
+EFI_STATUS\r
+ArmClearMemoryRegionNoExec (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ );\r
+\r
+EFI_STATUS\r
+ArmSetMemoryRegionReadOnly (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ );\r
+\r
+EFI_STATUS\r
+ArmClearMemoryRegionReadOnly (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ );\r
+\r
+#endif /* __STANDALONEMM_MMU_LIB__ */\r
--- /dev/null
+/** @file\r
+* File managing the MMU for ARMv8 architecture in S-EL0\r
+*\r
+* Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.\r
+*\r
+* This program and the accompanying materials\r
+* are licensed and made available under the terms and conditions of the BSD License\r
+* which accompanies this distribution. The full text of the license may be found at\r
+* http://opensource.org/licenses/bsd-license.php\r
+*\r
+* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+*\r
+**/\r
+\r
+#include <Uefi.h>\r
+#include <IndustryStandard/ArmMmSvc.h>\r
+\r
+#include <Library/ArmLib.h>\r
+#include <Library/ArmMmuLib.h>\r
+#include <Library/ArmSvcLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+\r
+STATIC\r
+EFI_STATUS\r
+GetMemoryPermissions (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ OUT UINT32 *MemoryAttributes\r
+ )\r
+{\r
+ ARM_SVC_ARGS GetMemoryPermissionsSvcArgs = {0};\r
+\r
+ GetMemoryPermissionsSvcArgs.Arg0 = ARM_SVC_ID_SP_GET_MEM_ATTRIBUTES_AARCH64;\r
+ GetMemoryPermissionsSvcArgs.Arg1 = BaseAddress;\r
+ GetMemoryPermissionsSvcArgs.Arg2 = 0;\r
+ GetMemoryPermissionsSvcArgs.Arg3 = 0;\r
+\r
+ ArmCallSvc (&GetMemoryPermissionsSvcArgs);\r
+ if (GetMemoryPermissionsSvcArgs.Arg0 == ARM_SVC_SPM_RET_INVALID_PARAMS) {\r
+ *MemoryAttributes = 0;\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ *MemoryAttributes = GetMemoryPermissionsSvcArgs.Arg0;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+RequestMemoryPermissionChange (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ IN UINTN Permissions\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ ARM_SVC_ARGS ChangeMemoryPermissionsSvcArgs = {0};\r
+\r
+ ChangeMemoryPermissionsSvcArgs.Arg0 = ARM_SVC_ID_SP_SET_MEM_ATTRIBUTES_AARCH64;\r
+ ChangeMemoryPermissionsSvcArgs.Arg1 = BaseAddress;\r
+ ChangeMemoryPermissionsSvcArgs.Arg2 = EFI_SIZE_TO_PAGES(Length);\r
+ ChangeMemoryPermissionsSvcArgs.Arg3 = Permissions;\r
+\r
+ ArmCallSvc (&ChangeMemoryPermissionsSvcArgs);\r
+\r
+ Status = ChangeMemoryPermissionsSvcArgs.Arg0;\r
+\r
+ switch (Status) {\r
+ case ARM_SVC_SPM_RET_SUCCESS:\r
+ Status = EFI_SUCCESS;\r
+ break;\r
+\r
+ case ARM_SVC_SPM_RET_NOT_SUPPORTED:\r
+ Status = EFI_UNSUPPORTED;\r
+ break;\r
+\r
+ case ARM_SVC_SPM_RET_INVALID_PARAMS:\r
+ Status = EFI_INVALID_PARAMETER;\r
+ break;\r
+\r
+ case ARM_SVC_SPM_RET_DENIED:\r
+ Status = EFI_ACCESS_DENIED;\r
+ break;\r
+\r
+ case ARM_SVC_SPM_RET_NO_MEMORY:\r
+ Status = EFI_BAD_BUFFER_SIZE;\r
+ break;\r
+\r
+ default:\r
+ Status = EFI_ACCESS_DENIED;\r
+ ASSERT (0);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+ArmSetMemoryRegionNoExec (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT32 MemoryAttributes;\r
+ UINT32 CodePermission;\r
+\r
+ Status = GetMemoryPermissions (BaseAddress, &MemoryAttributes);\r
+ if (Status != EFI_INVALID_PARAMETER) {\r
+ CodePermission = SET_MEM_ATTR_CODE_PERM_XN << SET_MEM_ATTR_CODE_PERM_SHIFT;\r
+ return RequestMemoryPermissionChange (\r
+ BaseAddress,\r
+ Length,\r
+ MemoryAttributes | CodePermission\r
+ );\r
+ }\r
+ return EFI_INVALID_PARAMETER;\r
+}\r
+\r
+EFI_STATUS\r
+ArmClearMemoryRegionNoExec (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT32 MemoryAttributes;\r
+ UINT32 CodePermission;\r
+\r
+ Status = GetMemoryPermissions (BaseAddress, &MemoryAttributes);\r
+ if (Status != EFI_INVALID_PARAMETER) {\r
+ CodePermission = SET_MEM_ATTR_CODE_PERM_XN << SET_MEM_ATTR_CODE_PERM_SHIFT;\r
+ return RequestMemoryPermissionChange (\r
+ BaseAddress,\r
+ Length,\r
+ MemoryAttributes & ~CodePermission\r
+ );\r
+ }\r
+ return EFI_INVALID_PARAMETER;\r
+}\r
+\r
+EFI_STATUS\r
+ArmSetMemoryRegionReadOnly (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT32 MemoryAttributes;\r
+ UINT32 DataPermission;\r
+\r
+ Status = GetMemoryPermissions (BaseAddress, &MemoryAttributes);\r
+ if (Status != EFI_INVALID_PARAMETER) {\r
+ DataPermission = SET_MEM_ATTR_DATA_PERM_RO << SET_MEM_ATTR_DATA_PERM_SHIFT;\r
+ return RequestMemoryPermissionChange (\r
+ BaseAddress,\r
+ Length,\r
+ MemoryAttributes | DataPermission\r
+ );\r
+ }\r
+ return EFI_INVALID_PARAMETER;\r
+}\r
+\r
+EFI_STATUS\r
+ArmClearMemoryRegionReadOnly (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINT32 MemoryAttributes;\r
+ UINT32 PermissionRequest;\r
+\r
+ Status = GetMemoryPermissions (BaseAddress, &MemoryAttributes);\r
+ if (Status != EFI_INVALID_PARAMETER) {\r
+ PermissionRequest = SET_MEM_ATTR_MAKE_PERM_REQUEST (SET_MEM_ATTR_DATA_PERM_RW,\r
+ MemoryAttributes);\r
+ return RequestMemoryPermissionChange (\r
+ BaseAddress,\r
+ Length,\r
+ PermissionRequest\r
+ );\r
+ }\r
+ return EFI_INVALID_PARAMETER;\r
+}\r