Fix/make the case of Bochs and Plex86 more consistent.
[wine.git] / memory / selector.c
blob7de71c6bb5e4a0101d6d2bdb7a279f191ceb3fe3
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
26 #include "winerror.h"
27 #include "wine/winbase16.h"
28 #include "miscemu.h"
29 #include "selectors.h"
30 #include "stackframe.h"
31 #include "wine/server.h"
32 #include "wine/debug.h"
33 #include "toolhelp.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(selector);
37 #define LDT_SIZE 8192
39 /* get the number of selectors needed to cover up to the selector limit */
40 inline static WORD get_sel_count( WORD sel )
42 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
46 /***********************************************************************
47 * AllocSelectorArray (KERNEL.206)
49 WORD WINAPI AllocSelectorArray16( WORD count )
51 WORD i, sel = wine_ldt_alloc_entries( count );
53 if (sel)
55 LDT_ENTRY entry;
56 wine_ldt_set_base( &entry, 0 );
57 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
58 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
59 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
61 return sel;
65 /***********************************************************************
66 * AllocSelector (KERNEL.175)
68 WORD WINAPI AllocSelector16( WORD sel )
70 WORD newsel, count, i;
72 count = sel ? get_sel_count(sel) : 1;
73 newsel = wine_ldt_alloc_entries( count );
74 TRACE("(%04x): returning %04x\n", sel, newsel );
75 if (!newsel) return 0;
76 if (!sel) return newsel; /* nothing to copy */
77 for (i = 0; i < count; i++)
79 LDT_ENTRY entry;
80 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
81 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
83 return newsel;
87 /***********************************************************************
88 * FreeSelector (KERNEL.176)
90 WORD WINAPI FreeSelector16( WORD sel )
92 LDT_ENTRY entry;
94 wine_ldt_get_entry( sel, &entry );
95 if (wine_ldt_is_empty( &entry )) return sel; /* error */
96 #ifdef __i386__
97 /* Check if we are freeing current %fs selector */
98 if (!((wine_get_fs() ^ sel) & ~3))
99 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
100 #endif /* __i386__ */
101 wine_ldt_free_entries( sel, 1 );
102 return 0;
106 /***********************************************************************
107 * SELECTOR_SetEntries
109 * Set the LDT entries for an array of selectors.
111 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
113 LDT_ENTRY entry;
114 WORD i, count;
116 wine_ldt_set_base( &entry, base );
117 wine_ldt_set_limit( &entry, size - 1 );
118 wine_ldt_set_flags( &entry, flags );
119 count = (size + 0xffff) / 0x10000;
120 for (i = 0; i < count; i++)
122 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
123 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
124 /* yep, Windows sets limit like that, not 64K sel units */
125 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
130 /***********************************************************************
131 * SELECTOR_AllocBlock
133 * Allocate selectors for a block of linear memory.
135 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
137 WORD sel, count;
139 if (!size) return 0;
140 count = (size + 0xffff) / 0x10000;
141 sel = wine_ldt_alloc_entries( count );
142 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
143 return sel;
147 /***********************************************************************
148 * SELECTOR_FreeBlock
150 * Free a block of selectors.
152 void SELECTOR_FreeBlock( WORD sel )
154 WORD i, count = get_sel_count( sel );
156 TRACE("(%04x,%d)\n", sel, count );
157 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
161 /***********************************************************************
162 * SELECTOR_ReallocBlock
164 * Change the size of a block of selectors.
166 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
168 LDT_ENTRY entry;
169 int oldcount, newcount;
171 if (!size) size = 1;
172 wine_ldt_get_entry( sel, &entry );
173 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
174 newcount = (size + 0xffff) >> 16;
176 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
177 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
178 return sel;
182 /***********************************************************************
183 * PrestoChangoSelector (KERNEL.177)
185 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
187 LDT_ENTRY entry;
188 wine_ldt_get_entry( selSrc, &entry );
189 /* toggle the executable bit */
190 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
191 wine_ldt_set_entry( selDst, &entry );
192 return selDst;
196 /***********************************************************************
197 * AllocCStoDSAlias (KERNEL.170)
198 * AllocAlias (KERNEL.172)
200 WORD WINAPI AllocCStoDSAlias16( WORD sel )
202 WORD newsel;
203 LDT_ENTRY entry;
205 newsel = wine_ldt_alloc_entries( 1 );
206 TRACE("(%04x): returning %04x\n",
207 sel, newsel );
208 if (!newsel) return 0;
209 wine_ldt_get_entry( sel, &entry );
210 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
211 wine_ldt_set_entry( newsel, &entry );
212 return newsel;
216 /***********************************************************************
217 * AllocDStoCSAlias (KERNEL.171)
219 WORD WINAPI AllocDStoCSAlias16( WORD sel )
221 WORD newsel;
222 LDT_ENTRY entry;
224 newsel = wine_ldt_alloc_entries( 1 );
225 TRACE("(%04x): returning %04x\n",
226 sel, newsel );
227 if (!newsel) return 0;
228 wine_ldt_get_entry( sel, &entry );
229 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
230 wine_ldt_set_entry( newsel, &entry );
231 return newsel;
235 /***********************************************************************
236 * LongPtrAdd (KERNEL.180)
238 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
240 LDT_ENTRY entry;
241 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
242 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
243 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
247 /***********************************************************************
248 * GetSelectorBase (KERNEL.186)
250 DWORD WINAPI GetSelectorBase( WORD sel )
252 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
254 /* if base points into DOSMEM, assume we have to
255 * return pointer into physical lower 1MB */
257 return DOSMEM_MapLinearToDos( base );
261 /***********************************************************************
262 * SetSelectorBase (KERNEL.187)
264 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
266 LDT_ENTRY entry;
267 wine_ldt_get_entry( sel, &entry );
268 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
269 wine_ldt_set_entry( sel, &entry );
270 return sel;
274 /***********************************************************************
275 * GetSelectorLimit (KERNEL.188)
277 DWORD WINAPI GetSelectorLimit16( WORD sel )
279 return wine_ldt_copy.limit[sel >> __AHSHIFT];
283 /***********************************************************************
284 * SetSelectorLimit (KERNEL.189)
286 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
288 LDT_ENTRY entry;
289 wine_ldt_get_entry( sel, &entry );
290 wine_ldt_set_limit( &entry, limit );
291 wine_ldt_set_entry( sel, &entry );
292 return sel;
296 /***********************************************************************
297 * SelectorAccessRights (KERNEL.196)
299 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
301 LDT_ENTRY entry;
302 wine_ldt_get_entry( sel, &entry );
304 if (op == 0) /* get */
306 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
308 else /* set */
310 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
311 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
312 wine_ldt_set_entry( sel, &entry );
313 return 0;
318 /***********************************************************************
319 * IsBadCodePtr (KERNEL.336)
321 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
323 WORD sel;
324 LDT_ENTRY entry;
326 sel = SELECTOROF(lpfn);
327 if (!sel) return TRUE;
328 wine_ldt_get_entry( sel, &entry );
329 if (wine_ldt_is_empty( &entry )) return TRUE;
330 /* check for code segment, ignoring conforming, read-only and accessed bits */
331 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
332 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
333 return FALSE;
337 /***********************************************************************
338 * IsBadStringPtr (KERNEL.337)
340 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
342 WORD sel;
343 LDT_ENTRY entry;
345 sel = SELECTOROF(ptr);
346 if (!sel) return TRUE;
347 wine_ldt_get_entry( sel, &entry );
348 if (wine_ldt_is_empty( &entry )) return TRUE;
349 /* check for data or readable code segment */
350 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
351 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
352 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
353 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
354 return FALSE;
358 /***********************************************************************
359 * IsBadHugeReadPtr (KERNEL.346)
361 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
363 WORD sel;
364 LDT_ENTRY entry;
366 sel = SELECTOROF(ptr);
367 if (!sel) return TRUE;
368 wine_ldt_get_entry( sel, &entry );
369 if (wine_ldt_is_empty( &entry )) return TRUE;
370 /* check for data or readable code segment */
371 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
372 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
373 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
374 return FALSE;
378 /***********************************************************************
379 * IsBadHugeWritePtr (KERNEL.347)
381 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
383 WORD sel;
384 LDT_ENTRY entry;
386 sel = SELECTOROF(ptr);
387 if (!sel) return TRUE;
388 wine_ldt_get_entry( sel, &entry );
389 if (wine_ldt_is_empty( &entry )) return TRUE;
390 /* check for writeable data segment, ignoring expand-down and accessed flags */
391 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
392 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
393 return FALSE;
396 /***********************************************************************
397 * IsBadReadPtr (KERNEL.334)
399 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
401 return IsBadHugeReadPtr16( ptr, size );
405 /***********************************************************************
406 * IsBadWritePtr (KERNEL.335)
408 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
410 return IsBadHugeWritePtr16( ptr, size );
414 /***********************************************************************
415 * IsBadFlatReadWritePtr (KERNEL.627)
417 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
419 return bWrite? IsBadHugeWritePtr16( ptr, size )
420 : IsBadHugeReadPtr16( ptr, size );
424 /***********************************************************************
425 * MemoryRead (TOOLHELP.78)
427 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
429 LDT_ENTRY entry;
430 DWORD limit;
432 wine_ldt_get_entry( sel, &entry );
433 if (wine_ldt_is_empty( &entry )) return 0;
434 limit = wine_ldt_get_limit( &entry );
435 if (offset > limit) return 0;
436 if (offset + count > limit + 1) count = limit + 1 - offset;
437 memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
438 return count;
442 /***********************************************************************
443 * MemoryWrite (TOOLHELP.79)
445 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
447 LDT_ENTRY entry;
448 DWORD limit;
450 wine_ldt_get_entry( sel, &entry );
451 if (wine_ldt_is_empty( &entry )) return 0;
452 limit = wine_ldt_get_limit( &entry );
453 if (offset > limit) return 0;
454 if (offset + count > limit) count = limit + 1 - offset;
455 memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
456 return count;
459 /************************************* Win95 pointer mapping functions *
463 struct mapls_entry
465 struct mapls_entry *next;
466 void *addr; /* linear address */
467 int count; /* ref count */
468 WORD sel; /* selector */
471 static struct mapls_entry *first_entry;
474 /***********************************************************************
475 * MapLS (KERNEL32.@)
476 * MapLS (KERNEL.358)
478 * Maps linear pointer to segmented.
480 SEGPTR WINAPI MapLS( LPCVOID ptr )
482 struct mapls_entry *entry, *free = NULL;
483 void *base;
484 SEGPTR ret = 0;
486 if (!HIWORD(ptr)) return (SEGPTR)ptr;
488 base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
489 HeapLock( GetProcessHeap() );
490 for (entry = first_entry; entry; entry = entry->next)
492 if (entry->addr == base) break;
493 if (!entry->count) free = entry;
496 if (!entry)
498 if (!free) /* no free entry found, create a new one */
500 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
501 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
503 HeapFree( GetProcessHeap(), 0, free );
504 goto done;
506 free->count = 0;
507 free->next = first_entry;
508 first_entry = free;
510 SetSelectorBase( free->sel, (DWORD)base );
511 free->addr = base;
512 entry = free;
514 entry->count++;
515 ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
516 done:
517 HeapUnlock( GetProcessHeap() );
518 return ret;
521 /***********************************************************************
522 * UnMapLS (KERNEL32.@)
523 * UnMapLS (KERNEL.359)
525 * Free mapped selector.
527 void WINAPI UnMapLS( SEGPTR sptr )
529 struct mapls_entry *entry;
530 WORD sel = SELECTOROF(sptr);
532 if (sel)
534 HeapLock( GetProcessHeap() );
535 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
536 if (entry && entry->count > 0) entry->count--;
537 HeapUnlock( GetProcessHeap() );
541 /***********************************************************************
542 * MapSL (KERNEL32.@)
543 * MapSL (KERNEL.357)
545 * Maps fixed segmented pointer to linear.
547 LPVOID WINAPI MapSL( SEGPTR sptr )
549 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
552 /***********************************************************************
553 * MapSLFix (KERNEL32.@)
555 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
556 * unexpected linear address change when GlobalCompact() shuffles
557 * moveable blocks.
560 LPVOID WINAPI MapSLFix( SEGPTR sptr )
562 return MapSL(sptr);
565 /***********************************************************************
566 * UnMapSLFixArray (KERNEL32.@)
569 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
571 /* Must not change EAX, hence defined as 'register' function */
574 /***********************************************************************
575 * GetThreadSelectorEntry (KERNEL32.@)
577 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
579 #ifdef __i386__
580 BOOL ret;
582 if (!(sel & 4)) /* GDT selector */
584 sel &= ~3; /* ignore RPL */
585 if (!sel) /* null selector */
587 memset( ldtent, 0, sizeof(*ldtent) );
588 return TRUE;
590 ldtent->BaseLow = 0;
591 ldtent->HighWord.Bits.BaseMid = 0;
592 ldtent->HighWord.Bits.BaseHi = 0;
593 ldtent->LimitLow = 0xffff;
594 ldtent->HighWord.Bits.LimitHi = 0xf;
595 ldtent->HighWord.Bits.Dpl = 3;
596 ldtent->HighWord.Bits.Sys = 0;
597 ldtent->HighWord.Bits.Pres = 1;
598 ldtent->HighWord.Bits.Granularity = 1;
599 ldtent->HighWord.Bits.Default_Big = 1;
600 ldtent->HighWord.Bits.Type = 0x12;
601 /* it has to be one of the system GDT selectors */
602 if (sel == (wine_get_ds() & ~3)) return TRUE;
603 if (sel == (wine_get_ss() & ~3)) return TRUE;
604 if (sel == (wine_get_cs() & ~3))
606 ldtent->HighWord.Bits.Type |= 8; /* code segment */
607 return TRUE;
609 SetLastError( ERROR_NOACCESS );
610 return FALSE;
613 SERVER_START_REQ( get_selector_entry )
615 req->handle = hthread;
616 req->entry = sel >> __AHSHIFT;
617 if ((ret = !wine_server_call_err( req )))
619 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
621 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
622 ret = FALSE;
624 else
626 wine_ldt_set_base( ldtent, (void *)reply->base );
627 wine_ldt_set_limit( ldtent, reply->limit );
628 wine_ldt_set_flags( ldtent, reply->flags );
632 SERVER_END_REQ;
633 return ret;
634 #else
635 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
636 return FALSE;
637 #endif
641 /**********************************************************************
642 * SMapLS* (KERNEL32)
643 * These functions map linear pointers at [EBP+xxx] to segmented pointers
644 * and return them.
645 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
646 * unravel them at SUnMapLS. We just store the segmented pointer there.
648 static void
649 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
650 DWORD val,ptr;
652 val =*(DWORD*)(context->Ebp + argoff);
653 if (val<0x10000) {
654 ptr=val;
655 *(DWORD*)(context->Ebp + argoff) = 0;
656 } else {
657 ptr = MapLS((LPVOID)val);
658 *(DWORD*)(context->Ebp + argoff) = ptr;
660 context->Eax = ptr;
663 /***********************************************************************
664 * SMapLS_IP_EBP_8 (KERNEL32.@)
666 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
668 /***********************************************************************
669 * SMapLS_IP_EBP_12 (KERNEL32.@)
671 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
673 /***********************************************************************
674 * SMapLS_IP_EBP_16 (KERNEL32.@)
676 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
678 /***********************************************************************
679 * SMapLS_IP_EBP_20 (KERNEL32.@)
681 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
683 /***********************************************************************
684 * SMapLS_IP_EBP_24 (KERNEL32.@)
686 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
688 /***********************************************************************
689 * SMapLS_IP_EBP_28 (KERNEL32.@)
691 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
693 /***********************************************************************
694 * SMapLS_IP_EBP_32 (KERNEL32.@)
696 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
698 /***********************************************************************
699 * SMapLS_IP_EBP_36 (KERNEL32.@)
701 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
703 /***********************************************************************
704 * SMapLS_IP_EBP_40 (KERNEL32.@)
706 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
708 /***********************************************************************
709 * SMapLS (KERNEL32.@)
711 void WINAPI SMapLS( CONTEXT86 *context )
713 if (HIWORD(context->Eax))
715 context->Eax = MapLS( (LPVOID)context->Eax );
716 context->Edx = context->Eax;
717 } else {
718 context->Edx = 0;
722 /***********************************************************************
723 * SUnMapLS (KERNEL32.@)
726 void WINAPI SUnMapLS( CONTEXT86 *context )
728 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
731 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
733 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
734 if (*ptr)
736 UnMapLS( *ptr );
737 *ptr = 0;
741 /***********************************************************************
742 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
744 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
746 /***********************************************************************
747 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
749 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
751 /***********************************************************************
752 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
754 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
756 /***********************************************************************
757 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
759 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
761 /***********************************************************************
762 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
764 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
766 /***********************************************************************
767 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
769 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
771 /***********************************************************************
772 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
774 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
776 /***********************************************************************
777 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
779 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
781 /***********************************************************************
782 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
784 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
786 /**********************************************************************
787 * AllocMappedBuffer (KERNEL32.38)
789 * This is a undocumented KERNEL32 function that
790 * SMapLS's a GlobalAlloc'ed buffer.
792 * Input: EDI register: size of buffer to allocate
793 * Output: EDI register: pointer to buffer
795 * Note: The buffer is preceded by 8 bytes:
796 * ...
797 * edi+0 buffer
798 * edi-4 SEGPTR to buffer
799 * edi-8 some magic Win95 needs for SUnMapLS
800 * (we use it for the memory handle)
802 * The SEGPTR is used by the caller!
805 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
807 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
808 DWORD *buffer = (DWORD *)GlobalLock(handle);
809 SEGPTR ptr = 0;
811 if (buffer)
812 if (!(ptr = MapLS(buffer + 2)))
814 GlobalUnlock(handle);
815 GlobalFree(handle);
818 if (!ptr)
819 context->Eax = context->Edi = 0;
820 else
822 buffer[0] = (DWORD)handle;
823 buffer[1] = ptr;
825 context->Eax = (DWORD) ptr;
826 context->Edi = (DWORD)(buffer + 2);
830 /**********************************************************************
831 * FreeMappedBuffer (KERNEL32.39)
833 * Free a buffer allocated by AllocMappedBuffer
835 * Input: EDI register: pointer to buffer
838 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
840 if (context->Edi)
842 DWORD *buffer = (DWORD *)context->Edi - 2;
844 UnMapLS(buffer[1]);
846 GlobalUnlock((HGLOBAL)buffer[0]);
847 GlobalFree((HGLOBAL)buffer[0]);