/*static char *SCCSID = "@(#)ptcnvrt.c  6.12 92/03/27";*/
#define SCCSID "@(#)ptcnvrt.c   6.12 92/03/27"
/***    PTCNVRT.C - DosDebug to PTrace Buffer Conversion Routines
 *
 *      SCCSID = @(#)ptcnvrt.c  6.12 92/03/27
 *
 *      THIS IS AN OS/2 VERSION 2.x SPECIFIC FILE
 *
 *       IBM/Microsoft Confidential
 *
 *       Copyright (c) IBM Corporation 1987, 1989
 *       Copyright (c) Microsoft Corporation 1987, 1989
 *
 *       All Rights Reserved
 *
 *      This file contains the conversion routines for converting the
 *      PTrace structure to the DosDebug structure, and vice versa.
 *
 *****************************************************************************
 *  Change                                                               Chg *
 *  Activity      Date    BY   Reason                                    Num *
 *  --------      ----    ---  -------                                   --- *
 *  DCR 1372    10/19/91  ARJ  Added for DOSPTRACE Support NEW           NEW *
 *  PTR B729735 11/14/91  ARJ  Correct IFDEF STATEMENTS (No CHG NUM)         *
 *  PTR B728776 11/17/91  ARJ  Return TRC_C_LIB_ret for first stop cmd   001 *
 *  PTR B730560 12/14/91  ARJ  Return TRC_C_GPF_ret for ACCESS VIOLATION 002 *
 *  PTR B732898 02/13/92  ARJ  Modify connect logic                      003 *
 *  PTR B733534 03/10/92  ARJ  Correct address conversion                004 *
 *  PTR B732918 03/13/92  ARJ  Warranty support did not want B733534     004 *
 *                             But I needed it to correct this one.      004 *
 *  PTR B734690 03/18/92  ARJ  Make Go Command Connect for Lotus Notes   005 *
 *  DFT 0046550 08/11/92  ARJ  Return VALUE instead of PID               006 *
 *  DEF 0058487 12/09/92  ARJ  Added Floating Point Support for 16 bit   007 *
 *  DEF 0070301 10/01/93  ARJ  Use Default Sever Support PJ09240         008 *
 *****************************************************************************
 ****************************************************************************/
#define INCL_DOS
#define INCL_TYPES                  /* DRV_\H\basedef.h typedefs            */
#define INCL_ERRORS                 /* DRV_\H\bseerr.h  OS/2 error numbers  */
#define FOR_EXEHDR     1            /*                                      */
#define FNO_NUM     FNO_PTCNVRT_C   /* DRV_\SRC\DOS\DOSINC\fnotask.h        */

#include <fnotask.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <os2.h>                    /* DRV_\H\                              */
#include <osconst.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <ostypes.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <osmac.h>                  /* DRV_\SRC\DOS\DOSINC\                 */
#include <infoseg.h>                /* DRV_\H\                              */
#include <vmowner.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <dossymc.h>                /* DRV_\SRC\DOS\DOSINC\                 */
#include <except.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <mi.h>                     /* DRV_\H\                              */
#include <pgexport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <vmexport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <selxport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <seldesc.h>                /* DRV_\H\                              */
#include <tkexport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <curdir.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <dirent.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <find.h>                   /* DRV_\SRC\DOS\DOSINC\                 */
#include <screen.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <sem.h>                    /* DRV_\SRC\DOS\DOSINC\                 */
#include <sigdef.h>
#include <tkptda.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <bseerr.h>                 /* DRV_\H\                              */
#include <tklocal.h>                /* DRV_\SRC\DOS\TASK\                   */
#include <proc.h>                   /* DRV_\SRC\DOS\DOSINC\                 */
#include <ldrxport.h>               /* DRV_\SRC\DOS\DOSINC\                 */
#include <ptrace.h>                 /* DRV_\SRC\DOS\DOSINC\                 */
#include <sched.h>                  /* DRV_\SRC\DOS\DOSINC\                 */
#include <string.h>                 /* DRV_\H\  and  DRV_\H386\             */
#include <dbgapi.h>
#include <dbgex.h>                  //d57053
#include <dbgin.h>                  //d57053


extern ULONG  PTCheck(kPDB_t *);
extern SELGetDescInfo(SEL sel, VMHOB hptda, struct seldesc_s *pseldesc_s);

#ifdef PTSTRICT
extern PTBugBug;                                /* DosPtrace debug flag         */
#endif

USHORT PT_CvtOUTBuff(kPTB *);
ULONG  PT_CvtINBuff(kPTB *);
ULONG  PTLinToSel(PPTDA uptda, ULONG ulAddr);
ULONG  PTSelToLin(PPTDA uptda, ULONG uSelector, ULONG uOffset, ULONG *newvalue);
ULONG  PTAlias(kPTB *);
extern VOID  PTModifyDesc(ULONG sel, ULONG laddr, ULONG limit, PPTDA pptda);
ULONG  PT_FPRegs(ULONG ulFunc, FP13STATE *AddrIO, FPSTATE *fpregs); /* A007 */



// Moved to sdata.asm for System Anchor Block - f122427
// FPSTATE fpregs;
extern   FPSTATE  fpregs;

#if RAS
#pragma code_seg("DEBUGSWAP_TEXT", "CODE")      /* swap PTRACE code */
#endif

/**** PT_CvtOUTBuff  -- Converts a DosDebug buffer structure to a PTrace
 *                     buffer structure.
 *
 *
 *      SYNTAX: USHORT PT_CvtOUTBuff(kPTB *kPTB);
 *
 *      ENTRY : pointer to PTrace Control buffer structure
 *
 *      EXIT :  None.
 *
 *
 *      WARNING :
 *
 *
 *      EFFECTS :
 */

USHORT PT_CvtOUTBuff(kPTB *kPTBBuff)
{
    USHORT rc = 0;
    uPTB_t *PTB;
    PEXCEPTIONREPORTRECORD pREPORT;
    PCONTEXTRECORD pCONTEXT;
    ULONG   tmpseloff = 0L, addr = 0L, psize = 0L, pflags = 0L, ulFunc = 0L;
    PTDA   *pdbgee_PTDA;                                   /*           A004 */

    PTB = (uPTB_t *)kPTBBuff->ulPTBAddr;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        {
          printf("\nPTCnvrt.c *** PT_CvtOUTBuff, \n     \\
                  \n***kPTBBuff: %4x,                   \\
                  \n***kPTBBuff->wrkPDB.wCmd: %4x, %4d, \\
                  \n***kPTBBuff->ulPTBAddr: %4x,        \\
                  \n***CMD Executed PTB->Cmd: %4x, %4d",
                       kPTBBuff,
                       kPTBBuff->wrkPDB.wCmd, kPTBBuff->wrkPDB.wCmd,
                       kPTBBuff->ulPTBAddr,
                       PTB->Cmd,PTB->Cmd);
        }
#endif

  /***************************************************************************/
  /* This routine use the PTDA of the Debuggee for address Conversions  A004 */
  /* because this is the information DEBUGEE which the 16 bit user      A004 */
  /* requested. This is pdbgee_PTDA.                                    A004 */
  /***************************************************************************/

    switch (kPTBBuff->wrkPDB.wCmd) {
        case DBG_N_Success:
            PTB->Cmd = (USHORT)TRC_C_SUC_ret;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
       {  printf("\nPTCnvrt.c *** DBG_N_SUCCESS    ");  }
#endif
            switch (kPTBBuff->command) {
                case TRC_C_Null:
                case TRC_C_WriteMem:        // TRC_C_WriteMem_I implicitly
                                            // included here
                case TRC_C_WriteMem_D:
                case TRC_C_Stop:
                case TRC_C_SStep:
                case TRC_C_Term:
                case TRC_C_UnMapAlias:
                    break;

                case TRC_C_ReadMem:         // TRC_C_ReadMem_I implicitly
                                            // included here
                case TRC_C_ReadMem_D:
                    PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;
                    break;

                case TRC_C_ReadReg:
                case TRC_C_Go:
                    PTB->TID = (USHORT)kPTBBuff->wrkPDB.wTid;
                    PTB->MTE = (USHORT)kPTBBuff->wrkPDB.wMTE;
                    break;

                case TRC_C_WriteReg:
                case TRC_C_Freeze:
                case TRC_C_Resume:
                    PTB->TID = (USHORT)kPTBBuff->wrkPDB.wTid;
                    break;

                case TRC_C_GetFPRegs:
                case TRC_C_SetFPRegs:
                    PTB->TID = (USHORT)kPTBBuff->wrkPDB.wTid;  /*       A007 */

                    /*********************************************************/
                    /* Pointer to the users floating point buffer       A007 */
                    /* is in kPTBBuff->ulAddrIO                         A007 */
                    /*********************************************************/

                    rc = (USHORT)PT_FPRegs((ULONG)TRC_C_GetFPRegs,
                                           (FP13STATE *)kPTBBuff->ulAddrIO,
                                           (FPSTATE *)&fpregs);/*       A007 */

                    if (rc != NO_ERROR)                       /*        A007 */
                     {                                        /*        A007 */
                      PTB->Cmd = (USHORT)TRC_C_ERR_ret;       /*        A007 */
                     } /* if end */                           /*        A007 */

                    break;                                    /*        A007 */

                case TRC_C_NumtoSel:
                    PTB->Value = 0;

                    /* Get the PTDA of the debugee                       A004*/
                    tmpseloff = PTPidToPTDA(PTB->PID,                //d57053
                                            (SSToDS(&pdbgee_PTDA) ) );/* A004*/

                    if (tmpseloff == NO_ERROR) /* we have a valid PTDA  A004 */
                    {                              /* if    valid  PTDA A004 */
                    tmpseloff = PTLinToSel(pdbgee_PTDA,       /*        A004 */
                                           (ULONG)kPTBBuff->wrkPDB.wAddr);
                    if (tmpseloff == NULL)                    /*        A004 */
                     {                                        /*        A004 */
                       PTB->Cmd = (USHORT)TRC_C_ERR_ret;      /*        A004 */
#ifdef PTSTRICT                                               /*        A004 */
  if (PTBugBug & PTCVTo)                                      /*        A004 */
     {printf("\n**ERROR NULL Returned from PTLinToSel Num_To_Sel  FAILED");}
#endif                                                        /*        A004 */
                     } /* if end */                           /*        A004 */
                     else                                     /*        A004 */
                     {                                        /*        A004 */
                       PTB->Value = (USHORT)(tmpseloff >> 16);
                       rc = 0;
                     }                                        /*        A004 */
                    }                              /* endif valid  PTDA A004 */
                    else
                    {                                         /*        A004 */
                      PTB->Cmd = (USHORT)TRC_C_ERR_ret;       /*        A004 */
#ifdef PTSTRICT                                               /*        A004 */
  if (PTBugBug & PTCVTo)                                      /*        A004 */
     {printf("\n**ERROR No PTDA for PTLinToSel Num_To_Sel  FAILED");}
#endif                                                        /*        A004 */
                    }                                         /*        A004 */

                    break;

                case TRC_C_ThrdStat:
                    PTB->TID = (USHORT)kPTBBuff->wrkPDB.wTid;
                    PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;
                    break;

                case TRC_C_MapROAlias:
                case TRC_C_MapRWAlias:
                    /* convert the linear address into a selector         */
                    tmpseloff = PTLinToSel(pPTDACur,
                                           kPTBBuff->wrkPDB.wBuffer);
                    if (tmpseloff != 0)
                    {
                     if (PTB->Value != 0) {
                        if (PTB->Value != (USHORT)(tmpseloff >> 16))
                           panic("PT_CvtOUTBuff: Different Selectors");
                     }
                     else
                        PTB->Value = (USHORT)(tmpseloff >> 16); /* Selector */

                     PTB->OffV = kPTBBuff->uHoldS;         /* Actual Seg Lim */

                     kPTBBuff->uHoldL = kPTBBuff->uHoldL +
                                        kPTBBuff->wrkPDB.wBuffer;
                                 PTModifyDesc (PTB->Value, /* sel to modify  */
                                    kPTBBuff->uHoldL,      /* original addrs */
                                    kPTBBuff->uHoldS,      /* original size  */  //d57053
                                    pPTDACur);             /* target ptda    */
                    }
                    else
                    { /* PTLinToSel Failed invalid selector returned  */
                      PTB->Cmd = (USHORT)TRC_C_ERR_ret;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n\n***ROnly or RWrite Alias FAILED: LinToSel \n\n");  }
#endif
                    }

                    kPTBBuff->uHoldL = kPTBBuff->uHoldS = 0;
                    break;

            } /* endswitch */
            break;

        case DBG_N_Error:
            PTB->Cmd = TRC_C_ERR_ret;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_Error, wVALUE is: %4x",kPTBBuff->wrkPDB.wValue); }
#endif
            switch (kPTBBuff->wrkPDB.wValue) {
                case ERROR_INVALID_PROCID:
                case ERROR_INVALID_THREADID:
                    PTB->Value = trace_child_not_found;
                    break;

                case ERROR_SYSTEM_TRACE:
                    PTB->Value = trace_child_untraceable;
                    break;

                case ERROR_INVALID_PARAMETER:
                case ERROR_BAD_COMMAND:
                    PTB->Value = trace_bad_command;
                    break;

                default:
                    PTB->Value = trace_bad_command;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_Error, Default taken wVALUE is: %4x",
                    kPTBBuff->wrkPDB.wValue);
        }
#endif
                    break;

            } /* endswitch */

            break;

        case DBG_N_Exception:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_Exception wBuffer is: %4x",
                   kPTBBuff->wrkPDB.wBuffer);
        }
