コード例 #1
0
ファイル: tinyio.c プロジェクト: ABratovic/open-watcom-v2
tiny_ret_t __far __fortran __TinySeek( tiny_handle_t a, unsigned long b, seek_info mode ){
    return( _TinySeek( a, b, mode ));
}
コード例 #2
0
/*
 * Init32BitTask - load and initialize the 32-bit application
 */
int Init32BitTask( HINSTANCE thishandle, HINSTANCE prevhandle, LPSTR cmdline,
                    int cmdshow )
{
    WORD                i,amount,bytes_read,j;
    WORD                sel;
    int                 handle;
    tiny_ret_t          rc;
    DWORD               size,currsize,curroff,minmem,maxmem;
    DWORD               relsize,exelen;
    struct wstart_vars  __far *dataptr;
    void                __far *aliasptr;
    DWORD               __far *relptr;
    struct fpu_area     __far *fpuptr;
    rex_exe             exe;
    exe_data            exedat;
    char                file[128];
    DWORD               flags;
    version_info        vi;
    DWORD               file_header_size;
    BOOL                tried_global_compact;
    DWORD               save_maxmem;
    DWORD               adata[2];

    flags = GetWinFlags();
    /*
     * verify that we are running on a 32-bit DPMI
     */
    _fDPMIGetVersion( &vi );
    if( !(vi.flags & VERSION_80386) ) {
        MessageBox( NULL, "Not running on a 386 DPMI implementation",MsgTitle,
                        MB_OK | MB_ICONHAND | MB_TASKMODAL );
        return( FALSE );
    }

    /*
     * get exe to load
     */
    GetModuleFileName( thishandle, file, 128 );
    rc = _fTinyOpen( file, TIO_READ );
    if( TINY_ERROR( rc ) ) {
        return( Fini( 2, (char _FAR *)"Error opening file",
                    (char _FAR *)file) );
    }
    handle = TINY_INFO( rc );

    _TinySeek( handle, 0x38, TIO_SEEK_START );
    _fTinyRead( handle, &exelen, sizeof( DWORD ) );
    _TinySeek( handle, exelen, TIO_SEEK_START );

    /*
     * check if we are being run by the debugger.  When the debugger
     * sees the 'DEADBEEF' bytes at the start of the code segment
     * (see begin.asm), it swaps them to be BEEFDEAD.  The debugger
     * then tells us to go, and if we see that we have BEEFDEAD,
     * we execute a breakpoint just before we call our 32-bit code.
     * Then the debugger traces a single instruction and it looks to
     * the user like the start of his/her code is the start of
     * the application.
     */
    if( deadbeef == 0xBEEFDEAD ) {
        InDebugger = 0;
    } else {
        InDebugger = 1;
    }
    DPL = (CS() & 0x03) << 5;   /* our privilege level */

    /*
     * validate header signature
     */
    _fTinyRead( handle, &exe, sizeof( rex_exe ) );
//    BreakPoint();
    if( !(exe.sig[0] == 'M' && exe.sig[1] == 'Q') ) {
        return( Fini( 1,(char _FAR *)"Invalid EXE" ) );
    }
    file_header_size = (DWORD) exe.file_header * 16L;
    /*
     * exe.one is supposed to always contain a 1 for a .REX file.
     * However, to allow relocation tables bigger than 64K, the
     * we extended the linker to have the .one field contain the
     * number of full 64K chunks of relocations, minus 1.
     */
    file_header_size += (exe.one-1)*0x10000L*16L;

    /*
     * get exe data - data start and stack start
     */
    _TinySeek( handle, exelen + file_header_size + (long)exe.initial_eip, TIO_SEEK_START );
    _fTinyRead( handle, &exedat, sizeof( exe_data ) );
    /*
     * get file size
     */
    size = (long) exe.file_size2 * 512L;
    if( exe.file_size1 > 0 ) {
        size += (long) exe.file_size1 - 512L;
    }

    /*
     * get stack size
     */
    StackSize = Align4K( exe.initial_esp - ((exedat.stackstart+15) & ~15ul) );
    if( StackSize < 0x1000 ) {
        StackSize = 0x1000;
    }

    /*
     * get minimum/maximum amounts of heap, then add in exe size
     * to get total area
     */
//    BreakPoint();
    minmem = (DWORD) exe.min_data *(DWORD) 4096L;
    if( exe.max_data == (WORD)-1 ) {
        maxmem = 4096L;
    } else {
        maxmem = (DWORD) exe.max_data*4096L;
    }
    minmem = Align4K( minmem + size + 0x10000ul );
    maxmem = Align4K( maxmem + size + 0x10000ul );
    if( minmem > maxmem ) {
        maxmem = minmem;
    }

    /*
     * get memory to load file
     */
    tried_global_compact = FALSE;
    save_maxmem = maxmem;
    for(;;) {
        i = DPMIGet32( adata, maxmem );
        if( i != 5 )  break;
        if( maxmem == minmem ) {
            if( tried_global_compact ) {
                return( Fini( 3,
                  (char _FAR *)"Not enough memory for application\n(minimum ",
                  dwordToStr( minmem ),(char _FAR *)" required)" ));
            }
            /*
             * GlobalCompact(-1) causes Windows to unfragment its
             * memory.  This might give us a chance to get a linear
             * chunk big enough
             */
            GlobalCompact( GlobalCompact( 0 ) );
            maxmem = save_maxmem;
            tried_global_compact = TRUE;
        } else if( maxmem < 64L * 1024L ) {
            maxmem = minmem;
        } else {
            maxmem -= 64L * 1024L;
            if( maxmem < minmem ) {                     /* 09-aug-93 */
                maxmem = minmem;
            }
        }
    }
    DataHandle = adata[1];
    BaseAddr   = adata[0] + 0x10000ul;
#if FLAT
    i = InitFlatAddrSpace( BaseAddr, 0L );
#else
    i = InitFlatAddrSpace( BaseAddr, maxmem );
#endif
    BaseAddr = 0L;
    if( i ) {
        DPMIFreeMemoryBlock( DataHandle );
        return( Fini( 2,(char _FAR *)"Allocation error ", dwordToStr( i ) ) );
    }
    SaveSP = BaseAddr + StackSize;
    CodeLoadAddr = SaveSP;
    MyDataSelector = DataSelector;
    GetDataSelectorInfo();
    CodeEntry.off = exe.initial_eip + CodeLoadAddr + sizeof( exe_data );

    /*
     * this builds a collection of LDT selectors that are ready for
     * allocation
     */
    if( InitSelectorCache() != 0 ) {
        return( Fini( 1,(char _FAR *)outOfSelectors) );
    }

    /*
     * read the exe into memory
     */
    currsize = size - file_header_size;
    _TinySeek( handle, exelen + file_header_size, TIO_SEEK_START );
    i = DPMIGetAliases( CodeLoadAddr, (LPDWORD) &aliasptr, 0 );
    if( i ) {
        return( Fini( 3,(char _FAR *)"Error ",
                dwordToStr( i ),
                (char _FAR *)" getting alias for read" ) );
    }
    dataptr = aliasptr;
    sel = ((DWORD) dataptr) >> 16;
    curroff = CodeLoadAddr;
    while( currsize != 0 ) {

        if( currsize >= (DWORD) READSIZE ) {
            amount = READSIZE;
        } else {
            amount = (WORD) currsize;
        }
        rc = _fTinyRead( handle, dataptr, amount );
        bytes_read = TINY_INFO( rc );
        if( bytes_read != amount ) {
            return( Fini( 1,(char _FAR *)"Read error" ) );
        }
        currsize -= (DWORD) amount;
        curroff += (DWORD) amount;
        DPMISetSegmentBaseAddress( sel, DataSelectorBase + curroff );
    }
    EDataAddr = curroff;                        // 03-jan-95

    DPMISetSegmentBaseAddress( sel, DataSelectorBase );
    relptr = (DWORD __far *)aliasptr;             // point to 32-bit stack area
    /*
     * get and apply relocation table
     */
    relsize = sizeof( DWORD ) * (DWORD) exe.reloc_cnt;
    {
        DWORD   realsize;
        WORD    kcnt;

        realsize = file_header_size - (DWORD) exe.first_reloc;
        kcnt = realsize / (0x10000L*sizeof(DWORD));
        relsize += kcnt * (0x10000L*sizeof(DWORD));
    }
    if( relsize != 0 ) {
        _TinySeek( handle, exelen + (DWORD)exe.first_reloc, TIO_SEEK_START );
        if( StackSize >= (DWORD) READSIZE ) {
            amount = READSIZE;
        } else {
            amount = (WORD) StackSize;
        }
        while( relsize != 0L ) {
            if( relsize < (DWORD)amount ) {
                amount = (WORD) relsize;
            }
            rc = _fTinyRead( handle, relptr, amount );
            bytes_read = TINY_INFO( rc );
            if( bytes_read != amount ) {
                return( Fini( 1,(char _FAR *)"Relocation read error" ) );
            }
            CodeRelocate( relptr, amount/sizeof(DWORD) );
            relsize -= (DWORD) amount;
        }
    }

    _TinyClose( handle );

    /* initialize emulator 8087 save area 20-oct-94 */

    fpuptr = (struct fpu_area __far *)((char __far *)aliasptr + FPU_AREA);
    _fmemset( fpuptr, 0, sizeof(struct fpu_area) );
    fpuptr->control_word = 0x033F;
    fpuptr->tag_word = 0xFFFF;

    /*
     * set dataptr to special area in data segment of 32-bit app
     */
    curroff = exedat.datastart;
    if( exe.reloc_cnt != 0 )  curroff += CodeLoadAddr;
    DPMISetSegmentBaseAddress( sel, DataSelectorBase + curroff );

    /*
     * insert command line parms
     */
    dataptr->thishandle = (WORD)thishandle;
    dataptr->prevhandle = (WORD)prevhandle;
    dataptr->cmdline    = (DWORD) cmdline;
    dataptr->cmdshow    = cmdshow;
    dataptr->_no87      = _no87;

    /*
     * set hardware selectors for screen memory
     */
    dataptr->_A000H     = (WORD) &_A000H;
    dataptr->_B000H     = (WORD) &_B000H;
    dataptr->_B800H     = (WORD) &_B800H;
    dataptr->_C000H     = (WORD) &_C000H;
    dataptr->_D000H     = (WORD) &_D000H;
    dataptr->_E000H     = (WORD) &_E000H;
    dataptr->_F000H     = (WORD) &_F000H;

    /*
     * ptrs to some data areas
     */
    dataptr->CodeSelectorBase.seg =  (WORD) FP_SEG( &CodeSelectorBase );
    dataptr->CodeSelectorBase.off = (DWORD) FP_OFF( &CodeSelectorBase );
    dataptr->DataSelectorBase.seg =  (WORD) FP_SEG( &DataSelectorBase );
    dataptr->DataSelectorBase.off = (DWORD) FP_OFF( &DataSelectorBase );
    dataptr->_32BitCallBackAddr.seg =  (WORD) FP_SEG( &_32BitCallBackAddr );
    dataptr->_32BitCallBackAddr.off = (DWORD) FP_OFF( &_32BitCallBackAddr );
    dataptr->_DLLEntryAddr.seg =  (WORD) FP_SEG( &_DLLEntryAddr );
    dataptr->_DLLEntryAddr.off = (DWORD) FP_OFF( &_DLLEntryAddr );
    dataptr->_WEPAddr.seg =  (WORD) FP_SEG( &_WEPAddr );
    dataptr->_WEPAddr.off = (DWORD) FP_OFF( &_WEPAddr );
    dataptr->_16BitCallBackAddr = &__CallBack;

    /*
     * insert glue routines into data area of caller
     */
    for( j = 0; j < MaxGlueRoutines; j++ ) {
        dataptr->gluertns[j].seg =  (WORD) FP_SEG( Glue[j].rtn );
        dataptr->gluertns[j].off = (DWORD) FP_OFF( Glue[j].rtn );
    }
    DPMIFreeAlias( sel );

    /*
     * check for FPU and WGod
     */
    if( flags & WF_80x87 ) {
        Has87 = TRUE;
    } else {
        Has87 = FALSE;
    }
    if( CheckWin386Debug() == WGOD_VERSION ) {
//    BreakPoint();
        HasWGod = TRUE;
        if( !Has87 ) {
            EMUInit();
            EMURegister( CodeEntry.seg, SaveSP - StackSize + FPU_AREA );
        }
    } else {
        HasWGod = FALSE;
    }
    return( TRUE );

} /* Init32BitTask */