kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / i386 / i386 / vm86bios.s
CommitLineData
984263bc
MD
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: src/sys/i386/i386/vm86bios.s,v 1.15.2.1 2000/05/16 06:58:07 dillon Exp $
96728c05 27 * $DragonFly: src/sys/i386/i386/Attic/vm86bios.s,v 1.10 2003/07/08 06:27:26 dillon Exp $
984263bc
MD
28 */
29
30#include <machine/asmacros.h> /* miscellaneous asm macros */
31#include <machine/trap.h>
32
33#include "assym.s"
34
35#define SCR_NEWPTD PCB_ESI /* readability macros */
36#define SCR_VMFRAME PCB_EBP /* see vm86.c for explanation */
37#define SCR_STACK PCB_ESP
38#define SCR_PGTABLE PCB_EBX
39#define SCR_ARGFRAME PCB_EIP
40#define SCR_TSS0 PCB_SPARE
41#define SCR_TSS1 (PCB_SPARE+4)
42
43 .data
44 ALIGN_DATA
45
2954c92f 46 .globl in_vm86call, vm86pcb
984263bc 47
2954c92f
MD
48in_vm86call: .long 0
49vm86pcb: .long 0
984263bc
MD
50
51 .text
52
53/*
54 * vm86_bioscall(struct trapframe_vm86 *vm86)
55 */
56ENTRY(vm86_bioscall)
2954c92f 57 movl vm86pcb,%edx /* scratch data area */
984263bc
MD
58 movl 4(%esp),%eax
59 movl %eax,SCR_ARGFRAME(%edx) /* save argument pointer */
60 pushl %ebx
61 pushl %ebp
62 pushl %esi
63 pushl %edi
64 pushl %gs
65
984263bc 66#if NNPX > 0
2954c92f
MD
67 movl PCPU(curthread),%ecx
68 cmpl %ecx,PCPU(npxthread) /* do we need to save fp? */
984263bc
MD
69 jne 1f
70 testl %ecx,%ecx
af0bff84 71 je 1f /* no curthread/npxthread */
984263bc 72 pushl %edx
b7c628e4 73 movl TD_PCB(%ecx),%ecx
984263bc
MD
74 addl $PCB_SAVEFPU,%ecx
75 pushl %ecx
2954c92f 76 call npxsave
984263bc
MD
77 popl %ecx
78 popl %edx /* recover our pcb */
79#endif
80
af0bff84 81 /* %ecx is garbage at this point */
984263bc
MD
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
b7c628e4
MD
91 /*
92 * YYY I really dislike replacing td_pcb, even temporarily. Find
93 * another way.
94 */
2954c92f 95 movl PCPU(curthread),%ebx
b7c628e4 96 movl TD_PCB(%ebx),%eax /* save curpcb */
984263bc 97 pushl %eax /* save curpcb */
b7c628e4 98 movl %edx,TD_PCB(%ebx) /* set curpcb to vm86pcb */
984263bc 99
2954c92f 100 movl PCPU(tss_gdt),%ebx /* entry in GDT */
984263bc
MD
101 movl 0(%ebx),%eax
102 movl %eax,SCR_TSS0(%edx) /* save first word */
103 movl 4(%ebx),%eax
104 andl $~0x200, %eax /* flip 386BSY -> 386TSS */
105 movl %eax,SCR_TSS1(%edx) /* save second word */
106
107 movl PCB_EXT(%edx),%edi /* vm86 tssd entry */
108 movl 0(%edi),%eax
109 movl %eax,0(%ebx)
110 movl 4(%edi),%eax
111 movl %eax,4(%ebx)
112 movl $GPROC0_SEL*8,%esi /* GSEL(entry, SEL_KPL) */
113 ltr %si
114
115 movl %cr3,%eax
116 pushl %eax /* save address space */
2954c92f 117 movl IdlePTD,%ecx
984263bc
MD
118 movl %ecx,%ebx
119 addl $KERNBASE,%ebx /* va of Idle PTD */
120 movl 0(%ebx),%eax
121 pushl %eax /* old ptde != 0 when booting */
122 pushl %ebx /* keep for reuse */
123
124 movl %esp,SCR_STACK(%edx) /* save current stack location */
125
126 movl SCR_NEWPTD(%edx),%eax /* mapping for vm86 page table */
127 movl %eax,0(%ebx) /* ... install as PTD entry 0 */
128
129 movl %ecx,%cr3 /* new page tables */
130 movl SCR_VMFRAME(%edx),%esp /* switch to new stack */
131
2954c92f 132 call vm86_prepcall /* finish setup */
984263bc 133
2954c92f 134 movl $1,in_vm86call /* set flag for trap() */
984263bc
MD
135
136 /*
8f41e33b 137 * Return via _doreti, restore the same cpl as our current cpl
984263bc 138 */
2954c92f 139 movl PCPU(curthread),%eax
ef0fdad1 140 pushl TD_CPL(%eax)
96728c05 141 incl PCPU(intr_nesting_level)/* dummy to match doreti */
984263bc 142 MEXITCOUNT
2954c92f 143 jmp doreti
984263bc
MD
144
145
146/*
147 * vm86_biosret(struct trapframe_vm86 *vm86)
148 */
149ENTRY(vm86_biosret)
2954c92f 150 movl vm86pcb,%edx /* data area */
984263bc
MD
151
152 movl 4(%esp),%esi /* source */
153 movl SCR_ARGFRAME(%edx),%edi /* destination */
154 movl $VM86_FRAMESIZE/4,%ecx /* size */
155 cld
156 rep
157 movsl /* copy frame to original frame */
158
159 movl SCR_STACK(%edx),%esp /* back to old stack */
160 popl %ebx /* saved va of Idle PTD */
161 popl %eax
162 movl %eax,0(%ebx) /* restore old pte */
163 popl %eax
164 movl %eax,%cr3 /* install old page table */
165
2954c92f 166 movl $0,in_vm86call /* reset trapflag */
984263bc 167
2954c92f 168 movl PCPU(tss_gdt),%ebx /* entry in GDT */
984263bc
MD
169 movl SCR_TSS0(%edx),%eax
170 movl %eax,0(%ebx) /* restore first word */
171 movl SCR_TSS1(%edx),%eax
172 movl %eax,4(%ebx) /* restore second word */
173 movl $GPROC0_SEL*8,%esi /* GSEL(entry, SEL_KPL) */
174 ltr %si
b7c628e4 175
2954c92f 176 movl PCPU(curthread),%eax
b7c628e4 177 popl TD_PCB(%eax) /* restore curpcb */
984263bc
MD
178 movl SCR_ARGFRAME(%edx),%edx /* original stack frame */
179 movl TF_TRAPNO(%edx),%eax /* return (trapno) */
180
181 popl %gs
182 popl %edi
183 popl %esi
184 popl %ebp
185 popl %ebx
186 ret /* back to our normal program */