Merge branch 'vendor/GCC50'
[dragonfly.git] / sys / platform / pc64 / vmm / vmm_utils.c
1 /*
2  * Copyright (c) 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         uint8_t i;
48         uint8_t *instr_ip;
49         uint8_t *instr_opcode;
50
51         instr_ip = (uint8_t *) ip;
52         instr_opcode = (uint8_t *) &instr->opcode;
53
54         /*  Skip REX prefix if present */
55         if (*instr_ip >= 0x40 && *instr_ip <= 0x4F) {
56                 instr_ip++;
57                 instr_length--;
58         }
59
60         for (i = 0; i < instr->opcode_bytes; i++) {
61                 if (i < instr_length) {
62                         if (instr_ip[i] != instr_opcode[i]) {
63                                 return -1;
64                         }
65                 } else {
66                         return -1;
67                 }
68         }
69         return 0;
70 }
71
72 static int
73 get_pt_entry(struct vmspace *vm, pt_entry_t *pte, vm_offset_t addr, int index)
74 {
75         struct lwbuf *lwb;
76         struct lwbuf lwb_cache;
77         pt_entry_t *pt;
78         int err = 0;
79         vm_page_t m;
80
81         m = vm_fault_page(&vm->vm_map, trunc_page(addr),
82                           VM_PROT_READ, VM_FAULT_NORMAL, &err);
83         if (err) {
84                 kprintf("%s: could not get addr %llx\n",
85                     __func__, (unsigned long long)addr);
86                 goto error;
87         }
88         lwb = lwbuf_alloc(m, &lwb_cache);
89         pt = (pt_entry_t *)lwbuf_kva(lwb) + ((vm_offset_t)addr & PAGE_MASK);
90
91         *pte = pt[index];
92         lwbuf_free(lwb);
93         vm_page_unhold(m);
94 error:
95         return err;
96 }
97
98 int
99 guest_phys_addr(struct vmspace *vm, register_t *gpa, register_t guest_cr3,
100     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]) == 0) {
114                 kprintf("%s: pml4e not valid\n", __func__);
115                 err = EFAULT;
116                 goto error;
117         }
118
119         err = get_pt_entry(vm, &pdpe, pml4e & PG_FRAME,
120             (uaddr & PML4MASK) >> PDPSHIFT);
121         if (err) {
122                 kprintf("%s: could not get pdpe\n", __func__);
123                 goto error;
124         }
125         if ((pdpe & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
126                 kprintf("%s: pdpe not valid\n", __func__);
127                 err = EFAULT;
128                 goto error;
129         }
130         if (pdpe & kernel_pmap.pmap_bits[PG_PS_IDX]) {
131                 *gpa = (pdpe & PG_FRAME) | (uaddr & PDPMASK);
132                 return 0;
133         }
134
135         err = get_pt_entry(vm, &pde, pdpe & PG_FRAME,
136             (uaddr & PDPMASK) >> PDRSHIFT);
137         if (err) {
138                 kprintf("%s: could not get pdpe\n", __func__);
139                 goto error;
140         }
141         if ((pde & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
142                 kprintf("%s: pde not valid\n", __func__);
143                 err = EFAULT;
144                 goto error;
145         }
146         if (pde & kernel_pmap.pmap_bits[PG_PS_IDX]) {
147                 *gpa = (pde & PG_FRAME) | (uaddr & PDRMASK);
148                 return 0;
149         }
150
151         err = get_pt_entry(vm, &pte, pde & PG_FRAME,
152             (uaddr & PDRMASK) >> PAGE_SHIFT);
153         if (err) {
154                 kprintf("%s: could not get pte\n", __func__);
155                 goto error;
156         }
157         if ((pte & kernel_pmap.pmap_bits[PG_V_IDX]) == 0) {
158                 kprintf("%s: pte not valid\n", __func__);
159                 err = EFAULT;
160                 goto error;
161         }
162         *gpa = (pte & PG_FRAME) | (uaddr & PAGE_MASK);
163         return 0;
164
165 error:
166         return err;
167 }