| 1 | /* $Id: k32QuerySystemMemInfo.cpp,v 1.2 2001-03-07 21:55:30 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * k32QuerySystemMemInfo - Collects more or less useful information on the | 
|---|
| 4 | *                         memory state of the system. | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c) 2001 knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 7 | * | 
|---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 9 | * | 
|---|
| 10 | */ | 
|---|
| 11 |  | 
|---|
| 12 |  | 
|---|
| 13 | /******************************************************************************* | 
|---|
| 14 | *   Defined Constants And Macros                                               * | 
|---|
| 15 | *******************************************************************************/ | 
|---|
| 16 | #define FOR_EXEHDR 1                    /* To make all object flags OBJ???. */ | 
|---|
| 17 | #define INCL_DOSMEMMGR | 
|---|
| 18 | #define INCL_DOSERRORS | 
|---|
| 19 |  | 
|---|
| 20 | #define INCL_OS2KRNL_PG | 
|---|
| 21 | #define INCL_OS2KRNL_SM | 
|---|
| 22 | #define INCL_OS2KRNL_VM | 
|---|
| 23 | #define INCL_OS2KRNL_TK | 
|---|
| 24 | #define INCL_OS2KRNL_SEM | 
|---|
| 25 | #define INCL_OS2KRNL_LDR | 
|---|
| 26 |  | 
|---|
| 27 | #define NO_WIN32K_LIB_FUNCTIONS | 
|---|
| 28 |  | 
|---|
| 29 | /******************************************************************************* | 
|---|
| 30 | *   Header Files                                                               * | 
|---|
| 31 | *******************************************************************************/ | 
|---|
| 32 | #include <os2.h>                        /* OS/2 header file. */ | 
|---|
| 33 | #include <peexe.h>                      /* Wine PE structs and definitions. */ | 
|---|
| 34 | #include <neexe.h>                      /* Wine NE structs and definitions. */ | 
|---|
| 35 | #include <newexe.h>                     /* OS/2 NE structs and definitions. */ | 
|---|
| 36 | #include <exe386.h>                     /* OS/2 LX structs and definitions. */ | 
|---|
| 37 |  | 
|---|
| 38 | #include "devSegDf.h"                   /* Win32k segment definitions. */ | 
|---|
| 39 |  | 
|---|
| 40 | #include "malloc.h"                     /* win32k malloc (resident). Not C library! */ | 
|---|
| 41 | #include "smalloc.h"                    /* win32k swappable heap. */ | 
|---|
| 42 | #include "rmalloc.h"                    /* win32k resident heap. */ | 
|---|
| 43 |  | 
|---|
| 44 | #include <string.h>                     /* C library string.h. */ | 
|---|
| 45 | #include <stdlib.h>                     /* C library stdlib.h. */ | 
|---|
| 46 | #include <stddef.h>                     /* C library stddef.h. */ | 
|---|
| 47 | #include <stdarg.h>                     /* C library stdarg.h. */ | 
|---|
| 48 |  | 
|---|
| 49 | #include "vprintf.h"                    /* win32k printf and vprintf. Not C library! */ | 
|---|
| 50 | #include "dev1632.h"                    /* Common 16- and 32-bit parts */ | 
|---|
| 51 | #include "dev32.h"                      /* 32-Bit part of the device driver. (SSToDS) */ | 
|---|
| 52 | #include "OS2Krnl.h"                    /* kernel structs.  (SFN) */ | 
|---|
| 53 | #include "log.h"                        /* Logging. */ | 
|---|
| 54 | #include "options.h"                    /* Win32k options. */ | 
|---|
| 55 |  | 
|---|
| 56 | #include "ProbKrnl.h"                   /* ProbKrnl variables and definitions. */ | 
|---|
| 57 | #include "win32k.h"                     /* Win32k API structures.  */ | 
|---|
| 58 | #include "k32.h"                        /* Internal Win32k API structures.  */ | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | /** | 
|---|
| 62 | * Collects more or less useful information on the memory state of the system. | 
|---|
| 63 | * @returns     OS2 returncode. | 
|---|
| 64 | * @param       pMemInfo    Pointer to options structure. (NULL is allowed) | 
|---|
| 65 | * @status      completely implelemented. | 
|---|
| 66 | * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 67 | * @remark      One of the pointer may be NULL. | 
|---|
| 68 | */ | 
|---|
| 69 | APIRET k32QuerySystemMemInfo(PK32SYSTEMMEMINFO pMemInfo) | 
|---|
| 70 | { | 
|---|
| 71 | APIRET              rc; | 
|---|
| 72 | K32SYSTEMMEMINFO    MemInfo; | 
|---|
| 73 |  | 
|---|
| 74 | /* | 
|---|
| 75 | * Validate parameters. | 
|---|
| 76 | *  Ensure that the buffer pointer is sensible. | 
|---|
| 77 | *  Ensure that the structure sizes are correct. | 
|---|
| 78 | */ | 
|---|
| 79 | if ((ULONG)pMemInfo < 0x10000) | 
|---|
| 80 | rc = ERROR_INVALID_PARAMETER; | 
|---|
| 81 |  | 
|---|
| 82 | rc = TKFuBuff(SSToDS(&MemInfo.cb), &pMemInfo->cb, sizeof(ULONG)*2, TK_FUSU_NONFATAL); | 
|---|
| 83 | if (rc) | 
|---|
| 84 | return rc; | 
|---|
| 85 | if (MemInfo.cb != sizeof(K32SYSTEMMEMINFO)) | 
|---|
| 86 | return ERROR_INVALID_PARAMETER; | 
|---|
| 87 | if (MemInfo.flFlags > 7) | 
|---|
| 88 | return ERROR_INVALID_PARAMETER; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | /* | 
|---|
| 92 | * Find parameters. | 
|---|
| 93 | */ | 
|---|
| 94 | ULONG   flFlags = !MemInfo.flFlags | 
|---|
| 95 | ? (K32_SYSMEMINFO_SWAPFILE | K32_SYSMEMINFO_PAGING | K32_SYSMEMINFO_VM) | 
|---|
| 96 | : MemInfo.flFlags; | 
|---|
| 97 |  | 
|---|
| 98 | /* | 
|---|
| 99 | * Swap file stuff. | 
|---|
| 100 | */ | 
|---|
| 101 | if (flFlags & K32_SYSMEMINFO_SWAPFILE) | 
|---|
| 102 | { | 
|---|
| 103 | MemInfo.fSwapFile               = SMswapping; | 
|---|
| 104 | MemInfo.cbSwapFileSize          = smFileSize << PAGESHIFT; | 
|---|
| 105 | MemInfo.cbSwapFileAvail         = SMcDFAvail << PAGESHIFT; | 
|---|
| 106 | MemInfo.cbSwapFileUsed          = SMcDFInuse << PAGESHIFT; | 
|---|
| 107 | MemInfo.cbSwapFileMinFree       = SMMinFree << PAGESHIFT; | 
|---|
| 108 | MemInfo.cbSwapFileCFGMinFree    = (SMCFGMinFree << PAGESHIFT) / 4; | 
|---|
| 109 | MemInfo.cbSwapFileCFGSwapSize   = (SMCFGSwapSize << PAGESHIFT) / 4; | 
|---|
| 110 | MemInfo.cSwapFileBrokenDF       = smcBrokenDF; | 
|---|
| 111 | MemInfo.cSwapFileGrowFails      = smcGrowFails; | 
|---|
| 112 | MemInfo.cSwapFileInMemFile      = SMcInMemFile; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | /* | 
|---|
| 117 | * Physical and Paging stuff. | 
|---|
| 118 | */ | 
|---|
| 119 | if (flFlags & K32_SYSMEMINFO_PAGING) | 
|---|
| 120 | { | 
|---|
| 121 | MemInfo.cbPhysSize              = pgPhysPages << PAGESHIFT; | 
|---|
| 122 | if (SMswapping)                 /* the 16-bit MemInfo uses this scheme. */ | 
|---|
| 123 | MemInfo.cbPhysAvail         = PGPhysAvail(); | 
|---|
| 124 | else | 
|---|
| 125 | { | 
|---|
| 126 | int cPages = (pgPhysPages - pgResidentPages - pgSwappablePages - pgDiscardablePages); | 
|---|
| 127 | MemInfo.cbPhysAvail = (cPages > 0) ? cPages << PAGESHIFT : 0; | 
|---|
| 128 | } | 
|---|
| 129 | MemInfo.cbPhysUsed              = PGPhysPresent() << PAGESHIFT; | 
|---|
| 130 | MemInfo.fPagingSwapEnabled      = PGSwapEnabled; | 
|---|
| 131 | MemInfo.cPagingPageFaults       = pgcPageFaults; | 
|---|
| 132 | MemInfo.cPagingPageFaultsActive = pgcPageFaultsActive; | 
|---|
| 133 | MemInfo.cPagingPhysPages        = pgPhysPages; | 
|---|
| 134 | MemInfo.ulPagingPhysMax         = pgPhysMax; | 
|---|
| 135 | MemInfo.cPagingResidentPages    = pgResidentPages; | 
|---|
| 136 | MemInfo.cPagingSwappablePages   = pgSwappablePages; | 
|---|
| 137 | MemInfo.cPagingDiscardableInmem = pgDiscardableInmem; | 
|---|
| 138 | MemInfo.cPagingDiscardablePages = pgDiscardablePages; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 | /* | 
|---|
| 143 | * Virtual Memory stuff. | 
|---|
| 144 | */ | 
|---|
| 145 | if (flFlags & K32_SYSMEMINFO_VM) | 
|---|
| 146 | { | 
|---|
| 147 | MemInfo.ulAddressLimit          = VirtualAddressLimit; | 
|---|
| 148 | vmRecalcShrBound(VMRSBF_ARENASHR, (PULONG)SSToDS(&MemInfo.ulVMArenaPrivMax)); | 
|---|
| 149 | MemInfo.ulVMArenaSharedMin      = pahvmShr->ah_laddrMin; | 
|---|
| 150 | MemInfo.ulVMArenaSharedMax      = pahvmShr->ah_laddrMax; | 
|---|
| 151 | MemInfo.ulVMArenaSystemMin      = pahvmSys->ah_laddrMin; | 
|---|
| 152 | MemInfo.ulVMArenaSystemMax      = pahvmSys->ah_laddrMax; | 
|---|
| 153 | if (pahvmhShr && VirtualAddressLimit > 0x20000000) /* Not valid if less or equal to 512MB user memory */ | 
|---|
| 154 | { | 
|---|
| 155 | MemInfo.ulVMArenaHighSharedMin  = pahvmhShr->ah_laddrMin; | 
|---|
| 156 | MemInfo.ulVMArenaHighSharedMax  = pahvmhShr->ah_laddrMax; | 
|---|
| 157 | vmRecalcShrBound(VMRSBF_ARENAHIGH, (PULONG)SSToDS(&MemInfo.ulVMArenaHighPrivMax)); | 
|---|
| 158 | } | 
|---|
| 159 | else | 
|---|
| 160 | MemInfo.ulVMArenaHighPrivMax = MemInfo.ulVMArenaHighSharedMin = MemInfo.ulVMArenaHighSharedMax = 0; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | /* | 
|---|
| 164 | * Now we're finished - so copy the data into user space and return. | 
|---|
| 165 | */ | 
|---|
| 166 | rc = TKSuBuff(pMemInfo, SSToDS(&MemInfo), MemInfo.cb, TK_FUSU_NONFATAL); | 
|---|
| 167 | if (rc) | 
|---|
| 168 | kprintf(("k32QuerySystemMemInfo: Failed to copy meminfo to user memory. rc=%d\n", rc)); | 
|---|
| 169 |  | 
|---|
| 170 | return rc; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|