/*static char *SCCSID = "@(#)fixinmem.c 6.1 90/11/18";*/
/*static char *SCCSID = "@(#)fixinmem.c 13.10 90/04/28";*/
#define INIT2 INIT2
#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsd.h>
#include <fsh.h>
#include <pvwxport.h>

#include "const.h"
#include "chain.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "assert.h"
#include "pvw.h"

#include "pr_fsini.h"
#include "pr_debug.h"
#include "pr_misc.h"

#include "log.h"
#include "lprintf.h"
#include "message.h"    /* for _TXT_MSG_RESPONSE_DATA */

extern MESSAGE _TXT_MSG_RESPONSE_DATA;

extern far _TEXT_START(),  far _TEXT_END() ;
extern far ASWAP0_START(), far ASWAP0_END();
extern far ASWAP1_START(), far ASWAP1_END();
extern far ASWAP2_START(), far ASWAP2_END();
extern far _BTXT_START(),  far _BTXT_END() ;
extern far BSWAP0_START(), far BSWAP0_END();
extern far BSWAP1_START(), far BSWAP1_END();
extern far BSWAP2_START(), far BSWAP2_END();

#define InitSwapMod(swap,pModA,pEndA)                               \
{                                                                   \
    PVOID pModAddress = (PVOID)(ULONG)(pModA);                      \
    PVOID pEndAddress = (PVOID)(ULONG)(pEndA);                      \
    swap.usLockCnt = 0;                                             \
    swap.semChgState = 0L;                                          \
    swap.pModAddress = pModAddress;                                 \
    swap.usModSize = OFFSETOF(pEndAddress) - OFFSETOF(pModAddress); \
}


#if defined(USE_VMLOCK) && !defined(MINIFSD)
/*
 * The mini can't use DevHlp so it must use FORCENOSWAP.
 * We don't care about locking mini-code since it's
 * small and runs only at boot time.
 */

/* 0x54L = 0x4L (below 16M) | 0x10L (long term) | 0x20L (permanent lock) */
#define LockSwapMod(swap)                                                   \
{                                                                           \
    if ((usRet = VMLock(0x54L,swap.pModAddress,(ULONG) swap.usModSize,      \
        ((void far *) -1L),swap.LockHandle,((PUINT) -1L))) != NO_ERROR) {   \
        /* return usRet; */                                                 \
    }                                                                       \
    swap.usLockCnt = 1;                                                    \
}

#else
/* Non-Debug :Lock the entire segment (i.e. _TEXT -> ASWAP?, _BTXT -> BSWAP?)
 * Debug : Need to lock ?SWAP?
 */
#define LockSwapMod(swap)                                               \
{                                                                       \
    if ((usRet = FSH_FORCENOSWAP (SELECTOROF (swap.pModAddress)))       \
                                                       != NO_ERROR) {   \
        return usRet;                                                   \
    }                                                                   \
    swap.usLockCnt = 1;                                                \
}

#endif



USHORT far
FixInMem ()
{
    USHORT usRet;
    PVOID pv;

    /* Lock data segment */
    pv = &bufLRUHead;
    if ((usRet = FSH_FORCENOSWAP (SELECTOROF (pv))) != NO_ERROR) {
        return usRet;
    }

    /* Set modules info */
    InitSwapMod(swap_TEXT,  _TEXT_START,  _TEXT_END  )
    InitSwapMod(swapASWAP0, ASWAP0_START, ASWAP0_END )
    InitSwapMod(swapASWAP1, ASWAP1_START, ASWAP1_END )
    InitSwapMod(swapASWAP2, ASWAP2_START, ASWAP2_END )
    InitSwapMod(swap_BTXT,  _BTXT_START,  _BTXT_END  )
    InitSwapMod(swapBSWAP0, BSWAP0_START, BSWAP0_END )
    InitSwapMod(swapBSWAP1, BSWAP1_START, BSWAP1_END )
    InitSwapMod(swapBSWAP2, BSWAP2_START, BSWAP2_END )

    /* Lock resident modules */

    LockSwapMod(swap_TEXT)
    LockSwapMod(swap_BTXT)

    /* This will go away when we're done with makeing everything swappable.
     * In the mean time it locks modules that will be swappable in the
     * future.
     */

// @76193 - until we fix the "DEBUG" definition, use "LOGGING" to know
//           that we're making a debug IFS.

#if defined(USE_VMLOCK) || defined(DEBUG) || defined (LOGGING)
    LockSwapMod(swapASWAP0)
    LockSwapMod(swapASWAP1)
    LockSwapMod(swapASWAP2)
    LockSwapMod(swapBSWAP0) /* @76193 don't swap ANYTHING! - EA code already swappable    */
    LockSwapMod(swapBSWAP1) /* @76193  - Mount code already swappable */
    LockSwapMod(swapBSWAP2)
#else
    /* If we use FORCENOSWAP then non-debug locks all code module
     * We still have to mark the individual modules.
     */
    swapASWAP0.usLockCnt = 1;
    swapASWAP1.usLockCnt = 1;
    swapASWAP2.usLockCnt = 1;
    swapBSWAP0.usLockCnt = 1; /* @76193 - EA code already swappable    */
    swapBSWAP1.usLockCnt = 1; /* @76193 - Mount code already swappable */
    swapBSWAP2.usLockCnt = 1;
#endif

    /*
     * MUST EXECUTE THIS CODE IF WE DO AN ASSERTION BUILD!
     */
#ifdef NEVER
    pv = AssertOFT;
    if ((usRet = FSH_FORCENOSWAP (SELECTOROF (pv))) != NO_ERROR) {
        return usRet;
    }
#endif

#if defined( LOGGING )
    pv = alogHistory;                           /* debug logging buffer */
    if ((usRet = FSH_FORCENOSWAP (SELECTOROF (pv))) != NO_ERROR) {
        return usRet;
    }
#endif


// @76193 don't bother if we're just logging

#if defined( DEBUG ) || defined( LOG_PRINT )
    /* lock lprintf buffers if needed */
    if( (usRet = InitLPrnt()) != NO_ERROR ) {
        return usRet;
    }
#endif

    /* Lock messages segment */
    pv = &_TXT_MSG_RESPONSE_DATA;
    if ((usRet = FSH_FORCENOSWAP (SELECTOROF (pv))) != NO_ERROR) {
        return usRet;
    }


#ifndef MINIFSD
#ifdef PERFVIEW
    FSH_REGISTERPERFCTRS((PCHAR)&pvwData,(PCHAR)&pvwTextHeader,RPC_16BIT);
#endif
#endif
    return (NO_ERROR);
}
