vm86bios.s revision 50477
134840Sjlemon/*-
234840Sjlemon * Copyright (c) 1998 Jonathan Lemon
334840Sjlemon * All rights reserved.
434840Sjlemon *
534840Sjlemon * Redistribution and use in source and binary forms, with or without
634840Sjlemon * modification, are permitted provided that the following conditions
734840Sjlemon * are met:
834840Sjlemon * 1. Redistributions of source code must retain the above copyright
934840Sjlemon *    notice, this list of conditions and the following disclaimer.
1034840Sjlemon * 2. Redistributions in binary form must reproduce the above copyright
1134840Sjlemon *    notice, this list of conditions and the following disclaimer in the
1234840Sjlemon *    documentation and/or other materials provided with the distribution.
1334840Sjlemon *
1434840Sjlemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1534840Sjlemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1634840Sjlemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1734840Sjlemon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1834840Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1934840Sjlemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2034840Sjlemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2134840Sjlemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2234840Sjlemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2334840Sjlemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2434840Sjlemon * SUCH DAMAGE.
2534840Sjlemon *
2650477Speter * $FreeBSD: head/sys/i386/i386/vm86bios.s 50477 1999-08-28 01:08:13Z peter $
2734840Sjlemon */
2834840Sjlemon
2934840Sjlemon#include <machine/asmacros.h>		/* miscellaneous asm macros */
3034840Sjlemon#include <machine/trap.h>
3134840Sjlemon
3234840Sjlemon#include "assym.s"
3334840Sjlemon
3437889Sjlemon#define SCR_NEWPTD	PCB_ESI		/* readability macros */
3537889Sjlemon#define SCR_VMFRAME	PCB_EBP		/* see vm86.c for explanation */
3637889Sjlemon#define SCR_STACK	PCB_ESP
3737889Sjlemon#define SCR_PGTABLE	PCB_EBX
3837889Sjlemon#define SCR_ARGFRAME	PCB_EIP
3946129Sluoqi#define SCR_TSS0	PCB_SPARE
4046129Sluoqi#define SCR_TSS1	(PCB_SPARE+4)
4137889Sjlemon
4234840Sjlemon	.data
4334840Sjlemon	ALIGN_DATA
4434840Sjlemon
4534840Sjlemon	.globl	_in_vm86call, _vm86pcb
4634840Sjlemon
4734840Sjlemon_in_vm86call:		.long	0
4834840Sjlemon_vm86pcb:		.long	0
4934840Sjlemon
5034840Sjlemon	.text
5134840Sjlemon
5234840Sjlemon/*
5334840Sjlemon * vm86_bioscall(struct trapframe_vm86 *vm86)
5434840Sjlemon */
5534840SjlemonENTRY(vm86_bioscall)
5637889Sjlemon	movl	_vm86pcb,%edx		/* scratch data area */
5734840Sjlemon	movl	4(%esp),%eax
5837889Sjlemon	movl	%eax,SCR_ARGFRAME(%edx)	/* save argument pointer */
5934840Sjlemon	pushl	%ebx
6034840Sjlemon	pushl	%ebp
6134840Sjlemon	pushl	%esi
6234840Sjlemon	pushl	%edi
6334840Sjlemon	pushl	%gs
6434840Sjlemon
6534840Sjlemon#ifdef SMP
6634840Sjlemon	pushl	%edx
6734840Sjlemon	ALIGN_LOCK			/* Get global lock */
6834840Sjlemon	popl	%edx
6934840Sjlemon#endif
7034840Sjlemon
7134840Sjlemon#if NNPX > 0
7239775Sjlemon	movl	_curproc,%ecx
7334840Sjlemon	cmpl	%ecx,_npxproc		/* do we need to save fp? */
7434840Sjlemon	jne	1f
7539872Sjlemon	testl	%ecx,%ecx
7639872Sjlemon	je 	1f			/* no curproc/npxproc */
7739046Syokota	pushl	%edx
7834840Sjlemon	movl	P_ADDR(%ecx),%ecx
7934840Sjlemon	addl	$PCB_SAVEFPU,%ecx
8039046Syokota	pushl	%ecx
8134840Sjlemon	call	_npxsave
8239046Syokota	popl	%ecx
8334840Sjlemon	popl	%edx			/* recover our pcb */
8434840Sjlemon#endif
8534840Sjlemon
8634840Sjlemon1:
8737889Sjlemon	movl	SCR_VMFRAME(%edx),%ebx	/* target frame location */
8834840Sjlemon	movl	%ebx,%edi		/* destination */
8937889Sjlemon	movl    SCR_ARGFRAME(%edx),%esi	/* source (set on entry) */
9047080Sluoqi	movl	$VM86_FRAMESIZE/4,%ecx	/* sizeof(struct vm86frame)/4 */
9134840Sjlemon	cld
9234840Sjlemon	rep
9334840Sjlemon	movsl				/* copy frame to new stack */
9434840Sjlemon
9534840Sjlemon	movl	_curpcb,%eax
9634840Sjlemon	pushl	%eax			/* save curpcb */
9734840Sjlemon	movl	%edx,_curpcb		/* set curpcb to vm86pcb */
9834840Sjlemon
9947081Sluoqi	movl	_tss_gdt,%ebx		/* entry in GDT */
10034840Sjlemon	movl	0(%ebx),%eax
10137889Sjlemon	movl	%eax,SCR_TSS0(%edx)	/* save first word */
10234840Sjlemon	movl	4(%ebx),%eax
10334840Sjlemon	andl    $~0x200, %eax		/* flip 386BSY -> 386TSS */
10437889Sjlemon	movl	%eax,SCR_TSS1(%edx)	/* save second word */
10534840Sjlemon
10634840Sjlemon	movl	PCB_EXT(%edx),%edi	/* vm86 tssd entry */
10734840Sjlemon	movl	0(%edi),%eax
10834840Sjlemon	movl	%eax,0(%ebx)
10934840Sjlemon	movl	4(%edi),%eax
11034840Sjlemon	movl	%eax,4(%ebx)
11147081Sluoqi	movl	$GPROC0_SEL*8,%esi	/* GSEL(entry, SEL_KPL) */
11234840Sjlemon	ltr	%si
11334840Sjlemon
11434840Sjlemon	movl	%cr3,%eax
11534840Sjlemon	pushl	%eax			/* save address space */
11634840Sjlemon	movl	_IdlePTD,%ecx
11734840Sjlemon	movl	%ecx,%ebx
11834840Sjlemon	addl	$KERNBASE,%ebx		/* va of Idle PTD */
11934840Sjlemon	movl	0(%ebx),%eax
12034840Sjlemon	pushl	%eax			/* old ptde != 0 when booting */
12134840Sjlemon	pushl	%ebx			/* keep for reuse */
12234840Sjlemon
12337889Sjlemon	movl	%esp,SCR_STACK(%edx)	/* save current stack location */
12434840Sjlemon
12537889Sjlemon	movl	SCR_NEWPTD(%edx),%eax	/* mapping for vm86 page table */
12634840Sjlemon	movl	%eax,0(%ebx)		/* ... install as PTD entry 0 */
12734840Sjlemon
12834840Sjlemon	movl	%ecx,%cr3		/* new page tables */
12937889Sjlemon	movl	SCR_VMFRAME(%edx),%esp	/* switch to new stack */
13034840Sjlemon
13134840Sjlemon	call	_vm86_prepcall		/* finish setup */
13234840Sjlemon
13334840Sjlemon	movl	$1,_in_vm86call		/* set flag for trap() */
13434840Sjlemon
13534840Sjlemon	/*
13634840Sjlemon	 * Return via _doreti
13734840Sjlemon	 */
13834840Sjlemon#ifdef SMP
13934840Sjlemon	ECPL_LOCK
14034840Sjlemon#ifdef CPL_AND_CML
14134840Sjlemon#error Not ready for CPL_AND_CML
14234840Sjlemon#endif
14334840Sjlemon	pushl	_cpl			/* cpl to restore */
14434840Sjlemon	ECPL_UNLOCK
14534840Sjlemon#else
14634840Sjlemon	pushl	_cpl			/* cpl to restore */
14734840Sjlemon#endif
14834840Sjlemon	subl	$4,%esp			/* dummy unit */
14934840Sjlemon	MPLOCKED incb _intr_nesting_level
15034840Sjlemon	MEXITCOUNT
15134840Sjlemon	jmp	_doreti
15234840Sjlemon
15334840Sjlemon
15434840Sjlemon/*
15534840Sjlemon * vm86_biosret(struct trapframe_vm86 *vm86)
15634840Sjlemon */
15734840SjlemonENTRY(vm86_biosret)
15834840Sjlemon	movl	_vm86pcb,%edx		/* data area */
15934840Sjlemon
16034840Sjlemon	movl	4(%esp),%esi		/* source */
16137889Sjlemon	movl	SCR_ARGFRAME(%edx),%edi	/* destination */
16247080Sluoqi	movl	$VM86_FRAMESIZE/4,%ecx	/* size */
16334840Sjlemon	cld
16434840Sjlemon	rep
16534840Sjlemon	movsl				/* copy frame to original frame */
16634840Sjlemon
16737889Sjlemon	movl	SCR_STACK(%edx),%esp	/* back to old stack */
16834840Sjlemon	popl	%ebx			/* saved va of Idle PTD */
16934840Sjlemon	popl	%eax
17034840Sjlemon	movl	%eax,0(%ebx)		/* restore old pte */
17134840Sjlemon	popl	%eax
17234840Sjlemon	movl	%eax,%cr3		/* install old page table */
17334840Sjlemon
17434840Sjlemon	movl	$0,_in_vm86call		/* reset trapflag */
17534840Sjlemon
17647081Sluoqi	movl	_tss_gdt,%ebx		/* entry in GDT */
17737889Sjlemon	movl	SCR_TSS0(%edx),%eax
17834840Sjlemon	movl	%eax,0(%ebx)		/* restore first word */
17937889Sjlemon	movl	SCR_TSS1(%edx),%eax
18034840Sjlemon	movl	%eax,4(%ebx)		/* restore second word */
18147081Sluoqi	movl	$GPROC0_SEL*8,%esi	/* GSEL(entry, SEL_KPL) */
18234840Sjlemon	ltr	%si
18334840Sjlemon
18434840Sjlemon	popl	_curpcb			/* restore curpcb/curproc */
18537889Sjlemon	movl	SCR_ARGFRAME(%edx),%edx	/* original stack frame */
18634840Sjlemon	movl	TF_TRAPNO(%edx),%eax	/* return (trapno) */
18734840Sjlemon
18834840Sjlemon	popl	%gs
18934840Sjlemon	popl	%edi
19034840Sjlemon	popl	%esi
19134840Sjlemon	popl	%ebp
19234840Sjlemon	popl	%ebx
19334840Sjlemon	ret				/* back to our normal program */
194