[BACK]Return to biostramp.S CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / arch / i386 / bioscall

File: [cvs.NetBSD.org] / src / sys / arch / i386 / bioscall / biostramp.S (download)

Revision 1.14, Mon Apr 28 20:23:23 2008 UTC (14 years ago) by martin
Branch: MAIN
CVS Tags: yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, yamt-nfs-mp-base9, yamt-nfs-mp-base8, yamt-nfs-mp-base7, yamt-nfs-mp-base6, yamt-nfs-mp-base5, yamt-nfs-mp-base4, yamt-nfs-mp-base3, yamt-nfs-mp-base2, yamt-nfs-mp-base11, yamt-nfs-mp-base10, wrstuden-revivesa-base-4, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, uebayasi-xip-base7, uebayasi-xip-base6, uebayasi-xip-base5, uebayasi-xip-base4, uebayasi-xip-base3, uebayasi-xip-base2, uebayasi-xip-base1, uebayasi-xip-base, uebayasi-xip, tls-maxphys-base-20171202, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, thorpej-i2c-spi-conf2-base, thorpej-i2c-spi-conf2, thorpej-i2c-spi-conf-base, thorpej-i2c-spi-conf, thorpej-futex2-base, thorpej-futex2, thorpej-futex-base, thorpej-futex, thorpej-cfargs2-base, thorpej-cfargs2, thorpej-cfargs-base, thorpej-cfargs, simonb-wapbl-nbase, simonb-wapbl-base, simonb-wapbl, rmind-uvmplock-nbase, rmind-uvmplock-base, rmind-uvmplock, rmind-smpnet-nbase, rmind-smpnet-base, rmind-smpnet, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, nick-nhusb-base-20170825, nick-nhusb-base-20170204, nick-nhusb-base-20161204, nick-nhusb-base-20161004, nick-nhusb-base-20160907, nick-nhusb-base-20160529, nick-nhusb-base-20160422, nick-nhusb-base-20160319, nick-nhusb-base-20151226, nick-nhusb-base-20150921, nick-nhusb-base-20150606, nick-nhusb-base-20150406, nick-nhusb-base, nick-nhusb, nick-hppapmap-base4, nick-hppapmap-base3, nick-hppapmap-base2, nick-hppapmap-base, nick-hppapmap, netbsd-9-base, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-devfs2-base, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, localcount-20160914, khorben-n900, jymxensuspend-base, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, jruoho-x86intr-base, jruoho-x86intr, jmcneill-usbmp-pre-base2, jmcneill-usbmp-base9, jmcneill-usbmp-base8, jmcneill-usbmp-base7, jmcneill-usbmp-base6, jmcneill-usbmp-base5, jmcneill-usbmp-base4, jmcneill-usbmp-base3, jmcneill-usbmp-base2, jmcneill-usbmp-base10, jmcneill-usbmp-base, jmcneill-usbmp, jmcneill-audiomp3-base, jmcneill-audiomp3, jdolecek-ncq-base, jdolecek-ncq, isaki-audio2-base, isaki-audio2, is-mlppp-base, is-mlppp, hpcarm-cleanup-nbase, haad-nbase2, haad-dm-base2, haad-dm-base1, haad-dm-base, haad-dm, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, cherry-xenmp-base, cherry-xenmp, bouyer-xenpvh-base2, bouyer-xenpvh-base1, bouyer-xenpvh-base, bouyer-xenpvh, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, ad-namecache-base3, ad-namecache-base2, ad-namecache-base1, ad-namecache-base, ad-namecache, ad-audiomp2-base, ad-audiomp2, HEAD
Changes since 1.13: +1 -8 lines

Remove clause 3 and 4 from TNF licenses

/*	$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
 *
 */

#include "assym.h"

#include <i386/include/param.h>
#include <i386/include/specialreg.h>
#include <i386/include/segments.h>
#include <i386/include/apmvar.h>
#include <i386/include/psl.h>
#include <i386/include/asm.h>

#define	addr32	.byte 0x67
#define	data32	.byte 0x66

	.set MYBASE,NBPG
	.set MYSCRATCH,NBPG*2
	.set CR3_ADDR,(MYSCRATCH-4)
	.set IDTR_SAVE_ADDR,CR3_ADDR-6
	.set GDTR_SAVE_ADDR,IDTR_SAVE_ADDR-6
	.set GDTR_LOCAL_ADDR,GDTR_SAVE_ADDR-6
	.set STACK_PTR_ADDR,GDTR_LOCAL_ADDR-4
	.set BASE_PTR_ADDR,STACK_PTR_ADDR-4
	.set FUNCTION_ADDR,(BASE_PTR_ADDR-2)
	.set GDT_COPY_ADDR,(FUNCTION_ADDR-NGDT*8)
	.set EAX_REGADDR,(GDT_COPY_ADDR-4)
	.set EBX_REGADDR,(EAX_REGADDR-4)
	.set ECX_REGADDR,(EBX_REGADDR-4)
	.set EDX_REGADDR,(ECX_REGADDR-4)
	.set ESI_REGADDR,(EDX_REGADDR-4)
	.set EDI_REGADDR,(ESI_REGADDR-4)
	.set EFLAGS_REGADDR,(EDI_REGADDR-4)
	.set ES_REGADDR, (EFLAGS_REGADDR-4)
	.set ENDREGADDR,(ES_REGADDR-4)
	
	.set REALSTACK,ENDREGADDR-20		# leave a red zone?

