summaryrefslogtreecommitdiffstats
path: root/src/security/intel/txt/getsec_enteraccs.S
blob: ff9db05f060879ad23518af20905e9416cf0cc64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* SPDX-License-Identifier: GPL-2.0-only */

#include <cpu/x86/mtrr.h>
#include <cpu/x86/cr.h>
#include <cpu/x86/msr.h>
#include <arch/ram_segs.h>

#include "getsec_mtrr_setup.inc"

.macro PUSH_MSR x
	movl	$(\x), %ecx
	rdmsr
	push	%eax
	push	%edx
.endm

.macro POP_MSR x
	movl	$(\x), %ecx
	pop	%edx
	pop	%eax
	wrmsr
.endm

.macro CLEAR_MSR x
	movl	$(\x), %ecx
	xorl	%edx, %edx
	xorl	%eax, %eax
	wrmsr
.endm

.align 4
.text

/*
 * See "SAFER MODE EXTENSIONS REFERENCE."
 * Chapter "GETSEC[ENTERACCS] - Execute Authenticated Chipset Code" for reference.
 * Intel 64 and IA-32 Architectures Software Developer Manuals Vol 2D
 *
 * void getsec_enteraccs(uint32_t esi,
 *                       uint32_t acm_base,
 *                       uint32_t acm_size);
 */
.global getsec_enteraccs
getsec_enteraccs:

	/* Backup current register state */
	pushl	%ebp
	movl	%esp, %ebp

	pushal

	movl	%cr0, %eax
	pushl	%eax
	movl	%cr4, %eax
	pushl	%eax

	/* Pushed 10 32bit registers */

	/* Reserve space on stack for GDT */
	subl	$8, %esp

	PUSH_MSR MTRR_DEF_TYPE_MSR
	PUSH_MSR IA32_MISC_ENABLE
	PUSH_MSR MTRR_FIX_64K_00000
	PUSH_MSR MTRR_FIX_16K_80000
	PUSH_MSR MTRR_FIX_16K_A0000
	PUSH_MSR MTRR_FIX_4K_C0000
	PUSH_MSR MTRR_FIX_4K_C8000
	PUSH_MSR MTRR_FIX_4K_D0000
	PUSH_MSR MTRR_FIX_4K_D8000
	PUSH_MSR MTRR_FIX_4K_E0000
	PUSH_MSR MTRR_FIX_4K_F0000
	PUSH_MSR MTRR_FIX_4K_F8000

	/* Push variable MTRRs in ascending order */

	xorl	%ebx, %ebx
	jmp	cond_push_var_mtrrs

body_push_var_mtrrs:

	movl	%ebx, %ecx
	shll	%ecx
	addl	$(MTRR_PHYS_BASE(0)), %ecx
	rdmsr
	push	%eax
	push	%edx
	incl	%ecx	/* MTRR_PHYS_MASK */
	rdmsr
	push	%eax
	push	%edx

	incl	%ebx

cond_push_var_mtrrs:

	movl	$(MTRR_CAP_MSR), %ecx
	rdmsr
	andl	$(0xff), %eax
	cmp	%ebx, %eax
	jg	body_push_var_mtrrs

	/*
	 * Disable cache.
	 * Chapter 2.2.4.3
	 * Intel TXT Software Development Guide (Document: 315168-015)
	 */
	movl	%cr0, %eax
	orl	$(CR0_CD | CR0_NW), %eax
	movl	%eax, %cr0

	/* Disable all MTRRs */
	movl	$(MTRR_DEF_TYPE_MSR), %ecx
	xorl	%eax, %eax
	xorl	%edx, %edx
	wrmsr

	/*
	 * Clear fixed MTRRs.
	 * Chapter 2.2.5.1
	 * Intel TXT Software Development Guide (Document: 315168-015)
	 */
	CLEAR_MSR MTRR_FIX_64K_00000
	CLEAR_MSR MTRR_FIX_16K_80000
	CLEAR_MSR MTRR_FIX_16K_A0000
	CLEAR_MSR MTRR_FIX_4K_C0000
	CLEAR_MSR MTRR_FIX_4K_C8000
	CLEAR_MSR MTRR_FIX_4K_D0000
	CLEAR_MSR MTRR_FIX_4K_D8000
	CLEAR_MSR MTRR_FIX_4K_E0000
	CLEAR_MSR MTRR_FIX_4K_F0000
	CLEAR_MSR MTRR_FIX_4K_F8000

	/*
	 * Clear variable MTRRs
	 * Chapter 2.2.5.1
	 * Intel TXT Software Development Guide (Document: 315168-015)
	 */
	movl	$(MTRR_CAP_MSR), %ecx
	rdmsr
	andl	$(0xff), %eax
	movl	%eax, %ebx

	xorl	%eax, %eax
	xorl	%edx, %edx

	jmp	cond_clear_var_mtrrs

body_clear_var_mtrrs:

	decl	%ebx
	movl	%ebx, %ecx
	shll	%ecx
	addl	$(MTRR_PHYS_BASE(0)), %ecx
	wrmsr
	incl	%ecx	/* MTRR_PHYS_MASK */
	wrmsr

