home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / XLIB40.ZIP / XLIB.DOC < prev    next >
Text File  |  1994-03-25  |  173KB  |  4,153 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.                              XLIB PROGRAMMER'S MANUAL
  32.                                    VERSION 4.0
  33.  
  34.                               (DOS Extender Library)
  35.  
  36.                                 TechniLib Company
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.                   Copyright 1993 1994, by TechniLib (TM) Company
  61.                                All Rights Reserved
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                           TERMS OF USE AND DISTRIBUTION
  69.  
  70.  
  71.        XLIB is a shareware product; therefore, unregistered copies of XLIB are
  72.   made available free of charge so that potential purchasers will have the
  73.   opportunity to examine and test the software before committing payment.
  74.   Distribution of unregistered copies of XLIB to other potential users is also
  75.   permitted and appreciated.  However, usage and distribution of XLIB must
  76.   conform to the following conditions.  In the following statement, the term
  77.   "commercial distribution," includes shareware distribution.
  78.  
  79.   1) XLIB and accompanying software must be distributed together in copies of
  80.   the original archive provided by TechniLib.  Neither the archive nor
  81.   individual files therein may be modified.
  82.  
  83.   2) The XLIB archive may be distributed in combination with other shareware
  84.   products; however, the XLIB archive may not be distributed with other
  85.   commercially distributed software without written consent of TechniLib.
  86.  
  87.   3) Copies of XLIB which have been used to develop software for commercial
  88.   distribution must be registered before such software is marketed.  Copies of
  89.   XLIB which have been used to develop noncommercial software must be registered
  90.   if such software is to be regularly used either by the developer or others.
  91.  
  92.   4) Commercially distributed software must embed XLIB procedures in the
  93.   software code.  Files contained in the XLIB archive may not be placed in the
  94.   distribution media.
  95.  
  96.   5) XLIB is designed to offer a set of services to other executable code.  XLIB
  97.   may not be used to develop software for commercial distribution which will
  98.   essentially offer any of these same services to other executable code.
  99.   Exceptions to this condition require written consent of TechniLib.
  100.  
  101.   6) Rights afforded by registering a single copy of XLIB pertain only to a
  102.   single computer.
  103.  
  104.   7) XLIB may be registered for a fee of $40.00 per copy.  Accompany payment
  105.   with the registration form included in the XLIB archive.  Registrants will be
  106.   entitled to the most recent version of the XLIB archive.
  107.  
  108.  
  109.                               DISCLAIMER OF WARRANTY
  110.  
  111.  
  112.        XLIB AND ALL ACCOMPANYING SOFTWARE AND LITERATURE ARE DISTRIBUTED WITH
  113.   THE EXCLUSION OF ANY AND ALL IMPLIED WARRANTIES, AND WITH THE EXCLUSION OF
  114.   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  TechniLib
  115.   SHALL HAVE NO LIABILITY FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  116.   RESULTING FROM THE USE OF XLIB OR ACCOMPANYING MATERIALS.  The user assumes
  117.   the entire risk of using this software.
  118.  
  119.  
  120.                   Copyright 1993 1994, by TechniLib (TM) Company
  121.                                All Rights Reserved
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.                                 TABLE OF CONTENTS
  129.  
  130.  
  131.   CHAPTERS
  132.                                                                       Page
  133.   1.  Introduction                                                       1
  134.   2.  XLIB Conventions and Structure                                     4
  135.   3.  XLIB Initialization and Termination                                7
  136.   4.  Mode Switching                                                    10
  137.   5.  Inline Mode Switching                                             14
  138.   6.  Interrupt Management                                              18
  139.   7.  Memory Management                                                 25
  140.   8.  File Management                                                   29
  141.   9.  Descriptor Management                                             34
  142.   10. Using XLIB in High-Level Language Libraries                       35
  143.   11. Using XLIBE for Debugging and Exception Trapping                  39
  144.  
  145.  
  146.   TABLES
  147.                                                                       Page
  148.   1.  XLIB Segments and Selectors by Public Symbol                       5
  149.   2.  IFLAGS Settings for Memory Allocation Control                      8
  150.   3.  CALLPM/ENTERPM Register Storage Locations by Public Symbol        12
  151.   4.  CALLRM Register Storage Locations by Public Symbol                13
  152.   5.  XLIB File Control Block Structure                                 29
  153.  
  154.  
  155.   EXAMPLES
  156.                                                                       Page
  157.   1.  Simple Mode Switching Under XLIB                                   3
  158.   2.  Using INLINEPM/INLINERM in C                                      15
  159.   3.  Calling Protected-Mode Libraries From BASIC                       35
  160.  
  161.  
  162.   APPENDICES
  163.                                                                       Page
  164.   A. Description of XLIB Public Data                                    43
  165.   B. XLIB Error Codes                                                   47
  166.   C. DPMI 1.0 Error Codes                                               49
  167.   D. XMS Error Codes                                                    50
  168.   E. Calling Protected-Mode Libraries From C                            51
  169.   F. Technical Support                                                  53
  170.   G. The SWITCHPM and SWITCHRM Procedures                               54
  171.   H. Debugging                                                          55
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.                                        iii
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.                         INDEX TO PROCEDURE SPECIFICATIONS
  193.  
  194.  
  195.   Initialization Routines
  196.  
  197.   Name         Purpose                                           CPU Mode   Page
  198.   INITXLIB     Initialize XLIB                                   Real          8
  199.   XLIBMEMREQ   Get XLIB convention memory requirements           Real          9
  200.   DPMIMEMREQ   Get DPMI conventional memory requirements         Real          9
  201.   VCPIMEMREQ   Get VCPI conventional memory requirements         Real          9
  202.   XLIBCONFIG   Get XLIB post-initialization configuration        Real          9
  203.  
  204.   Mode Switch Routines
  205.  
  206.   Name         Purpose                                           CPU Mode   Page
  207.   CALLPM       Call protected-mode procedure                     Real         11
  208.   RETPM        Return from protected mode to real mode           Protected    11
  209.   ENTERPM      Call protected mode procedure                     Real         11
  210.   EXITPM       Return from protected mode to real mode           Protected    12
  211.   CALLRM       Call real-mode procedure from protected mode      Protected    12
  212.  
  213.   Inline Mode Switch Routines
  214.  
  215.   Name         Purpose                                           CPU Mode   Page
  216.   INLINEPM     Return to caller in protected mode                Real         16
  217.   INLINERM     Return to caller in real mode                     Protected    16
  218.   CALL32       Call 32-bit procedure from 16-bit protected mode  Protected    17
  219.  
  220.   Interrupt Management Routines
  221.  
  222.   Name         Purpose                                           CPU Mode   Page
  223.   PMGETRMIV    Get real-mode interrupt vector                    Protected    22
  224.   PMSETRMIV    Set real-mode interrupt vector                    Protected    22
  225.   GETPMIV      Get protected-mode interrupt vector               Real         23
  226.   SETPMIV      Set protected-mode interrupt vector               Real         23
  227.   PMGETPMIV    Get protected-mode interrupt vector               Protected    23
  228.   PMSETPMIV    Set protected mode interrupt vector               Protected    24
  229.   DEFLECTPM    Deflect real-mode interrupt to protected mode     Real         24
  230.   SWITCHPM     Perform nestable switch to protected mode         Real         54
  231.   SWITCHRM     Perform nestable switch to real mode              Real         54
  232.  
  233.   Memory Management Routines
  234.  
  235.   Name         Purpose                                           CPU Mode   Page
  236.   PMGETDOSMEM  Allocate DOS memory block                         Protected    25
  237.   PMFREEDOSMEM Release DOS memory block                          Protected    26
  238.   GETMEM       Allocate extended memory block                    Real         26
  239.   FREEMEM      Release extended memory block                     Real         27
  240.   RESETMEM     Release all extended memory blocks                Real         27
  241.   PMGETMEM     Allocate extended memory block                    Protected    27
  242.   PMFREEMEM    Release extended memory block                     Protected    27
  243.   PMRESETMEM   Release all extended memory blocks                Protected    27
  244.   MAPIO        Get logical address for memory-mapped IO device   Real         28
  245.   PMMAPIO      Get logical address for memory-mapped IO device   Protected    28
  246.  
  247.  
  248.  
  249.                                         iv
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.                   INDEX TO PROCEDURE SPECIFICATIONS (CONTINUED)
  257.  
  258.  
  259.   File Management Routines
  260.  
  261.   Name         Purpose                                           CPU Mode   Page
  262.   XCREATE      Create file                                       Real         30
  263.   XOPEN        Open existing file                                Real         30
  264.   XCLOSE       Close file                                        Real         31
  265.   XSAVE        Save memory to created file                       Real         31
  266.   XLOAD        Load entire file to memory                        Real         31
  267.   XWRITE       Random write from memory to file                  Real         32
  268.   XREAD        Random read from file to memory                   Real         32
  269.   PMXCREATE    Create file                                       Protected    33
  270.   PMXOPEN      Open existing file                                Protected    33
  271.   PMXCLOSE     Close file                                        Protected    33
  272.   PMXSAVE      Save memory to created file                       Protected    33
  273.   PMXLOAD      Load entire file to memory                        Protected    33
  274.   PMXWRITE     Random write from memory to file                  Protected    33
  275.   PMXREAD      Random read from file to memory                   Protected    33
  276.  
  277.   Descriptor Management Routines
  278.  
  279.   Name         Purpose                                           CPU Mode   Page
  280.   SETDESC      Place descriptor in local descriptor table        Real         34
  281.   PMSETDESC    Place descriptor in local descriptor table        Protected    34
  282.  
  283.   Debugging Routines (XLIBE only)
  284.  
  285.   Name         Purpose                                           CPU Mode   Page
  286.   SETWATCH     Set debug data watchpoint                         Real         41
  287.   FREEWATCH    Release debug data watchpoint                     Real         41
  288.   RESETWATCH   Release all debug data watchpoints                Real         42
  289.   PMSETWATCH   Set debug data watchpoint                         Protected    42
  290.   PMFREEWATCH  Release debug data watchpoint                     Protected    42
  291.   PMRESETWATCH Release all debug data watchpoints                Protected    42
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.                                         v
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.                                  1. Introduction
  321.  
  322.  
  323.        XLIB is an assembly language library which greatly simplifies protected-
  324.   mode programming under Microsoft DOS.  Assembly language programmers will
  325.   likely abandon real-mode programming after acquiring XLIB.  With only two
  326.   calls to XLIB procedures, assembly language programs can be utilizing the
  327.   simplicity and power of 32-bit processing.  C and C++ programs can harness the
  328.   powers of 16-bit protected mode using inline assembly.  Additionally, the XLIB
  329.   archive contains a second library call EASYX which can be used by all high-
  330.   level languages to gain access to extended memory.
  331.        XLIB is designed for the Intel 386, 486, and Pentium processors.  XLIB
  332.   fully utilizes the 32-bit processing powers of these chips and makes these
  333.   powers available to the user.  The compactness of XLIB follows largely from
  334.   the fact that much of it is written in 32-bit code.
  335.        The archive for XLIB includes libraries and sample code in both Microsoft
  336.   and Borland formats.  XLIB has been successfully implemented with several
  337.   Borland and Microsoft languages.
  338.        The archive for version 4.0 provides a second library called XLIBE.  This
  339.   library contains all of the procedures in XLIB plus other procedures designed
  340.   for debugging and exception trapping.  XLIBE is capable of trapping exceptions
  341.   in real mode also; therefore, it is potentially useful even to programmers who
  342.   would otherwise have no interest in protected mode.
  343.        XLIB is used to produce extended DOS applications.  DOS is unfortunately
  344.   limited by the fact that it is a real-mode operating system intended to manage
  345.   only real-mode programs.  Real-mode programs cannot use memory addresses
  346.   requiring more than 20 bits, or use memory offsets requiring more than 16
  347.   bits.  Such programs are further limited by the fact that 32-bit instructions
  348.   execute awkwardly in real-mode.  When the processor is in real-mode, it will
  349.   expect all 32-bit instructions to be preceded by at least one prefix.  Each of
  350.   these prefixes consumes one byte of memory and requires at least one clock to
  351.   execute.  Such limitations do not exist in 32-bit protected mode.  Extended
  352.   DOS applications overcome the limitations of DOS with their ability to execute
  353.   in both real and protected modes.  DOS services can be utilized from real mode
  354.   while the 32-bit processing power of the CPU can be utilized from protected
  355.   mode.
  356.        There are presently several 32-bit operating systems available on the
  357.   market.  Such systems include IBM OS/2, Microsoft Windows NT, and UNIX.  These
  358.   systems can manage programs which operate exclusively in protected mode.
  359.   There is little doubt that microcomputer programming is headed for a
  360.   protected-mode future.  However, it is not apparent at present which of these
  361.   operating systems will prevail as the future leader.  DOS remains the dominant
  362.   system for the time being, and this may remain the case for longer than many
  363.   anticipate because DOS is the common thread which links all of these systems.
  364.   All of these systems have DOS emulation capabilities.  Extended DOS programs
  365.   will almost equal the native programs of these systems in power, and will far
  366.   surpass them in compatibility.
  367.        Future programs will likely operate exclusively in protected mode using
  368.   the flat (unsegmented) memory model.  The memory models supported by XLIB
  369.   approximate the flat model; therefore, code written for XLIB will require
  370.   little modification when being transported to flat-model operating
  371.   environments.  Indeed, many procedures will require no modification
  372.   whatsoever.  Moreover, XLIB includes flat-model descriptors which may be used
  373.  
  374.  
  375.  
  376.  
  377.                                         1
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.   to execute genuine flat-model code.  XLIB does not load and relocate such
  385.   code; however, it does provide the necessary tools to develop such procedures.
  386.        XLIB procedures handle important tasks such as mode switching between
  387.   real and protected modes, memory management under protected mode, protected-
  388.   mode interrupt management, and protected-mode file management.  XLIB includes
  389.   routines to perform these tasks in the absence of a protected-mode interface,
  390.   or in the presence of the Virtual Control Program Interface (VCPI), or the DOS
  391.   Protected Mode Interface (DPMI, version .9 or higher).  XLIB can also manage
  392.   extended memory through the Extended Memory Specification (XMS).  This
  393.   versatility makes XLIB compatible with nearly all common operating
  394.   environments for 386, 486, and Pentium processors.
  395.        XMS is the standard interface for managing extended memory.  HIMEM.SYS by
  396.   Microsoft is the most commonly known XMS driver.  VCPI is an older protected-
  397.   mode interface designed to coordinate possibly several protected-mode
  398.   programs.  VCPI is an optional subset of EMS (Expanded Memory Specification).
  399.   The most common VCPI driver is Microsoft's EMM386.SYS.  DPMI is the protected-
  400.   mode interface in Microsoft Windows and is considered the interface of the
  401.   future (though VCPI remains popular).  The more recent versions of Qualitas'
  402.   386MAX (version 6 and up) contain all of the above interfaces.  Quarterdeck's
  403.   QEMM386 contains XMS and VCPI.  Quarterdeck supplies DPMI in a second driver
  404.   called QDPMI.  The latter is supplied free of charge but requires QEMM386.
  405.        Upon initialization, XLIB will examine the operating environment for the
  406.   presence of DPMI, VCPI, and XMS, and then configure itself accordingly.  The
  407.   client program may therefore perform calls to XLIB procedures with few
  408.   concerns as to the environment in which it is executing.
  409.        This manual assumes the reader to have some degree of familiarity with
  410.   protected mode and a working knowledge of assembly language.  If you are not
  411.   familiar with protected mode, then read the tutorial in DONTREAD.ME.  This
  412.   tutorial is quite informative.  Many readers will be able to bypass this
  413.   manual altogether after working through the tutorial.  Those unfamiliar with
  414.   assembly language should consider using the EASYX library.  This library is a
  415.   real-mode interface to XLIB.  It contains procedures which may be utilized
  416.   without assembly language.  The documentation for EASYX is included in the
  417.   XLIB archive.  The reader might wish to refer to this documentation now;
  418.   however, the present document may still be needed for technical reference.
  419.        XLIB relieves the programmer of descriptor table management by supplying
  420.   a set of predefined segments along with their associated descriptors and
  421.   selectors.  Many protected-mode procedures will require no modification for
  422.   XLIB other than being placed in the proper segment.  XLIB provides a single
  423.   32-bit segment for protected-mode routines.  This segment may be larger than
  424.   64K, but must reside in conventional memory so that DOS can load it.  However,
  425.   code within this segment may access data throughout the address space.  XLIB
  426.   does allow user-defined descriptors for special circumstances.
  427.        The following program illustrates the simplicity with which protected-
  428.   mode execution may be initiated and terminated with XLIB.  The program was
  429.   written with the Microsoft Assembler (MASM).  It first initializes XLIB by
  430.   calling a procedure called INITXLIB (initialize XLIB).  After confirming that
  431.   initialization is successful, the program then transfers control to a 32-bit
  432.   protected-mode procedure which prints a message to the screen.  Control is
  433.   transferred by placing the protected-mode target address on the stack and then
  434.   calling an XLIB procedure named CALLPM (call protected mode).  CALLPM will
  435.   expect the target procedure to be contained in a segment called TSEG.  The
  436.   protected-mode procedure in TSEG returns control to real or virtual 8086 (V86)
  437.   mode simply by executing the RET instruction.
  438.  
  439.  
  440.  
  441.                                         2
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.   Example 1:  Simple Mode Switching Under XLIB
  449.   _____________________________________________________________________________
  450.                  .MODEL        LARGE,PASCAL
  451.                  .386P
  452.  
  453.                  INCLUDE        XLIB.INC       ;Include XLIB public symbols
  454.                  INCLUDELIB     XLIB.LIB       ;Link with XLIB.LIB
  455.  
  456.                  .STACK         1024
  457.                  .CODE
  458.                  .STARTUP
  459.  
  460.                  CALL           INITXLIB       ;Initialize XLIB
  461.                  OR             EAX,EAX        ;EAX = 0 if successful
  462.                  JZ             INITDONE
  463.                  .EXIT          0              ;Initialization failed
  464.  
  465.   INITDONE:      PUSHD          OFFSET DEMOPROC
  466.                  CALL           CALLPM         ;Execute DEMOPROC in protected
  467.                  .EXIT          0
  468.  
  469.   ;Protected-mode routines must be placed in following segment:
  470.   TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  471.                  ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  472.  
  473.   ;Protected-mode routine to print message to the screen using DOS function.
  474.   DEMOPROC       PROC NEAR
  475.                  MOV            EBX,OFFSET PMMSG
  476.                  MOV            AH,02H
  477.   MSGLOOP:       MOV            DL,CS:[EBX]    ;32-bit offset!!!!!
  478.                  OR             DL,DL
  479.                  JZ             EXIT
  480.                  INT            21H            ;Print character with DOS
  481.                  INC            EBX
  482.                  JMP            MSGLOOP
  483.   EXIT:          RET                           ;Go back to real or V86 mode
  484.   PMMSG          DB  "In 32-bit protected mode!!!  "
  485.                  DB  "Returning to real mode.",10,13,0
  486.   DEMOPROC       ENDP
  487.  
  488.   TSEG           ENDS
  489.                  END
  490.   _____________________________________________________________________________
  491.  
  492.  
  493.        The framework presented in the above program is extremely simple;
  494.   nonetheless, it will meet the demands of many protected-mode programs.  Most
  495.   protected-mode programs will require no further complications apart from a few
  496.   calls to XLIB extended memory management procedures.
  497.        To achieve the simplicity of the above program, XLIB must handle numerous
  498.   complexities internally.  This manual attempts to give the reader thorough
  499.   insight into the workings of XLIB.  It also discusses at length the numerous
  500.   configuration options that are available to the programmer.
  501.  
  502.  
  503.  
  504.  
  505.                                         3
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.                         2. XLIB Conventions and Structure
  513.  
  514.  
  515.        The XLIB archive contains files for both Microsoft and Borland languages.
  516.   The files for Borland have the same names as those for Microsoft except a "B"
  517.   is added to the base name.  For example, XLIB.LIB is the XLIB library intended
  518.   for Microsoft languages whereas XLIBB.LIB is intended for Borland languages.
  519.   Sample code in this document is always for Microsoft languages; however,
  520.   Borland versions may be found in the archive.  Important information about
  521.   using XLIB with Microsoft and Borland languages has been included in the
  522.   README.DOC file.
  523.        All instructions pertaining to XLIB.LIB also pertain to XLIBE.LIB;
  524.   however, the latter library contains additional procedures for debugging and
  525.   for trapping processor exceptions.  XLIBE is intended for program development
  526.   while XLIB is intended for release versions.  The additional features in XLIBE
  527.   are discussed in Chapter 11.
  528.        The Microsoft version of XLIB was developed and tested under Microsoft
  529.   DOS version 6.1 using Microsoft Assembler (MASM) version 6.11, Microsoft LINK
  530.   version 5.31.009, and Microsoft LIB version 3.20.01.  The Borland version of
  531.   XLIB was developed and tested using Turbo Assembler (TASM) version 4.0, TLINK
  532.   version 6.0, and TLIB version 4.0.
  533.        XLIB has been tested under Microsoft Windows 3.1, Qualitas 386MAX
  534.   versions 6.02 and 7.0, Quarterdeck QEMM386 versions 6.02 and 7.03, Quarterdeck
  535.   QDPMI versions 1.01 and 1.03, Quarterdeck DESQview version 2.42, and IBM OS/2
  536.   versions 2.0 and 2.1.
  537.        XLIB public procedures and public data are explained in the following
  538.   chapters.  A detailed explanation of most XLIB public data is also included in
  539.   Appendix A.  This chapter sets forth rules which will be generally applicable
  540.   to all XLIB data and procedures.
  541.        Though it is sometimes necessary for XLIB to distinguish between real
  542.   mode and virtual 8086 mode; this document uses the term "real mode" to include
  543.   virtual 8086 mode.
  544.        All public real-mode procedures and 16-bit protected-mode procedures in
  545.   XLIB are located in a segment called CSEG.  The user may also place code in
  546.   CSEG but is rarely required to do so.  All public XLIB procedures in CSEG have
  547.   far returns (exceptions are noted in Appendix G).
  548.        All public 32-bit protected-mode procedures in XLIB are located in a 32-
  549.   bit segment called TSEG and have near returns.  XLIB will also expect the user
  550.   to place all 32-bit procedures in TSEG and will expect these procedures to
  551.   have near returns.  This policy is adopted to achieve approximation to the
  552.   flat model.
  553.        Nearly all XLIB protected-mode procedures are 32-bit routines.  The only
  554.   exceptions to this rule are the inline mode-switch procedures discussed in
  555.   Chapter 5.  This policy is implemented to approximate the flat model.  There
  556.   are very few circumstances in which 16-bit protected mode is preferable to 32-
  557.   bit protected mode.  One can generally increase program speed and reduce
  558.   program size by writing the code for 32-bit segments.
  559.        TSEG may be larger than 64K provided that certain rules are observed.
  560.   XLIB adheres to all of the necessary rules.  First, only 32-bit protected-mode
  561.   code should be placed in TSEG.  The processor will not generally be able to
  562.   execute real-mode code in this segment because the offsets will be 32-bit
  563.   values.  Second, real-mode code should never write to or read from TSEG.  Such
  564.   instructions will also require 32-bit offsets.  Modifications to TSEG should
  565.   be done from a TSEG protected-mode procedure.  Finally, segment constants
  566.  
  567.  
  568.  
  569.                                         4
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.   should never be encoded in TSEG.  For example, the symbols CSEG, TSEG, DSEG,
  577.   and DGROUP should not be found in TSEG.  DOS will not be able to perform
  578.   relocation edits on these constants if they are in a segment larger than 64K.
  579.   To read these values from TSEG, initialize memory locations in a 16-bit
  580.   segment to the values and then read the memory locations.  Memory locations in
  581.   DSEG have already been initialized for XLIB segments (see Appendix A).
  582.        Microsoft LINK will issue a warning for code segments exceeding 64K;
  583.   however, this warning may be safely ignored provided that the above rules are
  584.   observed.
  585.        TASM programmers should use the LARGESTACK directive for code in TSEG.
  586.   Without this directive, stack frames will be set up with BP and SP rather than
  587.   EBP and ESP.  The results could be disastrous if the high words of the 32-bit
  588.   registers are nonzero, as could be the case if TSEG exceeds 64K.
  589.        All XLIB procedures may be called with interrupts enabled.  An enabled
  590.   interrupt state will never be returned disabled.
  591.        With one exception, all XLIB public data is contained in a 16-bit segment
  592.   called DSEG.  The user may also place data in DSEG but is seldom required to
  593.   do so.
  594.        XLIB uses the PASCAL calling and naming convention.  The PASCAL
  595.   convention is equivalent to the BASIC and FORTRAN conventions.  C programmers
  596.   must adapt XLIB procedures and symbols with declarations which specify the
  597.   PASCAL convention.  The header file XLIB.H contains such declarations.
  598.        XLIB routines which can encounter error conditions will always return
  599.   XLIB error codes in AX.  A list of such error codes is presented in Appendix
  600.   B.  In most cases, DX or the high word of EAX will be returned with specific
  601.   information about the error, such as DPMI, XMS, or DOS error codes.  DPMI
  602.   error codes are presented in Appendix C.  XMS error codes are in Appendix D.
  603.        Selectors for all XLIB segments are placed in public WORD locations in
  604.   segment DSEG.  The following table gives the name of each predefined selector
  605.   along with its associated segment name and description:
  606.  
  607.  
  608.   Table 1:  XLIB Segments and Selectors by Public Symbol
  609.   _____________________________________________________________________________
  610.   Selector Name    Segment Name      Description
  611.   -------------    ------------      -----------
  612.   CSEGSEL          CSEG              XLIB 16-bit code segment
  613.   CSEGDSEL         CSEG              Data selector to CSEG
  614.   TSEGSEL          TSEG              32-bit code segment
  615.   TSEGDSEL         TSEG              Data selector to TSEG
  616.   DSEGSEL          DSEG              XLIB data segment
  617.   FLATSEL          .                 Flat-model code
  618.   FLATDSEL         .                 Flat-model data
  619.   DGROUPSEL        DGROUP            DGROUP data group
  620.   SCRNSEL          .                 Screen data
  621.   MAINCSSEL        .                 CS selector for main caller
  622.   MAINSSSEL        .                 SS selector for main caller
  623.   MAINDSSEL        .                 DS selector for main caller
  624.   MAINESSEL        .                 ES selector for main caller
  625.   ILCSSEL          .                 Inline CS selector
  626.   ILSSSEL          .                 Inline SS selector
  627.   ILDSSEL          .                 Inline DS selector
  628.   _____________________________________________________________________________
  629.  
  630.  
  631.  
  632.  
  633.                                         5
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.        The flat-model and TSEG descriptors have limit FFFFFFFFH.  All other
  641.   descriptors have limit FFFFH.  The screen descriptor has base set to B8000H
  642.   for color monitors and B0000H for monochrome monitors.  MAINCSSEL, MAINSSEL,
  643.   MAINDSSEL, and MAINESSEL are selectors to descriptors which have base
  644.   addresses matching the contents of CS, SS, DS, and ES as of the call to
  645.   INITXLIB.  ILCSSEL, ILSSSEL, and ILDSSEL are selectors used by the inline
  646.   mode-switch procedures (see Chapter 5).  The base addresses of the
  647.   corresponding descriptors are adjusted dynamically.
  648.        All data segments are expand-up and writable.  Data descriptors also have
  649.   their big bits set; consequently, implicit stack instructions will use ESP
  650.   rather then SP.  All code segments are readable and nonconforming.  Descriptor
  651.   privilege levels and requested privilege levels are set to zero unless DPMI is
  652.   installed.  Privilege levels under DPMI will generally be set to three.
  653.        The values contained in the above selectors will be different under DPMI
  654.   than other environments.  Moreover, DPMI selector values can differ in
  655.   different environments and under different hosts.  Therefore, the user should
  656.   not assume these values to be constant.
  657.        Since selector values are stored in DSEG, the user must never lose track
  658.   of the DSEG selector.  This could prove a problem in interrupt handlers where
  659.   no assumptions can be made as to register contents.  Consequently, XLIB places
  660.   a copy of the DSEG selector in TSEG where it can always be found.  It is
  661.   stored under WORD symbol CSDSEGSEL and may be read with CS override.  For
  662.   example from TSEG code one can always load DS with DSEGSEL using MOV
  663.   DS,CS:CSDSEGSEL.
  664.        XLIB never uses selectors past DGROUPSEL in Table 1.  The user may
  665.   therefore redefine the associated descriptors if desired (see Chapter 9).
  666.   Selector values however should not be changed.
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.                                         6
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.                       3. XLIB Initialization and Termination
  705.  
  706.  
  707.        XLIB procedures apart from those presented in this chapter should be
  708.   called only after XLIB has been initialized by calling INITXLIB.  This routine
  709.   will examine the operating environment for the presence of DPMI, VCPI, and
  710.   XMS.  It will then perform extensive code modifications upon XLIB to
  711.   accommodate the resident software.  INITXLIB is to be called only once within
  712.   a program.  Subsequent calls have no effect.
  713.        If XLIB finds that neither DPMI nor VCPI are present, then XLIB will
  714.   completely handle all mode switching and interrupt management.  If XLIB finds
  715.   that XMS is absent also, then XLIB will handle all extended memory management.
  716.   If XLIB finds that both DPMI and VCPI are present, then it will configure
  717.   itself for DPMI by default.  However, the default may be changed by setting
  718.   bit 0 of IFLAGS (initialization flags) before calling INITXLIB.  If this bit
  719.   is set, then VCPI is given priority over DPMI.  IFLAGS is a public WORD
  720.   location in DSEG.
  721.        INITXLIB will possibly attempt to allocate some memory through DOS.
  722.   Since high-level language modules and assembly language modules often claim
  723.   all available conventional memory, INITXLIB may fail for lack of available
  724.   memory.  This problem can be averted with MASM programs by linking with the
  725.   CPARM:1 parameter.  This forces the program to claim no more conventional
  726.   memory than is necessary.  TASM programs and some high-level language programs
  727.   should release memory to DOS during initialization.  This process is
  728.   illustrated for TASM in the file EXAMP1B.ASM.  In some cases, the main program
  729.   will need to retain all memory except what is necessary for XLIB.  The XLIB
  730.   procedure XLIBMEMREQ (XLIB memory requirements) may be called to obtain
  731.   conventional memory requirements for XLIB.  Usage of XLIBMEMREQ is illustrated
  732.   for Microsoft BASIC in Chapter 10.  C and C++ do not allocate all conventional
  733.   memory and therefore do not require any special action.
  734.        If both VCPI and DPMI are present, then conventional memory requirements
  735.   will depend upon which of these interfaces is to be chosen by INITXLIB.  In
  736.   such cases, XLIBMEMREQ will return DPMI memory requirements if bit 0 of IFLAGS
  737.   is clear (the default), and will return VCPI requirements otherwise.
  738.   Therefore, this bit should be set to the appropriate value by the user before
  739.   calling XLIBMEMREQ.  DPMI conventional memory requirements may be obtained by
  740.   calling DPMIMEMREQ.  These will vary from host to host.  VCPI conventional
  741.   memory requirements may be obtained by calling VCPIMEMREQ.  VCPI requires
  742.   approximately 12K.
  743.        One of the principle functions of XLIB is of course to make protected-
  744.   mode interfaces and memory management interfaces transparent to the client
  745.   program.  However, there are special cases where a program should be informed
  746.   as to which interfaces are implemented.  In such cases, the procedure
  747.   XLIBCONFIG (XLIB configuration) may be called to determine the how XLIB has
  748.   been configured by INITXLIB.
  749.        XLIB is terminated with INT 21H function 4CH (DOS termination) issued
  750.   either from real or protected mode.
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.                                         7
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.   Detailed Specifications
  769.  
  770.  
  771.   INITXLIB (Initialize XLIB)
  772.   Purpose:  Check for presence of XMS, DPMI, and VCPI, then configure XLIB to
  773.   operate with the installed interfaces.
  774.   CPU Mode:  Real
  775.   Registers at Call:  None
  776.   Return Registers:
  777.      AX = 0 if successful, in which event DX and EAX are zero as well.  The
  778.   configured interfaces may be identified by calling XLIBCONFIG (see below).
  779.      AX <> 0 if unsuccessful.  An XLIB error code is returned in AX.  A specific
  780.   error code is returned in DX and in the high word of EAX (EAH).  If AX = 10H
  781.   or 11H, then a DOS error code is returned.  If AX = DPMI error, then a DPMI
  782.   1.0 error code is returned (if provided by host).  If AX = VCPI error, then DX
  783.   = EAH = 0.  If AX = XMS error, then an XMS error code is returned.
  784.   Details:
  785.      If both DPMI and VCPI are present and if EMS is enabled, then XLIB will be
  786.   configured for DPMI if the zero bit of IFLAGS is clear.  If this bit is set,
  787.   then XLIB will be configured for VCPI.  The bit is clear by default.  If EMS
  788.   is disabled, then XLIB will always be configured for DPMI if it is present.
  789.      This routine will possibly attempt to allocate conventional memory.  The
  790.   amount of memory XLIB will attempt to allocate can be obtained by calling
  791.   XLIBMEMREQ (see below).  The general address range from which this memory is
  792.   allocated can be controlled by appropriately setting the high byte of IFLAGS.
  793.   Alternative values for this byte are presented in Table 2.  The byte has a
  794.   value of 2 by default.  This setting forces XLIB to allocate from the highest
  795.   available address in conventional memory.  Under certain settings of IFLAGS,
  796.   XLIB can also allocate from upper memory (DOS 5.0 or higher).
  797.      Descriptors are created for the segments loaded in CS, SS, DS, and ES as of
  798.   call to this routine.  The selectors for these descriptors are MAINCSSEL,
  799.   MAINSSSEL, MAINDSSEL, and MAINESSEL.  These descriptors are never used by
  800.   other XLIB procedures.  They are provided to augment development of protected-
  801.   mode libraries.  Protected-mode library routines may access the stack and data
  802.   of the main program through these descriptors.
  803.      INITXLIB should be called only once within a program.  Subsequent calls are
  804.   returned with no action.  XLIB is terminated by INT 21H function 4CH (DOS
  805.   termination) issued from either real or protected mode.
  806.  
  807.  
  808.   Table 2:  IFLAGS Settings for Memory Allocation Control
  809.   _____________________________________________________________________________
  810.   Bits 8-15     Implied Memory Allocation Location
  811.   ---------     ----------------------------------
  812.   00H           Lowest available address in conventional memory (00000H-9FFFFH)
  813.   01H           Best fit in conventional memory
  814.   02H           Highest available address in conventional memory
  815.   40H           Lowest available address in upper memory (A0000H-FFFFFH)
  816.   41H           Best fit in upper memory
  817.   42H           Highest available address in upper memory
  818.   81H           Follow strategy 40H by 00H if necessary
  819.   82H           Follow strategy 41H by 01H if necessary
  820.   83H           Follow strategy 42H by 02H if necessary
  821.   _____________________________________________________________________________
  822.  
  823.  
  824.  
  825.                                         8
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.   XLIBMEMREQ (XLIB Memory Requirements)
  833.   Purpose:  Find XLIB conventional memory requirements.
  834.   CPU Mode:  Real
  835.   Registers at Call:  None
  836.   Return Registers:
  837.      Sign bit of DX clear if successful.  Memory requirements in bytes are
  838.   returned in DX:AX.
  839.      Sign bit of DX set if unsuccessful.  An error code is returned in AX
  840.   (always a DOS error code).
  841.   Details:
  842.      DX:AX is adjusted upward to an integer multiple of 16.
  843.      If both DPMI and VCPI are present and if EMS is enabled, then XLIBMEMREQ
  844.   will assume that DPMI will be used if bit 0 of IFLAGS is clear (the default);
  845.   otherwise, VCPI is assumed.  If EMS is disabled, then DPMI will always be
  846.   assumed if present.  No additional conventional memory is needed if both DPMI
  847.   and VCPI are absent.
  848.      This routine will return DX:AX = 0 if XLIB contains free internal memory in
  849.   sufficient quantity to meet conventional memory demands.
  850.      This routine obtains DPMI requirements by calling DPMIMEMREQ (see below)
  851.   and VCPI requirements by calling VCPIMEMREQ.
  852.  
  853.   DPMIMEMREQ  (DPMI Memory Requirements)
  854.   Purpose:  Find DPMI conventional memory requirements.
  855.   CPU Mode:  Real
  856.   Registers at Call:  None
  857.   Return Registers:  DX:AX = conventional memory requirements.
  858.   Details:
  859.      DX:AX is adjusted upward to an integer multiple of 16.
  860.      This routine does not assume the presence of DPMI.  It will return DX:AX
  861.   set to zero if DPMI is absent.
  862.      This routine will return DX:AX = 0 if XLIB contains free internal memory in
  863.   sufficient quantity to meet the conventional memory demands of DPMI.
  864.  
  865.   VCPIMEMREQ (VCPI Memory Requirements)
  866.   Purpose:  Find VCPI conventional memory requirements.
  867.   CPU Mode:  Real
  868.   Registers at Call:  None
  869.   Return Registers:  DX:AX = conventional memory requirements.
  870.   Details:
  871.      DX:AX is adjusted upward to an integer multiple of 16.
  872.      This routine simply loads DX:AX with a constant approximately equal to
  873.   12Kb.
  874.  
  875.   XLIBCONFIG (XLIB Configuration)
  876.   Purpose:  Get XLIB configuration.
  877.   CPU Mode:  Real
  878.   Registers at Call:  None
  879.   Return Registers:  AX = 0 if protected-mode structures are not initialized;
  880.   otherwise, AX = XLIB configuration.  The value of lower nibble identifies the
  881.   protected-mode host/server.  If 1 then DPMI is installed.  If 2 then VCPI is
  882.   installed.  If 3 then XLIB handles mode switches.  Bit 4 is set if XMS is
  883.   installed.
  884.  
  885.  
  886.  
  887.  
  888.  
  889.                                         9
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.                                 4. Mode Switching
  897.  
  898.  
  899.        As illustrated in Example 1, CALLPM may be used to transfer control to
  900.   32-bit protected mode in segment TSEG.  When execution is returned to real
  901.   mode, segment registers, ESP, system flags, and control flags are restored to
  902.   their original values.
  903.        Execution may also be transferred to protected mode in TSEG with the
  904.   ENTERPM (enter protected mode) procedure.  This procedure is specially
  905.   designed to accommodate mixed-language programming with high-level languages
  906.   operating in real mode.  High-level language modules may be linked with
  907.   libraries containing protected-mode procedures.  These procedures may then be
  908.   called from high-level code.  However, such procedures must generally be
  909.   careful to restore register state.  ENTERPM restores register state as
  910.   required by Microsoft high-level languages.  In particular, ENTERPM restores
  911.   all registers except EAX, EDX, and the status flags.  EAX and EDX are not
  912.   restored because these are typically used by high-level languages for return
  913.   values.  ENTERPM otherwise functions exactly as CALLPM.
  914.        Both CALLPM and ENTERPM save register state as of call.  CALLPM and
  915.   ENTERPM will also save and restore the state of the floating point unit (FPU)
  916.   if requested.  FPU save/restore can be enabled by setting bit 2 of OFLAGS
  917.   (operation flags).  The bit is clear by default.  OFLAGS is a public WORD in
  918.   DSEG.
  919.        FPU state is saved with the FSAVE instruction executed from real mode.
  920.   This instruction resets the FPU; consequently, the FPU control word must be
  921.   redefined.  XLIB will therefore load FPUCW (FPU control word) to the FPU
  922.   control word after performing FSAVE.  FPUCW is a public WORD location in DSEG.
  923.        After entering protected mode through CALLPM or ENTERPM, control would
  924.   typically be returned to real mode with the RET instruction.  However, control
  925.   may also be returned to real mode by jumping to either RETPM or EXITPM.  These
  926.   are both procedures in TSEG.  They will successfully return control to real
  927.   mode regardless of the stack state; therefore, they can be useful for
  928.   debugging.  For example, an unidentified area of protected-mode code that is
  929.   causing the system to crash can be isolated by placing jumps to EXITPM at
  930.   alternative locations.
  931.        RETPM returns control to the real-mode caller of CALLPM/ENTERPM after
  932.   restoring segment registers, ESP, system flags, and control flags.  EXITPM
  933.   returns control to the real mode caller after restoring all registers except
  934.   EAX, EDX, and the status flags.
  935.        The return address placed on the stack by CALLPM is actually a near
  936.   return to RETPM.  Likewise, ENTERPM places a near return to EXITPM.  CALLPM
  937.   and ENTERPM are otherwise identical procedures.
  938.        Once within protected mode, far procedures in real mode can be called
  939.   using CALLRM.  CALLRM may be called only by protected-mode procedures in TSEG.
  940.        XLIB contains two hardware interrupt handlers that are typically
  941.   activated upon entry to protected mode.  These handlers are fully explained in
  942.   Chapter 6.  The first handler is hooked to the keyboard interrupt.  This
  943.   handler manages hot key detection.  The second handler is hooked to the FPU
  944.   interrupt and is designed to handle FPU exceptions.
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.                                         10
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.   Detailed Specifications
  961.  
  962.  
  963.   CALLPM (Call Protected Mode)
  964.   Purpose:  Call a protected-mode procedure in TSEG with near return.
  965.   CPU Mode:  Real
  966.   Registers at Call:  SS:ESP = 32-bit protected-mode target offset.
  967.   Return Registers:  Returns through RETPM.  See RETPM for details.
  968.   Details:
  969.      All CPU registers except EAX and EDX are saved at locations presented in
  970.   Table 3.  The stack is saved after the return address and argument have been
  971.   popped.
  972.      The target receives SS = TSEGDSEL with 1000H free bytes on the stack.  The
  973.   return address on the stack is a near return to RETPM.  The target receives by
  974.   default:  DS = FLATDSEL, ES = TSEGDSEL, FS = DSEGSEL, and GS = DGROUPSEL.
  975.   Other registers, including the status flags and interrupt flag, are received
  976.   at values as of call.
  977.      If bit 2 of OFLAGS is set, then the FPU state is also saved; the FPU is
  978.   initialized, and FPUCW is loaded to the control word.
  979.      XLIB hardware interrupt handlers are enabled (see Chapter 6).  However, if
  980.   bit 1 of OFLAGS is set, then the XLIB interrupt handler for the FPU will not
  981.   be enabled.
  982.      If an FPU exception occurs after CALLPM, and if the FPU exception handler
  983.   is enabled, then protected mode will be exited through EXITPM rather than
  984.   RETPM.  If FPU save/restore is not enabled, then real-mode will receive an
  985.   initialized FPU with control word set to the value existing as of the
  986.   exception.
  987.      The user may change the stack after the mode switch.
  988.      DS, ES, FS, and GS are actually loaded from:  PMDS, PMES, PMFS, and PMGS.
  989.   These are public WORD locations in DSEG and are initialized to the default
  990.   selectors by INITXLIB.  The user however may change these selectors to any
  991.   legal values after initialization.
  992.  
  993.   RETPM (Return From Protected Mode)
  994.   Purpose:  Return control to real mode with partial register restoration.
  995.   CPU Mode:  Protected
  996.   Registers at Call:  None
  997.   Return Registers:  No return
  998.   Details:
  999.      RETPM switches to real mode and then restores all segment registers, ESP,
  1000.   system flags, and control flags to values as of call to either CALLPM or
  1001.   ENTERPM.  XLIB hardware interrupt handlers are disabled (see Chapter 6).
  1002.   Control is then transferred to the real-mode return address as of call to
  1003.   CALLPM/ENTERPM.
  1004.      If bit 2 of OFLAGS is set, then RETPM also restores FPU state.
  1005.      RETPM will successfully execute regardless of stack state.
  1006.  
  1007.   ENTERPM (Enter Protected Mode)
  1008.   Purpose:  Call a protected mode procedure in TSEG with near return.
  1009.   CPU Mode:  Real
  1010.   Registers at Call:  SS:ESP = 32-bit protected-mode target offset.
  1011.   Return Registers:  Returns through EXITPM.  See EXITPM for details.
  1012.   Details:  This routine executes exactly as CALLPM except that a near return to
  1013.   EXITPM is placed on the stack rather than to RETPM.
  1014.  
  1015.  
  1016.  
  1017.                                         11
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.   EXITPM (Exit Protected Mode)
  1025.   Purpose:  Return control to real mode with general register restoration.
  1026.   CPU Mode:  Protected
  1027.   Registers at Call:  None
  1028.   Return Registers:  No return
  1029.   Details:
  1030.      EXITPM switches to real mode and then restores all registers except EAX,
  1031.   EDX, and status flags to values as of call to either CALLPM or ENTERPM.  XLIB
  1032.   hardware interrupt handlers are disabled (see Chapter 6).  Control is then
  1033.   transferred to the real-mode return address as of call to CALLPM/ENTERPM.
  1034.      If bit 2 of OFLAGS is set, then EXITPM also restores FPU state.
  1035.      EXITPM will successfully execute regardless of stack state.
  1036.      The FPU exception handler performs a jump to EXITPM upon occurrence of any
  1037.   unmasked FPU exception.
  1038.  
  1039.  
  1040.   Table 3:  CALLPM/ENTERPM Register Storage Locations by Public Symbol
  1041.   _____________________________________________________________________________
  1042.   Register           Symbol              Symbol Type
  1043.   --------           ------              -----------
  1044.   EBX                ORGEBX              DWORD
  1045.   ECX                ORGECX              DWORD
  1046.   ESI                ORGESI              DWORD
  1047.   EDI                ORGEDI              DWORD
  1048.   EBP                ORGEBP              DWORD
  1049.   ESP                ORGESP              DWORD
  1050.   EFLAGS             ORGEFLAGS           DWORD
  1051.   SS                 ORGSS               WORD
  1052.   DS                 ORGDS               WORD
  1053.   ES                 ORGES               WORD
  1054.   FS                 ORGFS               WORD
  1055.   GS                 ORGGS               WORD
  1056.   FPU State          ORGFPU              BYTE[94]
  1057.   _____________________________________________________________________________
  1058.  
  1059.  
  1060.   CALLRM (Call a Real-Mode Procedure)
  1061.   Purpose:  Call a real-mode routine with far return from protected mode.
  1062.   CPU Mode:  Protected
  1063.   Registers at Call:  SS:ESP = far real-mode target address (four bytes).
  1064.   Return Registers:  All segment registers and ESP are restored.  Other
  1065.   registers, including status flags and the interrupt flag, are received with
  1066.   values as of the real-mode RET instruction.
  1067.   Details:
  1068.      This is a near procedure in TSEG.  It must therefore be called from TSEG.
  1069.      Segment registers and ESP are saved at locations presented in Table 4.  The
  1070.   stack is saved after popping the return address and argument.
  1071.      The target receives the XLIB real-mode stack (SS = DSEG) with 200H free
  1072.   bytes.  By default, the target receives DS = DGROUP, ES = DSEG, FS = DSEG, and
  1073.   GS = DSEG.  Other registers, including status flags and the interrupt flag,
  1074.   are received at values as of call.
  1075.      Code called by this routine should not perform calls to XLIB procedures
  1076.   other than SWITCHPM and SWITCHRM (see Appendix G).  Most real-mode procedures
  1077.  
  1078.  
  1079.  
  1080.  
  1081.                                         12
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.   in XLIB issue calls to CALLPM; however, neither CALLPM nor ENTERPM are
  1089.   reentrant.  For example, you cannot nest CALLPM/RETPM pairs.
  1090.      DS and ES are actually loaded from RMDS and RMES.  These are public WORD
  1091.   locations in DSEG and are initialized to the default values.  However, the
  1092.   user may change these values if desired.
  1093.      This procedure does not change values in OFLAGS; consequently, XLIB
  1094.   hardware interrupt handlers remain enabled in real mode if they were enabled
  1095.   as of call (see Chapter 6).
  1096.      FPU exceptions in real mode are handled the same as in protected mode;
  1097.   however, system software is less apt to be left in regular state after real-
  1098.   mode exceptions.  FPU instructions should therefore be executed in protected
  1099.   mode where possible.
  1100.  
  1101.  
  1102.   Table 4:  CALLRM Register Storage Locations by Public Symbol
  1103.   _____________________________________________________________________________
  1104.   Register           Symbol              Symbol Type
  1105.   --------           ------              -----------
  1106.   ESP                CALLESP             DWORD
  1107.   SS                 CALLSS              WORD
  1108.   DS                 CALLDS              WORD
  1109.   ES                 CALLES              WORD
  1110.   FS                 CALLFS              WORD
  1111.   GS                 CALLGS              WORD
  1112.   _____________________________________________________________________________
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.                                         13
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.                              5. Inline Mode Switching
  1153.  
  1154.  
  1155.        XLIB includes two routines to perform mode switching in C and C++
  1156.   programs using inline assembly.  These procedures are very versatile and
  1157.   simple.  They switch the processor between real mode and 16-bit protected
  1158.   mode.  A third routine is included to facilitate calls from 16-bit protected-
  1159.   mode to 32-bit near procedures in TSEG.
  1160.        Call INLINEPM to switch to 16-bit protected mode.  Descriptors are
  1161.   automatically created for CS, SS, and DS.  These registers are returned
  1162.   containing selectors to the respective descriptors.  ES is returned containing
  1163.   DSEGSEL.  ES may therefore be used to load other XLIB selectors to segment
  1164.   registers.
  1165.        Call INLINERM to return to real-mode.  This function should be called
  1166.   from the same segment as the previous call to INLINEPM and should be using the
  1167.   same stack segment.  INLINERM restores segment registers to values which
  1168.   existed as of the call to INLINEPM.
  1169.        The following Microsoft C 7.0 program illustrates the usage of these
  1170.   procedures.  The program contains a C subroutine called "getextmem" which uses
  1171.   INLINEPM and INLINERM to retrieve a DWORD from extended memory.
  1172.        Since the Microsoft C 7.0 compiler does not recognize 32-bit assembly
  1173.   instructions, the following program attains access to 32-bit registers by
  1174.   encoding prefixes in front of 16-bit instructions.  These prefixes are
  1175.   inserted with the _emit directive.  Also observe that INLINERM is called
  1176.   indirect through a supplied pointer in DSEG called INLINERMPTR.  This is done
  1177.   to ensure that the intersegment call loads CS with the protected-mode selector
  1178.   for CSEG (CSEGSEL) rather than with the segment constant.
  1179.        A Borland C version of this program is contained in the file EXAMP2B.C.
  1180.   The Borland version is somewhat simpler than the following program because 32-
  1181.   bit registers can be used in Borland C provided that TASM is used to perform
  1182.   the assembly.
  1183.        This program may fail if an attempt is made to access logical addresses
  1184.   which are either protected or undefined in the page tables.
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199.  
  1200.  
  1201.  
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.                                         14
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.   Example 2:  Using INLINEPM/INLINERM in C
  1217.   _____________________________________________________________________________
  1218.   #include <stdio.h>
  1219.   #include <xlib.h>
  1220.   #define som _emit 0x66               /*Switch operand mode*/
  1221.   #define sam _emit 0x67               /*Switch address mode*/
  1222.   long __far getextmem(long address);
  1223.   int goterr = 0;                      /*Error flag*/
  1224.  
  1225.   void main(void)
  1226.   {
  1227.     long l, xaddress;
  1228.     l = INITXLIB();                    /*Initialize XLIB*/
  1229.     if(l != 0)                         /*See if an error occurred*/
  1230.     {
  1231.       printf("Library initialization error:  %lX\n",l);
  1232.       return;
  1233.     }
  1234.  
  1235.     xaddress = 0x100000;               /*Read first dword in 2ond meg*/
  1236.     l = getextmem(xaddress);           /*See if an error occurred*/
  1237.     if(goterr != 0)
  1238.     {
  1239.       printf("Inline mode-switch error:  %lX\n",l);
  1240.       return;
  1241.     }
  1242.     printf("[%lX] = %lX\n",xaddress,l);
  1243.   }
  1244.  
  1245.   long __far getextmem(long address)
  1246.   {
  1247.     __asm
  1248.     {
  1249.       som                             ;mov  eax,[bp+6]
  1250.       mov       ax,[bp+6]             ;  ""
  1251.       call      INLINEPM              ;Switch to 16-bit protected mode
  1252.       jc        error                 ;Error code in ax
  1253.       mov       ds,es:FLATDSEL        ;Switch to flat data model
  1254.       sam                             ;push dword ptr [eax]
  1255.       som                             ;  ""
  1256.       _emit     0ffh                  ;  ""
  1257.       _emit     030h                  ;  ""
  1258.       pop       ax                    ;Return address contents in dx:ax
  1259.       pop       dx
  1260.       call      es:INLINERMPTR        ;Switch back to real mode by calling
  1261.       jmp       done                  ;INLINERM indirect.  A direct call would
  1262.                                       ;load cs with an invalid value.
  1263.   error:
  1264.       xor       dx,dx                 ;Return error code in dx:ax
  1265.       inc       goterr                ;Set error flag
  1266.   done:
  1267.     }
  1268.   }
  1269.   _____________________________________________________________________________
  1270.  
  1271.  
  1272.  
  1273.                                         15
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.   Detailed Specifications
  1281.  
  1282.  
  1283.   INLINEPM (Inline Protected-Mode)
  1284.   Purpose:  Return to real-mode caller in 16-bit protected mode.
  1285.   CPU Mode:  Real
  1286.   Registers at Call:  None
  1287.   Return Registers:
  1288.      CF clear if successful.  Descriptors are created for CS, SS, and DS.  These
  1289.   descriptors have base addresses matching the calling contents of the
  1290.   respective segment registers.  The segment registers are returned containing
  1291.   selectors to these descriptors.  These selectors are also stored in DSEG at
  1292.   the public WORD locations ILCSSEL, ILSSSEL, and ILDSSEL.  See Chapter 2 for
  1293.   further details as to descriptor specifications.  ES, FS, and GS are returned
  1294.   containing DSEGSEL.  Protected mode receives other registers, except the
  1295.   status flags, at values as of call.  System flags (including the interrupt
  1296.   flag) and control flags are preserved through the call.
  1297.      CF set if unsuccessful.  The processor will still be in real mode.
  1298.   Unsuccessful execution can occur only under DPMI.  EAX is returned with an
  1299.   error code.  AX = XLIB error code.  The high word of EAX will equal a DPMI 1.0
  1300.   error code (if provided by host).  Other registers, except the status flags,
  1301.   are unchanged.
  1302.   Details:
  1303.      INLINEPM stores segment registers at the same locations used by CALLPM and
  1304.   ENTERPM (Table 1).  INLINERM (see below) then restores these registers.  C
  1305.   will require that other registers be preserved also; however, the user is
  1306.   responsible for managing these.
  1307.      XLIB hardware interrupt handlers are not activated by this procedure (see
  1308.   Chapter 6).  The keyboard handler may be enabled by the user if hot key
  1309.   detection is needful; however, the FPU exception handler should never be
  1310.   enabled.  Therefore, hot key detection should be enabled by setting both bits
  1311.   0 and 1 in OFLAGS.
  1312.      SP is preserved through the mode switch; however, the high word of ESP is
  1313.   set to zero.  The high word must be cleared since SS is set to a descriptor
  1314.   having FFFFH limit and having its big bit set.
  1315.      If multiple calls are made to INLINEPM with no changes to CS, SS, and DS,
  1316.   then descriptors are created only on the first call.  Subsequent calls will
  1317.   therefore execute more quickly.
  1318.  
  1319.   INLINERM (Inline Real-Mode)
  1320.   Purpose:  Return to 16-bit protected-mode caller in real mode.
  1321.   CPU Mode:  16-bit protected mode
  1322.   Registers at Call:  CS and SS must equal values as of return from INLINEPM.
  1323.   Return Registers:  Segment registers are restored to values existing as of
  1324.   former call to INLINEPM.  Real mode receives other registers, except status
  1325.   flags, at values as of call.  System flags (including the interrupt flag) and
  1326.   control flags are preserved through the call.
  1327.   Details:
  1328.      Since INLINERM will be called intersegment, caution must be taken that CS
  1329.   is loaded with CSEGSEL and not CSEG.  XLIB provides a far pointer to this
  1330.   procedure called INLINERMPTR which may be used to execute the call.
  1331.      INLINERM and CALL32 are the only 16-bit protected-mode procedures in XLIB.
  1332.   They are also the only protected-mode procedures having far returns.
  1333.  
  1334.  
  1335.  
  1336.  
  1337.                                         16
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.   CALL32 (Call 32-bit Protected-Mode)
  1345.   Purpose:  Call a 32-bit protected-mode near procedure in segment TSEG from 16-
  1346.   bit protected-mode.
  1347.   CPU Mode:  16-bit protected mode
  1348.   Registers at Call:  SS:ESP =  32-bit protected-mode target offset.
  1349.   Return Registers:  All registers, including status flags, are returned with
  1350.   values existing as of the 32-bit RET instruction.
  1351.   Details:
  1352.      CALL32 is a far procedure; therefore, caution must be taken that calls to
  1353.   CALL32 load CS with CSEGSEL instead of CSEG.  XLIB provides a far pointer to
  1354.   this procedure in DSEG called CALL32PTR which may be used to execute the call.
  1355.      CALL32 and INLINERM are the only 16-bit protected-mode procedures in XLIB.
  1356.   They are also the only protected-mode procedures having far returns.
  1357.      This procedure does not alter the state of OFLAGS.
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.                                         17
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.                              6. Interrupt Management
  1409.  
  1410.  
  1411.        Interrupt management is the most complicated task performed by XLIB.
  1412.   Accordingly, this chapter is the most difficult section of the user's manual.
  1413.   In general, this chapter may be ignored for programs which do not install
  1414.   interrupt handlers, do not require hot key detection, and do not use floating
  1415.   point operations.
  1416.  
  1417.  
  1418.   XLIB Interrupt Handlers
  1419.  
  1420.  
  1421.        XLIB handles nearly all interrupts occurring in protected mode by
  1422.   shifting to real mode and calling the currently installed real-mode interrupt
  1423.   handlers.  In all but three cases, XLIB uses inherited real-mode handlers.
  1424.   XLIB installs its own handlers only for the DOS interrupt (INT 21H), the
  1425.   keyboard interrupt (INT 9), and the FPU interrupt (INT 75H).
  1426.        The DOS interrupt handler (INT 21H) intercepts all DOS calls and
  1427.   determines if termination is requested (AH = 4CH).  If not, then the interrupt
  1428.   is immediately cascaded to DOS.  If so, then XLIB performs housecleaning
  1429.   before relaying the request to DOS.  Under DPMI, XLIB will restore all
  1430.   interrupt vectors and release all allocated descriptors.  The DPMI host
  1431.   automatically releases all allocated memory.  Under other configurations, XLIB
  1432.   will reset all real-mode interrupt vectors and release all allocated extended
  1433.   memory.
  1434.        INT 21H function 4CH may be executed in either real mode or protected
  1435.   mode.  DPMI hosts will expect this function to be executed from protected
  1436.   mode; consequently, XLIB will switch to protected mode before relaying the
  1437.   request to DPMI.  XLIB also installs a protected-mode handler for INT 21H
  1438.   under DPMI.  This is to ensure that XLIB will see the termination request
  1439.   before the DPMI host.
  1440.        The keyboard interrupt handler is intended to facilitate termination of
  1441.   protected-mode procedures with a keypress.  When a user-defined hot key is
  1442.   pressed, the keyboard interrupt handler will modify a flag variable.  This
  1443.   flag variable may then be polled periodically in the main thread of execution.
  1444.   The keyboard interrupt handler is a real-mode routine.
  1445.        The inconvenience of having to poll the flag variable is unfortunately
  1446.   necessary.  It is not generally safe to terminate within a hardware interrupt
  1447.   handler, particularly in protected-mode environments.  A hardware interrupt
  1448.   generated by a keypress may have interrupted system software in the middle of
  1449.   a system maintenance operation.  Termination in such cases would likely leave
  1450.   the system in an irregular and potentially unstable state.
  1451.        Matters are further complicated in most protected-mode environments.  For
  1452.   example, a DPMI host will trap all hardware interrupts before cascading them
  1453.   to interrupt handlers.  If control is not returned to the host with the IRET
  1454.   instruction, then the host will be left in an irregular state.  Moreover, the
  1455.   trapping procedure will likely switch stacks before relaying the interrupt to
  1456.   the handler; consequently, the handler cannot determine the final return
  1457.   address and therefore cannot change this address to a termination procedure.
  1458.   These complications will nearly always occur when hardware interrupts are
  1459.   virtualized.  Virtual 8086 mode interrupts will almost certainly be
  1460.   virtualized in either VCPI-based or DPMI-based environments.
  1461.  
  1462.  
  1463.  
  1464.  
  1465.                                         18
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.        When a key is pressed, the keyboard interrupt handler will examine the
  1473.   key to determine if it is the hot key.  If not, then the interrupt is cascaded
  1474.   to the inherited real-mode handler.  If so, then a hot key flag is recorded to
  1475.   a DWORD whose linear address is recorded at CCODEPTR (condition code pointer).
  1476.   The hot key is not cascaded.  CCODEPTR is a public DWORD in DSEG.  The hot key
  1477.   flag is listed among XLIB error codes in Appendix B.
  1478.        CCODEPTR initially contains the linear address to public DWORD location
  1479.   CCODE which is in DSEG.  Therefore, the hot key flag would be written to CCODE
  1480.   by default.  CCODEPTR may be changed by the user; however, it must point to an
  1481.   address in conventional memory.
  1482.        The hot key specification is stored in DSEG at a public WORD location
  1483.   called HOTKEY.  The lower byte of HOTKEY contains the scan code of the hot key
  1484.   while the upper byte specifies the state of the shift keys.  Bit 8 specifies
  1485.   the state of SHIFT; bit 9 specifies CTRL, and bit 10 specifies ALT.  All other
  1486.   bits are ignored.  Set bits require that the designated shift key be pressed.
  1487.   The default setting for HOTKEY is zero.  This setting disables hot key
  1488.   detection since no key has a zero scan code.
  1489.        The XLIB interrupt handler for FPU exceptions performs three major
  1490.   functions upon the occurrence of any FPU exception that is unmasked in the FPU
  1491.   control word.  First, an error code is loaded to EAX and is also recorded at
  1492.   the linear address in CCODEPTR.  Next, the FPU is initialized with FNINIT (the
  1493.   FPU control word is preserved).  Third, control is transferred to EXITPM to
  1494.   return to real mode.  The high word stored in EAX and in the condition code
  1495.   will be the FPU status word.  This word may be examined to determine the
  1496.   nature of the exception.  The FPU interrupt handler is a real-mode routine.
  1497.        The response of the FPU to exception conditions is largely determined by
  1498.   the settings in the FPU control word.  If FPU save/restore is enabled, then
  1499.   the FPU control word will be set to FPUCW upon execution of CALLPM/ENTERPM.
  1500.   The default value for FPUCW is 0332H.  This sets rounding control to nearest,
  1501.   precision control to 64 bits, and unmasks exceptions for overflow, zero
  1502.   divide, and invalid operations.  Exceptions for underflow, precision, and
  1503.   denormalized operations are masked, and are therefore handled internally by
  1504.   the FPU.  FPUCW may be modified by the user.
  1505.        As explained above, the machine may be left in an unstable state after a
  1506.   program has been terminated from within a hardware interrupt handler.  This
  1507.   can also be the case for the FPU interrupt handler.  However, it will be safe
  1508.   to continue execution after FPU exceptions under clean configurations.  This
  1509.   follows because interrupts are not virtualized and because the exception will
  1510.   never be generated in the operating system (DOS seldom uses the FPU).  Nor
  1511.   should there be any problem with continuing execution after an exception in
  1512.   protected-mode under VCPI.  This follows since protected-mode interrupts
  1513.   cannot be virtualized under VCPI.  Exceptions occurring in virtual 8086 mode
  1514.   or under DPMI protected mode may however leave the machine in an irregular
  1515.   state.  Reboot may be necessary in these cases.
  1516.        All tested DPMI hosts appear to be restored to normal state by execution
  1517.   of INT 21H function 4CH (DOS termination).  However, the DPMI specifications
  1518.   offer no guarantees to this approach.  FPU exceptions in virtual 8086 mode are
  1519.   generally no problem in single-task environments; however, they will
  1520.   frequently prove problematic under a multitasker such as DESQview.
  1521.        Bit 0 of OFLAGS is used by XLIB to simultaneously enable or disable all
  1522.   of its own hardware interrupt handlers.  Setting the bit enables the handlers.
  1523.   XLIB sets this bit upon calls to CALLPM/ENTERPM and clears the bit upon
  1524.   return.  All interrupts are immediately cascaded to the inherited real-mode
  1525.   handlers when the bit is clear.  Therefore, FPU exceptions are handled by XLIB
  1526.  
  1527.  
  1528.  
  1529.                                         19
  1530.  
  1531.  
  1532.  
  1533.  
  1534.  
  1535.  
  1536.   only after calls to CALLPM/ENTERPM.  Accordingly, hot key detection is enabled
  1537.   only after calls to CALLPM/ENTERPM.  The user may set the bit under other
  1538.   circumstances; however, XLIB will not be able to properly handle FPU
  1539.   exceptions in these cases.  The bit should be set by the user only when hot
  1540.   key detection is desired and when it is certain that the FPU exception handler
  1541.   will not be invoked.
  1542.        The FPU exception handler may be permanently disabled by setting bit 1 in
  1543.   OFLAGS.  If this bit is set, then the FPU interrupt handler simply cascades
  1544.   the interrupt to the inherited real-mode handler.  XLIB will set the bit
  1545.   during initialization if an FPU is not present; it is otherwise cleared.
  1546.        Real-mode software interrupts which receive or return values in segment
  1547.   registers cannot be used within protected mode because the deflection routine
  1548.   will restore selectors to segment registers upon completion of the interrupt.
  1549.   To use such software interrupts, one must switch to real mode through CALLRM;
  1550.   issue the interrupt, and then transfer the segment registers to other
  1551.   registers or to memory before returning to protected mode.
  1552.        Certain software interrupts use status flags for return flags, typically
  1553.   to signal error conditions.  This is particularly the case for DOS interrupts.
  1554.   The deflection routine will pass these alterations to the code which issued
  1555.   the interrupt.
  1556.  
  1557.  
  1558.   Installation of Interrupt Handlers
  1559.  
  1560.  
  1561.        The user may install real-mode interrupt handlers in usual fashion.  Such
  1562.   handlers will also receive interrupts occurring in protected mode because
  1563.   either XLIB or the DPMI host will deflect all protected-mode interrupts.  If
  1564.   XLIB deflects the interrupt, then the handler will receive SS = DSEG with ESP
  1565.   set to 200H free bytes.  Stack sizes under DPMI will depend upon the host, but
  1566.   must contain a minimum of 200H free bytes to meet DPMI specifications.
  1567.        The DOS routines to get and set interrupt vectors (INT 21H functions 35H
  1568.   and 25H) receive and return values through segment registers; consequently,
  1569.   they cannot be used in protected mode.  Instead, use the XLIB procedures
  1570.   PMGETRMIV and PMSETRMIV (protected mode - get/set real-mode interrupt vector).
  1571.        The user may install a protected-mode interrupt handler from real mode by
  1572.   calling SETPMIV (set protected-mode interrupt vector).  The current protected-
  1573.   mode interrupt vector may be obtained by calling GETPMIV.
  1574.        From protected mode, interrupt vectors can be managed with PMSETPMIV and
  1575.   PMGETPMIV.  These procedures have the same specifications as SETPMIV and
  1576.   GETPMIV.
  1577.        Interrupt handlers installed with SETPMIV/PMSETPMIV are never disabled by
  1578.   XLIB and will therefore always be active under protected-mode execution.
  1579.   These handlers will not be active under real-mode execution in the absence of
  1580.   DPMI.  That is, real-mode interrupts are never deflected to protected-mode
  1581.   handlers in such environments.  However if DPMI is active, then all hardware
  1582.   interrupts (IRQs 0-15) and the software interrupts:  1CH (BIOS timer tick),
  1583.   23H (DOS CTRL C), and 24H (DOS critical error) are deflected from real mode to
  1584.   the installed protected-mode handler.  Therefore the protected-mode handler
  1585.   always receives the interrupt first.  If the handler cascades the interrupt,
  1586.   then the real-mode handler will receive it next.  If the user has not
  1587.   installed protected-mode handlers for these interrupts, then they are serviced
  1588.   by default handlers in the DPMI host.  The default handlers typically deflect
  1589.   the interrupts to the real-mode handlers.
  1590.  
  1591.  
  1592.  
  1593.                                         20
  1594.  
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.        If the programmer wishes to install a protected-mode interrupt handler
  1601.   for a hardware interrupt or for INT 1CH, INT 23H, or INT 24H, then
  1602.   consideration must be given to the fact that treatment of these interrupts
  1603.   will differ under different protected-mode configurations.  As noted above,
  1604.   the DPMI host will always send these interrupts to the protected-mode handler
  1605.   regardless of the CPU mode in which the interrupt occurred.  If DPMI is not
  1606.   installed, then protected-mode handlers receive control only under protected-
  1607.   mode interrupts.  Therefore, if the protected-mode handler is to receive real-
  1608.   mode interrupts under such configurations, the programmer must install a real-
  1609.   mode handler to perform the deflection.
  1610.        XLIB includes a procedure called DEFLECTPM which can be used within a
  1611.   real-mode interrupt handler to deflect control to a protected-mode handler.
  1612.   DEFLECTPM functions only under VCPI and XLIB mode switching.  If DPMI is
  1613.   installed, then the procedure returns with no action.  The intent of this
  1614.   procedure is to enable simulation of DPMI treatment of hardware interrupts,
  1615.   INT 1CH, INT 23H, and INT 24H.
  1616.        Observe that if the real-mode handler deflects to the protected-mode
  1617.   handler, then the latter should not cascade the interrupt since an infinite
  1618.   loop would result.  This follows because the initial protected-mode handler
  1619.   deflects to the real-mode handler.
  1620.        Were one to use DEFLECTPM in a real-mode handler for the interrupts named
  1621.   above, then the protected-mode handler will receive all interrupts regardless
  1622.   of the protected-mode configuration.  This occurs naturally under DPMI.
  1623.   DEFLECTPM ensures that it will occur under other configurations.  Observe that
  1624.   under DPMI, the real-mode handler will never be executed.
  1625.        A real-mode interrupt handler may need to perform shifts to protected
  1626.   mode in order to gain access to extended memory.  This mode switch should not
  1627.   be performed with CALLPM or ENTERPM if there is any possibility that the
  1628.   interrupt originated in protected mode or during a call through CALLRM.  This
  1629.   follows because CALLPM and ENTERPM are not reentrant; consequently, calls to
  1630.   these procedures cannot be nested.  The same may be said of INLINEPM and
  1631.   INLINERM.  Use the SWITCHPM and SWITCHRM procedures discussed in Appendix G
  1632.   for this purpose.
  1633.        Real-mode hardware interrupt handlers which perform shifts to protected
  1634.   mode should be installed after the call to INITXLIB.  Otherwise, the first
  1635.   interrupt may occur before protected-mode structures and code have been
  1636.   initialized.
  1637.        Interrupt handler should never call XLIB procedures apart from DEFLECTPM,
  1638.   SWITCHPM, and SWITCHRM.  This limitation applies to handlers in either CPU
  1639.   mode.
  1640.        It is sometimes important that interrupt handlers execute in shortest
  1641.   possible CPU time.  This would typically be the case for handlers of the
  1642.   communication ports.  Since mode switching is time consuming, such handlers
  1643.   should be installed for the CPU mode which is expected to receive the most
  1644.   interrupts.
  1645.        If DPMI is installed, then it is possible for multiple clients to operate
  1646.   in a single virtual machine.  In such cases, DPMI will always send hardware
  1647.   interrupts to the primary client (the most recently installed client in the
  1648.   virtual machine).
  1649.        Under DPMI, all protected-mode handlers for hardware interrupts and
  1650.   software interrupts 0-7 will receive control with interrupts disabled.  Since
  1651.   DPMI virtualizes the interrupt flag, the IRET instruction may not reenable
  1652.   interrupts.  Consequently, all handlers for these interrupts should execute
  1653.  
  1654.  
  1655.  
  1656.  
  1657.                                         21
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.   STI before executing IRET.  Other protected-mode interrupts do not affect the
  1665.   interrupt flag.
  1666.        All real-mode interrupt handlers will receive control with interrupts
  1667.   disabled regardless of the protected-mode configuration.  All protected-mode
  1668.   handlers will receive control with interrupts disabled under VCPI or XLIB mode
  1669.   switching.  However, if DPMI is installed, then protected-mode software
  1670.   interrupts apart from 0-7 will receive the virtual interrupt flag at its value
  1671.   as of the INT instruction.  That is, DPMI does not alter the interrupt flag in
  1672.   these cases.
  1673.        Hardware interrupts IRQ 0 through IRQ 7 are typically assigned to
  1674.   interrupt numbers 08H through 0FH, while IRQs 8 through 15 are typically
  1675.   assigned interrupt numbers 70H through 77H.  However, IRQs are remapped in
  1676.   some operating environments, typically to facilitate exception handling.
  1677.   XLIBE will in fact remap hardware interrupts in some situations (see Chapter
  1678.   11).  The current mappings may be loaded from IRQ0INTNO (IRQ 0 interrupt
  1679.   number) and IRQ8INTNO.  These are public BYTE locations in DSEG.  They should
  1680.   be read only after the call to INITXLIB.
  1681.        DESQview does remap hardware interrupts; however, its interrupt handlers
  1682.   for the new locations generally transfer control to the addresses at the
  1683.   conventional vectors.  DESQview must be started with a command-line switch if
  1684.   it is to accommodate certain hardware interrupts.  In particular, the FPU
  1685.   interrupt will not function properly under DESQview unless DESQview is started
  1686.   with DV /HW:75:C.
  1687.  
  1688.  
  1689.   Detailed Specifications
  1690.  
  1691.  
  1692.   PMGETRMIV (Protected Mode - Get Real-Mode Interrupt Vector)
  1693.   Purpose:  Retrieve address of real-mode interrupt handler.
  1694.   CPU Mode:  Protected
  1695.   Registers at Call:  AL = interrupt number.
  1696.   Return Registers:  Handler address returned in CX:DX.
  1697.   Details:  The DOS routine for this purpose (INT 21H function 35H) is not
  1698.   useful because it returns a value in ES.
  1699.  
  1700.   PMSETRMIV (Protected Mode - Set Real-Mode Interrupt Vector)
  1701.   Purpose:  Set address of real-mode interrupt handler.
  1702.   CPU Mode:  Protected
  1703.   Registers at Call:  AL = interrupt number, CX:DX = address of handler.
  1704.   Return Registers:  None
  1705.   Details:
  1706.      The DOS routine for this purpose (INT 21H function 25H) is not useful
  1707.   because it requires an argument in DS.
  1708.      Real-mode interrupt handlers will also be called when interrupts occur in
  1709.   protected mode provided that the protected-mode interrupt handler cascades the
  1710.   interrupt.  The default protected-mode handlers do in fact cascade all
  1711.   interrupts.
  1712.      XLIB never disables handlers installed by this procedure.
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.                                         22
  1722.  
  1723.  
  1724.  
  1725.  
  1726.  
  1727.  
  1728.   GETPMIV (Get Protected-Mode Interrupt Vector)
  1729.   Purpose:  Retrieve address of protected-mode interrupt handler from interrupt
  1730.   descriptor table.
  1731.   CPU Mode:  Real
  1732.   Registers at Call:  AL = interrupt number.
  1733.   Return Registers:  Handler address returned in CX:EDX (CX is a selector).
  1734.   Details:  This routine does not return addresses of CPU exception handlers
  1735.   under DPMI.  Use DPMI functions directly for this purpose.
  1736.  
  1737.   SETPMIV (Set Protected-Mode Interrupt Vector)
  1738.   Purpose:  Set address of protected-mode interrupt handler in interrupt
  1739.   descriptor table.
  1740.   CPU Mode:  Real
  1741.   Registers at Call:  AL = interrupt number.  Handler address in CX:EDX (CX is a
  1742.   selector).
  1743.   Return Registers:  EAX = 0 if successful.  EAX = error code if unsuccessful.
  1744.   AX = XLIB error code.  If DPMI is installed then the high word of EAX will
  1745.   contain a DPMI 1.0 error code (if provided by host).
  1746.   Details:
  1747.      XLIB never disables handlers installed by this procedure.
  1748.      Protected-mode interrupt handlers never receive interrupts occurring in
  1749.   real mode unless DPMI is active.  Under DPMI all hardware interrupts (IRQs 0-
  1750.   15) and software interrupts 1CH, 23H, and 24H are deflected from real mode to
  1751.   the installed protected-mode handler.  The protected-mode handler therefore
  1752.   receives control of the interrupt first.  It may cascade the interrupt if so
  1753.   desired, in which event, the real-mode handler receives the interrupt next. If
  1754.   no protected-mode handler has been installed, then DPMI generally deflects the
  1755.   interrupt to the real-mode handler.
  1756.      All protected-mode handlers will receive control with interrupts disabled
  1757.   unless DPMI is installed.  Under DPMI, protected-mode software interrupts
  1758.   apart from 0-7 do not alter the state of the virtual interrupt flag.
  1759.      Protected-mode handlers under DPMI for hardware interrupts and software
  1760.   interrupts 0-7 should execute STI before IRET to ensure that the virtual
  1761.   interrupt flag is enabled.
  1762.      If multiple DPMI clients are running in the same virtual machine, then the
  1763.   primary client (most recently installed client) always receives hardware
  1764.   interrupts.
  1765.      If DPMI .9 is installed, then protected-mode interrupt vectors should be
  1766.   reset to original values before termination.  Such action is not required for
  1767.   DPMI 1.0.
  1768.      This routine should not be used to install CPU exception handlers under
  1769.   DPMI.  DPMI functions should be used for this purpose.
  1770.  
  1771.   PMGETPMIV (Protected Mode - Get Protected-Mode Interrupt Vector)
  1772.   Purpose:  Retrieve address of protected-mode interrupt handler from interrupt
  1773.   descriptor table.
  1774.   CPU Mode:  Protected
  1775.   Details:  This routine is the protected-mode version of GETPMIV.  See GETPMIV
  1776.   for specifications.
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.                                         23
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.   PMSETPMIV (Protected Mode - Set Protected-Mode Interrupt Vector)
  1793.   Purpose:  Set address of protected-mode interrupt handler in interrupt
  1794.   descriptor table.
  1795.   CPU Mode:  Protected
  1796.   Details:  This routine is the protected-mode version of SETPMIV.  See SETPMIV
  1797.   for specifications.
  1798.  
  1799.   DEFLECTPM (Deflect to Protected-Mode)
  1800.   Purpose:  Call a protected-mode interrupt handler.
  1801.   CPU Mode:  Real
  1802.   Registers at Call:  SS:ESP = interrupt number (2 bytes)
  1803.   Return Registers:  Segment registers are preserved through the call.  The
  1804.   interrupt flag is also preserved through the call.  All other registers are
  1805.   returned at values existing as of the protected-mode IRET instruction.
  1806.   Details:
  1807.      The protected-mode handler will always receive interrupts disabled.  It may
  1808.   enable interrupts if desired; however, once control is returned to DEFLECTPM,
  1809.   the interrupt flag will be reset to its original value.
  1810.      This procedure can be used only under VCPI and XLIB mode switching.  It
  1811.   returns with no action under DPMI.  The routine is intended to simulate DPMI
  1812.   treatment of hardware interrupts, INT 1CH, INT 23H, and INT24H.  The routine
  1813.   may be called from a real-mode interrupt handler to deflect the interrupt to
  1814.   the protected-mode handler.
  1815.      The protected-mode handler should not cascade the interrupt; otherwise, an
  1816.   infinite loop will result.
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.                                         24
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.                                7. Memory Management
  1857.  
  1858.  
  1859.        XLIB supplies memory management procedures for both real and protected
  1860.   modes.  These procedures are configured at initialization to work with the
  1861.   currently resident memory management interfaces.
  1862.        Conventional memory may be allocated and released in real mode through
  1863.   DOS in usual fashion (INT 21H functions 48H and 49H).  However, DOS functions
  1864.   may not work properly in protected mode.  Therefore, use the XLIB routines
  1865.   PMGETDOSMEM and PMFREEDOSMEM for such requests.  PMFREEDOSMEM can also be used
  1866.   to find the amount of available DOS memory.
  1867.        The real-mode extended memory management procedures are GETMEM, FREEMEM,
  1868.   and RESETMEM.  GETMEM is used to allocate a block of extended memory.  FREEMEM
  1869.   may then be used to release the block.  RESETMEM releases all previously
  1870.   allocated blocks at once.  GETMEM may also be used to find the amount of
  1871.   available extended memory.
  1872.        The protected-mode memory management procedures are PMGETMEM, PMFREEMEM,
  1873.   and PMRESETMEM.  These procedures function exactly as the corresponding real-
  1874.   mode procedures:  GETMEM, FREEMEM, and RESETMEM.
  1875.        XLIB will seek extended memory through XMS only if it is present and if
  1876.   both DPMI and VCPI are absent.  If either protected-mode interface is present,
  1877.   then all extended memory will be allocated through the configured interface.
  1878.        XLIB will not use XMS to allocate memory from the high memory area (HMA)
  1879.   or from upper memory blocks (UMBs).  XLIB will however allocate from the HMA
  1880.   when it has full responsibility for extended memory management (DPMI, VCPI,
  1881.   and XMS are all absent).  XLIB never issues calls under EMS (apart from calls
  1882.   to VCPI).
  1883.        Under DPMI and VCPI, the CPU will typically be operating in page mode.
  1884.   Under this mode of operation, memory may be remapped such that logical
  1885.   addresses are not equal to physical addresses.  Addresses specified in the
  1886.   instruction code are logical addresses.  The physical addresses are the
  1887.   locations in memory which are actually accessed.  Typically, a programmer need
  1888.   not be concerned with the difference between these two types of addresses.
  1889.   However, the difference must be recognized when working with input/output
  1890.   devices which map to certain physical location in memory.  XLIB includes
  1891.   routines called MAPIO and PMMAPIO which assign logical addresses to specified
  1892.   physical addresses.  The physical addresses can then be accessed via the
  1893.   assigned logical addresses.
  1894.  
  1895.  
  1896.   Detailed Specifications
  1897.  
  1898.  
  1899.   PMGETDOSMEM (Protected Mode - Get DOS Memory)
  1900.   Purpose:  Allocate DOS memory block.
  1901.   CPU Mode:  Protected
  1902.   Registers at Call:  EAX = desired size of block in bytes.
  1903.   Return Registers:
  1904.      EAX = 0 if successful.  A block handle is returned in EBX.  The number of
  1905.   allocated bytes is returned in ECX.  The linear address of allocated block is
  1906.   returned in EDX.
  1907.      EAX = error code if unsuccessful.  AX = XLIB error code.  The high word of
  1908.   EAX (EAH) will be set to a DOS error code.  If DPMI is active, then EAH will
  1909.   be a DPMI error code (codes are supplied by DPMI .9 and up).
  1910.  
  1911.  
  1912.  
  1913.                                         25
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.   Details:
  1921.      The block will always be paragraph aligned and will have size equal to an
  1922.   integer multiple of 16.
  1923.      The location in free memory from which the block is allocated will depend
  1924.   upon the allocation strategy in force as of call (see INT 21H function 58H).
  1925.      Call with EAX = 0 to get largest available DOS memory block (not total free
  1926.   memory) in ECX (EAX, EBX, and EDX are preserved).
  1927.      If DPMI is active, then the handle is actually a selector with base address
  1928.   set to the linear address of the block.  If DPMI is not active, then the
  1929.   handle will be the segment of the block.
  1930.      In real mode, DOS memory may be allocated directly from DOS (INT 21H
  1931.   function 48H); however, this call will likely fail under DPMI protected mode.
  1932.  
  1933.   PMFREEDOSMEM (Protected Mode - Free DOS Memory)
  1934.   Purpose:  Release previously allocated DOS memory block.
  1935.   CPU Mode:  Protected
  1936.   Registers at Call:  EAX = block handle.
  1937.   Return Registers:  EAX = 0 if successful; otherwise, EAX = error code.  AX =
  1938.   XLIB error.  The high word of EAX (EAH) will be a DOS error code.  If DPMI is
  1939.   active, then EAH will equal a DPMI error code (codes are supplied by DPMI .9
  1940.   and up).
  1941.   Details:  In real mode, DOS memory may be released directly by DOS (INT 21H
  1942.   function 49H); however, this call will likely fail under DPMI protected mode.
  1943.  
  1944.   GETMEM (Get Memory)
  1945.   Purpose:  Allocate extended memory block.
  1946.   CPU Mode:  Real
  1947.   Registers at Call:  EAX = desired size of block in bytes.
  1948.   Return Registers:
  1949.      EAX = 0 if successful.  A block handle is returned in EBX.  The number of
  1950.   allocated bytes is returned in ECX.  The logical address of allocated block is
  1951.   returned in EDX.
  1952.      EAX = error code if unsuccessful.  AX = XLIB error code.  If DPMI is
  1953.   active, then the high word of EAX (EAH) will be a DPMI 1.0 error code (if
  1954.   provided by host).  If XMS is active, then EAH = XMS error code.
  1955.   Details:
  1956.      The page size for extended memory allocations is contained in PAGESIZE.
  1957.   PAGESIZE is a DWORD in DSEG and should be read after initialization.  The
  1958.   blocks will have addresses that are PAGESIZE aligned and will have sizes equal
  1959.   to an integer multiple of PAGESIZE.  PAGESIZE will equal:  1024 for XMS, 4096
  1960.   for VCPI, 4096 for most DPMI hosts, and 16 in the absence of a memory manager.
  1961.      If XMS is present in conjunction with either DPMI or VCPI, no extended
  1962.   memory will be requested through XMS.  All extended memory will be requested
  1963.   through the active protected-mode interface.
  1964.      XMS is never used to allocate from the HMA or from UMBs.  XLIB will however
  1965.   allocate from the HMA in the absence of a memory management interface.
  1966.      Call with EAX = 0 to get largest available extended memory block (not total
  1967.   free memory) in ECX (EBX and EDX are preserved).  This call can also return
  1968.   with an error condition in EAX.
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.                                         26
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.   FREEMEM (Free Memory)
  1985.   Purpose:  Release previously allocated extended memory block.
  1986.   CPU Mode:  Real
  1987.   Registers at Call:  EAX = block handle.
  1988.   Return Registers:  EAX = 0 if successful; otherwise, EAX = error code.  AX =
  1989.   XLIB error code.  If DPMI is active, then the high word of EAX (EAH) will be a
  1990.   DPMI 1.0 error code (if provided by host).  If XMS is active, then EAH = XMS
  1991.   error code.
  1992.   Details:  FREEMEM does not release page tables allocated under VCPI.  Call
  1993.   RESETMEM for this purpose.
  1994.  
  1995.   RESETMEM (Reset Memory)
  1996.   Purpose:  Release all previously allocated extended memory.
  1997.   CPU Mode:  Real
  1998.   Registers at Call:  None
  1999.   Return Registers:  EAX = 0 if successful; otherwise, EAX = error code.  AX =
  2000.   XLIB error code.  If DPMI is active, then the high word of EAX (EAH) will be a
  2001.   DPMI 1.0 error code (if provided by host).  If XMS is active, then EAH = XMS
  2002.   error code.
  2003.   Details:
  2004.      GETMEM will automatically allocate page tables as needed under VCPI.
  2005.   RESETMEM will release such tables.
  2006.      If DPMI is not installed, then RESETMEM will be called upon execution of
  2007.   INT 21H function 4C (DOS termination).  DPMI hosts reset extended memory
  2008.   automatically.
  2009.  
  2010.   PMGETMEM  (Protected Mode - Get Memory)
  2011.   Purpose:  Allocate extended memory block.
  2012.   CPU Mode:  Protected
  2013.   Details:  This routine is the protected-mode version of GETMEM.  See GETMEM
  2014.   for specifications.
  2015.  
  2016.   PMFREEMEM  (Protected Mode - Free Memory)
  2017.   Purpose:  Free previously allocated extended memory block.
  2018.   CPU Mode:  Protected
  2019.   Details:  This routine is the protected-mode version of FREEMEM.  See FREEMEM
  2020.   for specifications.
  2021.  
  2022.   PMRESETMEM (Protected Mode - Reset Memory)
  2023.   Purpose:  Free all previously allocated extended memory.
  2024.   CPU Mode:  Protected
  2025.   Details:  This routine is the protected-mode version of RESETMEM.  See
  2026.   RESETMEM for specifications.
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.                                         27
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.   MAPIO (Map Input/Output Device)
  2049.   Purpose:  Create a logical address mapping for an input/output device which
  2050.   maps to a fixed physical memory address.
  2051.   CPU Mode:  Real
  2052.   Registers at Call:  EDX = first physical address to be mapped.  EAX = size of
  2053.   physical address block.
  2054.   Return Registers:
  2055.      EAX = 0 if successful, in which event, EDX = the logical address for
  2056.   accessing the physical memory.
  2057.      EAX = error code if unsuccessful.  AX = XLIB error code.  If DPMI is
  2058.   active, then the high word of EAX (EAH) will be a DPMI 1.0 error code (if
  2059.   provided by host).  If XMS is active, then EAH = XMS error code.
  2060.   Details:
  2061.      DPMI will not allow mapping of physical addresses within the first
  2062.   megabyte; however, such mappings are allowed under other configurations.
  2063.      Logical addresses will always equal physical addresses in the absence of
  2064.   VCPI and DPMI because the processor will not be operating in page mode.  In
  2065.   such cases, MAPIO simply performs validity checks on EDX and EAX.
  2066.      MAPIO will automatically allocate page tables as needed under VCPI.
  2067.   RESETMEM will release such tables.
  2068.      Memory-mapped input/output devices are not typically mapped within the
  2069.   range of available memory since this would leave the possibility of confusing
  2070.   such addresses with ordinary memory.  Instead, such devices are mapped beyond
  2071.   the highest address that would otherwise be available.
  2072.  
  2073.   PMMAPIO (Protected Mode - Map Input/Output Device)
  2074.   Purpose:  Create a logical address mapping for an input/output device which
  2075.   maps to a fixed physical memory address.
  2076.   CPU Mode:  Protected
  2077.   Details:  This routine is the protected-mode version of MAPIO.  See MAPIO for
  2078.   specifications.
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.                                         28
  2106.  
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.                                 8. File Management
  2113.  
  2114.  
  2115.        XLIB file management procedures are low-level routines with powerful
  2116.   capabilities.  These routines can load files to extended memory or save
  2117.   extended memory to files.  They can read and write files either sequentially
  2118.   or randomly.
  2119.        All XLIB file management routines will receive and return values in a
  2120.   contiguous block of memory called a "file control block" (not to be confused
  2121.   with DOS file control blocks).  The file control block must be located in
  2122.   conventional memory and must have the form presented in Table 5.
  2123.  
  2124.  
  2125.   Table 5:  XLIB File Control Block Structure
  2126.   _____________________________________________________________________________
  2127.   Field Name   Field Type        Field Description
  2128.   ----------   ----------        -----------------
  2129.   CONDCODE     DWORD             Condition code from file operation
  2130.   FNAME        BYTE[68]          File path and name (zero terminated string)
  2131.   FHANDLE      WORD              File handle assigned by DOS
  2132.   FPTRMODE     WORD              File pointer mode
  2133.   FPTR         DWORD             File pointer
  2134.   BLKADR       DWORD             Memory source/destination address
  2135.   BLKSIZE      DWORD             Size of transfer block in bytes
  2136.   BUFADR       DWORD             Buffer address (conventional memory address)
  2137.   BUFSIZE      WORD              Buffer size in bytes
  2138.   CONTROL      WORD              Control word
  2139.   _____________________________________________________________________________
  2140.  
  2141.  
  2142.        CONDCODE is used to return error codes.
  2143.        FNAME is a zero-terminated ASCII string defining the file path and name.
  2144.   There cannot be more than 67 characters in this string, excluding the
  2145.   termination character.
  2146.        BLKADR and BLKSIZE define the source/destination memory block for the
  2147.   transfer.  This block may be in either conventional or extended memory.
  2148.   BLKADR is a linear address.
  2149.        XLIB uses DOS to access the disk.  DOS cannot read or write to extended
  2150.   memory; consequently, a conventional memory buffer must be set up for the DOS
  2151.   transfers.  File management routines shift to protected mode to perform
  2152.   transfers between the buffer and the source/destination memory.  BUFADR and
  2153.   BUFSIZE define the conventional memory buffer.  BUFADR is a linear address.
  2154.        For fastest transfers, the memory block and the buffer should be DWORD
  2155.   aligned and should have sizes equal to an integer multiple of four.
  2156.        FPTR and FPTRMODE specify the file pointer setting to be used before
  2157.   transfers to or from the disk.  FPTRMODE specifies how FPTR is to be
  2158.   interpreted.  The following values are valid for FPTRMODE:
  2159.  
  2160.  
  2161.   FPTRMODE    FPTR Interpretation
  2162.   0           Unsigned offset from the beginning of the file
  2163.   1           Signed offset from the current file pointer
  2164.   2           Signed offset from the end of the file
  2165.   3           FPTR is ignored.  Use current file pointer (sequential mode)
  2166.  
  2167.  
  2168.  
  2169.                                         29
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.        CONTROL is not used in the present version of XLIB.  Set all bits in
  2177.   CONTROL to zero.
  2178.        In assembly language or C, the file control block would typically be
  2179.   defined by a structure.  In BASIC, the file control block can be defined with
  2180.   a user defined type.
  2181.        Values are transferred to and from all file routines in EAX and in the
  2182.   file control block.  All routines should be called with the linear address of
  2183.   the file control block in EAX.  All routines return with two copies of the
  2184.   error code - one in EAX and one in the condition code of the file control
  2185.   block.  A zero error code indicates successful execution.
  2186.        Since these routines perform disk operations, special precautions should
  2187.   be taken to ensure that parameters in the file control block are properly
  2188.   defined before performing calls.  In particular, one should always make sure
  2189.   that the source/destination memory block and the conventional memory buffer
  2190.   are properly defined.  A safe rule is to simply set the buffer size to zero
  2191.   because this forces XLIB to supply a buffer when opening or creating the file.
  2192.  
  2193.  
  2194.   Detailed Specifications
  2195.  
  2196.  
  2197.   XCREATE (Create File)
  2198.   Purpose:  Create and open a new file of specified name in specified directory.
  2199.   CPU Mode:  Real
  2200.   Registers at Call:  EAX = linear address of file control block.
  2201.   Control Block at Call:  FNAME = file path and name.
  2202.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2203.   occurred, then the high word of EAX will be set to the DOS error code.
  2204.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  2205.   FHANDLE = file handle assigned by DOS.  If the procedure is called with
  2206.   BUFSIZE = 0, then XLIB will set BUFADR and BUFSIZE to its own internal buffer.
  2207.   Details:
  2208.      If the file already exists, then it will be truncated to zero length.
  2209.      The size and location of the internal buffer will depend upon how XLIB was
  2210.   initialized.  If DPMI is active, then the buffer will be slightly larger than
  2211.   2K; otherwise, the buffer will be slightly larger than 1K.  The linear address
  2212.   and size of the buffer may be obtained from FILEBUFADR (DWORD), and
  2213.   FILEBUFSIZE (WORD) in DSEG.
  2214.      Files created by this routine will be given both read and write access.
  2215.      This routine uses INT 21H function 3CH to create the file.
  2216.  
  2217.   XOPEN (Open File)
  2218.   Purpose:  Open existing file of specified name in specified directory.
  2219.   CPU Mode:  Real
  2220.   Registers at Call:  EAX = linear address of file control block.
  2221.   Control Block at Call:  FNAME = file path and name.
  2222.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2223.   occurred, then the high word of EAX will be set to the DOS error code.
  2224.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  2225.   FHANDLE = file handle assigned by DOS.  If the procedure is called with
  2226.   BUFSIZE = 0, then XLIB will set BUFADR and BUFSIZE to its own internal buffer.
  2227.  
  2228.  
  2229.  
  2230.  
  2231.  
  2232.  
  2233.                                         30
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.  
  2240.   Details:
  2241.      The file is opened for both read and write access.
  2242.      The size and location of the internal buffer will depend upon how XLIB was
  2243.   initialized.  If DPMI is active, then the buffer will be slightly larger than
  2244.   2K; otherwise, the buffer will be slightly larger than 1K.  The linear address
  2245.   and size of the buffer may be obtained from FILEBUFADR (DWORD), and
  2246.   FILEBUFSIZE (WORD) in DSEG.
  2247.      This routine uses INT 21H function 3DH to open the file.
  2248.  
  2249.   XCLOSE (Close File)
  2250.   Purpose:  Close previously opened file.
  2251.   CPU Mode:  Real
  2252.   Registers at Call:  EAX = linear address of file control block.
  2253.   Control Block at Call:  FHANDLE = file handle.
  2254.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2255.   occurred, then the high word of EAX will be set to the DOS error code.
  2256.   Control Block at Return:  CONDCODE = error code.
  2257.   Details:  This routine uses INT 21H function 3EH to close the file.
  2258.  
  2259.   XSAVE (Save File)
  2260.   Purpose:  Create file with contents equal to specified memory block.
  2261.   CPU Mode:  Real
  2262.   Registers at Call:  EAX = linear address of file control block.
  2263.   Control Block at Call:  FNAME = file path and name.  BLKADR/BLKSIZE = address
  2264.   and size of memory block to provide file contents.  BUFADR/BUFSIZE = address
  2265.   and size of conventional memory buffer.
  2266.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2267.   occurred, then the high word of EAX will be set to the DOS error code.
  2268.   Control Block at Return:  CONDCODE = error code.
  2269.   Details:
  2270.      The file cannot already be open.  The file is both created and closed by
  2271.   this routine.
  2272.      This routine will replace any previously existing file named FNAME.
  2273.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  2274.   block is not overlapped by BUFADR/BUFSIZE.
  2275.      If this routine is called with BUFSIZE = 0, then XLIB will automatically
  2276.   set BUFADR and BUFSIZE to its own internal buffer.
  2277.      This routine transfers the source memory to the file through the buffer.
  2278.   Transfers from buffer to disk are accomplished with INT 21H function 40H.
  2279.  
  2280.   XLOAD (Load File)
  2281.   Purpose:  Load file contents to specified memory block.
  2282.   CPU Mode:  Real
  2283.   Registers at Call:  EAX = linear address of file control block.
  2284.   Control Block at Call:  FNAME = file path and name.  BLKADR/BLKSIZE = address
  2285.   and size of memory block to receive file contents.  BUFADR/BUFSIZE = address
  2286.   and size of conventional memory buffer.
  2287.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2288.   occurred, then the high word of EAX will be set to the DOS error code.
  2289.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  2290.   BLKSIZE = actual number of bytes transferred.
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.                                         31
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.   Details:
  2305.      The file cannot already be open.  The file is both opened and closed by
  2306.   this routine.
  2307.      The value of BLKSIZE as of call is interpreted as an upper limit on the
  2308.   number of bytes to transfer.  The entire file is loaded provided that it does
  2309.   not contain more than BLKSIZE bytes.
  2310.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  2311.   block is not overlapped by BUFADR/BUFSIZE.
  2312.      If this routine is called with BUFSIZE = 0, then XLIB will automatically
  2313.   set BUFADR and BUFSIZE to its own internal buffer.
  2314.      This routine uses INT 21H function 3FH to transfer the disk contents to the
  2315.   buffer.  It then transfers the buffer contents to the destination memory.
  2316.  
  2317.   XWRITE (Write to File)
  2318.   Purpose:  Write specified memory block to specified location in open file.
  2319.   CPU Mode:  Real
  2320.   Registers at Call:  EAX = linear address of file control block.
  2321.   Control Block at Call:  FHANDLE = file handle.  FPTR/FPTRMODE = file pointer
  2322.   setting for beginning of transfer.  BLKADR/BLKSIZE = address and size of
  2323.   memory block to provide file contents.  BUFADR/BUFSIZE = address and size of
  2324.   conventional memory buffer.
  2325.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2326.   occurred, then the high word of EAX will be set to the DOS error code.
  2327.   Control Block at Return:  CONDCODE = error code.
  2328.   Details:
  2329.      The file must be opened with XOPEN or XCREATE before using this routine.
  2330.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  2331.   block is not overlapped by BUFADR/BUFSIZE.
  2332.      This routine uses INT 21H function 42H to set the file pointer.  The source
  2333.   memory is then transferred through the buffer to disk.  Transfers from buffer
  2334.   to disk are accomplished with INT 21H function 40H.
  2335.      Sequential transfers should set FPTRMODE = 3 for fastest execution.
  2336.  
  2337.   XREAD (Read From File)
  2338.   Purpose:  Load specified memory block from specified location in open file.
  2339.   CPU Mode:  Real
  2340.   Registers at Call:  EAX = linear address of file control block.
  2341.   Control Block at Call:  FHANDLE = file handle.  FPTR/FPTRMODE = file pointer
  2342.   setting for beginning of transfer.  BLKADR/BLKSIZE = address and size of
  2343.   memory block to receive file contents.  BUFADR/BUFSIZE = address and size of
  2344.   conventional memory buffer.
  2345.   Return Registers:  EAX = error code.  AX = XLIB error code.  If a DOS error
  2346.   occurred, then the high word of EAX will be set to the DOS error code.
  2347.   Control Block at Return:  CONDCODE = error code.  If CONDCODE = 0, then
  2348.   BLKSIZE = actual number of bytes transferred.
  2349.   Details:
  2350.      The file must be opened with XOPEN or XCREATE before using this routine.
  2351.      BLKADR/BLKSIZE may define a conventional memory block provided that this
  2352.   block is not overlapped by BUFADR/BUFSIZE.
  2353.      This routine uses INT 21H function 42H to set the file pointer.  The file
  2354.   contents are then transferred to the destination memory through the buffer.
  2355.   The file contents are transferred to the buffer using INT 21H function 3FH.
  2356.      Sequential transfers should set FPTRMODE = 3 for fastest execution.
  2357.  
  2358.  
  2359.  
  2360.  
  2361.                                         32
  2362.  
  2363.  
  2364.  
  2365.  
  2366.  
  2367.  
  2368.   PMXCREATE (Protected Mode - Create File)
  2369.   Purpose:  Create and open a new file of specified name in specified directory.
  2370.   CPU Mode:  Protected
  2371.   Details:  This routine is the protected-mode version of XCREATE.  See XCREATE
  2372.   for details.
  2373.  
  2374.   PMXOPEN (Protected Mode - Open File)
  2375.   Purpose:  Open an existing file of specified name in specified directory.
  2376.   CPU Mode:  Protected
  2377.   Details:  This routine is the protected-mode version of XOPEN.  See XOPEN for
  2378.   details.
  2379.  
  2380.   PMXCLOSE (Protected Mode - Close File)
  2381.   Purpose:  Close previously opened file.
  2382.   CPU Mode:  Protected
  2383.   Details:  This routine is the protected-mode version of XCLOSE.  See XCLOSE
  2384.   for details.
  2385.  
  2386.   PMXSAVE (Protected Mode - Save File)
  2387.   Purpose:  Create file with contents equal to specified memory block.
  2388.   CPU Mode:  Protected
  2389.   Details:  This routine is the protected-mode version of XSAVE.  See XSAVE for
  2390.   details.
  2391.  
  2392.   PMXLOAD (Protected Mode - Load File)
  2393.   Purpose:  Load file contents to specified memory block.
  2394.   CPU Mode:  Protected
  2395.   Details:  This routine is the protected-mode version of XLOAD.  See XLOAD for
  2396.   details.
  2397.  
  2398.   PMXWRITE (Protected Mode - Write to File)
  2399.   Purpose:  Write specified memory block to specified location in open file.
  2400.   CPU Mode:  Protected
  2401.   Details:  This routine is the protected-mode version of XWRITE.  See XWRITE
  2402.   for details.
  2403.  
  2404.   PMXREAD (Protected Mode - Read From File)
  2405.   Purpose:  Load specified memory block from specified location in open file.
  2406.   CPU Mode:  Protected
  2407.   Details:  This routine is the protected-mode version of XREAD.  See XREAD for
  2408.   details.
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414.  
  2415.  
  2416.  
  2417.  
  2418.  
  2419.  
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425.                                         33
  2426.  
  2427.  
  2428.  
  2429.  
  2430.  
  2431.  
  2432.                              9. Descriptor Management
  2433.  
  2434.  
  2435.        All selectors in Table 1 up to DGROUPSEL are used by XLIB procedures;
  2436.   consequently, the corresponding descriptors should never be changed.  However,
  2437.   descriptors for the other selectors may be modified.  XLIB includes a
  2438.   procedure called SETDESC (set descriptor) to facilitate such modifications.  A
  2439.   second routine called PMSETDESC is the protected-mode version of SETDESC.
  2440.        Descriptors corresponding to the inline selectors should not be changed
  2441.   in programs which also use the inline mode-switch procedures.
  2442.        XLIB will also allow descriptors to be placed in the global descriptor
  2443.   table provided that DPMI is not being used.  XLIB has no control over the
  2444.   global descriptor table under DPMI.  The global descriptor table in XLIB
  2445.   begins at a public DWORD in DSEG called GDT.  There are 48 descriptors in this
  2446.   table.  The first 32 are either in use or are reserved by XLIB.  Descriptors
  2447.   must be placed in the table by direct writes.  The first available descriptor
  2448.   begins at DSEG offset OFFSET GDT + 100H.  The selector values are simply the
  2449.   offsets from GDT.  Hence, the selector value for the first available
  2450.   descriptor is 100H.
  2451.  
  2452.  
  2453.   Detailed Specifications
  2454.  
  2455.  
  2456.   SETDESC (Set Descriptor)
  2457.   Purpose:  Change a descriptor in the local descriptor table.
  2458.   CPU Mode:  Real
  2459.   Registers at Call:  BX = selector.  EDX:EAX = the new descriptor.
  2460.   Return Registers:  EAX = error code.  AX = XLIB error code.  If DPMI is
  2461.   installed, then the high word of EAX will equal a DPMI 1.0 error code (if
  2462.   provided by host).  EDX may be returned with some modifications to the access
  2463.   rights bits.
  2464.   Details:
  2465.      The access rights bits in EDX will be edited before installation of the
  2466.   descriptor.  In particular:  The application bit will be set to indicate an
  2467.   application segment (rather than a system segment).  Reserved bits will be
  2468.   given proper settings.  The descriptor privilege level will be set to the
  2469.   appropriate value.  If the descriptor corresponds to a code segment, then the
  2470.   descriptor will also be marked as readable and nonconforming.
  2471.      Segment registers which are loaded with the old value of the descriptor
  2472.   will not necessarily be reloaded when the descriptor is changed.
  2473.  
  2474.   PMSETDESC (Protected Mode - Set Descriptor)
  2475.   Purpose:  Change a descriptor in the local descriptor table.
  2476.   CPU Mode:  Protected
  2477.   Details:  This routine is the protected-mode version of SETDESC.  See SETDESC
  2478.   for details.
  2479.  
  2480.  
  2481.  
  2482.  
  2483.  
  2484.  
  2485.  
  2486.  
  2487.  
  2488.  
  2489.                                         34
  2490.  
  2491.  
  2492.  
  2493.  
  2494.  
  2495.  
  2496.                  10. Using XLIB in High-Level Language Libraries
  2497.  
  2498.  
  2499.        The following program illustrates the usage of XLIB in libraries called
  2500.   from Microsoft BASIC 7.0.  The library contains a protected-mode procedure
  2501.   which sums the elements in a single precision array created within BASIC.  The
  2502.   general methodology here is recommended for developing assembly language
  2503.   libraries.
  2504.        Since BASIC cannot call a 32-bit segment, a real-mode interface procedure
  2505.   must be placed in a 16-bit segment to receive the BASIC call and then transfer
  2506.   execution to 32-bit protected mode.  The interface procedure is call SUMARRAY
  2507.   while the 32-bit protected-mode procedure which actually sums the array
  2508.   elements is called SUMARRAY32.
  2509.        BASIC must pass certain arguments to the library procedures.  These
  2510.   include the array address and the number of elements to be summed.  These
  2511.   arguments could be passed on the stack; however, such approach proves awkward
  2512.   since the stack must be changed when entering protected mode.  Consequently,
  2513.   BASIC places all arguments in a contiguous block of memory called a "control
  2514.   block," and then passes only the address of the control block to the library.
  2515.   BASIC constructs the control block with a user-defined type.
  2516.        The first four bytes of the control block are reserved for placement of
  2517.   error codes by the library procedures.  Also, the address of the control block
  2518.   is placed in CCODEPTR so that any FPU exceptions will be signalled in the
  2519.   error code location as well.  Were an FPU exception to occur, the FPU
  2520.   interrupt handler will immediately transfer control to EXITPM which will shift
  2521.   to real-mode, restore registers, and return control to the BASIC caller.
  2522.        The library also contains a real-mode function called LINADR which may be
  2523.   called by BASIC to convert segment addresses to linear addresses.
  2524.        An example of this same program for Microsoft C 7.0 is included in
  2525.   Appendix E.  An example for Borland C is included in the files EXAMP3B.C and
  2526.   EXAMP3B.ASM.
  2527.  
  2528.  
  2529.   Example 3:  Calling Protected-Mode Libraries From BASIC
  2530.   _____________________________________________________________________________
  2531.                             +++++++++++++++++++++++++
  2532.                             + ASSEMBLY CODE LIBRARY +
  2533.                             +++++++++++++++++++++++++
  2534.  
  2535.  
  2536.   ;The following library should be combined with XLIB.LIB using the Microsoft
  2537.   ;LINK and LIB utilities.  If BASIC is to be executed from the QBX environment,
  2538.   ;then a quick library must be loaded with the environment.  See BASIC
  2539.   ;documentation for instructions.
  2540.  
  2541.  
  2542.                  .MODEL         LARGE,PASCAL
  2543.                  .386P
  2544.  
  2545.                  INCLUDE        XLIB.INC
  2546.  
  2547.  
  2548.  
  2549.  
  2550.  
  2551.  
  2552.  
  2553.                                         35
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559.  
  2560.   CSEG           SEGMENT PARA PUBLIC USE16 'CODE'
  2561.                  ASSUME CS:CSEG, DS:DSEG
  2562.  
  2563.   ;Function to calculate linear address from segment address on stack.
  2564.   ;Returns linear address in DX:AX.
  2565.   LINADR         PROC FAR PUBLIC,
  2566.                  SEGADR:DWORD                  ;Segment address of variable
  2567.                  XOR            EAX,EAX        ;Clear high words
  2568.                  XOR            EDX,EDX
  2569.                  MOV            AX,WORD PTR SEGADR[0]
  2570.                  MOV            DX,WORD PTR SEGADR[2]
  2571.                  SHL            EDX,4          ;Calculate linear address
  2572.                  ADD            EDX,EAX
  2573.                  MOV            AX,DX
  2574.                  SHR            EDX,16         ;Return linear address in DX:AX
  2575.                  RET
  2576.   LINADR         ENDP
  2577.  
  2578.   ;Structure defining control block for SUMARRAY.
  2579.   ARRAYDATA      STRUCT
  2580.     CONDCODE     DWORD          0              ;Condition code
  2581.     N            DWORD          0              ;Number of elements to sum
  2582.     ADDRESS      DWORD          0              ;Address of first element
  2583.     SUM          DWORD          0              ;Sum of array elements
  2584.   ARRAYDATA      ENDS
  2585.  
  2586.   ;Real-mode interface to SUMARRAY32.  Segment address of control block having
  2587.   ;structure ARRAYDATA should be on the stack.
  2588.   SUMARRAY       PROC FAR PUBLIC,
  2589.                  CBSEGADR:DWORD                ;Control block segment address
  2590.                  PUSH           DS
  2591.                  PUSHW          DSEG
  2592.                  POP            DS
  2593.                  XOR            EAX,EAX        ;Clear high words
  2594.                  XOR            EDX,EDX
  2595.                  MOV            AX,WORD PTR CBSEGADR[2]
  2596.                  MOV            DX,WORD PTR CBSEGADR[0]
  2597.                  SHL            EAX,4          ;Calculate linear address
  2598.                  ADD            EAX,EDX
  2599.                  MOV            CCODEPTR,EAX   ;Reset condition code address
  2600.                  POP            DS             ;Pop calling DS
  2601.                  PUSHD          OFFSET SUMARRAY32
  2602.                  CALL           ENTERPM        ;Execute SUMARRAY32 in protected
  2603.                  RET
  2604.   SUMARRAY       ENDP
  2605.  
  2606.   CSEG           ENDS
  2607.  
  2608.  
  2609.  
  2610.  
  2611.  
  2612.  
  2613.  
  2614.  
  2615.  
  2616.  
  2617.                                         36
  2618.  
  2619.  
  2620.  
  2621.  
  2622.  
  2623.  
  2624.   TSEG           SEGMENT PARA PUBLIC USE32 'CODE'
  2625.                  ASSUME CS:TSEG, SS:TSEG, DS:TSEG, ES:TSEG, FS:DSEG, GS:DGROUP
  2626.  
  2627.   ;Sum the elements of a single precision array.  Array parameters are stored
  2628.   ;in a control block having structure of ARRAYDATA.  The linear address of the
  2629.   ;control block is stored at CCODEPTR.  An error code of -1 is returned in the
  2630.   ;condition code of the control block if the number of array elements is zero.
  2631.   ;XLIB places an error code in the control block if an FPU exception occurs
  2632.   ;while calculating the sum.  This error code will have the FPU status word in
  2633.   ;the high word and the XLIB FPU error code in the low word.  Observe that this
  2634.   ;routine will be called with DS = FLATDSEL (flat-model data descriptor) and
  2635.   ;FS = DSEGSEL (DSEG data descriptor).
  2636.   SUMARRAY32     PROC NEAR
  2637.                  MOV            EBX,FS:CCODEPTR               ;Get control block
  2638.                  MOV            EDX,ARRAYDATA.ADDRESS[EBX]    ;Get array address
  2639.                  MOV            ESI,ARRAYDATA.N[EBX]          ;Get N
  2640.                  SUB            ESI,1
  2641.                  JB             NODATA                        ;Error:  N = 0
  2642.                  FLDZ                                         ;Initialize sum
  2643.   SUMLOOP:       FADD           DWORD PTR [EDX+4*ESI]
  2644.                  SUB            ESI,1
  2645.                  JAE            SUMLOOP
  2646.                  FSTP           ARRAYDATA.SUM[EBX]            ;Save sum
  2647.                  RET
  2648.   NODATA:        MOV            ARRAYDATA.CONDCODE[EBX],-1    ;Record error code
  2649.                  RET
  2650.   SUMARRAY32     ENDP
  2651.  
  2652.   TSEG           ENDS
  2653.                  END
  2654.  
  2655.  
  2656.                               +++++++++++++++++++++
  2657.                               + BASIC MAIN MODULE +
  2658.                               +++++++++++++++++++++
  2659.  
  2660.  
  2661.   'The following Microsoft BASIC 7.0 program should be linked with the above
  2662.   'library.  The BASIC program first initializes XLIB.  Next, it creates a
  2663.   'single precision array.  A control block for SUMARRAY is then constructed
  2664.   'and the call to SUMARRAY is executed.  Finally, the condition code in the
  2665.   'control block is inspected and results are printed.
  2666.  
  2667.   DEFINT A-Z
  2668.  
  2669.   'Declare XLIB procedures
  2670.   DECLARE FUNCTION XLIBMEMREQ& ()
  2671.   DECLARE FUNCTION INITXLIB& ()
  2672.   DECLARE FUNCTION XLIBCONFIG% ()
  2673.  
  2674.   'Declare procedures in the library linked with XLIB
  2675.   DECLARE FUNCTION LINADR& (SEG VARIABLE AS ANY)
  2676.   DECLARE SUB SUMARRAY (SEG VARIABLE AS ANY)
  2677.  
  2678.  
  2679.  
  2680.  
  2681.                                         37
  2682.  
  2683.  
  2684.  
  2685.  
  2686.  
  2687.  
  2688.   'Structure for the control block
  2689.   TYPE ARRAYDATA
  2690.     CONDCODE AS LONG      'Location to receive any error codes
  2691.     N AS LONG             'Number of elements to be summed
  2692.     ADDRESS AS LONG       'Linear address of the array
  2693.     SUM AS SINGLE         'Location for array sum
  2694.   END TYPE
  2695.  
  2696.   'Check XLIBCONFIG to see if XLIB has already been initialized.  If not then
  2697.   'call XLIBMEMREQ to find amount of conventional memory needed by XLIB and
  2698.   'release at least this amount with the BASIC SETMEM function.  XLIBMEMREQ
  2699.   'returns with sign bit of DX set if an error occurred.  The error is then
  2700.   'identified by AX.  XLIB will not be terminated upon completion of this
  2701.   'program in the Microsoft QBX environment; therefore, initialization is
  2702.   'required only once within the environment.
  2703.   IF XLIBCONFIG = 0 THEN
  2704.     TEMP& = XLIBMEMREQ
  2705.     IF TEMP& >= 0& THEN
  2706.       IF TEMP& > 0 THEN TEMP& = SETMEM(-TEMP& - 16&)
  2707.       TEMP& = INITXLIB                 'INITXLIB error code returned in TEMP&
  2708.     ELSE
  2709.       TEMP& = TEMP& AND &H7FFFFFFF     'Mask sign bit to leave error code only
  2710.     END IF
  2711.     IF TEMP& THEN
  2712.       PRINT "Library initialization error:  "; HEX$(TEMP&)
  2713.       END
  2714.     END IF
  2715.   END IF
  2716.  
  2717.   DIM A(100) AS SINGLE
  2718.   DIM AD AS ARRAYDATA
  2719.  
  2720.   FOR I = 0 TO 100            'Assign numbers to array
  2721.     A(I) = I
  2722.   NEXT I
  2723.  
  2724.   AD.CONDCODE = 0&            'Clear the error code
  2725.   AD.N = 50&                  'Sum first 50 elements
  2726.   AD.ADDRESS = LINADR(A(0))   'Calculate and record linear address of A(0)
  2727.  
  2728.   CALL SUMARRAY(AD)
  2729.  
  2730.   IF AD.CONDCODE THEN
  2731.     PRINT "Error: ";HEX$(AD.CONDCODE)
  2732.   ELSE
  2733.     PRINT "Sum: ";AD.SUM      'Should equal 1225
  2734.   ENDIF
  2735.  
  2736.   END
  2737.   _____________________________________________________________________________
  2738.  
  2739.  
  2740.  
  2741.  
  2742.  
  2743.  
  2744.  
  2745.                                         38
  2746.  
  2747.  
  2748.  
  2749.  
  2750.  
  2751.  
  2752.                11. Using XLIBE for Debugging and Exception Trapping
  2753.  
  2754.  
  2755.        XLIBE.LIB contains all of the functions of XLIB.LIB plus other functions
  2756.   designed for debugging and exception trapping.  XLIBE.LIB consumes more memory
  2757.   than XLIB.LIB; consequently, the programmer may wish to use the former for
  2758.   program development and the latter for release versions.  The same header and
  2759.   include files are used for both libraries.
  2760.        Typically, a VCPI server or a DPMI host will be accompanied with
  2761.   exception handlers.  However, because such handlers do not have specific
  2762.   knowledge of the programs they will be serving, they typically do a relatively
  2763.   poor job of restoring machine state after an exception occurs.  As a
  2764.   consequence, reboot is often necessary.  Also, such handlers generally do a
  2765.   poor job of reporting the machine state as of the exception; consequently,
  2766.   they are of limited usefulness toward debugging programs.
  2767.        XLIBE.LIB contains protected-mode handlers for all CPU exceptions.  These
  2768.   handlers will generally be able to perform complete cleanup after an exception
  2769.   occurs thereby eliminating the need for reboot.  The state of the machine as
  2770.   of the exception is thoroughly reported; therefore, the programmer should be
  2771.   able to quickly identify the source of the exception.
  2772.        If DPMI 1.0 is being used, then XLIBE can also trap exceptions occurring
  2773.   in real mode.  This ability makes XLIBE useful even to programmers who would
  2774.   otherwise be writing exclusively real-mode code.
  2775.        All of the additional features of XLIBE are activated by default.  Once
  2776.   the call to INITXLIB has been successfully executed, protected-mode exception
  2777.   trapping will be enabled, and real-mode exception trapping will be enabled
  2778.   provided that DPMI 1.0 is installed.  The default behavior of XLIBE can
  2779.   however be modified by IFLAGS, as described below.
  2780.        Two CPU exceptions are specifically designed for debugging.  These are
  2781.   exception #1 and exception #3.  Exception #1 is used for debug data
  2782.   breakpoints.  This exception is generated with any attempt to access
  2783.   predefined addresses in either data or code.  As many as four data breakpoint
  2784.   addresses may be defined with use of XLIBE procedures.  Exception #3 is
  2785.   generated by the INT 3 instruction.  This instruction may be inserted in the
  2786.   code at any point where the programmer wishes to inspect the processor state.
  2787.   The XLIBE handlers for both exceptions are designed to allow the programmer
  2788.   the option of resuming execution after the exceptions are reported.
  2789.        One of the notorious flaws in the IBM AT architecture is the manner in
  2790.   which hardware interrupts are programmed.  Hardware interrupt requests (IRQs)
  2791.   0-7 are assigned to interrupt vectors 8-15.  Unfortunately, these same
  2792.   interrupt vectors are used for CPU exceptions.  Consequently, when an
  2793.   interrupt occurs through one of these vectors, it is sometimes difficult to
  2794.   determine if the interrupt was generated by external hardware or by the CPU.
  2795.   For example, an INT 8 could be generated either by the system timer or by a
  2796.   double fault exception.  The INT 8 handler should, if possible, make a
  2797.   determination of the source of the interrupt before servicing it.  However, an
  2798.   easier and more reliable solution is to correct the problem at its root by
  2799.   remapping hardware interrupts to different vectors.  The approach taken by
  2800.   XLIBE will depend upon the active protected-mode host/server.
  2801.        If DPMI is not installed, then the default behavior of XLIBE is to remap
  2802.   hardware interrupts during initialization.  By default, XLIBE remaps IRQs 0-8
  2803.   to vectors 50H-57H and IRQs 9-15 to vectors 58H-5FH.  These vectors may
  2804.   however be changed by the programmer.  The target vectors are contained in
  2805.   PIC1BASEINT (programmable interrupt controller #1 base interrupt) and
  2806.  
  2807.  
  2808.  
  2809.                                         39
  2810.  
  2811.  
  2812.  
  2813.  
  2814.  
  2815.  
  2816.   PIC2BASINT.  Both variables are BYTE locations in DSEG.  They should be
  2817.   changed before calling INITXLIB.  The base interrupt vectors must be evenly
  2818.   divisible by eight.
  2819.        IRQs 9-15 are traditionally assigned to vectors 70H-77H.  This mapping
  2820.   poses no problem because there is no conflict with CPU exceptions at these
  2821.   addresses.  XLIB remaps to 50H-57H simply because these addresses have been
  2822.   used in other protected-mode software (e.g. DESQview and OS/2 1.x).
  2823.        XLIBE will not attempt to remap hardware interrupts if it discovers that
  2824.   the mappings have already been changed from their conventional locations.
  2825.   INITXLIB terminates with an error code in such cases.
  2826.        When XLIBE remaps hardware interrupts, it also installs real-mode
  2827.   handlers at the new hardware interrupt vectors.  These handlers simply
  2828.   transfer execution to the addresses at the old vectors.  This approach ensures
  2829.   complete compatibility with any resident software which may expect
  2830.   conventional hardware interrupt mappings.  Accordingly, the programmer should
  2831.   install real-mode hardware interrupt handlers in usual fashion.  For example,
  2832.   a timer-tick interrupt handler should be installed at INT 8.  However,
  2833.   protected-mode handlers for these interrupts should be installed at the new
  2834.   vectors.  The new vectors should be read from IRQ0INTNO and IRQ8INTNO after
  2835.   calling INITXLIB.
  2836.        The approach taken under DPMI is somewhat different.  The DPMI host will
  2837.   expect conventional hardware interrupt mappings.  The host will always
  2838.   immediately receive control after interrupts.  The host will identify the
  2839.   interrupt as being a software interrupt, hardware interrupt, or CPU exception.
  2840.   It will then transfer control to the appropriate handler.  Consequently, it is
  2841.   not necessary for XLIBE to remap hardware interrupts under DPMI.  The values
  2842.   in PIC1BASEINT and PIC2BASEINT are ignored in this case.  After the call to
  2843.   INITXLIB, IRQ0INTNO will equal 08H and IRQ8INTNO will equal 70H.
  2844.        XLIBE allows a high degree of control over exception trapping behavior.
  2845.   Three bits in IFLAGS are designed for this purpose.  Bit 1 can be set to
  2846.   disable all exception trapping and remap of hardware interrupts.  XLIBE is
  2847.   functionally equivalent to XLIB when this bit is set.  Bit 2 may be set to
  2848.   disable real-mode exception trapping.  This bit is meaningful only when DPMI
  2849.   1.0 is installed and when bit 1 is clear.  Bit 3 may be set to prevent XLIBE
  2850.   from accessing the CPU debug registers.  This bit is meaningful only if bit 1
  2851.   is clear.  The programmer may wish to set this bit if a debugger is being
  2852.   used.  All of these bits are clear by default.
  2853.        The programmer may selectively install exception handlers by altering the
  2854.   bit settings in CPUINTFLAGS (CPU interrupt flags).  CPUINTFLAGS is a public
  2855.   DWORD in DSEG.  A set bit enables installation of the the corresponding
  2856.   exception handler.  The default value of CPUINTFLAGS is FFFBH.  This enables
  2857.   installation of handlers for all exceptions apart from exception #2 (non-
  2858.   maskable interrupt), exception #16 (FPU exception), and exception #17
  2859.   (alignment check).  FPU exceptions are handled in the AT architecture through
  2860.   a hardware interrupt (IRQ 13) rather than exception #16.  Alignment is seldom
  2861.   demanded in the AT architecture.
  2862.        When an exception occurs, the XLIBE exception handlers print useful
  2863.   information about the exception in a screen report.  Appendix H contains
  2864.   instructions for interpreting this information and for identification of the
  2865.   source of the exception.
  2866.  
  2867.  
  2868.  
  2869.  
  2870.  
  2871.  
  2872.  
  2873.                                         40
  2874.  
  2875.  
  2876.  
  2877.  
  2878.  
  2879.  
  2880.   Detailed Specifications
  2881.  
  2882.  
  2883.      The procedures presented in this section are contained in XLIBE.LIB but not
  2884.   in XLIB.LIB.
  2885.  
  2886.  
  2887.   SETWATCH (Set Watchpoint)
  2888.   Purpose:  Set a data breakpoint for generation of exception #1.
  2889.   CPU Mode:  Real
  2890.   Registers at Call:  EAX = linear address of breakpoint.  DL = length of
  2891.   breakpoint in bytes.  DH = type of breakpoint.  DL must equal 1, 2, or 4.
  2892.   Alternative values for DH are:  0 - execution breakpoint, 1 - write
  2893.   breakpoint, and 2 - read/write breakpoint.
  2894.   Return Registers:  EAX = error code.  AX = XLIB error code.  If DPMI is
  2895.   installed, then the high word of EAX will equal a DPMI 1.0 error code (if
  2896.   provided by host).  EBX = handle for releasing the breakpoint.
  2897.   Details:
  2898.      The linear address supplied in EAX must have alignment equal to the length
  2899.   supplied in DL.
  2900.      If an execute breakpoint is specified (DH = 0), then the length must equal
  2901.   1 (DL = 1).
  2902.      Exception #1 is generated upon access of any byte within the breakpoint
  2903.   field.
  2904.      Up to four data breakpoints may be simultaneously defined.
  2905.      Under all protected-mode configurations except DPMI .9, the handle will
  2906.   equal the number of the assigned debug breakpoint register.  The DPMI .9
  2907.   specifications have no requirements for handle values.
  2908.      The exception report will print the value of DR6 (debug status register).
  2909.   This register may be examined to determine which breakpoint generated the
  2910.   exception.  The lowest four bits contain this information.  Set bits indicate
  2911.   that the corresponding breakpoint condition is satisfied.
  2912.      The presented value for DR6 is not relevant under DPMI .9.  Under DPMI 1.0,
  2913.   the presented value is for a virtual DR6 and will not have exact
  2914.   correspondence with the actual DR6.  In particular, bits 15 and 13 (BT and BD
  2915.   bits) are undefined.  Under XLIB and VCPI mode switching, the actual DR6 is
  2916.   presented.
  2917.      If the programmer chooses to resume execution from the exception handler,
  2918.   then DR6 will be cleared before resumption.
  2919.      Breakpoints are globally enabled under XLIB and VCPI mode switching.
  2920.   Breakpoint visibility under DPMI will depend upon the host.
  2921.      Under XLIB or VCPI mode switching, exact breakpoint matching is used for
  2922.   386 processors.  This means that the reported CS:EIP points to the instruction
  2923.   which generated the exception rather than the instruction after.  Such
  2924.   behavior is always the case with 486 and Pentium processors.  Trapping
  2925.   behavior under DPMI will depend upon the host.
  2926.  
  2927.   FREEWATCH (Free Watchpoint)
  2928.   Purpose:  Release previously allocated debug data breakpoint.
  2929.   CPU Mode:  Real
  2930.   Registers at Call:  EAX = watchpoint handle.
  2931.   Return Registers:  EAX = error code.  AX = XLIB error code.  If DPMI is
  2932.   installed, then the high word of EAX will equal a DPMI 1.0 error code (if
  2933.   provided by host).
  2934.  
  2935.  
  2936.  
  2937.                                         41
  2938.  
  2939.  
  2940.  
  2941.  
  2942.  
  2943.  
  2944.   RESETWATCH (Reset Watchpoints)
  2945.   Purpose:  Release all previously allocated debug data breakpoints.
  2946.   CPU Mode:  Real
  2947.   Registers at Call:  None
  2948.   Return Registers:  EAX = error code.  AX = XLIB error code.  If DPMI is
  2949.   installed, then the high word of EAX will equal a DPMI 1.0 error code (if
  2950.   provided by host).
  2951.   Details:  Either RESETWATCH or PMRESETWATCH will be called automatically by
  2952.   XLIBE upon program termination.
  2953.  
  2954.   PMSETWATCH (Protected Mode - Set Watchpoint)
  2955.   Purpose:  Set a data breakpoint for generation of exception #1.
  2956.   CPU Mode:  Protected
  2957.   Details:  This routine is the protected-mode version of SETWATCH.  See
  2958.   SETWATCH for details.
  2959.  
  2960.   PMFREEWATCH (Protected Mode - Free Watchpoint)
  2961.   Purpose:  Release previously allocated debug data breakpoint.
  2962.   CPU Mode:  Protected
  2963.   Details:  This routine is the protected-mode version of FREEWATCH.  See
  2964.   FREEWATCH for details.
  2965.  
  2966.   PMRESETWATCH (Protected Mode - Reset Watchpoints)
  2967.   Purpose:  Release all previously allocated debug data breakpoints.
  2968.   CPU Mode:  Protected
  2969.   Details:  This routine is the protected-mode version of RESETWATCH.  See
  2970.   RESETWATCH for details.
  2971.  
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.  
  2978.  
  2979.  
  2980.  
  2981.  
  2982.  
  2983.  
  2984.  
  2985.  
  2986.  
  2987.  
  2988.  
  2989.  
  2990.  
  2991.  
  2992.  
  2993.  
  2994.  
  2995.  
  2996.  
  2997.  
  2998.  
  2999.  
  3000.  
  3001.                                         42
  3002.  
  3003.  
  3004.  
  3005.  
  3006.  
  3007.  
  3008.                    Appendix A: Description of XLIB Public Data
  3009.  
  3010.  
  3011.        The following is a summary of most public symbols for XLIB data.  These
  3012.   symbols pertain to segment DSEG except where otherwise noted.  This summary
  3013.   excludes many of the symbols presented in tables one through four.  All XLIB
  3014.   symbols conform to the PASCAL naming convention.
  3015.  
  3016.   Symbol:  CALL32PTR (CALL32 Pointer)
  3017.   Symbol Type:  DWORD
  3018.   Default Setting:  Far 16-bit protected-mode address of CALL32 procedure
  3019.   Description:  This location is a pointer to the CALL32 procedure and is
  3020.   included to facilitate intersegment calls.  The contents of the location
  3021.   should not be changed.
  3022.  
  3023.   Symbols:  CCODEPTR/CCODE (Condition Code Pointer/Condition Code)
  3024.   Symbol Types:  DWORD/DWORD
  3025.   Default Settings:  CCODEPTR = linear address of CCODE.  CCODE = 0.
  3026.   Descriptions:  XLIB interrupt handlers will place flags in the condition code
  3027.   to signal the occurrence of the interrupt.  Flags are placed for FPU
  3028.   exceptions and hot key presses.  CCODEPTR initially contains the linear
  3029.   address of CCODE.  CCODEPTR may be changed by the user, but must point to a
  3030.   DWORD in conventional memory.  The user is responsible for initializing the
  3031.   condition code.
  3032.  
  3033.   Symbol:  CPUINTFLAGS (CPU Interrupt Flags)
  3034.   Symbol Type:  DWORD
  3035.   Default Setting:  FFFBH
  3036.   Description:  One can selectively enable installation of CPU exception
  3037.   handlers in XLIBE by appropriately setting the bits in CPUINTFLAGS.  A set bit
  3038.   enables the corresponding exception.  By default, installation of handlers for
  3039.   all exceptions is enabled except for nonmaskable interrupts, FPU exceptions,
  3040.   and alignment checks.  FPU exceptions are handled through IRQ 13 rather than
  3041.   the CPU.  Alignment checks are not typically made in the IBM AT architecture.
  3042.   The settings in CPUINTFLAGS have no effect on XLIB.
  3043.  
  3044.   Symbol:  CSDSEGSEL (Code Segment Copy of DSEGSEL)
  3045.   Symbol Type:  WORD
  3046.   Default Setting:  Varies with operating environment
  3047.   Description:  This is a WORD in TSEG containing the value of the DSEG
  3048.   selector.  Since XLIB selector values are not constant, they must be read from
  3049.   memory locations.  Such locations are placed in segment DSEG (see Table 1);
  3050.   however, one must know the DSEG selector to read these locations.  Therefore,
  3051.   a copy of the DSEG selector is placed in the code segment where it can always
  3052.   be found.
  3053.  
  3054.   Symbols:  CSEGVAL, TSEGVAL, DSEGVAL, DGROUPVAL (Segment Values)
  3055.   Symbol Types:  WORD
  3056.   Default Settings:  CSEG, TSEG, DSEG, DGROUP
  3057.   Descriptions:  These are memory locations initialized to the respective
  3058.   segment values.  Code in TSEG should not contain segment constants since DOS
  3059.   may not be able to handle them in relocation edits.  Read these locations to
  3060.   get segment values.  User segments should be handled the same way.  These
  3061.   locations should not be changed.
  3062.  
  3063.  
  3064.  
  3065.                                         43
  3066.  
  3067.  
  3068.  
  3069.  
  3070.  
  3071.  
  3072.   Symbols:  FILEBUFADR/FILEBUFSIZE (File Buffer Specifications)
  3073.   Symbol Types:  DWORD/WORD
  3074.   Default Settings:  Varies with operating environment
  3075.   Descriptions:  FILEBUFADR contains the linear address of the internal file
  3076.   buffer in XLIB.  FILEBUFSIZE contains the size of the buffer in bytes.  This
  3077.   buffer is used only by the file management routines.  The size and location of
  3078.   the buffer will depend upon the operating environment.  If DPMI is active,
  3079.   then the buffer will be slightly larger than 2K; it is otherwise slightly
  3080.   larger than 1K.  These location should be read only after initialization.
  3081.  
  3082.   Symbol:  FPUCW (Floating Point Unit Control Word)
  3083.   Symbol Type:  WORD
  3084.   Default Setting:  0332H
  3085.   Description:  FPUCW is optionally loaded to the FPU control word by CALLPM and
  3086.   ENTERPM.  The default sets rounding control to nearest, precision control to
  3087.   64 bits, and unmasks exceptions for overflow, zero divide, and invalid
  3088.   operations.  Exceptions for underflow, precision, and denormalized operations
  3089.   are masked, and are therefore handled internally by the FPU.  Set bit 2 of
  3090.   OFLAGS to enable FPU save/restore and load of FPUCW.
  3091.  
  3092.   Symbol:  GDT (Global Descriptor Table)
  3093.   Symbol Type:  DWORD
  3094.   Default Setting:  Not applicable
  3095.   Description:  GDT is the first DWORD in the global descriptor table.  This
  3096.   table contains 40 descriptors.  The last sixteen are available for use.
  3097.   Descriptors must be placed in the table by direct writes.  The first available
  3098.   descriptor begins at DSEG offset OFFSET GDT + 100H.  The selector values are
  3099.   simply the offsets from GDT.  Hence, the selector value for the first
  3100.   descriptor is 100H.  The global descriptor table should not be used under
  3101.   DPMI.
  3102.  
  3103.   Symbol:  HOTKEY (Hot Key)
  3104.   Symbol Type:  WORD
  3105.   Default Setting:  0H
  3106.   Description:  HOTKEY specifies the hot key for the keyboard interrupt handler.
  3107.   The low byte of HOTKEY specifies the scan code for the key.  The upper byte
  3108.   specifies the state of the shift keys.  Bit 8 specifies SHIFT; bit 9 specifies
  3109.   CTLR, and bit 10 specifies ALT.  Set bits mean that the designated key must be
  3110.   pressed.  All other bits are ignored.  When the hot key is pressed, the XLIB
  3111.   keyboard interrupt handler will record the hot key flag at the DWORD whose
  3112.   linear address is stored at CCODEPTR.  The default setting for HOTKEY is 0.
  3113.   This setting effectively disables hot key detection since no key has a zero
  3114.   scan code.
  3115.  
  3116.  
  3117.  
  3118.  
  3119.  
  3120.  
  3121.  
  3122.  
  3123.  
  3124.  
  3125.  
  3126.  
  3127.  
  3128.  
  3129.                                         44
  3130.  
  3131.  
  3132.  
  3133.  
  3134.  
  3135.  
  3136.   Symbol:  IFLAGS (Initialization Flags)
  3137.   Symbol Type:  WORD
  3138.   Default Setting:  0200
  3139.   Description:  IFLAGS is used by INITXLIB to control the initialization
  3140.   process.
  3141.      Bit 0 determines DPMI/VCPI priority in the event that both interfaces are
  3142.   present.  If this bit is clear then DPMI will be installed; otherwise, VCPI
  3143.   will be installed.
  3144.      Bit 1 disables protected-mode exception trapping in XLIBE.  It also
  3145.   disables remap of hardware interrupts.  XLIBE is functionally equivalent to
  3146.   XLIB when this bit is set.  The bit does not effect XLIB.
  3147.      Bit 2 disables real-mode exception trapping in XLIBE.  This bit is
  3148.   meaningful only if DPMI 1.0 is installed and if bit 1 of IFLAGS is clear.  The
  3149.   bit does not effect XLIB.
  3150.      Bit 3 disables XLIBE usage of debug registers.  This bit is meaningful only
  3151.   if bit 1 of IFLAGS is clear.  The bit does not effect XLIB.
  3152.      The upper byte of IFLAGS determines the strategy to be used by INITXLIB if
  3153.   conventional memory is to be allocated.  The possible strategies are presented
  3154.   in Table 2.  The default causes XLIB to allocate from the highest available
  3155.   address in conventional memory.
  3156.  
  3157.   Symbol:  INLINERMPTR (INLINERM Pointer)
  3158.   Symbol Type:  DWORD
  3159.   Default Setting:  Far 16-bit protected-mode address of INLINERM procedure
  3160.   Description:  This location is a pointer to the INLINERM procedure and is
  3161.   included to facilitate intersegment calls.  The contents of the location
  3162.   should not be changed.
  3163.  
  3164.   Symbols:  IRQ0INTNO/IRQ8INTNO (IRQ X Interrupt Number)
  3165.   Symbol Types:  BYTE/BYTE
  3166.   Default Settings:  Varies with operating environment
  3167.   Descriptions:  Specifies the interrupt number assigned to IRQ X.  IRQs 0
  3168.   through 7 and IRQs 8 through 15 are assigned to contiguous interrupt numbers.
  3169.   These locations are valid only after call to INITXLIB.  Typically, IRQ 0 is
  3170.   assigned to interrupt 8, and IRQ 8 is assigned to interrupt 70H; however,
  3171.   these assignments may have been changed by system software or by XLIBE.
  3172.  
  3173.   Symbol:  OFLAGS (Operation Flags)
  3174.   Symbol Type:  WORD
  3175.   Default Setting:  Varies with operating environment
  3176.   Description:  OFLAGS controls post-initialization operation of XLIB.
  3177.      Setting bit 0 enables XLIB hardware interrupt handlers.  These handlers
  3178.   will continue to receive interrupts but will always cascade them when the bit
  3179.   is clear.  XLIB sets this bit only at calls to CALLPM and ENTERPM and then
  3180.   clears the bit upon return.  When the bit is clear, hot key detection is
  3181.   disabled, and the XLIB FPU interrupt handler is disabled.
  3182.      Setting bit 1 causes all FPU interrupts to be cascaded to the inherited
  3183.   real-mode interrupt handler.  This bit is initialized by INITXLIB.  It is set
  3184.   if no FPU is present; it is otherwise cleared.
  3185.      Setting bit 2 enables FPU save/restore in CALLPM and ENTERPM.  Setting this
  3186.   bit also causes load of FPUCW to the FPU control word.  The bit is clear by
  3187.   default.
  3188.  
  3189.  
  3190.  
  3191.  
  3192.  
  3193.                                         45
  3194.  
  3195.  
  3196.  
  3197.  
  3198.  
  3199.  
  3200.   Symbol:  PAGESIZE (Page Size)
  3201.   Symbol Type:  DWORD
  3202.   Default Setting:  Varies with operating environment
  3203.   Description:  This memory location contains the minimum unit (in bytes) for
  3204.   extended memory allocation.  PAGESIZE is initialized by INITXLIB.  It will
  3205.   contain 4096 for VCPI, 1024 for XMS, and 16 for clean configurations.  Values
  3206.   can vary under DPMI but will typically equal 4096.  Extended memory requests
  3207.   are rounded up to the nearest integer multiple of PAGESIZE.  Extended memory
  3208.   blocks will be PAGESIZE aligned.
  3209.  
  3210.   Symbols:  PIC1BASEINT/PIC2BASEINT (Programmable Interrupt Controller X Base
  3211.   Interrupt)
  3212.   Symbol Types:  BYTE/BYTE
  3213.   Default Settings:  50H/58H
  3214.   Description:  If exception trapping is enabled under XLIBE and if XLIB or VCPI
  3215.   mode switching is to be used, then hardware interrupts will be remapped during
  3216.   initialization.  PIC1BASEINT specifies the starting interrupt number for the
  3217.   master programmable interrupt controller (IRQs 0-7).  PIC2BASEINT specifies
  3218.   the starting interrupt number for the slave controller (IRQs 8-15).  These
  3219.   variables should be set to the desired values before the call to INITXLIB.
  3220.   Both interrupt numbers must be evenly divisible by eight.  These variables
  3221.   have no effect on XLIB.
  3222.  
  3223.   Symbols:  PMDS, PMES, PMFS, PMGS (Protected-Mode Segments)
  3224.   Symbol Types:  WORD
  3225.   Default Settings:  FLATDSEL, TSEGDSEL, DSEGSEL, DGROUPSEL
  3226.   Descriptions:  These memory locations are loaded to data segment registers by
  3227.   CALLPM and ENTERPM before transferring control to the protected-mode target.
  3228.   These locations are respectively loaded to DS, ES, FS, and GS.  The contents
  3229.   of these locations may be changed to any legal selectors after the call to
  3230.   INITXLIB.
  3231.  
  3232.   Symbols:  RMDS, RMES (Real-Mode Segments)
  3233.   Symbol Types:  WORD
  3234.   Default Settings:  DGROUP, DSEG
  3235.   Descriptions:  These memory locations are loaded to data segment registers by
  3236.   CALLRM before transferring control to the real-mode target.  These locations
  3237.   are respectively loaded to DS and ES.  The contents of these locations may be
  3238.   changed if desired.
  3239.  
  3240.  
  3241.  
  3242.  
  3243.  
  3244.  
  3245.  
  3246.  
  3247.  
  3248.  
  3249.  
  3250.  
  3251.  
  3252.  
  3253.  
  3254.  
  3255.  
  3256.  
  3257.                                         46
  3258.  
  3259.  
  3260.  
  3261.  
  3262.  
  3263.  
  3264.                            Appendix B: XLIB Error Codes
  3265.  
  3266.  
  3267.        XLIB error codes are always returned in AX.  In many cases, the high word
  3268.   of EAX will be returned with specific information about the error, such as
  3269.   XMS, DPMI, or DOS error codes.
  3270.        Although error codes are not provided in the DPMI .9 specification, many
  3271.   DPMI .9 hosts do return DPMI 1.0 error codes.  DPMI 1.0 error codes may in
  3272.   fact be DOS error codes returned to the DPMI host by DOS.  If the sign bit
  3273.   (bit 15) of the error code is clear, then the error code was issued by DOS.
  3274.        Error codes have been changed from earlier versions of XLIB.
  3275.  
  3276.   Condition Code Flags
  3277.   01H     FPU exception
  3278.   02H     Hot key pressed
  3279.  
  3280.   General Errors
  3281.   10H     Unable to identify operating environment
  3282.   11H     DOS memory allocation failure
  3283.   12H     DOS memory release error
  3284.   13H     Failed to enable A20
  3285.   14H     Insufficient logical address space
  3286.   15H     Insufficient number of handles
  3287.   16H     Bad handle
  3288.   17H     Bad selector
  3289.   18H     Bad address
  3290.   19H     Unable to create file
  3291.   1AH     Unable to open file
  3292.   1BH     Unable to read file
  3293.   1CH     Unable to write file
  3294.   1DH     Unable to set file pointer
  3295.   1EH     Unable to close file
  3296.   1FH     Disk full
  3297.   20H     Unable to remap hardware interrupts
  3298.   21H     Bad debug data watchpoint specification
  3299.  
  3300.   Errors Occurring Under DPMI (See Appendix C for codes returned by DPMI)
  3301.   30H     Protected mode initialization failure
  3302.   31H     Descriptor allocation error
  3303.   32H     Descriptor installation error
  3304.   33H     Unable to switch protected mode interrupt vector
  3305.   34H     Insufficient extended memory error
  3306.   35H     Extended memory allocation error
  3307.   36H     Extended memory release error
  3308.   37H     DOS memory allocation error
  3309.   38H     DOS memory release error
  3310.   39H     Unable to set descriptor base address
  3311.   3AH     Physical address mapping error
  3312.   3BH     Unable to get protected-mode exception vector
  3313.   3CH     Unable to set protected-mode exception vector
  3314.   3DH     Unable to get real-mode exception vector
  3315.   3EH     Unable to set real-mode exception vector
  3316.   3FH     Unable to set debug data breakpoint
  3317.   40H     Unable to release debug data breakpoint
  3318.  
  3319.  
  3320.  
  3321.                                         47
  3322.  
  3323.  
  3324.  
  3325.  
  3326.  
  3327.  
  3328.   Errors Occurring Under XMS (See Appendix D for codes returned by XMS)
  3329.   50H     Unable to enable A20
  3330.   51H     Unable to measure available extended memory
  3331.   52H     Insufficient extended memory error
  3332.   53H     Extended memory allocation error
  3333.   54H     Unable to lock extended memory
  3334.   55H     Unable to unlock extended memory
  3335.   56H     Extended memory release error
  3336.  
  3337.   Errors Occurring Under VCPI
  3338.   60H     Error in determining protected mode entry point
  3339.   61H     Unable to determine physical address of DOS memory page
  3340.   62H     Unable to determine hardware interrupt mappings
  3341.   63H     Insufficient extended memory error
  3342.   64H     Unable to determine number of free extended memory pages
  3343.   65H     Extended memory allocation error
  3344.   66H     Extended memory release error
  3345.   67H     Hardware interrupt remap error
  3346.  
  3347.  
  3348.  
  3349.  
  3350.  
  3351.  
  3352.  
  3353.  
  3354.  
  3355.  
  3356.  
  3357.  
  3358.  
  3359.  
  3360.  
  3361.  
  3362.  
  3363.  
  3364.  
  3365.  
  3366.  
  3367.  
  3368.  
  3369.  
  3370.  
  3371.  
  3372.  
  3373.  
  3374.  
  3375.  
  3376.  
  3377.  
  3378.  
  3379.  
  3380.  
  3381.  
  3382.  
  3383.  
  3384.  
  3385.                                         48
  3386.  
  3387.  
  3388.  
  3389.  
  3390.  
  3391.  
  3392.                          Appendix C: DPMI 1.0 Error Codes
  3393.  
  3394.  
  3395.        DPMI 1.0 error codes may in fact be DOS error codes returned to the DPMI
  3396.   host by DOS.  If the sign bit (bit 15) of the error code is clear, then the
  3397.   error code was issued by DOS.
  3398.  
  3399.  
  3400.   Number   Explanation
  3401.   8001H    Unsupported function
  3402.   8002H    Invalid state for requested operation
  3403.   8003H    System integrity would be endangered
  3404.   8004H    Deadlock situation detected by host
  3405.   8005H    Serialization request cancelled
  3406.   8010H    Resource unavailable
  3407.   8011H    Host unable to allocate descriptor
  3408.   8012H    Linear memory unavailable
  3409.   8013H    Physical memory unavailable
  3410.   8014H    Backing store unavailable
  3411.   8015H    Callback specifications cannot be allocated
  3412.   8016H    Cannot allocate handle
  3413.   8017H    Lock count limits exceeded
  3414.   8018H    Resource owned exclusively by another client
  3415.   8019H    Resource already shared by another client
  3416.   8021H    Invalid value
  3417.   8022H    Invalid selector
  3418.   8023H    Invalid handle
  3419.   8024H    Invalid callback
  3420.   8025H    Invalid linear address
  3421.   8026H    Request not supported by hardware
  3422.  
  3423.  
  3424.  
  3425.  
  3426.  
  3427.  
  3428.  
  3429.  
  3430.  
  3431.  
  3432.  
  3433.  
  3434.  
  3435.  
  3436.  
  3437.  
  3438.  
  3439.  
  3440.  
  3441.  
  3442.  
  3443.  
  3444.  
  3445.  
  3446.  
  3447.  
  3448.  
  3449.                                         49
  3450.  
  3451.  
  3452.  
  3453.  
  3454.  
  3455.  
  3456.                            Appendix D: XMS Error Codes
  3457.  
  3458.  
  3459.   Number   Explanation
  3460.   80H      Function not implemented
  3461.   81H      VDISK was detected
  3462.   82H      An A20 error occurred
  3463.   8EH      General driver error
  3464.   8FH      Unrecoverable driver error
  3465.   90H      HMA does not exist
  3466.   91H      HMA is already in use
  3467.   92H      Attempt to allocate less than HMAMIN of HMA
  3468.   93H      HMA is not allocated
  3469.   94H      A20 is still enabled
  3470.   A0H      All extended memory is allocated
  3471.   A1H      All available handles are allocated
  3472.   A2H      Invalid handle
  3473.   A3H      Source handle is invalid
  3474.   A4H      Source offset is invalid
  3475.   A5H      Destination handle is invalid
  3476.   A6H      Destination offset is invalid
  3477.   A7H      Length is invalid
  3478.   A8H      Move has an invalid overlap
  3479.   A9H      Parity error
  3480.   AAH      Block is not locked
  3481.   ABH      Block is locked
  3482.   ACH      Block lock count overflow
  3483.   ADH      Lock failed
  3484.   B0H      Only a smaller upper memory block (UMB) is available
  3485.   B1H      No UMB's are available
  3486.   B2H      UMB segment number is invalid
  3487.  
  3488.  
  3489.  
  3490.  
  3491.  
  3492.  
  3493.  
  3494.  
  3495.  
  3496.  
  3497.  
  3498.  
  3499.  
  3500.  
  3501.  
  3502.  
  3503.  
  3504.  
  3505.  
  3506.  
  3507.  
  3508.  
  3509.  
  3510.  
  3511.  
  3512.  
  3513.                                         50
  3514.  
  3515.  
  3516.  
  3517.  
  3518.  
  3519.  
  3520.                Appendix E: Calling Protected-Mode Libraries From C
  3521.  
  3522.  
  3523.        This appendix contains a C version of the BASIC program presented in
  3524.   Example 3.  Microsoft C version 7.0 is used to create a float array.  C then
  3525.   calls a protected-mode assembly language procedure in a library to sum the
  3526.   elements of the array.  The assembly language library in Example 3 will work
  3527.   here with no modification.  C calls a real-mode procedure called SUMARRAY.
  3528.   This procedure then transfers control to a 32-bit protected-mode procedure
  3529.   called SUMARRAY32.  The latter procedure performs the actual calculations.
  3530.        Parameters defining the array are placed in a contiguous block of memory
  3531.   defined by a C structure.  C passes the address of this structure to the
  3532.   library.  The first four bytes in the structure are reserved for error codes.
  3533.   The linear address of the structure is placed in CCODEPTR so that any FPU
  3534.   exceptions will be recorded by the FPU interrupt handler in the error code
  3535.   location.  The SUMARRAY32 procedure will also record an error if the parameter
  3536.   defining the number of elements to be summed is zero.
  3537.        The C code is somewhat simpler than the corresponding BASIC code because
  3538.   conventional memory does not have to be released prior to calling INITXLIB.
  3539.   This follows because C does not claim all DOS memory as does BASIC.
  3540.        C is more powerful than BASIC in that it can access data under external
  3541.   symbols whereas BASIC cannot.  Access to XLIB public data is made possible in
  3542.   C by including the header file called XLIB.H.  This file makes all XLIB public
  3543.   data and public real-mode procedures visible to C.  It also contains
  3544.   declarations which adapt the PASCAL conventions of XLIB.
  3545.        A Borland version of this program and of the accompanying library may be
  3546.   found in the files EXAMP3B.C and EXAMP3B.ASM.
  3547.  
  3548.  
  3549.  
  3550.  
  3551.  
  3552.  
  3553.  
  3554.  
  3555.  
  3556.  
  3557.  
  3558.  
  3559.  
  3560.  
  3561.  
  3562.  
  3563.  
  3564.  
  3565.  
  3566.  
  3567.  
  3568.  
  3569.  
  3570.  
  3571.  
  3572.  
  3573.  
  3574.  
  3575.  
  3576.  
  3577.                                         51
  3578.  
  3579.  
  3580.  
  3581.  
  3582.  
  3583.  
  3584.   _____________________________________________________________________________
  3585.   /*The following Microsoft C 7.0 program should be linked with the assembly
  3586.   language library in Example 3.  Combine the library with XLIB.LIB using the
  3587.   Microsoft LIB utility.  The C program first initializes XLIB.  Next, it
  3588.   creates a float array.  A control block for SUMARRAY is then constructed
  3589.   and the call to SUMARRAY is executed.  Finally, the condition code in the
  3590.   control block is inspected and results are printed.*/
  3591.  
  3592.   #include <stdio.h>
  3593.   #include <xlib.h>
  3594.  
  3595.   extern long __far __pascal LINADR(void __far *ptr);
  3596.   extern void __far __pascal SUMARRAY(void __far *ptr);
  3597.  
  3598.   struct arraydata            /*Structure for passing arguments to SUMARRAY*/
  3599.   {
  3600.     long condcode;
  3601.     long n;
  3602.     long address;
  3603.     float sum;
  3604.   } ad;
  3605.  
  3606.   main()
  3607.   {
  3608.     int i;
  3609.     long temp;
  3610.     float a[101];
  3611.     temp = INITXLIB();        /*Initialize XLIB*/
  3612.     if (temp != 0)            /*See if an error occurred*/
  3613.     {
  3614.       printf("Initialization Error:  %lX\n",temp);
  3615.       return 0;
  3616.     }
  3617.  
  3618.     for(i = 0; i <= 100; i++)   /*Initialize a[]*/
  3619.       a[i] = i;
  3620.  
  3621.     ad.condcode = 0;            /*Initialize the condition code*/
  3622.     ad.n = 50;                  /*Will sum the first 50 elements in a[]*/
  3623.     ad.address = LINADR(a);     /*Compute and record linear address of a[]*/
  3624.  
  3625.     SUMARRAY(&ad);              /*Sum the array elements*/
  3626.     if (ad.condcode != 0)       /*See if an FPU error occurred*/
  3627.     {
  3628.       printf("Error:  %lX\n",ad.condcode);
  3629.       return 0;
  3630.     }
  3631.     printf("Sum:  %f\n",ad.sum);   /*The sum should be 1225*/
  3632.   }
  3633.   _____________________________________________________________________________
  3634.  
  3635.  
  3636.  
  3637.  
  3638.  
  3639.  
  3640.  
  3641.                                         52
  3642.  
  3643.  
  3644.  
  3645.  
  3646.  
  3647.  
  3648.                           Appendix F: Technical Support
  3649.  
  3650.  
  3651.        Technical support for XLIB is provided to both registered and
  3652.   unregistered users; however, the registration fee is $60 per copy for those
  3653.   who have used technical support or who intend to use it.  The additional
  3654.   charges will be waived for those who discover bugs in the XLIB libraries.
  3655.        Before contacting TechniLib with a problem, the following steps should be
  3656.   taken:
  3657.  
  3658.   1) Ensure that your own program always checks the error codes returned by XLIB
  3659.   procedures.  These codes will likely resolve the problem.  If not, then make
  3660.   note of the code.
  3661.  
  3662.   2) Attempt to execute your program under DPMI, VCPI, and in the absence of
  3663.   both.  If the problem relates to memory management, then also attempt to
  3664.   execute your program in the presence of XMS but in the absence of DPMI and
  3665.   VCPI, then attempt to execute in the absence of all three interfaces.  It will
  3666.   generally be found that the problem occurs only under a specific interface.
  3667.   If so, then note the interface under which the problem occurs.
  3668.  
  3669.   3) If the problem occurs only under one interface, then attempt to execute
  3670.   your program under different implementations of the interface.  For example, a
  3671.   DPMI host is contained in Windows 3.1, 386MAX, QDPMI, and OS/2 2.x.  Try
  3672.   executing your program under each host and make note of the results.  Problems
  3673.   occurring only under one host are generally indicative of bugs in the host
  3674.   rather than XLIB.
  3675.  
  3676.   4) Try different options on your memory management software.
  3677.  
  3678.   5) Try different options on your compiler, assembler, and linker.  It is
  3679.   sometimes the case that code is not processed properly under some options.
  3680.  
  3681.   6) If the problem results in a processor exception, then run the program with
  3682.   one of the exception trapping libraries (e.g. XLIBE.LIB) and make note of the
  3683.   report that is presented as of the exception.
  3684.  
  3685.        Technical support is available at:
  3686.  
  3687.   Dr. David Pyles
  3688.   TechniLib Company
  3689.   P.O. Box 6818
  3690.   Jackson, Ms. 39282
  3691.   (601) 372-7433
  3692.  
  3693.   Problems may also be reported by CompuServe mail to CIS ID: 74730,167.
  3694.  
  3695.  
  3696.  
  3697.  
  3698.  
  3699.  
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.                                         53
  3706.  
  3707.  
  3708.  
  3709.  
  3710.  
  3711.  
  3712.                  Appendix G: The SWITCHPM and SWITCHRM Procedures
  3713.  
  3714.  
  3715.        SWITCHPM and SWITCHRM are the primitive mode-switch routines used by
  3716.   nearly all XLIB procedures requiring execution in both real and protected
  3717.   modes.  They are made public for users who need to perform mode switching
  3718.   tasks not otherwise provided by XLIB.  These routines do not conform to the
  3719.   general conventions followed by other XLIB procedures; consequently, they are
  3720.   presented in an appendix.
  3721.        In some cases the programmer may deem CALLPM and ENTERPM to be too slow
  3722.   for a particular mode switching task.  Both procedures consume time in storing
  3723.   register state and in making other preparations for abrupt or unpredictable
  3724.   exits from protected mode (via jumps to RETPM/EXITPM or FPU exceptions).
  3725.   SWITCHPM and SWITCHRM perform mode switches in minimum CPU time.
  3726.        The greatest limitation to CALLPM/ENTERPM is that these procedures are
  3727.   not reentrant; consequently, calls to these procedures cannot be nested.  This
  3728.   limitation could prove a problem if the programmer needed to perform a mode
  3729.   switch within a real-mode interrupt handler.  If the interrupt originated in
  3730.   protected mode, then CALLPM/ENTERPM could not be used to perform the mode
  3731.   switch.  Since interrupt handlers generally should not make assumptions about
  3732.   machine state, CALLPM/ENTERPM generally should not be used within such
  3733.   handlers.  Use SWITCHPM and SWITCHRM instead.
  3734.        A less likely situation occurs where the programmer first enters
  3735.   protected mode via CALLPM/ENTERPM then executes real-mode code with CALLRM.
  3736.   The real-mode code could not then switch to protected mode with CALLPM/ENTERPM
  3737.   since this would be nesting the procedures.
  3738.        Both SWITCHPM and SWITCHRM are near procedures in CSEG; therefore, they
  3739.   must be called from this segment.  SWITCHPM returns to the caller in 16-bit
  3740.   protected mode.  SWITCHRM returns to the caller in real mode.  Both procedures
  3741.   must be called with a stack in DSEG.
  3742.        SWITCHPM returns with CS = CSEGSEL and with all other segments equal to
  3743.   DSEGSEL.  All other registers, including the status flags, are preserved.
  3744.        SWITCHRM returns with CS = CSEG and with SS, DS, and ES set to DSEG.  FS
  3745.   and GS are undefined.  All other registers, including the status flags, are
  3746.   preserved.
  3747.        Each interrupt handler using SWITCHPM/SWITCHRM should have its own unique
  3748.   stack in DSEG in the event that nested interrupts occurred.
  3749.        After switching to 16-bit protected mode with SWITCHPM, one can transfer
  3750.   execution to a 32-bit segment with a far call.
  3751.        An example using the techniques discussed above is the MOVMEM function in
  3752.   the file EASYX.ASM.  This function is not an interrupt handler; however, it is
  3753.   designed to be called from such handlers and must therefore abide by the same
  3754.   principles.
  3755.  
  3756.  
  3757.  
  3758.  
  3759.  
  3760.  
  3761.  
  3762.  
  3763.  
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.                                         54
  3770.  
  3771.  
  3772.  
  3773.  
  3774.  
  3775.  
  3776.                               Appendix H: Debugging
  3777.  
  3778.  
  3779.        Unfortunately, neither Microsoft's CodeView nor Borland's Turbo Debugger
  3780.   are fully capable of debugging DOS-extended programs.  The real-mode areas of
  3781.   such programs can be debugged with these debuggers provided that no attempts
  3782.   are made to step into calls to protected-mode subroutines.  These subroutines
  3783.   must be debugged using other methods.  This appendix presents useful
  3784.   information that should aid the programmer toward this end.
  3785.        Debugging protected-mode code without the aid of a software debugger will
  3786.   not prove as limiting as it may sound.  In some cases there are better
  3787.   alternatives to software debuggers.  For example, many programmers waste time
  3788.   stepping through code with a debugger trying to resolve a processor exception
  3789.   condition when a quick resolution to the problem could be gained with a deeper
  3790.   understanding of the processor and the exception.
  3791.  
  3792.  
  3793.   Resolving Bugs Affecting Program Output
  3794.  
  3795.  
  3796.        Bugs which lead to erroneous program output can typically be diagnosed
  3797.   with strategically located print statements.  The XLIB archive contains a file
  3798.   call PMIO.INC which contains many protected-mode input/output routines,
  3799.   including a variety of print routines.  The print routines never call BIOS or
  3800.   DOS (they use direct screen writes); consequently, they will work fine both in
  3801.   interrupt handlers and the main thread of execution.
  3802.        A similar method for locating bugs involves strategic location of the INT
  3803.   3 instruction.  This instruction will generate a breakpoint exception.  The
  3804.   exception handlers in XLIBE will then give a complete report on register
  3805.   status as of the INT 3.  The programmer will then be given the option to
  3806.   resume execution.
  3807.  
  3808.  
  3809.   Resolving Bugs Causing Processor Exceptions
  3810.  
  3811.  
  3812.        Bugs causing processor exceptions will commonly occur in protected mode.
  3813.   These bugs can be particularly annoying because they often hang or reboot the
  3814.   machine or leave it in an unstable state.  However, the exception handlers in
  3815.   XLIBE should be able to successfully trap such bugs and then terminate the
  3816.   program in an orderly fashion.
  3817.        If the exception handlers in XLIBE fail to trap an exception, then it is
  3818.   likely the case that the bug is corrupting data or code used by the handlers.
  3819.   In any event, other methods may be used to track the bug.
  3820.        Once protected mode has been entered by calling either CALLPM or ENTERPM,
  3821.   one may return to the real-mode caller from any point in the code with a jump
  3822.   to either RETPM or EXITPM.  Therefore, if an unidentified instruction is
  3823.   generating exceptions in protected mode, one may locate the instruction by
  3824.   placing jumps to RETPM or ENTERPM at various locations in the code.  Initially
  3825.   the jumps should be placed early in the code and then moved forward.  This way
  3826.   the processor exception is avoided as it is searched, thereby saving the time
  3827.   necessary to reboot after the exception occurs.
  3828.  
  3829.  
  3830.  
  3831.  
  3832.  
  3833.                                         55
  3834.  
  3835.  
  3836.  
  3837.  
  3838.  
  3839.  
  3840.        XLIB is also designed such that INT 21H, function 4CH should terminate a
  3841.   program from either real or protected mode.  This provides an alternative way
  3842.   to exit the program before execution of a faulty instruction.
  3843.        Processor exceptions are not generally difficult to resolve if the
  3844.   programmer has a thorough understanding of the exception.  When an exception
  3845.   occurs, the exception handlers in XLIBE will print a report of register state
  3846.   to the screen.  The exception usually can be resolved by examining this
  3847.   report.  In particular, the report will contain the CS:EIP at which the
  3848.   exception occurred.  Using this information, the programmer can typically
  3849.   identify the problematic instruction by examining a list file generated by the
  3850.   assembler or compiler.
  3851.  
  3852.  
  3853.   Processor Exceptions and Their Causes
  3854.  
  3855.  
  3856.        Many processor exceptions issue error codes.  The exception handlers in
  3857.   XLIBE will always display such codes.  For all exceptions except page faults
  3858.   (exception #14), the error code will be nonzero if the processor can associate
  3859.   the exception with a particular segment, in which event, the error code
  3860.   identifies the segment.  In particular, bits 3-15 of the error code will be
  3861.   the selector for the segment.  Bit 1 will be set if this selector refers to a
  3862.   gate descriptor in the interrupt descriptor table.  If this bit is clear, then
  3863.   bit 2 identifies the selector as belonging to the global descriptor table (the
  3864.   bit is clear) or the local descriptor table (the bit is set).  Bit 0 is set if
  3865.   an event external to the program generated the exception.  Error codes are
  3866.   generated for exceptions:  8,10,11,12,13,14,15, and 17.
  3867.        The following is a summary of nearly all CPU exceptions:
  3868.  
  3869.   Exception #0, Divide Error - This exception occurs when a DIV or IDIV has been
  3870.   executed with a zero divisor, or when a quotient is too large to be contained
  3871.   in the targeted register (AL, AX, or EAX).
  3872.  
  3873.   Exception #1, Debug Exception - This exception occurs when a data breakpoint
  3874.   has been addressed, or upon execution of any instruction when single-step
  3875.   trapping has been enabled by setting the trap flag in the EFLAGS register.  If
  3876.   the exception is due to a data breakpoint, then the debug status register
  3877.   (DR6) identifies the breakpoint.  The lowest four bits of this register
  3878.   correspond to the four CPU breakpoint registers (DR0-DR3).  Set bits indicate
  3879.   that the corresponding breakpoint condition is satisfied.  Bit 14 of DR6 will
  3880.   be set if the exception is due to a single-step trap.  XLIB does not implement
  3881.   single-step trapping.
  3882.  
  3883.   Exception #3, Breakpoint - This exception is generated by INT 3.
  3884.  
  3885.   Exception #4, Overflow - This exception occurs when the INTO instruction is
  3886.   executed with the overflow flag being set.
  3887.  
  3888.   Exception #5, Bounds Check - This exception occurs when a BOUND instruction is
  3889.   executed with the operand violating the specified limits.
  3890.  
  3891.   Exception #6, Invalid Opcode - This exception occurs when an unrecognizable
  3892.   instruction is encountered in the code sequence.  Provided that there are no
  3893.   bugs in the compiler, this exception is almost certainly indicative of a bad
  3894.  
  3895.  
  3896.  
  3897.                                         56
  3898.  
  3899.  
  3900.  
  3901.  
  3902.  
  3903.  
  3904.   transfer (JMP, CALL, RET, or IRET) or of code corruption.  However, the
  3905.   exception can also be generated by instructions which are not intended for the
  3906.   current CPU mode (e.g. LLDT in real mode).
  3907.  
  3908.   Exception #7, Device Not Available - This exception occurs when an ESC
  3909.   instruction is executed under FPU emulation, or when a WAIT or ESC instruction
  3910.   is executed with the task-switch flag set in CR0.  Neither case is likely
  3911.   under XLIB.
  3912.  
  3913.   Exception #8, Double Fault - If an exception occurs while the processor is
  3914.   attempting to call an exception handler for a prior exception, and if the two
  3915.   exceptions cannot be serialized, then a double fault exception is generated.
  3916.   The error code for this exception is always zero.  Under XLIB, double faults
  3917.   are generally indicative of corruption of XLIB data used by the exception
  3918.   handlers.
  3919.  
  3920.   Exception #10, Invalid Task-State Segment - This exception occurs when an
  3921.   attempt is made to perform a task switch to a task having an invalid task-
  3922.   state segment.  Since XLIB performs task switches only when servicing
  3923.   exceptions themselves, this condition generally derives from code corruption
  3924.   or an invalid transfer.
  3925.  
  3926.   Exception #11, Segment Not Present - This exception occurs when an attempt is
  3927.   made to load a segment register with a descriptor which is marked not-present.
  3928.   A segment might be so marked if its contents have been swapped to disk.  Since
  3929.   all of the descriptors in XLIB are marked present, this exception likely
  3930.   derives from either corruption of the descriptor tables or an attempt to load
  3931.   a selector for an uninitialized descriptor.
  3932.  
  3933.   Exception #12, Stack Exception - Most stack exceptions are caused by limit
  3934.   violations in ESP or EBP.  For example, if the value in ESP is beyond the
  3935.   stack segment limit then a PUSH or POP will generate a stack exception.  It is
  3936.   also possible to have an "expand-down" stack where the segment limit is
  3937.   interpreted as a lower bound.  In such cases a stack exception could be
  3938.   generated if a value in ESP or EBP were too small.  Stack exceptions can also
  3939.   occur when SS is loaded with a selector for a descriptor which is marked not-
  3940.   present.
  3941.        Stack exceptions are generally common; however, they should be uncommon
  3942.   for XLIB programs while in protected mode.  This follows because XLIB uses an
  3943.   expand-up stack with a FFFFFFFFH limit.  Unless a switch has been made from
  3944.   the default stack, a stack exception under XLIB would typically be indicative
  3945.   of an invalid value being loaded to SS.
  3946.  
  3947.   Exception #13, General Protection Violation - This notorious exception is a
  3948.   catchall category on Intel processors that may be generated by a number of
  3949.   conditions.  However, three conditions account for nearly all such exceptions
  3950.   in practice.  These are:  1) An invalid value was loaded to a segment
  3951.   register, 2) An attempt was made to write to a code segment, or 3) Privilege
  3952.   rules were violated.
  3953.        Novices to protected mode often forget that segment registers must
  3954.   contain selectors in this mode.  Invalid values are also frequently popped
  3955.   from the stack into segment registers because the stack has been corrupted or
  3956.   because the stack pointer has not been properly maintained.
  3957.  
  3958.  
  3959.  
  3960.  
  3961.                                         57
  3962.  
  3963.  
  3964.  
  3965.  
  3966.  
  3967.  
  3968.        It must be remembered that writes to code segments are illegal in
  3969.   protected mode.  That is, one cannot write to a code segment through the CS
  3970.   register.  If code modification is desired, then load a data segment register
  3971.   with a selector to a descriptor which maps the code segment.  The CSEGDSEL and
  3972.   TSEGDSEL selectors are included in XLIB for this purpose.
  3973.        Protected-mode programs can run at four different privilege levels.  The
  3974.   highest is level zero and the lowest is level three.  XLIB programs will
  3975.   either be operating at the highest or lowest level.  Real-mode code always
  3976.   operates at level zero.  If a memory manager other than HIMEM.SYS is
  3977.   installed, then the machine is likely using virtual 8086 mode in lieu of real
  3978.   mode.  This is also the case under Windows and OS/2.  Virtual 8086 mode is a
  3979.   subset of protected mode wherein real mode is simulated.  Virtual 8086 mode
  3980.   always operates at level three.
  3981.        XLIB programs operating under DPMI will nearly always be at level three.
  3982.   Under VCPI the privilege level will depend upon the CPU mode. In protected
  3983.   mode the privilege level is zero, but in virtual 8086 mode the privilege level
  3984.   is three.  If neither DPMI nor VCPI are present, then XLIB programs will
  3985.   operate at level zero regardless of CPU mode.
  3986.        A program operating at level three is always under the supervision of
  3987.   another program at level zero.  The supervisor program will typically be a
  3988.   memory manager, Windows, or OS/2.  When the level three program attempts to
  3989.   execute a privileged instruction, the supervisor program is informed of the
  3990.   attempt by the processor.  The supervisor program may then execute the
  3991.   instruction in behalf of the unprivileged program, or it may terminate the
  3992.   unprivileged program with a privileged operation violation.
  3993.        Privileged instructions include reads and writes to IO ports and
  3994.   alterations to the interrupt flag.  Both of these types of instructions are
  3995.   generally permitted by the supervisor program.  However, other instructions (
  3996.   e.g. LGDT) will be denied.
  3997.        XLIB programmers have little occasion to use inadmissible privileged
  3998.   instructions.  Therefore privileged operation exceptions are nearly always
  3999.   indicative of code corruption or of an invalid transfer.
  4000.  
  4001.   Exception #14, Page Fault - A principal protection mechanism of the processor
  4002.   is its memory paging mode.  In this mode, memory is divided into 4K pages with
  4003.   the divisions being at 4K boundaries.  A memory manager can mark certain pages
  4004.   as being absent, read-only, or privileged.  An attempt to access an absent
  4005.   page or to write a read-only page always generates a page fault.  A privileged
  4006.   page can be accessed only by code operating at privilege levels higher than
  4007.   three; hence, a page fault will occur if code at level three attempts to
  4008.   access such pages.
  4009.        When a page fault occurs, an error code is supplied by the processor to
  4010.   describe the fault.  The error code is defined only in the lowest three bits.
  4011.   The zero bit describes whether the exception was caused by access of a not-
  4012.   present page (the bit is clear) or by a protection violation (the bit is set).
  4013.   Protection violations occur with attempts to access privileged pages or
  4014.   attempts to write read-only pages.  Bit two of the error code describes the
  4015.   privilege mode at which the exception occurred.  If the bit is clear, then the
  4016.   processor was executing in privileged mode.  In this case, protection
  4017.   violations are generated only by attempts to write read-only pages.  Bit one
  4018.   describes whether the fault took place during a read (the bit is clear) or a
  4019.   write (the bit is set).
  4020.        A page fault nearly always occurs because of an attempt to access a bad
  4021.   address or an attempt to transfer control to a bad address.  Memory managers
  4022.  
  4023.  
  4024.  
  4025.                                         58
  4026.  
  4027.  
  4028.  
  4029.  
  4030.  
  4031.  
  4032.   may mark unallocated pages as being not-present so that page faults will be
  4033.   generated whenever an attempt is made to access them.  This way the programmer
  4034.   will be alerted to bad address computations.  When VCPI is being used, XLIB
  4035.   becomes largely responsible for management of memory pages.  XLIB will in fact
  4036.   mark all unallocated pages as not present.  XLIB will mark all allocated pages
  4037.   as unprivileged and writable.
  4038.        The CR2 register will contain the address for the attempted access which
  4039.   generated the page fault.  CR2 is displayed by the exception handlers in
  4040.   XLIBE.
  4041.  
  4042.        Exception #13 and Exception #14 are by far the most common exceptions
  4043.   occurring under XLIB programming.  The following list of questions should be
  4044.   considered when attempting to resolve such exceptions.  These questions are
  4045.   also relevant to other exceptions:
  4046.  
  4047.   1)  Did you improperly maintain the stack pointer?
  4048.   2)  Did you forget that only selectors can be loaded to segment registers in
  4049.       protected mode?
  4050.   3)  Did you compute an address incorrectly?
  4051.   4)  Did you attempt to write to a code segment using CS override?
  4052.   5)  Did you fail to terminate a subroutine with RET?
  4053.   6)  Did you forget to properly match CALL (far or near) and RET (e.g. Did you
  4054.       forget that TSEG procedures must be near)?
  4055.   7)  Did you attempt to execute a privileged instruction?
  4056.   8)  Did you fail to terminate an interrupt handler with IRET?
  4057.   9)  Did you terminate a protected-mode interrupt handler with IRET instead of
  4058.       IRETD?
  4059.   10) If you are a TASM programmer, did you fail to specify LARGESTACK in your
  4060.       TSEG code?
  4061.  
  4062.  
  4063.   Miscellaneous Debugging Tips
  4064.  
  4065.  
  4066.        If a program runs unpredictably even when executed under identical
  4067.   circumstances, then the problem is due to an asynchronous phenomenon.  This
  4068.   almost definitely implies a problem with a hardware interrupt handler.  Such
  4069.   problems are oftentimes associated with the timer tick interrupt.  This theory
  4070.   can sometimes be tested by disabling certain hardware interrupts with the
  4071.   interrupt mask register.  The interrupt mask register for IRQs 0 through 7 is
  4072.   at port 21H.  The masks for IRQs 8 through 15 are at port A1H.  Set bits
  4073.   disable the corresponding IRQ.  For example, the following code sequence would
  4074.   disable the timer tick interrupt (IRQ 0):
  4075.  
  4076.   IN    AL,21H
  4077.   OR    AL,01H
  4078.   OUT   21H,AL
  4079.  
  4080.        If a program leaves the machine in an unstable state, then it is possible
  4081.   that XLIB is not being terminated correctly.  Try running an unregistered copy
  4082.   of XLIB and see if the registration reminder message is printed to the screen
  4083.   at program termination.  If not, then the XLIB termination handler is likely
  4084.   not receiving control.  This is likely due to code corruption.
  4085.  
  4086.  
  4087.  
  4088.  
  4089.                                         59
  4090.  
  4091.  
  4092.  
  4093.  
  4094.  
  4095.  
  4096.        Most subroutines intended for protected-mode will also operate in real
  4097.   mode when applied to small problems.  If a software debugger is deemed
  4098.   necessary, one might develop code in real mode and then convert to protected
  4099.   mode after debugging.  32-bit registers are admissible in real mode provided
  4100.   that no such register is used as a base or index register while containing a
  4101.   value greater than FFFFH.
  4102.  
  4103.  
  4104.  
  4105.  
  4106.  
  4107.  
  4108.  
  4109.  
  4110.  
  4111.  
  4112.  
  4113.  
  4114.  
  4115.  
  4116.  
  4117.  
  4118.  
  4119.  
  4120.  
  4121.  
  4122.  
  4123.  
  4124.  
  4125.  
  4126.  
  4127.  
  4128.  
  4129.  
  4130.  
  4131.  
  4132.  
  4133.  
  4134.  
  4135.  
  4136.  
  4137.  
  4138.  
  4139.  
  4140.  
  4141.  
  4142.  
  4143.  
  4144.  
  4145.  
  4146.  
  4147.  
  4148.  
  4149.  
  4150.  
  4151.  
  4152.  
  4153.                                         60