#endif
            switch (kPTBBuff->wrkPDB.wBuffer) {
                case XCPT_SINGLE_STEP:                 /* 0xC00000A0         */
                    PTB->Cmd = TRC_C_TBT_ret;
                    PTB->Value = 0;

                    /* Get the PTDA of the debugee                       A004*/
                    tmpseloff = PTPidToPTDA(PTB->PID,                //d57053
                                            (SSToDS(&pdbgee_PTDA) ) );/* A004*/

                    if (tmpseloff == NO_ERROR) /* we have a valid PTDA  A004 */
                    {                              /* if    valid  PTDA A004 */
                    tmpseloff = PTLinToSel(pdbgee_PTDA,       /*        A004 */
                                           kPTBBuff->wrkPDB.wAddr);
                    if (tmpseloff == NULL)                    /*        A004 */
                     {                                        /*        A004 */
                       PTB->Cmd = (USHORT)TRC_C_ERR_ret;      /*        A004 */
#ifdef PTSTRICT                                               /*        A004 */
  if (PTBugBug & PTCVTo)                                      /*        A004 */
     {printf("\n**ERROR NULL Returned from PTLinToSel Single Step FAILED");}
#endif                                                        /*        A004 */
                     } /* if end */                           /*        A004 */
                     else                                     /*        A004 */
                     {                                        /*        A004 */
                       PTB->SegV = (USHORT)(tmpseloff >> 16);
                       PTB->OffV = (USHORT)tmpseloff;
                     }                                        /*        A004 */
                    }                              /* endif valid  PTDA A004 */
                    else
                    {                                         /*        A004 */
                      PTB->Cmd = (USHORT)TRC_C_ERR_ret;       /*        A004 */
#ifdef PTSTRICT                                               /*        A004 */
  if (PTBugBug & PTCVTo)                                      /*        A004 */
     {printf("\n**ERROR No PTDA for PTLinToSel Single Step FAILED");}
#endif                                                        /*        A004 */
                    }                                         /*        A004 */

                    break;

                case XCPT_BREAKPOINT:                  /* 0xC000009F         */
                    PTB->Cmd = TRC_C_BPT_ret;
                    PTB->Value = 0;

                    /* Get the PTDA of the debugee                       A004*/
                    tmpseloff = PTPidToPTDA(PTB->PID,                //d57053
                                            (SSToDS(&pdbgee_PTDA) ) );/* A004*/

                    if (tmpseloff == NO_ERROR) /* we have a valid PTDA  A004 */
                    {                              /* if    valid  PTDA A004 */
                    tmpseloff = PTLinToSel(pdbgee_PTDA,       /*        A004 */
                                           kPTBBuff->wrkPDB.wAddr);
                    if (tmpseloff == NULL)                    /*        A004 */
                     {                                        /*        A004 */
                       PTB->Cmd = (USHORT)TRC_C_ERR_ret;      /*        A004 */
#ifdef PTSTRICT                                               /*        A004 */
  if (PTBugBug & PTCVTo)                                      /*        A004 */
     {printf("\n**ERROR NULL Returned from PTLinToSel Break Point FAILED");}
#endif                                                        /*        A004 */
                     } /* if end */                           /*        A004 */
                     else                                     /*        A004 */
                     {                                        /*        A004 */
                       PTB->SegV = (USHORT)(tmpseloff >> 16);
                       PTB->OffV = (USHORT)tmpseloff;
                     }                                        /*        A004 */
                    }                              /* endif valid  PTDA A004 */
                    else
                    {                                         /*        A004 */
                      PTB->Cmd = (USHORT)TRC_C_ERR_ret;       /*        A004 */
#ifdef PTSTRICT                                               /*        A004 */
  if (PTBugBug & PTCVTo)                                      /*        A004 */
     {printf("\n**ERROR No PTDA for PTLinToSel Break Point FAILED");}
#endif                                                        /*        A004 */
                    }                                         /*        A004 */

                    break;

                case XCPT_SIGNAL:                      /* 0xC0010003         */
                    pREPORT = ((PEXCEPTIONREPORTRECORD)             /*  A002 */
                             (kPTBBuff->wrkPDB.wValue));            /*  A002 */
                    pCONTEXT = ((PCONTEXTRECORD)                    /*  A002 */
                              (kPTBBuff->wrkPDB.wLen));             /*  A002 */

                    PTB->Cmd = TRC_C_SIG_ret;
                    PTB->SegV = (USHORT)(pCONTEXT->ctx_SegCs);       /*  A002 */
                    PTB->OffV = (USHORT)(pCONTEXT->ctx_RegEip);      /*  A002 */

                    switch (pREPORT->ExceptionInfo[0]) {
                        case XCPT_SIGNAL_KILLPROC:
                            PTB->Value = SIGTERM;
                            break;
                        case XCPT_SIGNAL_BREAK:
                            PTB->Value = SIGBREAK;
                            break;
                        case XCPT_SIGNAL_INTR:
                            PTB->Value = SIGINTR;
                            break;
                        default:
                            PTB->Value = SIGPFA;
                            break;
                    } /* endswitch */
                    break;

                case XCPT_ACCESS_VIOLATION:            /* 0xC0000005    A002 */
                    pREPORT = ((PEXCEPTIONREPORTRECORD)             /*  A002 */
                             (kPTBBuff->wrkPDB.wValue));            /*  A002 */
                    pCONTEXT = ((PCONTEXTRECORD)                    /*  A002 */
                              (kPTBBuff->wrkPDB.wLen));             /*  A002 */

                    PTB->Cmd = TRC_C_GPF_ret;                       /*  A002 */
                    PTB->Value = (USHORT)(pREPORT->ExceptionInfo[0]);/*  A002 */
                    PTB->SegV = (USHORT)(pCONTEXT->ctx_SegCs);       /*  A002 */
                    PTB->OffV = (USHORT)(pCONTEXT->ctx_RegEip);      /*  A002 */

                    break;                                          /*  A002 */

            } /* endswitch */

            break;

        case DBG_N_ProcTerm:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_ProcTerm"); }
#endif
            PTB->Cmd = TRC_C_KIL_ret;
            PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;
            break;

        case DBG_N_ModuleLoad:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_ModuleLoad"); }
#endif
            PTB->Cmd = TRC_C_LIB_ret;
            PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;
            PTB->SegV = 0;
            PTB->OffV = 0;
            break;

        case DBG_N_CoError:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_CoError"); }
#endif
            PTB->Cmd = TRC_C_FPE_ret;
            break;

        case DBG_N_ThreadTerm:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_ThreadTerm"); }
#endif
            PTB->Cmd = TRC_C_THD_ret;
            PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;
            PTB->SegV = 0;
            PTB->OffV = 0;
            break;

        case DBG_N_AsyncStop:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_AsyncStop"); }
#endif
            PTB->Cmd = TRC_C_STP_ret;
            PTB->Value = 0;
            PTB->SegV = 0;
            PTB->OffV = 0;
            break;

        case DBG_N_NewProc:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_NewProc"); }
#endif
            PTB->Cmd = TRC_C_NEW_ret;
            PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;         /*   M006 */
            PTB->SegV = 0;
            PTB->OffV = 0;
            break;

        case DBG_N_AliasFree:
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***DBG_N_AliasFree"); }
#endif
            PTB->Cmd = TRC_C_AFR_ret;
            PTB->Value = (USHORT)kPTBBuff->wrkPDB.wValue;
            PTB->SegV = 0;
            PTB->OffV = 0;
            break;

        default:
            PTB->Cmd = TRC_C_ERR_ret;
            PTB->Value = trace_bad_command;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n***ERROR Default Taken kPTBBuff->wrkPDB.wCmd is: %4x",
                     kPTBBuff->wrkPDB.wCmd);
        }
