Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * This file is part of the coreboot project. |
| 4 | * |
| 5 | * Copyright (C) 2013 Google Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 18 | * Foundation, Inc. |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 19 | */ |
| 20 | #ifndef CPU_X86_CR_H |
| 21 | #define CPU_X86_CR_H |
| 22 | |
| 23 | #if !defined(__ASSEMBLER__) |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | #include <arch/cpu.h> |
| 27 | |
| 28 | /* ROMCC apparently chokes certain clobber registers. */ |
| 29 | #if defined(__ROMCC__) |
| 30 | #define COMPILER_BARRIER |
| 31 | #else |
| 32 | #define COMPILER_BARRIER "memory" |
| 33 | #endif |
| 34 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 35 | #ifdef __x86_64__ |
| 36 | #define CRx_TYPE uint64_t |
| 37 | #define CRx_IN "q" |
| 38 | #define CRx_RET "=q" |
| 39 | #else |
| 40 | #define CRx_TYPE uint32_t |
| 41 | #define CRx_IN "r" |
| 42 | #define CRx_RET "=r" |
| 43 | #endif |
| 44 | static alwaysinline CRx_TYPE read_cr0(void) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 45 | { |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 46 | CRx_TYPE value; |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 47 | __asm__ __volatile__ ( |
| 48 | "mov %%cr0, %0" |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 49 | : CRx_RET (value) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 50 | : |
| 51 | : COMPILER_BARRIER |
| 52 | ); |
| 53 | return value; |
| 54 | } |
| 55 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 56 | static alwaysinline void write_cr0(CRx_TYPE data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 57 | { |
| 58 | __asm__ __volatile__ ( |
| 59 | "mov %0, %%cr0" |
| 60 | : |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 61 | : CRx_IN (data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 62 | : COMPILER_BARRIER |
| 63 | ); |
| 64 | } |
| 65 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 66 | static alwaysinline CRx_TYPE read_cr4(void) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 67 | { |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 68 | CRx_TYPE value; |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 69 | __asm__ __volatile__ ( |
| 70 | "mov %%cr4, %0" |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 71 | : CRx_RET (value) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 72 | : |
| 73 | : COMPILER_BARRIER |
| 74 | ); |
| 75 | return value; |
| 76 | } |
| 77 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 78 | static alwaysinline void write_cr4(CRx_TYPE data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 79 | { |
| 80 | __asm__ __volatile__ ( |
| 81 | "mov %0, %%cr4" |
| 82 | : |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame^] | 83 | : CRx_IN (data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 84 | : COMPILER_BARRIER |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | #endif /* !defined(__ASSEMBLER__) */ |
| 89 | |
| 90 | /* CR0 flags */ |
| 91 | #define CR0_PE (1 << 0) |
| 92 | #define CR0_MP (1 << 1) |
| 93 | #define CR0_EM (1 << 2) |
| 94 | #define CR0_TS (1 << 3) |
| 95 | #define CR0_ET (1 << 4) |
| 96 | #define CR0_NE (1 << 5) |
| 97 | #define CR0_WP (1 << 16) |
| 98 | #define CR0_AM (1 << 18) |
| 99 | #define CR0_NW (1 << 29) |
| 100 | #define CR0_CD (1 << 30) |
| 101 | #define CR0_PG (1 << 31) |
| 102 | |
| 103 | /* CR4 flags */ |
| 104 | #define CR4_VME (1 << 0) |
| 105 | #define CR4_PVI (1 << 1) |
| 106 | #define CR4_TSD (1 << 2) |
| 107 | #define CR4_DE (1 << 3) |
| 108 | #define CR4_PSE (1 << 4) |
| 109 | #define CR4_PAE (1 << 5) |
| 110 | #define CR4_MCE (1 << 6) |
| 111 | #define CR4_PGE (1 << 7) |
| 112 | #define CR4_PCE (1 << 8) |
| 113 | #define CR4_OSFXSR (1 << 9) |
| 114 | #define CR4_OSXMMEXCPT (1 << 10) |
| 115 | #define CR4_VMXE (1 << 13) |
| 116 | #define CR4_SMXE (1 << 14) |
| 117 | #define CR4_FSGSBASE (1 << 16) |
| 118 | #define CR4_PCIDE (1 << 17) |
| 119 | #define CR4_OSXSAVE (1 << 18) |
| 120 | #define CR4_SMEP (1 << 20) |
| 121 | |
| 122 | #endif /* CPU_X86_CR_H */ |