head	1.4;
access;
symbols
	version39-41:1.3;
locks;
comment	@ *  @;


1.4
date	92.08.09.20.52.38;	author amiga;	state Exp;
branches;
next	1.3;

1.3
date	92.05.22.01.43.46;	author mwild;	state Exp;
branches;
next	1.2;

1.2
date	92.05.18.00.49.07;	author mwild;	state Exp;
branches;
next	1.1;

1.1
date	92.05.17.21.25.55;	author mwild;	state Exp;
branches;
next	;


desc
@library initialization
@


1.4
log
@change to new way of remembering open libraries
get prepared to deal with ttys
@
text
@/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id: ix_init.c,v 1.3 1992/05/22 01:43:46 mwild Exp $
 *
 *  $Log: ix_init.c,v $
 *  Revision 1.3  1992/05/22  01:43:46  mwild
 *  initialize buddy allocator
 *
 * Revision 1.2  1992/05/18  00:49:07  mwild
 * changed async mp to be global
 *
 * Revision 1.1  1992/05/14  19:55:40  mwild
 * Initial revision
 *
 */

#define KERNEL
#include "ixemul.h"

#include <exec/memory.h>

/* #undef DEBUG */

#ifdef DEBUG
#define DP(a) kprintf a
#else
#define DP(a)
#endif

/* not changed after the library is initialized */
struct ixemul_base *ixemulbase = 0;
int _dos20;

/* used by gnulib.. I was too lazy to convert that library as well ;-) */
void *MathIeeeDoubBasBase, *MathIeeeDoubTransBase,
     *MathIeeeSingBasBase;

struct ExecBase *SysBase;

/* global port for asynchronous packet notification */
struct MsgPort *ix_async_mp;
extern int mp_interrupt ();

/* global port for tty handling */
struct MsgPort *ix_tty_mp;
extern int tty_interrupt ();

/* since gcc can now statically initialize unions, they're quite useful;-) */
union lib {
  char *name;
  struct Library *base;
};

void
open_libraries (union lib *libs)
{
  SysBase = *(struct ExecBase **)4;

  /* on input `libs' contains a pointer to the library name, on output it
     contains the library base pointer */
  do
    libs->base = OpenLibrary (libs->name, 0) ? : (struct Library *) -1;
  while ((++libs)->name);
}

void
close_libraries (union lib *libs)
{
  do
    if (libs->base != (struct Library *) -1)
      CloseLibrary (libs->base);
  while ((++libs)->base);
}

/* these are the libraries we are potentially interested in. Not finding
   a certain library doesn't mean we can't continue. */
static union lib ix_libs[] = {
  "dos.library",
  "arp.library",
  "intuition.library",
  "graphics.library",
  "mathieeesingbas.library",
  "mathieeedoubbas.library",
  "mathieeedoubtrans.library",
  0,
};
enum { DOS_LIB=0, ARP_LIB, INTUI_LIB, GFX_LIB, SINGB_LIB, DOUBB_LIB, DOUBT_LIB };


