Skip to main content

Module rtc

Module rtc 

Source
Expand description

s9vm : MC146818-compatible CMOS real-time clock model for guests.

A PC-compatible guest reads the wall clock through two I/O ports: the index port (0x70) selects one of 128 CMOS registers (bit 7 gates the NMI), and the data port (0x71) reads or writes the selected register. This module models the MC146818 (and its CMOS RAM) as pure state: the kernel calls CmosRtc::write_index / CmosRtc::read_data / CmosRtc::write_data when it emulates those ports, and never touches hardware.

§Time is computed, never stored per field

Only a UNIX timestamp is kept. The calendar registers (sec, min, hour, weekday, day, month, year, century) are computed on read from that timestamp, so the clock cannot drift between the fields and never needs a periodic update: the kernel advances it explicitly with CmosRtc::advance (for instance with the time elapsed between two vCPU runs) or CmosRtc::advance_events when it also wants the interrupt events. CmosRtc::new starts at the UNIX epoch, 1970-01-01T00:00:00Z, which is a valid calendar date, and leap years are handled by the civil date conversion.

§Interrupt model

The chip’s three interrupt sources are modelled from a 32.768 kHz reference clock (REFERENCE_HZ), the divider input of status register A:

  • periodic: the rate-select field RS3:0 (status A bits 3:0) selects a periodic frequency REFERENCE_HZ >> (RS - 1) for RS in 3..=15; RS < 3 disables the periodic interrupt. The counter is a phase accumulator, so a slow poller does not lose phase: the events of a whole interval collapse into one observed event, but the next event keeps the exact rate.
  • update-ended: one event at every second boundary of the host clock.
  • alarm: the alarm bytes (0x01 seconds, 0x03 minutes, 0x05 hours, and 0x31 day of month in this model) are compared with the guest-visible time at the new second boundary. A byte in 0xC0..=0xFF means don’t care, as on the chip: the top two bits at 11 are never produced by BCD or 12-hour encoding, so the convention is unambiguous. The chip itself has no day-of-month alarm register, so the model keeps that byte in spare CMOS RAM at REG_DAY_ALARM; the weekday alarm byte is not compared.

Events set the status-C flags (PF, AF, UF) whether or not the matching enable bit in status B is set, as on the hardware; the enable bits (PIE, AIE, UIE) only gate the interrupt request. The summary bit IRQF (status C bit 7) is not stored: it is computed on read as the OR of the enabled event flags. Reading status C returns the flags and clears them, exactly once.

A single CmosRtc::advance_events call reports at most one event of each kind because RtcEvents uses booleans: a caller that advances by several periods still sees periodic = true once, and one status-C flag. This is the documented granularity; poll more often to observe each period.

Register semantics, following the chip:

IndexRegisterModel behaviour
0x00secondscomputed
0x01seconds alarmwritable RAM, compared (don’t-care bytes)
0x02minutescomputed
0x03minutes alarmwritable RAM, compared (don’t-care bytes)
0x04hourscomputed
0x05hours alarmwritable RAM, compared (don’t-care bytes)
0x06weekday (1 = Sunday .. 7 = Saturday)computed
0x07day of monthcomputed
0x08monthcomputed
0x09year (two digits)computed
0x0Astatus Adivider/rate writable, UIP always 0
0x0Bstatus Bwritable; enables, formats and SET
0x0Cstatus Cevent flags + computed IRQF; reading clears
0x0Dstatus Dreads 0x80 (valid RAM and time)
0x0E..=0x7FCMOS RAMwritable, default 0 (except 0x31, 0x32)
0x31day-of-month alarm (model extension)writable RAM, compared (don’t-care bytes)
0x32centurycomputed

§Invariants

  • UIP (status A, bit 7) always reads as zero: the model is instantaneous, a driver may read the fields in any order;
  • status C flags are generated by events (or by CmosRtc::raise_status_c for the caller) and cleared by the read that returns them; the guest can never set them by writing status C;
  • while status B SET is asserted, the computed time is frozen at the moment SET was written (the host may then program the clock), and clearing SET makes the computed registers follow the current timestamp again; events keep being generated from the host clock, and the alarm compares the frozen guest-visible time;
  • writes to the computed time registers are accepted (they land in CMOS RAM bytes) but do not change the clock: a driver must use an external time source, which is the documented behaviour of this model;
  • new reports a valid calendar in BCD, 24-hour mode, with every interrupt disabled.

This module is pure core code and is host-tested by s9vm-tests.

Re-exports§

use crate::rtc_math; 🔒

Modules§

status_a
Bits of status register A.
status_b
Bits of status register B.
status_c
Bits of status register C.
status_d
Bits of status register D.

Structs§

CmosRtc
Model of an MC146818-compatible CMOS RTC and its CMOS RAM.
RtcEvents
Events produced by one CmosRtc::advance_events call.

Constants§

ALARM_DONT_CARE_MASK
Top two bits of an alarm byte meaning “don’t care”.
DATA_PORT
I/O port reading or writing the selected CMOS register.
INDEX_PORT
I/O port selecting a CMOS register (and gating the NMI with bit 7).
IRQ_LINE
I/O APIC line wired to the RTC periodic interrupt.
NMI_PORT_MASK
Bit of the index port that disables the NMI when set.
REFERENCE_HZ
Frequency of the RTC reference clock (the divider input), in hertz.
REGISTER_COUNT
Number of CMOS registers addressable by the index port.
REG_CENTURY
Century register.
REG_DAY
Day-of-month register.
REG_DAY_ALARM
Day-of-month alarm register.
REG_HOURS
Hours register.
REG_HOURS_ALARM
Hours alarm register.
REG_MINUTES
Minutes register.
REG_MINUTES_ALARM
Minutes alarm register.
REG_MONTH
Month register.
REG_SECONDS
Seconds register.
REG_SECONDS_ALARM
Seconds alarm register.
REG_STATUS_A
Status register A.
REG_STATUS_B
Status register B.
REG_STATUS_C
Status register C.
REG_STATUS_D
Status register D.
REG_WEEKDAY
Weekday register (1 = Sunday .. 7 = Saturday).
REG_YEAR
Two-digit year register.

Functions§

alarm_byte_matches 🔒
Compares one alarm byte with the encoded current value.
alarm_dont_care
True when an alarm byte carries the “don’t care” convention.
handles_port
True when port belongs to the modelled CMOS RTC (0x70 or 0x71).