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, NULL, NULL);
111 static int rtc_flag_broken_time = 1;
112 SYSCTL_INT(_hw_vmm_vrtc, OID_AUTO, flag_broken_time, CTLFLAG_RDTUN,
113 &rtc_flag_broken_time, 0, "Stop guest when invalid RTC time is detected");
116 divider_enabled(int reg_a)
119 * The RTC is counting only when dividers are not held in reset.
121 return ((reg_a & 0x70) == 0x20);
125 update_enabled(struct vrtc *vrtc)
128 * RTC date/time can be updated only if:
129 * - divider is not held in reset
130 * - guest has not disabled updates
131 * - the date/time fields have valid contents
133 if (!divider_enabled(vrtc->rtcdev.reg_a))
136 if (rtc_halted(vrtc))
139 if (vrtc->base_rtctime == VRTC_BROKEN_TIME)
146 vrtc_curtime(struct vrtc *vrtc, sbintime_t *basetime)
148 sbintime_t now, delta;
151 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
153 t = vrtc->base_rtctime;
154 *basetime = vrtc->base_uptime;
155 if (update_enabled(vrtc)) {
157 delta = now - vrtc->base_uptime;
158 KASSERT(delta >= 0, ("vrtc_curtime: uptime went backwards: "
159 "%#lx to %#lx", vrtc->base_uptime, now));
160 secs = delta / SBT_1S;
162 *basetime += secs * SBT_1S;
167 static __inline uint8_t
168 rtcset(struct rtcdev *rtc, int val)
171 KASSERT(val >= 0 && val < 100, ("%s: invalid bin2bcd index %d",
174 return ((rtc->reg_b & RTCSB_BIN) ? val : bin2bcd_data[val]);
178 secs_to_rtc(time_t rtctime, struct vrtc *vrtc, int force_update)
185 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
188 KASSERT(rtctime == VRTC_BROKEN_TIME,
189 ("%s: invalid vrtc time %#lx", __func__, rtctime));
194 * If the RTC is halted then the guest has "ownership" of the
195 * date/time fields. Don't update the RTC date/time fields in
196 * this case (unless forced).
198 if (rtc_halted(vrtc) && !force_update)
203 clock_ts_to_ct(&ts, &ct);
205 KASSERT(ct.sec >= 0 && ct.sec <= 59, ("invalid clocktime sec %d",
207 KASSERT(ct.min >= 0 && ct.min <= 59, ("invalid clocktime min %d",
209 KASSERT(ct.hour >= 0 && ct.hour <= 23, ("invalid clocktime hour %d",
211 KASSERT(ct.dow >= 0 && ct.dow <= 6, ("invalid clocktime wday %d",
213 KASSERT(ct.day >= 1 && ct.day <= 31, ("invalid clocktime mday %d",
215 KASSERT(ct.mon >= 1 && ct.mon <= 12, ("invalid clocktime month %d",
217 KASSERT(ct.year >= POSIX_BASE_YEAR, ("invalid clocktime year %d",
221 rtc->sec = rtcset(rtc, ct.sec);
222 rtc->min = rtcset(rtc, ct.min);
224 if (rtc->reg_b & RTCSB_24HR) {
228 * Convert to the 12-hour format.
237 * The remaining 'ct.hour' values are interpreted as:
238 * [1 - 11] -> 1 - 11 AM
239 * [13 - 23] -> 1 - 11 PM
246 rtc->hour = rtcset(rtc, hour);
248 if ((rtc->reg_b & RTCSB_24HR) == 0 && ct.hour >= 12)
249 rtc->hour |= 0x80; /* set MSB to indicate PM */
251 rtc->day_of_week = rtcset(rtc, ct.dow + 1);
252 rtc->day_of_month = rtcset(rtc, ct.day);
253 rtc->month = rtcset(rtc, ct.mon);
254 rtc->year = rtcset(rtc, ct.year % 100);
255 rtc->century = rtcset(rtc, ct.year / 100);
259 rtcget(struct rtcdev *rtc, int val, int *retval)
261 uint8_t upper, lower;
263 if (rtc->reg_b & RTCSB_BIN) {
269 upper = (val >> 4) & 0xf;
271 if (lower > 9 || upper > 9)
274 *retval = upper * 10 + lower;
279 rtc_to_secs(struct vrtc *vrtc)
285 int century, error, hour, pm, year;
287 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
292 bzero(&ct, sizeof(struct clocktime));
294 error = rtcget(rtc, rtc->sec, &ct.sec);
295 if (error || ct.sec < 0 || ct.sec > 59) {
296 VM_CTR2(vm, "Invalid RTC sec %#x/%d", rtc->sec, ct.sec);
300 error = rtcget(rtc, rtc->min, &ct.min);
301 if (error || ct.min < 0 || ct.min > 59) {
302 VM_CTR2(vm, "Invalid RTC min %#x/%d", rtc->min, ct.min);
308 if ((rtc->reg_b & RTCSB_24HR) == 0) {
314 error = rtcget(rtc, hour, &ct.hour);
315 if ((rtc->reg_b & RTCSB_24HR) == 0) {
316 if (ct.hour >= 1 && ct.hour <= 12) {
318 * Convert from 12-hour format to internal 24-hour
319 * representation as follows:
321 * 12-hour format ct.hour
332 VM_CTR2(vm, "Invalid RTC 12-hour format %#x/%d",
338 if (error || ct.hour < 0 || ct.hour > 23) {
339 VM_CTR2(vm, "Invalid RTC hour %#x/%d", rtc->hour, ct.hour);
344 * Ignore 'rtc->dow' because some guests like Linux don't bother
345 * setting it at all while others like OpenBSD/i386 set it incorrectly.
347 * clock_ct_to_ts() does not depend on 'ct.dow' anyways so ignore it.
351 error = rtcget(rtc, rtc->day_of_month, &ct.day);
352 if (error || ct.day < 1 || ct.day > 31) {
353 VM_CTR2(vm, "Invalid RTC mday %#x/%d", rtc->day_of_month,
358 error = rtcget(rtc, rtc->month, &ct.mon);
359 if (error || ct.mon < 1 || ct.mon > 12) {
360 VM_CTR2(vm, "Invalid RTC month %#x/%d", rtc->month, ct.mon);
364 error = rtcget(rtc, rtc->year, &year);
365 if (error || year < 0 || year > 99) {
366 VM_CTR2(vm, "Invalid RTC year %#x/%d", rtc->year, year);
370 error = rtcget(rtc, rtc->century, ¢ury);
371 ct.year = century * 100 + year;
372 if (error || ct.year < POSIX_BASE_YEAR) {
373 VM_CTR2(vm, "Invalid RTC century %#x/%d", rtc->century,
378 error = clock_ct_to_ts(&ct, &ts);
379 if (error || ts.tv_sec < 0) {
380 VM_CTR3(vm, "Invalid RTC clocktime.date %04d-%02d-%02d",
381 ct.year, ct.mon, ct.day);
382 VM_CTR3(vm, "Invalid RTC clocktime.time %02d:%02d:%02d",
383 ct.hour, ct.min, ct.sec);
386 return (ts.tv_sec); /* success */
389 * Stop updating the RTC if the date/time fields programmed by
390 * the guest are invalid.
392 VM_CTR0(vrtc->vm, "Invalid RTC date/time programming detected");
393 return (VRTC_BROKEN_TIME);
397 vrtc_time_update(struct vrtc *vrtc, time_t newtime, sbintime_t newbase)
402 uint8_t alarm_sec, alarm_min, alarm_hour;
404 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
407 alarm_sec = rtc->alarm_sec;
408 alarm_min = rtc->alarm_min;
409 alarm_hour = rtc->alarm_hour;
411 oldtime = vrtc->base_rtctime;
412 VM_CTR2(vrtc->vm, "Updating RTC secs from %#lx to %#lx",
415 oldbase = vrtc->base_uptime;
416 VM_CTR2(vrtc->vm, "Updating RTC base uptime from %#lx to %#lx",
418 vrtc->base_uptime = newbase;
420 if (newtime == oldtime)
424 * If 'newtime' indicates that RTC updates are disabled then just
425 * record that and return. There is no need to do alarm interrupt
426 * processing in this case.
428 if (newtime == VRTC_BROKEN_TIME) {
429 vrtc->base_rtctime = VRTC_BROKEN_TIME;
434 * Return an error if RTC updates are halted by the guest.
436 if (rtc_halted(vrtc)) {
437 VM_CTR0(vrtc->vm, "RTC update halted by guest");
443 * If the alarm interrupt is enabled and 'oldtime' is valid
444 * then visit all the seconds between 'oldtime' and 'newtime'
445 * to check for the alarm condition.
447 * Otherwise move the RTC time forward directly to 'newtime'.
449 if (aintr_enabled(vrtc) && oldtime != VRTC_BROKEN_TIME)
450 vrtc->base_rtctime++;
452 vrtc->base_rtctime = newtime;
454 if (aintr_enabled(vrtc)) {
456 * Update the RTC date/time fields before checking
457 * if the alarm conditions are satisfied.
459 secs_to_rtc(vrtc->base_rtctime, vrtc, 0);
461 if ((alarm_sec >= 0xC0 || alarm_sec == rtc->sec) &&
462 (alarm_min >= 0xC0 || alarm_min == rtc->min) &&
463 (alarm_hour >= 0xC0 || alarm_hour == rtc->hour)) {
464 vrtc_set_reg_c(vrtc, rtc->reg_c | RTCIR_ALARM);
467 } while (vrtc->base_rtctime != newtime);
469 if (uintr_enabled(vrtc))
470 vrtc_set_reg_c(vrtc, rtc->reg_c | RTCIR_UPDATE);
476 vrtc_freq(struct vrtc *vrtc)
480 static sbintime_t pf[16] = {
499 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
502 * If both periodic and alarm interrupts are enabled then use the
503 * periodic frequency to drive the callout. The minimum periodic
504 * frequency (2 Hz) is higher than the alarm frequency (1 Hz) so
505 * piggyback the alarm on top of it. The same argument applies to
506 * the update interrupt.
508 if (pintr_enabled(vrtc) && divider_enabled(vrtc->rtcdev.reg_a)) {
509 ratesel = vrtc->rtcdev.reg_a & 0xf;
510 return (pf[ratesel]);
511 } else if (aintr_enabled(vrtc) && update_enabled(vrtc)) {
513 } else if (uintr_enabled(vrtc) && update_enabled(vrtc)) {
521 vrtc_callout_reset(struct vrtc *vrtc, sbintime_t freqsbt)
524 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
527 if (callout_active(&vrtc->callout)) {
528 VM_CTR0(vrtc->vm, "RTC callout stopped");
529 callout_stop(&vrtc->callout);
533 VM_CTR1(vrtc->vm, "RTC callout frequency %d hz", SBT_1S / freqsbt);
534 callout_reset_sbt(&vrtc->callout, freqsbt, 0, vrtc_callout_handler,
539 vrtc_callout_handler(void *arg)
541 struct vrtc *vrtc = arg;
542 sbintime_t freqsbt, basetime;
546 VM_CTR0(vrtc->vm, "vrtc callout fired");
549 if (callout_pending(&vrtc->callout)) /* callout was reset */
552 if (!callout_active(&vrtc->callout)) /* callout was stopped */
555 callout_deactivate(&vrtc->callout);
557 KASSERT((vrtc->rtcdev.reg_b & RTCSB_ALL_INTRS) != 0,
558 ("gratuitous vrtc callout"));
560 if (pintr_enabled(vrtc))
561 vrtc_set_reg_c(vrtc, vrtc->rtcdev.reg_c | RTCIR_PERIOD);
563 if (aintr_enabled(vrtc) || uintr_enabled(vrtc)) {
564 rtctime = vrtc_curtime(vrtc, &basetime);
565 error = vrtc_time_update(vrtc, rtctime, basetime);
566 KASSERT(error == 0, ("%s: vrtc_time_update error %d",
570 freqsbt = vrtc_freq(vrtc);
571 KASSERT(freqsbt != 0, ("%s: vrtc frequency cannot be zero", __func__));
572 vrtc_callout_reset(vrtc, freqsbt);
578 vrtc_callout_check(struct vrtc *vrtc, sbintime_t freq)
582 active = callout_active(&vrtc->callout) ? 1 : 0;
583 KASSERT((freq == 0 && !active) || (freq != 0 && active),
584 ("vrtc callout %s with frequency %#lx",
585 active ? "active" : "inactive", freq));
589 vrtc_set_reg_c(struct vrtc *vrtc, uint8_t newval)
592 int oldirqf, newirqf;
593 uint8_t oldval, changed;
595 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
598 newval &= RTCIR_ALARM | RTCIR_PERIOD | RTCIR_UPDATE;
600 oldirqf = rtc->reg_c & RTCIR_INT;
601 if ((aintr_enabled(vrtc) && (newval & RTCIR_ALARM) != 0) ||
602 (pintr_enabled(vrtc) && (newval & RTCIR_PERIOD) != 0) ||
603 (uintr_enabled(vrtc) && (newval & RTCIR_UPDATE) != 0)) {
610 rtc->reg_c = newirqf | newval;
611 changed = oldval ^ rtc->reg_c;
613 VM_CTR2(vrtc->vm, "RTC reg_c changed from %#x to %#x",
617 if (!oldirqf && newirqf) {
618 VM_CTR1(vrtc->vm, "RTC irq %d asserted", RTC_IRQ);
619 vatpic_pulse_irq(vrtc->vm, RTC_IRQ);
620 vioapic_pulse_irq(vrtc->vm, RTC_IRQ);
621 } else if (oldirqf && !newirqf) {
622 VM_CTR1(vrtc->vm, "RTC irq %d deasserted", RTC_IRQ);
627 vrtc_set_reg_b(struct vrtc *vrtc, uint8_t newval)
630 sbintime_t oldfreq, newfreq, basetime;
631 time_t curtime, rtctime;
633 uint8_t oldval, changed;
635 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
639 oldfreq = vrtc_freq(vrtc);
642 changed = oldval ^ newval;
644 VM_CTR2(vrtc->vm, "RTC reg_b changed from %#x to %#x",
648 if (changed & RTCSB_HALT) {
649 if ((newval & RTCSB_HALT) == 0) {
650 rtctime = rtc_to_secs(vrtc);
651 basetime = sbinuptime();
652 if (rtctime == VRTC_BROKEN_TIME) {
653 if (rtc_flag_broken_time)
657 curtime = vrtc_curtime(vrtc, &basetime);
658 KASSERT(curtime == vrtc->base_rtctime, ("%s: mismatch "
659 "between vrtc basetime (%#lx) and curtime (%#lx)",
660 __func__, vrtc->base_rtctime, curtime));
663 * Force a refresh of the RTC date/time fields so
664 * they reflect the time right before the guest set
667 secs_to_rtc(curtime, vrtc, 1);
670 * Updates are halted so mark 'base_rtctime' to denote
671 * that the RTC date/time is in flux.
673 rtctime = VRTC_BROKEN_TIME;
674 rtc->reg_b &= ~RTCSB_UINTR;
676 error = vrtc_time_update(vrtc, rtctime, basetime);
677 KASSERT(error == 0, ("vrtc_time_update error %d", error));
681 * Side effect of changes to the interrupt enable bits.
683 if (changed & RTCSB_ALL_INTRS)
684 vrtc_set_reg_c(vrtc, vrtc->rtcdev.reg_c);
687 * Change the callout frequency if it has changed.
689 newfreq = vrtc_freq(vrtc);
690 if (newfreq != oldfreq)
691 vrtc_callout_reset(vrtc, newfreq);
693 vrtc_callout_check(vrtc, newfreq);
696 * The side effect of bits that control the RTC date/time format
697 * is handled lazily when those fields are actually read.
703 vrtc_set_reg_a(struct vrtc *vrtc, uint8_t newval)
705 sbintime_t oldfreq, newfreq;
706 uint8_t oldval, changed;
708 KASSERT(VRTC_LOCKED(vrtc), ("%s: vrtc not locked", __func__));
710 newval &= ~RTCSA_TUP;
711 oldval = vrtc->rtcdev.reg_a;
712 oldfreq = vrtc_freq(vrtc);
714 if (divider_enabled(oldval) && !divider_enabled(newval)) {
715 VM_CTR2(vrtc->vm, "RTC divider held in reset at %#lx/%#lx",
716 vrtc->base_rtctime, vrtc->base_uptime);
717 } else if (!divider_enabled(oldval) && divider_enabled(newval)) {
719 * If the dividers are coming out of reset then update
720 * 'base_uptime' before this happens. This is done to
721 * maintain the illusion that the RTC date/time was frozen
722 * while the dividers were disabled.
724 vrtc->base_uptime = sbinuptime();
725 VM_CTR2(vrtc->vm, "RTC divider out of reset at %#lx/%#lx",
726 vrtc->base_rtctime, vrtc->base_uptime);
731 vrtc->rtcdev.reg_a = newval;
732 changed = oldval ^ newval;
734 VM_CTR2(vrtc->vm, "RTC reg_a changed from %#x to %#x",
739 * Side effect of changes to rate select and divider enable bits.
741 newfreq = vrtc_freq(vrtc);
742 if (newfreq != oldfreq)
743 vrtc_callout_reset(vrtc, newfreq);
745 vrtc_callout_check(vrtc, newfreq);
749 vrtc_set_time(struct vm *vm, time_t secs)
756 error = vrtc_time_update(vrtc, secs, sbinuptime());
760 VM_CTR2(vrtc->vm, "Error %d setting RTC time to %#lx", error,
763 VM_CTR1(vrtc->vm, "RTC time set to %#lx", secs);
770 vrtc_get_time(struct vm *vm)
778 t = vrtc_curtime(vrtc, &basetime);
785 vrtc_nvram_write(struct vm *vm, int offset, uint8_t value)
793 * Don't allow writes to RTC control registers or the date/time fields.
795 if (offset < offsetof(struct rtcdev, nvram[0]) ||
796 offset == RTC_CENTURY || offset >= sizeof(struct rtcdev)) {
797 VM_CTR1(vrtc->vm, "RTC nvram write to invalid offset %d",
803 ptr = (uint8_t *)(&vrtc->rtcdev);
805 VM_CTR2(vrtc->vm, "RTC nvram write %#x to offset %#x", value, offset);
812 vrtc_nvram_read(struct vm *vm, int offset, uint8_t *retval)
820 * Allow all offsets in the RTC to be read.
822 if (offset < 0 || offset >= sizeof(struct rtcdev))
829 * Update RTC date/time fields if necessary.
831 if (offset < 10 || offset == RTC_CENTURY) {
832 curtime = vrtc_curtime(vrtc, &basetime);
833 secs_to_rtc(curtime, vrtc, 0);
836 ptr = (uint8_t *)(&vrtc->rtcdev);
837 *retval = ptr[offset];
844 vrtc_addr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
860 vrtc->addr = *val & 0x7f;
867 vrtc_data_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
884 if (offset >= sizeof(struct rtcdev)) {
890 curtime = vrtc_curtime(vrtc, &basetime);
891 vrtc_time_update(vrtc, curtime, basetime);
894 * Update RTC date/time fields if necessary.
896 * This is not just for reads of the RTC. The side-effect of writing
897 * the century byte requires other RTC date/time fields (e.g. sec)
898 * to be updated here.
900 if (offset < 10 || offset == RTC_CENTURY)
901 secs_to_rtc(curtime, vrtc, 0);
907 * reg_c interrupt flags are updated only if the
908 * corresponding interrupt enable bit in reg_b is set.
910 *val = vrtc->rtcdev.reg_c;
911 vrtc_set_reg_c(vrtc, 0);
913 *val = *((uint8_t *)rtc + offset);
915 VCPU_CTR2(vm, vcpuid, "Read value %#x from RTC offset %#x",
920 VCPU_CTR1(vm, vcpuid, "RTC reg_a set to %#x", *val);
921 vrtc_set_reg_a(vrtc, *val);
924 VCPU_CTR1(vm, vcpuid, "RTC reg_b set to %#x", *val);
925 error = vrtc_set_reg_b(vrtc, *val);
928 VCPU_CTR1(vm, vcpuid, "RTC reg_c set to %#x (ignored)",
932 VCPU_CTR1(vm, vcpuid, "RTC reg_d set to %#x (ignored)",
937 * High order bit of 'seconds' is readonly.
942 VCPU_CTR2(vm, vcpuid, "RTC offset %#x set to %#x",
944 *((uint8_t *)rtc + offset) = *val;
949 * XXX some guests (e.g. OpenBSD) write the century byte
950 * outside of RTCSB_HALT so re-calculate the RTC date/time.
952 if (offset == RTC_CENTURY && !rtc_halted(vrtc)) {
953 curtime = rtc_to_secs(vrtc);
954 error = vrtc_time_update(vrtc, curtime, sbinuptime());
955 KASSERT(!error, ("vrtc_time_update error %d", error));
956 if (curtime == VRTC_BROKEN_TIME && rtc_flag_broken_time)
965 vrtc_reset(struct vrtc *vrtc)
972 vrtc_set_reg_b(vrtc, rtc->reg_b & ~(RTCSB_ALL_INTRS | RTCSB_SQWE));
973 vrtc_set_reg_c(vrtc, 0);
974 KASSERT(!callout_active(&vrtc->callout), ("rtc callout still active"));
980 vrtc_init(struct vm *vm)
986 vrtc = malloc(sizeof(struct vrtc), M_VRTC, M_WAITOK | M_ZERO);
988 mtx_init(&vrtc->mtx, "vrtc lock", NULL, MTX_DEF);
989 callout_init(&vrtc->callout, 1);
991 /* Allow dividers to keep time but disable everything else */
994 rtc->reg_b = RTCSB_24HR;
996 rtc->reg_d = RTCSD_PWR;
998 /* Reset the index register to a safe value. */
999 vrtc->addr = RTC_STATUSD;
1002 * Initialize RTC time to 00:00:00 Jan 1, 1970.
1007 vrtc->base_rtctime = VRTC_BROKEN_TIME;
1008 vrtc_time_update(vrtc, curtime, sbinuptime());
1009 secs_to_rtc(curtime, vrtc, 0);
1016 vrtc_cleanup(struct vrtc *vrtc)
1019 callout_drain(&vrtc->callout);