| 1 | /* $Id: PerTaskW32kData.c,v 1.2 2001-07-10 16:39:19 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * Per Task (Win32k) Data. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 2001 knut st. osmundsen (kosmunds@csc.no) | 
|---|
| 6 | * | 
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 8 | * | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 |  | 
|---|
| 13 | /******************************************************************************* | 
|---|
| 14 | *   Defined Constants And Macros                                               * | 
|---|
| 15 | *******************************************************************************/ | 
|---|
| 16 | #define INCL_DOSERRORS                  /* Error codes */ | 
|---|
| 17 | #define INCL_OS2KRNL_VM                 /* OS2KRNL: Virtual Memory Management */ | 
|---|
| 18 | #define INCL_OS2KRNL_TK                 /* OS2KRNL: Task Stuff */ | 
|---|
| 19 | #define INCL_OS2KRNL_PTDA | 
|---|
| 20 |  | 
|---|
| 21 | /******************************************************************************* | 
|---|
| 22 | *   Header Files                                                               * | 
|---|
| 23 | *******************************************************************************/ | 
|---|
| 24 | #include <os2.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include "devSegDf.h"                   /* Win32k segment definitions. */ | 
|---|
| 27 | #include "dev32.h" | 
|---|
| 28 | #include "dev32hlp.h" | 
|---|
| 29 | #include "log.h" | 
|---|
| 30 | #include "OS2Krnl.h" | 
|---|
| 31 | #include <string.h> | 
|---|
| 32 | #include "macros.h" | 
|---|
| 33 | #include "avl.h" | 
|---|
| 34 | #include "PerTaskW32kData.h" | 
|---|
| 35 | #include "rmalloc.h" | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | /******************************************************************************* | 
|---|
| 39 | *   Global Variables                                                           * | 
|---|
| 40 | *******************************************************************************/ | 
|---|
| 41 | /*static*/ PAVLNODECORE pPTDTree;       /* Pointer to PTD tree */ | 
|---|
| 42 | /* Currently assumed protected by ldrSem. */ | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /******************************************************************************* | 
|---|
| 46 | *   Internal Functions                                                         * | 
|---|
| 47 | *******************************************************************************/ | 
|---|
| 48 | PID InternalGetPid(void); | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 | * Get the task data structure for a given process id. | 
|---|
| 53 | * If it doesn't exist we'll allocate a new one. | 
|---|
| 54 | * @returns Pointer to task data structure. | 
|---|
| 55 | *          Might be NULL (see fCreate). | 
|---|
| 56 | * @param   pPTDA   Pointer to PTDA for the task. | 
|---|
| 57 | * @param   fCreate TRUE:  Create structure if not found. | 
|---|
| 58 | *                  FALSE: Return NULL if not found. | 
|---|
| 59 | * @status  completely implemented. | 
|---|
| 60 | * @author  knut st. osmundsen (kosmunds@csc.no) | 
|---|
| 61 | */ | 
|---|
| 62 | PPTD    GetTaskData(PPTDA pPTDA, BOOL fCreate) | 
|---|
| 63 | { | 
|---|
| 64 | PPTD    pptd; | 
|---|
| 65 | if (pPTDA == 0) | 
|---|
| 66 | pPTDA = ptdaGetCur(); | 
|---|
| 67 | pptd = (PPTD)(void*)AVLGet(&pPTDTree, (AVLKEY)pPTDA); | 
|---|
| 68 | if (!pptd && fCreate) | 
|---|
| 69 | { | 
|---|
| 70 | pptd = rmalloc(sizeof(*pptd)); | 
|---|
| 71 | if (!pptd) | 
|---|
| 72 | { | 
|---|
| 73 | kprintf(("GetTaskData: rmalloc failed!\n")); | 
|---|
| 74 | return NULL; | 
|---|
| 75 | } | 
|---|
| 76 | memset(pptd, 0, sizeof(*pptd)); | 
|---|
| 77 | pptd->core.Key = (AVLKEY)pPTDA; | 
|---|
| 78 | AVLInsert(&pPTDTree, &pptd->core); | 
|---|
| 79 | } | 
|---|
| 80 | return pptd; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 | * Remove the give process id. | 
|---|
| 86 | * @param   pPTDA   Pointer to PTDA for the task. | 
|---|
| 87 | * @status  completely implemented. | 
|---|
| 88 | * @author  knut st. osmundsen (kosmunds@csc.no) | 
|---|
| 89 | */ | 
|---|
| 90 | void    RemoveTaskData(PPTDA pPTDA) | 
|---|
| 91 | { | 
|---|
| 92 | PPTD  pptd; | 
|---|
| 93 | if (pPTDA == 0) | 
|---|
| 94 | pPTDA = ptdaGetCur(); | 
|---|
| 95 | pptd = (PPTD)(void*)AVLRemove(&pPTDTree, (AVLKEY)pPTDA); | 
|---|
| 96 | if (pptd) | 
|---|
| 97 | { | 
|---|
| 98 | /* perhaps free data here... */ | 
|---|
| 99 | if (*(PULONG)(void*)&pptd->lockOdin32Env) | 
|---|
| 100 | { | 
|---|
| 101 | D32Hlp_VMUnLock(&pptd->lockOdin32Env); | 
|---|
| 102 | memset(&pptd->lockOdin32Env, 0, sizeof(pptd->lockOdin32Env)); | 
|---|
| 103 | } | 
|---|
| 104 | pptd->pszzOdin32Env = NULL; | 
|---|
| 105 | rfree(pptd); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | /** | 
|---|
| 111 | * Get the current Process Id. | 
|---|
| 112 | * @returns Process id. | 
|---|
| 113 | *          -1 on error. | 
|---|
| 114 | * @status  completely implemented. | 
|---|
| 115 | * @author  knut st. osmundsen (kosmunds@csc.no) | 
|---|
| 116 | * @remark | 
|---|
| 117 | */ | 
|---|
| 118 | PID InternalGetPid(void) | 
|---|
| 119 | { | 
|---|
| 120 | struct InfoSegLDT * pLIS; | 
|---|
| 121 | pLIS = (struct InfoSegLDT*)D32Hlp_GetDOSVar(DHGETDOSV_LOCINFOSEG, 0); | 
|---|
| 122 | if (!pLIS) | 
|---|
| 123 | { | 
|---|
| 124 | kprintf(("InternalGetPid: D32Hlp_GetDOSVar(DHGETDOSV_LOCINFOSEG, 0) failed\n")); | 
|---|
| 125 | return (PID)-1; | 
|---|
| 126 | } | 
|---|
| 127 | return pLIS->LIS_CurProcID; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|