b393fa3609401342189b481387e8f5e44e512db4
[dragonfly.git] / sys / platform / pc32 / i386 / vm86.c
1 /*-
2  * Copyright (c) 1997 Jonathan Lemon
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * modification, are permitted provided that the following conditions
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/i386/i386/vm86.c,v 1.31.2.2 2001/10/05 06:18:55 peter Exp $
28  * $DragonFly: src/sys/platform/pc32/i386/vm86.c,v 1.26 2008/08/02 01:14:43 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/proc.h>
35 #include <sys/priv.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/sysctl.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 <sys/user.h>
46
47 #include <sys/thread2.h>
48 #include <sys/mplock2.h>
49
50 #include <machine/md_var.h>
51 #include <machine/pcb_ext.h>    /* pcb.h included via sys/user.h */
52 #include <machine/psl.h>
53 #include <machine/specialreg.h>
54 #include <machine/sysarch.h>
55 #include <machine/clock.h>
56 #include <bus/isa/isa.h>
57 #include <bus/isa/rtc.h>
58 #include <machine_base/isa/timerreg.h>
59
60 extern int i386_extend_pcb      (struct lwp *);
61 extern int vm86pa;
62 extern struct pcb *vm86pcb;
63
64 extern int vm86_bioscall(struct vm86frame *);
65 extern void vm86_biosret(struct vm86frame *);
66
67 #define PGTABLE_SIZE    ((1024 + 64) * 1024 / PAGE_SIZE)
68 #define INTMAP_SIZE     32
69 #define IOMAP_SIZE      ctob(IOPAGES)
70 #define TSS_SIZE \
71         (sizeof(struct pcb_ext) - sizeof(struct segment_descriptor) + \
72          INTMAP_SIZE + IOMAP_SIZE + 1)
73
74 struct vm86_layout {
75         pt_entry_t      vml_pgtbl[PGTABLE_SIZE];
76         struct  pcb vml_pcb;
77         struct  pcb_ext vml_ext;
78         char    vml_intmap[INTMAP_SIZE];
79         char    vml_iomap[IOMAP_SIZE];
80         char    vml_iomap_trailer;
81 };
82
83 void vm86_prepcall(struct vm86frame *);
84
85 struct system_map {
86         int             type;
87         vm_offset_t     start;
88         vm_offset_t     end;
89 };
90
91 #define HLT     0xf4
92 #define CLI     0xfa
93 #define STI     0xfb
94 #define PUSHF   0x9c
95 #define POPF    0x9d
96 #define INTn    0xcd
97 #define IRET    0xcf
98 #define INB     0xe4
99 #define INW     0xe5
100 #define INBDX   0xec
101 #define INWDX   0xed
102 #define OUTB    0xe6
103 #define OUTW    0xe7
104 #define OUTBDX  0xee
105 #define OUTWDX  0xef
106 #define CALLm   0xff
107 #define OPERAND_SIZE_PREFIX     0x66
108 #define ADDRESS_SIZE_PREFIX     0x67
109 #define PUSH_MASK       ~(PSL_VM | PSL_RF | PSL_I)
110 #define POP_MASK        ~(PSL_VIP | PSL_VIF | PSL_VM | PSL_RF | PSL_IOPL)
111
112 static void vm86_setup_timer_fault(void);
113 static void vm86_clear_timer_fault(void);
114
115 static int vm86_blew_up_timer;
116
117 static int timer_warn = 1;
118 SYSCTL_INT(_debug, OID_AUTO, timer_warn, CTLFLAG_RW, &timer_warn, 0, "");
119
120 static __inline caddr_t
121 MAKE_ADDR(u_short sel, u_short off)
122 {
123         return ((caddr_t)((sel << 4) + off));
124 }
125
126 static __inline void
127 GET_VEC(u_int vec, u_short *sel, u_short *off)
128 {
129         *sel = vec >> 16;
130         *off = vec & 0xffff;
131 }
132
133 static __inline u_int
134 MAKE_VEC(u_short sel, u_short off)
135 {
136         return ((sel << 16) | off);
137 }
138
139 static __inline void
140 PUSH(u_short x, struct vm86frame *vmf)
141 {
142         vmf->vmf_sp -= 2;
143         susword(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp), x);
144 }
145
146 static __inline void
147 PUSHL(u_int x, struct vm86frame *vmf)
148 {
149         vmf->vmf_sp -= 4;
150         suword(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp), x);
151 }
152
153 static __inline u_short
154 POP(struct vm86frame *vmf)
155 {
156         u_short x = fusword(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp));
157
158         vmf->vmf_sp += 2;
159         return (x);
160 }
161
162 static __inline u_int
163 POPL(struct vm86frame *vmf)
164 {
165         u_int x = fuword(MAKE_ADDR(vmf->vmf_ss, vmf->vmf_sp));
166
167         vmf->vmf_sp += 4;
168         return (x);
169 }
170
171 /*
172  * MPSAFE
173  */
174 int
175 vm86_emulate(struct vm86frame *vmf)
176 {
177         struct vm86_kernel *vm86;
178         caddr_t addr;
179         u_char i_byte;
180         u_int temp_flags;
181         int inc_ip = 1;
182         int retcode = 0;
183
184         /*
185          * pcb_ext contains the address of the extension area, or zero if
186          * the extension is not present.  (This check should not be needed,
187          * as we can't enter vm86 mode until we set up an extension area)
188          */
189         if (curthread->td_pcb->pcb_ext == 0)
190                 return (SIGBUS);
191         vm86 = &curthread->td_pcb->pcb_ext->ext_vm86;
192
193         if (vmf->vmf_eflags & PSL_T)
194                 retcode = SIGTRAP;
195
196         /*
197          * Instruction emulation
198          */
199         addr = MAKE_ADDR(vmf->vmf_cs, vmf->vmf_ip);
200         i_byte = fubyte(addr);
201         if (i_byte == ADDRESS_SIZE_PREFIX) {
202                 i_byte = fubyte(++addr);
203                 inc_ip++;
204         }
205
206         /*
207          * I/O emulation (TIMER only, a big hack).  Just reenable the
208          * IO bits involved, flag it, and retry the instruction.
209          */
210         switch(i_byte) {
211         case OUTB:
212         case OUTW:
213         case OUTBDX:
214         case OUTWDX:
215                 vm86_blew_up_timer = 1;
216                 /* fall through */
217         case INB:
218         case INW:
219         case INBDX:
220         case INWDX:
221                 vm86_clear_timer_fault();
222                 /* retry insn */
223                 return(0);
224         }
225
226         if (vm86->vm86_has_vme) {
227                 switch (i_byte) {
228                 case OPERAND_SIZE_PREFIX:
229                         i_byte = fubyte(++addr);
230                         inc_ip++;
231                         switch (i_byte) {
232                         case PUSHF:
233                                 if (vmf->vmf_eflags & PSL_VIF)
234                                         PUSHL((vmf->vmf_eflags & PUSH_MASK)
235                                             | PSL_IOPL | PSL_I, vmf);
236                                 else
237                                         PUSHL((vmf->vmf_eflags & PUSH_MASK)
238                                             | PSL_IOPL, vmf);
239                                 vmf->vmf_ip += inc_ip;
240                                 return (0);
241
242                         case POPF:
243                                 temp_flags = POPL(vmf) & POP_MASK;
244                                 vmf->vmf_eflags = (vmf->vmf_eflags & ~POP_MASK)
245                                     | temp_flags | PSL_VM | PSL_I;
246                                 vmf->vmf_ip += inc_ip;
247                                 if (temp_flags & PSL_I) {
248                                         vmf->vmf_eflags |= PSL_VIF;
249                                         if (vmf->vmf_eflags & PSL_VIP)
250                                                 break;
251                                 } else {
252                                         vmf->vmf_eflags &= ~PSL_VIF;
253                                 }
254                                 return (0);
255                         }
256                         break;
257
258                 /* VME faults here if VIP is set, but does not set VIF. */
259                 case STI:
260                         vmf->vmf_eflags |= PSL_VIF;
261                         vmf->vmf_ip += inc_ip;
262                         if ((vmf->vmf_eflags & PSL_VIP) == 0) {
263                                 uprintf("fatal sti\n");
264                                 return (SIGKILL);
265                         }
266                         break;
267
268                 /* VME if no redirection support */
269                 case INTn:
270                         break;
271
272                 /* VME if trying to set PSL_TF, or PSL_I when VIP is set */
273                 case POPF:
274                         temp_flags = POP(vmf) & POP_MASK;
275                         vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
276                             | temp_flags | PSL_VM | PSL_I;
277                         vmf->vmf_ip += inc_ip;
278                         if (temp_flags & PSL_I) {
279                                 vmf->vmf_eflags |= PSL_VIF;
280                                 if (vmf->vmf_eflags & PSL_VIP)
281                                         break;
282                         } else {
283                                 vmf->vmf_eflags &= ~PSL_VIF;
284                         }
285                         return (retcode);
286
287                 /* VME if trying to set PSL_TF, or PSL_I when VIP is set */
288                 case IRET:
289                         vmf->vmf_ip = POP(vmf);
290                         vmf->vmf_cs = POP(vmf);
291                         temp_flags = POP(vmf) & POP_MASK;
292                         vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
293                             | temp_flags | PSL_VM | PSL_I;
294                         if (temp_flags & PSL_I) {
295                                 vmf->vmf_eflags |= PSL_VIF;
296                                 if (vmf->vmf_eflags & PSL_VIP)
297                                         break;
298                         } else {
299                                 vmf->vmf_eflags &= ~PSL_VIF;
300                         }
301                         return (retcode);
302
303                 }
304                 return (SIGBUS);
305         }
306
307         switch (i_byte) {
308         case OPERAND_SIZE_PREFIX:
309                 i_byte = fubyte(++addr);
310                 inc_ip++;
311                 switch (i_byte) {
312                 case PUSHF:
313                         if (vm86->vm86_eflags & PSL_VIF)
314                                 PUSHL((vmf->vmf_flags & PUSH_MASK)
315                                     | PSL_IOPL | PSL_I, vmf);
316                         else
317                                 PUSHL((vmf->vmf_flags & PUSH_MASK)
318                                     | PSL_IOPL, vmf);
319                         vmf->vmf_ip += inc_ip;
320                         return (retcode);
321
322                 case POPF:
323                         temp_flags = POPL(vmf) & POP_MASK;
324                         vmf->vmf_eflags = (vmf->vmf_eflags & ~POP_MASK)
325                             | temp_flags | PSL_VM | PSL_I;
326                         vmf->vmf_ip += inc_ip;
327                         if (temp_flags & PSL_I) {
328                                 vm86->vm86_eflags |= PSL_VIF;
329                                 if (vm86->vm86_eflags & PSL_VIP)
330                                         break;
331                         } else {
332                                 vm86->vm86_eflags &= ~PSL_VIF;
333                         }
334                         return (retcode);
335                 }
336                 return (SIGBUS);
337
338         case CLI:
339                 vm86->vm86_eflags &= ~PSL_VIF;
340                 vmf->vmf_ip += inc_ip;
341                 return (retcode);
342
343         case STI:
344                 /* if there is a pending interrupt, go to the emulator */
345                 vm86->vm86_eflags |= PSL_VIF;
346                 vmf->vmf_ip += inc_ip;
347                 if (vm86->vm86_eflags & PSL_VIP)
348                         break;
349                 return (retcode);
350
351         case PUSHF:
352                 if (vm86->vm86_eflags & PSL_VIF)
353                         PUSH((vmf->vmf_flags & PUSH_MASK)
354                             | PSL_IOPL | PSL_I, vmf);
355                 else
356                         PUSH((vmf->vmf_flags & PUSH_MASK) | PSL_IOPL, vmf);
357                 vmf->vmf_ip += inc_ip;
358                 return (retcode);
359
360         case INTn:
361                 i_byte = fubyte(addr + 1);
362                 if ((vm86->vm86_intmap[i_byte >> 3] & (1 << (i_byte & 7))) != 0)
363                         break;
364                 if (vm86->vm86_eflags & PSL_VIF)
365                         PUSH((vmf->vmf_flags & PUSH_MASK)
366                             | PSL_IOPL | PSL_I, vmf);
367                 else
368                         PUSH((vmf->vmf_flags & PUSH_MASK) | PSL_IOPL, vmf);
369                 PUSH(vmf->vmf_cs, vmf);
370                 PUSH(vmf->vmf_ip + inc_ip + 1, vmf);    /* increment IP */
371                 GET_VEC(fuword((caddr_t)(i_byte * 4)),
372                      &vmf->vmf_cs, &vmf->vmf_ip);
373                 vmf->vmf_flags &= ~PSL_T;
374                 vm86->vm86_eflags &= ~PSL_VIF;
375                 return (retcode);
376
377         case IRET:
378                 vmf->vmf_ip = POP(vmf);
379                 vmf->vmf_cs = POP(vmf);
380                 temp_flags = POP(vmf) & POP_MASK;
381                 vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
382                     | temp_flags | PSL_VM | PSL_I;
383                 if (temp_flags & PSL_I) {
384                         vm86->vm86_eflags |= PSL_VIF;
385                         if (vm86->vm86_eflags & PSL_VIP)
386                                 break;
387                 } else {
388                         vm86->vm86_eflags &= ~PSL_VIF;
389                 }
390                 return (retcode);
391
392         case POPF:
393                 temp_flags = POP(vmf) & POP_MASK;
394                 vmf->vmf_flags = (vmf->vmf_flags & ~POP_MASK)
395                     | temp_flags | PSL_VM | PSL_I;
396                 vmf->vmf_ip += inc_ip;
397                 if (temp_flags & PSL_I) {
398                         vm86->vm86_eflags |= PSL_VIF;
399                         if (vm86->vm86_eflags & PSL_VIP)
400                                 break;
401                 } else {
402                         vm86->vm86_eflags &= ~PSL_VIF;
403                 }
404                 return (retcode);
405         }
406         return (SIGBUS);
407 }
408
409 void
410 vm86_initialize(void)
411 {
412         int i;
413         u_int *addr;
414         struct vm86_layout *vml = (struct vm86_layout *)vm86paddr;
415         struct pcb *pcb;
416         struct pcb_ext *ext;
417         struct soft_segment_descriptor ssd = {
418                 0,                      /* segment base address (overwritten) */
419                 0,                      /* length (overwritten) */
420                 SDT_SYS386TSS,          /* segment type */
421                 0,                      /* priority level */
422                 1,                      /* descriptor present */
423                 0, 0,
424                 0,                      /* default 16 size */
425                 0                       /* granularity */
426         };
427
428         /*
429          * this should be a compile time error, but cpp doesn't grok sizeof().
430          */
431         if (sizeof(struct vm86_layout) > ctob(3))
432                 panic("struct vm86_layout exceeds space allocated in locore.s");
433
434         /*
435          * Below is the memory layout that we use for the vm86 region.
436          *
437          * +--------+
438          * |        | 
439          * |        |
440          * | page 0 |       
441          * |        | +--------+
442          * |        | | stack  |
443          * +--------+ +--------+ <--------- vm86paddr
444          * |        | |Page Tbl| 1M + 64K = 272 entries = 1088 bytes
445          * |        | +--------+
446          * |        | |  PCB   | size: ~240 bytes
447          * | page 1 | |PCB Ext | size: ~140 bytes (includes TSS)
448          * |        | +--------+
449          * |        | |int map |
450          * |        | +--------+
451          * +--------+ |        |
452          * | page 2 | |  I/O   |
453          * +--------+ | bitmap |
454          * | page 3 | |        |
455          * |        | +--------+
456          * +--------+ 
457          */
458
459         /*
460          * A rudimentary PCB must be installed, in order to get to the
461          * PCB extension area.  We use the PCB area as a scratchpad for
462          * data storage, the layout of which is shown below.
463          *
464          * pcb_esi      = new PTD entry 0
465          * pcb_ebp      = pointer to frame on vm86 stack
466          * pcb_esp      =    stack frame pointer at time of switch
467          * pcb_ebx      = va of vm86 page table
468          * pcb_eip      =    argument pointer to initial call
469          * pcb_spare[0] =    saved TSS descriptor, word 0
470          * pcb_space[1] =    saved TSS descriptor, word 1
471          */
472 #define new_ptd         pcb_esi
473 #define vm86_frame      pcb_ebp
474 #define pgtable_va      pcb_ebx
475
476         pcb = &vml->vml_pcb;
477         ext = &vml->vml_ext;
478
479         bzero(pcb, sizeof(struct pcb));
480         pcb->new_ptd = vm86pa | PG_V | PG_RW | PG_U;
481         pcb->vm86_frame = (pt_entry_t)vm86paddr - sizeof(struct vm86frame);
482         pcb->pgtable_va = (vm_offset_t)vm86paddr;
483         pcb->pcb_ext = ext;
484
485         bzero(ext, sizeof(struct pcb_ext)); 
486         ext->ext_tss.tss_esp0 = (vm_offset_t)vm86paddr;
487         ext->ext_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
488         ext->ext_tss.tss_ioopt = 
489                 ((u_int)vml->vml_iomap - (u_int)&ext->ext_tss) << 16;
490         ext->ext_iomap = vml->vml_iomap;
491         ext->ext_vm86.vm86_intmap = vml->vml_intmap;
492
493         if (cpu_feature & CPUID_VME)
494                 ext->ext_vm86.vm86_has_vme = (rcr4() & CR4_VME ? 1 : 0);
495
496         addr = (u_int *)ext->ext_vm86.vm86_intmap;
497         for (i = 0; i < (INTMAP_SIZE + IOMAP_SIZE) / sizeof(u_int); i++)
498                 *addr++ = 0;
499         vml->vml_iomap_trailer = 0xff;
500
501         ssd.ssd_base = (u_int)&ext->ext_tss;
502         ssd.ssd_limit = TSS_SIZE - 1; 
503         ssdtosd(&ssd, &ext->ext_tssd);
504
505         vm86pcb = pcb;
506
507 #if 0
508         /*
509          * use whatever is leftover of the vm86 page layout as a
510          * message buffer so we can capture early output.
511          */
512         msgbufinit((vm_offset_t)vm86paddr + sizeof(struct vm86_layout),
513             ctob(3) - sizeof(struct vm86_layout));
514 #endif
515 }
516
517 vm_offset_t
518 vm86_getpage(struct vm86context *vmc, int pagenum)
519 {
520         int i;
521
522         for (i = 0; i < vmc->npages; i++)
523                 if (vmc->pmap[i].pte_num == pagenum)
524                         return (vmc->pmap[i].kva);
525         return (0);
526 }
527
528 vm_offset_t
529 vm86_addpage(struct vm86context *vmc, int pagenum, vm_offset_t kva)
530 {
531         int i, flags = 0;
532
533         for (i = 0; i < vmc->npages; i++)
534                 if (vmc->pmap[i].pte_num == pagenum)
535                         goto bad;
536
537         if (vmc->npages == VM86_PMAPSIZE)
538                 goto bad;                       /* XXX grow map? */
539
540         if (kva == 0) {
541                 kva = (vm_offset_t)kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
542                 flags = VMAP_MALLOC;
543         }
544
545         i = vmc->npages++;
546         vmc->pmap[i].flags = flags;
547         vmc->pmap[i].kva = kva;
548         vmc->pmap[i].pte_num = pagenum;
549         return (kva);
550 bad:
551         panic("vm86_addpage: not enough room, or overlap");
552 }
553
554 static void
555 vm86_initflags(struct vm86frame *vmf)
556 {
557         int eflags = vmf->vmf_eflags;
558         struct vm86_kernel *vm86 = &curthread->td_pcb->pcb_ext->ext_vm86;
559
560         if (vm86->vm86_has_vme) {
561                 eflags = (vmf->vmf_eflags & ~VME_USERCHANGE) |
562                     (eflags & VME_USERCHANGE) | PSL_VM;
563         } else {
564                 vm86->vm86_eflags = eflags;     /* save VIF, VIP */
565                 eflags = (vmf->vmf_eflags & ~VM_USERCHANGE) |             
566                     (eflags & VM_USERCHANGE) | PSL_VM;
567         }
568         vmf->vmf_eflags = eflags | PSL_VM;
569 }
570
571 /*
572  * called from vm86_bioscall, while in vm86 address space, to finalize setup.
573  */
574 void
575 vm86_prepcall(struct vm86frame *vmf)
576 {
577         uintptr_t addr[] = { 0xA00, 0x1000 };   /* code, stack */
578         u_char intcall[] = {
579                 CLI, INTn, 0x00, STI, HLT
580         };
581
582         if ((vmf->vmf_trapno & PAGE_MASK) <= 0xff) {
583                 /* interrupt call requested */
584                 intcall[2] = (u_char)(vmf->vmf_trapno & 0xff);
585                 memcpy((void *)addr[0], (void *)intcall, sizeof(intcall));
586                 vmf->vmf_ip = addr[0];
587                 vmf->vmf_cs = 0;
588         }
589         vmf->vmf_sp = addr[1] - 2;              /* keep aligned */
590         vmf->kernel_fs = vmf->kernel_es = vmf->kernel_ds = vmf->kernel_gs = 0;
591         vmf->vmf_ss = 0;
592         vmf->vmf_eflags = PSL_VIF | PSL_VM | PSL_USER;
593         vm86_initflags(vmf);
594 }
595
596 /*
597  * vm86 trap handler; determines whether routine succeeded or not.
598  * Called while in vm86 space, returns to calling process.
599  *
600  * A MP lock ref is held on entry from trap() and must be released prior
601  * to returning to the VM86 call.
602  */
603 void
604 vm86_trap(struct vm86frame *vmf, int have_mplock)
605 {
606         caddr_t addr;
607
608         /* "should not happen" */
609         if ((vmf->vmf_eflags & PSL_VM) == 0)
610                 panic("vm86_trap called, but not in vm86 mode");
611
612         addr = MAKE_ADDR(vmf->vmf_cs, vmf->vmf_ip);
613         if (*(u_char *)addr == HLT)
614                 vmf->vmf_trapno = vmf->vmf_eflags & PSL_C;
615         else
616                 vmf->vmf_trapno = vmf->vmf_trapno << 16;
617
618         if (have_mplock)
619                 rel_mplock();
620         vm86_biosret(vmf);
621 }
622
623 int
624 vm86_intcall(int intnum, struct vm86frame *vmf)
625 {
626         int error;
627
628         if (intnum < 0 || intnum > 0xff)
629                 return (EINVAL);
630
631         crit_enter();
632         ASSERT_MP_LOCK_HELD(curthread);
633
634         vm86_setup_timer_fault();
635         vmf->vmf_trapno = intnum;
636         error = vm86_bioscall(vmf);
637
638         /*
639          * Yes, this happens, especially with video BIOS calls.  The BIOS
640          * will sometimes eat timer 2 for lunch, and we need timer 2.
641          */
642         if (vm86_blew_up_timer) {
643                 vm86_blew_up_timer = 0;
644                 timer_restore();
645                 if (timer_warn) {
646                         kprintf("Warning: BIOS played with the 8254, "
647                                 "resetting it\n");
648                 }
649         }
650         crit_exit();
651         return(error);
652 }
653
654 /*
655  * struct vm86context contains the page table to use when making
656  * vm86 calls.  If intnum is a valid interrupt number (0-255), then
657  * the "interrupt trampoline" will be used, otherwise we use the
658  * caller's cs:ip routine.  
659  */
660 int
661 vm86_datacall(int intnum, struct vm86frame *vmf, struct vm86context *vmc)
662 {
663         pt_entry_t *pte = vm86paddr;
664         u_int page;
665         int i, entry, retval;
666
667         crit_enter();
668         ASSERT_MP_LOCK_HELD(curthread);
669
670         for (i = 0; i < vmc->npages; i++) {
671                 page = vtophys(vmc->pmap[i].kva & PG_FRAME);
672                 entry = vmc->pmap[i].pte_num; 
673                 vmc->pmap[i].old_pte = pte[entry];
674                 pte[entry] = page | PG_V | PG_RW | PG_U;
675         }
676
677         vmf->vmf_trapno = intnum;
678         retval = vm86_bioscall(vmf);
679
680         for (i = 0; i < vmc->npages; i++) {
681                 entry = vmc->pmap[i].pte_num;
682                 pte[entry] = vmc->pmap[i].old_pte;
683         }
684         crit_exit();
685         return (retval);
686 }
687
688 vm_offset_t
689 vm86_getaddr(struct vm86context *vmc, u_short sel, u_short off)
690 {
691         int i, page;
692         vm_offset_t addr;
693
694         addr = (vm_offset_t)MAKE_ADDR(sel, off);
695         page = addr >> PAGE_SHIFT;
696         for (i = 0; i < vmc->npages; i++)
697                 if (page == vmc->pmap[i].pte_num)
698                         return (vmc->pmap[i].kva + (addr & PAGE_MASK));
699         return (0);
700 }
701
702 int
703 vm86_getptr(struct vm86context *vmc, vm_offset_t kva, u_short *sel,
704             u_short *off)
705 {
706         int i;
707
708         for (i = 0; i < vmc->npages; i++)
709                 if (kva >= vmc->pmap[i].kva &&
710                     kva < vmc->pmap[i].kva + PAGE_SIZE) {
711                         *off = kva - vmc->pmap[i].kva;
712                         *sel = vmc->pmap[i].pte_num << 8;
713                         return (1);
714                 }
715         return (0);
716         panic("vm86_getptr: address not found");
717 }
718         
719 int
720 vm86_sysarch(struct lwp *lp, char *args)
721 {
722         int error = 0;
723         struct i386_vm86_args ua;
724         struct vm86_kernel *vm86;
725
726         if ((error = copyin(args, &ua, sizeof(struct i386_vm86_args))) != 0)
727                 return (error);
728
729         if (lp->lwp_thread->td_pcb->pcb_ext == 0)
730                 if ((error = i386_extend_pcb(lp)) != 0)
731                         return (error);
732         vm86 = &lp->lwp_thread->td_pcb->pcb_ext->ext_vm86;
733
734         switch (ua.sub_op) {
735         case VM86_INIT: {
736                 struct vm86_init_args sa;
737
738                 if ((error = copyin(ua.sub_args, &sa, sizeof(sa))) != 0)
739                         return (error);
740                 if (cpu_feature & CPUID_VME)
741                         vm86->vm86_has_vme = (rcr4() & CR4_VME ? 1 : 0);
742                 else
743                         vm86->vm86_has_vme = 0;
744                 vm86->vm86_inited = 1;
745                 vm86->vm86_debug = sa.debug;
746                 bcopy(&sa.int_map, vm86->vm86_intmap, 32);
747                 }
748                 break;
749
750 #if 0
751         case VM86_SET_VME: {
752                 struct vm86_vme_args sa;
753         
754                 if ((cpu_feature & CPUID_VME) == 0)
755                         return (ENODEV);
756
757                 if (error = copyin(ua.sub_args, &sa, sizeof(sa)))
758                         return (error);
759                 if (sa.state)
760                         load_cr4(rcr4() | CR4_VME);
761                 else
762                         load_cr4(rcr4() & ~CR4_VME);
763                 }
764                 break;
765 #endif
766
767         case VM86_GET_VME: {
768                 struct vm86_vme_args sa;
769
770                 sa.state = (rcr4() & CR4_VME ? 1 : 0);
771                 error = copyout(&sa, ua.sub_args, sizeof(sa));
772                 }
773                 break;
774
775         case VM86_INTCALL: {
776                 struct vm86_intcall_args sa;
777
778                 if ((error = priv_check_cred(lp->lwp_proc->p_ucred, PRIV_ROOT, 0)))
779                         return (error);
780                 if ((error = copyin(ua.sub_args, &sa, sizeof(sa))))
781                         return (error);
782                 if ((error = vm86_intcall(sa.intnum, &sa.vmf)))
783                         return (error);
784                 error = copyout(&sa, ua.sub_args, sizeof(sa));
785                 }
786                 break;
787
788         default:
789                 error = EINVAL;
790         }
791         return (error);
792 }
793
794 /*
795  * Setup the VM86 I/O map to take faults on the timer
796  */
797 static void
798 vm86_setup_timer_fault(void)
799 {
800         struct vm86_layout *vml = (struct vm86_layout *)vm86paddr;
801
802         vml->vml_iomap[TIMER_MODE >> 3] |= 1 << (TIMER_MODE & 7);
803         vml->vml_iomap[TIMER_CNTR0 >> 3] |= 1 << (TIMER_CNTR0 & 7);
804         vml->vml_iomap[TIMER_CNTR1 >> 3] |= 1 << (TIMER_CNTR1 & 7);
805         vml->vml_iomap[TIMER_CNTR2 >> 3] |= 1 << (TIMER_CNTR2 & 7);
806 }
807
808 /*
809  * Setup the VM86 I/O map to not fault on the timer
810  */
811 static void
812 vm86_clear_timer_fault(void)
813 {
814         struct vm86_layout *vml = (struct vm86_layout *)vm86paddr;
815
816         vml->vml_iomap[TIMER_MODE >> 3] &= ~(1 << (TIMER_MODE & 7));
817         vml->vml_iomap[TIMER_CNTR0 >> 3] &= ~(1 << (TIMER_CNTR0 & 7));
818         vml->vml_iomap[TIMER_CNTR1 >> 3] &= ~(1 << (TIMER_CNTR1 & 7));
819         vml->vml_iomap[TIMER_CNTR2 >> 3] &= ~(1 << (TIMER_CNTR2 & 7));
820 }
821