Small atom fixes.
[wine/multimedia.git] / memory / selector.c
blob17577f39934209a032d56c6c9a5f1a558136133d
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <string.h>
8 #include "windows.h"
9 #include "ldt.h"
10 #include "miscemu.h"
11 #include "selectors.h"
12 #include "stackframe.h"
13 #include "debug.h"
16 /***********************************************************************
17 * AllocSelectorArray (KERNEL.206)
19 WORD WINAPI AllocSelectorArray( WORD count )
21 WORD i, sel, size = 0;
22 ldt_entry entry;
24 if (!count) return 0;
25 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
27 if (!IS_LDT_ENTRY_FREE(i)) size = 0;
28 else if (++size >= count) break;
30 if (i == LDT_SIZE) return 0;
31 sel = i - size + 1;
33 entry.base = 0;
34 entry.type = SEGMENT_DATA;
35 entry.seg_32bit = FALSE;
36 entry.read_only = FALSE;
37 entry.limit_in_pages = FALSE;
38 entry.limit = 1; /* avoid 0 base and limit */
40 for (i = 0; i < count; i++)
42 /* Mark selector as allocated */
43 ldt_flags_copy[sel + i] |= LDT_FLAGS_ALLOCATED;
44 LDT_SetEntry( sel + i, &entry );
46 return ENTRY_TO_SELECTOR( sel );
50 /***********************************************************************
51 * AllocSelector (KERNEL.175)
53 WORD WINAPI AllocSelector( WORD sel )
55 WORD newsel, count, i;
57 count = sel ? ((GET_SEL_LIMIT(sel) >> 16) + 1) : 1;
58 newsel = AllocSelectorArray( count );
59 TRACE(selector, "(%04x): returning %04x\n",
60 sel, newsel );
61 if (!newsel) return 0;
62 if (!sel) return newsel; /* nothing to copy */
63 for (i = 0; i < count; i++)
65 ldt_entry entry;
66 LDT_GetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
67 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel) + i, &entry );
69 return newsel;
73 /***********************************************************************
74 * FreeSelector (KERNEL.176)
76 WORD WINAPI FreeSelector( WORD sel )
78 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
79 SELECTOR_FreeBlock( sel, 1 );
80 return 0;
84 /***********************************************************************
85 * SELECTOR_SetEntries
87 * Set the LDT entries for an array of selectors.
89 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size,
90 enum seg_type type, BOOL32 is32bit,
91 BOOL32 readonly )
93 ldt_entry entry;
94 WORD i, count;
96 /* The limit for the first selector is the whole */
97 /* block. The next selectors get a 64k limit. */
98 entry.base = (unsigned long)base;
99 entry.type = type;
100 entry.seg_32bit = is32bit;
101 entry.read_only = readonly;
102 entry.limit_in_pages = (size > 0x100000);
103 if (entry.limit_in_pages) entry.limit = ((size + 0xfff) >> 12) - 1;
104 else entry.limit = size - 1;
105 /* Make sure base and limit are not 0 together if the size is not 0 */
106 if (!base && !entry.limit && size) entry.limit = 1;
107 count = (size + 0xffff) / 0x10000;
108 for (i = 0; i < count; i++)
110 LDT_SetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
111 entry.base += 0x10000;
112 /* Apparently the next selectors should *not* get a 64k limit. */
113 /* Can't remember where I read they should... --AJ */
114 entry.limit -= entry.limit_in_pages ? 0x10 : 0x10000;
119 /***********************************************************************
120 * SELECTOR_AllocBlock
122 * Allocate selectors for a block of linear memory.
124 WORD SELECTOR_AllocBlock( const void *base, DWORD size, enum seg_type type,
125 BOOL32 is32bit, BOOL32 readonly )
127 WORD sel, count;
129 if (!size) return 0;
130 count = (size + 0xffff) / 0x10000;
131 sel = AllocSelectorArray( count );
132 if (sel) SELECTOR_SetEntries( sel, base, size, type, is32bit, readonly );
133 return sel;
137 /***********************************************************************
138 * SELECTOR_MoveBlock
140 * Move a block of selectors in linear memory.
142 void SELECTOR_MoveBlock( WORD sel, const void *new_base )
144 WORD i, count = (GET_SEL_LIMIT(sel) >> 16) + 1;
146 for (i = 0; i < count; i++)
148 ldt_entry entry;
149 LDT_GetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
150 entry.base = (unsigned long)new_base;
151 LDT_SetEntry( SELECTOR_TO_ENTRY(sel) + i, &entry );
156 /***********************************************************************
157 * SELECTOR_FreeBlock
159 * Free a block of selectors.
161 void SELECTOR_FreeBlock( WORD sel, WORD count )
163 WORD i, nextsel;
164 ldt_entry entry;
166 TRACE(selector, "(%04x,%d)\n", sel, count );
167 sel &= ~(__AHINCR - 1); /* clear bottom bits of selector */
168 nextsel = sel + (count << __AHSHIFT);
170 #ifdef __i386__
172 /* Check if we are freeing current %fs or %gs selector */
174 WORD fs, gs;
175 GET_FS(fs);
176 if ((fs >= sel) && (fs < nextsel))
178 WARN(selector, "Freeing %%fs selector (%04x), not good.\n", fs );
179 SET_FS( 0 );
181 GET_GS(gs);
182 if ((gs >= sel) && (gs < nextsel)) SET_GS( 0 );
184 #endif /* __i386__ */
186 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
187 for (i = SELECTOR_TO_ENTRY(sel); count; i++, count--)
189 LDT_SetEntry( i, &entry );
190 ldt_flags_copy[i] &= ~LDT_FLAGS_ALLOCATED;
195 /***********************************************************************
196 * SELECTOR_ReallocBlock
198 * Change the size of a block of selectors.
200 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size,
201 enum seg_type type, BOOL32 is32bit, BOOL32 readonly)
203 WORD i, oldcount, newcount;
205 if (!size) size = 1;
206 oldcount = (GET_SEL_LIMIT(sel) >> 16) + 1;
207 newcount = (size + 0xffff) >> 16;
209 if (oldcount < newcount) /* We need to add selectors */
211 /* Check if the next selectors are free */
212 if (SELECTOR_TO_ENTRY(sel) + newcount > LDT_SIZE) i = oldcount;
213 else
214 for (i = oldcount; i < newcount; i++)
215 if (!IS_LDT_ENTRY_FREE(SELECTOR_TO_ENTRY(sel)+i)) break;
217 if (i < newcount) /* they are not free */
219 SELECTOR_FreeBlock( sel, oldcount );
220 sel = AllocSelectorArray( newcount );
222 else /* mark the selectors as allocated */
224 for (i = oldcount; i < newcount; i++)
225 ldt_flags_copy[SELECTOR_TO_ENTRY(sel)+i] |=LDT_FLAGS_ALLOCATED;
228 else if (oldcount > newcount) /* We need to remove selectors */
230 SELECTOR_FreeBlock( ENTRY_TO_SELECTOR(SELECTOR_TO_ENTRY(sel)+newcount),
231 oldcount - newcount );
233 if (sel) SELECTOR_SetEntries( sel, base, size, type, is32bit, readonly );
234 return sel;
238 /***********************************************************************
239 * PrestoChangoSelector (KERNEL.177)
241 WORD WINAPI PrestoChangoSelector( WORD selSrc, WORD selDst )
243 ldt_entry entry;
244 LDT_GetEntry( SELECTOR_TO_ENTRY( selSrc ), &entry );
245 entry.type ^= SEGMENT_CODE; /* toggle the executable bit */
246 LDT_SetEntry( SELECTOR_TO_ENTRY( selDst ), &entry );
247 return selDst;
251 /***********************************************************************
252 * AllocCStoDSAlias (KERNEL.170)
254 WORD WINAPI AllocCStoDSAlias( WORD sel )
256 WORD newsel;
257 ldt_entry entry;
259 newsel = AllocSelectorArray( 1 );
260 TRACE(selector, "(%04x): returning %04x\n",
261 sel, newsel );
262 if (!newsel) return 0;
263 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
264 entry.type = SEGMENT_DATA;
265 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel), &entry );
266 return newsel;
270 /***********************************************************************
271 * AllocDStoCSAlias (KERNEL.171)
273 WORD WINAPI AllocDStoCSAlias( WORD sel )
275 WORD newsel;
276 ldt_entry entry;
278 newsel = AllocSelectorArray( 1 );
279 TRACE(selector, "(%04x): returning %04x\n",
280 sel, newsel );
281 if (!newsel) return 0;
282 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
283 entry.type = SEGMENT_CODE;
284 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel), &entry );
285 return newsel;
289 /***********************************************************************
290 * LongPtrAdd (KERNEL.180)
292 void WINAPI LongPtrAdd( DWORD ptr, DWORD add )
294 ldt_entry entry;
295 LDT_GetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr)), &entry );
296 entry.base += add;
297 LDT_SetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr)), &entry );
301 /***********************************************************************
302 * GetSelectorBase (KERNEL.186)
304 DWORD WINAPI GetSelectorBase( WORD sel )
306 DWORD base = GET_SEL_BASE(sel);
308 /* if base points into DOSMEM, assume we have to
309 * return pointer into physical lower 1MB */
311 return DOSMEM_MapLinearToDos( (LPVOID)base );
315 /***********************************************************************
316 * SetSelectorBase (KERNEL.187)
318 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
320 ldt_entry entry;
322 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
324 entry.base = (DWORD)DOSMEM_MapDosToLinear(base);
326 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
327 return sel;
331 /***********************************************************************
332 * GetSelectorLimit (KERNEL.188)
334 DWORD WINAPI GetSelectorLimit( WORD sel )
336 return GET_SEL_LIMIT(sel);
340 /***********************************************************************
341 * SetSelectorLimit (KERNEL.189)
343 WORD WINAPI SetSelectorLimit( WORD sel, DWORD limit )
345 ldt_entry entry;
346 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
347 entry.limit_in_pages = (limit >= 0x100000);
348 if (entry.limit_in_pages) entry.limit = limit >> 12;
349 else entry.limit = limit;
350 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
351 return sel;
355 /***********************************************************************
356 * SelectorAccessRights (KERNEL.196)
358 WORD WINAPI SelectorAccessRights( WORD sel, WORD op, WORD val )
360 ldt_entry entry;
361 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
362 if (op == 0) /* get */
364 return 0x01 | /* accessed */
365 0x10 | /* not system */
366 0x60 | /* DPL 3 */
367 0x80 | /* present */
368 ((entry.read_only == 0) << 1) |
369 (entry.type << 2) |
370 (entry.seg_32bit << 14) |
371 (entry.limit_in_pages << 15);
373 else /* set */
375 entry.read_only = ((val & 2) == 0);
376 entry.type = (val >> 2) & 3;
377 entry.seg_32bit = val & 0x4000;
378 entry.limit_in_pages = val & 0x8000;
379 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
380 return 0;
385 /***********************************************************************
386 * IsBadCodePtr16 (KERNEL.336)
388 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
390 WORD sel;
391 ldt_entry entry;
393 sel = SELECTOROF(lpfn);
394 if (!sel) return TRUE;
395 if (IS_SELECTOR_FREE(sel)) return TRUE;
396 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
397 if (entry.type != SEGMENT_CODE) return TRUE;
398 if (OFFSETOF(lpfn) > GET_SEL_LIMIT(sel)) return TRUE;
399 return FALSE;
403 /***********************************************************************
404 * IsBadStringPtr16 (KERNEL.337)
406 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
408 WORD sel;
409 ldt_entry entry;
411 sel = SELECTOROF(ptr);
412 if (!sel) return TRUE;
413 if (IS_SELECTOR_FREE(sel)) return TRUE;
414 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
415 if ((entry.type == SEGMENT_CODE) && entry.read_only) return TRUE;
416 if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr));
417 if (OFFSETOF(ptr) + size - 1 > GET_SEL_LIMIT(sel)) return TRUE;
418 return FALSE;
422 /***********************************************************************
423 * IsBadHugeReadPtr16 (KERNEL.346)
425 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
427 WORD sel;
428 ldt_entry entry;
430 sel = SELECTOROF(ptr);
431 if (!sel) return TRUE;
432 if (IS_SELECTOR_FREE(sel)) return TRUE;
433 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
434 if ((entry.type == SEGMENT_CODE) && entry.read_only) return TRUE;
435 if (OFFSETOF(ptr) + size - 1 > GET_SEL_LIMIT(sel)) return TRUE;
436 return FALSE;
440 /***********************************************************************
441 * IsBadHugeWritePtr16 (KERNEL.347)
443 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
445 WORD sel;
446 ldt_entry entry;
448 sel = SELECTOROF(ptr);
449 if (!sel) return TRUE;
450 if (IS_SELECTOR_FREE(sel)) return TRUE;
451 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
452 if ((entry.type == SEGMENT_CODE) || entry.read_only) return TRUE;
453 if (OFFSETOF(ptr) + size - 1 > GET_SEL_LIMIT(sel)) return TRUE;
454 return FALSE;
457 /***********************************************************************
458 * IsBadReadPtr16 (KERNEL.334)
460 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
462 return IsBadHugeReadPtr16( ptr, size );
466 /***********************************************************************
467 * IsBadWritePtr16 (KERNEL.335)
469 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
471 return IsBadHugeWritePtr16( ptr, size );
475 /***********************************************************************
476 * MemoryRead (TOOLHELP.78)
478 DWORD WINAPI MemoryRead( WORD sel, DWORD offset, void *buffer, DWORD count )
480 if (IS_SELECTOR_FREE(sel)) return 0;
481 if (offset > GET_SEL_LIMIT(sel)) return 0;
482 if (offset + count > GET_SEL_LIMIT(sel) + 1)
483 count = GET_SEL_LIMIT(sel) + 1 - offset;
484 memcpy( buffer, ((char *)GET_SEL_BASE(sel)) + offset, count );
485 return count;
489 /***********************************************************************
490 * MemoryWrite (TOOLHELP.79)
492 DWORD WINAPI MemoryWrite( WORD sel, DWORD offset, void *buffer, DWORD count )
494 if (IS_SELECTOR_FREE(sel)) return 0;
495 if (offset > GET_SEL_LIMIT(sel)) return 0;
496 if (offset + count > GET_SEL_LIMIT(sel) + 1)
497 count = GET_SEL_LIMIT(sel) + 1 - offset;
498 memcpy( ((char *)GET_SEL_BASE(sel)) + offset, buffer, count );
499 return count;
502 /************************************* Win95 pointer mapping functions *
506 /***********************************************************************
507 * MapSL (KERNEL32.523)
509 * Maps fixed segmented pointer to linear.
511 LPVOID WINAPI MapSL( SEGPTR sptr )
513 return (LPVOID)PTR_SEG_TO_LIN(sptr);
516 /***********************************************************************
517 * MapSLFix (KERNEL32.524)
519 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
520 * unexpected linear address change when GlobalCompact() shuffles
521 * moveable blocks.
524 LPVOID WINAPI MapSLFix( SEGPTR sptr )
526 return (LPVOID)PTR_SEG_TO_LIN(sptr);
529 /***********************************************************************
530 * UnMapSLFixArray (KERNEL32.701)
533 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT32 length )
537 /***********************************************************************
538 * MapLS (KERNEL32.522)
540 * Maps linear pointer to segmented.
542 SEGPTR WINAPI MapLS( LPVOID ptr )
544 if (!HIWORD(ptr))
545 return (SEGPTR)ptr;
546 else
548 WORD sel = SELECTOR_AllocBlock( ptr, 0x10000, SEGMENT_DATA, FALSE, FALSE );
549 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
554 /***********************************************************************
555 * UnMapLS (KERNEL32.700)
557 * Free mapped selector.
559 void WINAPI UnMapLS( SEGPTR sptr )
561 if (SELECTOROF(sptr))
562 SELECTOR_FreeBlock( SELECTOROF(sptr), 1 );
565 /***********************************************************************
566 * GetThreadSelectorEntry (KERNEL32)
567 * FIXME: add #ifdef i386 for non x86
569 BOOL32 WINAPI GetThreadSelectorEntry( HANDLE32 hthread, DWORD sel,
570 LPLDT_ENTRY ldtent)
572 ldt_entry ldtentry;
574 LDT_GetEntry(SELECTOR_TO_ENTRY(sel),&ldtentry);
575 ldtent->BaseLow = ldtentry.base & 0x0000ffff;
576 ldtent->HighWord.Bits.BaseMid = (ldtentry.base & 0x00ff0000) >> 16;
577 ldtent->HighWord.Bits.BaseHi = (ldtentry.base & 0xff000000) >> 24;
578 ldtent->LimitLow = ldtentry.limit & 0x0000ffff;
579 ldtent->HighWord.Bits.LimitHi = (ldtentry.limit & 0x00ff0000) >> 16;
580 ldtent->HighWord.Bits.Dpl = 3;
581 ldtent->HighWord.Bits.Sys = 0;
582 ldtent->HighWord.Bits.Pres = 1;
583 ldtent->HighWord.Bits.Type = 0x10|(ldtentry.type << 2);
584 if (ldtentry.read_only)
585 ldtent->HighWord.Bits.Type|=0x2;
586 ldtent->HighWord.Bits.Granularity = ldtentry.limit_in_pages;
587 ldtent->HighWord.Bits.Default_Big = ldtentry.seg_32bit;
588 return TRUE;
592 /**********************************************************************
593 * SMapLS* (KERNEL32)
594 * These functions map linear pointers at [EBP+xxx] to segmented pointers
595 * and return them.
596 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
597 * unravel them at SUnMapLS. We just store the segmented pointer there.
599 static void
600 x_SMapLS_IP_EBP_x(CONTEXT *context,int argoff) {
601 DWORD val,ptr;
603 val =*(DWORD*)(EBP_reg(context)+argoff);
604 if (val<0x10000) {
605 ptr=val;
606 *(DWORD*)(EBP_reg(context)+argoff) = 0;
607 } else {
608 ptr = MapLS((LPVOID)val);
609 *(DWORD*)(EBP_reg(context)+argoff) = ptr;
611 EAX_reg(context) = ptr;
614 REGS_ENTRYPOINT(SMapLS_IP_EBP_8) {x_SMapLS_IP_EBP_x(context,8);}
615 REGS_ENTRYPOINT(SMapLS_IP_EBP_12) {x_SMapLS_IP_EBP_x(context,12);}
616 REGS_ENTRYPOINT(SMapLS_IP_EBP_16) {x_SMapLS_IP_EBP_x(context,16);}
617 REGS_ENTRYPOINT(SMapLS_IP_EBP_20) {x_SMapLS_IP_EBP_x(context,20);}
618 REGS_ENTRYPOINT(SMapLS_IP_EBP_24) {x_SMapLS_IP_EBP_x(context,24);}
619 REGS_ENTRYPOINT(SMapLS_IP_EBP_28) {x_SMapLS_IP_EBP_x(context,28);}
620 REGS_ENTRYPOINT(SMapLS_IP_EBP_32) {x_SMapLS_IP_EBP_x(context,32);}
621 REGS_ENTRYPOINT(SMapLS_IP_EBP_36) {x_SMapLS_IP_EBP_x(context,36);}
622 REGS_ENTRYPOINT(SMapLS_IP_EBP_40) {x_SMapLS_IP_EBP_x(context,40);}
624 REGS_ENTRYPOINT(SMapLS)
626 if (EAX_reg(context)>=0x10000) {
627 EAX_reg(context) = MapLS((LPVOID)EAX_reg(context));
628 EDX_reg(context) = EAX_reg(context);
629 } else {
630 EDX_reg(context) = 0;
634 REGS_ENTRYPOINT(SUnMapLS)
636 if (EAX_reg(context)>=0x10000)
637 UnMapLS((SEGPTR)EAX_reg(context));
640 static void
641 x_SUnMapLS_IP_EBP_x(CONTEXT *context,int argoff) {
642 if (*(DWORD*)(EBP_reg(context)+argoff))
643 UnMapLS(*(DWORD*)(EBP_reg(context)+argoff));
644 *(DWORD*)(EBP_reg(context)+argoff)=0;
646 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_8) { x_SUnMapLS_IP_EBP_x(context,12); }
647 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_12) { x_SUnMapLS_IP_EBP_x(context,12); }
648 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_16) { x_SUnMapLS_IP_EBP_x(context,16); }
649 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_20) { x_SUnMapLS_IP_EBP_x(context,20); }
650 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_24) { x_SUnMapLS_IP_EBP_x(context,24); }
651 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_28) { x_SUnMapLS_IP_EBP_x(context,28); }
652 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_32) { x_SUnMapLS_IP_EBP_x(context,32); }
653 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_36) { x_SUnMapLS_IP_EBP_x(context,36); }
654 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_40) { x_SUnMapLS_IP_EBP_x(context,40); }
656 /**********************************************************************
657 * AllocMappedBuffer (KERNEL32.38)
659 * This is a undocumented KERNEL32 function that
660 * SMapLS's a GlobalAlloc'ed buffer.
662 * Input: EDI register: size of buffer to allocate
663 * Output: EDI register: pointer to buffer
665 * Note: The buffer is preceeded by 8 bytes:
666 * ...
667 * edi+0 buffer
668 * edi-4 SEGPTR to buffer
669 * edi-8 some magic Win95 needs for SUnMapLS
670 * (we use it for the memory handle)
672 * The SEGPTR is used by the caller!
675 REGS_ENTRYPOINT(AllocMappedBuffer)
677 HGLOBAL32 handle = GlobalAlloc32(0, EDI_reg(context) + 8);
678 DWORD *buffer = (DWORD *)GlobalLock32(handle);
679 SEGPTR ptr = 0;
681 if (buffer)
682 if (!(ptr = MapLS(buffer + 2)))
684 GlobalUnlock32(handle);
685 GlobalFree32(handle);
688 if (!ptr)
689 EAX_reg(context) = EDI_reg(context) = 0;
690 else
692 buffer[0] = handle;
693 buffer[1] = ptr;
695 EAX_reg(context) = (DWORD) ptr;
696 EDI_reg(context) = (DWORD)(buffer + 2);
700 /**********************************************************************
701 * FreeMappedBuffer (KERNEL32.39)
703 * Free a buffer allocated by AllocMappedBuffer
705 * Input: EDI register: pointer to buffer
708 REGS_ENTRYPOINT(FreeMappedBuffer)
710 if (EDI_reg(context))
712 DWORD *buffer = (DWORD *)EDI_reg(context) - 2;
714 UnMapLS(buffer[1]);
716 GlobalUnlock32(buffer[0]);
717 GlobalFree32(buffer[0]);
721 /**********************************************************************
722 * WOWGetVDMPointer (KERNEL32.55)
723 * Get linear from segmented pointer. (MSDN lib)
725 LPVOID WINAPI WOWGetVDMPointer(DWORD vp,DWORD nrofbytes,BOOL32 protected)
727 /* FIXME: add size check too */
728 if (protected)
729 return PTR_SEG_TO_LIN(vp);
730 else
731 return DOSMEM_MapRealToLinear(vp);
734 /**********************************************************************
735 * GetVDMPointer32W (KERNEL.516)
737 LPVOID WINAPI GetVDMPointer32W(DWORD vp,DWORD mode)
739 return WOWGetVDMPointer(vp,0,mode);
742 /**********************************************************************
743 * WOWGetVDMPointerFix (KERNEL32.55)
744 * Dito, but fix heapsegment (MSDN lib)
746 LPVOID WINAPI WOWGetVDMPointerFix(DWORD vp,DWORD nrofbytes,BOOL32 protected)
748 /* FIXME: fix heapsegment */
749 return WOWGetVDMPointer(vp,nrofbytes,protected);
752 /**********************************************************************
753 * WOWGetVDMPointerUnFix (KERNEL32.56)
755 void WINAPI WOWGetVDMPointerUnfix(DWORD vp)
757 /* FIXME: unfix heapsegment */
760 /***********************************************************************
761 * UTSelectorOffsetToLinear (WIN32S16.48)
763 * rough guesswork, but seems to work (I had no "reasonable" docu)
765 LPVOID WINAPI UTSelectorOffsetToLinear(SEGPTR sptr)
767 return PTR_SEG_TO_LIN(sptr);
770 /***********************************************************************
771 * UTLinearToSelectorOffset (WIN32S16.49)
773 * FIXME: I don't know if that's the right way to do linear -> segmented
775 SEGPTR WINAPI UTLinearToSelectorOffset(LPVOID lptr)
777 return (SEGPTR)lptr;