#define COPY_FLAGS (PSL_C|PSL_PF|PSL_AF|PSL_Z|PSL_N|PSL_D|PSL_V)

/*
 * do_bios_call(int function, struct bioscall *regs)
 */

ENTRY(do_bios_call)
	pushl	%ebp
	movl	%esp,%ebp		/* set up frame ptr */
	pushl	%esi
	pushl	%edi
	pushl	%ebx
	pushl	%ds		
	pushl	%es
	pushl	%fs
	pushl	%gs

	# 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
	
	## load 16-bit segment descriptors
	movw $GSEL(GBIOSDATA_SEL,SEL_KPL),%bx
	movw %bx,%ds
	movw %bx,%es
	movw %bx,%fs
	movw %bx,%gs

	ljmp $GSEL(GBIOSCODE_SEL,SEL_KPL),$x16+MYBASE
	
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

	movw %cs,%ax
	movw %ax,%es
	movw %ax,%fs
	movw %ax,%gs
	movw %ax,%ds
	
	## load IDT, now that we are here.
	addr32
	lidt IDT_bios

	# Don't forget that we're in real mode, with 16-bit default data.
	# all these movl's are really movw's, and movw's are movl's!
	addr32
	movw EDI_REGADDR-MYBASE,%di
	addr32
	movw ESI_REGADDR-MYBASE,%si
	addr32
	movw EDX_REGADDR-MYBASE,%dx
	addr32
	movw ECX_REGADDR-MYBASE,%cx
	addr32
	movw EBX_REGADDR-MYBASE,%bx
	addr32
	movb FUNCTION_ADDR-MYBASE,%al
	addr32
	movb %al,intaddr+1	# self modifying code, yuck. no indirect interrupt instruction!
	# long jump to flush processor cache to reflect code modification
	data32
	ljmp $(MYBASE>>4),$flushit
flushit:
	addr32
	movw EFLAGS_REGADDR-MYBASE,%ax
	pushl %eax
	popfl
	addr32
	movw EAX_REGADDR-MYBASE,%ax
	
intaddr: 
	int $0xff

	# save results
	pushf
	addr32
	movw %ax,EAX_REGADDR-MYBASE
	addr32
	movw %bx,EBX_REGADDR-MYBASE
	addr32
	movw %cx,ECX_REGADDR-MYBASE
	addr32
	movw %dx,EDX_REGADDR-MYBASE
	addr32
	movw %si,ESI_REGADDR-MYBASE
	addr32
	movw %di,EDI_REGADDR-MYBASE
	pop %ax
	addr32
	movw %ax,EFLAGS_REGADDR-MYBASE
	addr32
	movw %es,ES_REGADDR-MYBASE

	# and return to protected mode
	cli	# just to be sure

	mov %cr0,%eax
	data32
	or $CR0_PE,%eax
	mov %eax,%cr0
	
	# long jump to 32-bit code segment
	data32
	ljmp $GSEL(GCODE_SEL,SEL_KPL),$x32+MYBASE
x32:	
	#back in 32-bit mode/protected mode (but not paging yet).
	# Reload the segment registers & IDT

	movw $GSEL(GDATA_SEL,SEL_KPL),%bx
	movw %bx,%ds
	movw %bx,%ss
	movw %bx,%es
	
	# reload PDBR
	movl CR3_ADDR,%eax
	movl %eax,%cr3
	movl %cr0,%eax
	orl $CR0_PG,%eax
	movl %eax,%cr0
	
	# reload system copy of GDT
	lgdt GDTR_SAVE_ADDR

	# restore protected-mode stack
	movl STACK_PTR_ADDR,%esp
	movl BASE_PTR_ADDR,%ebp
	
	#restore protected-mode IDT
	lidt IDTR_SAVE_ADDR
	
	# copy back arguments from holding pen

	movl 12(%ebp),%ebx
	movl EAX_REGADDR,%eax
	movl %eax,BIOSCALLREG_EAX(%ebx)
	movl EBX_REGADDR,%eax
	movl %eax,BIOSCALLREG_EBX(%ebx)
	movl ECX_REGADDR,%eax
	movl %eax,BIOSCALLREG_ECX(%ebx)
	movl EDX_REGADDR,%eax
	movl %eax,BIOSCALLREG_EDX(%ebx)
	movl ESI_REGADDR,%eax
	movl %eax,BIOSCALLREG_ESI(%ebx)
	movl EDI_REGADDR,%eax
	movl %eax,BIOSCALLREG_EDI(%ebx)
	movl EFLAGS_REGADDR,%eax
	movl %eax,BIOSCALLREG_EFLAGS(%ebx)
	movl ES_REGADDR, %eax
	movl %eax,BIOSCALLREG_ES(%ebx)
	
	# finish up, restore registers, and return
	popfl
	popl	%gs
	popl	%fs
	popl	%es
	popl	%ds		# see above
	popl	%ebx
	popl	%edi
	popl	%esi
	leave
	ret

#ifdef __ELF__
	.align 16
#else
	.align 4
#endif
IDT_bios:			# BIOS IDT descriptor (real-mode)
	.word 1023
	.long 0