vm86bios.s revision 73011
1/*-
2 * Copyright (c) 1998 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 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/i386/i386/vm86bios.s 73011 2001-02-25 06:29:04Z jake $
27 */
28
29#include "opt_npx.h"
30
31#include <machine/asmacros.h>		/* miscellaneous asm macros */
32#include <machine/trap.h>
33
34#include "assym.s"
35
36#define SCR_NEWPTD	PCB_ESI		/* readability macros */
37#define SCR_VMFRAME	PCB_EBP		/* see vm86.c for explanation */
38#define SCR_STACK	PCB_ESP
39#define SCR_PGTABLE	PCB_EBX
40#define SCR_ARGFRAME	PCB_EIP
41#define SCR_TSS0	PCB_SPARE
42#define SCR_TSS1	(PCB_SPARE+4)
43
44	.data
45	ALIGN_DATA
46
47	.globl	in_vm86call, vm86pcb
48
49in_vm86call:		.long	0
50vm86pcb:		.long	0
51
52	.text
53
54/*
55 * vm86_bioscall(struct trapframe_vm86 *vm86)
56 */
57ENTRY(vm86_bioscall)
58	movl	vm86pcb,%edx		/* scratch data area */
59	movl	4(%esp),%eax
60	movl	%eax,SCR_ARGFRAME(%edx)	/* save argument pointer */
61	pushl	%ebx
62	pushl	%ebp
63	pushl	%esi
64	pushl	%edi
65	pushl	%gs
66
67#ifdef DEV_NPX
68	movl	PCPU(CURPROC),%ecx
69	cmpl	%ecx,PCPU(NPXPROC)	/* do we need to save fp? */
70	jne	1f
71	testl	%ecx,%ecx
72	je 	1f			/* no curproc/npxproc */
73	pushl	%edx
74	movl	P_ADDR(%ecx),%ecx
75	addl	$PCB_SAVEFPU,%ecx
76	pushl	%ecx
77	call	npxsave
78	popl	%ecx
79	popl	%edx			/* recover our pcb */
80#endif
81
821:
83	movl	SCR_VMFRAME(%edx),%ebx	/* target frame location */
84	movl	%ebx,%edi		/* destination */
85	movl    SCR_ARGFRAME(%edx),%esi	/* source (set on entry) */
86	movl	$VM86_FRAMESIZE/4,%ecx	/* sizeof(struct vm86frame)/4 */
87	cld
88	rep
89	movsl				/* copy frame to new stack */
90
91	movl	PCPU(CURPCB),%eax
92	pushl	%eax			/* save curpcb */
93	movl	%edx,PCPU(CURPCB)	/* set curpcb to vm86pcb */
94
95	movl	PCPU(TSS_GDT),%ebx	/* entry in GDT */
96	movl	0(%ebx),%eax
97	movl	%eax,SCR_TSS0(%edx)	/* save first word */
98	movl	4(%ebx),%eax
99	andl    $~0x200, %eax		/* flip 386BSY -> 386TSS */
100	movl	%eax,SCR_TSS1(%edx)	/* save second word */
101
102	movl	PCB_EXT(%edx),%edi	/* vm86 tssd entry */
103	movl	0(%edi),%eax
104	movl	%eax,0(%ebx)
105	movl	4(%edi),%eax
106	movl	%eax,4(%ebx)
107	movl	$GPROC0_SEL*8,%esi	/* GSEL(entry, SEL_KPL) */
108	ltr	%si
109
110	movl	%cr3,%eax
111	pushl	%eax			/* save address space */
112	movl	IdlePTD,%ecx
113	movl	%ecx,%ebx
114	addl	$KERNBASE,%ebx		/* va of Idle PTD */
115	movl	0(%ebx),%eax
116	pushl	%eax			/* old ptde != 0 when booting */
117	pushl	%ebx			/* keep for reuse */
118
119	movl	%esp,SCR_STACK(%edx)	/* save current stack location */
120
121	movl	SCR_NEWPTD(%edx),%eax	/* mapping for vm86 page table */
122	movl	%eax,0(%ebx)		/* ... install as PTD entry 0 */
123
124	movl	%ecx,%cr3		/* new page tables */
125	movl	SCR_VMFRAME(%edx),%esp	/* switch to new stack */
126
127	call	vm86_prepcall		/* finish setup */
128
129	movl	$1,in_vm86call		/* set flag for trap() */
130
131	/*
132	 * Return via doreti
133	 */
134	MEXITCOUNT
135	jmp	doreti
136
137
138/*
139 * vm86_biosret(struct trapframe_vm86 *vm86)
140 */
141ENTRY(vm86_biosret)
142	movl	vm86pcb,%edx		/* data area */
143
144	movl	4(%esp),%esi		/* source */
145	movl	SCR_ARGFRAME(%edx),%edi	/* destination */
146	movl	$VM86_FRAMESIZE/4,%ecx	/* size */
147	cld
148	rep
149	movsl				/* copy frame to original frame */
150
151	movl	SCR_STACK(%edx),%esp	/* back to old stack */
152	popl	%ebx			/* saved va of Idle PTD */
153	popl	%eax
154	movl	%eax,0(%ebx)		/* restore old pte */
155	popl	%eax
156	movl	%eax,%cr3		/* install old page table */
157
158	movl	$0,in_vm86call		/* reset trapflag */
159
160	movl	PCPU(TSS_GDT),%ebx		/* entry in GDT */
161	movl	SCR_TSS0(%edx),%eax
162	movl	%eax,0(%ebx)		/* restore first word */
163	movl	SCR_TSS1(%edx),%eax
164	movl	%eax,4(%ebx)		/* restore second word */
165	movl	$GPROC0_SEL*8,%esi	/* GSEL(entry, SEL_KPL) */
166	ltr	%si
167
168	popl	PCPU(CURPCB)		/* restore curpcb/curproc */
169	movl	SCR_ARGFRAME(%edx),%edx	/* original stack frame */
170	movl	TF_TRAPNO(%edx),%eax	/* return (trapno) */
171
172	popl	%gs
173	popl	%edi
174	popl	%esi
175	popl	%ebp
176	popl	%ebx
177	ret				/* back to our normal program */
178