summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/os/pc_hvm.c
blob: 60f8e8d3ca53717ff22245db410a8673efda619f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2018 Joyent, Inc.
 */

#include <sys/cmn_err.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mutex.h>
#include <sys/debug.h>

static kmutex_t hvm_excl_lock;
static const char *hvm_excl_holder = NULL;

/*
 * HVM Exclusion Interface
 *
 * To avoid VMX/SVM conflicts from arising when multiple hypervisor providers
 * (eg. KVM, bhyve) are shipped with the system, this simple advisory locking
 * system is presented for their use.  Until a proper hypervisor API, like the
 * one in OSX, is shipped in illumos, this will serve as opt-in regulation to
 * dictate that only a single hypervisor be allowed to configure the system and
 * run at any given time.
 */

boolean_t
hvm_excl_hold(const char *consumer)
{
	boolean_t res;

	mutex_enter(&hvm_excl_lock);
	if (hvm_excl_holder == NULL) {
		hvm_excl_holder = consumer;
		res = B_TRUE;
	} else {
		cmn_err(CE_WARN, "zone '%s' cannot take HVM exclusion lock as "
		    "'%s': held by '%s'", curproc->p_zone->zone_name, consumer,
		    hvm_excl_holder);
		res = B_FALSE;
	}
	mutex_exit(&hvm_excl_lock);

	return (res);
}

void
hvm_excl_rele(const char *consumer)
{
	mutex_enter(&hvm_excl_lock);
	VERIFY(consumer == hvm_excl_holder);
	hvm_excl_holder = NULL;
	mutex_exit(&hvm_excl_lock);
}