1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
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, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 
31 #include <machine/specialreg.h>
32 #include <machine/segments.h>
33 #include <machine/vmm.h>
34 
35 #include <errno.h>
36 #include <string.h>
37 
38 #include "vmmapi.h"
39 #include "internal.h"
40 
41 #define	I386_TSS_SIZE		104
42 
43 #define	DESC_PRESENT		0x00000080
44 #define	DESC_LONGMODE		0x00002000
45 #define	DESC_DEF32		0x00004000
46 #define	DESC_GRAN		0x00008000
47 #define	DESC_UNUSABLE		0x00010000
48 
49 #define	GUEST_NULL_SEL		0
50 #define	GUEST_CODE_SEL		1
51 #define	GUEST_DATA_SEL		2
52 #define	GUEST_TSS_SEL		3
53 #define	GUEST_GDTR_LIMIT64	(3 * 8 - 1)
54 
55 static struct segment_descriptor i386_gdt[] = {
56 	{},						/* NULL */
57 	{ .sd_lolimit = 0xffff, .sd_type = SDT_MEMER,	/* CODE */
58 	  .sd_p = 1, .sd_hilimit = 0xf, .sd_def32 = 1, .sd_gran = 1 },
59 	{ .sd_lolimit = 0xffff, .sd_type = SDT_MEMRW,	/* DATA */
60 	  .sd_p = 1, .sd_hilimit = 0xf, .sd_def32 = 1, .sd_gran = 1 },
61 	{ .sd_lolimit = I386_TSS_SIZE - 1,		/* TSS */
62 	  .sd_type = SDT_SYS386TSS, .sd_p = 1 }
63 };
64 
65 /*
66  * Setup the 'vcpu' register set such that it will begin execution at
67  * 'eip' in flat mode.
68  */
69 int
70 vm_setup_freebsd_registers_i386(struct vcpu *vcpu, uint32_t eip,
71 				uint32_t gdtbase, uint32_t esp)
72 {
73 	uint64_t cr0, rflags, desc_base;
74 	uint32_t desc_access, desc_limit, tssbase;
75 	uint16_t gsel;
76 	struct segment_descriptor *gdt;
77 	int error, tmp;
78 
79 	/* A 32-bit guest requires unrestricted mode. */
80 	error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
81 	if (error)
82 		goto done;
83 	error = vm_set_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
84 	if (error)
85 		goto done;
86 
87 	cr0 = CR0_PE | CR0_NE;
88 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
89 		goto done;
90 
91 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CR4, 0)) != 0)
92 		goto done;
93 
94 	/*
95 	 * Forcing EFER to 0 causes bhyve to clear the "IA-32e guest
96 	 * mode" entry control.
97 	 */
98 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_EFER, 0)))
99 		goto done;
100 
101 	gdt = vm_map_gpa(vcpu->ctx, gdtbase, 0x1000);
102 	if (gdt == NULL)
103 		return (EFAULT);
104 	memcpy(gdt, i386_gdt, sizeof(i386_gdt));
105 	desc_base = gdtbase;
106 	desc_limit = sizeof(i386_gdt) - 1;
107 	error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR,
108 			    desc_base, desc_limit, 0);
109 	if (error != 0)
110 		goto done;
111 
112 	/* Place the TSS one page above the GDT. */
113 	tssbase = gdtbase + 0x1000;
114 	gdt[3].sd_lobase = tssbase;
115 
116 	rflags = 0x2;
117 	error = vm_set_register(vcpu, VM_REG_GUEST_RFLAGS, rflags);
118 	if (error)
119 		goto done;
120 
121 	desc_base = 0;
122 	desc_limit = 0xffffffff;
123 	desc_access = DESC_GRAN | DESC_DEF32 | DESC_PRESENT | SDT_MEMERA;
124 	error = vm_set_desc(vcpu, VM_REG_GUEST_CS,
125 			    desc_base, desc_limit, desc_access);
126 
127 	desc_access = DESC_GRAN | DESC_DEF32 | DESC_PRESENT | SDT_MEMRWA;
128 	error = vm_set_desc(vcpu, VM_REG_GUEST_DS,
129 			    desc_base, desc_limit, desc_access);
130 	if (error)
131 		goto done;
132 
133 	error = vm_set_desc(vcpu, VM_REG_GUEST_ES,
134 			    desc_base, desc_limit, desc_access);
135 	if (error)
136 		goto done;
137 
138 	error = vm_set_desc(vcpu, VM_REG_GUEST_FS,
139 			    desc_base, desc_limit, desc_access);
140 	if (error)
141 		goto done;
142 
143 	error = vm_set_desc(vcpu, VM_REG_GUEST_GS,
144 			    desc_base, desc_limit, desc_access);
145 	if (error)
146 		goto done;
147 
148 	error = vm_set_desc(vcpu, VM_REG_GUEST_SS,
149 			    desc_base, desc_limit, desc_access);
150 	if (error)
151 		goto done;
152 
153 	desc_base = tssbase;
154 	desc_limit = I386_TSS_SIZE - 1;
155 	desc_access = DESC_PRESENT | SDT_SYS386BSY;
156 	error = vm_set_desc(vcpu, VM_REG_GUEST_TR,
157 			    desc_base, desc_limit, desc_access);
158 	if (error)
159 		goto done;
160 
161 
162 	error = vm_set_desc(vcpu, VM_REG_GUEST_LDTR, 0, 0,
163 			    DESC_UNUSABLE);
164 	if (error)
165 		goto done;
166 
167 	gsel = GSEL(GUEST_CODE_SEL, SEL_KPL);
168 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CS, gsel)) != 0)
169 		goto done;
170 
171 	gsel = GSEL(GUEST_DATA_SEL, SEL_KPL);
172 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_DS, gsel)) != 0)
173 		goto done;
174 
175 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_ES, gsel)) != 0)
176 		goto done;
177 
178 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_FS, gsel)) != 0)
179 		goto done;
180 
181 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_GS, gsel)) != 0)
182 		goto done;
183 
184 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_SS, gsel)) != 0)
185 		goto done;
186 
187 	gsel = GSEL(GUEST_TSS_SEL, SEL_KPL);
188 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_TR, gsel)) != 0)
189 		goto done;
190 
191 	/* LDTR is pointing to the null selector */
192 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
193 		goto done;
194 
195 	/* entry point */
196 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_RIP, eip)) != 0)
197 		goto done;
198 
199 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_RSP, esp)) != 0)
200 		goto done;
201 
202 	error = 0;
203 done:
204 	return (error);
205 }
206 
207 void
208 vm_setup_freebsd_gdt(uint64_t *gdtr)
209 {
210 	gdtr[GUEST_NULL_SEL] = 0;
211 	gdtr[GUEST_CODE_SEL] = 0x0020980000000000;
212 	gdtr[GUEST_DATA_SEL] = 0x0000900000000000;
213 }
214 
215 /*
216  * Setup the 'vcpu' register set such that it will begin execution at
217  * 'rip' in long mode.
218  */
219 int
220 vm_setup_freebsd_registers(struct vcpu *vcpu,
221 			   uint64_t rip, uint64_t cr3, uint64_t gdtbase,
222 			   uint64_t rsp)
223 {
224 	int error;
225 	uint64_t cr0, cr4, efer, rflags, desc_base;
226 	uint32_t desc_access, desc_limit;
227 	uint16_t gsel;
228 
229 	cr0 = CR0_PE | CR0_PG | CR0_NE;
230 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
231 		goto done;
232 
233 	cr4 = CR4_PAE;
234 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
235 		goto done;
236 
237 	efer = EFER_LME | EFER_LMA;
238 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_EFER, efer)))
239 		goto done;
240 
241 	rflags = 0x2;
242 	error = vm_set_register(vcpu, VM_REG_GUEST_RFLAGS, rflags);
243 	if (error)
244 		goto done;
245 
246 	desc_base = 0;
247 	desc_limit = 0;
248 	desc_access = 0x0000209B;
249 	error = vm_set_desc(vcpu, VM_REG_GUEST_CS,
250 			    desc_base, desc_limit, desc_access);
251 	if (error)
252 		goto done;
253 
254 	desc_access = 0x00000093;
255 	error = vm_set_desc(vcpu, VM_REG_GUEST_DS,
256 			    desc_base, desc_limit, desc_access);
257 	if (error)
258 		goto done;
259 
260 	error = vm_set_desc(vcpu, VM_REG_GUEST_ES,
261 			    desc_base, desc_limit, desc_access);
262 	if (error)
263 		goto done;
264 
265 	error = vm_set_desc(vcpu, VM_REG_GUEST_FS,
266 			    desc_base, desc_limit, desc_access);
267 	if (error)
268 		goto done;
269 
270 	error = vm_set_desc(vcpu, VM_REG_GUEST_GS,
271 			    desc_base, desc_limit, desc_access);
272 	if (error)
273 		goto done;
274 
275 	error = vm_set_desc(vcpu, VM_REG_GUEST_SS,
276 			    desc_base, desc_limit, desc_access);
277 	if (error)
278 		goto done;
279 
280 	/*
281 	 * XXX TR is pointing to null selector even though we set the
282 	 * TSS segment to be usable with a base address and limit of 0.
283 	 */
284 	desc_access = 0x0000008b;
285 	error = vm_set_desc(vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
286 	if (error)
287 		goto done;
288 
289 	error = vm_set_desc(vcpu, VM_REG_GUEST_LDTR, 0, 0,
290 			    DESC_UNUSABLE);
291 	if (error)
292 		goto done;
293 
294 	gsel = GSEL(GUEST_CODE_SEL, SEL_KPL);
295 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CS, gsel)) != 0)
296 		goto done;
297 
298 	gsel = GSEL(GUEST_DATA_SEL, SEL_KPL);
299 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_DS, gsel)) != 0)
300 		goto done;
301 
302 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_ES, gsel)) != 0)
303 		goto done;
304 
305 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_FS, gsel)) != 0)
306 		goto done;
307 
308 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_GS, gsel)) != 0)
309 		goto done;
310 
311 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_SS, gsel)) != 0)
312 		goto done;
313 
314 	/* XXX TR is pointing to the null selector */
315 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_TR, 0)) != 0)
316 		goto done;
317 
318 	/* LDTR is pointing to the null selector */
319 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
320 		goto done;
321 
322 	/* entry point */
323 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_RIP, rip)) != 0)
324 		goto done;
325 
326 	/* page table base */
327 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_CR3, cr3)) != 0)
328 		goto done;
329 
330 	desc_base = gdtbase;
331 	desc_limit = GUEST_GDTR_LIMIT64;
332 	error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR,
333 			    desc_base, desc_limit, 0);
334 	if (error != 0)
335 		goto done;
336 
337 	if ((error = vm_set_register(vcpu, VM_REG_GUEST_RSP, rsp)) != 0)
338 		goto done;
339 
340 	error = 0;
341 done:
342 	return (error);
343 }
344