+// Count of how many outstanding hs_init()s there have been.
+static int hs_init_count = 0;
+
+// Here we save the terminal settings on the standard file
+// descriptors, if we need to change them (eg. to support NoBuffering
+// input).
+static void *saved_termios[3] = {NULL,NULL,NULL};
+
+void*
+__hscore_get_saved_termios(int fd)
+{
+ return (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
+ saved_termios[fd] : NULL;
+}
+
+void
+__hscore_set_saved_termios(int fd, void* ts)
+{
+ if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
+ saved_termios[fd] = ts;
+ }
+}
+
+/* -----------------------------------------------------------------------------
+ Initialise floating point unit on x86 (currently disabled. why?)
+ (see comment in ghc/compiler/nativeGen/MachInstrs.lhs).
+ -------------------------------------------------------------------------- */
+
+#define X86_INIT_FPU 0
+
+#if X86_INIT_FPU
+static void
+x86_init_fpu ( void )
+{
+ __volatile unsigned short int fpu_cw;
+
+ // Grab the control word
+ __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
+
+#if 0
+ printf("fpu_cw: %x\n", fpu_cw);
+#endif
+
+ // Set bits 8-9 to 10 (64-bit precision).
+ fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
+
+ // Store the new control word back
+ __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
+}
+#endif
+
+/* -----------------------------------------------------------------------------
+ Starting up the RTS
+ -------------------------------------------------------------------------- */
+
+void
+hs_init(int *argc, char **argv[])
+{
+ hs_init_count++;
+ if (hs_init_count > 1) {
+ // second and subsequent inits are ignored
+ return;
+ }
+