1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
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 *
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
79 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
80 {
81
82 free(vpmtmr, M_VPMTMR);
83 }
84
85 int
86 vpmtmr_handler(struct vm *vm, bool in, int port, int bytes, uint32_t *val)
87 {
88 struct vpmtmr *vpmtmr;
89 sbintime_t now, delta;
90
91 if (!in || bytes != 4)
92 return (-1);
93
94 vpmtmr = vm_pmtmr(vm);
95
96 /*
97 * No locking needed because 'baseuptime' and 'baseval' are
98 * written only during initialization.
99 */
100 now = sbinuptime();
101 delta = now - vpmtmr->baseuptime;
102 KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
103 "%#lx to %#lx", vpmtmr->baseuptime, now));
104 *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
105
106 return (0);
107 }
108
109 #ifdef BHYVE_SNAPSHOT
110 int
111 vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)
112 {
113 int ret;
114
115 SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);
116
117 done:
118 return (ret);
119 }
120 #endif
Cache object: ed766413dc325a8d3d4f85f6e95b5602
|