iPXE
Macros | Functions | Variables
rtc_entropy.c File Reference

RTC-based entropy source. More...

#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>

Go to the source code of this file.

Macros

#define RTC_MAX_WAIT_MS   100
 Maximum time to wait for an RTC interrupt, in milliseconds. More...
 
#define RTC_CHECK_COUNT   3
 Number of RTC interrupts to check for. More...
 
#define rtc_flag   __use_text16 ( rtc_flag )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct entropy_source rtc_entropy __entropy_source (ENTROPY_NORMAL)
 RTC entropy source. More...
 
void rtc_isr (void)
 RTC interrupt handler. More...
 
volatile uint8_t __text16 (rtc_flag)
 Flag set by RTC interrupt handler. More...
 
static void rtc_hook_isr (void)
 Hook RTC interrupt handler. More...
 
static void rtc_unhook_isr (void)
 Unhook RTC interrupt handler. More...
 
static void rtc_enable_int (void)
 Enable RTC interrupts. More...
 
static void rtc_disable_int (void)
 Disable RTC interrupts. More...
 
static int rtc_entropy_check (void)
 Check that entropy gathering is functional. More...
 
static int rtc_entropy_enable (void)
 Enable entropy gathering. More...
 
static void rtc_entropy_disable (void)
 Disable entropy gathering. More...
 
static int rtc_get_noise (noise_sample_t *noise)
 Get noise sample. More...
 

Variables

static struct segoff rtc_old_handler
 Previous RTC interrupt handler. More...
 

Detailed Description

RTC-based entropy source.

Definition in file rtc_entropy.c.

Macro Definition Documentation

◆ RTC_MAX_WAIT_MS

#define RTC_MAX_WAIT_MS   100

Maximum time to wait for an RTC interrupt, in milliseconds.

Definition at line 45 of file rtc_entropy.c.

◆ RTC_CHECK_COUNT

#define RTC_CHECK_COUNT   3

Number of RTC interrupts to check for.

Definition at line 48 of file rtc_entropy.c.

◆ rtc_flag

#define rtc_flag   __use_text16 ( rtc_flag )

Definition at line 58 of file rtc_entropy.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __entropy_source()

struct entropy_source rtc_entropy __entropy_source ( ENTROPY_NORMAL  )

RTC entropy source.

◆ rtc_isr()

void rtc_isr ( void  )

RTC interrupt handler.

Referenced by rtc_hook_isr(), and rtc_unhook_isr().

◆ __text16()

volatile uint8_t __text16 ( rtc_flag  )

Flag set by RTC interrupt handler.

◆ rtc_hook_isr()

static void rtc_hook_isr ( void  )
static

Hook RTC interrupt handler.

Definition at line 64 of file rtc_entropy.c.

64  {
65 
66  /* RTC interrupt handler */
68  TEXT16_CODE ( "\nrtc_isr:\n\t"
69  /* Preserve registers */
70  "pushw %%ax\n\t"
71  /* Set "interrupt triggered" flag */
72  "movb $0x01, %%cs:rtc_flag\n\t"
73  /* Read RTC status register C to
74  * acknowledge interrupt
75  */
76  "movb %2, %%al\n\t"
77  "outb %%al, %0\n\t"
78  "inb %1\n\t"
79  /* Send EOI */
80  "movb $0x20, %%al\n\t"
81  "outb %%al, $0xa0\n\t"
82  "outb %%al, $0x20\n\t"
83  /* Restore registers and return */
84  "popw %%ax\n\t"
85  "iret\n\t"
86  "\nrtc_flag:\n\t"
87  ".byte 0\n\t" )
88  :
89  : "i" ( CMOS_ADDRESS ), "i" ( CMOS_DATA ),
90  "i" ( RTC_STATUS_C ) );
91 
93 }
unsigned long intptr_t
Definition: stdint.h:21
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition: biosint.c:24
void rtc_isr(void)
RTC interrupt handler.
static struct segoff rtc_old_handler
Previous RTC interrupt handler.
Definition: rtc_entropy.c:54
#define CMOS_DATA
CMOS/RTC data register.
Definition: rtc.h:33
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
#define RTC_INT
RTC interrupt vector.
Definition: rtc.h:24
__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")
#define RTC_STATUS_C
RTC status register C.
Definition: rtc.h:75
#define CMOS_ADDRESS
CMOS/RTC address (and NMI) register.
Definition: rtc.h:27
#define TEXT16_CODE(asm_code_str)
Definition: libkir.h:217

