/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2014, Neel Natu ([email protected])4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice unmodified, this list of conditions, and the following11* disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/cdefs.h>29#include "opt_bhyve_snapshot.h"3031#include <sys/param.h>32#include <sys/queue.h>33#include <sys/kernel.h>34#include <sys/malloc.h>35#include <sys/systm.h>3637#include <machine/vmm.h>38#include <machine/vmm_snapshot.h>3940#include "vpmtmr.h"4142/*43* The ACPI Power Management timer is a free-running 24- or 32-bit44* timer with a frequency of 3.579545MHz45*46* This implementation will be 32-bits47*/4849#define PMTMR_FREQ 3579545 /* 3.579545MHz */5051struct vpmtmr {52sbintime_t freq_sbt;53sbintime_t baseuptime;54uint32_t baseval;55};5657static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");5859struct vpmtmr *60vpmtmr_init(struct vm *vm)61{62struct vpmtmr *vpmtmr;63struct bintime bt;6465vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);66vpmtmr->baseuptime = sbinuptime();67vpmtmr->baseval = 0;6869FREQ2BT(PMTMR_FREQ, &bt);70vpmtmr->freq_sbt = bttosbt(bt);7172return (vpmtmr);73}7475void76vpmtmr_cleanup(struct vpmtmr *vpmtmr)77{7879free(vpmtmr, M_VPMTMR);80}8182int83vpmtmr_handler(struct vm *vm, bool in, int port, int bytes, uint32_t *val)84{85struct vpmtmr *vpmtmr;86sbintime_t now, delta;8788if (!in || bytes != 4)89return (-1);9091vpmtmr = vm_pmtmr(vm);9293/*94* No locking needed because 'baseuptime' and 'baseval' are95* written only during initialization.96*/97now = sbinuptime();98delta = now - vpmtmr->baseuptime;99KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "100"%#lx to %#lx", vpmtmr->baseuptime, now));101*val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;102103return (0);104}105106#ifdef BHYVE_SNAPSHOT107int108vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)109{110int ret;111112SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);113114done:115return (ret);116}117#endif118119120