2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/queue.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/clock.h>
40 #include <sys/sysctl.h>
42 #include <machine/vmm.h>
51 /* Register layout of the RTC */
69 uint8_t nvram2[128 - 51];
71 CTASSERT(sizeof(struct rtcdev) == 128);
72 CTASSERT(offsetof(struct rtcdev, century) == RTC_CENTURY);
77 struct callout callout;
78 u_int addr; /* RTC register to read or write */
79 sbintime_t base_uptime;
84 #define VRTC_LOCK(vrtc) mtx_lock(&((vrtc)->mtx))
85 #define VRTC_UNLOCK(vrtc) mtx_unlock(&((vrtc)->mtx))
86 #define VRTC_LOCKED(vrtc) mtx_owned(&((vrtc)->mtx))
89 * RTC time is considered "broken" if:
90 * - RTC updates are halted by the guest
91 * - RTC date/time fields have invalid values
93 #define VRTC_BROKEN_TIME ((time_t)-1)
96 #define RTCSB_BIN 0x04
97 #define RTCSB_ALL_INTRS (RTCSB_UINTR | RTCSB_AINTR | RTCSB_PINTR)
98 #define rtc_halted(vrtc) ((vrtc->rtcdev.reg_b & RTCSB_HALT) != 0)
99 #define aintr_enabled(vrtc) (((vrtc)->rtcdev.reg_b & RTCSB_AINTR) != 0)
100 #define pintr_enabled(vrtc) (((vrtc)->rtcdev.reg_b & RTCSB_PINTR) != 0)
101 #define uintr_enabled(vrtc) (((vrtc)->rtcdev.reg_b & RTCSB_UINTR) != 0)
103 static void vrtc_callout_handler(void *arg);
104 static void vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval);
106 static MALLOC_DEFINE(M_VRTC, "vrtc", "bhyve virtual rtc");
108 SYSCTL_DECL(_hw_vmm);
109 SYSCTL_NODE(_hw_vmm, OID_AUTO, vrtc, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
112 static int rtc_flag_broken_time = 1;
113 SYSCTL_INT(_hw_vmm_vrtc, OID_AUTO, flag_broken_time, CTLFLAG_RDTUN,
114 &rtc_flag_broken_time, 0, "Stop guest when invalid RTC time is detected");
117 divider_enabled(int reg_a)
120 * The RTC is counting only when dividers are not held in reset.
122 return ((reg_a & 0x70) == 0x20);
126 update_enabled(struct vrtc *vrtc)
129 * RTC date/time can be updated only if:
130 * - divider is not held in reset
131 * - guest has not disabled updates
132 * - the date/time fields have valid contents
134 if (!divider_enabled(vrtc->rtcdev.reg_a))
137 if (rtc_halted(vrtc))
140 if (vrtc->base_rtctime == VRTC_BROKEN_TIME)
147 vrtc_curtime(struct vrtc *vrtc, sbintime_t *basetime)
149 sbintime_t now, delta;
152 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
154 t = vrtc->base_rtctime;
155 *basetime = vrtc->base_uptime;
156 if (update_enabled(vrtc)) {
158 delta = now - vrtc->base_uptime;
159 KASSERT(delta >= 0, ("vrtc_curtime: uptime went backwards: "
160 "%#lx to %#lx", vrtc->base_uptime, now));
161 secs = delta / SBT_1S;
163 *basetime += secs * SBT_1S;
168 static __inline uint8_t
169 rtcset(struct rtcdev *rtc, int val)
172 KASSERT(val >= 0 && val < 100, ("%s: invalid bin2bcd index %d",
175 return ((rtc->reg_b & RTCSB_BIN) ? val : bin2bcd_data[val]);
179 secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update)
186 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
189 KASSERT(rtctime == VRTC_BROKEN_TIME,
190 ("%s: invalid vrtc time %#lx", __func__, rtctime));
195 * If the RTC is halted then the guest has "ownership" of the
196 * date/time fields. Don't update the RTC date/time fields in
197 * this case (unless forced).
199 if (rtc_halted(vrtc) && !force_update)
204 clock_ts_to_ct(&ts, &ct);
206 KASSERT(ct.sec >= 0 && ct.sec <= 59, ("invalid clocktime sec %d",
208 KASSERT(ct.min >= 0 && ct.min <= 59, ("invalid clocktime min %d",
210 KASSERT(ct.hour >= 0 && ct.hour <= 23, ("invalid clocktime hour %d",
212 KASSERT(ct.dow >= 0 && ct.dow <= 6, ("invalid clocktime wday %d",
214 KASSERT(ct.day >= 1 && ct.day <= 31, ("invalid clocktime mday %d",
216 KASSERT(ct.mon >= 1 && ct.mon <= 12, ("invalid clocktime month %d",
218 KASSERT(ct.year >= POSIX_BASE_YEAR, ("invalid clocktime year %d",
222 rtc->sec = rtcset(rtc, ct.sec);
223 rtc->min = rtcset(rtc, ct.min);
225 if (rtc->reg_b & RTCSB_24HR) {
229 * Convert to the 12-hour format.
238 * The remaining 'ct.hour' values are interpreted as:
239 * [1 - 11] -> 1 - 11 AM
240 * [13 - 23] -> 1 - 11 PM
247 rtc->hour = rtcset(rtc, hour);
249 if ((rtc->reg_b & RTCSB_24HR) == 0 && ct.hour >= 12)
250 rtc->hour |= 0x80; /* set MSB to indicate PM */
252 rtc->day_of_week = rtcset(rtc, ct.dow + 1);
253 rtc->day_of_month = rtcset(rtc, ct.day);
254 rtc->month = rtcset(rtc, ct.mon);
255 rtc->year = rtcset(rtc, ct.year % 100);
256 rtc->century = rtcset(rtc, ct.year / 100);
260 rtcget(struct rtcdev *rtc, int val, int *retval)
262 uint8_t upper, lower;
264 if (rtc->reg_b & RTCSB_BIN) {
270 upper = (val >> 4) & 0xf;
272 if (lower > 9 || upper > 9)
275 *retval = upper * 10 + lower;
280 rtc_to_secs(struct vrtc *vrtc)
286 int century, error, hour, pm, year;
288 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
293 bzero(&ct, sizeof(struct clocktime));
295 error = rtcget(rtc, rtc->sec, &ct.sec);
296 if (error || ct.sec < 0 || ct.sec > 59) {
297 VM_CTR2(vm, "Invalid RTC sec %#x/%d", rtc->sec, ct.sec);
301 error = rtcget(rtc, rtc->min, &ct.min);
302 if (error || ct.min < 0 || ct.min > 59) {
303 VM_CTR2(vm, "Invalid RTC min %#x/%d", rtc->min, ct.min);
309 if ((rtc->reg_b & RTCSB_24HR) == 0) {
315 error = rtcget(rtc, hour, &ct.hour);
316 if ((rtc->reg_b & RTCSB_24HR) == 0) {
317 if (ct.hour >= 1 && ct.hour <= 12) {
319 * Convert from 12-hour format to internal 24-hour
320 * representation as follows:
322 * 12-hour format ct.hour
333 VM_CTR2(vm, "Invalid RTC 12-hour format %#x/%d",
339 if (error || ct.hour < 0 || ct.hour > 23) {
340 VM_CTR2(vm, "Invalid RTC hour %#x/%d", rtc->hour, ct.hour);
345 * Ignore 'rtc->dow' because some guests like Linux don't bother
346 * setting it at all while others like OpenBSD/i386 set it incorrectly.
348 * clock_ct_to_ts() does not depend on 'ct.dow' anyways so ignore it.
352 error = rtcget(rtc, rtc->day_of_month, &ct.day);
353 if (error || ct.day < 1 || ct.day > 31) {
354 VM_CTR2(vm, "Invalid RTC mday %#x/%d", rtc->day_of_month,
359 error = rtcget(rtc, rtc->month, &ct.mon);
360 if (error || ct.mon < 1 || ct.mon > 12) {
361 VM_CTR2(vm, "Invalid RTC month %#x/%d", rtc->month, ct.mon);
365 error = rtcget(rtc, rtc->year, &year);
366 if (error || year < 0 || year > 99) {
367 VM_CTR2(vm, "Invalid RTC year %#x/%d", rtc->year, year);
371 error = rtcget(rtc, rtc->century, ¢ury);
372 ct.year = century * 100 + year;
373 if (error || ct.year < POSIX_BASE_YEAR) {
374 VM_CTR2(vm, "Invalid RTC century %#x/%d", rtc->century,
379 error = clock_ct_to_ts(&ct, &ts);
380 if (error || ts.tv_sec < 0) {
381 VM_CTR3(vm, "Invalid RTC clocktime.date %04d-%02d-%02d",
382 ct.year, ct.mon, ct.day);
383 VM_CTR3(vm, "Invalid RTC clocktime.time %02d:%02d:%02d",
384 ct.hour, ct.min, ct.sec);
387 return (ts.tv_sec); /* success */
390 * Stop updating the RTC if the date/time fields programmed by
391 * the guest are invalid.
393 VM_CTR0(vrtc->vm, "Invalid RTC date/time programming detected");
394 return (VRTC_BROKEN_TIME);
398 vrtc_time_update(struct vrtc *vrtc, time_t newtime, sbintime_t newbase)
403 uint8_t alarm_sec, alarm_min, alarm_hour;
405 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
408 alarm_sec = rtc->alarm_sec;
409 alarm_min = rtc->alarm_min;
410 alarm_hour = rtc->alarm_hour;
412 oldtime = vrtc->base_rtctime;
413 VM_CTR2(vrtc->vm, "Updating RTC secs from %#lx to %#lx",
416 oldbase = vrtc->base_uptime;
417 VM_CTR2(vrtc->vm, "Updating RTC base uptime from %#lx to %#lx",
419 vrtc->base_uptime = newbase;
421 if (newtime == oldtime)
425 * If 'newtime' indicates that RTC updates are disabled then just
426 * record that and return. There is no need to do alarm interrupt
427 * processing in this case.
429 if (newtime == VRTC_BROKEN_TIME) {
430 vrtc->base_rtctime = VRTC_BROKEN_TIME;
435 * Return an error if RTC updates are halted by the guest.
437 if (rtc_halted(vrtc)) {
438 VM_CTR0(vrtc->vm, "RTC update halted by guest");
444 * If the alarm interrupt is enabled and 'oldtime' is valid
445 * then visit all the seconds between 'oldtime' and 'newtime'
446 * to check for the alarm condition.
448 * Otherwise move the RTC time forward directly to 'newtime'.
450 if (aintr_enabled(vrtc) && oldtime != VRTC_BROKEN_TIME)
451 vrtc->base_rtctime++;
453 vrtc->base_rtctime = newtime;
455 if (aintr_enabled(vrtc)) {
457 * Update the RTC date/time fields before checking
458 * if the alarm conditions are satisfied.
460 secs_to_rtc(vrtc->base_rtctime, vrtc, 0);
462 if ((alarm_sec >= 0xC0 || alarm_sec == rtc->sec) &&
463 (alarm_min >= 0xC0 || alarm_min == rtc->min) &&
464 (alarm_hour >= 0xC0 || alarm_hour == rtc->hour)) {
465 vrtc_set_reg_c(vrtc, rtc->reg_c | RTCIR_ALARM);
468 } while (vrtc->base_rtctime != newtime);
470 if (uintr_enabled(vrtc))
471 vrtc_set_reg_c(vrtc, rtc->reg_c | RTCIR_UPDATE);
477 vrtc_freq(struct vrtc *vrtc)
481 static sbintime_t pf[16] = {
500 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
503 * If both periodic and alarm interrupts are enabled then use the
504 * periodic frequency to drive the callout. The minimum periodic
505 * frequency (2 Hz) is higher than the alarm frequency (1 Hz) so
506 * piggyback the alarm on top of it. The same argument applies to
507 * the update interrupt.
509 if (pintr_enabled(vrtc) && divider_enabled(vrtc->rtcdev.reg_a)) {
510 ratesel = vrtc->rtcdev.reg_a & 0xf;
511 return (pf[ratesel]);
512 } else if (aintr_enabled(vrtc) && update_enabled(vrtc)) {
514 } else if (uintr_enabled(vrtc) && update_enabled(vrtc)) {
522 vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt)
525 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
528 if (callout_active(&vrtc->callout)) {
529 VM_CTR0(vrtc->vm, "RTC callout stopped");
530 callout_stop(&vrtc->callout);
534 VM_CTR1(vrtc->vm, "RTC callout frequency %d hz", SBT_1S / freqsbt);
535 callout_reset_sbt(&vrtc->callout, freqsbt, 0, vrtc_callout_handler,
540 vrtc_callout_handler(void *arg)
542 struct vrtc *vrtc = arg;
543 sbintime_t freqsbt, basetime;
547 VM_CTR0(vrtc->vm, "vrtc callout fired");
550 if (callout_pending(&vrtc->callout)) /* callout was reset */
553 if (!callout_active(&vrtc->callout)) /* callout was stopped */
556 callout_deactivate(&vrtc->callout);
558 KASSERT((vrtc->rtcdev.reg_b & RTCSB_ALL_INTRS) != 0,
559 ("gratuitous vrtc callout"));
561 if (pintr_enabled(vrtc))
562 vrtc_set_reg_c(vrtc, vrtc->rtcdev.reg_c | RTCIR_PERIOD);
564 if (aintr_enabled(vrtc) || uintr_enabled(vrtc)) {
565 rtctime = vrtc_curtime(vrtc, &basetime);
566 error = vrtc_time_update(vrtc, rtctime, basetime);
567 KASSERT(error == 0, ("%s: vrtc_time_update error %d",
571 freqsbt = vrtc_freq(vrtc);
572 KASSERT(freqsbt != 0, ("%s: vrtc frequency cannot be zero", __func__));
573 vrtc_callout_reset(vrtc, freqsbt);
579 vrtc_callout_check(struct vrtc *vrtc, sbintime_t freq)
583 active = callout_active(&vrtc->callout) ? 1 : 0;
584 KASSERT((freq == 0 && !active) || (freq != 0 && active),
585 ("vrtc callout %s with frequency %#lx",
586 active ? "active" : "inactive", freq));
590 vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval)
593 int oldirqf, newirqf;
594 uint8_t oldval, changed;
596 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
599 newval &= RTCIR_ALARM | RTCIR_PERIOD | RTCIR_UPDATE;
601 oldirqf = rtc->reg_c & RTCIR_INT;
602 if ((aintr_enabled(vrtc) && (newval & RTCIR_ALARM) != 0) ||
603 (pintr_enabled(vrtc) && (newval & RTCIR_PERIOD) != 0) ||
604 (uintr_enabled(vrtc) && (newval & RTCIR_UPDATE) != 0)) {
611 rtc->reg_c = newirqf | newval;
612 changed = oldval ^ rtc->reg_c;
614 VM_CTR2(vrtc->vm, "RTC reg_c changed from %#x to %#x",
618 if (!oldirqf && newirqf) {
619 VM_CTR1(vrtc->vm, "RTC irq %d asserted", RTC_IRQ);
620 vatpic_pulse_irq(vrtc->vm, RTC_IRQ);
621 vioapic_pulse_irq(vrtc->vm, RTC_IRQ);
622 } else if (oldirqf && !newirqf) {
623 VM_CTR1(vrtc->vm, "RTC irq %d deasserted", RTC_IRQ);
628 vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
631 sbintime_t oldfreq, newfreq, basetime;
632 time_t curtime, rtctime;
634 uint8_t oldval, changed;
636 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
640 oldfreq = vrtc_freq(vrtc);
643 changed = oldval ^ newval;
645 VM_CTR2(vrtc->vm, "RTC reg_b changed from %#x to %#x",
649 if (changed & RTCSB_HALT) {
650 if ((newval & RTCSB_HALT) == 0) {
651 rtctime = rtc_to_secs(vrtc);
652 basetime = sbinuptime();
653 if (rtctime == VRTC_BROKEN_TIME) {
654 if (rtc_flag_broken_time)
658 curtime = vrtc_curtime(vrtc, &basetime);
659 KASSERT(curtime == vrtc->base_rtctime, ("%s: mismatch "
660 "between vrtc basetime (%#lx) and curtime (%#lx)",
661 __func__, vrtc->base_rtctime, curtime));
664 * Force a refresh of the RTC date/time fields so
665 * they reflect the time right before the guest set
668 secs_to_rtc(curtime, vrtc, 1);
671 * Updates are halted so mark 'base_rtctime' to denote
672 * that the RTC date/time is in flux.
674 rtctime = VRTC_BROKEN_TIME;
675 rtc->reg_b &= ~RTCSB_UINTR;
677 error = vrtc_time_update(vrtc, rtctime, basetime);
678 KASSERT(error == 0, ("vrtc_time_update error %d", error));
682 * Side effect of changes to the interrupt enable bits.
684 if (changed & RTCSB_ALL_INTRS)
685 vrtc_set_reg_c(vrtc, vrtc->rtcdev.reg_c);
688 * Change the callout frequency if it has changed.
690 newfreq = vrtc_freq(vrtc);
691 if (newfreq != oldfreq)
692 vrtc_callout_reset(vrtc, newfreq);
694 vrtc_callout_check(vrtc, newfreq);
697 * The side effect of bits that control the RTC date/time format
698 * is handled lazily when those fields are actually read.
704 vrtc_set_reg_a(struct vrtc *vrtc, uint8_t newval)
706 sbintime_t oldfreq, newfreq;
707 uint8_t oldval, changed;
709 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
711 newval &= ~RTCSA_TUP;
712 oldval = vrtc->rtcdev.reg_a;
713 oldfreq = vrtc_freq(vrtc);
715 if (divider_enabled(oldval) && !divider_enabled(newval)) {
716 VM_CTR2(vrtc->vm, "RTC divider held in reset at %#lx/%#lx",
717 vrtc->base_rtctime, vrtc->base_uptime);
718 } else if (!divider_enabled(oldval) && divider_enabled(newval)) {
720 * If the dividers are coming out of reset then update
721 * 'base_uptime' before this happens. This is done to
722 * maintain the illusion that the RTC date/time was frozen
723 * while the dividers were disabled.
725 vrtc->base_uptime = sbinuptime();
726 VM_CTR2(vrtc->vm, "RTC divider out of reset at %#lx/%#lx",
727 vrtc->base_rtctime, vrtc->base_uptime);
732 vrtc->rtcdev.reg_a = newval;
733 changed = oldval ^ newval;
735 VM_CTR2(vrtc->vm, "RTC reg_a changed from %#x to %#x",
740 * Side effect of changes to rate select and divider enable bits.
742 newfreq = vrtc_freq(vrtc);
743 if (newfreq != oldfreq)
744 vrtc_callout_reset(vrtc, newfreq);
746 vrtc_callout_check(vrtc, newfreq);
750 vrtc_set_time(struct vm *vm, time_t secs)
757 error = vrtc_time_update(vrtc, secs, sbinuptime());
761 VM_CTR2(vrtc->vm, "Error %d setting RTC time to %#lx", error,
764 VM_CTR1(vrtc->vm, "RTC time set to %#lx", secs);
771 vrtc_get_time(struct vm *vm)
779 t = vrtc_curtime(vrtc, &basetime);
786 vrtc_nvram_write(struct vm *vm, int offset, uint8_t value)
794 * Don't allow writes to RTC control registers or the date/time fields.
796 if (offset < offsetof(struct rtcdev, nvram[0]) ||
797 offset == RTC_CENTURY || offset >= sizeof(struct rtcdev)) {
798 VM_CTR1(vrtc->vm, "RTC nvram write to invalid offset %d",
804 ptr = (uint8_t *)(&vrtc->rtcdev);
806 VM_CTR2(vrtc->vm, "RTC nvram write %#x to offset %#x", value, offset);
813 vrtc_nvram_read(struct vm *vm, int offset, uint8_t *retval)
821 * Allow all offsets in the RTC to be read.
823 if (offset < 0 || offset >= sizeof(struct rtcdev))
830 * Update RTC date/time fields if necessary.
832 if (offset < 10 || offset == RTC_CENTURY) {
833 curtime = vrtc_curtime(vrtc, &basetime);
834 secs_to_rtc(curtime, vrtc, 0);
837 ptr = (uint8_t *)(&vrtc->rtcdev);
838 *retval = ptr[offset];
845 vrtc_addr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
861 vrtc->addr = *val & 0x7f;
868 vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
885 if (offset >= sizeof(struct rtcdev)) {
891 curtime = vrtc_curtime(vrtc, &basetime);
892 vrtc_time_update(vrtc, curtime, basetime);
895 * Update RTC date/time fields if necessary.
897 * This is not just for reads of the RTC. The side-effect of writing
898 * the century byte requires other RTC date/time fields (e.g. sec)
899 * to be updated here.
901 if (offset < 10 || offset == RTC_CENTURY)
902 secs_to_rtc(curtime, vrtc, 0);
908 * reg_c interrupt flags are updated only if the
909 * corresponding interrupt enable bit in reg_b is set.
911 *val = vrtc->rtcdev.reg_c;
912 vrtc_set_reg_c(vrtc, 0);
914 *val = *((uint8_t *)rtc + offset);
916 VCPU_CTR2(vm, vcpuid, "Read value %#x from RTC offset %#x",
921 VCPU_CTR1(vm, vcpuid, "RTC reg_a set to %#x", *val);
922 vrtc_set_reg_a(vrtc, *val);
925 VCPU_CTR1(vm, vcpuid, "RTC reg_b set to %#x", *val);
926 error = vrtc_set_reg_b(vrtc, *val);
929 VCPU_CTR1(vm, vcpuid, "RTC reg_c set to %#x (ignored)",
933 VCPU_CTR1(vm, vcpuid, "RTC reg_d set to %#x (ignored)",
938 * High order bit of 'seconds' is readonly.
943 VCPU_CTR2(vm, vcpuid, "RTC offset %#x set to %#x",
945 *((uint8_t *)rtc + offset) = *val;
950 * XXX some guests (e.g. OpenBSD) write the century byte
951 * outside of RTCSB_HALT so re-calculate the RTC date/time.
953 if (offset == RTC_CENTURY && !rtc_halted(vrtc)) {
954 curtime = rtc_to_secs(vrtc);
955 error = vrtc_time_update(vrtc, curtime, sbinuptime());
956 KASSERT(!error, ("vrtc_time_update error %d", error));
957 if (curtime == VRTC_BROKEN_TIME && rtc_flag_broken_time)
966 vrtc_reset(struct vrtc *vrtc)
973 vrtc_set_reg_b(vrtc, rtc->reg_b & ~(RTCSB_ALL_INTRS | RTCSB_SQWE));
974 vrtc_set_reg_c(vrtc, 0);
975 KASSERT(!callout_active(&vrtc->callout), ("rtc callout still active"));
981 vrtc_init(struct vm *vm)
987 vrtc = malloc(sizeof(struct vrtc), M_VRTC, M_WAITOK | M_ZERO);
989 mtx_init(&vrtc->mtx, "vrtc lock", NULL, MTX_DEF);
990 callout_init(&vrtc->callout, 1);
992 /* Allow dividers to keep time but disable everything else */
995 rtc->reg_b = RTCSB_24HR;
997 rtc->reg_d = RTCSD_PWR;
999 /* Reset the index register to a safe value. */
1000 vrtc->addr = RTC_STATUSD;
1003 * Initialize RTC time to 00:00:00 Jan 1, 1970.
1008 vrtc->base_rtctime = VRTC_BROKEN_TIME;
1009 vrtc_time_update(vrtc, curtime, sbinuptime());
1010 secs_to_rtc(curtime, vrtc, 0);
1017 vrtc_cleanup(struct vrtc *vrtc)
1020 callout_drain(&vrtc->callout);