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/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_flag   __use_text16 ( rtc_flag )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
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...
 
uint8_t rtc_sample (void)
 Measure a single RTC tick. More...
 
 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)
 

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 42 of file rtc_entropy.c.

◆ rtc_flag

#define rtc_flag   __use_text16 ( rtc_flag )

Definition at line 52 of file rtc_entropy.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 58 of file rtc_entropy.c.

58  {
59 
60  /* RTC interrupt handler */
62  TEXT16_CODE ( "\nrtc_isr:\n\t"
63  /* Preserve registers */
64  "pushw %%ax\n\t"
65  /* Set "interrupt triggered" flag */
66  "movb $0x01, %%cs:rtc_flag\n\t"
67  /* Read RTC status register C to
68  * acknowledge interrupt
69  */
70  "movb %2, %%al\n\t"
71  "outb %%al, %0\n\t"
72  "inb %1\n\t"
73  /* Send EOI */
74  "movb $0x20, %%al\n\t"
75  "outb %%al, $0xa0\n\t"
76  "outb %%al, $0x20\n\t"
77  /* Restore registers and return */
78  "popw %%ax\n\t"
79  "iret\n\t"
80  "\nrtc_flag:\n\t"
81  ".byte 0\n\t" )
82  :
83  : "i" ( CMOS_ADDRESS ), "i" ( CMOS_DATA ),
84  "i" ( RTC_STATUS_C ) );
85 
87 }
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:48
#define CMOS_DATA
CMOS/RTC data register.
Definition: rtc.h:33
#define RTC_INT
RTC interrupt vector.
Definition: rtc.h:24
__asm__ __volatile__("\n1:\n\t" "movb -1(%2,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %3, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
__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 93 of file rtc_entropy.c.

93  {
94  int rc;
95 
97  &rtc_old_handler );
98  assert ( rc == 0 ); /* Should always be able to unhook */
99 }
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:48
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 105 of file rtc_entropy.c.

105  {
106  uint8_t status_b;
107 
108  /* Clear any stale pending interrupts via status register C */
110  inb ( CMOS_DATA );
111 
112  /* Set Periodic Interrupt Enable bit in status register B */
114  status_b = inb ( CMOS_DATA );
116  outb ( ( status_b | RTC_STATUS_B_PIE ), CMOS_DATA );
117 
118  /* Re-enable NMI and reset to default address */
120  inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
121 }
#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 127 of file rtc_entropy.c.

127  {
128  uint8_t status_b;
129 
130  /* Clear Periodic Interrupt Enable bit in status register B */
132  status_b = inb ( CMOS_DATA );
134  outb ( ( status_b & ~RTC_STATUS_B_PIE ), CMOS_DATA );
135 
136  /* Re-enable NMI and reset to default address */
138  inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
139 }
#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 146 of file rtc_entropy.c.

