Skip to content

Commit 51d8998

Browse files
82marbagwipawel
authored andcommitted
Memory-Type Range Registers (MTRR) support
This change introduces support for MTRRs. PAT support has already been added. This is an older way to control access and cacheability of physical memory regions. Issue #180 Signed-off-by: Daniele Ahmed <[email protected]>
1 parent 8a788cf commit 51d8998

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

arch/x86/mtrr.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright © 2022 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include <mtrr.h>
27+
#include <pagetable.h>
28+
29+
mtrr_cap_t mtrr_read_cap(void) {
30+
mtrr_cap_t c;
31+
c.reg = rdmsr(MSR_MTRR_CAP);
32+
return c;
33+
}
34+
35+
mtrr_def_type_t mtrr_read_def_type(void) {
36+
mtrr_def_type_t t;
37+
t.reg = rdmsr(MSR_MTRR_DEF_TYPE);
38+
return t;
39+
}
40+
41+
bool mtrr_set_phys_base(mtrr_base_t base, uint8_t reg) {
42+
mtrr_cap_t cap = mtrr_read_cap();
43+
if (reg < cap.vcnt) {
44+
wrmsr(MSR_MTRR_PHYS_BASE(reg), base.reg);
45+
return true;
46+
}
47+
return false;
48+
}
49+
50+
bool mtrr_set_phys_mask(mtrr_mask_t mask, uint8_t reg) {
51+
mtrr_cap_t cap = mtrr_read_cap();
52+
if (reg < cap.vcnt) {
53+
wrmsr(MSR_MTRR_PHYS_MASK(reg), mask.reg);
54+
return true;
55+
}
56+
return false;
57+
}

include/arch/x86/processor.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@
9393
*/
9494
#define MSR_PAT 0x277
9595

96+
#define MSR_MTRR_FIX_64K_00000 0x250
97+
#define MSR_MTRR_FIX_16K_80000 0x258
98+
#define MSR_MTRR_FIX_16K_A0000 0x259
99+
#define MSR_MTRR_FIX_4K_C0000 0x268
100+
#define MSR_MTRR_FIX_4K_C8000 0x269
101+
#define MSR_MTRR_FIX_4K_D0000 0x26A
102+
#define MSR_MTRR_FIX_4K_D8000 0x26B
103+
#define MSR_MTRR_FIX_4K_E0000 0x26C
104+
#define MSR_MTRR_FIX_4K_E8000 0x26D
105+
#define MSR_MTRR_FIX_4K_F0000 0x26E
106+
#define MSR_MTRR_FIX_4K_F8000 0x26F
107+
108+
#define MSR_MTRR_CAP 0x2FF
109+
#define MSR_MTRR_DEF_TYPE 0x2FF
110+
111+
#define MSR_MTRR_PHYS_BASE(n) (0x200 + (n * 2))
112+
#define MSR_MTRR_PHYS_MASK(n) (0x201 + (n * 2))
113+
96114
#define MSR_APIC_BASE 0x0000001B
97115

98116
#define MSR_EFER 0xc0000080 /* Extended Feature Enable Register */

include/mtrr.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2022 Amazon.com, Inc. or its affiliates.
3+
* All Rights Reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
#ifndef KTF_MTRR_H
26+
#define KTF_MTRR_H
27+
28+
#include <ktf.h>
29+
30+
enum mtrr_memory_type {
31+
MTRR_UC = 0, /* Uncacheable */
32+
MTRR_WC = 1, /* Write Combining */
33+
MTRR_WT = 4, /* Write Through */
34+
MTRR_WP, /* Write Protected */
35+
MTRR_WB, /* Write Back */
36+
};
37+
typedef enum mtrr_memory_type mtrr_memory_type_t;
38+
39+
union mtrr_cap {
40+
struct {
41+
/* clang-format off */
42+
uint64_t vcnt : 8,
43+
fix : 1,
44+
_rsvd : 1,
45+
wc : 1,
46+
_rsvd2 : 53;
47+
/* clang-format on */
48+
} __packed;
49+
uint64_t reg;
50+
};
51+
typedef union mtrr_cap mtrr_cap_t;
52+
53+
union mtrr_def_type {
54+
struct {
55+
/* clang-format off */
56+
uint64_t type : 8,
57+
_rsvd : 2,
58+
fe : 1,
59+
e : 1,
60+
_rsvd2 : 52;
61+
/* clang-format on */
62+
} __packed;
63+
uint64_t reg;
64+
};
65+
typedef union mtrr_def_type mtrr_def_type_t;
66+
67+
union mtrr_base {
68+
struct {
69+
/* clang-format off */
70+
uint64_t type : 8,
71+
_rsvd : 4,
72+
phys_base : 40,
73+
_rsvd2 : 12;
74+
/* clang-format on */
75+
} __packed;
76+
uint64_t reg;
77+
};
78+
typedef union mtrr_base mtrr_base_t;
79+
80+
union mtrr_mask {
81+
struct {
82+
/* clang-format off */
83+
uint64_t _rsvd : 10,
84+
valid : 1,
85+
phys_base : 40,
86+
_rsvd2 : 13;
87+
/* clang-format on */
88+
} __packed;
89+
uint64_t reg;
90+
};
91+
typedef union mtrr_mask mtrr_mask_t;
92+
93+
extern mtrr_cap_t mtrr_read_cap(void);
94+
extern mtrr_def_type_t mtrr_read_def_type(void);
95+
extern bool mtrr_set_phys_base(mtrr_base_t base, uint8_t reg);
96+
extern bool mtrr_set_phys_mask(mtrr_mask_t mask, uint8_t reg);
97+
98+
#endif /* KTF_MTRR_H */

0 commit comments

Comments
 (0)