15b81b6b3SRodney W. Grimes /*- 25b81b6b3SRodney W. Grimes * Copyright (c) 1990 William Jolitz. 35b81b6b3SRodney W. Grimes * Copyright (c) 1991 The Regents of the University of California. 45b81b6b3SRodney W. Grimes * All rights reserved. 55b81b6b3SRodney W. Grimes * 65b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 75b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 85b81b6b3SRodney W. Grimes * are met: 95b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 105b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 115b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 125b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 135b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 145b81b6b3SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 155b81b6b3SRodney W. Grimes * must display the following acknowledgement: 165b81b6b3SRodney W. Grimes * This product includes software developed by the University of 175b81b6b3SRodney W. Grimes * California, Berkeley and its contributors. 185b81b6b3SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 195b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 205b81b6b3SRodney W. Grimes * without specific prior written permission. 215b81b6b3SRodney W. Grimes * 225b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 235b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 245b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 255b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 265b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 275b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 285b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 295b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 305b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 315b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 325b81b6b3SRodney W. Grimes * SUCH DAMAGE. 335b81b6b3SRodney W. Grimes * 345c644711SRodney W. Grimes * from: @(#)npx.c 7.2 (Berkeley) 5/12/91 35da4113b3SPeter Wemm * $Id: npx.c,v 1.75 1999/07/26 05:47:31 cracauer Exp $ 365b81b6b3SRodney W. Grimes */ 375b81b6b3SRodney W. Grimes 385b81b6b3SRodney W. Grimes #include "npx.h" 395b81b6b3SRodney W. Grimes #if NNPX > 0 405b81b6b3SRodney W. Grimes 413f2076daSEivind Eklund #include "opt_debug_npx.h" 42d9256606SPeter Wemm #include "opt_math_emulate.h" 43a73af3a2SGarrett Wollman 44f540b106SGarrett Wollman #include <sys/param.h> 45f540b106SGarrett Wollman #include <sys/systm.h> 466182fdbdSPeter Wemm #include <sys/bus.h> 473a34a5c3SPoul-Henning Kamp #include <sys/kernel.h> 48cd59d49dSBruce Evans #include <sys/malloc.h> 496182fdbdSPeter Wemm #include <sys/module.h> 503a34a5c3SPoul-Henning Kamp #include <sys/sysctl.h> 51f540b106SGarrett Wollman #include <sys/proc.h> 526182fdbdSPeter Wemm #include <machine/bus.h> 536182fdbdSPeter Wemm #include <sys/rman.h> 5426add149SBruce Evans #ifdef NPX_DEBUG 55663f1485SBruce Evans #include <sys/syslog.h> 5626add149SBruce Evans #endif 57663f1485SBruce Evans #include <sys/signalvar.h> 582f86936aSGarrett Wollman 59e0605ebdSBruce Evans #ifndef SMP 609081eec1SJohn Polstra #include <machine/asmacros.h> 61e0605ebdSBruce Evans #endif 627f47cf2fSBruce Evans #include <machine/cputypes.h> 637f47cf2fSBruce Evans #include <machine/frame.h> 645400ed3bSPeter Wemm #include <machine/ipl.h> 65c673fe98SBruce Evans #include <machine/md_var.h> 665400ed3bSPeter Wemm #include <machine/pcb.h> 677f47cf2fSBruce Evans #include <machine/psl.h> 68e0605ebdSBruce Evans #ifndef SMP 69663f1485SBruce Evans #include <machine/clock.h> 70e0605ebdSBruce Evans #endif 716182fdbdSPeter Wemm #include <machine/resource.h> 72f540b106SGarrett Wollman #include <machine/specialreg.h> 737f47cf2fSBruce Evans #include <machine/segments.h> 742f86936aSGarrett Wollman 75e0605ebdSBruce Evans #ifndef SMP 76f540b106SGarrett Wollman #include <i386/isa/icu.h> 7768352337SDoug Rabson #include <i386/isa/intr_machdep.h> 78f540b106SGarrett Wollman #include <i386/isa/isa.h> 79e0605ebdSBruce Evans #endif 805b81b6b3SRodney W. Grimes 815b81b6b3SRodney W. Grimes /* 825b81b6b3SRodney W. Grimes * 387 and 287 Numeric Coprocessor Extension (NPX) Driver. 835b81b6b3SRodney W. Grimes */ 845b81b6b3SRodney W. Grimes 851fe04850SBruce Evans /* Configuration flags. */ 861fe04850SBruce Evans #define NPX_DISABLE_I586_OPTIMIZED_BCOPY (1 << 0) 871fe04850SBruce Evans #define NPX_DISABLE_I586_OPTIMIZED_BZERO (1 << 1) 881fe04850SBruce Evans #define NPX_DISABLE_I586_OPTIMIZED_COPYIO (1 << 2) 89a7674320SMartin Cracauer #define NPX_PREFER_EMULATOR (1 << 3) 901fe04850SBruce Evans 915b81b6b3SRodney W. Grimes #ifdef __GNUC__ 925b81b6b3SRodney W. Grimes 9337e52b59SBruce Evans #define fldcw(addr) __asm("fldcw %0" : : "m" (*(addr))) 945b81b6b3SRodney W. Grimes #define fnclex() __asm("fnclex") 955b81b6b3SRodney W. Grimes #define fninit() __asm("fninit") 9637e52b59SBruce Evans #define fnop() __asm("fnop") 971d37f051SBruce Evans #define fnsave(addr) __asm __volatile("fnsave %0" : "=m" (*(addr))) 981d37f051SBruce Evans #define fnstcw(addr) __asm __volatile("fnstcw %0" : "=m" (*(addr))) 991d37f051SBruce Evans #define fnstsw(addr) __asm __volatile("fnstsw %0" : "=m" (*(addr))) 10037e52b59SBruce Evans #define fp_divide_by_0() __asm("fldz; fld1; fdiv %st,%st(1); fnop") 10137e52b59SBruce Evans #define frstor(addr) __asm("frstor %0" : : "m" (*(addr))) 1025b81b6b3SRodney W. Grimes #define start_emulating() __asm("smsw %%ax; orb %0,%%al; lmsw %%ax" \ 1035b81b6b3SRodney W. Grimes : : "n" (CR0_TS) : "ax") 1045b81b6b3SRodney W. Grimes #define stop_emulating() __asm("clts") 1055b81b6b3SRodney W. Grimes 1065b81b6b3SRodney W. Grimes #else /* not __GNUC__ */ 1075b81b6b3SRodney W. Grimes 1085b81b6b3SRodney W. Grimes void fldcw __P((caddr_t addr)); 1095b81b6b3SRodney W. Grimes void fnclex __P((void)); 1105b81b6b3SRodney W. Grimes void fninit __P((void)); 11137e52b59SBruce Evans void fnop __P((void)); 1125b81b6b3SRodney W. Grimes void fnsave __P((caddr_t addr)); 1135b81b6b3SRodney W. Grimes void fnstcw __P((caddr_t addr)); 1145b81b6b3SRodney W. Grimes void fnstsw __P((caddr_t addr)); 1155b81b6b3SRodney W. Grimes void fp_divide_by_0 __P((void)); 1165b81b6b3SRodney W. Grimes void frstor __P((caddr_t addr)); 1175b81b6b3SRodney W. Grimes void start_emulating __P((void)); 1185b81b6b3SRodney W. Grimes void stop_emulating __P((void)); 1195b81b6b3SRodney W. Grimes 1205b81b6b3SRodney W. Grimes #endif /* __GNUC__ */ 1215b81b6b3SRodney W. Grimes 1225b81b6b3SRodney W. Grimes typedef u_char bool_t; 1235b81b6b3SRodney W. Grimes 1246182fdbdSPeter Wemm static int npx_attach __P((device_t dev)); 1256182fdbdSPeter Wemm void npx_intr __P((void *)); 126da4113b3SPeter Wemm static void npx_identify __P((driver_t *driver, device_t parent)); 1276182fdbdSPeter Wemm static int npx_probe __P((device_t dev)); 1286182fdbdSPeter Wemm static int npx_probe1 __P((device_t dev)); 129a4ca0596SDmitrij Tejblum #ifdef I586_CPU 130cd59d49dSBruce Evans static long timezero __P((const char *funcname, 131cd59d49dSBruce Evans void (*func)(void *buf, size_t len))); 132a4ca0596SDmitrij Tejblum #endif /* I586_CPU */ 1335b81b6b3SRodney W. Grimes 13437e52b59SBruce Evans int hw_float; /* XXX currently just alias for npx_exists */ 1353a34a5c3SPoul-Henning Kamp 1363a34a5c3SPoul-Henning Kamp SYSCTL_INT(_hw,HW_FLOATINGPT, floatingpoint, 1373a34a5c3SPoul-Henning Kamp CTLFLAG_RD, &hw_float, 0, 1383a34a5c3SPoul-Henning Kamp "Floatingpoint instructions executed in hardware"); 1393a34a5c3SPoul-Henning Kamp 140f1d19042SArchie Cobbs #ifndef SMP 1416f4e0bebSPoul-Henning Kamp static u_int npx0_imask = SWI_CLOCK_MASK; 142f1d19042SArchie Cobbs static struct gate_descriptor npx_idt_probeintr; 143407e5f39SBruce Evans static int npx_intrno; 144f1d19042SArchie Cobbs static volatile u_int npx_intrs_while_probing; 145f1d19042SArchie Cobbs static volatile u_int npx_traps_while_probing; 146f1d19042SArchie Cobbs #endif 147b3196e4bSPeter Wemm 1485b81b6b3SRodney W. Grimes static bool_t npx_ex16; 1495b81b6b3SRodney W. Grimes static bool_t npx_exists; 1505b81b6b3SRodney W. Grimes static bool_t npx_irq13; 1512f2ffb8dSPeter Wemm static int npx_irq; /* irq number */ 1525b81b6b3SRodney W. Grimes 1533902c3efSSteve Passe #ifndef SMP 1545b81b6b3SRodney W. Grimes /* 1555b81b6b3SRodney W. Grimes * Special interrupt handlers. Someday intr0-intr15 will be used to count 1565b81b6b3SRodney W. Grimes * interrupts. We'll still need a special exception 16 handler. The busy 15737e52b59SBruce Evans * latch stuff in probeintr() can be moved to npxprobe(). 1585b81b6b3SRodney W. Grimes */ 159663f1485SBruce Evans inthand_t probeintr; 16016967563SBruce Evans __asm(" \n\ 16116967563SBruce Evans .text \n\ 16216967563SBruce Evans .p2align 2,0x90 \n\ 163ea2b3e3dSBruce Evans .type " __XSTRING(CNAME(probeintr)) ",@function \n\ 16416967563SBruce Evans " __XSTRING(CNAME(probeintr)) ": \n\ 16516967563SBruce Evans ss \n\ 16616967563SBruce Evans incl " __XSTRING(CNAME(npx_intrs_while_probing)) " \n\ 16716967563SBruce Evans pushl %eax \n\ 16816967563SBruce Evans movb $0x20,%al # EOI (asm in strings loses cpp features) \n\ 16916967563SBruce Evans outb %al,$0xa0 # IO_ICU2 \n\ 17016967563SBruce Evans outb %al,$0x20 # IO_ICU1 \n\ 17116967563SBruce Evans movb $0,%al \n\ 17216967563SBruce Evans outb %al,$0xf0 # clear BUSY# latch \n\ 17316967563SBruce Evans popl %eax \n\ 17416967563SBruce Evans iret \n\ 1755b81b6b3SRodney W. Grimes "); 1765b81b6b3SRodney W. Grimes 177663f1485SBruce Evans inthand_t probetrap; 17816967563SBruce Evans __asm(" \n\ 17916967563SBruce Evans .text \n\ 18016967563SBruce Evans .p2align 2,0x90 \n\ 181ea2b3e3dSBruce Evans .type " __XSTRING(CNAME(probetrap)) ",@function \n\ 18216967563SBruce Evans " __XSTRING(CNAME(probetrap)) ": \n\ 18316967563SBruce Evans ss \n\ 18416967563SBruce Evans incl " __XSTRING(CNAME(npx_traps_while_probing)) " \n\ 18516967563SBruce Evans fnclex \n\ 18616967563SBruce Evans iret \n\ 1875b81b6b3SRodney W. Grimes "); 1883902c3efSSteve Passe #endif /* SMP */ 1893902c3efSSteve Passe 1905b81b6b3SRodney W. Grimes /* 191da4113b3SPeter Wemm * Identify routine. Create a connection point on our parent for probing. 192da4113b3SPeter Wemm */ 193da4113b3SPeter Wemm static void 194da4113b3SPeter Wemm npx_identify(driver, parent) 195da4113b3SPeter Wemm driver_t *driver; 196da4113b3SPeter Wemm device_t parent; 197da4113b3SPeter Wemm { 198da4113b3SPeter Wemm device_t child; 199da4113b3SPeter Wemm 200da4113b3SPeter Wemm child = BUS_ADD_CHILD(parent, 0, "npx", 0); 201da4113b3SPeter Wemm if (child == NULL) 202da4113b3SPeter Wemm panic("npx_identify"); 203da4113b3SPeter Wemm } 204da4113b3SPeter Wemm 205da4113b3SPeter Wemm /* 2065b81b6b3SRodney W. Grimes * Probe routine. Initialize cr0 to give correct behaviour for [f]wait 2075b81b6b3SRodney W. Grimes * whether the device exists or not (XXX should be elsewhere). Set flags 2085b81b6b3SRodney W. Grimes * to tell npxattach() what to do. Modify device struct if npx doesn't 2095b81b6b3SRodney W. Grimes * need to use interrupts. Return 1 if device exists. 2105b81b6b3SRodney W. Grimes */ 2115b81b6b3SRodney W. Grimes static int 2126182fdbdSPeter Wemm npx_probe(dev) 2136182fdbdSPeter Wemm device_t dev; 2145b81b6b3SRodney W. Grimes { 21597bf1787SPeter Wemm #ifdef SMP 2163902c3efSSteve Passe 2172f2ffb8dSPeter Wemm if (resource_int_value("npx", 0, "irq", &npx_irq) != 0) 2182f2ffb8dSPeter Wemm npx_irq = 13; 2196182fdbdSPeter Wemm return npx_probe1(dev); 2203902c3efSSteve Passe 2213902c3efSSteve Passe #else /* SMP */ 2223902c3efSSteve Passe 2235b81b6b3SRodney W. Grimes int result; 2245b81b6b3SRodney W. Grimes u_long save_eflags; 2255b81b6b3SRodney W. Grimes u_char save_icu1_mask; 2265b81b6b3SRodney W. Grimes u_char save_icu2_mask; 2275b81b6b3SRodney W. Grimes struct gate_descriptor save_idt_npxintr; 2285b81b6b3SRodney W. Grimes struct gate_descriptor save_idt_npxtrap; 2295b81b6b3SRodney W. Grimes /* 2305b81b6b3SRodney W. Grimes * This routine is now just a wrapper for npxprobe1(), to install 2315b81b6b3SRodney W. Grimes * special npx interrupt and trap handlers, to enable npx interrupts 2325b81b6b3SRodney W. Grimes * and to disable other interrupts. Someday isa_configure() will 2335b81b6b3SRodney W. Grimes * install suitable handlers and run with interrupts enabled so we 2345b81b6b3SRodney W. Grimes * won't need to do so much here. 2355b81b6b3SRodney W. Grimes */ 2362f2ffb8dSPeter Wemm if (resource_int_value("npx", 0, "irq", &npx_irq) != 0) 2372f2ffb8dSPeter Wemm npx_irq = 13; 2382f2ffb8dSPeter Wemm npx_intrno = NRSVIDT + npx_irq; 2395b81b6b3SRodney W. Grimes save_eflags = read_eflags(); 2405b81b6b3SRodney W. Grimes disable_intr(); 2415b81b6b3SRodney W. Grimes save_icu1_mask = inb(IO_ICU1 + 1); 2425b81b6b3SRodney W. Grimes save_icu2_mask = inb(IO_ICU2 + 1); 2435b81b6b3SRodney W. Grimes save_idt_npxintr = idt[npx_intrno]; 2445b81b6b3SRodney W. Grimes save_idt_npxtrap = idt[16]; 2456182fdbdSPeter Wemm outb(IO_ICU1 + 1, ~IRQ_SLAVE); 2462f2ffb8dSPeter Wemm outb(IO_ICU2 + 1, ~(1 << (npx_irq - 8))); 2472838c968SDavid Greenman setidt(16, probetrap, SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 2482838c968SDavid Greenman setidt(npx_intrno, probeintr, SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 2495b81b6b3SRodney W. Grimes npx_idt_probeintr = idt[npx_intrno]; 2505b81b6b3SRodney W. Grimes enable_intr(); 2516182fdbdSPeter Wemm result = npx_probe1(dev); 2525b81b6b3SRodney W. Grimes disable_intr(); 2535b81b6b3SRodney W. Grimes outb(IO_ICU1 + 1, save_icu1_mask); 2545b81b6b3SRodney W. Grimes outb(IO_ICU2 + 1, save_icu2_mask); 2555b81b6b3SRodney W. Grimes idt[npx_intrno] = save_idt_npxintr; 2565b81b6b3SRodney W. Grimes idt[16] = save_idt_npxtrap; 2575b81b6b3SRodney W. Grimes write_eflags(save_eflags); 2585b81b6b3SRodney W. Grimes return (result); 2593902c3efSSteve Passe 2603902c3efSSteve Passe #endif /* SMP */ 2615b81b6b3SRodney W. Grimes } 2625b81b6b3SRodney W. Grimes 2635b81b6b3SRodney W. Grimes static int 2646182fdbdSPeter Wemm npx_probe1(dev) 2656182fdbdSPeter Wemm device_t dev; 2665b81b6b3SRodney W. Grimes { 267f1d19042SArchie Cobbs #ifndef SMP 26837e52b59SBruce Evans u_short control; 26937e52b59SBruce Evans u_short status; 270f1d19042SArchie Cobbs #endif 27137e52b59SBruce Evans 2725b81b6b3SRodney W. Grimes /* 2735b81b6b3SRodney W. Grimes * Partially reset the coprocessor, if any. Some BIOS's don't reset 2745b81b6b3SRodney W. Grimes * it after a warm boot. 2755b81b6b3SRodney W. Grimes */ 2765b81b6b3SRodney W. Grimes outb(0xf1, 0); /* full reset on some systems, NOP on others */ 2775b81b6b3SRodney W. Grimes outb(0xf0, 0); /* clear BUSY# latch */ 2785b81b6b3SRodney W. Grimes /* 2795b81b6b3SRodney W. Grimes * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT 2805b81b6b3SRodney W. Grimes * instructions. We must set the CR0_MP bit and use the CR0_TS 2815b81b6b3SRodney W. Grimes * bit to control the trap, because setting the CR0_EM bit does 2825b81b6b3SRodney W. Grimes * not cause WAIT instructions to trap. It's important to trap 2835b81b6b3SRodney W. Grimes * WAIT instructions - otherwise the "wait" variants of no-wait 2845b81b6b3SRodney W. Grimes * control instructions would degenerate to the "no-wait" variants 2855b81b6b3SRodney W. Grimes * after FP context switches but work correctly otherwise. It's 2865b81b6b3SRodney W. Grimes * particularly important to trap WAITs when there is no NPX - 2875b81b6b3SRodney W. Grimes * otherwise the "wait" variants would always degenerate. 2885b81b6b3SRodney W. Grimes * 2895b81b6b3SRodney W. Grimes * Try setting CR0_NE to get correct error reporting on 486DX's. 2905b81b6b3SRodney W. Grimes * Setting it should fail or do nothing on lesser processors. 2915b81b6b3SRodney W. Grimes */ 2925b81b6b3SRodney W. Grimes load_cr0(rcr0() | CR0_MP | CR0_NE); 2935b81b6b3SRodney W. Grimes /* 2945b81b6b3SRodney W. Grimes * But don't trap while we're probing. 2955b81b6b3SRodney W. Grimes */ 2965b81b6b3SRodney W. Grimes stop_emulating(); 2975b81b6b3SRodney W. Grimes /* 2985b81b6b3SRodney W. Grimes * Finish resetting the coprocessor, if any. If there is an error 2995b81b6b3SRodney W. Grimes * pending, then we may get a bogus IRQ13, but probeintr() will handle 3005b81b6b3SRodney W. Grimes * it OK. Bogus halts have never been observed, but we enabled 3015b81b6b3SRodney W. Grimes * IRQ13 and cleared the BUSY# latch early to handle them anyway. 3025b81b6b3SRodney W. Grimes */ 3035b81b6b3SRodney W. Grimes fninit(); 3043902c3efSSteve Passe 30597bf1787SPeter Wemm #ifdef SMP 3063902c3efSSteve Passe /* 3073902c3efSSteve Passe * Exception 16 MUST work for SMP. 3083902c3efSSteve Passe */ 3093902c3efSSteve Passe npx_irq13 = 0; 3103902c3efSSteve Passe npx_ex16 = hw_float = npx_exists = 1; 3116182fdbdSPeter Wemm device_set_desc(dev, "math processor"); 3126182fdbdSPeter Wemm return (0); 3133902c3efSSteve Passe 3146182fdbdSPeter Wemm #else /* !SMP */ 3156182fdbdSPeter Wemm device_set_desc(dev, "math processor"); 3163902c3efSSteve Passe 3177f3ae831SBruce Evans /* 3187f3ae831SBruce Evans * Don't use fwait here because it might hang. 3197f3ae831SBruce Evans * Don't use fnop here because it usually hangs if there is no FPU. 3207f3ae831SBruce Evans */ 32137e52b59SBruce Evans DELAY(1000); /* wait for any IRQ13 */ 3225b81b6b3SRodney W. Grimes #ifdef DIAGNOSTIC 3235b81b6b3SRodney W. Grimes if (npx_intrs_while_probing != 0) 3245b81b6b3SRodney W. Grimes printf("fninit caused %u bogus npx interrupt(s)\n", 3255b81b6b3SRodney W. Grimes npx_intrs_while_probing); 3265b81b6b3SRodney W. Grimes if (npx_traps_while_probing != 0) 3275b81b6b3SRodney W. Grimes printf("fninit caused %u bogus npx trap(s)\n", 3285b81b6b3SRodney W. Grimes npx_traps_while_probing); 3295b81b6b3SRodney W. Grimes #endif 3305b81b6b3SRodney W. Grimes /* 3315b81b6b3SRodney W. Grimes * Check for a status of mostly zero. 3325b81b6b3SRodney W. Grimes */ 3335b81b6b3SRodney W. Grimes status = 0x5a5a; 3345b81b6b3SRodney W. Grimes fnstsw(&status); 3355b81b6b3SRodney W. Grimes if ((status & 0xb8ff) == 0) { 3365b81b6b3SRodney W. Grimes /* 3375b81b6b3SRodney W. Grimes * Good, now check for a proper control word. 3385b81b6b3SRodney W. Grimes */ 3395b81b6b3SRodney W. Grimes control = 0x5a5a; 3405b81b6b3SRodney W. Grimes fnstcw(&control); 3415b81b6b3SRodney W. Grimes if ((control & 0x1f3f) == 0x033f) { 342501c2393SGarrett Wollman hw_float = npx_exists = 1; 3435b81b6b3SRodney W. Grimes /* 3445b81b6b3SRodney W. Grimes * We have an npx, now divide by 0 to see if exception 3455b81b6b3SRodney W. Grimes * 16 works. 3465b81b6b3SRodney W. Grimes */ 3475b81b6b3SRodney W. Grimes control &= ~(1 << 2); /* enable divide by 0 trap */ 3485b81b6b3SRodney W. Grimes fldcw(&control); 3495b81b6b3SRodney W. Grimes npx_traps_while_probing = npx_intrs_while_probing = 0; 3505b81b6b3SRodney W. Grimes fp_divide_by_0(); 3515b81b6b3SRodney W. Grimes if (npx_traps_while_probing != 0) { 3525b81b6b3SRodney W. Grimes /* 3535b81b6b3SRodney W. Grimes * Good, exception 16 works. 3545b81b6b3SRodney W. Grimes */ 3555b81b6b3SRodney W. Grimes npx_ex16 = 1; 3566182fdbdSPeter Wemm return (0); 3575b81b6b3SRodney W. Grimes } 3585b81b6b3SRodney W. Grimes if (npx_intrs_while_probing != 0) { 3596182fdbdSPeter Wemm int rid; 3606182fdbdSPeter Wemm struct resource *r; 3616182fdbdSPeter Wemm void *intr; 3625b81b6b3SRodney W. Grimes /* 3635b81b6b3SRodney W. Grimes * Bad, we are stuck with IRQ13. 3645b81b6b3SRodney W. Grimes */ 3655b81b6b3SRodney W. Grimes npx_irq13 = 1; 36637e52b59SBruce Evans /* 3672f2ffb8dSPeter Wemm * npxattach would be too late to set npx0_imask 36837e52b59SBruce Evans */ 3692f2ffb8dSPeter Wemm npx0_imask |= (1 << npx_irq); 3706182fdbdSPeter Wemm 3716182fdbdSPeter Wemm /* 3726182fdbdSPeter Wemm * We allocate these resources permanently, 3736182fdbdSPeter Wemm * so there is no need to keep track of them. 3746182fdbdSPeter Wemm */ 3756182fdbdSPeter Wemm rid = 0; 3766182fdbdSPeter Wemm r = bus_alloc_resource(dev, SYS_RES_IOPORT, 3776182fdbdSPeter Wemm &rid, IO_NPX, IO_NPX, 3786182fdbdSPeter Wemm IO_NPXSIZE, RF_ACTIVE); 3796182fdbdSPeter Wemm if (r == 0) 3806182fdbdSPeter Wemm panic("npx: can't get ports"); 3816182fdbdSPeter Wemm rid = 0; 3826182fdbdSPeter Wemm r = bus_alloc_resource(dev, SYS_RES_IRQ, 3832f2ffb8dSPeter Wemm &rid, npx_irq, npx_irq, 3846182fdbdSPeter Wemm 1, RF_ACTIVE); 3856182fdbdSPeter Wemm if (r == 0) 3866182fdbdSPeter Wemm panic("npx: can't get IRQ"); 3876182fdbdSPeter Wemm BUS_SETUP_INTR(device_get_parent(dev), 388566643e3SDoug Rabson dev, r, INTR_TYPE_MISC, 389566643e3SDoug Rabson npx_intr, 0, &intr); 3906182fdbdSPeter Wemm if (intr == 0) 3916182fdbdSPeter Wemm panic("npx: can't create intr"); 3926182fdbdSPeter Wemm 3936182fdbdSPeter Wemm return (0); 3945b81b6b3SRodney W. Grimes } 3955b81b6b3SRodney W. Grimes /* 3965b81b6b3SRodney W. Grimes * Worse, even IRQ13 is broken. Use emulator. 3975b81b6b3SRodney W. Grimes */ 3985b81b6b3SRodney W. Grimes } 3995b81b6b3SRodney W. Grimes } 4005b81b6b3SRodney W. Grimes /* 4015b81b6b3SRodney W. Grimes * Probe failed, but we want to get to npxattach to initialize the 4025b81b6b3SRodney W. Grimes * emulator and say that it has been installed. XXX handle devices 4035b81b6b3SRodney W. Grimes * that aren't really devices better. 4045b81b6b3SRodney W. Grimes */ 4056182fdbdSPeter Wemm return (0); 4063902c3efSSteve Passe #endif /* SMP */ 4075b81b6b3SRodney W. Grimes } 4085b81b6b3SRodney W. Grimes 4095b81b6b3SRodney W. Grimes /* 4105b81b6b3SRodney W. Grimes * Attach routine - announce which it is, and wire into system 4115b81b6b3SRodney W. Grimes */ 4125b81b6b3SRodney W. Grimes int 4136182fdbdSPeter Wemm npx_attach(dev) 4146182fdbdSPeter Wemm device_t dev; 4155b81b6b3SRodney W. Grimes { 4166182fdbdSPeter Wemm int flags; 417a7674320SMartin Cracauer 418a7674320SMartin Cracauer if (resource_int_value("npx", 0, "flags", &flags) != 0) 419a7674320SMartin Cracauer flags = 0; 420fe310de8SBruce Evans 4216182fdbdSPeter Wemm device_print_prettyname(dev); 4226182fdbdSPeter Wemm if (npx_irq13) { 4236182fdbdSPeter Wemm printf("using IRQ 13 interface\n"); 4246182fdbdSPeter Wemm } else { 42537e52b59SBruce Evans #if defined(MATH_EMULATE) || defined(GPL_MATH_EMULATE) 426a7674320SMartin Cracauer if (npx_ex16) { 427a7674320SMartin Cracauer if (!(flags & NPX_PREFER_EMULATOR)) 428a7674320SMartin Cracauer printf("INT 16 interface\n"); 429a7674320SMartin Cracauer else { 430a7674320SMartin Cracauer printf("FPU exists, but flags request " 431a7674320SMartin Cracauer "emulator\n"); 432a7674320SMartin Cracauer hw_float = npx_exists = 0; 433a7674320SMartin Cracauer } 434a7674320SMartin Cracauer } else if (npx_exists) { 4351fe04850SBruce Evans printf("error reporting broken; using 387 emulator\n"); 4361fe04850SBruce Evans hw_float = npx_exists = 0; 43737e52b59SBruce Evans } else 4381fe04850SBruce Evans printf("387 emulator\n"); 43937e52b59SBruce Evans #else 440a7674320SMartin Cracauer if (npx_ex16) { 441a7674320SMartin Cracauer printf("INT 16 interface\n"); 442a7674320SMartin Cracauer if (flags & NPX_PREFER_EMULATOR) { 443a7674320SMartin Cracauer printf("emulator requested, but none compiled " 444a7674320SMartin Cracauer "into kernel, using FPU\n"); 445a7674320SMartin Cracauer } 446a7674320SMartin Cracauer } else 447a7674320SMartin Cracauer printf("no 387 emulator in kernel and no FPU!\n"); 44837e52b59SBruce Evans #endif 4491fe04850SBruce Evans } 4505b81b6b3SRodney W. Grimes npxinit(__INITIAL_NPXCW__); 4511fe04850SBruce Evans 452cd59d49dSBruce Evans #ifdef I586_CPU 453a7674320SMartin Cracauer if (cpu_class == CPUCLASS_586 && npx_ex16 && npx_exists && 454cd59d49dSBruce Evans timezero("i586_bzero()", i586_bzero) < 455cd59d49dSBruce Evans timezero("bzero()", bzero) * 4 / 5) { 4566182fdbdSPeter Wemm if (!(flags & NPX_DISABLE_I586_OPTIMIZED_BCOPY)) { 4571fe04850SBruce Evans bcopy_vector = i586_bcopy; 4581fe04850SBruce Evans ovbcopy_vector = i586_bcopy; 4591fe04850SBruce Evans } 4606182fdbdSPeter Wemm if (!(flags & NPX_DISABLE_I586_OPTIMIZED_BZERO)) 4611fe04850SBruce Evans bzero = i586_bzero; 4626182fdbdSPeter Wemm if (!(flags & NPX_DISABLE_I586_OPTIMIZED_COPYIO)) { 4631fe04850SBruce Evans copyin_vector = i586_copyin; 4641fe04850SBruce Evans copyout_vector = i586_copyout; 4651fe04850SBruce Evans } 4661fe04850SBruce Evans } 4671fe04850SBruce Evans #endif 4681fe04850SBruce Evans 4696182fdbdSPeter Wemm return (0); /* XXX unused */ 4705b81b6b3SRodney W. Grimes } 4715b81b6b3SRodney W. Grimes 4725b81b6b3SRodney W. Grimes /* 4735b81b6b3SRodney W. Grimes * Initialize floating point unit. 4745b81b6b3SRodney W. Grimes */ 4755b81b6b3SRodney W. Grimes void 4765b81b6b3SRodney W. Grimes npxinit(control) 47737e52b59SBruce Evans u_short control; 4785b81b6b3SRodney W. Grimes { 4795b81b6b3SRodney W. Grimes struct save87 dummy; 4805b81b6b3SRodney W. Grimes 4815b81b6b3SRodney W. Grimes if (!npx_exists) 4825b81b6b3SRodney W. Grimes return; 4835b81b6b3SRodney W. Grimes /* 4845b81b6b3SRodney W. Grimes * fninit has the same h/w bugs as fnsave. Use the detoxified 48537e52b59SBruce Evans * fnsave to throw away any junk in the fpu. npxsave() initializes 4865b81b6b3SRodney W. Grimes * the fpu and sets npxproc = NULL as important side effects. 4875b81b6b3SRodney W. Grimes */ 4885b81b6b3SRodney W. Grimes npxsave(&dummy); 4895b81b6b3SRodney W. Grimes stop_emulating(); 4905b81b6b3SRodney W. Grimes fldcw(&control); 4915b81b6b3SRodney W. Grimes if (curpcb != NULL) 4925b81b6b3SRodney W. Grimes fnsave(&curpcb->pcb_savefpu); 4935b81b6b3SRodney W. Grimes start_emulating(); 4945b81b6b3SRodney W. Grimes } 4955b81b6b3SRodney W. Grimes 4965b81b6b3SRodney W. Grimes /* 4975b81b6b3SRodney W. Grimes * Free coprocessor (if we have it). 4985b81b6b3SRodney W. Grimes */ 4995b81b6b3SRodney W. Grimes void 5005b81b6b3SRodney W. Grimes npxexit(p) 5015b81b6b3SRodney W. Grimes struct proc *p; 5025b81b6b3SRodney W. Grimes { 5035b81b6b3SRodney W. Grimes 504663f1485SBruce Evans if (p == npxproc) 505663f1485SBruce Evans npxsave(&curpcb->pcb_savefpu); 50626add149SBruce Evans #ifdef NPX_DEBUG 507663f1485SBruce Evans if (npx_exists) { 508663f1485SBruce Evans u_int masked_exceptions; 509663f1485SBruce Evans 510663f1485SBruce Evans masked_exceptions = curpcb->pcb_savefpu.sv_env.en_cw 511663f1485SBruce Evans & curpcb->pcb_savefpu.sv_env.en_sw & 0x7f; 512663f1485SBruce Evans /* 51326add149SBruce Evans * Log exceptions that would have trapped with the old 51426add149SBruce Evans * control word (overflow, divide by 0, and invalid operand). 515663f1485SBruce Evans */ 516663f1485SBruce Evans if (masked_exceptions & 0x0d) 517663f1485SBruce Evans log(LOG_ERR, 518663f1485SBruce Evans "pid %d (%s) exited with masked floating point exceptions 0x%02x\n", 519663f1485SBruce Evans p->p_pid, p->p_comm, masked_exceptions); 5205b81b6b3SRodney W. Grimes } 52126add149SBruce Evans #endif 5225b81b6b3SRodney W. Grimes } 5235b81b6b3SRodney W. Grimes 5245b81b6b3SRodney W. Grimes /* 525a7674320SMartin Cracauer * The following mechanism is used to ensure that the FPE_... value 526a7674320SMartin Cracauer * that is passed as a trapcode to the signal handler of the user 527a7674320SMartin Cracauer * process does not have more than one bit set. 528a7674320SMartin Cracauer * 529a7674320SMartin Cracauer * Multiple bits may be set if the user process modifies the control 530a7674320SMartin Cracauer * word while a status word bit is already set. While this is a sign 531a7674320SMartin Cracauer * of bad coding, we have no choise than to narrow them down to one 532a7674320SMartin Cracauer * bit, since we must not send a trapcode that is not exactly one of 533a7674320SMartin Cracauer * the FPE_ macros. 534a7674320SMartin Cracauer * 535a7674320SMartin Cracauer * The mechanism has a static table with 127 entries. Each combination 536a7674320SMartin Cracauer * of the 7 FPU status word exception bits directly translates to a 537a7674320SMartin Cracauer * position in this table, where a single FPE_... value is stored. 538a7674320SMartin Cracauer * This FPE_... value stored there is considered the "most important" 539a7674320SMartin Cracauer * of the exception bits and will be sent as the signal code. The 540a7674320SMartin Cracauer * precedence of the bits is based upon Intel Document "Numerical 541a7674320SMartin Cracauer * Applications", Chapter "Special Computational Situations". 542a7674320SMartin Cracauer * 543a7674320SMartin Cracauer * The macro to choose one of these values does these steps: 1) Throw 544a7674320SMartin Cracauer * away status word bits that cannot be masked. 2) Throw away the bits 545a7674320SMartin Cracauer * currently masked in the control word, assuming the user isn't 546a7674320SMartin Cracauer * interested in them anymore. 3) Reinsert status word bit 7 (stack 547a7674320SMartin Cracauer * fault) if it is set, which cannot be masked but must be presered. 548a7674320SMartin Cracauer * 4) Use the remaining bits to point into the trapcode table. 549a7674320SMartin Cracauer * 550a7674320SMartin Cracauer * The 6 maskable bits in order of their preference, as stated in the 551a7674320SMartin Cracauer * above referenced Intel manual: 552a7674320SMartin Cracauer * 1 Invalid operation (FP_X_INV) 553a7674320SMartin Cracauer * 1a Stack underflow 554a7674320SMartin Cracauer * 1b Stack overflow 555a7674320SMartin Cracauer * 1c Operand of unsupported format 556a7674320SMartin Cracauer * 1d SNaN operand. 557a7674320SMartin Cracauer * 2 QNaN operand (not an exception, irrelavant here) 558a7674320SMartin Cracauer * 3 Any other invalid-operation not mentioned above or zero divide 559a7674320SMartin Cracauer * (FP_X_INV, FP_X_DZ) 560a7674320SMartin Cracauer * 4 Denormal operand (FP_X_DNML) 561a7674320SMartin Cracauer * 5 Numeric over/underflow (FP_X_OFL, FP_X_UFL) 562784648c6SMartin Cracauer * 6 Inexact result (FP_X_IMP) 563784648c6SMartin Cracauer */ 564a7674320SMartin Cracauer static char fpetable[128] = { 565a7674320SMartin Cracauer 0, 566a7674320SMartin Cracauer FPE_FLTINV, /* 1 - INV */ 567a7674320SMartin Cracauer FPE_FLTUND, /* 2 - DNML */ 568a7674320SMartin Cracauer FPE_FLTINV, /* 3 - INV | DNML */ 569a7674320SMartin Cracauer FPE_FLTDIV, /* 4 - DZ */ 570a7674320SMartin Cracauer FPE_FLTINV, /* 5 - INV | DZ */ 571a7674320SMartin Cracauer FPE_FLTDIV, /* 6 - DNML | DZ */ 572a7674320SMartin Cracauer FPE_FLTINV, /* 7 - INV | DNML | DZ */ 573a7674320SMartin Cracauer FPE_FLTOVF, /* 8 - OFL */ 574a7674320SMartin Cracauer FPE_FLTINV, /* 9 - INV | OFL */ 575a7674320SMartin Cracauer FPE_FLTUND, /* A - DNML | OFL */ 576a7674320SMartin Cracauer FPE_FLTINV, /* B - INV | DNML | OFL */ 577a7674320SMartin Cracauer FPE_FLTDIV, /* C - DZ | OFL */ 578a7674320SMartin Cracauer FPE_FLTINV, /* D - INV | DZ | OFL */ 579a7674320SMartin Cracauer FPE_FLTDIV, /* E - DNML | DZ | OFL */ 580a7674320SMartin Cracauer FPE_FLTINV, /* F - INV | DNML | DZ | OFL */ 581a7674320SMartin Cracauer FPE_FLTUND, /* 10 - UFL */ 582a7674320SMartin Cracauer FPE_FLTINV, /* 11 - INV | UFL */ 583a7674320SMartin Cracauer FPE_FLTUND, /* 12 - DNML | UFL */ 584a7674320SMartin Cracauer FPE_FLTINV, /* 13 - INV | DNML | UFL */ 585a7674320SMartin Cracauer FPE_FLTDIV, /* 14 - DZ | UFL */ 586a7674320SMartin Cracauer FPE_FLTINV, /* 15 - INV | DZ | UFL */ 587a7674320SMartin Cracauer FPE_FLTDIV, /* 16 - DNML | DZ | UFL */ 588a7674320SMartin Cracauer FPE_FLTINV, /* 17 - INV | DNML | DZ | UFL */ 589a7674320SMartin Cracauer FPE_FLTOVF, /* 18 - OFL | UFL */ 590a7674320SMartin Cracauer FPE_FLTINV, /* 19 - INV | OFL | UFL */ 591a7674320SMartin Cracauer FPE_FLTUND, /* 1A - DNML | OFL | UFL */ 592a7674320SMartin Cracauer FPE_FLTINV, /* 1B - INV | DNML | OFL | UFL */ 593a7674320SMartin Cracauer FPE_FLTDIV, /* 1C - DZ | OFL | UFL */ 594a7674320SMartin Cracauer FPE_FLTINV, /* 1D - INV | DZ | OFL | UFL */ 595a7674320SMartin Cracauer FPE_FLTDIV, /* 1E - DNML | DZ | OFL | UFL */ 596a7674320SMartin Cracauer FPE_FLTINV, /* 1F - INV | DNML | DZ | OFL | UFL */ 597a7674320SMartin Cracauer FPE_FLTRES, /* 20 - IMP */ 598a7674320SMartin Cracauer FPE_FLTINV, /* 21 - INV | IMP */ 599a7674320SMartin Cracauer FPE_FLTUND, /* 22 - DNML | IMP */ 600a7674320SMartin Cracauer FPE_FLTINV, /* 23 - INV | DNML | IMP */ 601a7674320SMartin Cracauer FPE_FLTDIV, /* 24 - DZ | IMP */ 602a7674320SMartin Cracauer FPE_FLTINV, /* 25 - INV | DZ | IMP */ 603a7674320SMartin Cracauer FPE_FLTDIV, /* 26 - DNML | DZ | IMP */ 604a7674320SMartin Cracauer FPE_FLTINV, /* 27 - INV | DNML | DZ | IMP */ 605a7674320SMartin Cracauer FPE_FLTOVF, /* 28 - OFL | IMP */ 606a7674320SMartin Cracauer FPE_FLTINV, /* 29 - INV | OFL | IMP */ 607a7674320SMartin Cracauer FPE_FLTUND, /* 2A - DNML | OFL | IMP */ 608a7674320SMartin Cracauer FPE_FLTINV, /* 2B - INV | DNML | OFL | IMP */ 609a7674320SMartin Cracauer FPE_FLTDIV, /* 2C - DZ | OFL | IMP */ 610a7674320SMartin Cracauer FPE_FLTINV, /* 2D - INV | DZ | OFL | IMP */ 611a7674320SMartin Cracauer FPE_FLTDIV, /* 2E - DNML | DZ | OFL | IMP */ 612a7674320SMartin Cracauer FPE_FLTINV, /* 2F - INV | DNML | DZ | OFL | IMP */ 613a7674320SMartin Cracauer FPE_FLTUND, /* 30 - UFL | IMP */ 614a7674320SMartin Cracauer FPE_FLTINV, /* 31 - INV | UFL | IMP */ 615a7674320SMartin Cracauer FPE_FLTUND, /* 32 - DNML | UFL | IMP */ 616a7674320SMartin Cracauer FPE_FLTINV, /* 33 - INV | DNML | UFL | IMP */ 617a7674320SMartin Cracauer FPE_FLTDIV, /* 34 - DZ | UFL | IMP */ 618a7674320SMartin Cracauer FPE_FLTINV, /* 35 - INV | DZ | UFL | IMP */ 619a7674320SMartin Cracauer FPE_FLTDIV, /* 36 - DNML | DZ | UFL | IMP */ 620a7674320SMartin Cracauer FPE_FLTINV, /* 37 - INV | DNML | DZ | UFL | IMP */ 621a7674320SMartin Cracauer FPE_FLTOVF, /* 38 - OFL | UFL | IMP */ 622a7674320SMartin Cracauer FPE_FLTINV, /* 39 - INV | OFL | UFL | IMP */ 623a7674320SMartin Cracauer FPE_FLTUND, /* 3A - DNML | OFL | UFL | IMP */ 624a7674320SMartin Cracauer FPE_FLTINV, /* 3B - INV | DNML | OFL | UFL | IMP */ 625a7674320SMartin Cracauer FPE_FLTDIV, /* 3C - DZ | OFL | UFL | IMP */ 626a7674320SMartin Cracauer FPE_FLTINV, /* 3D - INV | DZ | OFL | UFL | IMP */ 627a7674320SMartin Cracauer FPE_FLTDIV, /* 3E - DNML | DZ | OFL | UFL | IMP */ 628a7674320SMartin Cracauer FPE_FLTINV, /* 3F - INV | DNML | DZ | OFL | UFL | IMP */ 629a7674320SMartin Cracauer FPE_FLTSUB, /* 40 - STK */ 630a7674320SMartin Cracauer FPE_FLTSUB, /* 41 - INV | STK */ 631a7674320SMartin Cracauer FPE_FLTUND, /* 42 - DNML | STK */ 632a7674320SMartin Cracauer FPE_FLTSUB, /* 43 - INV | DNML | STK */ 633a7674320SMartin Cracauer FPE_FLTDIV, /* 44 - DZ | STK */ 634a7674320SMartin Cracauer FPE_FLTSUB, /* 45 - INV | DZ | STK */ 635a7674320SMartin Cracauer FPE_FLTDIV, /* 46 - DNML | DZ | STK */ 636a7674320SMartin Cracauer FPE_FLTSUB, /* 47 - INV | DNML | DZ | STK */ 637a7674320SMartin Cracauer FPE_FLTOVF, /* 48 - OFL | STK */ 638a7674320SMartin Cracauer FPE_FLTSUB, /* 49 - INV | OFL | STK */ 639a7674320SMartin Cracauer FPE_FLTUND, /* 4A - DNML | OFL | STK */ 640a7674320SMartin Cracauer FPE_FLTSUB, /* 4B - INV | DNML | OFL | STK */ 641a7674320SMartin Cracauer FPE_FLTDIV, /* 4C - DZ | OFL | STK */ 642a7674320SMartin Cracauer FPE_FLTSUB, /* 4D - INV | DZ | OFL | STK */ 643a7674320SMartin Cracauer FPE_FLTDIV, /* 4E - DNML | DZ | OFL | STK */ 644a7674320SMartin Cracauer FPE_FLTSUB, /* 4F - INV | DNML | DZ | OFL | STK */ 645a7674320SMartin Cracauer FPE_FLTUND, /* 50 - UFL | STK */ 646a7674320SMartin Cracauer FPE_FLTSUB, /* 51 - INV | UFL | STK */ 647a7674320SMartin Cracauer FPE_FLTUND, /* 52 - DNML | UFL | STK */ 648a7674320SMartin Cracauer FPE_FLTSUB, /* 53 - INV | DNML | UFL | STK */ 649a7674320SMartin Cracauer FPE_FLTDIV, /* 54 - DZ | UFL | STK */ 650a7674320SMartin Cracauer FPE_FLTSUB, /* 55 - INV | DZ | UFL | STK */ 651a7674320SMartin Cracauer FPE_FLTDIV, /* 56 - DNML | DZ | UFL | STK */ 652a7674320SMartin Cracauer FPE_FLTSUB, /* 57 - INV | DNML | DZ | UFL | STK */ 653a7674320SMartin Cracauer FPE_FLTOVF, /* 58 - OFL | UFL | STK */ 654a7674320SMartin Cracauer FPE_FLTSUB, /* 59 - INV | OFL | UFL | STK */ 655a7674320SMartin Cracauer FPE_FLTUND, /* 5A - DNML | OFL | UFL | STK */ 656a7674320SMartin Cracauer FPE_FLTSUB, /* 5B - INV | DNML | OFL | UFL | STK */ 657a7674320SMartin Cracauer FPE_FLTDIV, /* 5C - DZ | OFL | UFL | STK */ 658a7674320SMartin Cracauer FPE_FLTSUB, /* 5D - INV | DZ | OFL | UFL | STK */ 659a7674320SMartin Cracauer FPE_FLTDIV, /* 5E - DNML | DZ | OFL | UFL | STK */ 660a7674320SMartin Cracauer FPE_FLTSUB, /* 5F - INV | DNML | DZ | OFL | UFL | STK */ 661a7674320SMartin Cracauer FPE_FLTRES, /* 60 - IMP | STK */ 662a7674320SMartin Cracauer FPE_FLTSUB, /* 61 - INV | IMP | STK */ 663a7674320SMartin Cracauer FPE_FLTUND, /* 62 - DNML | IMP | STK */ 664a7674320SMartin Cracauer FPE_FLTSUB, /* 63 - INV | DNML | IMP | STK */ 665a7674320SMartin Cracauer FPE_FLTDIV, /* 64 - DZ | IMP | STK */ 666a7674320SMartin Cracauer FPE_FLTSUB, /* 65 - INV | DZ | IMP | STK */ 667a7674320SMartin Cracauer FPE_FLTDIV, /* 66 - DNML | DZ | IMP | STK */ 668a7674320SMartin Cracauer FPE_FLTSUB, /* 67 - INV | DNML | DZ | IMP | STK */ 669a7674320SMartin Cracauer FPE_FLTOVF, /* 68 - OFL | IMP | STK */ 670a7674320SMartin Cracauer FPE_FLTSUB, /* 69 - INV | OFL | IMP | STK */ 671a7674320SMartin Cracauer FPE_FLTUND, /* 6A - DNML | OFL | IMP | STK */ 672a7674320SMartin Cracauer FPE_FLTSUB, /* 6B - INV | DNML | OFL | IMP | STK */ 673a7674320SMartin Cracauer FPE_FLTDIV, /* 6C - DZ | OFL | IMP | STK */ 674a7674320SMartin Cracauer FPE_FLTSUB, /* 6D - INV | DZ | OFL | IMP | STK */ 675a7674320SMartin Cracauer FPE_FLTDIV, /* 6E - DNML | DZ | OFL | IMP | STK */ 676a7674320SMartin Cracauer FPE_FLTSUB, /* 6F - INV | DNML | DZ | OFL | IMP | STK */ 677a7674320SMartin Cracauer FPE_FLTUND, /* 70 - UFL | IMP | STK */ 678a7674320SMartin Cracauer FPE_FLTSUB, /* 71 - INV | UFL | IMP | STK */ 679a7674320SMartin Cracauer FPE_FLTUND, /* 72 - DNML | UFL | IMP | STK */ 680a7674320SMartin Cracauer FPE_FLTSUB, /* 73 - INV | DNML | UFL | IMP | STK */ 681a7674320SMartin Cracauer FPE_FLTDIV, /* 74 - DZ | UFL | IMP | STK */ 682a7674320SMartin Cracauer FPE_FLTSUB, /* 75 - INV | DZ | UFL | IMP | STK */ 683a7674320SMartin Cracauer FPE_FLTDIV, /* 76 - DNML | DZ | UFL | IMP | STK */ 684a7674320SMartin Cracauer FPE_FLTSUB, /* 77 - INV | DNML | DZ | UFL | IMP | STK */ 685a7674320SMartin Cracauer FPE_FLTOVF, /* 78 - OFL | UFL | IMP | STK */ 686a7674320SMartin Cracauer FPE_FLTSUB, /* 79 - INV | OFL | UFL | IMP | STK */ 687a7674320SMartin Cracauer FPE_FLTUND, /* 7A - DNML | OFL | UFL | IMP | STK */ 688a7674320SMartin Cracauer FPE_FLTSUB, /* 7B - INV | DNML | OFL | UFL | IMP | STK */ 689a7674320SMartin Cracauer FPE_FLTDIV, /* 7C - DZ | OFL | UFL | IMP | STK */ 690a7674320SMartin Cracauer FPE_FLTSUB, /* 7D - INV | DZ | OFL | UFL | IMP | STK */ 691a7674320SMartin Cracauer FPE_FLTDIV, /* 7E - DNML | DZ | OFL | UFL | IMP | STK */ 692a7674320SMartin Cracauer FPE_FLTSUB, /* 7F - INV | DNML | DZ | OFL | UFL | IMP | STK */ 693a7674320SMartin Cracauer }; 694a7674320SMartin Cracauer 695a7674320SMartin Cracauer /* 69637e52b59SBruce Evans * Preserve the FP status word, clear FP exceptions, then generate a SIGFPE. 6975b81b6b3SRodney W. Grimes * 69837e52b59SBruce Evans * Clearing exceptions is necessary mainly to avoid IRQ13 bugs. We now 69937e52b59SBruce Evans * depend on longjmp() restoring a usable state. Restoring the state 70037e52b59SBruce Evans * or examining it might fail if we didn't clear exceptions. 7015b81b6b3SRodney W. Grimes * 702a7674320SMartin Cracauer * The error code chosen will be one of the FPE_... macros. It will be 703a7674320SMartin Cracauer * sent as the second argument to old BSD-style signal handlers and as 704a7674320SMartin Cracauer * "siginfo_t->si_code" (second argument) to SA_SIGINFO signal handlers. 70537e52b59SBruce Evans * 70637e52b59SBruce Evans * XXX the FP state is not preserved across signal handlers. So signal 70737e52b59SBruce Evans * handlers cannot afford to do FP unless they preserve the state or 70837e52b59SBruce Evans * longjmp() out. Both preserving the state and longjmp()ing may be 70937e52b59SBruce Evans * destroyed by IRQ13 bugs. Clearing FP exceptions is not an acceptable 71037e52b59SBruce Evans * solution for signals other than SIGFPE. 7115b81b6b3SRodney W. Grimes */ 7125b81b6b3SRodney W. Grimes void 7136182fdbdSPeter Wemm npx_intr(dummy) 7146182fdbdSPeter Wemm void *dummy; 7155b81b6b3SRodney W. Grimes { 7165b81b6b3SRodney W. Grimes int code; 717784648c6SMartin Cracauer u_short control; 7182e69f359SBruce Evans struct intrframe *frame; 7195b81b6b3SRodney W. Grimes 7205b81b6b3SRodney W. Grimes if (npxproc == NULL || !npx_exists) { 72137e52b59SBruce Evans printf("npxintr: npxproc = %p, curproc = %p, npx_exists = %d\n", 72237e52b59SBruce Evans npxproc, curproc, npx_exists); 7235b81b6b3SRodney W. Grimes panic("npxintr from nowhere"); 7245b81b6b3SRodney W. Grimes } 7255b81b6b3SRodney W. Grimes if (npxproc != curproc) { 72637e52b59SBruce Evans printf("npxintr: npxproc = %p, curproc = %p, npx_exists = %d\n", 72737e52b59SBruce Evans npxproc, curproc, npx_exists); 7285b81b6b3SRodney W. Grimes panic("npxintr from non-current process"); 7295b81b6b3SRodney W. Grimes } 73037e52b59SBruce Evans 731390784fbSBruce Evans outb(0xf0, 0); 73237e52b59SBruce Evans fnstsw(&curpcb->pcb_savefpu.sv_ex_sw); 733784648c6SMartin Cracauer fnstcw(&control); 73437e52b59SBruce Evans fnclex(); 7355b81b6b3SRodney W. Grimes 7365b81b6b3SRodney W. Grimes /* 7375b81b6b3SRodney W. Grimes * Pass exception to process. 7385b81b6b3SRodney W. Grimes */ 7396182fdbdSPeter Wemm frame = (struct intrframe *)&dummy; /* XXX */ 74040d50994SPhilippe Charnier if ((ISPL(frame->if_cs) == SEL_UPL) || (frame->if_eflags & PSL_VM)) { 7415b81b6b3SRodney W. Grimes /* 7425b81b6b3SRodney W. Grimes * Interrupt is essentially a trap, so we can afford to call 7435b81b6b3SRodney W. Grimes * the SIGFPE handler (if any) as soon as the interrupt 7445b81b6b3SRodney W. Grimes * returns. 7455b81b6b3SRodney W. Grimes * 7465b81b6b3SRodney W. Grimes * XXX little or nothing is gained from this, and plenty is 7475b81b6b3SRodney W. Grimes * lost - the interrupt frame has to contain the trap frame 7485b81b6b3SRodney W. Grimes * (this is otherwise only necessary for the rescheduling trap 7495b81b6b3SRodney W. Grimes * in doreti, and the frame for that could easily be set up 7505b81b6b3SRodney W. Grimes * just before it is used). 7515b81b6b3SRodney W. Grimes */ 752cd121c9cSLuoqi Chen curproc->p_md.md_regs = INTR_TO_TRAPFRAME(frame); 7535b81b6b3SRodney W. Grimes /* 7545b81b6b3SRodney W. Grimes * Encode the appropriate code for detailed information on 7555b81b6b3SRodney W. Grimes * this exception. 7565b81b6b3SRodney W. Grimes */ 757784648c6SMartin Cracauer code = 758784648c6SMartin Cracauer fpetable[(curpcb->pcb_savefpu.sv_ex_sw & ~control & 0x3f) | 759784648c6SMartin Cracauer (curpcb->pcb_savefpu.sv_ex_sw & 0x40)]; 7605b81b6b3SRodney W. Grimes trapsignal(curproc, SIGFPE, code); 7615b81b6b3SRodney W. Grimes } else { 7625b81b6b3SRodney W. Grimes /* 7635b81b6b3SRodney W. Grimes * Nested interrupt. These losers occur when: 7645b81b6b3SRodney W. Grimes * o an IRQ13 is bogusly generated at a bogus time, e.g.: 7655b81b6b3SRodney W. Grimes * o immediately after an fnsave or frstor of an 7665b81b6b3SRodney W. Grimes * error state. 7675b81b6b3SRodney W. Grimes * o a couple of 386 instructions after 7685b81b6b3SRodney W. Grimes * "fstpl _memvar" causes a stack overflow. 7695b81b6b3SRodney W. Grimes * These are especially nasty when combined with a 7705b81b6b3SRodney W. Grimes * trace trap. 7715b81b6b3SRodney W. Grimes * o an IRQ13 occurs at the same time as another higher- 7725b81b6b3SRodney W. Grimes * priority interrupt. 7735b81b6b3SRodney W. Grimes * 7745b81b6b3SRodney W. Grimes * Treat them like a true async interrupt. 7755b81b6b3SRodney W. Grimes */ 77637e52b59SBruce Evans psignal(curproc, SIGFPE); 7775b81b6b3SRodney W. Grimes } 7785b81b6b3SRodney W. Grimes } 7795b81b6b3SRodney W. Grimes 7805b81b6b3SRodney W. Grimes /* 7815b81b6b3SRodney W. Grimes * Implement device not available (DNA) exception 7825b81b6b3SRodney W. Grimes * 78337e52b59SBruce Evans * It would be better to switch FP context here (if curproc != npxproc) 78437e52b59SBruce Evans * and not necessarily for every context switch, but it is too hard to 78537e52b59SBruce Evans * access foreign pcb's. 7865b81b6b3SRodney W. Grimes */ 7875b81b6b3SRodney W. Grimes int 7885b81b6b3SRodney W. Grimes npxdna() 7895b81b6b3SRodney W. Grimes { 7905b81b6b3SRodney W. Grimes if (!npx_exists) 7915b81b6b3SRodney W. Grimes return (0); 7925b81b6b3SRodney W. Grimes if (npxproc != NULL) { 79337e52b59SBruce Evans printf("npxdna: npxproc = %p, curproc = %p\n", 79437e52b59SBruce Evans npxproc, curproc); 7955b81b6b3SRodney W. Grimes panic("npxdna"); 7965b81b6b3SRodney W. Grimes } 7975b81b6b3SRodney W. Grimes stop_emulating(); 7985b81b6b3SRodney W. Grimes /* 7995b81b6b3SRodney W. Grimes * Record new context early in case frstor causes an IRQ13. 8005b81b6b3SRodney W. Grimes */ 8015b81b6b3SRodney W. Grimes npxproc = curproc; 80237e52b59SBruce Evans curpcb->pcb_savefpu.sv_ex_sw = 0; 8035b81b6b3SRodney W. Grimes /* 8045b81b6b3SRodney W. Grimes * The following frstor may cause an IRQ13 when the state being 8055b81b6b3SRodney W. Grimes * restored has a pending error. The error will appear to have been 8065b81b6b3SRodney W. Grimes * triggered by the current (npx) user instruction even when that 8075b81b6b3SRodney W. Grimes * instruction is a no-wait instruction that should not trigger an 8085b81b6b3SRodney W. Grimes * error (e.g., fnclex). On at least one 486 system all of the 8095b81b6b3SRodney W. Grimes * no-wait instructions are broken the same as frstor, so our 8105b81b6b3SRodney W. Grimes * treatment does not amplify the breakage. On at least one 8115b81b6b3SRodney W. Grimes * 386/Cyrix 387 system, fnclex works correctly while frstor and 8125b81b6b3SRodney W. Grimes * fnsave are broken, so our treatment breaks fnclex if it is the 8135b81b6b3SRodney W. Grimes * first FPU instruction after a context switch. 8145b81b6b3SRodney W. Grimes */ 8155b81b6b3SRodney W. Grimes frstor(&curpcb->pcb_savefpu); 8165b81b6b3SRodney W. Grimes 8175b81b6b3SRodney W. Grimes return (1); 8185b81b6b3SRodney W. Grimes } 8195b81b6b3SRodney W. Grimes 8205b81b6b3SRodney W. Grimes /* 8215b81b6b3SRodney W. Grimes * Wrapper for fnsave instruction to handle h/w bugs. If there is an error 8225b81b6b3SRodney W. Grimes * pending, then fnsave generates a bogus IRQ13 on some systems. Force 8235b81b6b3SRodney W. Grimes * any IRQ13 to be handled immediately, and then ignore it. This routine is 8245b81b6b3SRodney W. Grimes * often called at splhigh so it must not use many system services. In 8255b81b6b3SRodney W. Grimes * particular, it's much easier to install a special handler than to 8265b81b6b3SRodney W. Grimes * guarantee that it's safe to use npxintr() and its supporting code. 8275b81b6b3SRodney W. Grimes */ 8285b81b6b3SRodney W. Grimes void 8295b81b6b3SRodney W. Grimes npxsave(addr) 8305b81b6b3SRodney W. Grimes struct save87 *addr; 8315b81b6b3SRodney W. Grimes { 8323902c3efSSteve Passe #ifdef SMP 8333902c3efSSteve Passe 8343902c3efSSteve Passe stop_emulating(); 8353902c3efSSteve Passe fnsave(addr); 8363902c3efSSteve Passe /* fnop(); */ 8373902c3efSSteve Passe start_emulating(); 8383902c3efSSteve Passe npxproc = NULL; 8393902c3efSSteve Passe 8403902c3efSSteve Passe #else /* SMP */ 8413902c3efSSteve Passe 8425b81b6b3SRodney W. Grimes u_char icu1_mask; 8435b81b6b3SRodney W. Grimes u_char icu2_mask; 8445b81b6b3SRodney W. Grimes u_char old_icu1_mask; 8455b81b6b3SRodney W. Grimes u_char old_icu2_mask; 8465b81b6b3SRodney W. Grimes struct gate_descriptor save_idt_npxintr; 8475b81b6b3SRodney W. Grimes 8485b81b6b3SRodney W. Grimes disable_intr(); 8495b81b6b3SRodney W. Grimes old_icu1_mask = inb(IO_ICU1 + 1); 8505b81b6b3SRodney W. Grimes old_icu2_mask = inb(IO_ICU2 + 1); 8515b81b6b3SRodney W. Grimes save_idt_npxintr = idt[npx_intrno]; 852d2306226SDavid Greenman outb(IO_ICU1 + 1, old_icu1_mask & ~(IRQ_SLAVE | npx0_imask)); 853d2306226SDavid Greenman outb(IO_ICU2 + 1, old_icu2_mask & ~(npx0_imask >> 8)); 8545b81b6b3SRodney W. Grimes idt[npx_intrno] = npx_idt_probeintr; 8555b81b6b3SRodney W. Grimes enable_intr(); 8565b81b6b3SRodney W. Grimes stop_emulating(); 8575b81b6b3SRodney W. Grimes fnsave(addr); 85837e52b59SBruce Evans fnop(); 8595b81b6b3SRodney W. Grimes start_emulating(); 8605b81b6b3SRodney W. Grimes npxproc = NULL; 8615b81b6b3SRodney W. Grimes disable_intr(); 8625b81b6b3SRodney W. Grimes icu1_mask = inb(IO_ICU1 + 1); /* masks may have changed */ 8635b81b6b3SRodney W. Grimes icu2_mask = inb(IO_ICU2 + 1); 8645b81b6b3SRodney W. Grimes outb(IO_ICU1 + 1, 865d2306226SDavid Greenman (icu1_mask & ~npx0_imask) | (old_icu1_mask & npx0_imask)); 8665b81b6b3SRodney W. Grimes outb(IO_ICU2 + 1, 867d2306226SDavid Greenman (icu2_mask & ~(npx0_imask >> 8)) 868d2306226SDavid Greenman | (old_icu2_mask & (npx0_imask >> 8))); 8695b81b6b3SRodney W. Grimes idt[npx_intrno] = save_idt_npxintr; 8705b81b6b3SRodney W. Grimes enable_intr(); /* back to usual state */ 8713902c3efSSteve Passe 8723902c3efSSteve Passe #endif /* SMP */ 8735b81b6b3SRodney W. Grimes } 8745b81b6b3SRodney W. Grimes 875cd59d49dSBruce Evans #ifdef I586_CPU 876cd59d49dSBruce Evans static long 877cd59d49dSBruce Evans timezero(funcname, func) 878cd59d49dSBruce Evans const char *funcname; 879cd59d49dSBruce Evans void (*func) __P((void *buf, size_t len)); 880cd59d49dSBruce Evans 881cd59d49dSBruce Evans { 882cd59d49dSBruce Evans void *buf; 883cd59d49dSBruce Evans #define BUFSIZE 1000000 884cd59d49dSBruce Evans long usec; 885cd59d49dSBruce Evans struct timeval finish, start; 886cd59d49dSBruce Evans 887cd59d49dSBruce Evans buf = malloc(BUFSIZE, M_TEMP, M_NOWAIT); 888cd59d49dSBruce Evans if (buf == NULL) 889cd59d49dSBruce Evans return (BUFSIZE); 890cd59d49dSBruce Evans microtime(&start); 891cd59d49dSBruce Evans (*func)(buf, BUFSIZE); 892cd59d49dSBruce Evans microtime(&finish); 893cd59d49dSBruce Evans usec = 1000000 * (finish.tv_sec - start.tv_sec) + 894cd59d49dSBruce Evans finish.tv_usec - start.tv_usec; 895cd59d49dSBruce Evans if (usec <= 0) 896cd59d49dSBruce Evans usec = 1; 897cd59d49dSBruce Evans if (bootverbose) 898cd59d49dSBruce Evans printf("%s bandwidth = %ld bytes/sec\n", 89916967563SBruce Evans funcname, (long)(BUFSIZE * (int64_t)1000000 / usec)); 900cd59d49dSBruce Evans free(buf, M_TEMP); 901cd59d49dSBruce Evans return (usec); 902cd59d49dSBruce Evans } 903cd59d49dSBruce Evans #endif /* I586_CPU */ 904cd59d49dSBruce Evans 9056182fdbdSPeter Wemm static device_method_t npx_methods[] = { 9066182fdbdSPeter Wemm /* Device interface */ 907da4113b3SPeter Wemm DEVMETHOD(device_identify, npx_identify), 9086182fdbdSPeter Wemm DEVMETHOD(device_probe, npx_probe), 9096182fdbdSPeter Wemm DEVMETHOD(device_attach, npx_attach), 9106182fdbdSPeter Wemm DEVMETHOD(device_detach, bus_generic_detach), 9116182fdbdSPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 9126182fdbdSPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 9136182fdbdSPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 9146182fdbdSPeter Wemm 9156182fdbdSPeter Wemm { 0, 0 } 9166182fdbdSPeter Wemm }; 9176182fdbdSPeter Wemm 9186182fdbdSPeter Wemm static driver_t npx_driver = { 9196182fdbdSPeter Wemm "npx", 9206182fdbdSPeter Wemm npx_methods, 9216182fdbdSPeter Wemm 1, /* no softc */ 9226182fdbdSPeter Wemm }; 9236182fdbdSPeter Wemm 9246182fdbdSPeter Wemm static devclass_t npx_devclass; 9256182fdbdSPeter Wemm 9266182fdbdSPeter Wemm /* 9276182fdbdSPeter Wemm * We prefer to attach to the root nexus so that the usual case (exception 16) 9286182fdbdSPeter Wemm * doesn't describe the processor as being `on isa'. 9296182fdbdSPeter Wemm */ 9306182fdbdSPeter Wemm DRIVER_MODULE(npx, nexus, npx_driver, npx_devclass, 0, 0); 9316182fdbdSPeter Wemm 9325b81b6b3SRodney W. Grimes #endif /* NNPX > 0 */ 933