2 * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/queue.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/clock.h>
38 #include <sys/sysctl.h>
40 #include <machine/vmm.h>
49 /* Register layout of the RTC */
67 uint8_t nvram2[128 - 51];
69 CTASSERT(sizeof(struct rtcdev) == 128);
70 CTASSERT(offsetof(struct rtcdev, century) == RTC_CENTURY);
75 struct callout callout;
76 u_int addr; /* RTC register to read or write */
77 sbintime_t base_uptime;
82 #define VRTC_LOCK(vrtc) mtx_lock(&((vrtc)->mtx))
83 #define VRTC_UNLOCK(vrtc) mtx_unlock(&((vrtc)->mtx))
84 #define VRTC_LOCKED(vrtc) mtx_owned(&((vrtc)->mtx))
87 * RTC time is considered "broken" if:
88 * - RTC updates are halted by the guest
89 * - RTC date/time fields have invalid values
91 #define VRTC_BROKEN_TIME ((time_t)-1)
94 #define RTCSB_BIN 0x04
95 #define RTCSB_ALL_INTRS (RTCSB_UINTR | RTCSB_AINTR | RTCSB_PINTR)
96 #define rtc_halted(vrtc) ((vrtc->rtcdev.reg_b & RTCSB_HALT) != 0)
97 #define aintr_enabled(vrtc) (((vrtc)->rtcdev.reg_b & RTCSB_AINTR) != 0)
98 #define pintr_enabled(vrtc) (((vrtc)->rtcdev.reg_b & RTCSB_PINTR) != 0)
99 #define uintr_enabled(vrtc) (((vrtc)->rtcdev.reg_b & RTCSB_UINTR) != 0)
101 static void vrtc_callout_handler(void *arg);
102 static void vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval);
104 static MALLOC_DEFINE(M_VRTC, "vrtc", "bhyve virtual rtc");
106 SYSCTL_DECL(_hw_vmm);
107 SYSCTL_NODE(_hw_vmm, OID_AUTO, vrtc, CTLFLAG_RW, NULL, NULL);
109 static int rtc_flag_broken_time = 1;
110 SYSCTL_INT(_hw_vmm_vrtc, OID_AUTO, flag_broken_time, CTLFLAG_RDTUN,
111 &rtc_flag_broken_time, 0, "Stop guest when invalid RTC time is detected");
114 divider_enabled(int reg_a)
117 * The RTC is counting only when dividers are not held in reset.
119 return ((reg_a & 0x70) == 0x20);
123 update_enabled(struct vrtc *vrtc)
126 * RTC date/time can be updated only if:
127 * - divider is not held in reset
128 * - guest has not disabled updates
129 * - the date/time fields have valid contents
131 if (!divider_enabled(vrtc->rtcdev.reg_a))
134 if (rtc_halted(vrtc))
137 if (vrtc->base_rtctime == VRTC_BROKEN_TIME)
144 vrtc_curtime(struct vrtc *vrtc, sbintime_t *basetime)
146 sbintime_t now, delta;
149 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
151 t = vrtc->base_rtctime;
152 *basetime = vrtc->base_uptime;
153 if (update_enabled(vrtc)) {
155 delta = now - vrtc->base_uptime;
156 KASSERT(delta >= 0, ("vrtc_curtime: uptime went backwards: "
157 "%#lx to %#lx", vrtc->base_uptime, now));
158 secs = delta / SBT_1S;
160 *basetime += secs * SBT_1S;
165 static __inline uint8_t
166 rtcset(struct rtcdev *rtc, int val)
169 KASSERT(val >= 0 && val < 100, ("%s: invalid bin2bcd index %d",
172 return ((rtc->reg_b & RTCSB_BIN) ? val : bin2bcd_data[val]);
176 secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update)
183 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
186 KASSERT(rtctime == VRTC_BROKEN_TIME,
187 ("%s: invalid vrtc time %#lx", __func__, rtctime));
192 * If the RTC is halted then the guest has "ownership" of the
193 * date/time fields. Don't update the RTC date/time fields in
194 * this case (unless forced).
196 if (rtc_halted(vrtc) && !force_update)
201 clock_ts_to_ct(&ts, &ct);
203 KASSERT(ct.sec >= 0 && ct.sec <= 59, ("invalid clocktime sec %d",
205 KASSERT(ct.min >= 0 && ct.min <= 59, ("invalid clocktime min %d",
207 KASSERT(ct.hour >= 0 && ct.hour <= 23, ("invalid clocktime hour %d",
209 KASSERT(ct.dow >= 0 && ct.dow <= 6, ("invalid clocktime wday %d",
211 KASSERT(ct.day >= 1 && ct.day <= 31, ("invalid clocktime mday %d",
213 KASSERT(ct.mon >= 1 && ct.mon <= 12, ("invalid clocktime month %d",
215 KASSERT(ct.year >= POSIX_BASE_YEAR, ("invalid clocktime year %d",
219 rtc->sec = rtcset(rtc, ct.sec);
220 rtc->min = rtcset(rtc, ct.min);
222 if (rtc->reg_b & RTCSB_24HR) {
226 * Convert to the 12-hour format.
235 * The remaining 'ct.hour' values are interpreted as:
236 * [1 - 11] -> 1 - 11 AM
237 * [13 - 23] -> 1 - 11 PM
244 rtc->hour = rtcset(rtc, hour);
246 if ((rtc->reg_b & RTCSB_24HR) == 0 && ct.hour >= 12)
247 rtc->hour |= 0x80; /* set MSB to indicate PM */
249 rtc->day_of_week = rtcset(rtc, ct.dow + 1);
250 rtc->day_of_month = rtcset(rtc, ct.day);
251 rtc->month = rtcset(rtc, ct.mon);
252 rtc->year = rtcset(rtc, ct.year % 100);
253 rtc->century = rtcset(rtc, ct.year / 100);
257 rtcget(struct rtcdev *rtc, int val, int *retval)
259 uint8_t upper, lower;
261 if (rtc->reg_b & RTCSB_BIN) {
267 upper = (val >> 4) & 0xf;
269 if (lower > 9 || upper > 9)
272 *retval = upper * 10 + lower;
277 rtc_to_secs(struct vrtc *vrtc)
283 int century, error, hour, pm, year;
285 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
290 bzero(&ct, sizeof(struct clocktime));
292 error = rtcget(rtc, rtc->sec, &ct.sec);
293 if (error || ct.sec < 0 || ct.sec > 59) {
294 VM_CTR2(vm, "Invalid RTC sec %#x/%d", rtc->sec, ct.sec);
298 error = rtcget(rtc, rtc->min, &ct.min);
299 if (error || ct.min < 0 || ct.min > 59) {
300 VM_CTR2(vm, "Invalid RTC min %#x/%d", rtc->min, ct.min);
306 if ((rtc->reg_b & RTCSB_24HR) == 0) {
312 error = rtcget(rtc, hour, &ct.hour);
313 if ((rtc->reg_b & RTCSB_24HR) == 0) {
314 if (ct.hour >= 1 && ct.hour <= 12) {
316 * Convert from 12-hour format to internal 24-hour
317 * representation as follows:
319 * 12-hour format ct.hour
330 VM_CTR2(vm, "Invalid RTC 12-hour format %#x/%d",
336 if (error || ct.hour < 0 || ct.hour > 23) {
337 VM_CTR2(vm, "Invalid RTC hour %#x/%d", rtc->hour, ct.hour);
342 * Ignore 'rtc->dow' because some guests like Linux don't bother
343 * setting it at all while others like OpenBSD/i386 set it incorrectly.
345 * clock_ct_to_ts() does not depend on 'ct.dow' anyways so ignore it.
349 error = rtcget(rtc, rtc->day_of_month, &ct.day);
350 if (error || ct.day < 1 || ct.day > 31) {
351 VM_CTR2(vm, "Invalid RTC mday %#x/%d", rtc->day_of_month,
356 error = rtcget(rtc, rtc->month, &ct.mon);
357 if (error || ct.mon < 1 || ct.mon > 12) {
358 VM_CTR2(vm, "Invalid RTC month %#x/%d", rtc->month, ct.mon);
362 error = rtcget(rtc, rtc->year, &year);
363 if (error || year < 0 || year > 99) {
364 VM_CTR2(vm, "Invalid RTC year %#x/%d", rtc->year, year);
368 error = rtcget(rtc, rtc->century, ¢ury);
369 ct.year = century * 100 + year;
370 if (error || ct.year < POSIX_BASE_YEAR) {
371 VM_CTR2(vm, "Invalid RTC century %#x/%d", rtc->century,
376 error = clock_ct_to_ts(&ct, &ts);
377 if (error || ts.tv_sec < 0) {
378 VM_CTR3(vm, "Invalid RTC clocktime.date %04d-%02d-%02d",
379 ct.year, ct.mon, ct.day);
380 VM_CTR3(vm, "Invalid RTC clocktime.time %02d:%02d:%02d",
381 ct.hour, ct.min, ct.sec);
384 return (ts.tv_sec); /* success */
387 * Stop updating the RTC if the date/time fields programmed by
388 * the guest are invalid.
390 VM_CTR0(vrtc->vm, "Invalid RTC date/time programming detected");
391 return (VRTC_BROKEN_TIME);
395 vrtc_time_update(struct vrtc *vrtc, time_t newtime, sbintime_t newbase)
400 uint8_t alarm_sec, alarm_min, alarm_hour;
402 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
405 alarm_sec = rtc->alarm_sec;
406 alarm_min = rtc->alarm_min;
407 alarm_hour = rtc->alarm_hour;
409 oldtime = vrtc->base_rtctime;
410 VM_CTR2(vrtc->vm, "Updating RTC secs from %#lx to %#lx",
413 oldbase = vrtc->base_uptime;
414 VM_CTR2(vrtc->vm, "Updating RTC base uptime from %#lx to %#lx",
416 vrtc->base_uptime = newbase;
418 if (newtime == oldtime)
422 * If 'newtime' indicates that RTC updates are disabled then just
423 * record that and return. There is no need to do alarm interrupt
424 * processing in this case.
426 if (newtime == VRTC_BROKEN_TIME) {
427 vrtc->base_rtctime = VRTC_BROKEN_TIME;
432 * Return an error if RTC updates are halted by the guest.
434 if (rtc_halted(vrtc)) {
435 VM_CTR0(vrtc->vm, "RTC update halted by guest");
441 * If the alarm interrupt is enabled and 'oldtime' is valid
442 * then visit all the seconds between 'oldtime' and 'newtime'
443 * to check for the alarm condition.
445 * Otherwise move the RTC time forward directly to 'newtime'.
447 if (aintr_enabled(vrtc) && oldtime != VRTC_BROKEN_TIME)
448 vrtc->base_rtctime++;
450 vrtc->base_rtctime = newtime;
452 if (aintr_enabled(vrtc)) {
454 * Update the RTC date/time fields before checking
455 * if the alarm conditions are satisfied.
457 secs_to_rtc(vrtc->base_rtctime, vrtc, 0);
459 if ((alarm_sec >= 0xC0 || alarm_sec == rtc->sec) &&
460 (alarm_min >= 0xC0 || alarm_min == rtc->min) &&
461 (alarm_hour >= 0xC0 || alarm_hour == rtc->hour)) {
462 vrtc_set_reg_c(vrtc, rtc->reg_c | RTCIR_ALARM);
465 } while (vrtc->base_rtctime != newtime);
467 if (uintr_enabled(vrtc))
468 vrtc_set_reg_c(vrtc, rtc->reg_c | RTCIR_UPDATE);
474 vrtc_freq(struct vrtc *vrtc)
478 static sbintime_t pf[16] = {
497 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
500 * If both periodic and alarm interrupts are enabled then use the
501 * periodic frequency to drive the callout. The minimum periodic
502 * frequency (2 Hz) is higher than the alarm frequency (1 Hz) so
503 * piggyback the alarm on top of it. The same argument applies to
504 * the update interrupt.
506 if (pintr_enabled(vrtc) && divider_enabled(vrtc->rtcdev.reg_a)) {
507 ratesel = vrtc->rtcdev.reg_a & 0xf;
508 return (pf[ratesel]);
509 } else if (aintr_enabled(vrtc) && update_enabled(vrtc)) {
511 } else if (uintr_enabled(vrtc) && update_enabled(vrtc)) {
519 vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt)
522 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
525 if (callout_active(&vrtc->callout)) {
526 VM_CTR0(vrtc->vm, "RTC callout stopped");
527 callout_stop(&vrtc->callout);
531 VM_CTR1(vrtc->vm, "RTC callout frequency %d hz", SBT_1S / freqsbt);
532 callout_reset_sbt(&vrtc->callout, freqsbt, 0, vrtc_callout_handler,
537 vrtc_callout_handler(void *arg)
539 struct vrtc *vrtc = arg;
540 sbintime_t freqsbt, basetime;
544 VM_CTR0(vrtc->vm, "vrtc callout fired");
547 if (callout_pending(&vrtc->callout)) /* callout was reset */
550 if (!callout_active(&vrtc->callout)) /* callout was stopped */
553 callout_deactivate(&vrtc->callout);
555 KASSERT((vrtc->rtcdev.reg_b & RTCSB_ALL_INTRS) != 0,
556 ("gratuitous vrtc callout"));
558 if (pintr_enabled(vrtc))
559 vrtc_set_reg_c(vrtc, vrtc->rtcdev.reg_c | RTCIR_PERIOD);
561 if (aintr_enabled(vrtc) || uintr_enabled(vrtc)) {
562 rtctime = vrtc_curtime(vrtc, &basetime);
563 error = vrtc_time_update(vrtc, rtctime, basetime);
564 KASSERT(error == 0, ("%s: vrtc_time_update error %d",
568 freqsbt = vrtc_freq(vrtc);
569 KASSERT(freqsbt != 0, ("%s: vrtc frequency cannot be zero", __func__));
570 vrtc_callout_reset(vrtc, freqsbt);
576 vrtc_callout_check(struct vrtc *vrtc, sbintime_t freq)
580 active = callout_active(&vrtc->callout) ? 1 : 0;
581 KASSERT((freq == 0 && !active) || (freq != 0 && active),
582 ("vrtc callout %s with frequency %#lx",
583 active ? "active" : "inactive", freq));
587 vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval)
590 int oldirqf, newirqf;
591 uint8_t oldval, changed;
593 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
596 newval &= RTCIR_ALARM | RTCIR_PERIOD | RTCIR_UPDATE;
598 oldirqf = rtc->reg_c & RTCIR_INT;
599 if ((aintr_enabled(vrtc) && (newval & RTCIR_ALARM) != 0) ||
600 (pintr_enabled(vrtc) && (newval & RTCIR_PERIOD) != 0) ||
601 (uintr_enabled(vrtc) && (newval & RTCIR_UPDATE) != 0)) {
608 rtc->reg_c = newirqf | newval;
609 changed = oldval ^ rtc->reg_c;
611 VM_CTR2(vrtc->vm, "RTC reg_c changed from %#x to %#x",
615 if (!oldirqf && newirqf) {
616 VM_CTR1(vrtc->vm, "RTC irq %d asserted", RTC_IRQ);
617 vatpic_pulse_irq(vrtc->vm, RTC_IRQ);
618 vioapic_pulse_irq(vrtc->vm, RTC_IRQ);
619 } else if (oldirqf && !newirqf) {
620 VM_CTR1(vrtc->vm, "RTC irq %d deasserted", RTC_IRQ);
625 vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
628 sbintime_t oldfreq, newfreq, basetime;
629 time_t curtime, rtctime;
631 uint8_t oldval, changed;
633 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
637 oldfreq = vrtc_freq(vrtc);
640 changed = oldval ^ newval;
642 VM_CTR2(vrtc->vm, "RTC reg_b changed from %#x to %#x",
646 if (changed & RTCSB_HALT) {
647 if ((newval & RTCSB_HALT) == 0) {
648 rtctime = rtc_to_secs(vrtc);
649 basetime = sbinuptime();
650 if (rtctime == VRTC_BROKEN_TIME) {
651 if (rtc_flag_broken_time)
655 curtime = vrtc_curtime(vrtc, &basetime);
656 KASSERT(curtime == vrtc->base_rtctime, ("%s: mismatch "
657 "between vrtc basetime (%#lx) and curtime (%#lx)",
658 __func__, vrtc->base_rtctime, curtime));
661 * Force a refresh of the RTC date/time fields so
662 * they reflect the time right before the guest set
665 secs_to_rtc(curtime, vrtc, 1);
668 * Updates are halted so mark 'base_rtctime' to denote
669 * that the RTC date/time is in flux.
671 rtctime = VRTC_BROKEN_TIME;
672 rtc->reg_b &= ~RTCSB_UINTR;
674 error = vrtc_time_update(vrtc, rtctime, basetime);
675 KASSERT(error == 0, ("vrtc_time_update error %d", error));
679 * Side effect of changes to the interrupt enable bits.
681 if (changed & RTCSB_ALL_INTRS)
682 vrtc_set_reg_c(vrtc, vrtc->rtcdev.reg_c);
685 * Change the callout frequency if it has changed.
687 newfreq = vrtc_freq(vrtc);
688 if (newfreq != oldfreq)
689 vrtc_callout_reset(vrtc, newfreq);
691 vrtc_callout_check(vrtc, newfreq);
694 * The side effect of bits that control the RTC date/time format
695 * is handled lazily when those fields are actually read.
701 vrtc_set_reg_a(struct vrtc *vrtc, uint8_t newval)
703 sbintime_t oldfreq, newfreq;
704 uint8_t oldval, changed;
706 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
708 newval &= ~RTCSA_TUP;
709 oldval = vrtc->rtcdev.reg_a;
710 oldfreq = vrtc_freq(vrtc);
712 if (divider_enabled(oldval) && !divider_enabled(newval)) {
713 VM_CTR2(vrtc->vm, "RTC divider held in reset at %#lx/%#lx",
714 vrtc->base_rtctime, vrtc->base_uptime);
715 } else if (!divider_enabled(oldval) && divider_enabled(newval)) {
717 * If the dividers are coming out of reset then update
718 * 'base_uptime' before this happens. This is done to
719 * maintain the illusion that the RTC date/time was frozen
720 * while the dividers were disabled.
722 vrtc->base_uptime = sbinuptime();
723 VM_CTR2(vrtc->vm, "RTC divider out of reset at %#lx/%#lx",
724 vrtc->base_rtctime, vrtc->base_uptime);
729 vrtc->rtcdev.reg_a = newval;
730 changed = oldval ^ newval;
732 VM_CTR2(vrtc->vm, "RTC reg_a changed from %#x to %#x",
737 * Side effect of changes to rate select and divider enable bits.
739 newfreq = vrtc_freq(vrtc);
740 if (newfreq != oldfreq)
741 vrtc_callout_reset(vrtc, newfreq);
743 vrtc_callout_check(vrtc, newfreq);
747 vrtc_set_time(struct vm *vm, time_t secs)
754 error = vrtc_time_update(vrtc, secs, sbinuptime());
758 VM_CTR2(vrtc->vm, "Error %d setting RTC time to %#lx", error,
761 VM_CTR1(vrtc->vm, "RTC time set to %#lx", secs);
768 vrtc_get_time(struct vm *vm)
776 t = vrtc_curtime(vrtc, &basetime);
783 vrtc_nvram_write(struct vm *vm, int offset, uint8_t value)
791 * Don't allow writes to RTC control registers or the date/time fields.
793 if (offset < offsetof(struct rtcdev, nvram[0]) ||
794 offset == RTC_CENTURY || offset >= sizeof(struct rtcdev)) {
795 VM_CTR1(vrtc->vm, "RTC nvram write to invalid offset %d",
801 ptr = (uint8_t *)(&vrtc->rtcdev);
803 VM_CTR2(vrtc->vm, "RTC nvram write %#x to offset %#x", value, offset);
810 vrtc_nvram_read(struct vm *vm, int offset, uint8_t *retval)
818 * Allow all offsets in the RTC to be read.
820 if (offset < 0 || offset >= sizeof(struct rtcdev))
827 * Update RTC date/time fields if necessary.
829 if (offset < 10 || offset == RTC_CENTURY) {
830 curtime = vrtc_curtime(vrtc, &basetime);
831 secs_to_rtc(curtime, vrtc, 0);
834 ptr = (uint8_t *)(&vrtc->rtcdev);
835 *retval = ptr[offset];
842 vrtc_addr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
858 vrtc->addr = *val & 0x7f;
865 vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
882 if (offset >= sizeof(struct rtcdev)) {
888 curtime = vrtc_curtime(vrtc, &basetime);
889 vrtc_time_update(vrtc, curtime, basetime);
892 * Update RTC date/time fields if necessary.
894 * This is not just for reads of the RTC. The side-effect of writing
895 * the century byte requires other RTC date/time fields (e.g. sec)
896 * to be updated here.
898 if (offset < 10 || offset == RTC_CENTURY)
899 secs_to_rtc(curtime, vrtc, 0);
905 * reg_c interrupt flags are updated only if the
906 * corresponding interrupt enable bit in reg_b is set.
908 *val = vrtc->rtcdev.reg_c;
909 vrtc_set_reg_c(vrtc, 0);
911 *val = *((uint8_t *)rtc + offset);
913 VCPU_CTR2(vm, vcpuid, "Read value %#x from RTC offset %#x",
918 VCPU_CTR1(vm, vcpuid, "RTC reg_a set to %#x", *val);
919 vrtc_set_reg_a(vrtc, *val);
922 VCPU_CTR1(vm, vcpuid, "RTC reg_b set to %#x", *val);
923 error = vrtc_set_reg_b(vrtc, *val);
926 VCPU_CTR1(vm, vcpuid, "RTC reg_c set to %#x (ignored)",
930 VCPU_CTR1(vm, vcpuid, "RTC reg_d set to %#x (ignored)",
935 * High order bit of 'seconds' is readonly.
940 VCPU_CTR2(vm, vcpuid, "RTC offset %#x set to %#x",
942 *((uint8_t *)rtc + offset) = *val;
947 * XXX some guests (e.g. OpenBSD) write the century byte
948 * outside of RTCSB_HALT so re-calculate the RTC date/time.
950 if (offset == RTC_CENTURY && !rtc_halted(vrtc)) {
951 curtime = rtc_to_secs(vrtc);
952 error = vrtc_time_update(vrtc, curtime, sbinuptime());
953 KASSERT(!error, ("vrtc_time_update error %d", error));
954 if (curtime == VRTC_BROKEN_TIME && rtc_flag_broken_time)
963 vrtc_reset(struct vrtc *vrtc)
970 vrtc_set_reg_b(vrtc, rtc->reg_b & ~(RTCSB_ALL_INTRS | RTCSB_SQWE));
971 vrtc_set_reg_c(vrtc, 0);
972 KASSERT(!callout_active(&vrtc->callout), ("rtc callout still active"));
978 vrtc_init(struct vm *vm)
984 vrtc = malloc(sizeof(struct vrtc), M_VRTC, M_WAITOK | M_ZERO);
986 mtx_init(&vrtc->mtx, "vrtc lock", NULL, MTX_DEF);
987 callout_init(&vrtc->callout, 1);
989 /* Allow dividers to keep time but disable everything else */
992 rtc->reg_b = RTCSB_24HR;
994 rtc->reg_d = RTCSD_PWR;
996 /* Reset the index register to a safe value. */
997 vrtc->addr = RTC_STATUSD;
1000 * Initialize RTC time to 00:00:00 Jan 1, 1970.
1005 vrtc->base_rtctime = VRTC_BROKEN_TIME;
1006 vrtc_time_update(vrtc, curtime, sbinuptime());
1007 secs_to_rtc(curtime, vrtc, 0);
1014 vrtc_cleanup(struct vrtc *vrtc)
1017 callout_drain(&vrtc->callout);