Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PAT support #254

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions arch/x86/pat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © 2022 Amazon.com, Inc. or its affiliates.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <pagetable.h>

#define PAT_FIELD_BIT_POS(field) (8 * field)

void pat_set_type(pat_field_t field, pat_memory_type_t type) {
/* TODO: cpuid: check this feature is implemented */
unsigned long value = rdmsr(MSR_PAT);
value = value & ~(0x7 << PAT_FIELD_BIT_POS(field));
value = value | (type << PAT_FIELD_BIT_POS(field));
wrmsr(MSR_PAT, value);
}

pat_memory_type_t pat_get_type(pat_field_t field) {
/* TODO: cpuid: check this feature is implemented */
unsigned long value = rdmsr(MSR_PAT);
value = value >> PAT_FIELD_BIT_POS(field);
return value & 0x7;
}
25 changes: 25 additions & 0 deletions include/arch/x86/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@

#ifndef __ASSEMBLY__

enum pat_field {
PA0 = 0,
PA1,
PA2,
PA3,
PA4,
PA5,
PA6,
PA7,
};
typedef enum pat_field pat_field_t;

enum pat_memory_type {
UC = 0, /* Uncacheable */
WC = 1, /* Write Combining */
WT = 4, /* Write Through */
WP, /* Write Protected */
WB, /* Write Back */
UC_, /* Uncached */
};
typedef enum pat_memory_type pat_memory_type_t;

typedef unsigned long paddr_t;
typedef unsigned long mfn_t;

Expand All @@ -143,6 +165,9 @@ extern void *vmap(void *va, mfn_t mfn, unsigned int order,

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

extern void pat_set_type(pat_field_t field, pat_memory_type_t type);
extern pat_memory_type_t pat_get_type(pat_field_t field);

/* Static declarations */

static inline mfn_t paddr_to_mfn(paddr_t pa) { return (mfn_t)(pa >> PAGE_SHIFT); }
Expand Down
2 changes: 2 additions & 0 deletions include/arch/x86/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
/*
* Model Specific Registers (MSR)
*/
#define MSR_PAT 0x277

#define MSR_APIC_BASE 0x0000001B

#define MSR_EFER 0xc0000080 /* Extended Feature Enable Register */
Expand Down