References __asm__(), __volatile__(), CMOS_ADDRESS, CMOS_DATA, hook_bios_interrupt(), RTC_INT, rtc_isr(), rtc_old_handler, RTC_STATUS_C, and TEXT16_CODE.

Referenced by rtc_entropy_enable().

◆ rtc_unhook_isr()

static void rtc_unhook_isr ( void  )
static

Unhook RTC interrupt handler.

Definition at line 99 of file rtc_entropy.c.

99  {
100  int rc;
101 
103  &rtc_old_handler );
104  assert ( rc == 0 ); /* Should always be able to unhook */
105 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long intptr_t
Definition: stdint.h:21
void rtc_isr(void)
RTC interrupt handler.
static struct segoff rtc_old_handler
Previous RTC interrupt handler.
Definition: rtc_entropy.c:54
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition: biosint.c:69
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define RTC_INT
RTC interrupt vector.
Definition: rtc.h:24

References assert(), rc, RTC_INT, rtc_isr(), rtc_old_handler, and unhook_bios_interrupt().

Referenced by rtc_entropy_disable(), and rtc_entropy_enable().

◆ rtc_enable_int()

static void rtc_enable_int ( void  )
static

Enable RTC interrupts.

Definition at line 111 of file rtc_entropy.c.

111  {
112  uint8_t status_b;
113 
114  /* Clear any stale pending interrupts via status register C */
116  inb ( CMOS_DATA );
117 
118  /* Set Periodic Interrupt Enable bit in status register B */
120  status_b = inb ( CMOS_DATA );
122  outb ( ( status_b | RTC_STATUS_B_PIE ), CMOS_DATA );
123 
124  /* Re-enable NMI and reset to default address */
126  inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
127 }
#define CMOS_DEFAULT_ADDRESS
CMOS default address.
Definition: rtc.h:81
#define RTC_STATUS_B
RTC status register B.
Definition: rtc.h:63
#define CMOS_DATA
CMOS/RTC data register.
Definition: rtc.h:33
unsigned char uint8_t
Definition: stdint.h:10
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
#define outb(data, io_addr)
Definition: io.h:309
#define RTC_STATUS_C
RTC status register C.
Definition: rtc.h:75
#define CMOS_ADDRESS
CMOS/RTC address (and NMI) register.
Definition: rtc.h:27
#define CMOS_DISABLE_NMI
NMI disable bit.
Definition: rtc.h:30
#define RTC_STATUS_B_PIE
RTC Periodic Interrupt Enabled bit.
Definition: rtc.h:72

References CMOS_ADDRESS, CMOS_DATA, CMOS_DEFAULT_ADDRESS, CMOS_DISABLE_NMI, inb(), outb, RTC_STATUS_B, RTC_STATUS_B_PIE, and RTC_STATUS_C.

Referenced by rtc_entropy_enable().

◆ rtc_disable_int()

static void rtc_disable_int ( void  )
static

Disable RTC interrupts.

Definition at line 133 of file rtc_entropy.c.

133  {
134  uint8_t status_b;
135 
136  /* Clear Periodic Interrupt Enable bit in status register B */
138  status_b = inb ( CMOS_DATA );
140  outb ( ( status_b & ~RTC_STATUS_B_PIE ), CMOS_DATA );
141 
142  /* Re-enable NMI and reset to default address */
144  inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
145 }
#define CMOS_DEFAULT_ADDRESS
CMOS default address.
Definition: rtc.h:81
#define RTC_STATUS_B
RTC status register B.
Definition: rtc.h:63
#define CMOS_DATA
CMOS/RTC data register.
Definition: rtc.h:33
unsigned char uint8_t
Definition: stdint.h:10
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
#define outb(data, io_addr)
Definition: io.h:309
#define CMOS_ADDRESS
CMOS/RTC address (and NMI) register.
Definition: rtc.h:27
#define CMOS_DISABLE_NMI
NMI disable bit.
Definition: rtc.h:30
#define RTC_STATUS_B_PIE
RTC Periodic Interrupt Enabled bit.
Definition: rtc.h:72

References CMOS_ADDRESS, CMOS_DATA, CMOS_DEFAULT_ADDRESS, CMOS_DISABLE_NMI, inb(), outb, RTC_STATUS_B, and RTC_STATUS_B_PIE.

Referenced by rtc_entropy_disable(), and rtc_entropy_enable().

◆ rtc_entropy_check()

static int rtc_entropy_check ( void  )
static

Check that entropy gathering is functional.

Return values
rcReturn status code

Definition at line 152 of file rtc_entropy.c.

152  {
153  unsigned int count = 0;
154  unsigned int i;
155 
156  /* Check that RTC interrupts are working */
157  rtc_flag = 0;
158  for ( i = 0 ; i < RTC_MAX_WAIT_MS ; i++ ) {
159 
160  /* Allow interrupts to occur */
161  __asm__ __volatile__ ( "sti\n\t"
162  "nop\n\t"
163  "nop\n\t"
164  "cli\n\t" );
165 
166  /* Check for RTC interrupt flag */
167  if ( rtc_flag ) {
168  rtc_flag = 0;
169  if ( ++count >= RTC_CHECK_COUNT )
170  return 0;
171  }
172 
173  /* Delay */
174  mdelay ( 1 );
175  }
176 
177  DBGC ( &rtc_flag, "RTC timed out waiting for interrupt %d/%d\n",
178  ( count + 1 ), RTC_CHECK_COUNT );
179  return -ETIMEDOUT;
180 }
#define DBGC(...)
Definition: compiler.h:505
#define RTC_CHECK_COUNT
Number of RTC interrupts to check for.
Definition: rtc_entropy.c:48
#define rtc_flag
Definition: rtc_entropy.c:58
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
#define RTC_MAX_WAIT_MS
Maximum time to wait for an RTC interrupt, in milliseconds.
Definition: rtc_entropy.c:45
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
__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")
uint16_t count
Number of entries.
Definition: ena.h:22
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669

References __asm__(), __volatile__(), count, DBGC, ETIMEDOUT, mdelay(), RTC_CHECK_COUNT, rtc_flag, and RTC_MAX_WAIT_MS.

Referenced by rtc_entropy_enable().

◆ rtc_entropy_enable()

static int rtc_entropy_enable ( void  )
static

Enable entropy gathering.

Return values
rcReturn status code

Definition at line 187 of file rtc_entropy.c.