146  {
147  unsigned int i;
148 
149  /* Check that RTC interrupts are working */
150  rtc_flag = 0;
151  for ( i = 0 ; i < RTC_MAX_WAIT_MS ; i++ ) {
152 
153  /* Allow interrupts to occur */
154  __asm__ __volatile__ ( "sti\n\t"
155  "nop\n\t"
156  "nop\n\t"
157  "cli\n\t" );
158 
159  /* Check for RTC interrupt flag */
160  if ( rtc_flag )
161  return 0;
162 
163  /* Delay */
164  mdelay ( 1 );
165  }
166 
167  DBGC ( &rtc_flag, "RTC timed out waiting for interrupt\n" );
168  return -ETIMEDOUT;
169 }
#define DBGC(...)
Definition: compiler.h:505
#define rtc_flag
Definition: rtc_entropy.c:52
__asm__ __volatile__("\n1:\n\t" "movb -1(%2,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %3, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c) :"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:42
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")
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669

References __asm__(), __volatile__(), DBGC, ETIMEDOUT, mdelay(), 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 176 of file rtc_entropy.c.

176  {
177  int rc;
178 
179  /* Hook ISR and enable RTC interrupts */
180  rtc_hook_isr();
181  enable_irq ( RTC_IRQ );
182  rtc_enable_int();
183 
184  /* Check that RTC interrupts are working */
185  if ( ( rc = rtc_entropy_check() ) != 0 )
186  goto err_check;
187 
188  return 0;
189 
190  err_check:
191  rtc_disable_int();
192  disable_irq ( RTC_IRQ );
193  rtc_unhook_isr();
194  return rc;
195 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define RTC_IRQ
RTC IRQ.
Definition: rtc.h:21
#define disable_irq(x)
Definition: pic8259.h:52
static void rtc_enable_int(void)
Enable RTC interrupts.
Definition: rtc_entropy.c:105
static void rtc_hook_isr(void)
Hook RTC interrupt handler.
Definition: rtc_entropy.c:58
static int rtc_entropy_check(void)
Check that entropy gathering is functional.
Definition: rtc_entropy.c:146
#define enable_irq(x)
Definition: pic8259.h:51
static void rtc_disable_int(void)
Disable RTC interrupts.
Definition: rtc_entropy.c:127
static void rtc_unhook_isr(void)
Unhook RTC interrupt handler.
Definition: rtc_entropy.c:93

References disable_irq, enable_irq, rc, rtc_disable_int(), rtc_enable_int(), rtc_entropy_check(), rtc_hook_isr(), RTC_IRQ, and rtc_unhook_isr().

◆ rtc_entropy_disable()

static void rtc_entropy_disable ( void  )
static

Disable entropy gathering.

Definition at line 201 of file rtc_entropy.c.

201  {
202 
203  /* Disable RTC interrupts and unhook ISR */
204  rtc_disable_int();
205  disable_irq ( RTC_IRQ );
206  rtc_unhook_isr();
207 }
#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:127
static void rtc_unhook_isr(void)
Unhook RTC interrupt handler.
Definition: rtc_entropy.c:93

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

◆ rtc_sample()

uint8_t rtc_sample ( void  )

Measure a single RTC tick.

Return values
deltaLength of RTC tick (in TSC units)

Definition at line 214 of file rtc_entropy.c.

214  {
215  uint32_t before;
216  uint32_t after;
217  uint32_t temp;
218 
220  REAL_CODE ( /* Enable interrupts */
221  "sti\n\t"
222  /* Wait for RTC interrupt */
223  "movb %b2, %%cs:rtc_flag\n\t"
224  "\n1:\n\t"
225  "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
226  "testb %b2, %b2\n\t"
227  "jz 1b\n\t"
228  /* Read "before" TSC */
229  "rdtsc\n\t"
230  /* Store "before" TSC on stack */
231  "pushl %0\n\t"
232  /* Wait for another RTC interrupt */
233  "xorb %b2, %b2\n\t"
234  "movb %b2, %%cs:rtc_flag\n\t"
235  "\n1:\n\t"
236  "xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
237  "testb %b2, %b2\n\t"
238  "jz 1b\n\t"
239  /* Read "after" TSC */
240  "rdtsc\n\t"
241  /* Retrieve "before" TSC on stack */
242  "popl %1\n\t"
243  /* Disable interrupts */
244  "cli\n\t"
245  )
246  : "=a" ( after ), "=d" ( before ), "=Q" ( temp )
247  : "2" ( 0 ) );
248 
249  return ( after - before );
250 }
unsigned int uint32_t
Definition: stdint.h:12
__asm__ __volatile__("\n1:\n\t" "movb -1(%2,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %3, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
__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 REAL_CODE(asm_code_str)
Definition: libkir.h:226

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

Referenced by ENTROPY_INLINE().

◆ PROVIDE_ENTROPY_INLINE() [1/2]

PROVIDE_ENTROPY_INLINE ( rtc  ,
min_entropy_per_sample   
)

◆ PROVIDE_ENTROPY() [1/2]

PROVIDE_ENTROPY ( rtc  ,
entropy_enable  ,
rtc_entropy_enable   
)

◆ PROVIDE_ENTROPY() [2/2]

PROVIDE_ENTROPY ( rtc  ,
entropy_disable  ,
rtc_entropy_disable   
)

◆ PROVIDE_ENTROPY_INLINE() [2/2]

PROVIDE_ENTROPY_INLINE ( rtc  ,
get_noise   
)

Variable Documentation

◆ rtc_old_handler

struct segoff rtc_old_handler
static

Previous RTC interrupt handler.

Definition at line 48 of file rtc_entropy.c.

Referenced by rtc_hook_isr(), and rtc_unhook_isr().