aboutsummaryrefslogtreecommitdiff
path: root/src/arch/x86/interface/pcbios/rtc_entropy.c
blob: c9e5e461721eac5390265b331d0545a13f12a5dc (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
337
338
339
340
341
342
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * RTC-based entropy source
 *
 */

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <biosint.h>
#include <pic8259.h>
#include <rtc.h>
#include <ipxe/cpuid.h>
#include <ipxe/entropy.h>

/** Maximum time to wait for an RTC interrupt, in milliseconds */
#define RTC_MAX_WAIT_MS 100

/** Number of RTC interrupts to check for */
#define RTC_CHECK_COUNT 3

/** RTC interrupt handler */
extern void rtc_isr ( void );

/** Previous RTC interrupt handler */
static struct segoff rtc_old_handler;

/** Flag set by RTC interrupt handler */
extern volatile uint8_t __text16 ( rtc_flag );
#define rtc_flag __use_text16 ( rtc_flag )

/** RTC interrupt requires rearming each time */
extern volatile uint8_t __text16 ( rtc_rearm );
#define rtc_rearm __use_text16 ( rtc_rearm )

/**
 * Hook RTC interrupt handler
 *
 */
static void rtc_hook_isr ( void ) {

	/* RTC interrupt handler */
	__asm__ __volatile__ (
		TEXT16_CODE ( "\nrtc_isr:\n\t"
			      /* Preserve registers */
			      "pushw %%ax\n\t"
			      /* Set "interrupt triggered" flag */
			      "movb $0x01, %%cs:rtc_flag\n\t"
			      /* Read RTC status register C to
			       * acknowledge interrupt
			       */
			      "movb %2, %%al\n\t"
			      "outb %%al, %0\n\t"
			      "inb %1, %%al\n\t"

			      /* Rearm RTC interrupt, if required */
			      "testb $0xff, %%cs:rtc_rearm\n\t"
			      "jz rtc_isr_done\n\t"
			      /* Preserve registers */
			      "pushw %%bx\n\t"
			      /* Read current contents of register B */
			      "movb %3, %%al\n\t"
			      "outb %%al, %0\n\t"
			      "inb %1, %%al\n\t"
			      "movb %%al, %%bl\n\t"
			      /* Toggle periodic interrupt enable in register B */
			      "movb %3, %%al\n\t"
			      "outb %%al, %0\n\t"
			      "movb %%bl, %%al\n\t"
			      "xorb %4, %%al\n\t"
			      "outb %%al, %1\n\t"
			      /* Restore periodic interrupt enable in register B */
			      "movb %3, %%al\n\t"
			      "outb %%al, %0\n\t"
			      "movb %%bl, %%al\n\t"
			      "outb %%al, %1\n\t"
			      /* Restore registers */
			      "popw %%bx\n\t"

			      /* Send EOI */
			      "\nrtc_isr_done:\n\t"
			      "movb $0x20, %%al\n\t"
			      "outb %%al, $0xa0\n\t"
			      "outb %%al, $0x20\n\t"
			      /* Restore registers and return */
			      "popw %%ax\n\t"
			      "iret\n\t"

			      "\nrtc_flag:\n\t"
			      ".byte 0\n\t"

			      "\nrtc_rearm:\n\t"
			      ".byte 0\n\t" )
		:
		: "i" ( CMOS_ADDRESS ), "i" ( CMOS_DATA ), "i" ( RTC_STATUS_C ),
		  "i" ( RTC_STATUS_B ), "i" ( RTC_STATUS_B_PIE ) );

	hook_bios_interrupt ( RTC_INT, ( intptr_t ) rtc_isr, &rtc_old_handler );
}

/**
 * Unhook RTC interrupt handler
 *
 */
static void rtc_unhook_isr ( void ) {
	int rc;

	rc = unhook_bios_interrupt ( RTC_INT, ( intptr_t ) rtc_isr,
				     &rtc_old_handler );
	assert ( rc == 0 ); /* Should always be able to unhook */
}

/**
 * Enable RTC interrupts
 *
 */
static void rtc_enable_int ( void ) {
	uint8_t status_b;

	/* Clear any stale pending interrupts via status register C */
	outb ( ( RTC_STATUS_C | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
	inb ( CMOS_DATA );

	/* Set Periodic Interrupt Enable bit in status register B */
	outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
	status_b = inb ( CMOS_DATA );
	outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
	outb ( ( status_b | RTC_STATUS_B_PIE ), CMOS_DATA );

	/* Re-enable NMI and reset to default address */
	outb ( CMOS_DEFAULT_ADDRESS, CMOS_ADDRESS );
	inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
}

/**
 * Disable RTC interrupts
 *
 */
static void rtc_disable_int ( void ) {
	uint8_t status_b;

	/* Clear Periodic Interrupt Enable bit in status register B */
	outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
	status_b = inb ( CMOS_DATA );
	outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
	outb ( ( status_b & ~RTC_STATUS_B_PIE ), CMOS_DATA );

	/* Re-enable NMI and reset to default address */
	outb ( CMOS_DEFAULT_ADDRESS, CMOS_ADDRESS );
	inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
}

/**
 * Check that entropy gathering is functional
 *
 * @ret rc		Return status code
 */
static int rtc_entropy_check ( void ) {
	unsigned int count = 0;
	unsigned int i;

	/* Check that RTC interrupts are working */
	rtc_flag = 0;
	for ( i = 0 ; i < RTC_MAX_WAIT_MS ; i++ ) {

		/* Allow interrupts to occur */
		__asm__ __volatile__ ( "sti\n\t"
				       "nop\n\t"
				       "nop\n\t"
				       "cli\n\t" );

		/* Check for RTC interrupt flag */
		if ( rtc_flag ) {
			rtc_flag = 0;
			if ( ++count >= RTC_CHECK_COUNT )
				return 0;
		}

		/* Delay */
		mdelay ( 1 );
	}

	DBGC ( &rtc_flag, "RTC timed out waiting for interrupt %d/%d\n",
	       ( count + 1 ), RTC_CHECK_COUNT );
	return -ETIMEDOUT;
}

/**
 * Apply workaround for broken RTC interrupts
 *
 * @ret rc		Return status code
 *
 * Some versions of Hyper-V (observed with Windows Server 2022) fail
 * to properly emulate the RTC periodic interrupt.  The typical
 * symptom is that only a single interrupt will be generated:
 * subsequent interrupts will appear to be asserted by the virtual RTC
 * but will be ignored by the virtual PIC.
 *
 * Experiments show that this apparent hypervisor bug can be worked
 * around by disabling and re-enabling the periodic interrupt within
 * the interrupt handler.
 */
static int rtc_entropy_workaround ( void ) {
	int rc;

	/* Apply workaround */
	DBGC ( &rtc_flag, "RTC applying workaround for broken interrupts\n" );
	rtc_rearm = 1;

	/* Force one interrupt, to trigger the rearming code path */
	__asm__ __volatile__ ( "int %0" : : "i" ( RTC_INT ) );

	/* Check that RTC interrupts are now working */
	if ( ( rc = rtc_entropy_check() ) != 0 )
		return rc;

	return 0;
}

/**
 * Enable entropy gathering
 *
 * @ret rc		Return status code
 */
static int rtc_entropy_enable ( void ) {
	struct x86_features features;
	int rc;

	/* Check that TSC is supported */
	x86_features ( &features );
	if ( ! ( features.intel.edx & CPUID_FEATURES_INTEL_EDX_TSC ) ) {
		DBGC ( &rtc_flag, "RTC has no TSC\n" );
		rc = -ENOTSUP;
		goto err_no_tsc;
	}

	/* Hook ISR and enable RTC interrupts */
	rtc_hook_isr();
	enable_irq ( RTC_IRQ );
	rtc_enable_int();

	/* Check that RTC interrupts are working */
	if ( ( ( rc = rtc_entropy_check() ) != 0 ) &&
	     ( ( rc = rtc_entropy_workaround() ) != 0 ) ) {
		goto err_check;
	}

	return 0;

 err_check:
	rtc_disable_int();
	disable_irq ( RTC_IRQ );
	rtc_unhook_isr();
 err_no_tsc:
	return rc;
}

/**
 * Disable entropy gathering
 *
 */
static void rtc_entropy_disable ( void ) {

	/* Disable RTC interrupts and unhook ISR */
	rtc_disable_int();
	disable_irq ( RTC_IRQ );
	rtc_unhook_isr();
}

/**
 * Measure a single RTC tick
 *
 * @ret delta		Length of RTC tick (in TSC units)
 */
uint8_t rtc_sample ( void ) {
	uint32_t before;
	uint32_t after;
	uint32_t temp;

	__asm__ __volatile__ (
		REAL_CODE ( /* Enable interrupts */
			    "sti\n\t"
			    /* Wait for RTC interrupt */
			    "movb %b2, %%cs:rtc_flag\n\t"
			    "\n1:\n\t"
			    "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
			    "testb %b2, %b2\n\t"
			    "jz 1b\n\t"
			    /* Read "before" TSC */
			    "rdtsc\n\t"
			    /* Store "before" TSC on stack */
			    "pushl %0\n\t"
			    /* Wait for another RTC interrupt */
			    "xorb %b2, %b2\n\t"
			    "movb %b2, %%cs:rtc_flag\n\t"
			    "\n1:\n\t"
			    "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
			    "testb %b2, %b2\n\t"
			    "jz 1b\n\t"
			    /* Read "after" TSC */
			    "rdtsc\n\t"
			    /* Retrieve "before" TSC on stack */
			    "popl %1\n\t"
			    /* Disable interrupts */
			    "cli\n\t"
			    )
		: "=a" ( after ), "=d" ( before ), "=Q" ( temp )
		: "2" ( 0 ) );

	return ( after - before );
}

PROVIDE_ENTROPY_INLINE ( rtc, min_entropy_per_sample );
PROVIDE_ENTROPY ( rtc, entropy_enable, rtc_entropy_enable );
PROVIDE_ENTROPY ( rtc, entropy_disable, rtc_entropy_disable );
PROVIDE_ENTROPY_INLINE ( rtc, get_noise );