biostramp.S revision 1.10.2.2 1 1.10.2.2 christos /* $NetBSD: biostramp.S,v 1.10.2.2 2001/12/14 20:30:11 christos Exp $ */
2 1.10.2.2 christos
3 1.10.2.2 christos /*-
4 1.10.2.2 christos * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.10.2.2 christos * All rights reserved.
6 1.10.2.2 christos *
7 1.10.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.10.2.2 christos * by John Kohl.
9 1.10.2.2 christos *
10 1.10.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.10.2.2 christos * modification, are permitted provided that the following conditions
12 1.10.2.2 christos * are met:
13 1.10.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.10.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.10.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.10.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.10.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.10.2.2 christos * 3. All advertising materials mentioning features or use of this software
19 1.10.2.2 christos * must display the following acknowledgement:
20 1.10.2.2 christos * This product includes software developed by the NetBSD
21 1.10.2.2 christos * Foundation, Inc. and its contributors.
22 1.10.2.2 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.10.2.2 christos * contributors may be used to endorse or promote products derived
24 1.10.2.2 christos * from this software without specific prior written permission.
25 1.10.2.2 christos *
26 1.10.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.10.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.10.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.10.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.10.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.10.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.10.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.10.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.10.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.10.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.10.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.10.2.2 christos */
38 1.10.2.2 christos
39 1.10.2.2 christos /*
40 1.10.2.2 christos * biostramp.S: provide a means for NetBSD to call BIOS interrupts
41 1.10.2.2 christos * by switching to real mode, calling it, and switching
42 1.10.2.2 christos * back to protected & paging mode.
43 1.10.2.2 christos */
44 1.10.2.2 christos
45 1.10.2.2 christos /*
46 1.10.2.2 christos * Micro$haft's book on i386/i486 programming says you should do the following
47 1.10.2.2 christos * to return to real mode from protected mode:
48 1.10.2.2 christos *
49 1.10.2.2 christos * 1) disable paging, by jumping to code with identical virtual and physical
50 1.10.2.2 christos * addresses, clearing PG in CR0, and zeroing CR3 (PDBR).
51 1.10.2.2 christos *
52 1.10.2.2 christos * 2) segment descriptors must be byte-granular with limit 64k-1, def32 = 0,
53 1.10.2.2 christos * (i.e. 16-bit data accesses and/or 80286 instructions)
54 1.10.2.2 christos * CS must be executable; DS,ES,FS,GS should be writable
55 1.10.2.2 christos *
56 1.10.2.2 christos * 3) disable interrupts, load IDTR with original value (base 0, limit 1023)
57 1.10.2.2 christos *
58 1.10.2.2 christos * 4) clear PE in CR0, execute FAR jump to load CS.
59 1.10.2.2 christos *
60 1.10.2.2 christos * 5) load SP, and off you go
61 1.10.2.2 christos *
62 1.10.2.2 christos */
63 1.10.2.2 christos
64 1.10.2.2 christos #include "assym.h"
65 1.10.2.2 christos
66 1.10.2.2 christos #include <i386/include/param.h>
67 1.10.2.2 christos #include <i386/include/specialreg.h>
68 1.10.2.2 christos #include <i386/include/segments.h>
69 1.10.2.2 christos #include <i386/include/apmvar.h>
70 1.10.2.2 christos #include <i386/include/psl.h>
71 1.10.2.2 christos #include <i386/include/asm.h>
72 1.10.2.2 christos
73 1.10.2.2 christos #define addr32 .byte 0x67
74 1.10.2.2 christos #define data32 .byte 0x66
75 1.10.2.2 christos
76 1.10.2.2 christos .set MYBASE,NBPG
77 1.10.2.2 christos .set MYSCRATCH,NBPG+NBPG
78 1.10.2.2 christos .set CR3_ADDR,(MYSCRATCH-4)
79 1.10.2.2 christos .set IDTR_SAVE_ADDR,CR3_ADDR-6
80 1.10.2.2 christos .set GDTR_SAVE_ADDR,IDTR_SAVE_ADDR-6
81 1.10.2.2 christos .set GDTR_LOCAL_ADDR,GDTR_SAVE_ADDR-6
82 1.10.2.2 christos .set STACK_PTR_ADDR,GDTR_LOCAL_ADDR-4
83 1.10.2.2 christos .set BASE_PTR_ADDR,STACK_PTR_ADDR-4
84 1.10.2.2 christos .set FUNCTION_ADDR,(BASE_PTR_ADDR-2)
85 1.10.2.2 christos .set GDT_COPY_ADDR,(FUNCTION_ADDR-NGDT*8)
86 1.10.2.2 christos .set EAX_REGADDR,(GDT_COPY_ADDR-4)
87 1.10.2.2 christos .set EBX_REGADDR,(EAX_REGADDR-4)
88 1.10.2.2 christos .set ECX_REGADDR,(EBX_REGADDR-4)
89 1.10.2.2 christos .set EDX_REGADDR,(ECX_REGADDR-4)
90 1.10.2.2 christos .set ESI_REGADDR,(EDX_REGADDR-4)
91 1.10.2.2 christos .set EDI_REGADDR,(ESI_REGADDR-4)
92 1.10.2.2 christos .set EFLAGS_REGADDR,(EDI_REGADDR-4)
93 1.10.2.2 christos .set ES_REGADDR, (EFLAGS_REGADDR-4)
94 1.10.2.2 christos .set ENDREGADDR,(ES_REGADDR-4)
95 1.10.2.2 christos
96 1.10.2.2 christos .set REALSTACK,ENDREGADDR-20 # leave a red zone?
97 1.10.2.2 christos
98 1.10.2.2 christos #define COPY_FLAGS (PSL_C|PSL_PF|PSL_AF|PSL_Z|PSL_N|PSL_D|PSL_V)
99 1.10.2.2 christos
100 1.10.2.2 christos /*
101 1.10.2.2 christos * do_bios_call(int function, struct bioscall *regs)
102 1.10.2.2 christos */
103 1.10.2.2 christos
104 1.10.2.2 christos ENTRY(do_bios_call)
105 1.10.2.2 christos pushl %ebp
106 1.10.2.2 christos movl %esp,%ebp /* set up frame ptr */
107 1.10.2.2 christos pushl %esi
108 1.10.2.2 christos pushl %edi
109 1.10.2.2 christos pushl %ebx
110 1.10.2.2 christos pushl %ds
111 1.10.2.2 christos pushl %es
112 1.10.2.2 christos pushl %fs
113 1.10.2.2 christos pushl %gs
114 1.10.2.2 christos
115 1.10.2.2 christos # copy data to where the real-mode hook can handle it
116 1.10.2.2 christos movl 8(%ebp),%eax
117 1.10.2.2 christos movw %ax,FUNCTION_ADDR
118 1.10.2.2 christos movl 12(%ebp),%ebx
119 1.10.2.2 christos movl BIOSCALLREG_EAX(%ebx),%eax
120 1.10.2.2 christos movl %eax,EAX_REGADDR
121 1.10.2.2 christos movl BIOSCALLREG_EBX(%ebx),%eax
122 1.10.2.2 christos movl %eax,EBX_REGADDR
123 1.10.2.2 christos movl BIOSCALLREG_ECX(%ebx),%eax
124 1.10.2.2 christos movl %eax,ECX_REGADDR
125 1.10.2.2 christos movl BIOSCALLREG_EDX(%ebx),%eax
126 1.10.2.2 christos movl %eax,EDX_REGADDR
127 1.10.2.2 christos movl BIOSCALLREG_ESI(%ebx),%eax
128 1.10.2.2 christos movl %eax,ESI_REGADDR
129 1.10.2.2 christos movl BIOSCALLREG_EDI(%ebx),%eax
130 1.10.2.2 christos movl %eax,EDI_REGADDR
131 1.10.2.2 christos # merge current flags with certain provided flags
132 1.10.2.2 christos movl BIOSCALLREG_EFLAGS(%ebx),%ecx
133 1.10.2.2 christos pushfl
134 1.10.2.2 christos popl %eax
135 1.10.2.2 christos andl $~(COPY_FLAGS|PSL_I),%eax
136 1.10.2.2 christos andl $COPY_FLAGS,%ecx
137 1.10.2.2 christos orl %ecx,%eax
138 1.10.2.2 christos movl %eax,EFLAGS_REGADDR
139 1.10.2.2 christos movl $0, ES_REGADDR
140 1.10.2.2 christos
141 1.10.2.2 christos # save flags, disable interrupts, do real mode stuff
142 1.10.2.2 christos pushfl
143 1.10.2.2 christos
144 1.10.2.2 christos # save GDT
145 1.10.2.2 christos sgdt GDTR_SAVE_ADDR
146 1.10.2.2 christos
147 1.10.2.2 christos # copy the GDT to local area
148 1.10.2.2 christos movl GDTR_SAVE_ADDR+2,%esi
149 1.10.2.2 christos movl $GDT_COPY_ADDR,%edi
150 1.10.2.2 christos movl $(NGDT*8),%ecx
151 1.10.2.2 christos cld
152 1.10.2.2 christos rep
153 1.10.2.2 christos movsb
154 1.10.2.2 christos movw $(NGDT*8)-1,GDTR_LOCAL_ADDR
155 1.10.2.2 christos movl $GDT_COPY_ADDR,GDTR_LOCAL_ADDR+2
156 1.10.2.2 christos
157 1.10.2.2 christos # install GDT copy
158 1.10.2.2 christos lgdt GDTR_LOCAL_ADDR
159 1.10.2.2 christos
160 1.10.2.2 christos cli
161 1.10.2.2 christos
162 1.10.2.2 christos # save IDT
163 1.10.2.2 christos sidt IDTR_SAVE_ADDR
164 1.10.2.2 christos
165 1.10.2.2 christos # set up new stack: save old ones, create new segs
166 1.10.2.2 christos movl %esp,STACK_PTR_ADDR
167 1.10.2.2 christos movl %ebp,BASE_PTR_ADDR
168 1.10.2.2 christos movl $REALSTACK,%esp
169 1.10.2.2 christos movl $0,%ebp # leave no trace, there is none.
170 1.10.2.2 christos
171 1.10.2.2 christos # save CR3
172 1.10.2.2 christos movl %cr3,%eax
173 1.10.2.2 christos movl %eax,CR3_ADDR
174 1.10.2.2 christos
175 1.10.2.2 christos # turn off paging
176 1.10.2.2 christos movl %cr0,%eax
177 1.10.2.2 christos andl $~(CR0_PG),%eax
178 1.10.2.2 christos movl %eax,%cr0
179 1.10.2.2 christos
180 1.10.2.2 christos # flush TLB, drop PDBR
181 1.10.2.2 christos xorl %eax,%eax
182 1.10.2.2 christos movl %eax,%cr3
183 1.10.2.2 christos
184 1.10.2.2 christos ## load 16-bit segment descriptors
185 1.10.2.2 christos movw $GSEL(GBIOSDATA_SEL,SEL_KPL),%bx
186 1.10.2.2 christos movw %bx,%ds
187 1.10.2.2 christos movw %bx,%es
188 1.10.2.2 christos movw %bx,%fs
189 1.10.2.2 christos movw %bx,%gs
190 1.10.2.2 christos
191 1.10.2.2 christos ljmp $GSEL(GBIOSCODE_SEL,SEL_KPL),$x16+MYBASE
192 1.10.2.2 christos
193 1.10.2.2 christos x16:
194 1.10.2.2 christos # turn off protected mode--yikes!
195 1.10.2.2 christos mov %cr0,%eax
196 1.10.2.2 christos data32
197 1.10.2.2 christos and $~CR0_PE,%eax
198 1.10.2.2 christos mov %eax,%cr0
199 1.10.2.2 christos
200 1.10.2.2 christos # need inter-segment jump to reload real-mode CS
201 1.10.2.2 christos data32
202 1.10.2.2 christos ljmp $(MYBASE>>4),$xreal
203 1.10.2.2 christos
204 1.10.2.2 christos xreal: # really in real mode now
205 1.10.2.2 christos # set up segment selectors. Note: everything is now relative
206 1.10.2.2 christos # to zero-base in this file, except %ss.
207 1.10.2.2 christos # data items in our scratch area need to reflect MYADDR
208 1.10.2.2 christos xorl %eax,%eax
209 1.10.2.2 christos movw %ax,%ss
210 1.10.2.2 christos
211 1.10.2.2 christos movw %cs,%ax
212 1.10.2.2 christos movw %ax,%es
213 1.10.2.2 christos movw %ax,%fs
214 1.10.2.2 christos movw %ax,%gs
215 1.10.2.2 christos movw %ax,%ds
216 1.10.2.2 christos
217 1.10.2.2 christos ## load IDT, now that we are here.
218 1.10.2.2 christos addr32
219 1.10.2.2 christos lidt IDT_bios
220 1.10.2.2 christos
221 1.10.2.2 christos # Don't forget that we're in real mode, with 16-bit default data.
222 1.10.2.2 christos # all these movl's are really movw's, and movw's are movl's!
223 1.10.2.2 christos addr32
224 1.10.2.2 christos movw EDI_REGADDR-MYBASE,%di
225 1.10.2.2 christos addr32
226 1.10.2.2 christos movw ESI_REGADDR-MYBASE,%si
227 1.10.2.2 christos addr32
228 1.10.2.2 christos movw EDX_REGADDR-MYBASE,%dx
229 1.10.2.2 christos addr32
230 1.10.2.2 christos movw ECX_REGADDR-MYBASE,%cx
231 1.10.2.2 christos addr32
232 1.10.2.2 christos movw EBX_REGADDR-MYBASE,%bx
233 1.10.2.2 christos addr32
234 1.10.2.2 christos movb FUNCTION_ADDR-MYBASE,%al
235 1.10.2.2 christos addr32
236 1.10.2.2 christos movb %al,intaddr+1 # self modifying code, yuck. no indirect interrupt instruction!
237 1.10.2.2 christos # long jump to flush processor cache to reflect code modification
238 1.10.2.2 christos data32
239 1.10.2.2 christos ljmp $(MYBASE>>4),$flushit
240 1.10.2.2 christos flushit:
241 1.10.2.2 christos addr32
242 1.10.2.2 christos movw EFLAGS_REGADDR-MYBASE,%ax
243 1.10.2.2 christos pushl %eax
244 1.10.2.2 christos popfl
245 1.10.2.2 christos addr32
246 1.10.2.2 christos movw EAX_REGADDR-MYBASE,%ax
247 1.10.2.2 christos
248 1.10.2.2 christos intaddr:
249 1.10.2.2 christos int $0xff
250 1.10.2.2 christos
251 1.10.2.2 christos # save results
252 1.10.2.2 christos pushf
253 1.10.2.2 christos addr32
254 1.10.2.2 christos movw %ax,EAX_REGADDR-MYBASE
255 1.10.2.2 christos addr32
256 1.10.2.2 christos movw %bx,EBX_REGADDR-MYBASE
257 1.10.2.2 christos addr32
258 1.10.2.2 christos movw %cx,ECX_REGADDR-MYBASE
259 1.10.2.2 christos addr32
260 1.10.2.2 christos movw %dx,EDX_REGADDR-MYBASE
261 1.10.2.2 christos addr32
262 1.10.2.2 christos movw %si,ESI_REGADDR-MYBASE
263 1.10.2.2 christos addr32
264 1.10.2.2 christos movw %di,EDI_REGADDR-MYBASE
265 1.10.2.2 christos pop %ax
266 1.10.2.2 christos addr32
267 1.10.2.2 christos movw %ax,EFLAGS_REGADDR-MYBASE
268 1.10.2.2 christos addr32
269 1.10.2.2 christos movw %es,ES_REGADDR-MYBASE
270 1.10.2.2 christos
271 1.10.2.2 christos # and return to protected mode
272 1.10.2.2 christos cli # just to be sure
273 1.10.2.2 christos
274 1.10.2.2 christos mov %cr0,%eax
275 1.10.2.2 christos data32
276 1.10.2.2 christos or $CR0_PE,%eax
277 1.10.2.2 christos mov %eax,%cr0
278 1.10.2.2 christos
279 1.10.2.2 christos # long jump to 32-bit code segment
280 1.10.2.2 christos data32
281 1.10.2.2 christos ljmp $GSEL(GCODE_SEL,SEL_KPL),$x32+MYBASE
282 1.10.2.2 christos x32:
283 1.10.2.2 christos #back in 32-bit mode/protected mode (but not paging yet).
284 1.10.2.2 christos # Reload the segment registers & IDT
285 1.10.2.2 christos
286 1.10.2.2 christos movw $GSEL(GDATA_SEL,SEL_KPL),%bx
287 1.10.2.2 christos movw %bx,%ds
288 1.10.2.2 christos movw %bx,%ss
289 1.10.2.2 christos movw %bx,%es
290 1.10.2.2 christos
291 1.10.2.2 christos # reload PDBR
292 1.10.2.2 christos movl CR3_ADDR,%eax
293 1.10.2.2 christos movl %eax,%cr3
294 1.10.2.2 christos movl %cr0,%eax
295 1.10.2.2 christos orl $CR0_PG,%eax
296 1.10.2.2 christos movl %eax,%cr0
297 1.10.2.2 christos
298 1.10.2.2 christos # reload system copy of GDT
299 1.10.2.2 christos lgdt GDTR_SAVE_ADDR
300 1.10.2.2 christos
301 1.10.2.2 christos # restore protected-mode stack
302 1.10.2.2 christos movl STACK_PTR_ADDR,%esp
303 1.10.2.2 christos movl BASE_PTR_ADDR,%ebp
304 1.10.2.2 christos
305 1.10.2.2 christos #restore protected-mode IDT
306 1.10.2.2 christos lidt IDTR_SAVE_ADDR
307 1.10.2.2 christos
308 1.10.2.2 christos # copy back arguments from holding pen
309 1.10.2.2 christos
310 1.10.2.2 christos movl 12(%ebp),%ebx
311 1.10.2.2 christos movl EAX_REGADDR,%eax
312 1.10.2.2 christos movl %eax,BIOSCALLREG_EAX(%ebx)
313 1.10.2.2 christos movl EBX_REGADDR,%eax
314 1.10.2.2 christos movl %eax,BIOSCALLREG_EBX(%ebx)
315 1.10.2.2 christos movl ECX_REGADDR,%eax
316 1.10.2.2 christos movl %eax,BIOSCALLREG_ECX(%ebx)
317 1.10.2.2 christos movl EDX_REGADDR,%eax
318 1.10.2.2 christos movl %eax,BIOSCALLREG_EDX(%ebx)
319 1.10.2.2 christos movl ESI_REGADDR,%eax
320 1.10.2.2 christos movl %eax,BIOSCALLREG_ESI(%ebx)
321 1.10.2.2 christos movl EDI_REGADDR,%eax
322 1.10.2.2 christos movl %eax,BIOSCALLREG_EDI(%ebx)
323 1.10.2.2 christos movl EFLAGS_REGADDR,%eax
324 1.10.2.2 christos movl %eax,BIOSCALLREG_EFLAGS(%ebx)
325 1.10.2.2 christos movl ES_REGADDR, %eax
326 1.10.2.2 christos movl %eax,BIOSCALLREG_ES(%ebx)
327 1.10.2.2 christos
328 1.10.2.2 christos # finish up, restore registers, and return
329 1.10.2.2 christos popfl
330 1.10.2.2 christos popl %gs
331 1.10.2.2 christos popl %fs
332 1.10.2.2 christos popl %es
333 1.10.2.2 christos popl %ds # see above
334 1.10.2.2 christos popl %ebx
335 1.10.2.2 christos popl %edi
336 1.10.2.2 christos popl %esi
337 1.10.2.2 christos leave
338 1.10.2.2 christos ret
339 1.10.2.2 christos
340 1.10.2.2 christos #ifdef __ELF__
341 1.10.2.2 christos .align 16
342 1.10.2.2 christos #else
343 1.10.2.2 christos .align 4
344 1.10.2.2 christos #endif
345 1.10.2.2 christos IDT_bios: # BIOS IDT descriptor (real-mode)
346 1.10.2.2 christos .word 1023
347 1.10.2.2 christos .long 0
348