Changeset 55474 in vbox
- Timestamp:
- Apr 28, 2015 8:53:39 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/VBox/HGSMI/HGSMI.h
r55421 r55474 72 72 */ 73 73 74 #pragma pack(1)75 74 typedef struct HGSMIHEAP 76 75 { … … 78 77 HGSMIMADATA ma; /* Memory allocator */ 79 78 } HGSMIHEAP; 80 #pragma pack() 81 82 #pragma pack(1) 79 83 80 /* The size of the array of channels. Array indexes are uint8_t. Note: the value must not be changed. */ 84 81 #define HGSMI_NUMBER_OF_CHANNELS 0x100 … … 110 107 */ 111 108 } HGSMICHANNELINFO; 112 #pragma pack()113 109 114 110 … … 125 121 } 126 122 127 DECLINLINE(HGSMIBUFFERTAIL *) HGSMIBufferTailFromPtr(void *pvBuffer, uint32_t u32DataSize) 123 DECLINLINE(HGSMIBUFFERTAIL *) HGSMIBufferTailFromPtr(void *pvBuffer, 124 uint32_t u32DataSize) 128 125 { 129 126 return (HGSMIBUFFERTAIL *)(HGSMIBufferDataFromPtr(pvBuffer) + u32DataSize); 130 127 } 131 128 132 DECLINLINE(HGSMISIZE) HGSMIBufferMinimumSize (void) 133 { 134 return sizeof (HGSMIBUFFERHEADER) + sizeof (HGSMIBUFFERTAIL); 135 } 136 137 DECLINLINE(uint8_t *) HGSMIBufferData (const HGSMIBUFFERHEADER *pHeader) 138 { 139 return (uint8_t *)pHeader + sizeof (HGSMIBUFFERHEADER); 140 } 141 142 DECLINLINE(HGSMIBUFFERTAIL *) HGSMIBufferTail (const HGSMIBUFFERHEADER *pHeader) 143 { 144 return (HGSMIBUFFERTAIL *)(HGSMIBufferData (pHeader) + pHeader->u32DataSize); 145 } 146 147 DECLINLINE(HGSMIBUFFERHEADER *) HGSMIBufferHeaderFromData (const void *pvData) 148 { 149 return (HGSMIBUFFERHEADER *)((uint8_t *)pvData - sizeof (HGSMIBUFFERHEADER)); 150 } 151 152 DECLINLINE(HGSMISIZE) HGSMIBufferRequiredSize (uint32_t u32DataSize) 153 { 154 return HGSMIBufferMinimumSize () + u32DataSize; 129 DECLINLINE(HGSMISIZE) HGSMIBufferMinimumSize(void) 130 { 131 return sizeof(HGSMIBUFFERHEADER) + sizeof(HGSMIBUFFERTAIL); 132 } 133 134 DECLINLINE(HGSMIBUFFERHEADER *) HGSMIBufferHeaderFromData(const void *pvData) 135 { 136 return (HGSMIBUFFERHEADER *)((uint8_t *)pvData - sizeof(HGSMIBUFFERHEADER)); 137 } 138 139 DECLINLINE(HGSMISIZE) HGSMIBufferRequiredSize(uint32_t u32DataSize) 140 { 141 return HGSMIBufferMinimumSize() + u32DataSize; 155 142 } 156 143 … … 181 168 } 182 169 183 DECLINLINE(uint8_t *) HGSMIBufferDataAndChInfoFromOffset (const HGSMIAREA *pArea, HGSMIOFFSET offBuffer, uint16_t * pChInfo) 184 { 185 HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer (pArea, offBuffer); 186 Assert(pHeader); 187 if(pHeader) 188 { 189 *pChInfo = pHeader->u16ChannelInfo; 190 return HGSMIBufferData(pHeader); 191 } 192 return NULL; 193 } 194 195 uint32_t HGSMIChecksum (HGSMIOFFSET offBuffer, 196 const HGSMIBUFFERHEADER *pHeader, 197 const HGSMIBUFFERTAIL *pTail); 198 199 int HGSMIAreaInitialize (HGSMIAREA *pArea, 200 void *pvBase, 201 HGSMISIZE cbArea, 202 HGSMIOFFSET offBase); 203 204 void HGSMIAreaClear (HGSMIAREA *pArea); 170 DECLINLINE(uint8_t *) HGSMIBufferDataAndChInfoFromOffset(const HGSMIAREA *pArea, 171 HGSMIOFFSET offBuffer, 172 uint16_t *pu16ChannelInfo) 173 { 174 HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIOffsetToPointer(pArea, offBuffer); 175 *pu16ChannelInfo = pHeader->u16ChannelInfo; 176 return HGSMIBufferDataFromPtr(pHeader); 177 } 178 179 uint32_t HGSMIChecksum(HGSMIOFFSET offBuffer, 180 const HGSMIBUFFERHEADER *pHeader, 181 const HGSMIBUFFERTAIL *pTail); 182 183 int HGSMIAreaInitialize(HGSMIAREA *pArea, 184 void *pvBase, 185 HGSMISIZE cbArea, 186 HGSMIOFFSET offBase); 187 188 void HGSMIAreaClear(HGSMIAREA *pArea); 205 189 206 190 DECLINLINE(bool) HGSMIAreaContainsOffset(const HGSMIAREA *pArea, HGSMIOFFSET off) … … 214 198 } 215 199 216 HGSMIOFFSET HGSMIBufferInitializeSingle (const HGSMIAREA *pArea, 217 HGSMIBUFFERHEADER *pHeader, 218 HGSMISIZE cbBuffer, 219 uint8_t u8Channel, 220 uint16_t u16ChannelInfo); 200 HGSMIOFFSET HGSMIBufferInitializeSingle(const HGSMIAREA *pArea, 201 HGSMIBUFFERHEADER *pHeader, 202 HGSMISIZE cbBuffer, 203 uint8_t u8Channel, 204 uint16_t u16ChannelInfo); 205 206 int HGSMIHeapSetup(HGSMIHEAP *pHeap, 207 void *pvBase, 208 HGSMISIZE cbArea, 209 HGSMIOFFSET offBase, 210 const HGSMIENV *pEnv); 211 212 void HGSMIHeapDestroy(HGSMIHEAP *pHeap); 213 214 void *HGSMIHeapBufferAlloc(HGSMIHEAP *pHeap, 215 HGSMISIZE cbBuffer); 216 217 void HGSMIHeapBufferFree(HGSMIHEAP *pHeap, 218 void *pvBuf); 219 220 void *HGSMIHeapAlloc(HGSMIHEAP *pHeap, 221 HGSMISIZE cbData, 222 uint8_t u8Channel, 223 uint16_t u16ChannelInfo); 224 225 void HGSMIHeapFree(HGSMIHEAP *pHeap, 226 void *pvData); 221 227 222 228 DECLINLINE(const HGSMIAREA *) HGSMIHeapArea(HGSMIHEAP *pHeap) … … 225 231 } 226 232 227 int HGSMIHeapSetup (HGSMIHEAP *pHeap, 228 void *pvBase, 229 HGSMISIZE cbArea, 230 HGSMIOFFSET offBase, 231 const HGSMIENV *pEnv); 232 233 void HGSMIHeapDestroy (HGSMIHEAP *pHeap); 234 235 void* HGSMIHeapBufferAlloc (HGSMIHEAP *pHeap, 236 HGSMISIZE cbBuffer); 237 238 void HGSMIHeapBufferFree(HGSMIHEAP *pHeap, 239 void *pvBuf); 240 241 void *HGSMIHeapAlloc (HGSMIHEAP *pHeap, 242 HGSMISIZE cbData, 243 uint8_t u8Channel, 244 uint16_t u16ChannelInfo); 245 246 DECLINLINE(HGSMIOFFSET) HGSMIHeapBufferOffset(HGSMIHEAP *pHeap, void *pvData) 233 DECLINLINE(HGSMIOFFSET) HGSMIHeapOffset(HGSMIHEAP *pHeap) 234 { 235 return HGSMIHeapArea(pHeap)->offBase; 236 } 237 238 DECLINLINE(HGSMISIZE) HGSMIHeapSize(HGSMIHEAP *pHeap) 239 { 240 return HGSMIHeapArea(pHeap)->cbArea; 241 } 242 243 DECLINLINE(HGSMIOFFSET) HGSMIHeapBufferOffset(HGSMIHEAP *pHeap, 244 void *pvData) 247 245 { 248 246 return HGSMIBufferOffsetFromData(HGSMIHeapArea(pHeap), pvData); 249 }250 251 void HGSMIHeapFree (HGSMIHEAP *pHeap,252 void *pvData);253 254 DECLINLINE(HGSMIOFFSET) HGSMIHeapOffset(HGSMIHEAP *pHeap)255 {256 return HGSMIHeapArea(pHeap)->offBase;257 }258 259 DECLINLINE(HGSMISIZE) HGSMIHeapSize(HGSMIHEAP *pHeap)260 {261 return HGSMIHeapArea(pHeap)->cbArea;262 247 } 263 248 … … 271 256 void *pvChannelHandler); 272 257 273 int HGSMIBufferProcess( HGSMIAREA *pArea,258 int HGSMIBufferProcess(const HGSMIAREA *pArea, 274 259 HGSMICHANNELINFO *pChannelInfo, 275 260 HGSMIOFFSET offBuffer); -
trunk/src/VBox/GuestHost/HGSMI/HGSMICommon.cpp
r55421 r55474 58 58 */ 59 59 60 static uint32_t hgsmiHashBegin 60 static uint32_t hgsmiHashBegin(void) 61 61 { 62 62 return 0; 63 63 } 64 64 65 static uint32_t hgsmiHashProcess 66 67 65 static uint32_t hgsmiHashProcess(uint32_t hash, 66 const void *pvData, 67 size_t cbData) 68 68 { 69 69 const uint8_t *pu8Data = (const uint8_t *)pvData; … … 79 79 } 80 80 81 static uint32_t hgsmiHashEnd 81 static uint32_t hgsmiHashEnd(uint32_t hash) 82 82 { 83 83 hash += (hash << 3); … … 88 88 } 89 89 90 uint32_t HGSMIChecksum (HGSMIOFFSET offBuffer, 91 const HGSMIBUFFERHEADER *pHeader, 92 const HGSMIBUFFERTAIL *pTail) 93 { 94 uint32_t u32Checksum = hgsmiHashBegin (); 95 96 u32Checksum = hgsmiHashProcess (u32Checksum, &offBuffer, sizeof (offBuffer)); 97 u32Checksum = hgsmiHashProcess (u32Checksum, pHeader, sizeof (HGSMIBUFFERHEADER)); 98 u32Checksum = hgsmiHashProcess (u32Checksum, pTail, RT_OFFSETOF(HGSMIBUFFERTAIL, u32Checksum)); 99 100 return hgsmiHashEnd (u32Checksum); 101 } 102 103 int HGSMIAreaInitialize (HGSMIAREA *pArea, void *pvBase, HGSMISIZE cbArea, HGSMIOFFSET offBase) 90 uint32_t HGSMIChecksum(HGSMIOFFSET offBuffer, 91 const HGSMIBUFFERHEADER *pHeader, 92 const HGSMIBUFFERTAIL *pTail) 93 { 94 uint32_t u32Checksum = hgsmiHashBegin(); 95 96 u32Checksum = hgsmiHashProcess(u32Checksum, &offBuffer, sizeof(offBuffer)); 97 u32Checksum = hgsmiHashProcess(u32Checksum, pHeader, sizeof(HGSMIBUFFERHEADER)); 98 u32Checksum = hgsmiHashProcess(u32Checksum, pTail, RT_OFFSETOF(HGSMIBUFFERTAIL, u32Checksum)); 99 100 return hgsmiHashEnd(u32Checksum); 101 } 102 103 int HGSMIAreaInitialize(HGSMIAREA *pArea, 104 void *pvBase, 105 HGSMISIZE cbArea, 106 HGSMIOFFSET offBase) 104 107 { 105 108 uint8_t *pu8Base = (uint8_t *)pvBase; 106 109 107 110 if ( !pArea /* Check that the area: */ 108 || cbArea < HGSMIBufferMinimumSize () /* Large enough.*/109 || pu8Base + cbArea < pu8Base /* No address space wrap.*/110 || offBase > UINT32_C(0xFFFFFFFF) - cbArea /* Area within the 32 bit space: offBase + cbMem <= 0xFFFFFFFF*/111 || cbArea < HGSMIBufferMinimumSize() /* large enough; */ 112 || pu8Base + cbArea < pu8Base /* no address space wrap; */ 113 || offBase > UINT32_C(0xFFFFFFFF) - cbArea /* area within the 32 bit space: offBase + cbMem <= 0xFFFFFFFF. */ 111 114 ) 112 115 { … … 116 119 pArea->pu8Base = pu8Base; 117 120 pArea->offBase = offBase; 118 pArea->offLast = cbArea - HGSMIBufferMinimumSize 121 pArea->offLast = cbArea - HGSMIBufferMinimumSize() + offBase; 119 122 pArea->cbArea = cbArea; 120 123 … … 122 125 } 123 126 124 void HGSMIAreaClear 127 void HGSMIAreaClear(HGSMIAREA *pArea) 125 128 { 126 129 if (pArea) 127 130 { 128 memset (pArea, 0, sizeof (HGSMIAREA));131 RT_ZERO(*pArea); 129 132 } 130 133 } … … 161 164 } 162 165 163 HGSMIOFFSET offBuffer = HGSMIPointerToOffset 166 HGSMIOFFSET offBuffer = HGSMIPointerToOffset(pArea, pHeader); 164 167 165 168 pHeader->u8Flags = HGSMI_BUFFER_HEADER_F_SEQ_SINGLE; … … 167 170 pHeader->u8Channel = u8Channel; 168 171 pHeader->u16ChannelInfo = u16ChannelInfo; 169 memset (pHeader->u.au8Union, 0, sizeof (pHeader->u.au8Union)); 170 171 HGSMIBUFFERTAIL *pTail = HGSMIBufferTail (pHeader); 172 172 RT_ZERO(pHeader->u.au8Union); 173 174 HGSMIBUFFERTAIL *pTail = HGSMIBufferTailFromPtr(pHeader, u32DataSize); 173 175 pTail->u32Reserved = 0; 174 pTail->u32Checksum = HGSMIChecksum 176 pTail->u32Checksum = HGSMIChecksum(offBuffer, pHeader, pTail); 175 177 176 178 return offBuffer; 177 179 } 178 180 179 int HGSMIHeapSetup (HGSMIHEAP *pHeap, 180 void *pvBase, 181 HGSMISIZE cbArea, 182 HGSMIOFFSET offBase, 183 const HGSMIENV *pEnv) 184 { 185 if ( !pHeap 186 || !pvBase) 187 { 188 return VERR_INVALID_PARAMETER; 189 } 190 191 int rc = HGSMIAreaInitialize (&pHeap->area, pvBase, cbArea, offBase); 192 193 if (RT_SUCCESS (rc)) 181 int HGSMIHeapSetup(HGSMIHEAP *pHeap, 182 void *pvBase, 183 HGSMISIZE cbArea, 184 HGSMIOFFSET offBase, 185 const HGSMIENV *pEnv) 186 { 187 AssertPtrReturn(pHeap, VERR_INVALID_PARAMETER); 188 AssertPtrReturn(pvBase, VERR_INVALID_PARAMETER); 189 190 int rc = HGSMIAreaInitialize(&pHeap->area, pvBase, cbArea, offBase); 191 if (RT_SUCCESS(rc)) 194 192 { 195 193 rc = HGSMIMAInit(&pHeap->ma, &pHeap->area, NULL, 0, 0, pEnv); 196 194 if (RT_FAILURE(rc)) 197 195 { 198 HGSMIAreaClear 196 HGSMIAreaClear(&pHeap->area); 199 197 } 200 198 } … … 203 201 } 204 202 205 void HGSMIHeapDestroy 203 void HGSMIHeapDestroy(HGSMIHEAP *pHeap) 206 204 { 207 205 if (pHeap) 208 206 { 209 207 HGSMIMAUninit(&pHeap->ma); 210 } 211 } 212 213 void *HGSMIHeapAlloc (HGSMIHEAP *pHeap, 214 HGSMISIZE cbData, 215 uint8_t u8Channel, 216 uint16_t u16ChannelInfo) 217 { 218 HGSMISIZE cbAlloc = HGSMIBufferRequiredSize (cbData); 219 220 HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIHeapBufferAlloc (pHeap, cbAlloc); 221 if (!pHeader) 222 return NULL; 223 224 HGSMIOFFSET offBuffer = HGSMIBufferInitializeSingle(HGSMIHeapArea(pHeap), pHeader, 225 cbAlloc, u8Channel, u16ChannelInfo); 226 if (offBuffer == HGSMIOFFSET_VOID) 227 { 208 RT_ZERO(*pHeap); 209 } 210 } 211 212 void *HGSMIHeapAlloc(HGSMIHEAP *pHeap, 213 HGSMISIZE cbData, 214 uint8_t u8Channel, 215 uint16_t u16ChannelInfo) 216 { 217 HGSMISIZE cbAlloc = HGSMIBufferRequiredSize(cbData); 218 HGSMIBUFFERHEADER *pHeader = (HGSMIBUFFERHEADER *)HGSMIHeapBufferAlloc(pHeap, cbAlloc); 219 if (pHeader) 220 { 221 HGSMIOFFSET offBuffer = HGSMIBufferInitializeSingle(HGSMIHeapArea(pHeap), pHeader, 222 cbAlloc, u8Channel, u16ChannelInfo); 223 if (offBuffer == HGSMIOFFSET_VOID) 224 { 225 HGSMIHeapBufferFree(pHeap, pHeader); 226 pHeader = NULL; 227 } 228 } 229 230 return pHeader? HGSMIBufferDataFromPtr(pHeader): NULL; 231 } 232 233 void HGSMIHeapFree(HGSMIHEAP *pHeap, 234 void *pvData) 235 { 236 if (pvData) 237 { 238 HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData(pvData); 228 239 HGSMIHeapBufferFree(pHeap, pHeader); 229 return NULL; 230 } 231 232 return HGSMIBufferData (pHeader); 233 } 234 235 void HGSMIHeapFree (HGSMIHEAP *pHeap, 236 void *pvData) 237 { 238 if (pvData) 239 { 240 HGSMIBUFFERHEADER *pHeader = HGSMIBufferHeaderFromData (pvData); 241 242 HGSMIHeapBufferFree (pHeap, pHeader); 243 } 244 } 245 246 void* HGSMIHeapBufferAlloc (HGSMIHEAP *pHeap, HGSMISIZE cbBuffer) 240 } 241 } 242 243 void *HGSMIHeapBufferAlloc(HGSMIHEAP *pHeap, 244 HGSMISIZE cbBuffer) 247 245 { 248 246 void *pvBuf = HGSMIMAAlloc(&pHeap->ma, cbBuffer); … … 263 261 } HGSMIBUFFERCONTEXT; 264 262 265 /* Verify that the given offBuffer points to a valid buffer, which is within the area. 263 /** Verify that the given offBuffer points to a valid buffer, which is within the area. 264 * 265 * @returns VBox status and the buffer information in pBufferContext. 266 * @param pArea Area which supposed to contain the buffer. 267 * @param offBuffer The buffer location in the area. 268 * @param pBufferContext Where to write information about the buffer. 266 269 */ 267 270 static int hgsmiVerifyBuffer(const HGSMIAREA *pArea, … … 328 331 } 329 332 330 /* A wrapper to safely call the handler.331 */332 static void hgsmiChannelHandlerCall(const HGSMICHANNELHANDLER *pHandler,333 const HGSMIBUFFERCONTEXT *pBufferContext)334 {335 LogFlowFunc(("pHandler %p\n", pHandler));336 337 if ( pHandler338 && pHandler->pfnHandler)339 {340 pHandler->pfnHandler(pHandler->pvHandler, pBufferContext->pHeader->u16ChannelInfo,341 pBufferContext->pvData, pBufferContext->cbData);342 }343 }344 345 333 /** Helper to convert HGSMI channel index to the channel structure pointer. 346 334 * … … 370 358 * @param offBuffer The buffer location in the area. 371 359 */ 372 int HGSMIBufferProcess( HGSMIAREA *pArea,360 int HGSMIBufferProcess(const HGSMIAREA *pArea, 373 361 HGSMICHANNELINFO *pChannelInfo, 374 362 HGSMIOFFSET offBuffer) … … 387 375 * Start with the handler list head, which is the preallocated HGSMI setup channel. 388 376 */ 389 HGSMICHANNEL *pChannel = HGSMIChannelFindById(pChannelInfo, bufferContext.pHeader->u8Channel);377 const HGSMICHANNEL *pChannel = HGSMIChannelFindById(pChannelInfo, bufferContext.pHeader->u8Channel); 390 378 if (pChannel) 391 379 { 392 hgsmiChannelHandlerCall(&pChannel->handler, &bufferContext); 380 const HGSMICHANNELHANDLER *pHandler = &pChannel->handler; 381 if (pHandler->pfnHandler) 382 { 383 pHandler->pfnHandler(pHandler->pvHandler, bufferContext.pHeader->u16ChannelInfo, 384 bufferContext.pvData, bufferContext.cbData); 385 } 393 386 HGSMI_STRICT_ASSERT(RT_SUCCESS(hgsmiVerifyBuffer(pArea, offBuffer, &bufferContext))); 394 387 }
Note:
See TracChangeset
for help on using the changeset viewer.