Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
btwise
OpenCorePKG_MOD
提交
f30c1c65
O
OpenCorePKG_MOD
项目概览
btwise
/
OpenCorePKG_MOD
通知
26
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
OpenCorePKG_MOD
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f30c1c65
编写于
4月 03, 2020
作者:
V
vit9696
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
OcMemoryLib: Add memory map splitting routines
上级
cfda552f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
311 addition
and
23 deletion
+311
-23
Include/Library/OcMemoryLib.h
Include/Library/OcMemoryLib.h
+36
-0
Library/OcMemoryLib/MemoryMap.c
Library/OcMemoryLib/MemoryMap.c
+275
-23
未找到文件。
Include/Library/OcMemoryLib.h
浏览文件 @
f30c1c65
...
...
@@ -16,6 +16,7 @@
#define OC_MEMORY_LIB_H
#include <Uefi.h>
#include <Guid/MemoryAttributesTable.h>
#include <IndustryStandard/VirtualMemory.h>
/**
...
...
@@ -245,6 +246,19 @@ OcUpdateDescriptors (
IN
UINT64
DropAttributes
);
/**
Obtain memory attributes table.
@param[out] MemoryAttributesEntry memory descriptor pointer, optional.
@retval pointer to memory attributes table.
@retval NULL if memory attributes table is unsupported.
**/
EFI_MEMORY_ATTRIBUTES_TABLE
*
OcGetMemoryAttributes
(
OUT
EFI_MEMORY_DESCRIPTOR
**
MemoryAttributesEntry
OPTIONAL
);
/**
Refresh memory attributes entry containing the specified address.
...
...
@@ -275,6 +289,28 @@ OcCountSplitDescritptors (
VOID
);
/**
Split memory map by memory attributes if available.
@param[in] OriginalMemoryMapSize Upper memory map size bound for growth.
@param[in,out] MemoryMapSize Current memory map size, updated on return.
@param[in,out] MemoryMap Memory map to split.
@param[in] DescriptorSize Memory map descriptor size.
Note, the function is guaranteed to return valid memory map, though not necessarily split.
@retval EFI_SUCCESS on success.
@retval EFI_UNSUPPORTED memory attributes are not supported by the platform.
@retval EFI_OUT_OF_RESOURCES new memory map did not fit.
**/
EFI_STATUS
OcSplitMemoryMapByAttributes
(
IN
UINTN
OriginalMemoryMapSize
,
IN
OUT
UINTN
*
MemoryMapSize
,
IN
OUT
EFI_MEMORY_DESCRIPTOR
*
MemoryMap
,
IN
UINTN
DescriptorSize
);
/**
Return pointer to PML4 table in PageTable and PWT and PCD flags in Flags.
...
...
Library/OcMemoryLib/MemoryMap.c
浏览文件 @
f30c1c65
...
...
@@ -510,6 +510,27 @@ OcUpdateDescriptors (
return
EFI_NOT_FOUND
;
}
EFI_MEMORY_ATTRIBUTES_TABLE
*
OcGetMemoryAttributes
(
OUT
EFI_MEMORY_DESCRIPTOR
**
MemoryAttributesEntry
OPTIONAL
)
{
EFI_MEMORY_ATTRIBUTES_TABLE
*
MemoryAttributesTable
;
UINTN
Index
;
for
(
Index
=
0
;
Index
<
gST
->
NumberOfTableEntries
;
++
Index
)
{
if
(
CompareGuid
(
&
gST
->
ConfigurationTable
[
Index
].
VendorGuid
,
&
gEfiMemoryAttributesTableGuid
))
{
MemoryAttributesTable
=
gST
->
ConfigurationTable
[
Index
].
VendorTable
;
if
(
MemoryAttributesEntry
!=
NULL
)
{
*
MemoryAttributesEntry
=
(
EFI_MEMORY_DESCRIPTOR
*
)
(
MemoryAttributesTable
+
1
);
}
return
MemoryAttributesTable
;
}
}
return
NULL
;
}
EFI_STATUS
OcUpdateAttributes
(
IN
EFI_PHYSICAL_ADDRESS
Address
,
...
...
@@ -518,27 +539,23 @@ OcUpdateAttributes (
IN
UINT64
DropAttributes
)
{
UINTN
Index
;
CONST
EFI_MEMORY_ATTRIBUTES_TABLE
*
MemoryAttributesTable
;
EFI_MEMORY_DESCRIPTOR
*
MemoryAttributesEntry
;
for
(
Index
=
0
;
Index
<
gST
->
NumberOfTableEntries
;
++
Index
)
{
if
(
CompareGuid
(
&
gST
->
ConfigurationTable
[
Index
].
VendorGuid
,
&
gEfiMemoryAttributesTableGuid
))
{
MemoryAttributesTable
=
(
CONST
EFI_MEMORY_ATTRIBUTES_TABLE
*
)
gST
->
ConfigurationTable
[
Index
].
VendorTable
;
MemoryAttributesEntry
=
(
EFI_MEMORY_DESCRIPTOR
*
)
(
MemoryAttributesTable
+
1
);
return
OcUpdateDescriptors
(
MemoryAttributesTable
->
NumberOfEntries
*
MemoryAttributesTable
->
DescriptorSize
,
MemoryAttributesEntry
,
MemoryAttributesTable
->
DescriptorSize
,
Address
,
Type
,
SetAttributes
,
DropAttributes
);
}
MemoryAttributesTable
=
OcGetMemoryAttributes
(
&
MemoryAttributesEntry
);
if
(
MemoryAttributesTable
==
NULL
)
{
return
EFI_UNSUPPORTED
;
}
return
EFI_UNSUPPORTED
;
return
OcUpdateDescriptors
(
MemoryAttributesTable
->
NumberOfEntries
*
MemoryAttributesTable
->
DescriptorSize
,
MemoryAttributesEntry
,
MemoryAttributesTable
->
DescriptorSize
,
Address
,
Type
,
SetAttributes
,
DropAttributes
);
}
UINTN
...
...
@@ -551,16 +568,222 @@ OcCountSplitDescritptors (
CONST
EFI_MEMORY_ATTRIBUTES_TABLE
*
MemoryAttributesTable
;
EFI_MEMORY_DESCRIPTOR
*
MemoryAttributesEntry
;
MemoryAttributesTable
=
OcGetMemoryAttributes
(
&
MemoryAttributesEntry
);
if
(
MemoryAttributesTable
==
NULL
)
{
return
0
;
}
DescriptorCount
=
0
;
for
(
Index
=
0
;
Index
<
MemoryAttributesTable
->
NumberOfEntries
;
++
Index
)
{
if
(
MemoryAttributesEntry
->
Type
==
EfiRuntimeServicesCode
||
MemoryAttributesEntry
->
Type
==
EfiRuntimeServicesData
)
{
++
DescriptorCount
;
}
for
(
Index
=
0
;
Index
<
gST
->
NumberOfTableEntries
;
++
Index
)
{
if
(
CompareGuid
(
&
gST
->
ConfigurationTable
[
Index
].
VendorGuid
,
&
gEfiMemoryAttributesTableGuid
))
{
MemoryAttributesTable
=
(
CONST
EFI_MEMORY_ATTRIBUTES_TABLE
*
)
gST
->
ConfigurationTable
[
Index
].
VendorTable
;
MemoryAttributesEntry
=
(
EFI_MEMORY_DESCRIPTOR
*
)
(
MemoryAttributesTable
+
1
);
for
(
Index
=
0
;
Index
<
MemoryAttributesTable
->
NumberOfEntries
;
++
Index
)
{
MemoryAttributesEntry
=
NEXT_MEMORY_DESCRIPTOR
(
MemoryAttributesEntry
,
MemoryAttributesTable
->
DescriptorSize
);
}
return
DescriptorCount
;
}
/**
Split memory map descriptor by attribute.
@param[in,out] RetMemoryMapEntry Pointer to descriptor in the memory map, updated to next proccessed.
@param[in,out] CurrentEntryIndex Current index of the descriptor in the memory map, updated on increase.
@param[in,out] CurrentEntryCount Number of descriptors in the memory map, updated on increase.
@param[in] TotalEntryCount Max number of descriptors in the memory map.
@param[in] MemoryAttribute Memory attribute used for splitting.
@param[in] DescriptorSize Memory map descriptor size.
@retval EFI_SUCCESS on success.
@retval EFI_OUT_OF_RESOURCES when there are not enough free descriptor slots.
**/
STATIC
EFI_STATUS
OcSplitMemoryEntryByAttribute
(
IN
OUT
EFI_MEMORY_DESCRIPTOR
**
RetMemoryMapEntry
,
IN
OUT
UINTN
*
CurrentEntryIndex
,
IN
OUT
UINTN
*
CurrentEntryCount
,
IN
UINTN
TotalEntryCount
,
IN
EFI_MEMORY_DESCRIPTOR
*
MemoryAttribte
,
IN
UINTN
DescriptorSize
)
{
EFI_MEMORY_DESCRIPTOR
*
MemoryMapEntry
;
EFI_MEMORY_DESCRIPTOR
*
NewMemoryMapEntry
;
UINTN
DiffPages
;
MemoryMapEntry
=
*
RetMemoryMapEntry
;
//
// Memory attribute starts after our descriptor.
// Shorten the existing descriptor and insert the new one after it.
// [DESC1] -> [DESC1][DESC2]
//
if
(
MemoryAttribte
->
PhysicalStart
>
MemoryMapEntry
->
PhysicalStart
)
{
if
(
*
CurrentEntryCount
==
TotalEntryCount
)
{
return
EFI_OUT_OF_RESOURCES
;
}
NewMemoryMapEntry
=
NEXT_MEMORY_DESCRIPTOR
(
MemoryMapEntry
,
DescriptorSize
);
DiffPages
=
EFI_SIZE_TO_PAGES
(
MemoryAttribte
->
PhysicalStart
-
MemoryMapEntry
->
PhysicalStart
);
CopyMem
(
NewMemoryMapEntry
,
MemoryMapEntry
,
DescriptorSize
*
(
*
CurrentEntryCount
-
*
CurrentEntryIndex
)
);
MemoryMapEntry
->
NumberOfPages
=
DiffPages
;
NewMemoryMapEntry
->
PhysicalStart
=
MemoryAttribte
->
PhysicalStart
;
NewMemoryMapEntry
->
NumberOfPages
-=
DiffPages
;
MemoryMapEntry
=
NewMemoryMapEntry
;
//
// Current processed entry is now the one we inserted.
//
++
(
*
CurrentEntryIndex
);
++
(
*
CurrentEntryCount
);
}
ASSERT
(
MemoryAttribte
->
PhysicalStart
==
MemoryMapEntry
->
PhysicalStart
);
//
// Memory attribute matches our descriptor.
// Simply update its protection.
// [DESC1] -> [DESC1*]
//
if
(
MemoryMapEntry
->
NumberOfPages
==
MemoryAttribte
->
NumberOfPages
)
{
MemoryMapEntry
->
Type
=
MemoryAttribte
->
Type
;
*
RetMemoryMapEntry
=
MemoryMapEntry
;
return
EFI_SUCCESS
;
}
//
// Memory attribute is shorter than our descriptor.
// Shorten current descriptor, update its type, and inseret the new one after it.
// [DESC1] -> [DESC1*][DESC2]
//
if
(
*
CurrentEntryCount
==
TotalEntryCount
)
{
return
EFI_OUT_OF_RESOURCES
;
}
NewMemoryMapEntry
=
NEXT_MEMORY_DESCRIPTOR
(
MemoryMapEntry
,
DescriptorSize
);
DiffPages
=
MemoryMapEntry
->
NumberOfPages
-
MemoryAttribte
->
NumberOfPages
;
CopyMem
(
NewMemoryMapEntry
,
MemoryMapEntry
,
DescriptorSize
*
(
*
CurrentEntryCount
-
*
CurrentEntryIndex
)
);
MemoryMapEntry
->
Type
=
MemoryAttribte
->
Type
;
MemoryMapEntry
->
NumberOfPages
=
DiffPages
;
NewMemoryMapEntry
->
PhysicalStart
+=
EFI_PAGES_TO_SIZE
(
DiffPages
);
NewMemoryMapEntry
->
NumberOfPages
-=
DiffPages
;
//
// Current processed entry is now the one we need to process.
//
++
(
*
CurrentEntryIndex
);
++
(
*
CurrentEntryCount
);
*
RetMemoryMapEntry
=
NewMemoryMapEntry
;
return
EFI_SUCCESS
;
}
EFI_STATUS
OcSplitMemoryMapByAttributes
(
IN
UINTN
OriginalMemoryMapSize
,
IN
OUT
UINTN
*
MemoryMapSize
,
IN
OUT
EFI_MEMORY_DESCRIPTOR
*
MemoryMap
,
IN
UINTN
DescriptorSize
)
{
EFI_STATUS
Status
;
CONST
EFI_MEMORY_ATTRIBUTES_TABLE
*
MemoryAttributesTable
;
EFI_MEMORY_DESCRIPTOR
*
MemoryAttributesEntry
;
EFI_MEMORY_DESCRIPTOR
*
MemoryMapEntry
;
EFI_MEMORY_DESCRIPTOR
*
LastAttributeEntry
;
UINTN
LastAttributeIndex
;
UINTN
Index
;
UINTN
Index2
;
UINTN
CurrentEntryCount
;
UINTN
TotalEntryCount
;
UINTN
AttributeCount
;
BOOLEAN
CanSplit
;
BOOLEAN
InDescAttrs
;
ASSERT
(
OriginalMemoryMapSize
>=
*
MemoryMapSize
);
MemoryAttributesTable
=
OcGetMemoryAttributes
(
&
MemoryAttributesEntry
);
if
(
MemoryAttributesTable
==
NULL
)
{
return
EFI_UNSUPPORTED
;
}
LastAttributeEntry
=
MemoryAttributesEntry
;
LastAttributeIndex
=
0
;
MemoryMapEntry
=
MemoryMap
;
CurrentEntryCount
=
*
MemoryMapSize
/
DescriptorSize
;
TotalEntryCount
=
OriginalMemoryMapSize
/
DescriptorSize
;
AttributeCount
=
MemoryAttributesTable
->
NumberOfEntries
;
//
// We assume that the memory map and attribute table are sorted.
// If this is not the case we can add a sort call here.
//
for
(
Index
=
0
;
Index
<
CurrentEntryCount
;
++
Index
)
{
//
// Split entry by as many attributes as possible.
//
CanSplit
=
TRUE
;
while
((
MemoryMapEntry
->
Type
==
EfiRuntimeServicesCode
||
MemoryMapEntry
->
Type
==
EfiRuntimeServicesData
)
&&
CanSplit
)
{
//
// Find corresponding memory attribute.
//
InDescAttrs
=
FALSE
;
MemoryAttributesEntry
=
LastAttributeEntry
;
for
(
Index2
=
LastAttributeIndex
;
Index2
<
AttributeCount
;
++
Index2
)
{
if
(
MemoryAttributesEntry
->
Type
==
EfiRuntimeServicesCode
||
MemoryAttributesEntry
->
Type
==
EfiRuntimeServicesData
)
{
++
DescriptorCount
;
//
// UEFI spec says attribute entries are fully within memory map entries.
// Find first one of a different type.
//
if
(
AREA_WITHIN_DESCRIPTOR
(
MemoryMapEntry
,
MemoryAttributesEntry
->
PhysicalStart
,
EFI_PAGES_TO_SIZE
(
MemoryAttributesEntry
->
NumberOfPages
)))
{
//
// We are within descriptor attribute sequence.
//
InDescAttrs
=
TRUE
;
//
// No need to process the attribute of the same type.
// FIXME: Do we need this to inspect attributes?
//
if
(
MemoryAttributesEntry
->
Type
!=
MemoryMapEntry
->
Type
)
{
//
// Start with the next attribute on the second iteration.
//
LastAttributeEntry
=
NEXT_MEMORY_DESCRIPTOR
(
MemoryAttributesEntry
,
MemoryAttributesTable
->
DescriptorSize
);
LastAttributeIndex
=
Index2
+
1
;
break
;
}
}
else
if
(
InDescAttrs
)
{
//
// Reached the end of descriptor attribute sequence, abort.
//
InDescAttrs
=
FALSE
;
break
;
}
}
MemoryAttributesEntry
=
NEXT_MEMORY_DESCRIPTOR
(
...
...
@@ -568,8 +791,37 @@ OcCountSplitDescritptors (
MemoryAttributesTable
->
DescriptorSize
);
}
if
(
Index2
<
AttributeCount
&&
InDescAttrs
)
{
//
// Split current memory map entry.
//
Status
=
OcSplitMemoryEntryByAttribute
(
&
MemoryMapEntry
,
&
Index
,
&
CurrentEntryCount
,
TotalEntryCount
,
MemoryAttributesEntry
,
DescriptorSize
);
if
(
EFI_ERROR
(
Status
))
{
*
MemoryMapSize
=
CurrentEntryCount
*
DescriptorSize
;
return
Status
;
}
}
else
{
//
// Did not find a suitable attribute or processed all the attributes.
//
CanSplit
=
FALSE
;
}
}
MemoryMapEntry
=
NEXT_MEMORY_DESCRIPTOR
(
MemoryMapEntry
,
DescriptorSize
);
}
return
DescriptorCount
;
*
MemoryMapSize
=
CurrentEntryCount
*
DescriptorSize
;
return
EFI_SUCCESS
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录