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 bits3:0) selects a periodic frequencyREFERENCE_HZ >> (RS - 1)forRS in 3..=15;RS < 3disables 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 (
0x01seconds,0x03minutes,0x05hours, and0x31day of month in this model) are compared with the guest-visible time at the new second boundary. A byte in0xC0..=0xFFmeans don’t care, as on the chip: the top two bits at11are 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 atREG_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:
| Index | Register | Model behaviour |
|---|---|---|
0x00 | seconds | computed |
0x01 | seconds alarm | writable RAM, compared (don’t-care bytes) |
0x02 | minutes | computed |
0x03 | minutes alarm | writable RAM, compared (don’t-care bytes) |
0x04 | hours | computed |
0x05 | hours alarm | writable RAM, compared (don’t-care bytes) |
0x06 | weekday (1 = Sunday .. 7 = Saturday) | computed |
0x07 | day of month | computed |
0x08 | month | computed |
0x09 | year (two digits) | computed |
0x0A | status A | divider/rate writable, UIP always 0 |
0x0B | status B | writable; enables, formats and SET |
0x0C | status C | event flags + computed IRQF; reading clears |
0x0D | status D | reads 0x80 (valid RAM and time) |
0x0E..=0x7F | CMOS RAM | writable, default 0 (except 0x31, 0x32) |
0x31 | day-of-month alarm (model extension) | writable RAM, compared (don’t-care bytes) |
0x32 | century | computed |
§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_cfor 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;
newreports 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_eventscall.
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
portbelongs to the modelled CMOS RTC (0x70or0x71).