xenfunc.c revision 1.20 1 /* $NetBSD: xenfunc.c,v 1.20 2018/09/23 00:59:59 cherry Exp $ */
2
3 /*
4 * Copyright (c) 2004 Christian Limpach.
5 * All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: xenfunc.c,v 1.20 2018/09/23 00:59:59 cherry Exp $");
30
31 #include <sys/param.h>
32
33 #include <uvm/uvm_extern.h>
34
35 #include <machine/intr.h>
36 #include <machine/vmparam.h>
37 #include <machine/pmap.h>
38 #include <xen/xen.h>
39 #include <xen/hypervisor.h>
40 //#include <xen/evtchn.h>
41 #include <xen/xenpmap.h>
42 #include <machine/pte.h>
43
44 void xen_set_ldt(vaddr_t, uint32_t);
45
46 void
47 invlpg(vaddr_t addr)
48 {
49 int s = splvm(); /* XXXSMP */
50 xpq_queue_invlpg(addr);
51 splx(s);
52 }
53
54 void
55 lidt(struct region_descriptor *rd)
56 {
57 /*
58 * We need to do this because we can't assume kmem_alloc(9)
59 * will be available at the boot stage when this is called.
60 */
61 static char xen_idt_page[PAGE_SIZE] __attribute__((__aligned__ (PAGE_SIZE)));
62
63 struct trap_info *xen_idt = (void * )xen_idt_page;
64 int xen_idt_idx = 0;
65
66 struct trap_info * idd = (void *) rd->rd_base;
67 const int nidt = rd->rd_limit / (sizeof *idd);
68
69 int i;
70
71 /*
72 * Sweep in all initialised entries, consolidate them back to
73 * back in the requestor array.
74 */
75 for (i = 0; i < nidt; i++) {
76 if (idd->address == 0) /* Skip gap */
77 continue;
78
79 /* Copy over entry */
80 xen_idt[xen_idt_idx++] = idd[i];
81 }
82
83 /* page needs to be r/o */
84 pmap_changeprot_local((vaddr_t) xen_idt, VM_PROT_READ);
85
86 /* Hook it up in the hypervisor */
87 if (HYPERVISOR_set_trap_table(xen_idt))
88 panic("HYPERVISOR_set_trap_table() failed");
89
90 /* reset */
91 pmap_changeprot_local((vaddr_t) xen_idt, VM_PROT_READ|VM_PROT_WRITE);
92 }
93
94 void
95 lldt(u_short sel)
96 {
97 #ifndef __x86_64__
98 struct cpu_info *ci;
99
100 ci = curcpu();
101
102 if (ci->ci_curldt == sel)
103 return;
104 if (sel == GSEL(GLDT_SEL, SEL_KPL))
105 xen_set_ldt((vaddr_t)ldtstore, NLDT);
106 else
107 xen_set_ldt(ci->ci_gdt[IDXSELN(sel)].ld.ld_base,
108 ci->ci_gdt[IDXSELN(sel)].ld.ld_entries);
109 ci->ci_curldt = sel;
110 #endif
111 }
112
113 void
114 ltr(u_short sel)
115 {
116 panic("XXX ltr not supported\n");
117 }
118
119 void
120 lcr0(u_long val)
121 {
122 panic("XXX lcr0 not supported\n");
123 }
124
125 u_long
126 rcr0(void)
127 {
128 /* XXX: handle X86_CR0_TS ? */
129 return 0;
130 }
131
132 #ifndef __x86_64__
133 void
134 lcr3(vaddr_t val)
135 {
136 int s = splvm(); /* XXXSMP */
137 xpq_queue_pt_switch(xpmap_ptom_masked(val));
138 splx(s);
139 }
140 #endif
141
142 void
143 tlbflush(void)
144 {
145 int s = splvm(); /* XXXSMP */
146 xpq_queue_tlb_flush();
147 splx(s);
148 }
149
150 void
151 tlbflushg(void)
152 {
153 tlbflush();
154 }
155
156 register_t
157 rdr0(void)
158 {
159
160 return HYPERVISOR_get_debugreg(0);
161 }
162
163 void
164 ldr0(register_t val)
165 {
166
167 HYPERVISOR_set_debugreg(0, val);
168 }
169
170 register_t
171 rdr1(void)
172 {
173
174 return HYPERVISOR_get_debugreg(1);
175 }
176
177 void
178 ldr1(register_t val)
179 {
180
181 HYPERVISOR_set_debugreg(1, val);
182 }
183
184 register_t
185 rdr2(void)
186 {
187
188 return HYPERVISOR_get_debugreg(2);
189 }
190
191 void
192 ldr2(register_t val)
193 {
194
195 HYPERVISOR_set_debugreg(2, val);
196 }
197
198 register_t
199 rdr3(void)
200 {
201
202 return HYPERVISOR_get_debugreg(3);
203 }
204
205 void
206 ldr3(register_t val)
207 {
208
209 HYPERVISOR_set_debugreg(3, val);
210 }
211 register_t
212 rdr6(void)
213 {
214
215 return HYPERVISOR_get_debugreg(6);
216 }
217
218 void
219 ldr6(register_t val)
220 {
221
222 HYPERVISOR_set_debugreg(6, val);
223 }
224
225 register_t
226 rdr7(void)
227 {
228
229 return HYPERVISOR_get_debugreg(7);
230 }
231
232 void
233 ldr7(register_t val)
234 {
235
236 HYPERVISOR_set_debugreg(7, val);
237 }
238
239 void
240 wbinvd(void)
241 {
242
243 xpq_flush_cache();
244 }
245
246 vaddr_t
247 rcr2(void)
248 {
249 return curcpu()->ci_vcpu->arch.cr2;
250 }
251
252 #ifdef __x86_64__
253 void
254 setusergs(int gssel)
255 {
256 HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, gssel);
257 }
258 #endif
259