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: releng/11.0/sys/amd64/vmm/amd/npt.c 295880 2016-02-22 09:02:20Z skra $");
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 "npt.h"
40249967Sneel
41259579SgrehanSYSCTL_DECL(_hw_vmm);
42259579SgrehanSYSCTL_NODE(_hw_vmm, OID_AUTO, npt, CTLFLAG_RW, NULL, NULL);
43249967Sneel
44259579Sgrehanstatic int npt_flags;
45259579SgrehanSYSCTL_INT(_hw_vmm_npt, OID_AUTO, pmap_flags, CTLFLAG_RD,
46259579Sgrehan	&npt_flags, 0, NULL);
47267003Sgrehan
48267003Sgrehan#define NPT_IPIMASK	0xFF
49272927Sneel
50249967Sneel/*
51249967Sneel * AMD nested page table init.
52249967Sneel */
53249967Sneelint
54267003Sgrehansvm_npt_init(int ipinum)
55249967Sneel{
56259579Sgrehan	int enable_superpage = 1;
57259579Sgrehan
58267003Sgrehan	npt_flags = ipinum & NPT_IPIMASK;
59259579Sgrehan	TUNABLE_INT_FETCH("hw.vmm.npt.enable_superpage", &enable_superpage);
60259579Sgrehan	if (enable_superpage)
61259579Sgrehan		npt_flags |= PMAP_PDE_SUPERPAGE;
62249967Sneel
63249967Sneel	return (0);
64249967Sneel}
65249967Sneel
66259579Sgrehanstatic int
67259579Sgrehannpt_pinit(pmap_t pmap)
68249967Sneel{
69249967Sneel
70259579Sgrehan	return (pmap_pinit_type(pmap, PT_RVI, npt_flags));
71249967Sneel}
72249967Sneel
73259579Sgrehanstruct vmspace *
74259579Sgrehansvm_npt_alloc(vm_offset_t min, vm_offset_t max)
75249967Sneel{
76259579Sgrehan
77259579Sgrehan	return (vmspace_alloc(min, max, npt_pinit));
78249967Sneel}
79249967Sneel
80259579Sgrehanvoid
81259579Sgrehansvm_npt_free(struct vmspace *vmspace)
82249967Sneel{
83249967Sneel
84259579Sgrehan	vmspace_free(vmspace);
85249967Sneel}
86