1/*-
2 * Copyright (c) 2012 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/11.0/usr.sbin/bhyve/spinup_ap.c 263432 2014-03-20 18:15:37Z neel $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: releng/11.0/usr.sbin/bhyve/spinup_ap.c 263432 2014-03-20 18:15:37Z neel $");
31
32#include <sys/param.h>
33#include <sys/types.h>
34
35#include <machine/vmm.h>
36#include <vmmapi.h>
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <assert.h>
41
42#include "bhyverun.h"
43#include "spinup_ap.h"
44
45static void
46spinup_ap_realmode(struct vmctx *ctx, int newcpu, uint64_t *rip)
47{
48	int vector, error;
49	uint16_t cs;
50	uint64_t desc_base;
51	uint32_t desc_limit, desc_access;
52
53	vector = *rip >> PAGE_SHIFT;
54	*rip = 0;
55
56	/*
57	 * Update the %cs and %rip of the guest so that it starts
58	 * executing real mode code at at 'vector << 12'.
59	 */
60	error = vm_set_register(ctx, newcpu, VM_REG_GUEST_RIP, *rip);
61	assert(error == 0);
62
63	error = vm_get_desc(ctx, newcpu, VM_REG_GUEST_CS, &desc_base,
64			    &desc_limit, &desc_access);
65	assert(error == 0);
66
67	desc_base = vector << PAGE_SHIFT;
68	error = vm_set_desc(ctx, newcpu, VM_REG_GUEST_CS,
69			    desc_base, desc_limit, desc_access);
70	assert(error == 0);
71
72	cs = (vector << PAGE_SHIFT) >> 4;
73	error = vm_set_register(ctx, newcpu, VM_REG_GUEST_CS, cs);
74	assert(error == 0);
75}
76
77int
78spinup_ap(struct vmctx *ctx, int vcpu, int newcpu, uint64_t rip)
79{
80	int error;
81
82	assert(newcpu != 0);
83	assert(newcpu < guest_ncpus);
84
85	error = vcpu_reset(ctx, newcpu);
86	assert(error == 0);
87
88	fbsdrun_set_capabilities(ctx, newcpu);
89
90	/*
91	 * Enable the 'unrestricted guest' mode for 'newcpu'.
92	 *
93	 * Set up the processor state in power-on 16-bit mode, with the CS:IP
94	 * init'd to the specified low-mem 4K page.
95	 */
96	error = vm_set_capability(ctx, newcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
97	assert(error == 0);
98
99	spinup_ap_realmode(ctx, newcpu, &rip);
100
101	fbsdrun_addcpu(ctx, vcpu, newcpu, rip);
102
103	return (newcpu);
104}
105