#endif
            break;
        } /* endswitch */

 PTB->TID = (USHORT)kPTBBuff->wrkPDB.wTid;
 PTB->MTE = (USHORT)kPTBBuff->wrkPDB.wMTE;
 PTB->rAX = (USHORT)kPTBBuff->wrkPDB.wEAX;
 PTB->rBX = (USHORT)kPTBBuff->wrkPDB.wEBX;
 PTB->rCX = (USHORT)kPTBBuff->wrkPDB.wECX;
 PTB->rDX = (USHORT)kPTBBuff->wrkPDB.wEDX;
 PTB->rSP = (USHORT)kPTBBuff->wrkPDB.wESP;
 PTB->rBP = (USHORT)kPTBBuff->wrkPDB.wEBP;
 PTB->rSI = (USHORT)kPTBBuff->wrkPDB.wESI;
 PTB->rDI = (USHORT)kPTBBuff->wrkPDB.wEDI;
 PTB->rDS = (USHORT)kPTBBuff->wrkPDB.wDS;
 PTB->rES = (USHORT)kPTBBuff->wrkPDB.wES;
 PTB->rCS = (USHORT)kPTBBuff->wrkPDB.wCS;
 PTB->rF  = (USHORT)kPTBBuff->wrkPDB.wEFlags;
 PTB->rSS = (USHORT)kPTBBuff->wrkPDB.wSS;
 PTB->rIP = (USHORT)kPTBBuff->wrkPDB.wEIP;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTo)
        { printf("\n*** PT_CvtOUTBUff Exit RC: %2x, Cmd: %2d\n",rc,PTB->Cmd); }
#endif

  return(rc);
}

/**** PT_CvtINBuff  -- Converts a PTrace buffer structure to a DosDebug
 *                     buffer structure.
 *
 *
 *      SYNTAX: ULONG  PT_CvtINBuff(kPTB *kPTBBuff);
 *
 *      ENTRY : pointer to PTrace Control buffer structure
 *
 *      EXIT :  None.
 *
 *
 *      WARNING :
 *
 *
 *      EFFECTS :
 ****************************************************************************/

ULONG  PT_CvtINBuff(kPTB *kPTBBuff)
{
    uPTB_t *PTB;
    ULONG   rc = 0L;
    PCHAR   libname;
    ULONG   liblen = 0L, P1 = 0L, P2 = 0L;
    PCHAR   P3 = NULL;
    ULONG   psize = 0L, pflags = 0L;
    PTDA   *pdbgee_PTDA;                                   /*           A004 */

    PTB = (uPTB_t *)(kPTBBuff->ulPTBAddr);

    kPTBBuff->wrkPDB.wPid = PTB->PID;
    kPTBBuff->wrkPDB.wTid = PTB->TID;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTi)
    {
    printf("\nPTCnvrt.c *** PT_CvtINBuff  \n\n \\
            \n**PT Command: %2x, %2d \n\n      \\
            \n**kPTBBuff: %4x                  \\
            \n**kPTBBuff->ulPTBAddr: %4x \n    \\
            \n**uPTB from user contains \n\n   \\
            \n**kPTBBuff->ulPTBAddr->PID: %2x  \\
           kPTBBuff->ulPTBAddr->TID: %2x    \\
            \n**kPTBBuff->ulPTBAddr->Cmd: %2x  \\
           kPTBBuff->ulPTBAddr->Value: %2x  \\
            \n**kPTBBuff->ulPTBAddr->OffV: %2x \\
           kPTBBuff->ulPTBAddr->SegV: %2x   \\
            \n**kPTBBuff->ulPTBAddr->MTE: %2x",
                   kPTBBuff->command, kPTBBuff->command,
                   kPTBBuff,
                   kPTBBuff->ulPTBAddr,
                   PTB->PID, PTB->TID,
                   PTB->Cmd, PTB->Value,
                   PTB->OffV, PTB->SegV,
                   PTB->MTE);
    }
