2 * Copyright (c) 1997 Jonathan Lemon
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
40 #include <vm/vm_map.h>
41 #include <vm/vm_page.h>
43 #include <machine/md_var.h>
44 #include <machine/pcb.h>
45 #include <machine/pcb_ext.h>
46 #include <machine/psl.h>
47 #include <machine/specialreg.h>
48 #include <machine/sysarch.h>
51 extern struct pcb
*vm86pcb
;
53 static struct mtx vm86_lock
;
55 extern int vm86_bioscall(struct vm86frame
*);
56 extern void vm86_biosret(struct vm86frame
*);
58 void vm86_prepcall(struct vm86frame
*);
74 #define OPERAND_SIZE_PREFIX 0x66
75 #define ADDRESS_SIZE_PREFIX 0x67
76 #define PUSH_MASK ~(PSL_VM | PSL_RF | PSL_I)
77 #define POP_MASK ~(PSL_VIP | PSL_VIF | PSL_VM | PSL_RF | PSL_IOPL)
79 static __inline caddr_t
80 MAKE_ADDR(u_short sel
, u_short off
)
82 return ((caddr_t
)((sel
<< 4) + off
));
86 GET_VEC(u_int vec
, u_short
*sel
, u_short
*off
)
93 MAKE_VEC(u_short sel
, u_short off
)
95 return ((sel
<< 16) | off
);
99 PUSH(u_short x
, struct vm86frame
*vmf
)
102 suword16(MAKE_ADDR(vmf
->vmf_ss
, vmf
->vmf_sp
), x
);
106 PUSHL(u_int x
, struct vm86frame
*vmf
)
109 suword(MAKE_ADDR(vmf
->vmf_ss
, vmf
->vmf_sp
), x
);
112 static __inline u_short
113 POP(struct vm86frame
*vmf
)
115 u_short x
= fuword16(MAKE_ADDR(vmf
->vmf_ss
, vmf
->vmf_sp
));
121 static __inline u_int
122 POPL(struct vm86frame
*vmf
)
124 u_int x
= fuword(MAKE_ADDR(vmf
->vmf_ss
, vmf
->vmf_sp
));
132 struct vm86frame
*vmf
;
134 struct vm86_kernel
*vm86
;
142 * pcb_ext contains the address of the extension area, or zero if
143 * the extension is not present. (This check should not be needed,
144 * as we can't enter vm86 mode until we set up an extension area)
146 if (curpcb
->pcb_ext
== 0)
148 vm86
= &curpcb
->pcb_ext
->ext_vm86
;
150 if (vmf
->vmf_eflags
& PSL_T
)
153 addr
= MAKE_ADDR(vmf
->vmf_cs
, vmf
->vmf_ip
);
154 i_byte
= fubyte(addr
);
155 if (i_byte
== ADDRESS_SIZE_PREFIX
) {
156 i_byte
= fubyte(++addr
);
160 if (vm86
->vm86_has_vme
) {
162 case OPERAND_SIZE_PREFIX
:
163 i_byte
= fubyte(++addr
);
167 if (vmf
->vmf_eflags
& PSL_VIF
)
168 PUSHL((vmf
->vmf_eflags
& PUSH_MASK
)
169 | PSL_IOPL
| PSL_I
, vmf
);
171 PUSHL((vmf
->vmf_eflags
& PUSH_MASK
)
173 vmf
->vmf_ip
+= inc_ip
;
177 temp_flags
= POPL(vmf
) & POP_MASK
;
178 vmf
->vmf_eflags
= (vmf
->vmf_eflags
& ~POP_MASK
)
179 | temp_flags
| PSL_VM
| PSL_I
;
180 vmf
->vmf_ip
+= inc_ip
;
181 if (temp_flags
& PSL_I
) {
182 vmf
->vmf_eflags
|= PSL_VIF
;
183 if (vmf
->vmf_eflags
& PSL_VIP
)
186 vmf
->vmf_eflags
&= ~PSL_VIF
;
192 /* VME faults here if VIP is set, but does not set VIF. */
194 vmf
->vmf_eflags
|= PSL_VIF
;
195 vmf
->vmf_ip
+= inc_ip
;
196 if ((vmf
->vmf_eflags
& PSL_VIP
) == 0) {
197 uprintf("fatal sti\n");
202 /* VME if no redirection support */
206 /* VME if trying to set PSL_TF, or PSL_I when VIP is set */
208 temp_flags
= POP(vmf
) & POP_MASK
;
209 vmf
->vmf_flags
= (vmf
->vmf_flags
& ~POP_MASK
)
210 | temp_flags
| PSL_VM
| PSL_I
;
211 vmf
->vmf_ip
+= inc_ip
;
212 if (temp_flags
& PSL_I
) {
213 vmf
->vmf_eflags
|= PSL_VIF
;
214 if (vmf
->vmf_eflags
& PSL_VIP
)
217 vmf
->vmf_eflags
&= ~PSL_VIF
;
221 /* VME if trying to set PSL_TF, or PSL_I when VIP is set */
223 vmf
->vmf_ip
= POP(vmf
);
224 vmf
->vmf_cs
= POP(vmf
);
225 temp_flags
= POP(vmf
) & POP_MASK
;
226 vmf
->vmf_flags
= (vmf
->vmf_flags
& ~POP_MASK
)
227 | temp_flags
| PSL_VM
| PSL_I
;
228 if (temp_flags
& PSL_I
) {
229 vmf
->vmf_eflags
|= PSL_VIF
;
230 if (vmf
->vmf_eflags
& PSL_VIP
)
233 vmf
->vmf_eflags
&= ~PSL_VIF
;
242 case OPERAND_SIZE_PREFIX
:
243 i_byte
= fubyte(++addr
);
247 if (vm86
->vm86_eflags
& PSL_VIF
)
248 PUSHL((vmf
->vmf_flags
& PUSH_MASK
)
249 | PSL_IOPL
| PSL_I
, vmf
);
251 PUSHL((vmf
->vmf_flags
& PUSH_MASK
)
253 vmf
->vmf_ip
+= inc_ip
;
257 temp_flags
= POPL(vmf
) & POP_MASK
;
258 vmf
->vmf_eflags
= (vmf
->vmf_eflags
& ~POP_MASK
)
259 | temp_flags
| PSL_VM
| PSL_I
;
260 vmf
->vmf_ip
+= inc_ip
;
261 if (temp_flags
& PSL_I
) {
262 vm86
->vm86_eflags
|= PSL_VIF
;
263 if (vm86
->vm86_eflags
& PSL_VIP
)
266 vm86
->vm86_eflags
&= ~PSL_VIF
;
273 vm86
->vm86_eflags
&= ~PSL_VIF
;
274 vmf
->vmf_ip
+= inc_ip
;
278 /* if there is a pending interrupt, go to the emulator */
279 vm86
->vm86_eflags
|= PSL_VIF
;
280 vmf
->vmf_ip
+= inc_ip
;
281 if (vm86
->vm86_eflags
& PSL_VIP
)
286 if (vm86
->vm86_eflags
& PSL_VIF
)
287 PUSH((vmf
->vmf_flags
& PUSH_MASK
)
288 | PSL_IOPL
| PSL_I
, vmf
);
290 PUSH((vmf
->vmf_flags
& PUSH_MASK
) | PSL_IOPL
, vmf
);
291 vmf
->vmf_ip
+= inc_ip
;
295 i_byte
= fubyte(addr
+ 1);
296 if ((vm86
->vm86_intmap
[i_byte
>> 3] & (1 << (i_byte
& 7))) != 0)
298 if (vm86
->vm86_eflags
& PSL_VIF
)
299 PUSH((vmf
->vmf_flags
& PUSH_MASK
)
300 | PSL_IOPL
| PSL_I
, vmf
);
302 PUSH((vmf
->vmf_flags
& PUSH_MASK
) | PSL_IOPL
, vmf
);
303 PUSH(vmf
->vmf_cs
, vmf
);
304 PUSH(vmf
->vmf_ip
+ inc_ip
+ 1, vmf
); /* increment IP */
305 GET_VEC(fuword((caddr_t
)(i_byte
* 4)),
306 &vmf
->vmf_cs
, &vmf
->vmf_ip
);
307 vmf
->vmf_flags
&= ~PSL_T
;
308 vm86
->vm86_eflags
&= ~PSL_VIF
;
312 vmf
->vmf_ip
= POP(vmf
);
313 vmf
->vmf_cs
= POP(vmf
);
314 temp_flags
= POP(vmf
) & POP_MASK
;
315 vmf
->vmf_flags
= (vmf
->vmf_flags
& ~POP_MASK
)
316 | temp_flags
| PSL_VM
| PSL_I
;
317 if (temp_flags
& PSL_I
) {
318 vm86
->vm86_eflags
|= PSL_VIF
;
319 if (vm86
->vm86_eflags
& PSL_VIP
)
322 vm86
->vm86_eflags
&= ~PSL_VIF
;
327 temp_flags
= POP(vmf
) & POP_MASK
;
328 vmf
->vmf_flags
= (vmf
->vmf_flags
& ~POP_MASK
)
329 | temp_flags
| PSL_VM
| PSL_I
;
330 vmf
->vmf_ip
+= inc_ip
;
331 if (temp_flags
& PSL_I
) {
332 vm86
->vm86_eflags
|= PSL_VIF
;
333 if (vm86
->vm86_eflags
& PSL_VIP
)
336 vm86
->vm86_eflags
&= ~PSL_VIF
;
343 #define PGTABLE_SIZE ((1024 + 64) * 1024 / PAGE_SIZE)
344 #define INTMAP_SIZE 32
345 #define IOMAP_SIZE ctob(IOPAGES)
347 (sizeof(struct pcb_ext) - sizeof(struct segment_descriptor) + \
348 INTMAP_SIZE + IOMAP_SIZE + 1)
351 pt_entry_t vml_pgtbl
[PGTABLE_SIZE
];
353 struct pcb_ext vml_ext
;
354 char vml_intmap
[INTMAP_SIZE
];
355 char vml_iomap
[IOMAP_SIZE
];
356 char vml_iomap_trailer
;
360 vm86_initialize(void)
364 struct vm86_layout
*vml
= (struct vm86_layout
*)vm86paddr
;
367 struct soft_segment_descriptor ssd
= {
368 0, /* segment base address (overwritten) */
369 0, /* length (overwritten) */
370 SDT_SYS386TSS
, /* segment type */
371 0, /* priority level */
372 1, /* descriptor present */
374 0, /* default 16 size */
379 * this should be a compile time error, but cpp doesn't grok sizeof().
381 if (sizeof(struct vm86_layout
) > ctob(3))
382 panic("struct vm86_layout exceeds space allocated in locore.s");
385 * Below is the memory layout that we use for the vm86 region.
393 * +--------+ +--------+ <--------- vm86paddr
394 * | | |Page Tbl| 1M + 64K = 272 entries = 1088 bytes
396 * | | | PCB | size: ~240 bytes
397 * | page 1 | |PCB Ext | size: ~140 bytes (includes TSS)
403 * +--------+ | bitmap |
410 * A rudimentary PCB must be installed, in order to get to the
411 * PCB extension area. We use the PCB area as a scratchpad for
412 * data storage, the layout of which is shown below.
414 * pcb_esi = new PTD entry 0
415 * pcb_ebp = pointer to frame on vm86 stack
416 * pcb_esp = stack frame pointer at time of switch
417 * pcb_ebx = va of vm86 page table
418 * pcb_eip = argument pointer to initial call
419 * pcb_spare[0] = saved TSS descriptor, word 0
420 * pcb_space[1] = saved TSS descriptor, word 1
422 #define new_ptd pcb_esi
423 #define vm86_frame pcb_ebp
424 #define pgtable_va pcb_ebx
429 mtx_init(&vm86_lock
, "vm86 lock", NULL
, MTX_DEF
);
431 bzero(pcb
, sizeof(struct pcb
));
432 pcb
->new_ptd
= vm86pa
| PG_V
| PG_RW
| PG_U
;
433 pcb
->vm86_frame
= vm86paddr
- sizeof(struct vm86frame
);
434 pcb
->pgtable_va
= vm86paddr
;
435 pcb
->pcb_flags
= PCB_VM86CALL
;
438 bzero(ext
, sizeof(struct pcb_ext
));
439 ext
->ext_tss
.tss_esp0
= vm86paddr
;
440 ext
->ext_tss
.tss_ss0
= GSEL(GDATA_SEL
, SEL_KPL
);
441 ext
->ext_tss
.tss_ioopt
=
442 ((u_int
)vml
->vml_iomap
- (u_int
)&ext
->ext_tss
) << 16;
443 ext
->ext_iomap
= vml
->vml_iomap
;
444 ext
->ext_vm86
.vm86_intmap
= vml
->vml_intmap
;
446 if (cpu_feature
& CPUID_VME
)
447 ext
->ext_vm86
.vm86_has_vme
= (rcr4() & CR4_VME
? 1 : 0);
449 addr
= (u_int
*)ext
->ext_vm86
.vm86_intmap
;
450 for (i
= 0; i
< (INTMAP_SIZE
+ IOMAP_SIZE
) / sizeof(u_int
); i
++)
452 vml
->vml_iomap_trailer
= 0xff;
454 ssd
.ssd_base
= (u_int
)&ext
->ext_tss
;
455 ssd
.ssd_limit
= TSS_SIZE
- 1;
456 ssdtosd(&ssd
, &ext
->ext_tssd
);
462 * use whatever is leftover of the vm86 page layout as a
463 * message buffer so we can capture early output.
465 msgbufinit((vm_offset_t
)vm86paddr
+ sizeof(struct vm86_layout
),
466 ctob(3) - sizeof(struct vm86_layout
));
471 vm86_getpage(struct vm86context
*vmc
, int pagenum
)
475 for (i
= 0; i
< vmc
->npages
; i
++)
476 if (vmc
->pmap
[i
].pte_num
== pagenum
)
477 return (vmc
->pmap
[i
].kva
);
482 vm86_addpage(struct vm86context
*vmc
, int pagenum
, vm_offset_t kva
)
486 for (i
= 0; i
< vmc
->npages
; i
++)
487 if (vmc
->pmap
[i
].pte_num
== pagenum
)
490 if (vmc
->npages
== VM86_PMAPSIZE
)
491 goto full
; /* XXX grow map? */
494 kva
= (vm_offset_t
)malloc(PAGE_SIZE
, M_TEMP
, M_WAITOK
);
499 vmc
->pmap
[i
].flags
= flags
;
500 vmc
->pmap
[i
].kva
= kva
;
501 vmc
->pmap
[i
].pte_num
= pagenum
;
504 panic("vm86_addpage: overlap");
506 panic("vm86_addpage: not enough room");
510 * called from vm86_bioscall, while in vm86 address space, to finalize setup.
513 vm86_prepcall(struct vm86frame
*vmf
)
515 struct vm86_kernel
*vm86
;
519 code
= (void *)0xa00;
520 stack
= (void *)(0x1000 - 2); /* keep aligned */
521 if ((vmf
->vmf_trapno
& PAGE_MASK
) <= 0xff) {
522 /* interrupt call requested */
524 code
[1] = vmf
->vmf_trapno
& 0xff;
526 vmf
->vmf_ip
= (uintptr_t)code
;
531 stack
[0] = MAKE_VEC(0, (uintptr_t)code
);
533 vmf
->vmf_sp
= (uintptr_t)stack
;
535 vmf
->kernel_fs
= vmf
->kernel_es
= vmf
->kernel_ds
= 0;
536 vmf
->vmf_eflags
= PSL_VIF
| PSL_VM
| PSL_USER
;
538 vm86
= &curpcb
->pcb_ext
->ext_vm86
;
539 if (!vm86
->vm86_has_vme
)
540 vm86
->vm86_eflags
= vmf
->vmf_eflags
; /* save VIF, VIP */
544 * vm86 trap handler; determines whether routine succeeded or not.
545 * Called while in vm86 space, returns to calling process.
548 vm86_trap(struct vm86frame
*vmf
)
552 /* "should not happen" */
553 if ((vmf
->vmf_eflags
& PSL_VM
) == 0)
554 panic("vm86_trap called, but not in vm86 mode");
556 addr
= MAKE_ADDR(vmf
->vmf_cs
, vmf
->vmf_ip
);
557 if (*(u_char
*)addr
== HLT
)
558 vmf
->vmf_trapno
= vmf
->vmf_eflags
& PSL_C
;
560 vmf
->vmf_trapno
= vmf
->vmf_trapno
<< 16;
566 vm86_intcall(int intnum
, struct vm86frame
*vmf
)
570 if (intnum
< 0 || intnum
> 0xff)
573 vmf
->vmf_trapno
= intnum
;
574 mtx_lock(&vm86_lock
);
576 retval
= vm86_bioscall(vmf
);
578 mtx_unlock(&vm86_lock
);
583 * struct vm86context contains the page table to use when making
584 * vm86 calls. If intnum is a valid interrupt number (0-255), then
585 * the "interrupt trampoline" will be used, otherwise we use the
586 * caller's cs:ip routine.
589 vm86_datacall(intnum
, vmf
, vmc
)
591 struct vm86frame
*vmf
;
592 struct vm86context
*vmc
;
594 pt_entry_t
*pte
= (pt_entry_t
*)vm86paddr
;
596 int i
, entry
, retval
;
598 mtx_lock(&vm86_lock
);
599 for (i
= 0; i
< vmc
->npages
; i
++) {
600 page
= vtophys(vmc
->pmap
[i
].kva
& PG_FRAME
);
601 entry
= vmc
->pmap
[i
].pte_num
;
602 vmc
->pmap
[i
].old_pte
= pte
[entry
];
603 pte
[entry
] = page
| PG_V
| PG_RW
| PG_U
;
604 pmap_invalidate_page(kernel_pmap
, vmc
->pmap
[i
].kva
);
607 vmf
->vmf_trapno
= intnum
;
609 retval
= vm86_bioscall(vmf
);
612 for (i
= 0; i
< vmc
->npages
; i
++) {
613 entry
= vmc
->pmap
[i
].pte_num
;
614 pte
[entry
] = vmc
->pmap
[i
].old_pte
;
615 pmap_invalidate_page(kernel_pmap
, vmc
->pmap
[i
].kva
);
617 mtx_unlock(&vm86_lock
);
623 vm86_getaddr(struct vm86context
*vmc
, u_short sel
, u_short off
)
628 addr
= (vm_offset_t
)MAKE_ADDR(sel
, off
);
629 page
= addr
>> PAGE_SHIFT
;
630 for (i
= 0; i
< vmc
->npages
; i
++)
631 if (page
== vmc
->pmap
[i
].pte_num
)
632 return (vmc
->pmap
[i
].kva
+ (addr
& PAGE_MASK
));
637 vm86_getptr(vmc
, kva
, sel
, off
)
638 struct vm86context
*vmc
;
645 for (i
= 0; i
< vmc
->npages
; i
++)
646 if (kva
>= vmc
->pmap
[i
].kva
&&
647 kva
< vmc
->pmap
[i
].kva
+ PAGE_SIZE
) {
648 *off
= kva
- vmc
->pmap
[i
].kva
;
649 *sel
= vmc
->pmap
[i
].pte_num
<< 8;
656 vm86_sysarch(td
, args
)
661 struct i386_vm86_args ua
;
662 struct vm86_kernel
*vm86
;
664 if ((error
= copyin(args
, &ua
, sizeof(struct i386_vm86_args
))) != 0)
667 if (td
->td_pcb
->pcb_ext
== 0)
668 if ((error
= i386_extend_pcb(td
)) != 0)
670 vm86
= &td
->td_pcb
->pcb_ext
->ext_vm86
;
674 struct vm86_init_args sa
;
676 if ((error
= copyin(ua
.sub_args
, &sa
, sizeof(sa
))) != 0)
678 if (cpu_feature
& CPUID_VME
)
679 vm86
->vm86_has_vme
= (rcr4() & CR4_VME
? 1 : 0);
681 vm86
->vm86_has_vme
= 0;
682 vm86
->vm86_inited
= 1;
683 vm86
->vm86_debug
= sa
.debug
;
684 bcopy(&sa
.int_map
, vm86
->vm86_intmap
, 32);
690 struct vm86_vme_args sa
;
692 if ((cpu_feature
& CPUID_VME
) == 0)
695 if (error
= copyin(ua
.sub_args
, &sa
, sizeof(sa
)))
698 load_cr4(rcr4() | CR4_VME
);
700 load_cr4(rcr4() & ~CR4_VME
);
706 struct vm86_vme_args sa
;
708 sa
.state
= (rcr4() & CR4_VME
? 1 : 0);
709 error
= copyout(&sa
, ua
.sub_args
, sizeof(sa
));
714 struct vm86_intcall_args sa
;
716 if ((error
= priv_check(td
, PRIV_VM86_INTCALL
)))
718 if ((error
= copyin(ua
.sub_args
, &sa
, sizeof(sa
))))
720 if ((error
= vm86_intcall(sa
.intnum
, &sa
.vmf
)))
722 error
= copyout(&sa
, ua
.sub_args
, sizeof(sa
));