1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014, Neel Natu ([email protected])
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
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.
16 *
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.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_bhyve_snapshot.h"
33
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39
40 #include <machine/vmm.h>
41 #include <machine/vmm_snapshot.h>
42
43 #include "vpmtmr.h"
44
45 /*
46 * The ACPI Power Management timer is a free-running 24- or 32-bit
47 * timer with a frequency of 3.579545MHz
48 *
49 * This implementation will be 32-bits
50 */
51
52 #define PMTMR_FREQ 3579545 /* 3.579545MHz */
53
54 struct vpmtmr {
55 sbintime_t freq_sbt;
56 sbintime_t baseuptime;
57 uint32_t baseval;
58 };
59
60 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
61
62 struct vpmtmr *
vpmtmr_init(struct vm * vm)63 vpmtmr_init(struct vm *vm)
64 {
65 struct vpmtmr *vpmtmr;
66 struct bintime bt;
67
68 vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
69 vpmtmr->baseuptime = sbinuptime();
70 vpmtmr->baseval = 0;
71
72 FREQ2BT(PMTMR_FREQ, &bt);
73 vpmtmr->freq_sbt = bttosbt(bt);
74
75 return (vpmtmr);
76 }
77
78 void
vpmtmr_cleanup(struct vpmtmr * vpmtmr)79 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
80 {
81
82 free(vpmtmr, M_VPMTMR);
83 }
84
85 int
vpmtmr_handler(struct vm * vm,int vcpuid,bool in,int port,int bytes,uint32_t * val)86 vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
87 uint32_t *val)
88 {
89 struct vpmtmr *vpmtmr;
90 sbintime_t now, delta;
91
92 if (!in || bytes != 4)
93 return (-1);
94
95 vpmtmr = vm_pmtmr(vm);
96
97 /*
98 * No locking needed because 'baseuptime' and 'baseval' are
99 * written only during initialization.
100 */
101 now = sbinuptime();
102 delta = now - vpmtmr->baseuptime;
103 KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
104 "%#lx to %#lx", vpmtmr->baseuptime, now));
105 *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
106
107 return (0);
108 }
109
110 #ifdef BHYVE_SNAPSHOT
111 int
vpmtmr_snapshot(struct vpmtmr * vpmtmr,struct vm_snapshot_meta * meta)112 vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)
113 {
114 int ret;
115
116 SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);
117
118 done:
119 return (ret);
120 }
121 #endif
122