xref: /freebsd/usr.sbin/bhyve/vmgenc.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2020 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/mman.h>
30 #include <sys/uuid.h>
31 
32 #include <assert.h>
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <stdbool.h>
42 #include <unistd.h>
43 
44 #include <machine/vmm.h>
45 #include <vmmapi.h>
46 
47 #include "acpi.h"
48 #include "bootrom.h"
49 #include "vmgenc.h"
50 
51 static uint64_t	vmgen_gpa;
52 
53 void
54 vmgenc_init(struct vmctx *ctx)
55 {
56 	char *region;
57 	int error;
58 
59 	error = bootrom_alloc(ctx, PAGE_SIZE, PROT_READ, 0, &region,
60 	    &vmgen_gpa);
61 	if (error != 0)
62 		errx(4, "%s: bootrom_alloc", __func__);
63 
64 	/*
65 	 * It is basically harmless to always generate a random ID when
66 	 * starting a VM.
67 	 */
68 	error = getentropy(region, sizeof(struct uuid));
69 	if (error == -1)
70 		err(4, "%s: getentropy", __func__);
71 
72 	/* XXX When we have suspend/resume/rollback. */
73 #if 0
74 	acpi_raise_gpe(ctx, GPE_VMGENC);
75 #endif
76 }
77 
78 void
79 vmgenc_write_dsdt(void)
80 {
81 	dsdt_line("");
82 	dsdt_indent(1);
83 	dsdt_line("Scope (_SB)");
84 	dsdt_line("{");
85 
86 	dsdt_line("  Device (GENC)");
87 	dsdt_line("  {");
88 
89 	dsdt_indent(2);
90 	dsdt_line("Name (_CID, \"VM_Gen_Counter\")");
91 	dsdt_line("Method (_HID, 0, NotSerialized)");
92 	dsdt_line("{");
93 	dsdt_line("  Return (\"Bhyve_V_Gen_Counter_V1\")");
94 	dsdt_line("}");
95 	dsdt_line("Name (_UID, 0)");
96 	dsdt_line("Name (_DDN, \"VM_Gen_Counter\")");
97 	dsdt_line("Name (ADDR, Package (0x02)");
98 	dsdt_line("{");
99 	dsdt_line("  0x%08x,", (uint32_t)vmgen_gpa);
100 	dsdt_line("  0x%08x", (uint32_t)(vmgen_gpa >> 32));
101 	dsdt_line("})");
102 
103 	dsdt_unindent(2);
104 	dsdt_line("  }");	/* Device (GENC) */
105 
106 	dsdt_line("}");		/* Scope (_SB) */
107 	dsdt_line("");
108 
109 	dsdt_line("Scope (_GPE)");
110 	dsdt_line("{");
111 	dsdt_line("  Method (_E%02x, 0, NotSerialized)", GPE_VMGENC);
112 	dsdt_line("  {");
113 	dsdt_line("    Notify (\\_SB.GENC, 0x80)");
114 	dsdt_line("  }");
115 	dsdt_line("}");
116 	dsdt_unindent(1);
117 }
118