* Copyright (c) 1990 William Jolitz.
* Copyright (c) 1991 The Regents of the University of California.
* %sccs.include.redist.c%
* @(#)npx.c 7.2 (Berkeley) %G%
#include "machine/trap.h"
#include "machine/specialreg.h"
#include "i386/isa/isa_device.h"
* 387 and 287 Numeric Coprocessor Extension (NPX) Driver.
int npxprobe(), npxattach(), npxintr();
struct isa_driver npxdriver
= {
npxprobe
, npxattach
, "npx",
struct proc
*npxproc
; /* process who owns device, otherwise zero */
struct pcb
*npxpcb
; /* owners context structure */
* Probe routine - look device, otherwise set emulator bit
{ static status
, control
;
asm(" fninit "); /* put device in known state */
/* check for a proper status of zero */
asm (" fnstsw %0 " : "=m" (status
) : "m" (status
) );
/* good, now check for a proper control word */
asm (" fnstcw %0 " : "=m" (control
) : "m" (control
));
if ((control
&0x103f) == 0x3f) {
/* then we have a numeric coprocessor */
/* XXX should force an exception here to generate an intr */
* Attach routine - announce which it is, and wire into system
/* check for ET bit to decide 387/287 */
/*outb(0xb1,0); /* reset processor */
* Initialize floating point unit, usually after an error
if (npxexists
== 0) return;
load_cr0(rcr0() & ~CR0_EM
); /* stop emulating */
asm(" fldcw %0" : : "g" (control
));
asm(" fnsave %0 " : : "g" (curpcb
->pcb_savefpu
) );
asm(" fnsave %0 " : : "g" (curpcb
->pcb_savefpu
) );
load_cr0(rcr0() | CR0_EM
); /* start emulating */
* Load floating point context and record ownership to suite
if (npxproc
) panic ("npxload");
asm(" frstor %0 " : : "g" (curpcb
->pcb_savefpu
) );
* Unload floating point context and relinquish ownership
if (npxproc
== 0) panic ("npxunload");
asm(" fsave %0 " : : "g" (npxpcb
->pcb_savefpu
) );
* Record information needed in processing an exception and clear status word
npxintr(frame
) struct intrframe frame
; {
outb(0xf0,0); /* reset processor */
/* sync state in process context structure, in advance of debugger/process looking for it */
if (npxproc
== 0 || npxexists
== 0) panic ("npxintr");
asm (" fnsave %0 " : : "g" (npxpcb
->pcb_savefpu
) );
* Prepair a trap frame for our generic exception processing routine, trap()
bcopy(&frame
.if_es
, &tf
, sizeof(tf
));
tf
.tf_trapno
= T_ARITHTRAP
;
/* encode the appropriate code for detailed information on this exception */
* Restore with any changes to superior frame
bcopy(&tf
, &frame
.if_es
, sizeof(tf
));
/* clear the exception so we can catch others like it */
* Implement device not available (DNA) exception
if (npxexists
== 0) return(0);
if (!(curpcb
->pcb_flags
& FP_WASUSED
)
||(curpcb
->pcb_flags
& FP_NEEDSRESTORE
)) {
load_cr0(rcr0() & ~CR0_EM
); /* stop emulating */
asm(" frstor %0 " : : "g" (curpcb
->pcb_savefpu
));
curpcb
->pcb_flags
|= FP_WASUSED
| FP_NEEDSSAVE
;
curpcb
->pcb_flags
&= ~FP_NEEDSRESTORE
;