Skip to content

Commit 8c76cef

Browse files
committed
Add PAT support
Support Page Attribute Tables (PAT) Signed-off-by: Daniele Ahmed <ahmeddan@amazon.com>
1 parent 4abc254 commit 8c76cef

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

arch/x86/pat.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright © 2020 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 <pagetable.h>
27+
28+
#define PAT_FIELD_BIT_POS(field) (8 * field)
29+
30+
void pat_set(pat_field_t field, pat_memory_type_t type) {
31+
/* TODO: cpuid: check this feature is implemented */
32+
unsigned long value = rdmsr(MSR_PAT);
33+
value = value & ~(0x7 << PAT_FIELD_BIT_POS(field));
34+
value = value | (type << PAT_FIELD_BIT_POS(field));
35+
wrmsr(MSR_PAT, value);
36+
}

include/arch/x86/page.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@
123123

124124
#ifndef __ASSEMBLY__
125125

126+
enum pat_field {
127+
PA0 = 0,
128+
PA1,
129+
PA2,
130+
PA3,
131+
PA4,
132+
PA5,
133+
PA6,
134+
PA7,
135+
};
136+
typedef enum pat_field pat_field_t;
137+
138+
enum pat_memory_type {
139+
UC = 0, /* Uncacheable */
140+
WC = 1, /* Write Combining */
141+
WT = 4, /* Write Through */
142+
WP, /* Write Protected */
143+
WB, /* Write Back */
144+
UC_, /* Uncached */
145+
};
146+
typedef enum pat_memory_type pat_memory_type_t;
147+
126148
typedef unsigned long paddr_t;
127149
typedef unsigned long mfn_t;
128150

@@ -143,6 +165,8 @@ extern void *vmap(void *va, mfn_t mfn, unsigned int order,
143165

144166
extern void vunmap(void *va, unsigned int order);
145167

168+
extern void pat_set(pat_field_t field, pat_memory_type_t type);
169+
146170
/* Static declarations */
147171

148172
static inline mfn_t paddr_to_mfn(paddr_t pa) { return (mfn_t)(pa >> PAGE_SHIFT); }

include/arch/x86/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
/*
9292
* Model Specific Registers (MSR)
9393
*/
94+
#define MSR_PAT 0x277
95+
9496
#define MSR_APIC_BASE 0x0000001B
9597

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

0 commit comments

Comments
 (0)