#endif

  /* Get the PTDA of the debugee, needed for address conversions        A004 */
  rc = PTPidToPTDA(PTB->PID, (SSToDS(&pdbgee_PTDA) ) );              //d57053

  if (rc == NO_ERROR) {                                       /*        A004 */
    switch (kPTBBuff->command) {
        case TRC_C_Term:
            kPTBBuff->wrkPDB.wCmd = DBG_C_Term;
            break;

        case TRC_C_Null:
            kPTBBuff->wrkPDB.wCmd = DBG_C_Null;
            break;

        case TRC_C_Go:
        /*********************************************************************/
        /* **      THIS CODE SHOULD MATCH THE CODE IN TRC_C_Stop     ** A005 */
        /*********************************************************************/

            kPTBBuff->wrkPDB.wCmd = DBG_C_Null;                /*       A005 */
            rc = PTCheck(&(kPTBBuff->wrkPDB));                 /*       A005 */

            if ( (rc == NO_ERROR) &&                           /*       A005 */
                 (kPTBBuff->wrkPDB.wCmd != DBG_N_Error) )      /*       A005 */
            {  // Debugee is already connected                 /*       A005 */
               kPTBBuff->wrkPDB.wCmd = DBG_C_Go;               /*       A005 */
#ifdef PTSTRICT                                                /*       A005 */
    if (PTBugBug & PTCVTi)                                     /*       A005 */
        {  printf("\n**PT_CVTINBUFF ** is doing a GO     ");  }/*       A005 */
#endif                                                         /*       A005 */
            }                                                  /*       A005 */
            else                                               /*       A005 */
            {  // If Debugee is NOT Connected                  /*       A005 */
             if ( (kPTBBuff->wrkPDB.wCmd == DBG_N_Error) )     /*       A005 */
             {                                                 /*       A005 */
#ifdef PTSTRICT                                                /*       A005 */
    if (PTBugBug & PTCVTi)                                     /*       A005 */
        {  printf("\n**PT_CVTINBUFF ** is doing a Connect for GO Cmd");  }
#endif                                                         /*       A005 */
               kPTBBuff->wrkPDB.wCmd = DBG_C_Connect;          /*       A005 */
               kPTBBuff->wrkPDB.wValue = DBG_L_386;            /*       A005 */
               kPTBBuff->wrkPDB.wTid = 0;                      /*       A005 */
               kPTBBuff->wrkPDB.wAddr = (ULONG) DBGS_SEVER;    /*       A008 */

               /* Call PTCHECK to issue the intial connect command      A005 */
               rc = PTCheck(&(kPTBBuff->wrkPDB));              /*       A005 */

               /* If Connect was successful get the first notification  A005 */
               if ( (rc == NO_ERROR) &&                        /*       A005 */
                    (kPTBBuff->wrkPDB.wCmd == DBG_N_Success) ) /*       A005 */
                 { kPTBBuff->wrkPDB.wCmd = DBG_C_Go; }         /*       A005 */
                else                                           /*       A005 */
                 {                                             /*       A005 */
#ifdef PTSTRICT                                                /*       A005 */
    if (PTBugBug & PTCVTi)                                     /*       A005 */
          { printf("\n**PT_CVTINBUFF ** Connect Failed"); }    /*       A005 */
#endif                                                         /*       A005 */
                   kPTBBuff->wrkPDB.wCmd = TRC_C_ERR_ret;      /*       A005 */
                   rc = ERROR_ACCESS_DENIED;                   /*       A005 */
                 }                                             /*       A005 */
             }                                                 /*       A005 */
            }                                                  /*       A005 */
            break;                                             /*       A005 */

        case TRC_C_SStep:
            kPTBBuff->wrkPDB.wCmd = DBG_C_SStep;
            break;

        case TRC_C_Freeze:
            kPTBBuff->wrkPDB.wCmd = DBG_C_Freeze;
            break;

        case TRC_C_Resume:
            kPTBBuff->wrkPDB.wCmd = DBG_C_Resume;
            break;

        case TRC_C_GetLibName:
            P1 = PTB->SegV;
            P2 = PTB->OffV;

            rc = (USHORT)PTSelToLin(pPTDACur,
                                    P1, P2,
                                    &(kPTBBuff->ulAddrIO) );

            if (rc == NO_ERROR)
             {
               liblen = MAXPATHLEN;
               libname = (PCHAR)kPTBBuff->ulAddrIO;
               PTB->Cmd = TRC_C_SUC_ret;

               rc = LDRGetLibName((VMHOB)PTB->Value,
                                          (PCHAR) libname,
                                          (ULONG) liblen);
               if (rc != NO_ERROR)
               {
                  PTB->Cmd = TRC_C_ERR_ret;
               }
             } /* endif */
             else                                             /*         A004*/
             {                                                /*         A004*/
                PTB->Cmd = TRC_C_ERR_ret;                     /*         A004*/
             }                                                /*         A004*/

            break;

        case TRC_C_Stop:
        /*********************************************************************/
        /* **      THIS CODE SHOULD MATCH THE CODE IN TRC_C_Go       ** A005 */
        /*********************************************************************/
            kPTBBuff->wrkPDB.wCmd = DBG_C_Null;
            rc = PTCheck(&(kPTBBuff->wrkPDB));


            if ( (rc == NO_ERROR) && (kPTBBuff->wrkPDB.wCmd != DBG_N_Error) )
            {  // Debugee is already connected

               kPTBBuff->wrkPDB.wCmd = DBG_C_ReadReg;
               rc = PTCheck(&(kPTBBuff->wrkPDB));

               kPTBBuff->wrkPDB.wCmd = DBG_C_Stop;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTi)
        {  printf("\n**PT_CVTINBUFF ** is doing a STOP   ");  }
#endif
            }
            else
            {  // If Debugee is NOT Connected
             if ( (kPTBBuff->wrkPDB.wCmd == DBG_N_Error) )      /*      A001*/
             {
#ifdef PTSTRICT
    if (PTBugBug & PTCVTi)
        {  printf("\n**PT_CVTINBUFF ** is doing a Connect");  }
#endif
               kPTBBuff->wrkPDB.wCmd = DBG_C_Connect;
               kPTBBuff->wrkPDB.wValue = DBG_L_386;
               kPTBBuff->wrkPDB.wTid = 0;
               kPTBBuff->wrkPDB.wAddr = (ULONG) DBGS_SEVER;  /*       A008 */

               /* Call PTCHECK to issue the intial connect command     A001*/
               rc = PTCheck(&(kPTBBuff->wrkPDB));               /*     A001*/

               /* If Connect was successful get the first notification  A001*/
               if ( (rc == NO_ERROR) &&
                    (kPTBBuff->wrkPDB.wCmd == DBG_N_Success) )  /*      A001*/
                 {
                   kPTBBuff->wrkPDB.wCmd = DBG_C_ReadReg;
                   rc = PTCheck(&(kPTBBuff->wrkPDB));
                   kPTBBuff->wrkPDB.wCmd = DBG_C_Stop;
                 }                                              /*      A001 */
                else                                            /*      A003 */
                 {                                              /*      A003 */
#ifdef PTSTRICT                                                 /*      A003 */
    if (PTBugBug & PTCVTi)                                      /*      A003 */
          { printf("\n**PT_CVTINBUFF ** Connect Failed"); }     /*      A003 */
#endif                                                          /*      A003 */
                   kPTBBuff->wrkPDB.wCmd = TRC_C_ERR_ret;       /*      A005 */
                   rc = ERROR_ACCESS_DENIED;                    /*      A003 */
                 }                                              /*      A003 */
             }
            }
            break;

        case TRC_C_ThrdStat:
            kPTBBuff->wrkPDB.wCmd = DBG_C_ThrdStat;
            kPTBBuff->wrkPDB.wValue = PTB->Value;
            P1 = PTB->SegV;
            P2 = PTB->OffV;

            rc = (USHORT)PTSelToLin(pPTDACur,
                                    P1, P2,
                                    &(kPTBBuff->ulAddrIO) );

            if (rc == NO_ERROR)
             {
               kPTBBuff->wrkPDB.wBuffer = kPTBBuff->ulAddrIO;
               kPTBBuff->wrkPDB.wLen = 4;
             } /* endif */
             else                                             /*         A004*/
             {                                                /*         A004*/
                PTB->Cmd = TRC_C_ERR_ret;                     /*         A004*/
             }                                                /*         A004*/

            break;

        case TRC_C_ReadReg:
            kPTBBuff->wrkPDB.wCmd = DBG_C_ReadReg;
            break;

        case TRC_C_WriteReg:
            kPTBBuff->wrkPDB.wCmd = DBG_C_WriteReg;
            break;

        case TRC_C_ReadMem_D:
        case TRC_C_ReadMem:
            P1 = PTB->SegV;
            P2 = PTB->OffV;

            rc = (USHORT)PTSelToLin(pdbgee_PTDA,
                                    P1, P2,
                                    &(kPTBBuff->ulAddrIO) );

            if (rc == NO_ERROR)
             {
               kPTBBuff->wrkPDB.wCmd = DBG_C_ReadMem;
               kPTBBuff->wrkPDB.wAddr = kPTBBuff->ulAddrIO;
             } /* endif */
             else                                             /*         A004*/
             {                                                /*         A004*/
                PTB->Cmd = TRC_C_ERR_ret;                     /*         A004*/
             }                                                /*         A004*/

            break;

        case TRC_C_WriteMem_D:
        case TRC_C_WriteMem:
            P1 = PTB->SegV;
            P2 = PTB->OffV;

            rc = (USHORT)PTSelToLin(pdbgee_PTDA,
                                    P1, P2,
                                    &(kPTBBuff->ulAddrIO) );

            if (rc == NO_ERROR)
             {
               kPTBBuff->wrkPDB.wCmd = DBG_C_WriteMem;
               kPTBBuff->wrkPDB.wAddr = kPTBBuff->ulAddrIO;
               kPTBBuff->wrkPDB.wValue = PTB->Value;
             } /* endif */
             else                                             /*         A004*/
             {                                                /*         A004*/
                PTB->Cmd = TRC_C_ERR_ret;                     /*         A004*/
             }                                                /*         A004*/

            break;

        case TRC_C_SetFPRegs:
            kPTBBuff->wrkPDB.wCmd = DBG_C_WriteCoRegs;
            P1 = PTB->SegV;                                    /*       A007 */
            P2 = PTB->OffV;                                    /*       A007 */

            rc = (USHORT)PTSelToLin(pPTDACur,                  /*       A007 */
                                    P1, P2,                    /*       A007 */
                                    &(kPTBBuff->ulAddrIO) );   /*       A007 */
            /*
             * we must have access to the users buffer                  A007
             */

            if (rc == NO_ERROR)                                /*       A007 */
             {                                                 /*       A007 */
               /*  Zero out My Floating Point before we get information A007 */
               memset(&fpregs, 0, FP387_size);                 /*       A007 */

               kPTBBuff->wrkPDB.wCmd = DBG_C_WriteCoRegs;      /*       A007 */
               kPTBBuff->wrkPDB.wValue = DBG_CO_387;           /*       A007 */
               kPTBBuff->wrkPDB.wBuffer = (ULONG)&fpregs;      /*       A007 */
               kPTBBuff->wrkPDB.wLen = 108;                    /*       A007 */
               kPTBBuff->wrkPDB.wIndex = 0;                    /*       A007 */

               /**************************************************************/
               /* Pointer to the users floating point buffer            A007 */
               /* is in kPTBBuff->ulAddrIO                              A007 */
               /**************************************************************/

               rc = (USHORT)PT_FPRegs((ULONG)TRC_C_SetFPRegs,
                                      (FP13STATE *)kPTBBuff->ulAddrIO,
                                      (FPSTATE *)&fpregs);     /*       A007 */

               if (rc != NO_ERROR)                            /*        A007 */
                { PTB->Cmd = (USHORT)TRC_C_ERR_ret;  }        /*        A007 */

             } /* endif */                                    /*        A007 */
             else                                    /*                 A007 */
             {                                       /*                 A007 */
                PTB->Cmd = TRC_C_ERR_ret;            /*                 A007 */
             }                                       /*                 A007 */
                                                     /*                 A007 */
            break;                                   /*                 A007 */

        case TRC_C_GetFPRegs:
            P1 = PTB->SegV;                                    /*       A007 */
            P2 = PTB->OffV;                                    /*       A007 */

            rc = (USHORT)PTSelToLin(pPTDACur,                  /*       A007 */
                                    P1, P2,                    /*       A007 */
                                    &(kPTBBuff->ulAddrIO) );   /*       A007 */
            /*
             * we must have access to the users buffer                  A007
             */

            if (rc == NO_ERROR)                                /*       A007 */
             {                                                 /*       A007 */
               /*  Zero out My Floating Point before we get information A007 */
               memset(&fpregs, 0, FP387_size);                 /*       A007 */

               kPTBBuff->wrkPDB.wCmd = DBG_C_ReadCoRegs;       /*       A007 */
               kPTBBuff->wrkPDB.wValue = DBG_CO_387;           /*       A007 */
               kPTBBuff->wrkPDB.wBuffer = (ULONG)&fpregs;      /*       A007 */
               kPTBBuff->wrkPDB.wLen = 108;                    /*       A007 */
               kPTBBuff->wrkPDB.wIndex = 0;                    /*       A007 */
             } /* endif */                                     /*       A007 */
             else                                    /*                 A007 */
             {                                       /*                 A007 */
                PTB->Cmd = TRC_C_ERR_ret;            /*                 A007 */
             }                                       /*                 A007 */
                                                     /*                 A007 */
            break;                                   /*                 A007 */

        case TRC_C_NumtoSel:
            kPTBBuff->wrkPDB.wCmd = DBG_C_NumToAddr;
            kPTBBuff->wrkPDB.wValue = PTB->Value;
            break;

        case TRC_C_MapROAlias:
            rc = (USHORT)PTAlias(kPTBBuff);
            if (rc == NO_ERROR)
            {  kPTBBuff->wrkPDB.wCmd = DBG_C_MapROAlias;  }
            else
            {  PTB->Cmd = TRC_C_ERR_ret;  }
            break;

        case TRC_C_MapRWAlias:
            rc = (USHORT)PTAlias(kPTBBuff);
            if (rc == NO_ERROR)
            {  kPTBBuff->wrkPDB.wCmd = DBG_C_MapRWAlias;  }
            else
            {  PTB->Cmd = TRC_C_ERR_ret;  }
            break;

        case TRC_C_UnMapAlias:
            kPTBBuff->wrkPDB.wCmd = DBG_C_UnMapAlias;
            kPTBBuff->wrkPDB.wBuffer = (ULONG)PTB->Value;

            rc = (USHORT)PTSelToLin(pPTDACur,
                                    kPTBBuff->wrkPDB.wBuffer,
                                    0,
                                    &(kPTBBuff->ulAddrIO) );

            if (rc == NO_ERROR)
             {
               kPTBBuff->wrkPDB.wBuffer = kPTBBuff->ulAddrIO;
             } /* endif */
             else                                             /*         A004*/
             {                                                /*         A004*/
                PTB->Cmd = TRC_C_ERR_ret;                     /*         A004*/
             }                                                /*         A004*/

            break;

        default:
            kPTBBuff->wrkPDB.wCmd = DBG_N_Error;
            kPTBBuff->wrkPDB.wValue = ERROR_INVALID_PARAMETER;
            rc = ERROR_INVALID_PARAMETER;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTi)
        {
          printf("\n\n**ERROR Default Taken Invalid User CMD: %2x\n",PTB->Cmd);
        }