187  {
188  struct x86_features features;
189  int rc;
190 
191  /* Check that TSC is supported */
192  x86_features ( &features );
193  if ( ! ( features.intel.edx & CPUID_FEATURES_INTEL_EDX_TSC ) ) {
194  DBGC ( &rtc_flag, "RTC has no TSC\n" );
195  rc = -ENOTSUP;
196  goto err_no_tsc;
197  }
198 
199  /* Hook ISR and enable RTC interrupts */
200  rtc_hook_isr();
201  enable_irq ( RTC_IRQ );
202  rtc_enable_int();
203 
204  /* Check that RTC interrupts are working */
205  if ( ( rc = rtc_entropy_check() ) != 0 )
206  goto err_check;
207 
208  /* The min-entropy has been measured on several platforms
209  * using the entropy_sample test code. Modelling the samples
210  * as independent, and using a confidence level of 99.99%, the
211  * measurements were as follows:
212  *
213  * qemu-kvm : 7.38 bits
214  * VMware : 7.46 bits
215  * Physical hardware : 2.67 bits
216  *
217  * We choose the lowest of these (2.67 bits) and apply a 50%
218  * safety margin to allow for some potential non-independence
219  * of samples.
220  */
221  entropy_init ( &rtc_entropy, MIN_ENTROPY ( 1.3 ) );
222 
223  return 0;
224 
225  err_check:
226  rtc_disable_int();
227  disable_irq ( RTC_IRQ );
228  rtc_unhook_isr();
229  err_no_tsc:
230  return rc;
231 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define RTC_IRQ
RTC IRQ.
Definition: rtc.h:21
#define DBGC(...)
Definition: compiler.h:505
void x86_features(struct x86_features *features)
Get x86 CPU features.
Definition: cpuid.c:163
#define disable_irq(x)
Definition: pic8259.h:52
x86 CPU features
Definition: cpuid.h:23
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
static void rtc_enable_int(void)
Enable RTC interrupts.
Definition: rtc_entropy.c:111
#define rtc_flag
Definition: rtc_entropy.c:58
uint32_t features
Supported features.
Definition: ena.h:16
static void rtc_hook_isr(void)
Hook RTC interrupt handler.
Definition: rtc_entropy.c:64
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition: entropy.h:42
static int rtc_entropy_check(void)
Check that entropy gathering is functional.
Definition: rtc_entropy.c:152
#define enable_irq(x)
Definition: pic8259.h:51
static void rtc_disable_int(void)
Disable RTC interrupts.
Definition: rtc_entropy.c:133
static void rtc_unhook_isr(void)
Unhook RTC interrupt handler.
Definition: rtc_entropy.c:99
#define CPUID_FEATURES_INTEL_EDX_TSC
TSC is present.
Definition: cpuid.h:49
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition: entropy.h:489

References CPUID_FEATURES_INTEL_EDX_TSC, DBGC, disable_irq, enable_irq, ENOTSUP, entropy_init(), features, MIN_ENTROPY, rc, rtc_disable_int(), rtc_enable_int(), rtc_entropy_check(), rtc_flag, rtc_hook_isr(), RTC_IRQ, rtc_unhook_isr(), and x86_features().

◆ rtc_entropy_disable()

static void rtc_entropy_disable ( void  )
static

Disable entropy gathering.

Definition at line 237 of file rtc_entropy.c.

237  {
238 
239  /* Disable RTC interrupts and unhook ISR */
240  rtc_disable_int();
241  disable_irq ( RTC_IRQ );
242  rtc_unhook_isr();
243 }
#define RTC_IRQ
RTC IRQ.
Definition: rtc.h:21
#define disable_irq(x)
Definition: pic8259.h:52
static void rtc_disable_int(void)
Disable RTC interrupts.
Definition: rtc_entropy.c:133
static void rtc_unhook_isr(void)
Unhook RTC interrupt handler.
Definition: rtc_entropy.c:99

References disable_irq, rtc_disable_int(), RTC_IRQ, and rtc_unhook_isr().

◆ rtc_get_noise()

static int rtc_get_noise ( noise_sample_t noise)
static

Get noise sample.

Return values
noiseNoise sample
rcReturn status code

Definition at line 251 of file rtc_entropy.c.

251  {
253  uint32_t after;
254  uint32_t temp;
255 
257  REAL_CODE ( /* Enable interrupts */
258  "sti\n\t"
259  /* Wait for RTC interrupt */
260  "movb %b2, %%cs:rtc_flag\n\t"
261  "\n1:\n\t"
262  "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
263  "testb %b2, %b2\n\t"
264  "jz 1b\n\t"
265  /* Read "before" TSC */
266  "rdtsc\n\t"
267  /* Store "before" TSC on stack */
268  "pushl %0\n\t"
269  /* Wait for another RTC interrupt */
270  "xorb %b2, %b2\n\t"
271  "movb %b2, %%cs:rtc_flag\n\t"
272  "\n1:\n\t"
273  "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
274  "testb %b2, %b2\n\t"
275  "jz 1b\n\t"
276  /* Read "after" TSC */
277  "rdtsc\n\t"
278  /* Retrieve "before" TSC on stack */
279  "popl %1\n\t"
280  /* Disable interrupts */
281  "cli\n\t"
282  )
283  : "=a" ( after ), "=d" ( before ), "=Q" ( temp )
284  : "2" ( 0 ) );
285 
286  *noise = ( after - before );
287  return 0;
288 }
int32_t before
Initial microcode version.
Definition: ucode.h:16
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
unsigned int uint32_t
Definition: stdint.h:12
__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")
int32_t after
Final microcode version.
Definition: ucode.h:18
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226

References __asm__(), __volatile__(), after, before, and REAL_CODE.

Variable Documentation

◆ rtc_old_handler

struct segoff rtc_old_handler
static

Previous RTC interrupt handler.

Definition at line 54 of file rtc_entropy.c.

Referenced by rtc_hook_isr(), and rtc_unhook_isr().