struct ixemul_base *
ix_init (struct ixemul_base *ixbase)
{
  int i;

  DP (("ix_init1, ix_init @@$%lx\n", ix_init));
  ixemulbase = ixbase;
  open_libraries (ix_libs);

  /* these ones are necessary and are very (!) unlikely to be missing */
  if (   ! (ixbase->ix_dos_base = ix_libs[DOS_LIB].base)
      || ! (ixbase->ix_intui_base = (struct IntuitionBase *) ix_libs[INTUI_LIB].base)
      || ! (ixbase->ix_gfx_base = (struct GfxBase *) ix_libs[GFX_LIB].base))
    {
      close_libraries (ix_libs);
      return 0;
    }

  DP(("ix_init2\n"));
  _dos20 = ixbase->ix_dos_base->lib_Version >= 36;

  /* arp is only needed for 1.3, but there it's vital */
  if (! (ixbase->ix_arp_base = (struct ArpBase *) ix_libs[ARP_LIB].base) && ! _dos20)
    {
      /* but in that case we have intuition open, and can display a requester
         complaining in more detail than just failing */
      ix_panic ("arp.library required below kick 2.0");
      close_libraries (ix_libs);
      return 0;
    }

  DP(("ix_init3\n"));
  /* those are more or less optional, and if not available cause 
     ix_patch_functions() to replace functions that need them by aborting
     functions */
  ixbase->ix_ms_base = 
    MathIeeeSingBasBase = ix_libs[SINGB_LIB].base;
  ixbase->ix_md_base =
    MathIeeeDoubBasBase = ix_libs[DOUBB_LIB].base;
  ixbase->ix_mdt_base =
    MathIeeeDoubTransBase = ix_libs[DOUBT_LIB].base;

  ixbase->ix_file_tab = (struct file *)
    AllocMem (NOFILE * sizeof(struct file), MEMF_PUBLIC | MEMF_CLEAR);
  ixbase->ix_fileNFILE = ixbase->ix_file_tab + NOFILE;
  ixbase->ix_lastf = ixbase->ix_file_tab;
  ixbase->ix_red_zone_size = 0; /* not enabled by default */
  ixbase->ix_membuf_limit = 0;
  ixbase->ix_fs_buf_factor = 64;
  ixbase->ix_watch_stack = 0;
  ixbase->ix_translate_dots = 1;
  ixbase->ix_translate_slash = 1;
  ixbase->ix_translate_symlinks = 0;
  ixbase->ix_force_translation = 0;
  ixbase->ix_ignore_global_env = 0;

  DP(("ix_init4\n"));
  /* initialize the list structures for the allocator */
  init_buddy ();

  DP(("ix_init5\n"));
  /* patch our library to optimally adapt to the given hardware */
  ix_patch_functions (ixbase);

  DP(("ix_init6\n"));
  ix_async_mp = (struct MsgPort *) CreateInterruptPort (0, 0, mp_interrupt, 0);
  ix_tty_mp = 0; /* (struct MsgPort *) CreateInterruptPort (0, 0, tty_interrupt, 0);*/

  if (ixbase->ix_file_tab && ix_async_mp /* && ix_tty_mp */)
    {
      InitSemaphore (& ixbase->ix_semaph);

      configure_context_switch ();

      NewList ((struct List *) &ixbase->ix_socket_list);

      for (i = 0; i < IX_NUM_SLEEP_QUEUES; i++)
        NewList ((struct List *) &ixbase->ix_sleep_queues[i]);

      /* pass the array over to ix_expunge () */
      ixbase->ix_libs = ix_libs;

      return ixbase;
    }
        
  if (ix_async_mp)
    DeleteInterruptPort (ix_async_mp);

  if (ix_tty_mp)
    DeleteInterruptPort (ix_tty_mp);

  if (ixbase->ix_file_tab)
    FreeMem (ixbase->ix_file_tab, NOFILE * sizeof(struct file));
  else
    ix_panic ("out of memory");

  close_libraries (ix_libs);
  
  return 0;
}      


void
ix_lock_base ()
{
  u_save.u_oldmask = u_save.p_sigmask;
  u_save.p_sigmask = ~0;
  ObtainSemaphore (& ix.ix_semaph);
}

void
ix_unlock_base ()
{
  ReleaseSemaphore (& ix.ix_semaph);
  u_save.p_sigmask = u_save.u_oldmask;
}
@


1.3
log
@initialize buddy allocator
@
text
@d19 1
a19 1
 *  $Id: ix_init.c,v 1.2 1992/05/18 00:49:07 mwild Exp $
d22 3
a34 1
#include <libraries/arpbase.h>
d38 1
a38 1
#undef DEBUG
d52 3
a54 1
     *MathIeeeSingBasBase, *MathIeeeSingTransBase;
d58 46
a103 1
extern int mp_interrupt();
d105 1
d111 37
d152 1
a152 1
  ixbase->ix_red_zone_size = 0;	/* not enabled by default */
d160 1
a160 2

  ixemulbase = ixbase;
