2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
14 #include "selectors.h"
15 #include "stackframe.h"
16 #include "wine/server.h"
17 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(selector);
24 /* get the number of selectors needed to cover up to the selector limit */
25 inline static WORD get_sel_count( WORD sel )
27 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
30 /***********************************************************************
33 * Allocate a selector array without setting the LDT entries
35 static WORD SELECTOR_AllocArray( WORD count )
37 WORD i, sel, size = 0;
40 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
42 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
43 else if (++size >= count) break;
45 if (i == LDT_SIZE) return 0;
48 /* mark selectors as allocated */
49 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
51 return (sel << __AHSHIFT) | 7;
55 /***********************************************************************
56 * AllocSelectorArray (KERNEL.206)
58 WORD WINAPI AllocSelectorArray16( WORD count )
60 WORD i, sel = SELECTOR_AllocArray( count );
65 wine_ldt_set_base( &entry, 0 );
66 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
67 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
68 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
74 /***********************************************************************
75 * AllocSelector (KERNEL.175)
77 WORD WINAPI AllocSelector16( WORD sel )
79 WORD newsel, count, i;
81 count = sel ? get_sel_count(sel) : 1;
82 newsel = SELECTOR_AllocArray( count );
83 TRACE("(%04x): returning %04x\n", sel, newsel );
84 if (!newsel) return 0;
85 if (!sel) return newsel; /* nothing to copy */
86 for (i = 0; i < count; i++)
89 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
90 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
96 /***********************************************************************
97 * FreeSelector (KERNEL.176)
99 WORD WINAPI FreeSelector16( WORD sel )
103 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
106 /* Check if we are freeing current %fs or %gs selector */
107 if (!((__get_fs() ^ sel) & ~7))
109 WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
112 if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
113 #endif /* __i386__ */
115 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
116 wine_ldt_set_entry( sel, &entry );
117 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
122 /***********************************************************************
123 * SELECTOR_SetEntries
125 * Set the LDT entries for an array of selectors.
127 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
132 wine_ldt_set_base( &entry, base );
133 wine_ldt_set_limit( &entry, size - 1 );
134 wine_ldt_set_flags( &entry, flags );
135 /* Make sure base and limit are not 0 together if the size is not 0 */
136 if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
137 count = (size + 0xffff) / 0x10000;
138 for (i = 0; i < count; i++)
140 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
141 wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
142 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
147 /***********************************************************************
148 * SELECTOR_AllocBlock
150 * Allocate selectors for a block of linear memory.
152 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
157 count = (size + 0xffff) / 0x10000;
158 sel = SELECTOR_AllocArray( count );
159 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
164 /***********************************************************************
167 * Free a block of selectors.
169 void SELECTOR_FreeBlock( WORD sel )
171 WORD i, count = get_sel_count( sel );
173 TRACE("(%04x,%d)\n", sel, count );
174 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
178 /***********************************************************************
179 * SELECTOR_ReallocBlock
181 * Change the size of a block of selectors.
183 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
186 WORD i, oldcount, newcount;
189 oldcount = get_sel_count( sel );
190 newcount = (size + 0xffff) >> 16;
191 wine_ldt_get_entry( sel, &entry );
193 if (oldcount < newcount) /* We need to add selectors */
195 WORD index = sel >> __AHSHIFT;
196 /* Check if the next selectors are free */
197 if (index + newcount > LDT_SIZE) i = oldcount;
199 for (i = oldcount; i < newcount; i++)
200 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
202 if (i < newcount) /* they are not free */
204 SELECTOR_FreeBlock( sel );
205 sel = SELECTOR_AllocArray( newcount );
207 else /* mark the selectors as allocated */
209 for (i = oldcount; i < newcount; i++)
210 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
213 else if (oldcount > newcount) /* We need to remove selectors */
215 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
217 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
222 /***********************************************************************
223 * PrestoChangoSelector (KERNEL.177)
225 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
228 wine_ldt_get_entry( selSrc, &entry );
229 /* toggle the executable bit */
230 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
231 wine_ldt_set_entry( selDst, &entry );
236 /***********************************************************************
237 * AllocCStoDSAlias (KERNEL.170)
238 * AllocAlias (KERNEL.172)
240 WORD WINAPI AllocCStoDSAlias16( WORD sel )
245 newsel = SELECTOR_AllocArray( 1 );
246 TRACE("(%04x): returning %04x\n",
248 if (!newsel) return 0;
249 wine_ldt_get_entry( sel, &entry );
250 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
251 wine_ldt_set_entry( newsel, &entry );
256 /***********************************************************************
257 * AllocDStoCSAlias (KERNEL.171)
259 WORD WINAPI AllocDStoCSAlias16( WORD sel )
264 newsel = SELECTOR_AllocArray( 1 );
265 TRACE("(%04x): returning %04x\n",
267 if (!newsel) return 0;
268 wine_ldt_get_entry( sel, &entry );
269 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
270 wine_ldt_set_entry( newsel, &entry );
275 /***********************************************************************
276 * LongPtrAdd (KERNEL.180)
278 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
281 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
282 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
283 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
287 /***********************************************************************
288 * GetSelectorBase (KERNEL.186)
290 DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
293 * Note: For Win32s processes, the whole linear address space is
294 * shifted by 0x10000 relative to the OS linear address space.
295 * See the comment in msdos/vxd.c.
298 DWORD base = GetSelectorBase( sel );
299 return W32S_WINE2APP( base );
301 DWORD WINAPI GetSelectorBase( WORD sel )
303 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
305 /* if base points into DOSMEM, assume we have to
306 * return pointer into physical lower 1MB */
308 return DOSMEM_MapLinearToDos( base );
312 /***********************************************************************
313 * SetSelectorBase (KERNEL.187)
315 DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
318 * Note: For Win32s processes, the whole linear address space is
319 * shifted by 0x10000 relative to the OS linear address space.
320 * See the comment in msdos/vxd.c.
323 SetSelectorBase( sel, W32S_APP2WINE( base ) );
326 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
329 wine_ldt_get_entry( sel, &entry );
330 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
331 wine_ldt_set_entry( sel, &entry );
336 /***********************************************************************
337 * GetSelectorLimit (KERNEL.188)
339 DWORD WINAPI GetSelectorLimit16( WORD sel )
341 return wine_ldt_copy.limit[sel >> __AHSHIFT];
345 /***********************************************************************
346 * SetSelectorLimit (KERNEL.189)
348 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
351 wine_ldt_get_entry( sel, &entry );
352 wine_ldt_set_limit( &entry, limit );
353 wine_ldt_set_entry( sel, &entry );
358 /***********************************************************************
359 * SelectorAccessRights (KERNEL.196)
361 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
364 wine_ldt_get_entry( sel, &entry );
366 if (op == 0) /* get */
368 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
372 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
373 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
374 wine_ldt_set_entry( sel, &entry );
380 /***********************************************************************
381 * IsBadCodePtr (KERNEL.336)
383 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
388 sel = SELECTOROF(lpfn);
389 if (!sel) return TRUE;
390 if (IS_SELECTOR_FREE(sel)) return TRUE;
391 wine_ldt_get_entry( sel, &entry );
392 /* check for code segment, ignoring conforming, read-only and accessed bits */
393 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
394 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
399 /***********************************************************************
400 * IsBadStringPtr (KERNEL.337)
402 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
407 sel = SELECTOROF(ptr);
408 if (!sel) return TRUE;
409 if (IS_SELECTOR_FREE(sel)) return TRUE;
410 wine_ldt_get_entry( sel, &entry );
411 /* check for data or readable code segment */
412 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
413 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
414 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
415 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
420 /***********************************************************************
421 * IsBadHugeReadPtr (KERNEL.346)
423 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
428 sel = SELECTOROF(ptr);
429 if (!sel) return TRUE;
430 if (IS_SELECTOR_FREE(sel)) return TRUE;
431 wine_ldt_get_entry( sel, &entry );
432 /* check for data or readable code segment */
433 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
434 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
435 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
440 /***********************************************************************
441 * IsBadHugeWritePtr (KERNEL.347)
443 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
448 sel = SELECTOROF(ptr);
449 if (!sel) return TRUE;
450 if (IS_SELECTOR_FREE(sel)) return TRUE;
451 wine_ldt_get_entry( sel, &entry );
452 /* check for writeable data segment, ignoring expand-down and accessed flags */
453 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
454 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
458 /***********************************************************************
459 * IsBadReadPtr (KERNEL.334)
461 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
463 return IsBadHugeReadPtr16( ptr, size );
467 /***********************************************************************
468 * IsBadWritePtr (KERNEL.335)
470 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
472 return IsBadHugeWritePtr16( ptr, size );
476 /***********************************************************************
477 * IsBadFlatReadWritePtr (KERNEL.627)
479 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
481 return bWrite? IsBadHugeWritePtr16( ptr, size )
482 : IsBadHugeReadPtr16( ptr, size );
486 /***********************************************************************
487 * MemoryRead (TOOLHELP.78)
489 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
491 WORD index = sel >> __AHSHIFT;
493 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
494 if (offset > wine_ldt_copy.limit[index]) return 0;
495 if (offset + count > wine_ldt_copy.limit[index] + 1)
496 count = wine_ldt_copy.limit[index] + 1 - offset;
497 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
502 /***********************************************************************
503 * MemoryWrite (TOOLHELP.79)
505 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
507 WORD index = sel >> __AHSHIFT;
509 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
510 if (offset > wine_ldt_copy.limit[index]) return 0;
511 if (offset + count > wine_ldt_copy.limit[index] + 1)
512 count = wine_ldt_copy.limit[index] + 1 - offset;
513 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
517 /************************************* Win95 pointer mapping functions *
521 /***********************************************************************
525 * Maps fixed segmented pointer to linear.
527 LPVOID WINAPI MapSL( SEGPTR sptr )
529 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
532 /***********************************************************************
533 * MapSLFix (KERNEL32.@)
535 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
536 * unexpected linear address change when GlobalCompact() shuffles
540 LPVOID WINAPI MapSLFix( SEGPTR sptr )
545 /***********************************************************************
546 * UnMapSLFixArray (KERNEL32.@)
549 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
551 /* Must not change EAX, hence defined as 'register' function */
554 /***********************************************************************
555 * GetThreadSelectorEntry (KERNEL32.@)
557 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
562 if (!(sel & 4)) /* GDT selector */
564 sel &= ~3; /* ignore RPL */
565 if (!sel) /* null selector */
567 memset( ldtent, 0, sizeof(*ldtent) );
571 ldtent->HighWord.Bits.BaseMid = 0;
572 ldtent->HighWord.Bits.BaseHi = 0;
573 ldtent->LimitLow = 0xffff;
574 ldtent->HighWord.Bits.LimitHi = 0xf;
575 ldtent->HighWord.Bits.Dpl = 3;
576 ldtent->HighWord.Bits.Sys = 0;
577 ldtent->HighWord.Bits.Pres = 1;
578 ldtent->HighWord.Bits.Granularity = 1;
579 ldtent->HighWord.Bits.Default_Big = 1;
580 ldtent->HighWord.Bits.Type = 0x12;
581 /* it has to be one of the system GDT selectors */
582 if (sel == (__get_ds() & ~3)) return TRUE;
583 if (sel == (__get_ss() & ~3)) return TRUE;
584 if (sel == (__get_cs() & ~3))
586 ldtent->HighWord.Bits.Type |= 8; /* code segment */
589 SetLastError( ERROR_NOACCESS );
593 SERVER_START_REQ( get_selector_entry )
595 req->handle = hthread;
596 req->entry = sel >> __AHSHIFT;
597 if ((ret = !SERVER_CALL_ERR()))
599 if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
601 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
606 wine_ldt_set_base( ldtent, (void *)req->base );
607 wine_ldt_set_limit( ldtent, req->limit );
608 wine_ldt_set_flags( ldtent, req->flags );
615 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
621 /**********************************************************************
623 * These functions map linear pointers at [EBP+xxx] to segmented pointers
625 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
626 * unravel them at SUnMapLS. We just store the segmented pointer there.
629 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
632 val =*(DWORD*)(context->Ebp + argoff);
635 *(DWORD*)(context->Ebp + argoff) = 0;
637 ptr = MapLS((LPVOID)val);
638 *(DWORD*)(context->Ebp + argoff) = ptr;
643 /***********************************************************************
644 * SMapLS_IP_EBP_8 (KERNEL32.@)
646 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
648 /***********************************************************************
649 * SMapLS_IP_EBP_12 (KERNEL32.@)
651 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
653 /***********************************************************************
654 * SMapLS_IP_EBP_16 (KERNEL32.@)
656 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
658 /***********************************************************************
659 * SMapLS_IP_EBP_20 (KERNEL32.@)
661 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
663 /***********************************************************************
664 * SMapLS_IP_EBP_24 (KERNEL32.@)
666 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
668 /***********************************************************************
669 * SMapLS_IP_EBP_28 (KERNEL32.@)
671 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
673 /***********************************************************************
674 * SMapLS_IP_EBP_32 (KERNEL32.@)
676 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
678 /***********************************************************************
679 * SMapLS_IP_EBP_36 (KERNEL32.@)
681 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
683 /***********************************************************************
684 * SMapLS_IP_EBP_40 (KERNEL32.@)
686 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
688 /***********************************************************************
689 * SMapLS (KERNEL32.@)
691 void WINAPI SMapLS( CONTEXT86 *context )
693 if (HIWORD(context->Eax))
695 context->Eax = MapLS( (LPVOID)context->Eax );
696 context->Edx = context->Eax;
702 /***********************************************************************
703 * SUnMapLS (KERNEL32.@)
706 void WINAPI SUnMapLS( CONTEXT86 *context )
708 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
711 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
713 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
721 /***********************************************************************
722 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
724 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
726 /***********************************************************************
727 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
729 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
731 /***********************************************************************
732 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
734 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
736 /***********************************************************************
737 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
739 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
741 /***********************************************************************
742 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
744 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
746 /***********************************************************************
747 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
749 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
751 /***********************************************************************
752 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
754 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
756 /***********************************************************************
757 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
759 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
761 /***********************************************************************
762 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
764 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
766 /**********************************************************************
767 * AllocMappedBuffer (KERNEL32.38)
769 * This is a undocumented KERNEL32 function that
770 * SMapLS's a GlobalAlloc'ed buffer.
772 * Input: EDI register: size of buffer to allocate
773 * Output: EDI register: pointer to buffer
775 * Note: The buffer is preceded by 8 bytes:
778 * edi-4 SEGPTR to buffer
779 * edi-8 some magic Win95 needs for SUnMapLS
780 * (we use it for the memory handle)
782 * The SEGPTR is used by the caller!
785 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
787 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
788 DWORD *buffer = (DWORD *)GlobalLock(handle);
792 if (!(ptr = MapLS(buffer + 2)))
794 GlobalUnlock(handle);
799 context->Eax = context->Edi = 0;
805 context->Eax = (DWORD) ptr;
806 context->Edi = (DWORD)(buffer + 2);
810 /**********************************************************************
811 * FreeMappedBuffer (KERNEL32.39)
813 * Free a buffer allocated by AllocMappedBuffer
815 * Input: EDI register: pointer to buffer
818 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
822 DWORD *buffer = (DWORD *)context->Edi - 2;
826 GlobalUnlock(buffer[0]);
827 GlobalFree(buffer[0]);
832 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
833 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
834 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
835 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
836 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
837 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
838 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
839 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )