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. |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 15 | */ |
| 16 | #ifndef CPU_X86_CR_H |
| 17 | #define CPU_X86_CR_H |
| 18 | |
| 19 | #if !defined(__ASSEMBLER__) |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <arch/cpu.h> |
| 23 | |
| 24 | /* ROMCC apparently chokes certain clobber registers. */ |
| 25 | #if defined(__ROMCC__) |
| 26 | #define COMPILER_BARRIER |
| 27 | #else |
| 28 | #define COMPILER_BARRIER "memory" |
| 29 | #endif |
| 30 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 31 | #ifdef __x86_64__ |
| 32 | #define CRx_TYPE uint64_t |
| 33 | #define CRx_IN "q" |
| 34 | #define CRx_RET "=q" |
| 35 | #else |
| 36 | #define CRx_TYPE uint32_t |
| 37 | #define CRx_IN "r" |
| 38 | #define CRx_RET "=r" |
| 39 | #endif |
| 40 | static alwaysinline CRx_TYPE read_cr0(void) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 41 | { |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 42 | CRx_TYPE value; |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 43 | __asm__ __volatile__ ( |
| 44 | "mov %%cr0, %0" |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 45 | : CRx_RET (value) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 46 | : |
| 47 | : COMPILER_BARRIER |
| 48 | ); |
| 49 | return value; |
| 50 | } |
| 51 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 52 | static alwaysinline void write_cr0(CRx_TYPE data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 53 | { |
| 54 | __asm__ __volatile__ ( |
| 55 | "mov %0, %%cr0" |
| 56 | : |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 57 | : CRx_IN (data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 58 | : COMPILER_BARRIER |
| 59 | ); |
| 60 | } |
| 61 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 62 | static alwaysinline CRx_TYPE read_cr4(void) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 63 | { |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 64 | CRx_TYPE value; |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 65 | __asm__ __volatile__ ( |
| 66 | "mov %%cr4, %0" |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 67 | : CRx_RET (value) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 68 | : |
| 69 | : COMPILER_BARRIER |
| 70 | ); |
| 71 | return value; |
| 72 | } |
| 73 | |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 74 | static alwaysinline void write_cr4(CRx_TYPE data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 75 | { |
| 76 | __asm__ __volatile__ ( |
| 77 | "mov %0, %%cr4" |
| 78 | : |
Stefan Reinauer | bef400b | 2015-06-17 16:11:18 -0700 | [diff] [blame] | 79 | : CRx_IN (data) |
Aaron Durbin | 029aaf6 | 2013-10-10 12:41:49 -0500 | [diff] [blame] | 80 | : COMPILER_BARRIER |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | #endif /* !defined(__ASSEMBLER__) */ |
| 85 | |
| 86 | /* CR0 flags */ |
| 87 | #define CR0_PE (1 << 0) |
| 88 | #define CR0_MP (1 << 1) |
| 89 | #define CR0_EM (1 << 2) |
| 90 | #define CR0_TS (1 << 3) |
| 91 | #define CR0_ET (1 << 4) |
| 92 | #define CR0_NE (1 << 5) |
| 93 | #define CR0_WP (1 << 16) |
| 94 | #define CR0_AM (1 << 18) |
| 95 | #define CR0_NW (1 << 29) |
| 96 | #define CR0_CD (1 << 30) |
| 97 | #define CR0_PG (1 << 31) |
| 98 | |
| 99 | /* CR4 flags */ |
| 100 | #define CR4_VME (1 << 0) |
| 101 | #define CR4_PVI (1 << 1) |
| 102 | #define CR4_TSD (1 << 2) |
| 103 | #define CR4_DE (1 << 3) |
| 104 | #define CR4_PSE (1 << 4) |
| 105 | #define CR4_PAE (1 << 5) |
| 106 | #define CR4_MCE (1 << 6) |
| 107 | #define CR4_PGE (1 << 7) |
| 108 | #define CR4_PCE (1 << 8) |
| 109 | #define CR4_OSFXSR (1 << 9) |
| 110 | #define CR4_OSXMMEXCPT (1 << 10) |
| 111 | #define CR4_VMXE (1 << 13) |
| 112 | #define CR4_SMXE (1 << 14) |
| 113 | #define CR4_FSGSBASE (1 << 16) |
| 114 | #define CR4_PCIDE (1 << 17) |
| 115 | #define CR4_OSXSAVE (1 << 18) |
| 116 | #define CR4_SMEP (1 << 20) |
| 117 | |
| 118 | #endif /* CPU_X86_CR_H */ |