#endif
    } /* endswitch */

    kPTBBuff->wrkPDB.wMTE = PTB->MTE;
    kPTBBuff->wrkPDB.wEAX = PTB->rAX;
    kPTBBuff->wrkPDB.wEBX = PTB->rBX;
    kPTBBuff->wrkPDB.wECX = PTB->rCX;
    kPTBBuff->wrkPDB.wEDX = PTB->rDX;
    kPTBBuff->wrkPDB.wESP = PTB->rSP;
    kPTBBuff->wrkPDB.wEBP = PTB->rBP;
    kPTBBuff->wrkPDB.wESI = PTB->rSI;
    kPTBBuff->wrkPDB.wEDI = PTB->rDI;
    kPTBBuff->wrkPDB.wDS = PTB->rDS;
    kPTBBuff->wrkPDB.wCS = PTB->rCS;
    kPTBBuff->wrkPDB.wSS = PTB->rSS;
    kPTBBuff->wrkPDB.wEIP = PTB->rIP;
#ifdef PTSTRICT
    if (PTBugBug & PTCVTi)
          printf("\n\n **Exit PT_CvtINBuff with RC: %4x, %4d\n\n",rc, rc);
#endif
  }
  else {
     PTB->Cmd = TRC_C_ERR_ret;                     /*         A004*/
     rc = ERROR_INVALID_PARAMETER;
  }

  return(rc);
}

/****************************************************************************
 *
 *
 *
 *
 ****************************************************************************/
