xref: /freebsd/sys/xen/xen_common.c (revision c7368ccb68018c318f2320de2ff3c2b15127c8d6)
16d765bffSElliott Mitchell /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
36d765bffSElliott Mitchell  *
46d765bffSElliott Mitchell  * Copyright © 2008, 2013 Citrix Systems, Inc.
56d765bffSElliott Mitchell  * Copyright © 2012 Spectra Logic Corporation
66d765bffSElliott Mitchell  * Copyright © 2021 Elliott Mitchell
76d765bffSElliott Mitchell  * All rights reserved.
86d765bffSElliott Mitchell  *
96d765bffSElliott Mitchell  * Redistribution and use in source and binary forms, with or without
106d765bffSElliott Mitchell  * modification, are permitted provided that the following conditions
116d765bffSElliott Mitchell  * are met:
126d765bffSElliott Mitchell  * 1. Redistributions of source code must retain the above copyright
136d765bffSElliott Mitchell  *    notice, this list of conditions and the following disclaimer.
146d765bffSElliott Mitchell  * 2. Redistributions in binary form must reproduce the above copyright
156d765bffSElliott Mitchell  *    notice, this list of conditions and the following disclaimer in the
166d765bffSElliott Mitchell  *    documentation and/or other materials provided with the distribution.
176d765bffSElliott Mitchell  *
186d765bffSElliott Mitchell  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
196d765bffSElliott Mitchell  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206d765bffSElliott Mitchell  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
216d765bffSElliott Mitchell  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
226d765bffSElliott Mitchell  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
236d765bffSElliott Mitchell  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
246d765bffSElliott Mitchell  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
256d765bffSElliott Mitchell  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
266d765bffSElliott Mitchell  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
276d765bffSElliott Mitchell  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286d765bffSElliott Mitchell  * SUCH DAMAGE.
296d765bffSElliott Mitchell  */
306d765bffSElliott Mitchell 
316d765bffSElliott Mitchell #include <sys/param.h> /* required by xen/xen-os.h */
326d765bffSElliott Mitchell 
33*20fc5bf7SElliott Mitchell #include <vm/vm.h>
34*20fc5bf7SElliott Mitchell #include <vm/pmap.h>
35*20fc5bf7SElliott Mitchell 
366d765bffSElliott Mitchell #include <machine/atomic.h> /* required by xen/xen-os.h */
376d765bffSElliott Mitchell 
386d765bffSElliott Mitchell #include <xen/xen-os.h>
396d765bffSElliott Mitchell #include <xen/hvm.h>
406d765bffSElliott Mitchell 
41*20fc5bf7SElliott Mitchell #include <contrib/xen/vcpu.h>
42*20fc5bf7SElliott Mitchell 
436d765bffSElliott Mitchell /*-------------------------------- Global Data -------------------------------*/
446d765bffSElliott Mitchell /**
456d765bffSElliott Mitchell  * Start info flags. ATM this only used to store the initial domain flag for
466d765bffSElliott Mitchell  * PVHv2, and it's always empty for HVM guests.
476d765bffSElliott Mitchell  */
486d765bffSElliott Mitchell uint32_t hvm_start_flags;
496d765bffSElliott Mitchell 
506d765bffSElliott Mitchell /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
516d765bffSElliott Mitchell shared_info_t *HYPERVISOR_shared_info;
52*20fc5bf7SElliott Mitchell 
53*20fc5bf7SElliott Mitchell /*------------------------------- Per-CPU Data -------------------------------*/
54*20fc5bf7SElliott Mitchell DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
55*20fc5bf7SElliott Mitchell 
56*20fc5bf7SElliott Mitchell void
xen_setup_vcpu_info(void)57*20fc5bf7SElliott Mitchell xen_setup_vcpu_info(void)
58*20fc5bf7SElliott Mitchell {
59*20fc5bf7SElliott Mitchell 	/* This isn't directly accessed outside this function */
60*20fc5bf7SElliott Mitchell 	DPCPU_DEFINE_STATIC(vcpu_info_t, vcpu_local_info)
61*20fc5bf7SElliott Mitchell 	    __attribute__((aligned(64)));
62*20fc5bf7SElliott Mitchell 
63*20fc5bf7SElliott Mitchell 	vcpu_info_t *vcpu_info = DPCPU_PTR(vcpu_local_info);
64*20fc5bf7SElliott Mitchell 	vcpu_register_vcpu_info_t info = {
65*20fc5bf7SElliott Mitchell 		.mfn = vtophys(vcpu_info) >> PAGE_SHIFT,
66*20fc5bf7SElliott Mitchell 		.offset = vtophys(vcpu_info) & PAGE_MASK,
67*20fc5bf7SElliott Mitchell 	};
68*20fc5bf7SElliott Mitchell 	unsigned int cpu = XEN_VCPUID();
69*20fc5bf7SElliott Mitchell 	int rc;
70*20fc5bf7SElliott Mitchell 
71*20fc5bf7SElliott Mitchell 	KASSERT(xen_domain(), ("%s(): invoked when not on Xen?", __func__));
72*20fc5bf7SElliott Mitchell 
73*20fc5bf7SElliott Mitchell 	_Static_assert(sizeof(struct vcpu_info) <= 64,
74*20fc5bf7SElliott Mitchell 	    "struct vcpu_info is larger than supported limit of 64 bytes");
75*20fc5bf7SElliott Mitchell 
76*20fc5bf7SElliott Mitchell 	/*
77*20fc5bf7SElliott Mitchell 	 * Set the vCPU info.
78*20fc5bf7SElliott Mitchell 	 *
79*20fc5bf7SElliott Mitchell 	 * NB: the vCPU info for some vCPUs can be fetched from the shared info
80*20fc5bf7SElliott Mitchell 	 * page, but in order to make sure the mapping code is correct always
81*20fc5bf7SElliott Mitchell 	 * attempt to map the vCPU info at a custom place.
82*20fc5bf7SElliott Mitchell 	 */
83*20fc5bf7SElliott Mitchell 	rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
84*20fc5bf7SElliott Mitchell 	if (rc == 0)
85*20fc5bf7SElliott Mitchell 		DPCPU_SET(vcpu_info, vcpu_info);
86*20fc5bf7SElliott Mitchell 	else if (cpu < nitems(HYPERVISOR_shared_info->vcpu_info)) {
87*20fc5bf7SElliott Mitchell 		static bool warned = false;
88*20fc5bf7SElliott Mitchell 
89*20fc5bf7SElliott Mitchell 		DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
90*20fc5bf7SElliott Mitchell 
91*20fc5bf7SElliott Mitchell 		if (bootverbose && !warned) {
92*20fc5bf7SElliott Mitchell 			warned = true;
93*20fc5bf7SElliott Mitchell 			printf(
94*20fc5bf7SElliott Mitchell 		"WARNING: Xen vCPU %u failed to setup vcpu_info rc = %d\n",
95*20fc5bf7SElliott Mitchell 			    cpu, rc);
96*20fc5bf7SElliott Mitchell 		}
97*20fc5bf7SElliott Mitchell 	} else
98*20fc5bf7SElliott Mitchell 		panic("Unable to register vCPU %u, rc=%d\n", cpu, rc);
99*20fc5bf7SElliott Mitchell }
100