2 * Copyright (c) 2013 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Mihai Carabas <mihai.carabas@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <cpu/lwbuf.h>
39 #include <vm/vm_page.h>
40 #include <vm/vm_extern.h>
42 #include "vmm_utils.h"
45 instr_check(struct instr_decode *instr, void *ip, uint8_t instr_length)
49 uint8_t *instr_opcode;
51 instr_ip = (uint8_t *) ip;
52 instr_opcode = (uint8_t *) &instr->opcode;
54 /* Skip REX prefix if present */
55 if (*instr_ip >= 0x40 && *instr_ip <= 0x4F) {
60 for (i = 0; i < instr->opcode_bytes; i++) {
61 if (i < instr_length) {
62 if (instr_ip[i] != instr_opcode[i]) {
73 get_pt_entry(struct vmspace *vm, pt_entry_t *pte, vm_offset_t addr, int index)
76 struct lwbuf lwb_cache;
81 m = vm_fault_page(&vm->vm_map, trunc_page(addr),
82 VM_PROT_READ, VM_FAULT_NORMAL, &err);
84 kprintf("%s: could not get addr %llx\n",
85 __func__, (unsigned long long)addr);
88 lwb = lwbuf_alloc(m, &lwb_cache);
89 pt = (pt_entry_t *)lwbuf_kva(lwb) + ((vm_offset_t)addr & PAGE_MASK);
99 guest_phys_addr(struct vmspace *vm, register_t *gpa, register_t guest_cr3,
108 err = get_pt_entry(vm, &pml4e, guest_cr3, uaddr >> PML4SHIFT);
110 kprintf("%s: could not get pml4e\n", __func__);
113 if ((pml4e & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
114 kprintf("%s: pml4e not valid\n", __func__);
119 err = get_pt_entry(vm, &pdpe, pml4e & PG_FRAME,
120 (uaddr & PML4MASK) >> PDPSHIFT);
122 kprintf("%s: could not get pdpe\n", __func__);
125 if ((pdpe & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
126 kprintf("%s: pdpe not valid\n", __func__);
130 if (pdpe & kernel_pmap.pmap_bits[PG_PS_IDX]) {
131 *gpa = (pdpe & PG_FRAME) | (uaddr & PDPMASK);
135 err = get_pt_entry(vm, &pde, pdpe & PG_FRAME,
136 (uaddr & PDPMASK) >> PDRSHIFT);
138 kprintf("%s: could not get pdpe\n", __func__);
141 if ((pde & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
142 kprintf("%s: pde not valid\n", __func__);
146 if (pde & kernel_pmap.pmap_bits[PG_PS_IDX]) {
147 *gpa = (pde & PG_FRAME) | (uaddr & PDRMASK);
151 err = get_pt_entry(vm, &pte, pde & PG_FRAME,
152 (uaddr & PDRMASK) >> PAGE_SHIFT);
154 kprintf("%s: could not get pte\n", __func__);
157 if ((pte & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
158 kprintf("%s: pte not valid\n", __func__);
162 *gpa = (pte & PG_FRAME) | (uaddr & PAGE_MASK);