1*5c4a5fe1SAndy Fiddaman /*-
2*5c4a5fe1SAndy Fiddaman * SPDX-License-Identifier: BSD-2-Clause
3*5c4a5fe1SAndy Fiddaman *
4*5c4a5fe1SAndy Fiddaman * Copyright (c) 2012 NetApp, Inc.
5*5c4a5fe1SAndy Fiddaman * All rights reserved.
6*5c4a5fe1SAndy Fiddaman *
7*5c4a5fe1SAndy Fiddaman * Redistribution and use in source and binary forms, with or without
8*5c4a5fe1SAndy Fiddaman * modification, are permitted provided that the following conditions
9*5c4a5fe1SAndy Fiddaman * are met:
10*5c4a5fe1SAndy Fiddaman * 1. Redistributions of source code must retain the above copyright
11*5c4a5fe1SAndy Fiddaman * notice, this list of conditions and the following disclaimer.
12*5c4a5fe1SAndy Fiddaman * 2. Redistributions in binary form must reproduce the above copyright
13*5c4a5fe1SAndy Fiddaman * notice, this list of conditions and the following disclaimer in the
14*5c4a5fe1SAndy Fiddaman * documentation and/or other materials provided with the distribution.
15*5c4a5fe1SAndy Fiddaman *
16*5c4a5fe1SAndy Fiddaman * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17*5c4a5fe1SAndy Fiddaman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*5c4a5fe1SAndy Fiddaman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*5c4a5fe1SAndy Fiddaman * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20*5c4a5fe1SAndy Fiddaman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*5c4a5fe1SAndy Fiddaman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*5c4a5fe1SAndy Fiddaman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*5c4a5fe1SAndy Fiddaman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*5c4a5fe1SAndy Fiddaman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*5c4a5fe1SAndy Fiddaman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*5c4a5fe1SAndy Fiddaman * SUCH DAMAGE.
27*5c4a5fe1SAndy Fiddaman */
28*5c4a5fe1SAndy Fiddaman /*
29*5c4a5fe1SAndy Fiddaman * This file and its contents are supplied under the terms of the
30*5c4a5fe1SAndy Fiddaman * Common Development and Distribution License ("CDDL"), version 1.0.
31*5c4a5fe1SAndy Fiddaman * You may only use this file in accordance with the terms of version
32*5c4a5fe1SAndy Fiddaman * 1.0 of the CDDL.
33*5c4a5fe1SAndy Fiddaman *
34*5c4a5fe1SAndy Fiddaman * A full copy of the text of the CDDL should have accompanied this
35*5c4a5fe1SAndy Fiddaman * source. A copy of the CDDL is also available via the Internet at
36*5c4a5fe1SAndy Fiddaman * http://www.illumos.org/license/CDDL.
37*5c4a5fe1SAndy Fiddaman *
38*5c4a5fe1SAndy Fiddaman * Copyright 2020 Oxide Computer Company
39*5c4a5fe1SAndy Fiddaman */
40*5c4a5fe1SAndy Fiddaman
41*5c4a5fe1SAndy Fiddaman
42*5c4a5fe1SAndy Fiddaman #include <sys/param.h>
43*5c4a5fe1SAndy Fiddaman #include <sys/types.h>
44*5c4a5fe1SAndy Fiddaman
45*5c4a5fe1SAndy Fiddaman #include <machine/vmm.h>
46*5c4a5fe1SAndy Fiddaman #include <vmmapi.h>
47*5c4a5fe1SAndy Fiddaman
48*5c4a5fe1SAndy Fiddaman #include <stdio.h>
49*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
50*5c4a5fe1SAndy Fiddaman #include <assert.h>
51*5c4a5fe1SAndy Fiddaman
52*5c4a5fe1SAndy Fiddaman #include "bhyverun.h"
53*5c4a5fe1SAndy Fiddaman #include "spinup_ap.h"
54*5c4a5fe1SAndy Fiddaman
55*5c4a5fe1SAndy Fiddaman #ifdef __FreeBSD__
56*5c4a5fe1SAndy Fiddaman static void
spinup_ap_realmode(struct vcpu * newcpu,uint64_t rip)57*5c4a5fe1SAndy Fiddaman spinup_ap_realmode(struct vcpu *newcpu, uint64_t rip)
58*5c4a5fe1SAndy Fiddaman {
59*5c4a5fe1SAndy Fiddaman int vector, error;
60*5c4a5fe1SAndy Fiddaman uint16_t cs;
61*5c4a5fe1SAndy Fiddaman uint64_t desc_base;
62*5c4a5fe1SAndy Fiddaman uint32_t desc_limit, desc_access;
63*5c4a5fe1SAndy Fiddaman
64*5c4a5fe1SAndy Fiddaman vector = rip >> PAGE_SHIFT;
65*5c4a5fe1SAndy Fiddaman
66*5c4a5fe1SAndy Fiddaman /*
67*5c4a5fe1SAndy Fiddaman * Update the %cs and %rip of the guest so that it starts
68*5c4a5fe1SAndy Fiddaman * executing real mode code at 'vector << 12'.
69*5c4a5fe1SAndy Fiddaman */
70*5c4a5fe1SAndy Fiddaman error = vm_set_register(newcpu, VM_REG_GUEST_RIP, 0);
71*5c4a5fe1SAndy Fiddaman assert(error == 0);
72*5c4a5fe1SAndy Fiddaman
73*5c4a5fe1SAndy Fiddaman error = vm_get_desc(newcpu, VM_REG_GUEST_CS, &desc_base,
74*5c4a5fe1SAndy Fiddaman &desc_limit, &desc_access);
75*5c4a5fe1SAndy Fiddaman assert(error == 0);
76*5c4a5fe1SAndy Fiddaman
77*5c4a5fe1SAndy Fiddaman desc_base = vector << PAGE_SHIFT;
78*5c4a5fe1SAndy Fiddaman error = vm_set_desc(newcpu, VM_REG_GUEST_CS,
79*5c4a5fe1SAndy Fiddaman desc_base, desc_limit, desc_access);
80*5c4a5fe1SAndy Fiddaman assert(error == 0);
81*5c4a5fe1SAndy Fiddaman
82*5c4a5fe1SAndy Fiddaman cs = (vector << PAGE_SHIFT) >> 4;
83*5c4a5fe1SAndy Fiddaman error = vm_set_register(newcpu, VM_REG_GUEST_CS, cs);
84*5c4a5fe1SAndy Fiddaman assert(error == 0);
85*5c4a5fe1SAndy Fiddaman }
86*5c4a5fe1SAndy Fiddaman #endif /* __FreeBSD__ */
87*5c4a5fe1SAndy Fiddaman
88*5c4a5fe1SAndy Fiddaman void
spinup_ap(struct vcpu * newcpu,uint64_t rip)89*5c4a5fe1SAndy Fiddaman spinup_ap(struct vcpu *newcpu, uint64_t rip)
90*5c4a5fe1SAndy Fiddaman {
91*5c4a5fe1SAndy Fiddaman int error;
92*5c4a5fe1SAndy Fiddaman
93*5c4a5fe1SAndy Fiddaman error = vcpu_reset(newcpu);
94*5c4a5fe1SAndy Fiddaman assert(error == 0);
95*5c4a5fe1SAndy Fiddaman
96*5c4a5fe1SAndy Fiddaman #ifdef __FreeBSD__
97*5c4a5fe1SAndy Fiddaman spinup_ap_realmode(newcpu, rip);
98*5c4a5fe1SAndy Fiddaman
99*5c4a5fe1SAndy Fiddaman vm_resume_cpu(newcpu);
100*5c4a5fe1SAndy Fiddaman #endif
101*5c4a5fe1SAndy Fiddaman }
102