cond_clear_var_mtrrs:

	cmpl	$0, %ebx
	jnz	body_clear_var_mtrrs

	/*
	 * Setup BIOS ACM as WB
	 * Chapter A.1.1
	 * Intel TXT Software Development Guide (Document: 315168-015)
	 */

	/* Determine size of AC module */
	movl	12(%ebp), %eax			/* %eax = acmbase */
	movl	$1, %ebx
	movl	16(%ebp), %ebx			/* %ebx = acmsize */

	/* Round up to page size */
	addl	$(0xfff), %ebx
	andl	$(~0xfff), %ebx			/* Aligned to a page (4 KiB) */

	/*
	 * Use XMM to store local variables. This code will need to be
	 * used in romstage, and CAR will have been torn down by then.
	 */
	movd	%eax, %xmm0			/* XMM0: Base address of next MTRR */
	movd	%ebx, %xmm1			/* XMM1: Remaining size to cache */

	/*
	 * Important note: The MTRRs must cache less than a page (4 KiB)
	 * of unused memory after the BIOS ACM. Failure to do so will
	 * result in a TXT reset with Class Code 5, Major Error Code 2.
	 *
	 * The caller must have checked that there are enough variable
	 * MTRRs to cache the ACM size prior to invoking this routine.
	 */
	SET_UP_MTRRS_FOR_BIOS_ACM

	/*
	 * Now that the variable MTRRs have been set up, enable them.
	 */
	movl	$(MTRR_DEF_TYPE_MSR), %ecx
	rdmsr
	orl	$(MTRR_DEF_TYPE_EN), %eax
	wrmsr

	/* Enable cache - GPF# if not done */
	movl	%cr0, %eax
	andl	$(~(CR0_CD | CR0_NW)), %eax
	movl	%eax, %cr0

	/* Enable Numeric error - GPE# if not done */
	movl	%cr0, %eax
	orl	$(CR0_NE), %eax
	movl	%eax, %cr0

	/* Enable SMX and FXSTORE - for getsec */
	movl	%cr4, %eax
	orl	$(CR4_SMXE | CR4_OSFXSR), %eax
	movl	%eax, %cr4

	/*
	 * Save GDT
	 * Chapter A.1.2
	 * Intel TXT Software Development Guide (Document: 315168-015)
	 */
	sgdt	-48(%ebp)

	/* Backup stack pointer */
	movd	%esp, %xmm0
	movd	%ebp, %xmm1

	/* Backup %gs used by cpu_info() */
	movl	%gs, %eax
	movd	%eax, %xmm2

	/*
	 * Get function arguments.
	 * It's important to pass the exact ACM size as it's used by getsec to verify
	 * the integrity of ACM. Unlike the size for MTRR programming, which needs to
	 * be power of two.
	 *
	 * The following assembly code is based on tboot's tboot/include/txt/smx.h.
	 */
	movl	8(%ebp), %esi	/* flags */
	movl	12(%ebp), %ebx	/* acm_base */
	movl	16(%ebp), %ecx	/* acm_size */

	movl	$0, %edx	/* reserved, must be zero */
	movl	$0, %edi	/* must be zero */
	movl	$2, %eax	/* GetSec[ENTERACCS] */

	getsec

	/* Restore stack pointer */
	movd	%xmm0, %esp
	movd	%xmm1, %ebp

	/* Reload GDT */
	lgdt	-48(%ebp)

	/* Set cs */
	ljmp	$RAM_CODE_SEG, $1f
1:
	/* Fix segment registers */
	movl	$RAM_DATA_SEG, %eax
	movl	%eax, %ds
	movl	%eax, %es
	movl	%eax, %ss
	movl	%eax, %fs
	/* Restore %gs used by cpu_info */
	movd	%xmm2, %eax
	movl	%eax, %gs

	/* Disable cache */
	movl	%cr0, %eax
	orl	$(CR0_CD | CR0_NW), %eax
	movl	%eax, %cr0

	/* Pop variable MTRRs in descending order */

	movl	$(MTRR_CAP_MSR), %ecx
	rdmsr
	andl	$(0xff), %eax
	movl	%eax, %ebx

	jmp	cond_pop_var_mtrrs

body_pop_var_mtrrs:

	decl	%ebx
	movl	%ebx, %ecx
	shll	%ecx
	addl	$(MTRR_PHYS_MASK(0)), %ecx
	pop	%edx
	pop	%eax
	wrmsr
	decl	%ecx	/* MTRR_PHYS_BASE */
	pop	%edx
	pop	%eax
	wrmsr

cond_pop_var_mtrrs:

	cmpl	$0, %ebx
	jne	body_pop_var_mtrrs

	POP_MSR MTRR_FIX_4K_F8000
	POP_MSR MTRR_FIX_4K_F0000
	POP_MSR MTRR_FIX_4K_E0000
	POP_MSR MTRR_FIX_4K_D8000
	POP_MSR MTRR_FIX_4K_D0000
	POP_MSR MTRR_FIX_4K_C8000
	POP_MSR MTRR_FIX_4K_C0000
	POP_MSR MTRR_FIX_16K_A0000
	POP_MSR MTRR_FIX_16K_80000
	POP_MSR MTRR_FIX_64K_00000
	POP_MSR IA32_MISC_ENABLE
	POP_MSR MTRR_DEF_TYPE_MSR

	/* Enable cache */
	movl	%cr0, %eax
	andl	$(~(CR0_CD | CR0_NW)), %eax
	movl	%eax, %cr0

	/* Pop GDT */
	addl	$8, %esp

	popl	%eax
	movl	%eax, %cr4
	popl	%eax
	movl	%eax, %cr0

	popal

	movl	%ebp, %esp
	popl	%ebp

	ret