/* $NetBSD: biostramp.S,v 1.14 2008/04/28 20:23:23 martin Exp $ */
/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by John Kohl.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* biostramp.S: provide a means for NetBSD to call BIOS interrupts
* by switching to real mode, calling it, and switching
* back to protected & paging mode.
*/
/*
* Micro$haft's book on i386/i486 programming says you should do the following
* to return to real mode from protected mode:
*
* 1) disable paging, by jumping to code with identical virtual and physical
* addresses, clearing PG in CR0, and zeroing CR3 (PDBR).
*
* 2) segment descriptors must be byte-granular with limit 64k-1, def32 = 0,
* (i.e. 16-bit data accesses and/or 80286 instructions)
* CS must be executable; DS,ES,FS,GS should be writable
*
* 3) disable interrupts, load IDTR with original value (base 0, limit 1023)
*
* 4) clear PE in CR0, execute FAR jump to load CS.
*
* 5) load SP, and off you go
*
*/
# copy data to where the real-mode hook can handle it
movl 8(%ebp),%eax
movw %ax,FUNCTION_ADDR
movl 12(%ebp),%ebx
movl BIOSCALLREG_EAX(%ebx),%eax
movl %eax,EAX_REGADDR
movl BIOSCALLREG_EBX(%ebx),%eax
movl %eax,EBX_REGADDR
movl BIOSCALLREG_ECX(%ebx),%eax
movl %eax,ECX_REGADDR
movl BIOSCALLREG_EDX(%ebx),%eax
movl %eax,EDX_REGADDR
movl BIOSCALLREG_ESI(%ebx),%eax
movl %eax,ESI_REGADDR
movl BIOSCALLREG_EDI(%ebx),%eax
movl %eax,EDI_REGADDR
# merge current flags with certain provided flags
movl BIOSCALLREG_EFLAGS(%ebx),%ecx
pushfl
popl %eax
andl $~(COPY_FLAGS|PSL_I),%eax
andl $COPY_FLAGS,%ecx
orl %ecx,%eax
movl %eax,EFLAGS_REGADDR
movl $0, ES_REGADDR
# save flags, disable interrupts, do real mode stuff
pushfl
# save GDT
sgdt GDTR_SAVE_ADDR
# copy the GDT to local area
movl GDTR_SAVE_ADDR+2,%esi
movl $GDT_COPY_ADDR,%edi
movl $(NGDT*8),%ecx
cld
rep
movsb
movw $(NGDT*8)-1,GDTR_LOCAL_ADDR
movl $GDT_COPY_ADDR,GDTR_LOCAL_ADDR+2
# install GDT copy
lgdt GDTR_LOCAL_ADDR
cli
# save IDT
sidt IDTR_SAVE_ADDR
# set up new stack: save old ones, create new segs
movl %esp,STACK_PTR_ADDR
movl %ebp,BASE_PTR_ADDR
movl $REALSTACK,%esp
movl $0,%ebp # leave no trace, there is none.
# save CR3
movl %cr3,%eax
movl %eax,CR3_ADDR
# turn off paging
movl %cr0,%eax
andl $~(CR0_PG),%eax
movl %eax,%cr0
# flush TLB, drop PDBR
xorl %eax,%eax
movl %eax,%cr3
x16:
# turn off protected mode--yikes!
mov %cr0,%eax
data32
and $~CR0_PE,%eax
mov %eax,%cr0
# need inter-segment jump to reload real-mode CS
data32
ljmp $(MYBASE>>4),$xreal
xreal: # really in real mode now
# set up segment selectors. Note: everything is now relative
# to zero-base in this file, except %ss.
# data items in our scratch area need to reflect MYADDR
xorl %eax,%eax
movw %ax,%ss