ULONG  PTSelToLin(PPTDA uptda,
                  ULONG  uSelector,
                  ULONG  uOffset,
                  ULONG *new_value) {

ULONG   new_addr = 0L;
ULONG   rc = 0L;
struct  seldesc_s    pdesc;                   /* Return desc info  A004*/

#define  max_offset   0x0000ffff              /*                       */
#define  sel_error    -1                      /* selector error -1     */
#define  off_error    -2                      /* offset   error -2     */
#define  sel_non_app_err  -3                  /* selector not in app -3 A004 */

#ifdef PTSTRICT
    if (PTBugBug & PTCVTr)
        {
             printf("\n\n****** Entered PTSelToLin\n,     \\
                     \n****** uptda is: %4x,              \\
                     \n****** uSelector is: %4x,          \\
                     \n****** uOffset is: %4x",
                        uptda, uSelector, uOffset);
        }
#endif

    if (uOffset > max_offset)
    {
       new_addr = off_error;
       rc = ERROR_INVALID_PARAMETER;
    }
    else
    {
       rc = SELGetDescInfo(uSelector,                         /*        A004 */
                           uptda->ptda_handle,                /*        A004 */
                           (SSToDS(&pdesc) ) );               /*        A004 */

       if (rc != NO_ERROR)                                    /*        A004 */
       {
          new_addr = sel_error;
          rc = ERROR_INVALID_PARAMETER;
       }
       else
       {
          if ( ( (pdesc.desc_attr & (D_SEG + D_GATE)) == D_GATE) ||  /*  A004 */
               ( (pdesc.desc_attr &  D_TSS) == D_TSS)  )             /*  A004 */
          {                                          /* Selector is not  A004 */
             new_addr = sel_non_app_err;             /* in Appl CS or DS A004 */
             rc = ERROR_INVALID_PARAMETER;           /*                  A004 */
          }                                          /*                  A004 */
          else                                       /*                  A004 */
             new_addr = pdesc.desc_laddr + uOffset;  /* in Appl CS or DS A004 */
       }  /* endif */

    }  /* endif  */

    *new_value = new_addr;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTr)
        {
             printf("\n****** RC: %4x, New Addr: %4x, %4d\n , \\
                     \n****** Exit PTSelToLin\n\n",
                        rc, new_addr, new_addr);
        }
#endif

    return(rc);
}

/****************************************************************************
 *
 *
 *
 *
 ****************************************************************************/
ULONG PTLinToSel(PPTDA upptda, ULONG ulAddr)
{
    unsigned char rpl_bits = -1;      /* SELConvertToSelOff RPL BITs       */

    union                             /* this redefines the storage        */
    {                              /*                                   */
       unsigned long  ul_value;     /* SELConvertToSelOff return value   */
       struct                       /* this is done for bit handling     */
          {                         /* structure                         */
            unsigned short LO_16;      /* Low  order 16 bits is offset   */
            unsigned short HI_16;      /* High order 16 bits is selector */
          }   s_value;              /* structure name                    */
    } sel_off;                     /* union name                        */

#ifdef PTSTRICT
    if (PTBugBug & PTCVTr)
        {
             printf("\n\n****** Entered PTLinToSel,      \\
                     \n****** uptda is: %4x,  **   ulAddr is: %4x",
                         upptda,   ulAddr);
        }
#endif

    /* Convert 32 bit Flat or Linear to 16 BIT Selector                  */

    sel_off.ul_value = 0;

    /* address is above the 64k line and below the 512 meg line          A004*/
    if ( ( ulAddr >= SEL_64K ) &&               /* lower SEL Limit       A004*/
         ( ulAddr <= SEL_512MEG ) )             /* upper SEL Limit       A004*/
         sel_off.ul_value = SELConvertToSelOff(ulAddr,
                                               rpl_bits,
                                        (VMHOB)upptda->ptda_handle);

#ifdef PTSTRICT
    if (PTBugBug & PTCVTr)
        {
             printf("\n****** Selector: %4x, **  Offset  : %4x\n",
                        sel_off.s_value.HI_16,  sel_off.s_value.LO_16,
                    "\n****** Exit PTLinToSel\n\n");
        }
#endif

    return(sel_off.ul_value);
}

/****************************************************************************
 *
 *
 *
 *
 ****************************************************************************/
  ULONG PTAlias(kPTB *kPTBBuff)
{
  uPTB_t *PTB;
  PTDA   *pdbgee_PTDA;
  struct seldesc_s    desc;
  ULONG   rc = 0L,
          sel32 = 0L,
          value;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTar)
        { printf("\n\n**** Entered PTAlias");  }
#endif

  PTB = (uPTB_t *)kPTBBuff->ulPTBAddr;
  sel32 = (ULONG)PTB->SegV;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTar)
        {
             printf("\n****PTB->Value: %2x",PTB->Value);
             printf("\n****PTB->SegV:  %2x",PTB->SegV);
             printf("\n****Selector to map is sel32: %4x",sel32);
        }
#endif

  kPTBBuff->uHoldL = 0;                               /* will fill in latter */

  /* Get the PTDA of the debugee, this is where the alias must come from     */
  rc = PTPidToPTDA(PTB->PID, (SSToDS(&pdbgee_PTDA) ) );              //d57053

  if (rc == NO_ERROR)
  {  /* Get the Linear address of the Selector this is used by DosDebug      */
     rc = PTSelToLin(pdbgee_PTDA,
                     sel32, 0,
                     &(kPTBBuff->ulAddrIO) );
#ifdef PTSTRICT
    if (PTBugBug & PTCVTar)
        { printf("\n****Linear addr of sel32: %4x",kPTBBuff->ulAddrIO); }
#endif

     if (rc != NO_ERROR)
     {  rc = ERROR_INVALID_PARAMETER;  }
  }
  else
  {
     rc = ERROR_INVALID_PARAMETER;
  }

 /* Get the Segment Limit of the Selector, DosDebug Needs needs a Length     */
  if (rc == NO_ERROR)
  {
     if (rc == NO_ERROR)
     {
        /* Get the Segment Limit of the Debugees Selector                   */
        rc = SELGetDescInfo(sel32,
                            (VMHOB)pdbgee_PTDA->ptda_handle,
                            (SSToDS(&desc) ) );                  /*      A004*/
     }

     if (rc != NO_ERROR)
     {
        rc = ERROR_INVALID_PARAMETER;
     }
     else
     {  /* rc is zero */
        /*Round the selector to a page boundary the true selector is in sel32*/
        kPTBBuff->uHoldL = kPTBBuff->ulAddrIO & 0xFFFFF000; /* Round  Sel    */

        if (PTB->Value != 0)
        {
           rc = PTSelToLin(pPTDACur,
                           PTB->Value, 0,
                           SSToDS(&value));

           if (rc == NO_ERROR) {
              /* we need to upmap this alias before we create a new one  */
              kPTBBuff->wrkPDB.wCmd = DBG_C_UnMapAlias;
              kPTBBuff->wrkPDB.wBuffer = value;

              rc = PTCheck(&kPTBBuff->wrkPDB);
           }

           if (rc != NO_ERROR)
              return(rc);

        } /* endif ptb->value is not zero */

        /*********************************************************************/
        /****** set up parameters for MapROAlias or MapRWAlias Commands ******/
        /****************** DosDebug Needs the Fields  ***********************/
        /*********************************************************************/

        kPTBBuff->wrkPDB.wAddr = kPTBBuff->uHoldL;  /* Start of Alias region */
        kPTBBuff->wrkPDB.wBuffer = 0;               /* reserved              */

#ifdef PTSTRICT
    if (PTBugBug & PTCVTar)
        { printf("\n****Rounded Selector is: %4x",kPTBBuff->wrkPDB.wAddr); }
#endif
        /*********************************************************************/
        /* Round Segment Limit up to the next page multiple                  */
        /* The Segment Limit must be a page multiple                         */
        /* This will be the requested length of the Alias Region             */
        /* This Length must also include the differance in the true and      */
        /***************************************************                 */
        kPTBBuff->uHoldL = kPTBBuff->ulAddrIO -           /* Actual  Selector*/
                           kPTBBuff->uHoldL;              /* Rounded Selector*/

        kPTBBuff->wrkPDB.wLen = kPTBBuff->uHoldL + desc.desc_cb;
        kPTBBuff->wrkPDB.wLen = kPTBBuff->wrkPDB.wLen + 0x00000FFF;
        kPTBBuff->wrkPDB.wLen = kPTBBuff->wrkPDB.wLen & 0xFFFFF000;

     } /* end else rc is zero */

  } /* end if rc is zero      */

  /* d57053
   * SELGetDescInfo returns the size of the selector in desc_cb.
   * ptrace must return the limit of the selector so convert size
   * to limit.
   */
  desc.desc_cb--;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTar)
        {
        printf("\n****ACTUAL and Rounded SELctor Differ is: %4x",
                 kPTBBuff->uHoldL);
        printf("\n****Buffer value is: %4x",kPTBBuff->wrkPDB.wBuffer);
        printf("\n****Actual  seglim: %4x",desc.desc_cb);
        printf("\n****Rounded seglim: %4x",kPTBBuff->wrkPDB.wLen);
        printf("\n****PTAlias Exit rc: %4x\n\n",rc);
        }