d162 1
d166 1
d170 1
d172 1
d174 1
a174 1
  if (ixbase->ix_file_tab && ix_async_mp)
d176 13
a188 77
      ixbase->ix_arp_base = 
        (struct ArpBase *) OpenLibrary (ArpName, ArpVersion);
      
      if (ixbase->ix_arp_base)
        {
      	  /* Arpbase is so kind to provide us with 3 free further libraries ;-) */
          ixbase->ix_dos_base = 
	    (struct DOSBase *) ixbase->ix_arp_base->DosBase;
	    
	  _dos20 = ((struct Library *)ixbase->ix_dos_base)->lib_Version >= 36;

	  ixbase->ix_intui_base =
	    (struct IntuitionBase *) ixbase->ix_arp_base->IntuiBase;
	    
	  ixbase->ix_gfx_base =
	    (struct GfxBase *) ixbase->ix_arp_base->GfxBase;
	    
	  /* now go for the math-libraries */
	  ixbase->ix_ms_base = (struct MathIeeeSingBasBase *) 
            OpenLibrary ("mathieeesingbas.library", 0);

	  if (ixbase->ix_ms_base)
	    {
	      ixbase->ix_mst_base = (struct MathIeeeSingTransBase *)
	        OpenLibrary ("mathieeesingtrans.library", 0);
	        
	      if (ixbase->ix_mst_base)
	        {
	          ixbase->ix_md_base = (struct MathIeeeDoubBasBase *)
	            OpenLibrary ("mathieeedoubbas.library", 0);

		  if (ixbase->ix_md_base)
		    {
		      ixbase->ix_mdt_base = (struct MathIeeeDoubTransBase *)
		        OpenLibrary ("mathieeedoubtrans.library", 0);

		      MathIeeeDoubBasBase = ixbase->ix_md_base;
		      MathIeeeSingBasBase = ixbase->ix_ms_base;
		      MathIeeeDoubTransBase = ixbase->ix_mdt_base;
		      MathIeeeSingTransBase = ixbase->ix_mst_base;

		      InitSemaphore (& ixbase->ix_semaph);

		      configure_context_switch ();

		      NewList ((struct List *) &ixbase->ix_socket_list);

		      for (i = 0; i < IX_NUM_SLEEP_QUEUES; i++)
		        NewList ((struct List *) &ixbase->ix_sleep_queues[i]);

		      if (ixbase->ix_mdt_base)
			return ixbase;

		      ix_panic ("can't open `mathieeedoubtrans.library'");

		      /* else we have to backup what we obtained */
		      CloseLibrary (ixbase->ix_md_base);
		    }
		  else
		    ix_panic ("can't open `mathieeedoubbas.library'");
		  
		  CloseLibrary (ixbase->ix_mst_base);
		}
	      else
	      	ix_panic ("can't open `mathieeesingtrans.library'");
		
	      CloseLibrary (ixbase->ix_ms_base);
	    }
	  else
	    ix_panic ("can't open `mathieeesingbas.library'");
	   
	  CloseLibrary (ixbase->ix_arp_base);
	}
      else
        ix_panic ("can't open `arp.library'");
	
      FreeMem (ixbase->ix_file_tab, NOFILE * sizeof(struct file));
d190 9
d202 1
a202 5
  if (ixbase->ix_file_tab)
    FreeMem (ixbase->ix_file_tab, NOFILE * sizeof (struct file));

  if (ix_async_mp)
    DeleteInterruptPort (ix_async_mp);
d211 2
a212 2
  u.u_oldmask = u.p_sigmask;
  u.p_sigmask = ~0;
d220 1
a220 1
  u.p_sigmask = u.u_oldmask;
@


1.2
log
@changed async mp to be global
@
text
@d19 1
a19 1
 *  $Id: ix_init.c,v 1.1 1992/05/14 19:55:40 mwild Exp $
d22 3
d75 3
@


1.1
log
@Initial revision
@
text
@d18 7
d49 4
d76 3
a78 1
  if (ixbase->ix_file_tab)
d160 6
a176 1
  u.p_sigmask = u.p_sigmask;
@
