| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <cpu/x86/msr.h> |
| 5 |
#include <cpu/x86/mtrr.h> |
| 6 |
#include <cpu/intel/microcode.h> |
| 7 |
#include <cpu/intel/common/common.h> |
| 8 |
#include <intelblocks/cpulib.h> |
| 9 |
#include <intelblocks/msr.h> |
| 10 |
#include <intelblocks/sgx.h> |
| 11 |
#include <intelblocks/systemagent.h> |
| 12 |
#include <soc/cpu.h> |
| 13 |
#include <soc/pci_devs.h> |
| 14 |
|
| 15 |
void prmrr_core_configure(void) |
| 16 |
{ |
| 17 |
msr_t prmrr_base, prmrr_mask; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
if (!is_sgx_supported() || intel_ht_sibling()) |
| 28 |
return; |
| 29 |
|
| 30 |
|
| 31 |
prmrr_mask = rdmsr(MSR_PRMRR_PHYS_MASK); |
| 32 |
|
| 33 |
if (prmrr_mask.lo & PRMRR_PHYS_MASK_LOCK) |
| 34 |
return; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
if (soc_get_uncore_prmmr_base_and_mask(&prmrr_base.raw, |
| 39 |
&prmrr_mask.raw) < 0) { |
| 40 |
printk(BIOS_ERR, "SGX: Failed to get PRMRR base and mask\n"); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (!prmrr_base.lo) { |
| 45 |
printk(BIOS_ERR, "SGX Error: Uncore PRMRR is not set!\n"); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
printk(BIOS_INFO, "SGX: prmrr_base = 0x%llx\n", prmrr_base.raw); |
| 50 |
printk(BIOS_INFO, "SGX: prmrr_mask = 0x%llx\n", prmrr_mask.raw); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
prmrr_base.lo |= MTRR_TYPE_WRBACK; |
| 57 |
wrmsr(MSR_PRMRR_PHYS_BASE, prmrr_base); |
| 58 |
prmrr_mask.lo &= ~PRMRR_PHYS_MASK_VALID; |
| 59 |
prmrr_mask.lo |= PRMRR_PHYS_MASK_LOCK; |
| 60 |
wrmsr(MSR_PRMRR_PHYS_MASK, prmrr_mask); |
| 61 |
} |
| 62 |
|
| 63 |
static int is_prmrr_set(void) |
| 64 |
{ |
| 65 |
msr_t prmrr_base, prmrr_mask; |
| 66 |
prmrr_base = rdmsr(MSR_PRMRR_PHYS_BASE); |
| 67 |
prmrr_mask = rdmsr(MSR_PRMRR_PHYS_MASK); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if ((prmrr_base.hi == 0) && (prmrr_base.lo == 0) |
| 72 |
&& (prmrr_mask.lo & PRMRR_PHYS_MASK_LOCK)) |
| 73 |
return 0; |
| 74 |
return 1; |
| 75 |
} |
| 76 |
|
| 77 |
static void enable_sgx(void) |
| 78 |
{ |
| 79 |
msr_t msr; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
msr = rdmsr(IA32_FEATURE_CONTROL); |
| 88 |
|
| 89 |
if ((msr.lo & FEATURE_CONTROL_LOCK_BIT) == 0) { |
| 90 |
msr.lo |= SGX_GLOBAL_ENABLE; |
| 91 |
wrmsr(IA32_FEATURE_CONTROL, msr); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static void lock_sgx(void) |
| 96 |
{ |
| 97 |
msr_t msr; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
msr = rdmsr(IA32_FEATURE_CONTROL); |
| 106 |
|
| 107 |
if ((msr.lo & 1) == 0) { |
| 108 |
msr.lo |= 1; |
| 109 |
wrmsr(IA32_FEATURE_CONTROL, msr); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static int owner_epoch_update(void) |
| 114 |
{ |
| 115 |
|
| 116 |
|
| 117 |
msr_t msr = { .raw = 0 }; |
| 118 |
|
| 119 |
|
| 120 |
wrmsr(MSR_SGX_OWNEREPOCH0, msr); |
| 121 |
wrmsr(MSR_SGX_OWNEREPOCH1, msr); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static void activate_sgx(void) |
| 126 |
{ |
| 127 |
msr_t msr; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
msr = rdmsr(MSR_BIOS_UPGD_TRIG); |
| 133 |
if (msr.lo & SGX_ACTIVATE_BIT) { |
| 134 |
wrmsr(MSR_BIOS_UPGD_TRIG, |
| 135 |
(msr_t) {.lo = SGX_ACTIVATE_BIT, .hi = 0}); |
| 136 |
|
| 137 |
msr = rdmsr(MSR_BIOS_UPGD_TRIG); |
| 138 |
if (msr.lo & SGX_ACTIVATE_BIT) |
| 139 |
printk(BIOS_ERR, "SGX activation failed.\n"); |
| 140 |
else |
| 141 |
printk(BIOS_INFO, "SGX activation was successful.\n"); |
| 142 |
} else { |
| 143 |
printk(BIOS_ERR, "SGX feature is deactivated.\n"); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static int is_prmrr_approved(void) |
| 148 |
{ |
| 149 |
msr_t msr; |
| 150 |
msr = rdmsr(MSR_PRMRR_PHYS_MASK); |
| 151 |
if (msr.lo & PRMRR_PHYS_MASK_VALID) { |
| 152 |
printk(BIOS_INFO, "SGX: MCHECK approved SGX PRMRR\n"); |
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
printk(BIOS_INFO, "SGX: MCHECK did not approve SGX PRMRR\n"); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void sgx_configure(void *unused) |
| 165 |
{ |
| 166 |
if (!is_sgx_supported() || !is_prmrr_set()) { |
| 167 |
printk(BIOS_ERR, "SGX: not supported or pre-conditions not met\n"); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
enable_sgx(); |
| 173 |
|
| 174 |
|
| 175 |
if (owner_epoch_update() < 0) |
| 176 |
return; |
| 177 |
|
| 178 |
|
| 179 |
if (CONFIG(SOC_INTEL_COMMON_BLOCK_SGX_LOCK_MEMORY)) |
| 180 |
cpu_lt_lock_memory(); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if (!intel_ht_sibling()) { |
| 197 |
const void *microcode_patch = intel_microcode_find(); |
| 198 |
intel_microcode_load_unlocked(microcode_patch); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
lock_sgx(); |
| 203 |
|
| 204 |
|
| 205 |
if (is_prmrr_approved()) |
| 206 |
activate_sgx(); |
| 207 |
} |