1 /* $NetBSD: biostramp.S,v 1.13 2005/12/11 12:17:40 christos Exp $ */
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * biostramp.S: provide a means for NetBSD to call BIOS interrupts
34 * by switching to real mode, calling it, and switching
35 * back to protected & paging mode.
39 * Micro$haft's book on i386/i486 programming says you should do the following
40 * to return to real mode from protected mode:
42 * 1) disable paging, by jumping to code with identical virtual and physical
43 * addresses, clearing PG in CR0, and zeroing CR3 (PDBR).
45 * 2) segment descriptors must be byte-granular with limit 64k-1, def32 = 0,
46 * (i.e. 16-bit data accesses and/or 80286 instructions)
47 * CS must be executable; DS,ES,FS,GS should be writable
49 * 3) disable interrupts, load IDTR with original value (base 0, limit 1023)
51 * 4) clear PE in CR0, execute FAR jump to load CS.
53 * 5) load SP, and off you go
59 #include <i386/include/param.h>
60 #include <i386/include/specialreg.h>
61 #include <i386/include/segments.h>
62 #include <i386/include/apmvar.h>
63 #include <i386/include/psl.h>
64 #include <i386/include/asm.h>
66 #define addr32 .byte 0x67
67 #define data32 .byte 0x66
71 .set CR3_ADDR,(MYSCRATCH-4)
72 .set IDTR_SAVE_ADDR,CR3_ADDR-6
73 .set GDTR_SAVE_ADDR,IDTR_SAVE_ADDR-6
74 .set GDTR_LOCAL_ADDR,GDTR_SAVE_ADDR-6
75 .set STACK_PTR_ADDR,GDTR_LOCAL_ADDR-4
76 .set BASE_PTR_ADDR,STACK_PTR_ADDR-4
77 .set FUNCTION_ADDR,(BASE_PTR_ADDR-2)
78 .set GDT_COPY_ADDR,(FUNCTION_ADDR-NGDT*8)
79 .set EAX_REGADDR,(GDT_COPY_ADDR-4)
80 .set EBX_REGADDR,(EAX_REGADDR-4)
81 .set ECX_REGADDR,(EBX_REGADDR-4)
82 .set EDX_REGADDR,(ECX_REGADDR-4)
83 .set ESI_REGADDR,(EDX_REGADDR-4)
84 .set EDI_REGADDR,(ESI_REGADDR-4)
85 .set EFLAGS_REGADDR,(EDI_REGADDR-4)
86 .set ES_REGADDR, (EFLAGS_REGADDR-4)
87 .set ENDREGADDR,(ES_REGADDR-4)
89 .set REALSTACK,ENDREGADDR-20 # leave a red zone?
91 #define COPY_FLAGS (PSL_C|PSL_PF|PSL_AF|PSL_Z|PSL_N|PSL_D|PSL_V)
94 * do_bios_call(int function, struct bioscall *regs)
99 movl %esp,%ebp /* set up frame ptr */
108 # copy data to where the real-mode hook can handle it
110 movw %ax,FUNCTION_ADDR
112 movl BIOSCALLREG_EAX(%ebx),%eax
113 movl %eax,EAX_REGADDR
114 movl BIOSCALLREG_EBX(%ebx),%eax
115 movl %eax,EBX_REGADDR
116 movl BIOSCALLREG_ECX(%ebx),%eax
117 movl %eax,ECX_REGADDR
118 movl BIOSCALLREG_EDX(%ebx),%eax
119 movl %eax,EDX_REGADDR
120 movl BIOSCALLREG_ESI(%ebx),%eax
121 movl %eax,ESI_REGADDR
122 movl BIOSCALLREG_EDI(%ebx),%eax
123 movl %eax,EDI_REGADDR
124 # merge current flags with certain provided flags
125 movl BIOSCALLREG_EFLAGS(%ebx),%ecx
128 andl $~(COPY_FLAGS|PSL_I),%eax
129 andl $COPY_FLAGS,%ecx
131 movl %eax,EFLAGS_REGADDR
134 # save flags, disable interrupts, do real mode stuff
140 # copy the GDT to local area
141 movl GDTR_SAVE_ADDR+2,%esi
142 movl $GDT_COPY_ADDR,%edi
147 movw $(NGDT*8)-1,GDTR_LOCAL_ADDR
148 movl $GDT_COPY_ADDR,GDTR_LOCAL_ADDR+2
158 # set up new stack: save old ones, create new segs
159 movl %esp,STACK_PTR_ADDR
160 movl %ebp,BASE_PTR_ADDR
162 movl $0,%ebp # leave no trace, there is none.
173 # flush TLB, drop PDBR
177 ## load 16-bit segment descriptors
178 movw $GSEL(GBIOSDATA_SEL,SEL_KPL),%bx
184 ljmp $GSEL(GBIOSCODE_SEL,SEL_KPL),$x16+MYBASE
187 # turn off protected mode--yikes!
193 # need inter-segment jump to reload real-mode CS
195 ljmp $(MYBASE>>4),$xreal
197 xreal: # really in real mode now
198 # set up segment selectors. Note: everything is now relative
199 # to zero-base in this file, except %ss.
200 # data items in our scratch area need to reflect MYADDR
210 ## load IDT, now that we are here.
214 # Don't forget that we're in real mode, with 16-bit default data.
215 # all these movl's are really movw's, and movw's are movl's!
217 movw EDI_REGADDR-MYBASE,%di
219 movw ESI_REGADDR-MYBASE,%si
221 movw EDX_REGADDR-MYBASE,%dx
223 movw ECX_REGADDR-MYBASE,%cx
225 movw EBX_REGADDR-MYBASE,%bx
227 movb FUNCTION_ADDR-MYBASE,%al
229 movb %al,intaddr+1 # self modifying code, yuck. no indirect interrupt instruction!
230 # long jump to flush processor cache to reflect code modification
232 ljmp $(MYBASE>>4),$flushit
235 movw EFLAGS_REGADDR-MYBASE,%ax
239 movw EAX_REGADDR-MYBASE,%ax
247 movw %ax,EAX_REGADDR-MYBASE
249 movw %bx,EBX_REGADDR-MYBASE
251 movw %cx,ECX_REGADDR-MYBASE
253 movw %dx,EDX_REGADDR-MYBASE
255 movw %si,ESI_REGADDR-MYBASE
257 movw %di,EDI_REGADDR-MYBASE
260 movw %ax,EFLAGS_REGADDR-MYBASE
262 movw %es,ES_REGADDR-MYBASE
264 # and return to protected mode
265 cli # just to be sure
272 # long jump to 32-bit code segment
274 ljmp $GSEL(GCODE_SEL,SEL_KPL),$x32+MYBASE
276 #back in 32-bit mode/protected mode (but not paging yet).
277 # Reload the segment registers & IDT
279 movw $GSEL(GDATA_SEL,SEL_KPL),%bx
291 # reload system copy of GDT
294 # restore protected-mode stack
295 movl STACK_PTR_ADDR,%esp
296 movl BASE_PTR_ADDR,%ebp
298 #restore protected-mode IDT
301 # copy back arguments from holding pen
304 movl EAX_REGADDR,%eax
305 movl %eax,BIOSCALLREG_EAX(%ebx)
306 movl EBX_REGADDR,%eax
307 movl %eax,BIOSCALLREG_EBX(%ebx)
308 movl ECX_REGADDR,%eax
309 movl %eax,BIOSCALLREG_ECX(%ebx)
310 movl EDX_REGADDR,%eax
311 movl %eax,BIOSCALLREG_EDX(%ebx)
312 movl ESI_REGADDR,%eax
313 movl %eax,BIOSCALLREG_ESI(%ebx)
314 movl EDI_REGADDR,%eax
315 movl %eax,BIOSCALLREG_EDI(%ebx)
316 movl EFLAGS_REGADDR,%eax
317 movl %eax,BIOSCALLREG_EFLAGS(%ebx)
318 movl ES_REGADDR, %eax
319 movl %eax,BIOSCALLREG_ES(%ebx)
321 # finish up, restore registers, and return
338 IDT_bios: # BIOS IDT descriptor (real-mode)