]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/vm86.c
ANSIfy i386/vm86.c
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / vm86.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997 Jonathan Lemon
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/priv.h>
35 #include <sys/proc.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_map.h>
43 #include <vm/vm_page.h>
44
45 #include <machine/md_var.h>
46 #include <machine/pcb.h>
47 #include <machine/pcb_ext.h>
48 #include <machine/psl.h>
49 #include <machine/specialreg.h>
50 #include <machine/sysarch.h>
51
52 extern int vm86pa;
53 extern struct pcb *vm86pcb;
54
55 static struct mtx vm86_lock;
56
57 extern int vm86_bioscall(struct vm86frame *);
58 extern void vm86_biosret(struct vm86frame *);
59
60 void vm86_prepcall(struct vm86frame *);
61
62 struct system_map {
63         int             type;
64         vm_offset_t     start;
65         vm_offset_t     end;
66 };
67
68 #define HLT     0xf4
69 #define CLI     0xfa
70 #define STI     0xfb
71 #define PUSHF   0x9c
72 #define POPF    0x9d
73 #define INTn    0xcd
74 #define IRET    0xcf
75 #define CALLm   0xff
76 #define OPERAND_SIZE_PREFIX     0x66
77 #define ADDRESS_SIZE_PREFIX     0x67
78 #define PUSH_MASK       ~(PSL_VM | PSL_RF | PSL_I)
79 #define POP_MASK        ~(PSL_VIP | PSL_VIF | PSL_VM | PSL_RF | PSL_IOPL)
80
81 static __inline caddr_t
82 MAKE_ADDR(u_short sel, u_short off)
83 {
84         return ((caddr_t)((sel << 4) + off));
85 }
86
87 static __inline void
88 GET_VEC(u_int vec, u_short *sel, u_short *off)
89 {
90         *sel = vec >> 16;
91         *off = vec & 0xffff;
92 }
93
94 static __inline u_int
95 MAKE_VEC(u_short sel, u_short off)
96 {
97         return ((sel << 16) | off);
98 }
99
100 static __inline void
101 PUSH(u_short x, struct vm86frame *vmf)
102 {
103         vmf->vmf_sp -= 2;
104         suword16(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp), x);
105 }
106
107 static __inline void
108 PUSHL(u_int x, struct vm86frame *vmf)
109 {
110         vmf->vmf_sp -= 4;
111         suword(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp), x);
112 }
113
114 static __inline u_short
115 POP(struct vm86frame *vmf)
116 {
117         u_short x = fuword16(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp));
118
119         vmf->vmf_sp += 2;
120         return (x);
121 }
122
123 static __inline u_int
124 POPL(struct vm86frame *vmf)
125 {
126         u_int x = fuword(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp));
127
128         vmf->vmf_sp += 4;
129         return (x);
130 }
131
132 int
133 vm86_emulate(struct vm86frame *vmf)
134 {
135         struct vm86_kernel *vm86;
136         caddr_t addr;
137         u_char i_byte;
138         u_int temp_flags;
139         int inc_ip = 1;
140         int retcode = 0;
141
142         /*
143          * pcb_ext contains the address of the extension area, or zero if
144          * the extension is not present.  (This check should not be needed,
145          * as we can't enter vm86 mode until we set up an extension area)
146          */
147         if (curpcb->pcb_ext == 0)
148                 return (SIGBUS);
149         vm86 = &curpcb->pcb_ext->ext_vm86;
150
151         if (vmf->vmf_eflags & PSL_T)
152                 retcode = SIGTRAP;
153
154         addr = MAKE_ADDR(vmf->vmf_cs, vmf->vmf_ip);
155         i_byte = fubyte(addr);
156         if (i_byte == ADDRESS_SIZE_PREFIX) {
157                 i_byte = fubyte(++addr);
158                 inc_ip++;
159         }
160
161         if (vm86->vm86_has_vme) {
162                 switch (i_byte) {
163                 case OPERAND_SIZE_PREFIX:
164                         i_byte = fubyte(++addr);
165                         inc_ip++;
166                         switch (i_byte) {
167                         case PUSHF:
168                                 if (vmf->vmf_eflags & PSL_VIF)
169                                         PUSHL((vmf->vmf_eflags & PUSH_MASK)
170                                             | PSL_IOPL | PSL_I, vmf);
171                                 else
172                                         PUSHL((vmf->vmf_eflags & PUSH_MASK)
173                                             | PSL_IOPL, vmf);
174                                 vmf->vmf_ip += inc_ip;
175                                 return (retcode);
176
177                         case POPF:
178                                 temp_flags = POPL(vmf) & POP_MASK;
179                                 vmf->vmf_eflags = (vmf->vmf_eflags & ~POP_MASK)
180                                     | temp_flags | PSL_VM | PSL_I;
181                                 vmf->vmf_ip += inc_ip;
182                                 if (temp_flags & PSL_I) {
183                                         vmf->vmf_eflags |= PSL_VIF;
184                                         if (vmf->vmf_eflags & PSL_VIP)
185                                                 break;
186                                 } else {
187                                         vmf->vmf_eflags &= ~PSL_VIF;
188                                 }
189                                 return (retcode);
190                         }
191                         break;
192
193                 /* VME faults here if VIP is set, but does not set VIF. */
194                 case STI:
195                         vmf->vmf_eflags |= PSL_VIF;
196                         vmf->vmf_ip += inc_ip;
197                         if ((vmf->vmf_eflags & PSL_VIP) == 0) {
198                                 uprintf("fatal sti\n");
199                                 return (SIGKILL);
200                         }
201                         break;
202
203                 /* VME if no redirection support */
204                 case INTn:
205                         break;
206
207                 /* VME if trying to set PSL_T, or PSL_I when VIP is set */
208                 case POPF:
209                         temp_flags = POP(vmf) & POP_MASK;
210                         vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
211                             | temp_flags | PSL_VM | PSL_I;
212                         vmf->vmf_ip += inc_ip;
213                         if (temp_flags & PSL_I) {
214                                 vmf->vmf_eflags |= PSL_VIF;
215                                 if (vmf->vmf_eflags & PSL_VIP)
216                                         break;
217                         } else {
218                                 vmf->vmf_eflags &= ~PSL_VIF;
219                         }
220                         return (retcode);
221
222                 /* VME if trying to set PSL_T, or PSL_I when VIP is set */
223                 case IRET:
224                         vmf->vmf_ip = POP(vmf);
225                         vmf->vmf_cs = POP(vmf);
226                         temp_flags = POP(vmf) & POP_MASK;
227                         vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
228                             | temp_flags | PSL_VM | PSL_I;
229                         if (temp_flags & PSL_I) {
230                                 vmf->vmf_eflags |= PSL_VIF;
231                                 if (vmf->vmf_eflags & PSL_VIP)
232                                         break;
233                         } else {
234                                 vmf->vmf_eflags &= ~PSL_VIF;
235                         }
236                         return (retcode);
237
238                 }
239                 return (SIGBUS);
240         }
241
242         switch (i_byte) {
243         case OPERAND_SIZE_PREFIX:
244                 i_byte = fubyte(++addr);
245                 inc_ip++;
246                 switch (i_byte) {
247                 case PUSHF:
248                         if (vm86->vm86_eflags & PSL_VIF)
249                                 PUSHL((vmf->vmf_flags & PUSH_MASK)
250                                     | PSL_IOPL | PSL_I, vmf);
251                         else
252                                 PUSHL((vmf->vmf_flags & PUSH_MASK)
253                                     | PSL_IOPL, vmf);
254                         vmf->vmf_ip += inc_ip;
255                         return (retcode);
256
257                 case POPF:
258                         temp_flags = POPL(vmf) & POP_MASK;
259                         vmf->vmf_eflags = (vmf->vmf_eflags & ~POP_MASK)
260                             | temp_flags | PSL_VM | PSL_I;
261                         vmf->vmf_ip += inc_ip;
262                         if (temp_flags & PSL_I) {
263                                 vm86->vm86_eflags |= PSL_VIF;
264                                 if (vm86->vm86_eflags & PSL_VIP)
265                                         break;
266                         } else {
267                                 vm86->vm86_eflags &= ~PSL_VIF;
268                         }
269                         return (retcode);
270                 }
271                 return (SIGBUS);
272
273         case CLI:
274                 vm86->vm86_eflags &= ~PSL_VIF;
275                 vmf->vmf_ip += inc_ip;
276                 return (retcode);
277
278         case STI:
279                 /* if there is a pending interrupt, go to the emulator */
280                 vm86->vm86_eflags |= PSL_VIF;
281                 vmf->vmf_ip += inc_ip;
282                 if (vm86->vm86_eflags & PSL_VIP)
283                         break;
284                 return (retcode);
285
286         case PUSHF:
287                 if (vm86->vm86_eflags & PSL_VIF)
288                         PUSH((vmf->vmf_flags & PUSH_MASK)
289                             | PSL_IOPL | PSL_I, vmf);
290                 else
291                         PUSH((vmf->vmf_flags & PUSH_MASK) | PSL_IOPL, vmf);
292                 vmf->vmf_ip += inc_ip;
293                 return (retcode);
294
295         case INTn:
296                 i_byte = fubyte(addr + 1);
297                 if ((vm86->vm86_intmap[i_byte >> 3] & (1 << (i_byte & 7))) != 0)
298                         break;
299                 if (vm86->vm86_eflags & PSL_VIF)
300                         PUSH((vmf->vmf_flags & PUSH_MASK)
301                             | PSL_IOPL | PSL_I, vmf);
302                 else
303                         PUSH((vmf->vmf_flags & PUSH_MASK) | PSL_IOPL, vmf);
304                 PUSH(vmf->vmf_cs, vmf);
305                 PUSH(vmf->vmf_ip + inc_ip + 1, vmf);    /* increment IP */
306                 GET_VEC(fuword((caddr_t)(i_byte * 4)),
307                      &vmf->vmf_cs, &vmf->vmf_ip);
308                 vmf->vmf_flags &= ~PSL_T;
309                 vm86->vm86_eflags &= ~PSL_VIF;
310                 return (retcode);
311
312         case IRET:
313                 vmf->vmf_ip = POP(vmf);
314                 vmf->vmf_cs = POP(vmf);
315                 temp_flags = POP(vmf) & POP_MASK;
316                 vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
317                     | temp_flags | PSL_VM | PSL_I;
318                 if (temp_flags & PSL_I) {
319                         vm86->vm86_eflags |= PSL_VIF;
320                         if (vm86->vm86_eflags & PSL_VIP)
321                                 break;
322                 } else {
323                         vm86->vm86_eflags &= ~PSL_VIF;
324                 }
325                 return (retcode);
326
327         case POPF:
328                 temp_flags = POP(vmf) & POP_MASK;
329                 vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
330                     | temp_flags | PSL_VM | PSL_I;
331                 vmf->vmf_ip += inc_ip;
332                 if (temp_flags & PSL_I) {
333                         vm86->vm86_eflags |= PSL_VIF;
334                         if (vm86->vm86_eflags & PSL_VIP)
335                                 break;
336                 } else {
337                         vm86->vm86_eflags &= ~PSL_VIF;
338                 }
339                 return (retcode);
340         }
341         return (SIGBUS);
342 }
343
344 #define PGTABLE_SIZE    ((1024 + 64) * 1024 / PAGE_SIZE)
345 #define INTMAP_SIZE     32
346 #define IOMAP_SIZE      ctob(IOPAGES)
347 #define TSS_SIZE \
348         (sizeof(struct pcb_ext) - sizeof(struct segment_descriptor) + \
349          INTMAP_SIZE + IOMAP_SIZE + 1)
350
351 struct vm86_layout {
352         pt_entry_t      vml_pgtbl[PGTABLE_SIZE];
353         struct  pcb vml_pcb;
354         struct  pcb_ext vml_ext;
355         char    vml_intmap[INTMAP_SIZE];
356         char    vml_iomap[IOMAP_SIZE];
357         char    vml_iomap_trailer;
358 };
359
360 void
361 vm86_initialize(void)
362 {
363         int i;
364         u_int *addr;
365         struct vm86_layout *vml = (struct vm86_layout *)vm86paddr;
366         struct pcb *pcb;
367         struct pcb_ext *ext;
368         struct soft_segment_descriptor ssd = {
369                 0,                      /* segment base address (overwritten) */
370                 0,                      /* length (overwritten) */
371                 SDT_SYS386TSS,          /* segment type */
372                 0,                      /* priority level */
373                 1,                      /* descriptor present */
374                 0, 0,
375                 0,                      /* default 16 size */
376                 0                       /* granularity */
377         };
378
379         /*
380          * this should be a compile time error, but cpp doesn't grok sizeof().
381          */
382         if (sizeof(struct vm86_layout) > ctob(3))
383                 panic("struct vm86_layout exceeds space allocated in locore.s");
384
385         /*
386          * Below is the memory layout that we use for the vm86 region.
387          *
388          * +--------+
389          * |        | 
390          * |        |
391          * | page 0 |       
392          * |        | +--------+
393          * |        | | stack  |
394          * +--------+ +--------+ <--------- vm86paddr
395          * |        | |Page Tbl| 1M + 64K = 272 entries = 1088 bytes
396          * |        | +--------+
397          * |        | |  PCB   | size: ~240 bytes
398          * | page 1 | |PCB Ext | size: ~140 bytes (includes TSS)
399          * |        | +--------+
400          * |        | |int map |
401          * |        | +--------+
402          * +--------+ |        |
403          * | page 2 | |  I/O   |
404          * +--------+ | bitmap |
405          * | page 3 | |        |
406          * |        | +--------+
407          * +--------+ 
408          */
409
410         /*
411          * A rudimentary PCB must be installed, in order to get to the
412          * PCB extension area.  We use the PCB area as a scratchpad for
413          * data storage, the layout of which is shown below.
414          *
415          * pcb_esi      = new PTD entry 0
416          * pcb_ebp      = pointer to frame on vm86 stack
417          * pcb_esp      =    stack frame pointer at time of switch
418          * pcb_ebx      = va of vm86 page table
419          * pcb_eip      =    argument pointer to initial call
420          * pcb_spare[0] =    saved TSS descriptor, word 0
421          * pcb_space[1] =    saved TSS descriptor, word 1
422          */
423 #define new_ptd         pcb_esi
424 #define vm86_frame      pcb_ebp
425 #define pgtable_va      pcb_ebx
426
427         pcb = &vml->vml_pcb;
428         ext = &vml->vml_ext;
429
430         mtx_init(&vm86_lock, "vm86 lock", NULL, MTX_DEF);
431
432         bzero(pcb, sizeof(struct pcb));
433         pcb->new_ptd = vm86pa | PG_V | PG_RW | PG_U;
434         pcb->vm86_frame = vm86paddr - sizeof(struct vm86frame);
435         pcb->pgtable_va = vm86paddr;
436         pcb->pcb_flags = PCB_VM86CALL; 
437         pcb->pcb_ext = ext;
438
439         bzero(ext, sizeof(struct pcb_ext)); 
440         ext->ext_tss.tss_esp0 = vm86paddr;
441         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
442         ext->ext_tss.tss_ioopt = 
443                 ((u_int)vml->vml_iomap - (u_int)&ext->ext_tss) << 16;
444         ext->ext_iomap = vml->vml_iomap;
445         ext->ext_vm86.vm86_intmap = vml->vml_intmap;
446
447         if (cpu_feature & CPUID_VME)
448                 ext->ext_vm86.vm86_has_vme = (rcr4() & CR4_VME ? 1 : 0);
449
450         addr = (u_int *)ext->ext_vm86.vm86_intmap;
451         for (i = 0; i < (INTMAP_SIZE + IOMAP_SIZE) / sizeof(u_int); i++)
452                 *addr++ = 0;
453         vml->vml_iomap_trailer = 0xff;
454
455         ssd.ssd_base = (u_int)&ext->ext_tss;
456         ssd.ssd_limit = TSS_SIZE - 1; 
457         ssdtosd(&ssd, &ext->ext_tssd);
458
459         vm86pcb = pcb;
460
461 #if 0
462         /*
463          * use whatever is leftover of the vm86 page layout as a
464          * message buffer so we can capture early output.
465          */
466         msgbufinit((vm_offset_t)vm86paddr + sizeof(struct vm86_layout),
467             ctob(3) - sizeof(struct vm86_layout));
468 #endif
469 }
470
471 vm_offset_t
472 vm86_getpage(struct vm86context *vmc, int pagenum)
473 {
474         int i;
475
476         for (i = 0; i < vmc->npages; i++)
477                 if (vmc->pmap[i].pte_num == pagenum)
478                         return (vmc->pmap[i].kva);
479         return (0);
480 }
481
482 vm_offset_t
483 vm86_addpage(struct vm86context *vmc, int pagenum, vm_offset_t kva)
484 {
485         int i, flags = 0;
486
487         for (i = 0; i < vmc->npages; i++)
488                 if (vmc->pmap[i].pte_num == pagenum)
489                         goto overlap;
490
491         if (vmc->npages == VM86_PMAPSIZE)
492                 goto full;                      /* XXX grow map? */
493
494         if (kva == 0) {
495                 kva = (vm_offset_t)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
496                 flags = VMAP_MALLOC;
497         }
498
499         i = vmc->npages++;
500         vmc->pmap[i].flags = flags;
501         vmc->pmap[i].kva = kva;
502         vmc->pmap[i].pte_num = pagenum;
503         return (kva);
504 overlap:
505         panic("vm86_addpage: overlap");
506 full:
507         panic("vm86_addpage: not enough room");
508 }
509
510 /*
511  * called from vm86_bioscall, while in vm86 address space, to finalize setup.
512  */
513 void
514 vm86_prepcall(struct vm86frame *vmf)
515 {
516         struct vm86_kernel *vm86;
517         uint32_t *stack;
518         uint8_t *code;
519
520         code = (void *)0xa00;
521         stack = (void *)(0x1000 - 2);   /* keep aligned */
522         if ((vmf->vmf_trapno & PAGE_MASK) <= 0xff) {
523                 /* interrupt call requested */
524                 code[0] = INTn;
525                 code[1] = vmf->vmf_trapno & 0xff;
526                 code[2] = HLT;
527                 vmf->vmf_ip = (uintptr_t)code;
528                 vmf->vmf_cs = 0;
529         } else {
530                 code[0] = HLT;
531                 stack--;
532                 stack[0] = MAKE_VEC(0, (uintptr_t)code);
533         }
534         vmf->vmf_sp = (uintptr_t)stack;
535         vmf->vmf_ss = 0;
536         vmf->kernel_fs = vmf->kernel_es = vmf->kernel_ds = 0;
537         vmf->vmf_eflags = PSL_VIF | PSL_VM | PSL_USER;
538
539         vm86 = &curpcb->pcb_ext->ext_vm86;
540         if (!vm86->vm86_has_vme) 
541                 vm86->vm86_eflags = vmf->vmf_eflags;  /* save VIF, VIP */
542 }
543
544 /*
545  * vm86 trap handler; determines whether routine succeeded or not.
546  * Called while in vm86 space, returns to calling process.
547  */
548 void
549 vm86_trap(struct vm86frame *vmf)
550 {
551         caddr_t addr;
552
553         /* "should not happen" */
554         if ((vmf->vmf_eflags & PSL_VM) == 0)
555                 panic("vm86_trap called, but not in vm86 mode");
556
557         addr = MAKE_ADDR(vmf->vmf_cs, vmf->vmf_ip);
558         if (*(u_char *)addr == HLT)
559                 vmf->vmf_trapno = vmf->vmf_eflags & PSL_C;
560         else
561                 vmf->vmf_trapno = vmf->vmf_trapno << 16;
562
563         vm86_biosret(vmf);
564 }
565
566 int
567 vm86_intcall(int intnum, struct vm86frame *vmf)
568 {
569         int retval;
570
571         if (intnum < 0 || intnum > 0xff)
572                 return (EINVAL);
573
574         vmf->vmf_trapno = intnum;
575         mtx_lock(&vm86_lock);
576         critical_enter();
577         retval = vm86_bioscall(vmf);
578         critical_exit();
579         mtx_unlock(&vm86_lock);
580         return (retval);
581 }
582
583 /*
584  * struct vm86context contains the page table to use when making
585  * vm86 calls.  If intnum is a valid interrupt number (0-255), then
586  * the "interrupt trampoline" will be used, otherwise we use the
587  * caller's cs:ip routine.  
588  */
589 int
590 vm86_datacall(int intnum, struct vm86frame *vmf, struct vm86context *vmc)
591 {
592         pt_entry_t *pte = (pt_entry_t *)vm86paddr;
593         vm_paddr_t page;
594         int i, entry, retval;
595
596         mtx_lock(&vm86_lock);
597         for (i = 0; i < vmc->npages; i++) {
598                 page = vtophys(vmc->pmap[i].kva & PG_FRAME);
599                 entry = vmc->pmap[i].pte_num; 
600                 vmc->pmap[i].old_pte = pte[entry];
601                 pte[entry] = page | PG_V | PG_RW | PG_U;
602                 pmap_invalidate_page(kernel_pmap, vmc->pmap[i].kva);
603         }
604
605         vmf->vmf_trapno = intnum;
606         critical_enter();
607         retval = vm86_bioscall(vmf);
608         critical_exit();
609
610         for (i = 0; i < vmc->npages; i++) {
611                 entry = vmc->pmap[i].pte_num;
612                 pte[entry] = vmc->pmap[i].old_pte;
613                 pmap_invalidate_page(kernel_pmap, vmc->pmap[i].kva);
614         }
615         mtx_unlock(&vm86_lock);
616
617         return (retval);
618 }
619
620 vm_offset_t
621 vm86_getaddr(struct vm86context *vmc, u_short sel, u_short off)
622 {
623         int i, page;
624         vm_offset_t addr;
625
626         addr = (vm_offset_t)MAKE_ADDR(sel, off);
627         page = addr >> PAGE_SHIFT;
628         for (i = 0; i < vmc->npages; i++)
629                 if (page == vmc->pmap[i].pte_num)
630                         return (vmc->pmap[i].kva + (addr & PAGE_MASK));
631         return (0);
632 }
633
634 int
635 vm86_getptr(struct vm86context *vmc, vm_offset_t kva, u_short *sel,
636      u_short *off)
637 {
638         int i;
639
640         for (i = 0; i < vmc->npages; i++)
641                 if (kva >= vmc->pmap[i].kva &&
642                     kva < vmc->pmap[i].kva + PAGE_SIZE) {
643                         *off = kva - vmc->pmap[i].kva;
644                         *sel = vmc->pmap[i].pte_num << 8;
645                         return (1);
646                 }
647         return (0);
648 }
649         
650 int
651 vm86_sysarch(struct thread *td, char *args)
652 {
653         int error = 0;
654         struct i386_vm86_args ua;
655         struct vm86_kernel *vm86;
656
657         if ((error = copyin(args, &ua, sizeof(struct i386_vm86_args))) != 0)
658                 return (error);
659
660         if (td->td_pcb->pcb_ext == 0)
661                 if ((error = i386_extend_pcb(td)) != 0)
662                         return (error);
663         vm86 = &td->td_pcb->pcb_ext->ext_vm86;
664
665         switch (ua.sub_op) {
666         case VM86_INIT: {
667                 struct vm86_init_args sa;
668
669                 if ((error = copyin(ua.sub_args, &sa, sizeof(sa))) != 0)
670                         return (error);
671                 if (cpu_feature & CPUID_VME)
672                         vm86->vm86_has_vme = (rcr4() & CR4_VME ? 1 : 0);
673                 else
674                         vm86->vm86_has_vme = 0;
675                 vm86->vm86_inited = 1;
676                 vm86->vm86_debug = sa.debug;
677                 bcopy(&sa.int_map, vm86->vm86_intmap, 32);
678                 }
679                 break;
680
681 #if 0
682         case VM86_SET_VME: {
683                 struct vm86_vme_args sa;
684         
685                 if ((cpu_feature & CPUID_VME) == 0)
686                         return (ENODEV);
687
688                 if (error = copyin(ua.sub_args, &sa, sizeof(sa)))
689                         return (error);
690                 if (sa.state)
691                         load_cr4(rcr4() | CR4_VME);
692                 else
693                         load_cr4(rcr4() & ~CR4_VME);
694                 }
695                 break;
696 #endif
697
698         case VM86_GET_VME: {
699                 struct vm86_vme_args sa;
700
701                 sa.state = (rcr4() & CR4_VME ? 1 : 0);
702                 error = copyout(&sa, ua.sub_args, sizeof(sa));
703                 }
704                 break;
705
706         case VM86_INTCALL: {
707                 struct vm86_intcall_args sa;
708
709                 if ((error = priv_check(td, PRIV_VM86_INTCALL)))
710                         return (error);
711                 if ((error = copyin(ua.sub_args, &sa, sizeof(sa))))
712                         return (error);
713                 if ((error = vm86_intcall(sa.intnum, &sa.vmf)))
714                         return (error);
715                 error = copyout(&sa, ua.sub_args, sizeof(sa));
716                 }
717                 break;
718
719         default:
720                 error = EINVAL;
721         }
722         return (error);
723 }