xref: /freebsd/sys/amd64/amd64/fpu.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
1 /*-
2  * Copyright (c) 1990 William Jolitz.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)npx.c	7.2 (Berkeley) 5/12/91
35  *	$Id: npx.c,v 1.45 1997/06/02 08:19:05 dfr Exp $
36  */
37 
38 #include "npx.h"
39 #if NNPX > 0
40 
41 #include "opt_cpu.h"
42 #include "opt_math_emulate.h"
43 #include "opt_smp.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/conf.h>
50 #include <sys/file.h>
51 #include <sys/proc.h>
52 #ifdef NPX_DEBUG
53 #include <sys/syslog.h>
54 #endif
55 #include <sys/signalvar.h>
56 
57 #include <machine/asmacros.h>
58 #include <machine/cpu.h>
59 #include <machine/ipl.h>
60 #include <machine/md_var.h>
61 #include <machine/pcb.h>
62 #include <machine/trap.h>
63 #include <machine/clock.h>
64 #include <machine/specialreg.h>
65 #if defined(APIC_IO)
66 #include <machine/smp.h>
67 #include <machine/apic.h>
68 #include <machine/mpapic.h>
69 #endif /* APIC_IO */
70 
71 #include <i386/isa/icu.h>
72 #include <i386/isa/isa_device.h>
73 #include <i386/isa/intr_machdep.h>
74 #include <i386/isa/isa.h>
75 
76 /*
77  * 387 and 287 Numeric Coprocessor Extension (NPX) Driver.
78  */
79 
80 /* Configuration flags. */
81 #define	NPX_DISABLE_I586_OPTIMIZED_BCOPY	(1 << 0)
82 #define	NPX_DISABLE_I586_OPTIMIZED_BZERO	(1 << 1)
83 #define	NPX_DISABLE_I586_OPTIMIZED_COPYIO	(1 << 2)
84 
85 /* XXX - should be in header file. */
86 extern void (*bcopy_vector) __P((const void *from, void *to, size_t len));
87 extern void (*ovbcopy_vector) __P((const void *from, void *to, size_t len));
88 extern int (*copyin_vector) __P((const void *udaddr, void *kaddr, size_t len));
89 extern int (*copyout_vector) __P((const void *kaddr, void *udaddr, size_t len));
90 
91 void	i586_bcopy __P((const void *from, void *to, size_t len));
92 void	i586_bzero __P((void *buf, size_t len));
93 int	i586_copyin __P((const void *udaddr, void *kaddr, size_t len));
94 int	i586_copyout __P((const void *kaddr, void *udaddr, size_t len));
95 
96 #ifdef	__GNUC__
97 
98 #define	fldcw(addr)		__asm("fldcw %0" : : "m" (*(addr)))
99 #define	fnclex()		__asm("fnclex")
100 #define	fninit()		__asm("fninit")
101 #define	fnop()			__asm("fnop")
102 #define	fnsave(addr)		__asm("fnsave %0" : "=m" (*(addr)))
103 #define	fnstcw(addr)		__asm("fnstcw %0" : "=m" (*(addr)))
104 #define	fnstsw(addr)		__asm("fnstsw %0" : "=m" (*(addr)))
105 #define	fp_divide_by_0()	__asm("fldz; fld1; fdiv %st,%st(1); fnop")
106 #define	frstor(addr)		__asm("frstor %0" : : "m" (*(addr)))
107 #define	start_emulating()	__asm("smsw %%ax; orb %0,%%al; lmsw %%ax" \
108 				      : : "n" (CR0_TS) : "ax")
109 #define	stop_emulating()	__asm("clts")
110 
111 #else	/* not __GNUC__ */
112 
113 void	fldcw		__P((caddr_t addr));
114 void	fnclex		__P((void));
115 void	fninit		__P((void));
116 void	fnop		__P((void));
117 void	fnsave		__P((caddr_t addr));
118 void	fnstcw		__P((caddr_t addr));
119 void	fnstsw		__P((caddr_t addr));
120 void	fp_divide_by_0	__P((void));
121 void	frstor		__P((caddr_t addr));
122 void	start_emulating	__P((void));
123 void	stop_emulating	__P((void));
124 
125 #endif	/* __GNUC__ */
126 
127 typedef u_char bool_t;
128 
129 static	int	npxattach	__P((struct isa_device *dvp));
130 static	int	npxprobe	__P((struct isa_device *dvp));
131 static	int	npxprobe1	__P((struct isa_device *dvp));
132 
133 struct	isa_driver npxdriver = {
134 	npxprobe, npxattach, "npx",
135 };
136 
137 int	hw_float;		/* XXX currently just alias for npx_exists */
138 
139 SYSCTL_INT(_hw,HW_FLOATINGPT, floatingpoint,
140 	CTLFLAG_RD, &hw_float, 0,
141 	"Floatingpoint instructions executed in hardware");
142 
143 static u_int	npx0_imask = SWI_CLOCK_MASK;
144 
145 #ifndef SMP	/* XXX per-cpu on smp */
146 struct proc	*npxproc;
147 #endif
148 
149 static	bool_t			npx_ex16;
150 static	bool_t			npx_exists;
151 static	struct gate_descriptor	npx_idt_probeintr;
152 static	int			npx_intrno;
153 static	volatile u_int		npx_intrs_while_probing;
154 static	bool_t			npx_irq13;
155 static	volatile u_int		npx_traps_while_probing;
156 
157 /*
158  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
159  * interrupts.  We'll still need a special exception 16 handler.  The busy
160  * latch stuff in probeintr() can be moved to npxprobe().
161  */
162 inthand_t probeintr;
163 
164 #if defined(APIC_IO)
165 
166 asm
167 ("
168 	.text
169 	.p2align 2,0x90
170 " __XSTRING(CNAME(probeintr)) ":
171 	ss
172 	incl	" __XSTRING(CNAME(npx_intrs_while_probing)) "
173 	pushl	%eax
174 	movl	$lapic_eoi,%eax		# EOI to local APIC
175 	movl	$0,(%eax)		# movl $0, APIC_EOI(%eax)
176 	movb	$0,%al
177 	outb	%al,$0xf0		# clear BUSY# latch
178 	popl	%eax
179 	iret
180 ");
181 
182 #else
183 
184 asm
185 ("
186 	.text
187 	.p2align 2,0x90
188 " __XSTRING(CNAME(probeintr)) ":
189 	ss
190 	incl	" __XSTRING(CNAME(npx_intrs_while_probing)) "
191 	pushl	%eax
192 	movb	$0x20,%al	# EOI (asm in strings loses cpp features)
193 	outb	%al,$0xa0	# IO_ICU2
194 	outb	%al,$0x20	# IO_ICU1
195 	movb	$0,%al
196 	outb	%al,$0xf0	# clear BUSY# latch
197 	popl	%eax
198 	iret
199 ");
200 
201 #endif /* APIC_IO */
202 
203 inthand_t probetrap;
204 asm
205 ("
206 	.text
207 	.p2align 2,0x90
208 " __XSTRING(CNAME(probetrap)) ":
209 	ss
210 	incl	" __XSTRING(CNAME(npx_traps_while_probing)) "
211 	fnclex
212 	iret
213 ");
214 
215 /*
216  * Probe routine.  Initialize cr0 to give correct behaviour for [f]wait
217  * whether the device exists or not (XXX should be elsewhere).  Set flags
218  * to tell npxattach() what to do.  Modify device struct if npx doesn't
219  * need to use interrupts.  Return 1 if device exists.
220  */
221 static int
222 npxprobe(dvp)
223 	struct isa_device *dvp;
224 {
225 	int	result;
226 	u_long	save_eflags;
227 #if defined(APIC_IO)
228 	u_int	save_apic_mask;
229 #else
230 	u_char	save_icu1_mask;
231 	u_char	save_icu2_mask;
232 #endif /* APIC_IO */
233 	struct	gate_descriptor save_idt_npxintr;
234 	struct	gate_descriptor save_idt_npxtrap;
235 	/*
236 	 * This routine is now just a wrapper for npxprobe1(), to install
237 	 * special npx interrupt and trap handlers, to enable npx interrupts
238 	 * and to disable other interrupts.  Someday isa_configure() will
239 	 * install suitable handlers and run with interrupts enabled so we
240 	 * won't need to do so much here.
241 	 */
242 	npx_intrno = NRSVIDT + ffs(dvp->id_irq) - 1;
243 	save_eflags = read_eflags();
244 	disable_intr();
245 #if defined(APIC_IO)
246 	save_apic_mask = INTRGET();
247 #else
248 	save_icu1_mask = inb(IO_ICU1 + 1);
249 	save_icu2_mask = inb(IO_ICU2 + 1);
250 #endif /* APIC_IO */
251 	save_idt_npxintr = idt[npx_intrno];
252 	save_idt_npxtrap = idt[16];
253 #if defined(APIC_IO)
254 	INTRSET( ~dvp->id_irq );
255 #else
256 	outb(IO_ICU1 + 1, ~(IRQ_SLAVE | dvp->id_irq));
257 	outb(IO_ICU2 + 1, ~(dvp->id_irq >> 8));
258 #endif /* APIC_IO */
259 	setidt(16, probetrap, SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
260 	setidt(npx_intrno, probeintr, SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
261 	npx_idt_probeintr = idt[npx_intrno];
262 	enable_intr();
263 	result = npxprobe1(dvp);
264 	disable_intr();
265 #if defined(APIC_IO)
266 	INTRSET( save_apic_mask );
267 #else
268 	outb(IO_ICU1 + 1, save_icu1_mask);
269 	outb(IO_ICU2 + 1, save_icu2_mask);
270 #endif /* APIC_IO */
271 	idt[npx_intrno] = save_idt_npxintr;
272 	idt[16] = save_idt_npxtrap;
273 	write_eflags(save_eflags);
274 	return (result);
275 }
276 
277 static int
278 npxprobe1(dvp)
279 	struct isa_device *dvp;
280 {
281 	u_short control;
282 	u_short status;
283 
284 	/*
285 	 * Partially reset the coprocessor, if any.  Some BIOS's don't reset
286 	 * it after a warm boot.
287 	 */
288 	outb(0xf1, 0);		/* full reset on some systems, NOP on others */
289 	outb(0xf0, 0);		/* clear BUSY# latch */
290 	/*
291 	 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
292 	 * instructions.  We must set the CR0_MP bit and use the CR0_TS
293 	 * bit to control the trap, because setting the CR0_EM bit does
294 	 * not cause WAIT instructions to trap.  It's important to trap
295 	 * WAIT instructions - otherwise the "wait" variants of no-wait
296 	 * control instructions would degenerate to the "no-wait" variants
297 	 * after FP context switches but work correctly otherwise.  It's
298 	 * particularly important to trap WAITs when there is no NPX -
299 	 * otherwise the "wait" variants would always degenerate.
300 	 *
301 	 * Try setting CR0_NE to get correct error reporting on 486DX's.
302 	 * Setting it should fail or do nothing on lesser processors.
303 	 */
304 	load_cr0(rcr0() | CR0_MP | CR0_NE);
305 	/*
306 	 * But don't trap while we're probing.
307 	 */
308 	stop_emulating();
309 	/*
310 	 * Finish resetting the coprocessor, if any.  If there is an error
311 	 * pending, then we may get a bogus IRQ13, but probeintr() will handle
312 	 * it OK.  Bogus halts have never been observed, but we enabled
313 	 * IRQ13 and cleared the BUSY# latch early to handle them anyway.
314 	 */
315 	fninit();
316 	/*
317 	 * Don't use fwait here because it might hang.
318 	 * Don't use fnop here because it usually hangs if there is no FPU.
319 	 */
320 	DELAY(1000);		/* wait for any IRQ13 */
321 #ifdef DIAGNOSTIC
322 	if (npx_intrs_while_probing != 0)
323 		printf("fninit caused %u bogus npx interrupt(s)\n",
324 		       npx_intrs_while_probing);
325 	if (npx_traps_while_probing != 0)
326 		printf("fninit caused %u bogus npx trap(s)\n",
327 		       npx_traps_while_probing);
328 #endif
329 	/*
330 	 * Check for a status of mostly zero.
331 	 */
332 	status = 0x5a5a;
333 	fnstsw(&status);
334 	if ((status & 0xb8ff) == 0) {
335 		/*
336 		 * Good, now check for a proper control word.
337 		 */
338 		control = 0x5a5a;
339 		fnstcw(&control);
340 		if ((control & 0x1f3f) == 0x033f) {
341 			hw_float = npx_exists = 1;
342 			/*
343 			 * We have an npx, now divide by 0 to see if exception
344 			 * 16 works.
345 			 */
346 			control &= ~(1 << 2);	/* enable divide by 0 trap */
347 			fldcw(&control);
348 			npx_traps_while_probing = npx_intrs_while_probing = 0;
349 			fp_divide_by_0();
350 			if (npx_traps_while_probing != 0) {
351 				/*
352 				 * Good, exception 16 works.
353 				 */
354 				npx_ex16 = 1;
355 				dvp->id_irq = 0;	/* zap the interrupt */
356 				/*
357 				 * special return value to flag that we do not
358 				 * actually use any I/O registers
359 				 */
360 				return (-1);
361 			}
362 			if (npx_intrs_while_probing != 0) {
363 				/*
364 				 * Bad, we are stuck with IRQ13.
365 				 */
366 				npx_irq13 = 1;
367 				/*
368 				 * npxattach would be too late to set npx0_imask.
369 				 */
370 				npx0_imask |= dvp->id_irq;
371 				return (IO_NPXSIZE);
372 			}
373 			/*
374 			 * Worse, even IRQ13 is broken.  Use emulator.
375 			 */
376 		}
377 	}
378 	/*
379 	 * Probe failed, but we want to get to npxattach to initialize the
380 	 * emulator and say that it has been installed.  XXX handle devices
381 	 * that aren't really devices better.
382 	 */
383 	dvp->id_irq = 0;
384 	/*
385 	 * special return value to flag that we do not
386 	 * actually use any I/O registers
387 	 */
388 	return (-1);
389 }
390 
391 /*
392  * Attach routine - announce which it is, and wire into system
393  */
394 int
395 npxattach(dvp)
396 	struct isa_device *dvp;
397 {
398 	/* The caller has printed "irq 13" for the npx_irq13 case. */
399 	if (!npx_irq13) {
400 		printf("npx%d: ", dvp->id_unit);
401 		if (npx_ex16)
402 			printf("INT 16 interface\n");
403 #if defined(MATH_EMULATE) || defined(GPL_MATH_EMULATE)
404 		else if (npx_exists) {
405 			printf("error reporting broken; using 387 emulator\n");
406 			hw_float = npx_exists = 0;
407 		} else
408 			printf("387 emulator\n");
409 #else
410 		else
411 			printf("no 387 emulator in kernel!\n");
412 #endif
413 	}
414 	npxinit(__INITIAL_NPXCW__);
415 
416 #if defined(I586_CPU) && !defined(SMP)
417 	/* FPU not working under SMP yet */
418 	if (cpu_class == CPUCLASS_586 && npx_ex16) {
419 		if (!(dvp->id_flags & NPX_DISABLE_I586_OPTIMIZED_BCOPY)) {
420 			bcopy_vector = i586_bcopy;
421 			ovbcopy_vector = i586_bcopy;
422 		}
423 		if (!(dvp->id_flags & NPX_DISABLE_I586_OPTIMIZED_BZERO))
424 			bzero = i586_bzero;
425 		if (!(dvp->id_flags & NPX_DISABLE_I586_OPTIMIZED_COPYIO)) {
426 			copyin_vector = i586_copyin;
427 			copyout_vector = i586_copyout;
428 		}
429 	}
430 #endif
431 
432 	return (1);		/* XXX unused */
433 }
434 
435 /*
436  * Initialize floating point unit.
437  */
438 void
439 npxinit(control)
440 	u_short control;
441 {
442 	struct save87 dummy;
443 
444 	if (!npx_exists)
445 		return;
446 	/*
447 	 * fninit has the same h/w bugs as fnsave.  Use the detoxified
448 	 * fnsave to throw away any junk in the fpu.  npxsave() initializes
449 	 * the fpu and sets npxproc = NULL as important side effects.
450 	 */
451 	npxsave(&dummy);
452 	stop_emulating();
453 	fldcw(&control);
454 	if (curpcb != NULL)
455 		fnsave(&curpcb->pcb_savefpu);
456 	start_emulating();
457 }
458 
459 /*
460  * Free coprocessor (if we have it).
461  */
462 void
463 npxexit(p)
464 	struct proc *p;
465 {
466 
467 	if (p == npxproc)
468 		npxsave(&curpcb->pcb_savefpu);
469 #ifdef NPX_DEBUG
470 	if (npx_exists) {
471 		u_int	masked_exceptions;
472 
473 		masked_exceptions = curpcb->pcb_savefpu.sv_env.en_cw
474 				    & curpcb->pcb_savefpu.sv_env.en_sw & 0x7f;
475 		/*
476 		 * Log exceptions that would have trapped with the old
477 		 * control word (overflow, divide by 0, and invalid operand).
478 		 */
479 		if (masked_exceptions & 0x0d)
480 			log(LOG_ERR,
481 	"pid %d (%s) exited with masked floating point exceptions 0x%02x\n",
482 			    p->p_pid, p->p_comm, masked_exceptions);
483 	}
484 #endif
485 }
486 
487 /*
488  * Preserve the FP status word, clear FP exceptions, then generate a SIGFPE.
489  *
490  * Clearing exceptions is necessary mainly to avoid IRQ13 bugs.  We now
491  * depend on longjmp() restoring a usable state.  Restoring the state
492  * or examining it might fail if we didn't clear exceptions.
493  *
494  * XXX there is no standard way to tell SIGFPE handlers about the error
495  * state.  The old interface:
496  *
497  *	void handler(int sig, int code, struct sigcontext *scp);
498  *
499  * is broken because it is non-ANSI and because the FP state is not in
500  * struct sigcontext.
501  *
502  * XXX the FP state is not preserved across signal handlers.  So signal
503  * handlers cannot afford to do FP unless they preserve the state or
504  * longjmp() out.  Both preserving the state and longjmp()ing may be
505  * destroyed by IRQ13 bugs.  Clearing FP exceptions is not an acceptable
506  * solution for signals other than SIGFPE.
507  */
508 void
509 npxintr(unit)
510 	int unit;
511 {
512 	int code;
513 	struct intrframe *frame;
514 
515 	if (npxproc == NULL || !npx_exists) {
516 		printf("npxintr: npxproc = %p, curproc = %p, npx_exists = %d\n",
517 		       npxproc, curproc, npx_exists);
518 		panic("npxintr from nowhere");
519 	}
520 	if (npxproc != curproc) {
521 		printf("npxintr: npxproc = %p, curproc = %p, npx_exists = %d\n",
522 		       npxproc, curproc, npx_exists);
523 		panic("npxintr from non-current process");
524 	}
525 
526 	outb(0xf0, 0);
527 	fnstsw(&curpcb->pcb_savefpu.sv_ex_sw);
528 	fnclex();
529 	fnop();
530 
531 	/*
532 	 * Pass exception to process.
533 	 */
534 	frame = (struct intrframe *)&unit;	/* XXX */
535 	if (ISPL(frame->if_cs) == SEL_UPL) {
536 		/*
537 		 * Interrupt is essentially a trap, so we can afford to call
538 		 * the SIGFPE handler (if any) as soon as the interrupt
539 		 * returns.
540 		 *
541 		 * XXX little or nothing is gained from this, and plenty is
542 		 * lost - the interrupt frame has to contain the trap frame
543 		 * (this is otherwise only necessary for the rescheduling trap
544 		 * in doreti, and the frame for that could easily be set up
545 		 * just before it is used).
546 		 */
547 		curproc->p_md.md_regs = (struct trapframe *)&frame->if_es;
548 #ifdef notyet
549 		/*
550 		 * Encode the appropriate code for detailed information on
551 		 * this exception.
552 		 */
553 		code = XXX_ENCODE(curpcb->pcb_savefpu.sv_ex_sw);
554 #else
555 		code = 0;	/* XXX */
556 #endif
557 		trapsignal(curproc, SIGFPE, code);
558 	} else {
559 		/*
560 		 * Nested interrupt.  These losers occur when:
561 		 *	o an IRQ13 is bogusly generated at a bogus time, e.g.:
562 		 *		o immediately after an fnsave or frstor of an
563 		 *		  error state.
564 		 *		o a couple of 386 instructions after
565 		 *		  "fstpl _memvar" causes a stack overflow.
566 		 *	  These are especially nasty when combined with a
567 		 *	  trace trap.
568 		 *	o an IRQ13 occurs at the same time as another higher-
569 		 *	  priority interrupt.
570 		 *
571 		 * Treat them like a true async interrupt.
572 		 */
573 		psignal(curproc, SIGFPE);
574 	}
575 }
576 
577 /*
578  * Implement device not available (DNA) exception
579  *
580  * It would be better to switch FP context here (if curproc != npxproc)
581  * and not necessarily for every context switch, but it is too hard to
582  * access foreign pcb's.
583  */
584 int
585 npxdna()
586 {
587 	if (!npx_exists)
588 		return (0);
589 	if (npxproc != NULL) {
590 		printf("npxdna: npxproc = %p, curproc = %p\n",
591 		       npxproc, curproc);
592 		panic("npxdna");
593 	}
594 	stop_emulating();
595 	/*
596 	 * Record new context early in case frstor causes an IRQ13.
597 	 */
598 	npxproc = curproc;
599 	curpcb->pcb_savefpu.sv_ex_sw = 0;
600 	/*
601 	 * The following frstor may cause an IRQ13 when the state being
602 	 * restored has a pending error.  The error will appear to have been
603 	 * triggered by the current (npx) user instruction even when that
604 	 * instruction is a no-wait instruction that should not trigger an
605 	 * error (e.g., fnclex).  On at least one 486 system all of the
606 	 * no-wait instructions are broken the same as frstor, so our
607 	 * treatment does not amplify the breakage.  On at least one
608 	 * 386/Cyrix 387 system, fnclex works correctly while frstor and
609 	 * fnsave are broken, so our treatment breaks fnclex if it is the
610 	 * first FPU instruction after a context switch.
611 	 */
612 	frstor(&curpcb->pcb_savefpu);
613 
614 	return (1);
615 }
616 
617 /*
618  * Wrapper for fnsave instruction to handle h/w bugs.  If there is an error
619  * pending, then fnsave generates a bogus IRQ13 on some systems.  Force
620  * any IRQ13 to be handled immediately, and then ignore it.  This routine is
621  * often called at splhigh so it must not use many system services.  In
622  * particular, it's much easier to install a special handler than to
623  * guarantee that it's safe to use npxintr() and its supporting code.
624  */
625 void
626 npxsave(addr)
627 	struct save87 *addr;
628 {
629 #if defined(APIC_IO)
630 	u_int	apic_mask;
631 	u_int	old_apic_mask;
632 #else
633 	u_char	icu1_mask;
634 	u_char	icu2_mask;
635 	u_char	old_icu1_mask;
636 	u_char	old_icu2_mask;
637 #endif /* APIC_IO */
638 	struct gate_descriptor	save_idt_npxintr;
639 
640 	disable_intr();
641 #if defined(APIC_IO)
642 	old_apic_mask = INTRGET();
643 #else
644 	old_icu1_mask = inb(IO_ICU1 + 1);
645 	old_icu2_mask = inb(IO_ICU2 + 1);
646 #endif /* APIC_IO */
647 	save_idt_npxintr = idt[npx_intrno];
648 #if defined(APIC_IO)
649 	/** FIXME: try clrIoApicMaskBit( npx0_imask ); */
650 	INTRSET( old_apic_mask & ~(npx0_imask & 0xffff) );
651 #else
652 	outb(IO_ICU1 + 1, old_icu1_mask & ~(IRQ_SLAVE | npx0_imask));
653 	outb(IO_ICU2 + 1, old_icu2_mask & ~(npx0_imask >> 8));
654 #endif /* APIC_IO */
655 	idt[npx_intrno] = npx_idt_probeintr;
656 	enable_intr();
657 	stop_emulating();
658 	fnsave(addr);
659 	fnop();
660 	start_emulating();
661 	npxproc = NULL;
662 	disable_intr();
663 #if defined(APIC_IO)
664 	apic_mask = INTRGET();		/* masks may have changed */
665         INTRSET( (apic_mask & ~(npx0_imask & 0xffff)) |
666 		 (old_apic_mask & (npx0_imask & 0xffff)));
667 #else
668 	icu1_mask = inb(IO_ICU1 + 1);	/* masks may have changed */
669 	icu2_mask = inb(IO_ICU2 + 1);
670 	outb(IO_ICU1 + 1,
671 	     (icu1_mask & ~npx0_imask) | (old_icu1_mask & npx0_imask));
672 	outb(IO_ICU2 + 1,
673 	     (icu2_mask & ~(npx0_imask >> 8))
674 	     | (old_icu2_mask & (npx0_imask >> 8)));
675 #endif /* APIC_IO */
676 	idt[npx_intrno] = save_idt_npxintr;
677 	enable_intr();		/* back to usual state */
678 }
679 
680 #endif /* NNPX > 0 */
681