1249967Sneel/*-
2336190Saraujo * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3336190Saraujo *
4249967Sneel * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
5249967Sneel * All rights reserved.
6249967Sneel *
7249967Sneel * Redistribution and use in source and binary forms, with or without
8249967Sneel * modification, are permitted provided that the following conditions
9249967Sneel * are met:
10249967Sneel * 1. Redistributions of source code must retain the above copyright
11249967Sneel *    notice unmodified, this list of conditions, and the following
12249967Sneel *    disclaimer.
13249967Sneel * 2. Redistributions in binary form must reproduce the above copyright
14249967Sneel *    notice, this list of conditions and the following disclaimer in the
15249967Sneel *    documentation and/or other materials provided with the distribution.
16249967Sneel *
17249967Sneel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18249967Sneel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19249967Sneel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20249967Sneel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21249967Sneel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22249967Sneel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23249967Sneel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24249967Sneel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25249967Sneel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26249967Sneel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27249967Sneel */
28249967Sneel
29249967Sneel#include <sys/cdefs.h>
30249967Sneel__FBSDID("$FreeBSD: stable/11/sys/amd64/vmm/amd/npt.c 336190 2018-07-11 07:19:42Z araujo $");
31249967Sneel
32249967Sneel#include <sys/param.h>
33259579Sgrehan#include <sys/kernel.h>
34249967Sneel#include <sys/systm.h>
35259579Sgrehan#include <sys/sysctl.h>
36249967Sneel
37249967Sneel#include <vm/vm.h>
38249967Sneel#include <vm/pmap.h>
39259579Sgrehan#include <vm/vm_extern.h>
40249967Sneel
41249967Sneel#include "npt.h"
42249967Sneel
43259579SgrehanSYSCTL_DECL(_hw_vmm);
44259579SgrehanSYSCTL_NODE(_hw_vmm, OID_AUTO, npt, CTLFLAG_RW, NULL, NULL);
45249967Sneel
46259579Sgrehanstatic int npt_flags;
47259579SgrehanSYSCTL_INT(_hw_vmm_npt, OID_AUTO, pmap_flags, CTLFLAG_RD,
48259579Sgrehan	&npt_flags, 0, NULL);
49267003Sgrehan
50267003Sgrehan#define NPT_IPIMASK	0xFF
51272927Sneel
52249967Sneel/*
53249967Sneel * AMD nested page table init.
54249967Sneel */
55249967Sneelint
56267003Sgrehansvm_npt_init(int ipinum)
57249967Sneel{
58259579Sgrehan	int enable_superpage = 1;
59259579Sgrehan
60267003Sgrehan	npt_flags = ipinum & NPT_IPIMASK;
61259579Sgrehan	TUNABLE_INT_FETCH("hw.vmm.npt.enable_superpage", &enable_superpage);
62259579Sgrehan	if (enable_superpage)
63259579Sgrehan		npt_flags |= PMAP_PDE_SUPERPAGE;
64249967Sneel
65249967Sneel	return (0);
66249967Sneel}
67249967Sneel
68259579Sgrehanstatic int
69259579Sgrehannpt_pinit(pmap_t pmap)
70249967Sneel{
71249967Sneel
72259579Sgrehan	return (pmap_pinit_type(pmap, PT_RVI, npt_flags));
73249967Sneel}
74249967Sneel
75259579Sgrehanstruct vmspace *
76259579Sgrehansvm_npt_alloc(vm_offset_t min, vm_offset_t max)
77249967Sneel{
78259579Sgrehan
79259579Sgrehan	return (vmspace_alloc(min, max, npt_pinit));
80249967Sneel}
81249967Sneel
82259579Sgrehanvoid
83259579Sgrehansvm_npt_free(struct vmspace *vmspace)
84249967Sneel{
85249967Sneel
86259579Sgrehan	vmspace_free(vmspace);
87249967Sneel}
88