npt.c revision 259579
1249967Sneel/*-
2249967Sneel * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
3249967Sneel * All rights reserved.
4249967Sneel *
5249967Sneel * Redistribution and use in source and binary forms, with or without
6249967Sneel * modification, are permitted provided that the following conditions
7249967Sneel * are met:
8249967Sneel * 1. Redistributions of source code must retain the above copyright
9249967Sneel *    notice unmodified, this list of conditions, and the following
10249967Sneel *    disclaimer.
11249967Sneel * 2. Redistributions in binary form must reproduce the above copyright
12249967Sneel *    notice, this list of conditions and the following disclaimer in the
13249967Sneel *    documentation and/or other materials provided with the distribution.
14249967Sneel *
15249967Sneel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16249967Sneel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17249967Sneel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18249967Sneel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19249967Sneel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20249967Sneel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21249967Sneel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22249967Sneel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23249967Sneel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24249967Sneel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25249967Sneel */
26249967Sneel
27249967Sneel#include <sys/cdefs.h>
28249967Sneel__FBSDID("$FreeBSD: projects/bhyve_svm/sys/amd64/vmm/amd/npt.c 259579 2013-12-18 23:39:42Z grehan $");
29249967Sneel
30249967Sneel#include <sys/param.h>
31259579Sgrehan#include <sys/kernel.h>
32249967Sneel#include <sys/systm.h>
33259579Sgrehan#include <sys/sysctl.h>
34249967Sneel
35249967Sneel#include <vm/vm.h>
36249967Sneel#include <vm/pmap.h>
37259579Sgrehan#include <vm/vm_extern.h>
38249967Sneel
39249967Sneel#include <machine/pmap.h>
40249967Sneel#include <machine/md_var.h>
41249967Sneel#include <machine/vmparam.h>
42249967Sneel#include <machine/vmm.h>
43249967Sneel
44249967Sneel#include "svm.h"
45249967Sneel#include "vmcb.h"
46249967Sneel#include "svm_softc.h"
47249967Sneel#include "npt.h"
48249967Sneel
49259579SgrehanSYSCTL_DECL(_hw_vmm);
50259579SgrehanSYSCTL_NODE(_hw_vmm, OID_AUTO, npt, CTLFLAG_RW, NULL, NULL);
51249967Sneel
52259579Sgrehanstatic int npt_flags;
53259579SgrehanSYSCTL_INT(_hw_vmm_npt, OID_AUTO, pmap_flags, CTLFLAG_RD,
54259579Sgrehan	&npt_flags, 0, NULL);
55249967Sneel/*
56249967Sneel * AMD nested page table init.
57249967Sneel */
58249967Sneelint
59249967Sneelsvm_npt_init(void)
60249967Sneel{
61259579Sgrehan	int enable_superpage = 1;
62259579Sgrehan
63259579Sgrehan	TUNABLE_INT_FETCH("hw.vmm.npt.enable_superpage", &enable_superpage);
64259579Sgrehan	if (enable_superpage)
65259579Sgrehan		npt_flags |= PMAP_PDE_SUPERPAGE;
66249967Sneel
67249967Sneel	return (0);
68249967Sneel}
69249967Sneel
70249967Sneel
71249967Sneel
72259579Sgrehanstatic int
73259579Sgrehannpt_pinit(pmap_t pmap)
74249967Sneel{
75249967Sneel
76259579Sgrehan	return (pmap_pinit_type(pmap, PT_RVI, npt_flags));
77249967Sneel}
78249967Sneel
79259579Sgrehanstruct vmspace *
80259579Sgrehansvm_npt_alloc(vm_offset_t min, vm_offset_t max)
81249967Sneel{
82259579Sgrehan
83259579Sgrehan	return (vmspace_alloc(min, max, npt_pinit));
84249967Sneel}
85249967Sneel
86259579Sgrehanvoid
87259579Sgrehansvm_npt_free(struct vmspace *vmspace)
88249967Sneel{
89249967Sneel
90259579Sgrehan	vmspace_free(vmspace);
91249967Sneel}
92