9e65f29d06ad3e0983dff3462ce264f62c1905e9
[dragonfly.git] / sys / platform / pc64 / vmm / vmm_utils.c
1 /*
2  * Copyright (c) 2003-2013 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Mihai Carabas <mihai.carabas@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/types.h>
36 #include <sys/proc.h>
37 #include <sys/systm.h>
38 #include <cpu/lwbuf.h>
39 #include <vm/vm_page.h>
40 #include <vm/vm_extern.h>
41
42 #include "vmm_utils.h"
43
44 int
45 instr_check(struct instr_decode *instr, void *ip, uint8_t instr_length)
46 {
47
48         uint8_t i;
49         uint8_t *instr_ip;
50         uint8_t *instr_opcode;
51
52         instr_ip = (uint8_t *) ip;
53         instr_opcode = (uint8_t *) &instr->opcode;
54
55         /*  Skip REX prefix if present */
56         if (*instr_ip >= 0x40 && *instr_ip <= 0x4F) {
57                 instr_ip++;
58                 instr_length--;
59         }
60
61         for (i = 0; i < instr->opcode_bytes; i++) {
62                 if (i < instr_length) {
63                         if (instr_ip[i] != instr_opcode[i]) {
64                                 return -1;
65                         }
66                 } else {
67                         return -1;
68                 }
69         }
70         return 0;
71 }
72
73 static int
74 get_pt_entry(struct vmspace *vm, pt_entry_t *pte, vm_offset_t addr, int index)
75 {
76         struct lwbuf *lwb;
77         struct lwbuf lwb_cache;
78         pt_entry_t *pt;
79         int err = 0;
80         vm_page_t m;
81
82         m = vm_fault_page(&vm->vm_map, trunc_page(addr),
83                           VM_PROT_READ, VM_FAULT_NORMAL, &err);
84         if (err) {
85                 kprintf("%s: could not get addr %llx\n",
86                     __func__, (unsigned long long)addr);
87                 goto error;
88         }
89         lwb = lwbuf_alloc(m, &lwb_cache);
90         pt = (pt_entry_t *)lwbuf_kva(lwb) + ((vm_offset_t)addr & PAGE_MASK);
91
92         *pte = pt[index];
93         lwbuf_free(lwb);
94         vm_page_unhold(m);
95 error:
96         return err;
97 }
98
99 int
100 guest_phys_addr(struct vmspace *vm, register_t *gpa, register_t guest_cr3, vm_offset_t uaddr)
101 {
102         pt_entry_t pml4e;
103         pt_entry_t pdpe;
104         pt_entry_t pde;
105         pt_entry_t pte;
106         int err = 0;
107
108         err = get_pt_entry(vm, &pml4e, guest_cr3, uaddr >> PML4SHIFT);
109         if (err) {
110                 kprintf("%s: could not get pml4e\n", __func__);
111                 goto error;
112         }
113         if (pml4e & kernel_pmap.pmap_bits[PG_V_IDX]) {
114                 err = get_pt_entry(vm, &pdpe, pml4e & PG_FRAME, (uaddr & PML4MASK) >> PDPSHIFT);
115                 if (err) {
116                         kprintf("%s: could not get pdpe\n", __func__);
117                         goto error;
118                 }
119                 if (pdpe & kernel_pmap.pmap_bits[PG_V_IDX]) {
120                         if (pdpe & kernel_pmap.pmap_bits[PG_PS_IDX]) {
121                                 *gpa = (pdpe & PG_FRAME) | (uaddr & PDPMASK);
122                                 goto out;
123                         } else {
124                                 err = get_pt_entry(vm, &pde, pdpe & PG_FRAME, (uaddr & PDPMASK) >> PDRSHIFT);
125                                 if(err) {
126                                         kprintf("%s: could not get pdpe\n", __func__);
127                                         goto error;
128                                 }
129                                 if (pde & kernel_pmap.pmap_bits[PG_V_IDX]) {
130                                         if (pde & kernel_pmap.pmap_bits[PG_PS_IDX]) {
131                                                 *gpa = (pde & PG_FRAME) | (uaddr & PDRMASK);
132                                                 goto out;
133                                         } else {
134                                                 err = get_pt_entry(vm, &pte, pde & PG_FRAME, (uaddr & PDRMASK) >> PAGE_SHIFT);
135                                                 if (err) {
136                                                         kprintf("%s: could not get pte\n", __func__);
137                                                         goto error;
138                                                 }
139                                                 if (pte & kernel_pmap.pmap_bits[PG_V_IDX]) {
140                                                         *gpa = (pte & PG_FRAME) | (uaddr & PAGE_MASK);
141                                                 } else {
142                                                         kprintf("%s: pte not valid\n", __func__);
143                                                         err = EFAULT;
144                                                         goto error;
145                                                 }
146                                         }
147                                 } else {
148                                         kprintf("%s: pde not valid\n", __func__);
149                                         err = EFAULT;
150                                         goto error;
151                                 }
152                         }
153                 } else {
154                         kprintf("%s: pdpe not valid\n", __func__);
155                         err = EFAULT;
156                         goto error;
157                 }
158         } else {
159                 kprintf("%s: pml4e not valid\n", __func__);
160                 err = EFAULT;
161                 goto error;
162         }
163 out:
164 error:
165         return err;
166 }