#endif

  /* Note: kPTBBuff->hHoldL is the differance in the selectors,save for user */
  /* Save the actual Segement Limit so it can be returned to the user        */
  kPTBBuff->uHoldS = (USHORT)desc.desc_cb;           /* save Actual Seg. Len */

  return(rc);
}

/****************************************************************************
 *                                                                          *
 *                                                                          *
 *                                                                          *
 *                                                                          *
 ****************************************************************************/
ULONG PT_FPRegs(ULONG ulFunc, FP13STATE *AddrIO, FPSTATE *fpregs)
{

  ULONG   rc = 0L;
  ULONG   to_addr = 0L, from_addr = 0L;
  PFP13STATE *p13_buff;
  PFPSTATE   *p20_buff;

#ifdef PTSTRICT
    if (PTBugBug & PTCVTfp)
        {
             printf("\n\n****** Entered PT_FPRegs, ** Function is: %4x,  \\
                     \n****** User Fp Buff is: %4x, **  My Fp Buff is: %4x",
                           ulFunc,  AddrIO,   fpregs);
        }
#endif

   /* Pointers to Register information in 20 buffer to Users 13 buffer  A007*/
   p13_buff = (PFP13STATE *)&AddrIO->fpstate_13_stack[0];  /*           A007*/
   p20_buff = (PFPSTATE *)&fpregs->fpstate_stack[0];       /*           A007*/

   switch (ulFunc)
    {
      case  TRC_C_GetFPRegs:
      /**********************************************************************/
      /* Move the contents of the DosDebug Floating Point Buffer to    A007 */
      /* The USERs Floating Point Buffer                               A007 */
      /**********************************************************************/
        AddrIO->fpstate_13_env.Control_13 =
                (USHORT)fpregs->fpstate_env.Control;  /* Control Word    A007*/

        AddrIO->fpstate_13_env.Status_13 =
                (USHORT)fpregs->fpstate_env.Status;   /* Status Word     A007*/

        AddrIO->fpstate_13_env.Tag_13 =
                (USHORT)fpregs->fpstate_env.fpTag;    /* Tag Word        A007*/

        AddrIO->fpstate_13_env.EIP_13 =         /* Instruction Offset    A007*/
                (USHORT)fpregs->fpstate_env.fpEIP;

        AddrIO->fpstate_13_env.CS_13 =          /* CS Selector           A007*/
                (USHORT)fpregs->fpstate_env.fpCS;

        AddrIO->fpstate_13_env.DataOffset_13 =  /* Data Operand Offset   A007*/
                (USHORT)fpregs->fpstate_env.DataOffset;

        AddrIO->fpstate_13_env.DataSel_13 =     /* Data Operand selector A007*/
                (USHORT)fpregs->fpstate_env.DataSel;

        /* copy Register information from 20 buffer to Users 13 buffer   A007*/
        memcpy(p13_buff,                                 /* to location  A007*/
               p20_buff,                                 /*from location A007*/
               80);                                      /* Length       A007*/

        break;

      case  TRC_C_SetFPRegs:
      /**********************************************************************/
      /* Move the contents of the Users Floating Point Buffer to       A007 */
      /* The DosDebug Floating Point Buffer                            A007 */
      /**********************************************************************/
        fpregs->fpstate_env.Control =
                AddrIO->fpstate_13_env.Control_13;    /* Control Word    A007*/

        fpregs->fpstate_env.Status =
                AddrIO->fpstate_13_env.Status_13;     /* Status Word     A007*/

        fpregs->fpstate_env.fpTag =
                AddrIO->fpstate_13_env.Tag_13;        /* Tag Word        A007*/

        fpregs->fpstate_env.fpEIP =             /* Instruction Offset    A007*/
                AddrIO->fpstate_13_env.EIP_13;

        fpregs->fpstate_env.fpCS =              /* CS Selector           A007*/
                AddrIO->fpstate_13_env.CS_13;

        fpregs->fpstate_env.OpCode = 0L;        /* NOT USED in 1.3       A007*/

        fpregs->fpstate_env.DataOffset =        /* Data Operand Offset   A007*/
                AddrIO->fpstate_13_env.DataOffset_13;

        fpregs->fpstate_env.DataSel =           /* Data Operand selector A007*/
                AddrIO->fpstate_13_env.DataSel_13;

        /* copy Register information from Users 13 buffer to 20 buffer   A007*/
        memcpy(p20_buff,                                 /* to location  A007*/
               p13_buff,                                 /*from location A007*/
               80);                                      /* Length       A007*/

        break;

      default:
      /**********************************************************************/
      /* Invalid call to this routine                                  A007 */
      /**********************************************************************/
         rc = ERROR_INVALID_PARAMETER;

        break;

    }  /* end switch */

#ifdef PTSTRICT
    if (PTBugBug & PTCVTfp)
        {
             printf("\n\n***** Exit PT_FPRegs, ** RC is: %4x,  \\
                     \n\n************ User Buffer, **  DosDebug Buffer,  \\
                     \n Control word: %4x, ** %4x,              \\
                     \n Status  word: %4x, ** %4x,              \\
                     \n IP    Offset: %4x, ** %4x,              \\
                     \n CS  Selector: %4x, ** %4x,              \\
                     \n Data  Offset: %4x, ** %4x,              \\
                     \n Data Selecto: %4x, ** %4x,              \\
                     \n\n User   FP   ReGisters: %80x,           \\
                     \n\n DosDebug FP ReGisters: %80x",

                           rc,

                           AddrIO->fpstate_13_env.Control_13,
                           fpregs->fpstate_env.Control,

                           AddrIO->fpstate_13_env.Status_13,
                           fpregs->fpstate_env.Status,

                           AddrIO->fpstate_13_env.Tag_13,
                           fpregs->fpstate_env.fpTag,

                           AddrIO->fpstate_13_env.EIP_13,
                           fpregs->fpstate_env.fpEIP,

                           AddrIO->fpstate_13_env.CS_13,
                           fpregs->fpstate_env.fpCS,

                           AddrIO->fpstate_13_env.DataOffset_13,
                           fpregs->fpstate_env.DataOffset,

                           AddrIO->fpstate_13_env.DataSel_13,
                           fpregs->fpstate_env.DataSel,

                           p13_buff,
                           p20_buff);
        }
#endif

    return(rc);
}

