iPXE
rtc_entropy.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * RTC-based entropy source
29 *
30 */
31
32#include <stdint.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
36#include <biosint.h>
37#include <pic8259.h>
38#include <rtc.h>
39#include <ipxe/cpuid.h>
40#include <ipxe/entropy.h>
41
43
44/** Maximum time to wait for an RTC interrupt, in milliseconds */
45#define RTC_MAX_WAIT_MS 100
46
47/** Number of RTC interrupts to check for */
48#define RTC_CHECK_COUNT 3
49
50/** RTC interrupt handler */
51extern void rtc_isr ( void );
52
53/** Previous RTC interrupt handler */
54static struct segoff rtc_old_handler;
55
56/** Previous RTC interrupt enabled state */
58
59/** Previous RTC periodic interrupt enabled state */
61
62/** Flag set by RTC interrupt handler */
63extern volatile uint8_t __text16 ( rtc_flag );
64#define rtc_flag __use_text16 ( rtc_flag )
65
66/**
67 * Hook RTC interrupt handler
68 *
69 */
70static void rtc_hook_isr ( void ) {
71
72 /* RTC interrupt handler */
74 TEXT16_CODE ( "\nrtc_isr:\n\t"
75 /* Preserve registers */
76 "pushw %%ax\n\t"
77 /* Set "interrupt triggered" flag */
78 "movb $0x01, %%cs:rtc_flag\n\t"
79 /* Read RTC status register C to
80 * acknowledge interrupt
81 */
82 "movb %2, %%al\n\t"
83 "outb %%al, %0\n\t"
84 "inb %1\n\t"
85 /* Send EOI */
86 "movb $0x20, %%al\n\t"
87 "outb %%al, $0xa0\n\t"
88 "outb %%al, $0x20\n\t"
89 /* Restore registers and return */
90 "popw %%ax\n\t"
91 "iret\n\t"
92 "\nrtc_flag:\n\t"
93 ".byte 0\n\t" )
94 :
95 : "i" ( CMOS_ADDRESS ), "i" ( CMOS_DATA ),
96 "i" ( RTC_STATUS_C ) );
97
99}
100
101/**
102 * Unhook RTC interrupt handler
103 *
104 */
105static void rtc_unhook_isr ( void ) {
106 int rc;
107
110 assert ( rc == 0 ); /* Should always be able to unhook */
111}
112
113/**
114 * Enable RTC interrupts
115 *
116 * @ret enabled Periodic interrupt was previously enabled
117 */
118static int rtc_enable_int ( void ) {
119 uint8_t status_b;
120
121 /* Clear any stale pending interrupts via status register C */
123 inb ( CMOS_DATA );
124
125 /* Set Periodic Interrupt Enable bit in status register B */
127 status_b = inb ( CMOS_DATA );
129 outb ( ( status_b | RTC_STATUS_B_PIE ), CMOS_DATA );
130
131 /* Re-enable NMI and reset to default address */
133 inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
134
135 /* Return previous state */
136 return ( status_b & RTC_STATUS_B_PIE );
137}
138
139/**
140 * Disable RTC interrupts
141 *
142 */
143static void rtc_disable_int ( void ) {
144 uint8_t status_b;
145
146 /* Clear Periodic Interrupt Enable bit in status register B */
148 status_b = inb ( CMOS_DATA );
150 outb ( ( status_b & ~RTC_STATUS_B_PIE ), CMOS_DATA );
151
152 /* Re-enable NMI and reset to default address */
154 inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
155}
156
157/**
158 * Check that entropy gathering is functional
159 *
160 * @ret rc Return status code
161 */
162static int rtc_entropy_check ( void ) {
163 unsigned int count = 0;
164 unsigned int i;
165
166 /* Check that RTC interrupts are working */
167 rtc_flag = 0;
168 for ( i = 0 ; i < RTC_MAX_WAIT_MS ; i++ ) {
169
170 /* Allow interrupts to occur */
171 __asm__ __volatile__ ( "sti\n\t"
172 "nop\n\t"
173 "nop\n\t"
174 "cli\n\t" );
175
176 /* Check for RTC interrupt flag */
177 if ( rtc_flag ) {
178 rtc_flag = 0;
179 if ( ++count >= RTC_CHECK_COUNT )
180 return 0;
181 }
182
183 /* Delay */
184 mdelay ( 1 );
185 }
186
187 DBGC ( &rtc_flag, "RTC timed out waiting for interrupt %d/%d\n",
188 ( count + 1 ), RTC_CHECK_COUNT );
189 return -ETIMEDOUT;
190}
191
192/**
193 * Enable entropy gathering
194 *
195 * @ret rc Return status code
196 */
197static int rtc_entropy_enable ( void ) {
198 struct x86_features features;
199 int rc;
200
201 /* Check that TSC is supported */
203 if ( ! ( features.intel.edx & CPUID_FEATURES_INTEL_EDX_TSC ) ) {
204 DBGC ( &rtc_flag, "RTC has no TSC\n" );
205 rc = -ENOTSUP;
206 goto err_no_tsc;
207 }
208
209 /* Hook ISR and enable RTC interrupts */
210 rtc_hook_isr();
211 rtc_irq_enabled = enable_irq ( RTC_IRQ );
213 DBGC ( &rtc_flag, "RTC had IRQ%d %sabled, interrupt %sabled\n",
214 RTC_IRQ, ( rtc_irq_enabled ? "en" : "dis" ),
215 ( rtc_int_enabled ? "en" : "dis" ) );
216
217 /* Check that RTC interrupts are working */
218 if ( ( rc = rtc_entropy_check() ) != 0 )
219 goto err_check;
220
221 /* The min-entropy has been measured on several platforms
222 * using the entropy_sample test code. Modelling the samples
223 * as independent, and using a confidence level of 99.99%, the
224 * measurements were as follows:
225 *
226 * qemu-kvm : 7.38 bits
227 * VMware : 7.46 bits
228 * Physical hardware : 2.67 bits
229 *
230 * We choose the lowest of these (2.67 bits) and apply a 50%
231 * safety margin to allow for some potential non-independence
232 * of samples.
233 */
234 entropy_init ( &rtc_entropy, MIN_ENTROPY ( 1.3 ) );
235
236 return 0;
237
238 err_check:
239 if ( ! rtc_int_enabled )
241 if ( ! rtc_irq_enabled )
242 disable_irq ( RTC_IRQ );
244 err_no_tsc:
245 return rc;
246}
247
248/**
249 * Disable entropy gathering
250 *
251 */
252static void rtc_entropy_disable ( void ) {
253
254 /* Restore RTC interrupt state and unhook ISR */
255 if ( ! rtc_int_enabled )
257 if ( ! rtc_irq_enabled )
258 disable_irq ( RTC_IRQ );
260}
261
262/**
263 * Get noise sample
264 *
265 * @ret noise Noise sample
266 * @ret rc Return status code
267 */
268static int rtc_get_noise ( noise_sample_t *noise ) {
271 uint32_t temp;
272
274 REAL_CODE ( /* Enable interrupts */
275 "sti\n\t"
276 /* Wait for RTC interrupt */
277 "movb %b2, %%cs:rtc_flag\n\t"
278 "\n1:\n\t"
279 "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
280 "testb %b2, %b2\n\t"
281 "jz 1b\n\t"
282 /* Read "before" TSC */
283 "rdtsc\n\t"
284 /* Store "before" TSC on stack */
285 "pushl %0\n\t"
286 /* Wait for another RTC interrupt */
287 "xorb %b2, %b2\n\t"
288 "movb %b2, %%cs:rtc_flag\n\t"
289 "\n1:\n\t"
290 "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
291 "testb %b2, %b2\n\t"
292 "jz 1b\n\t"
293 /* Read "after" TSC */
294 "rdtsc\n\t"
295 /* Retrieve "before" TSC on stack */
296 "popl %1\n\t"
297 /* Disable interrupts */
298 "cli\n\t"
299 )
300 : "=a" ( after ), "=d" ( before ), "=Q" ( temp )
301 : "2" ( 0 ) );
302
303 *noise = ( after - before );
304 return 0;
305}
306
307/** RTC entropy source */
308struct entropy_source rtc_entropy __entropy_source ( ENTROPY_NORMAL ) = {
309 .name = "rtc",
310 .enable = rtc_entropy_enable,
311 .disable = rtc_entropy_disable,
312 .get_noise = rtc_get_noise,
313};
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
__asm__ __volatile__("call *%9" :"=a"(result), "=c"(discard_ecx), "=d"(discard_edx) :"d"(0), "a"(code), "b"(0), "c"(in_phys), "D"(0), "S"(out_phys), "m"(hypercall))
unsigned long intptr_t
Definition stdint.h:21
unsigned int uint32_t
Definition stdint.h:12
unsigned char uint8_t
Definition stdint.h:10
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition biosint.c:25
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition biosint.c:70
void x86_features(struct x86_features *features)
Get x86 CPU features.
Definition cpuid.c:164
x86 CPU feature detection
#define CPUID_FEATURES_INTEL_EDX_TSC
TSC is present.
Definition cpuid.h:53
uint32_t features
Supported features.
Definition ena.h:5
Error codes.
#define DBGC(...)
Definition compiler.h:505
#define ENTROPY_NORMAL
Normal entropy source.
Definition entropy.h:181
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ETIMEDOUT
Connection timed out.
Definition errno.h:670
#define ENOTSUP
Operation not supported.
Definition errno.h:590
Entropy source.
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition entropy.h:490
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition entropy.h:43
uint8_t noise_sample_t
A noise sample.
Definition entropy.h:22
#define __entropy_source(order)
Declare an entropy source.
Definition entropy.h:173
#define outb(data, io_addr)
Definition io.h:310
#define inb(io_addr)
Definition io.h:283
String functions.
#define REAL_CODE(asm_code_str)
Definition libkir.h:226
#define TEXT16_CODE(asm_code_str)
Definition libkir.h:217
#define __text16(variable)
Definition libkir.h:18
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
CMOS Real-Time Clock (RTC)
#define RTC_STATUS_C
RTC status register C.
Definition rtc.h:75
#define RTC_IRQ
RTC IRQ.
Definition rtc.h:21
#define CMOS_DATA
CMOS/RTC data register.
Definition rtc.h:33
#define CMOS_DISABLE_NMI
NMI disable bit.
Definition rtc.h:30
#define RTC_INT
RTC interrupt vector.
Definition rtc.h:24
#define RTC_STATUS_B
RTC status register B.
Definition rtc.h:63
#define CMOS_DEFAULT_ADDRESS
CMOS default address.
Definition rtc.h:81
#define CMOS_ADDRESS
CMOS/RTC address (and NMI) register.
Definition rtc.h:27
#define RTC_STATUS_B_PIE
RTC Periodic Interrupt Enabled bit.
Definition rtc.h:72
static int rtc_entropy_check(void)
Check that entropy gathering is functional.
#define RTC_CHECK_COUNT
Number of RTC interrupts to check for.
Definition rtc_entropy.c:48
static int rtc_entropy_enable(void)
Enable entropy gathering.
static void rtc_hook_isr(void)
Hook RTC interrupt handler.
Definition rtc_entropy.c:70
static struct segoff rtc_old_handler
Previous RTC interrupt handler.
Definition rtc_entropy.c:54
static void rtc_unhook_isr(void)
Unhook RTC interrupt handler.
#define RTC_MAX_WAIT_MS
Maximum time to wait for an RTC interrupt, in milliseconds.
Definition rtc_entropy.c:45
static uint8_t rtc_int_enabled
Previous RTC periodic interrupt enabled state.
Definition rtc_entropy.c:60
static void rtc_disable_int(void)
Disable RTC interrupts.
static void rtc_entropy_disable(void)
Disable entropy gathering.
static uint8_t rtc_irq_enabled
Previous RTC interrupt enabled state.
Definition rtc_entropy.c:57
void rtc_isr(void)
RTC interrupt handler.
static int rtc_get_noise(noise_sample_t *noise)
Get noise sample.
static int rtc_enable_int(void)
Enable RTC interrupts.
#define rtc_flag
Definition rtc_entropy.c:64
An entropy source.
Definition entropy.h:117
x86 CPU features
Definition cpuid.h:24
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79
int32_t after
Final microcode version.
Definition ucode.h:7
int32_t before
Initial microcode version.
Definition ucode.h:5