Add missing #include <netinet/in.h> to get struct in_addr on all platforms.
[wine/wine64.git] / memory / selector.c
blobd5d8df089d4c1e9e59120b07d6dd47bcb1bd0ffe
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <string.h>
9 #include "config.h"
10 #include "winerror.h"
11 #include "wine/winbase16.h"
12 #include "ldt.h"
13 #include "miscemu.h"
14 #include "selectors.h"
15 #include "stackframe.h"
16 #include "process.h"
17 #include "server.h"
18 #include "debugtools.h"
19 #include "toolhelp.h"
21 DEFAULT_DEBUG_CHANNEL(selector);
23 #define LDT_SIZE 8192
25 /* get the number of selectors needed to cover up to the selector limit */
26 inline static WORD get_sel_count( WORD sel )
28 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
31 /***********************************************************************
32 * SELECTOR_AllocArray
34 * Allocate a selector array without setting the LDT entries
36 static WORD SELECTOR_AllocArray( WORD count )
38 WORD i, sel, size = 0;
40 if (!count) return 0;
41 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
43 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
44 else if (++size >= count) break;
46 if (i == LDT_SIZE) return 0;
47 sel = i - size + 1;
49 /* mark selectors as allocated */
50 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
52 return (sel << __AHSHIFT) | 7;
56 /***********************************************************************
57 * AllocSelectorArray (KERNEL.206)
59 WORD WINAPI AllocSelectorArray16( WORD count )
61 WORD i, sel = SELECTOR_AllocArray( count );
63 if (sel)
65 LDT_ENTRY entry;
66 wine_ldt_set_base( &entry, 0 );
67 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
68 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
69 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
71 return sel;
75 /***********************************************************************
76 * AllocSelector (KERNEL.175)
78 WORD WINAPI AllocSelector16( WORD sel )
80 WORD newsel, count, i;
82 count = sel ? get_sel_count(sel) : 1;
83 newsel = SELECTOR_AllocArray( count );
84 TRACE("(%04x): returning %04x\n", sel, newsel );
85 if (!newsel) return 0;
86 if (!sel) return newsel; /* nothing to copy */
87 for (i = 0; i < count; i++)
89 LDT_ENTRY entry;
90 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
91 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
93 return newsel;
97 /***********************************************************************
98 * FreeSelector (KERNEL.176)
100 WORD WINAPI FreeSelector16( WORD sel )
102 LDT_ENTRY entry;
104 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
106 #ifdef __i386__
107 /* Check if we are freeing current %fs or %gs selector */
108 if (!((__get_fs() ^ sel) & ~7))
110 WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
111 __set_fs( 0 );
113 if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
114 #endif /* __i386__ */
116 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
117 wine_ldt_set_entry( sel, &entry );
118 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
119 return 0;
123 /***********************************************************************
124 * SELECTOR_SetEntries
126 * Set the LDT entries for an array of selectors.
128 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
130 LDT_ENTRY entry;
131 WORD i, count;
133 wine_ldt_set_base( &entry, base );
134 wine_ldt_set_limit( &entry, size - 1 );
135 wine_ldt_set_flags( &entry, flags );
136 /* Make sure base and limit are not 0 together if the size is not 0 */
137 if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
138 count = (size + 0xffff) / 0x10000;
139 for (i = 0; i < count; i++)
141 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
142 wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
143 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
148 /***********************************************************************
149 * SELECTOR_AllocBlock
151 * Allocate selectors for a block of linear memory.
153 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
155 WORD sel, count;
157 if (!size) return 0;
158 count = (size + 0xffff) / 0x10000;
159 sel = SELECTOR_AllocArray( count );
160 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
161 return sel;
165 /***********************************************************************
166 * SELECTOR_FreeBlock
168 * Free a block of selectors.
170 void SELECTOR_FreeBlock( WORD sel )
172 WORD i, count = get_sel_count( sel );
174 TRACE("(%04x,%d)\n", sel, count );
175 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
179 /***********************************************************************
180 * SELECTOR_ReallocBlock
182 * Change the size of a block of selectors.
184 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
186 LDT_ENTRY entry;
187 WORD i, oldcount, newcount;
189 if (!size) size = 1;
190 oldcount = get_sel_count( sel );
191 newcount = (size + 0xffff) >> 16;
192 wine_ldt_get_entry( sel, &entry );
194 if (oldcount < newcount) /* We need to add selectors */
196 WORD index = sel >> __AHSHIFT;
197 /* Check if the next selectors are free */
198 if (index + newcount > LDT_SIZE) i = oldcount;
199 else
200 for (i = oldcount; i < newcount; i++)
201 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
203 if (i < newcount) /* they are not free */
205 SELECTOR_FreeBlock( sel );
206 sel = SELECTOR_AllocArray( newcount );
208 else /* mark the selectors as allocated */
210 for (i = oldcount; i < newcount; i++)
211 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
214 else if (oldcount > newcount) /* We need to remove selectors */
216 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
218 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
219 return sel;
223 /***********************************************************************
224 * PrestoChangoSelector (KERNEL.177)
226 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
228 LDT_ENTRY entry;
229 wine_ldt_get_entry( selSrc, &entry );
230 /* toggle the executable bit */
231 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
232 wine_ldt_set_entry( selDst, &entry );
233 return selDst;
237 /***********************************************************************
238 * AllocCStoDSAlias (KERNEL.170)
240 WORD WINAPI AllocCStoDSAlias16( WORD sel )
242 WORD newsel;
243 LDT_ENTRY entry;
245 newsel = SELECTOR_AllocArray( 1 );
246 TRACE("(%04x): returning %04x\n",
247 sel, newsel );
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 );
252 return newsel;
256 /***********************************************************************
257 * AllocDStoCSAlias (KERNEL.171)
259 WORD WINAPI AllocDStoCSAlias16( WORD sel )
261 WORD newsel;
262 LDT_ENTRY entry;
264 newsel = SELECTOR_AllocArray( 1 );
265 TRACE("(%04x): returning %04x\n",
266 sel, newsel );
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 );
271 return newsel;
275 /***********************************************************************
276 * LongPtrAdd (KERNEL.180)
278 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
280 LDT_ENTRY entry;
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, W32S_APPLICATION() ? W32S_OFFSET : 0 );
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,
324 W32S_APP2WINE( base, W32S_APPLICATION() ? W32S_OFFSET : 0 ) );
325 return sel;
327 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
329 LDT_ENTRY entry;
330 wine_ldt_get_entry( sel, &entry );
331 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
332 wine_ldt_set_entry( sel, &entry );
333 return sel;
337 /***********************************************************************
338 * GetSelectorLimit (KERNEL.188)
340 DWORD WINAPI GetSelectorLimit16( WORD sel )
342 return wine_ldt_copy.limit[sel >> __AHSHIFT];
346 /***********************************************************************
347 * SetSelectorLimit (KERNEL.189)
349 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
351 LDT_ENTRY entry;
352 wine_ldt_get_entry( sel, &entry );
353 wine_ldt_set_limit( &entry, limit );
354 wine_ldt_set_entry( sel, &entry );
355 return sel;
359 /***********************************************************************
360 * SelectorAccessRights (KERNEL.196)
362 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
364 LDT_ENTRY entry;
365 wine_ldt_get_entry( sel, &entry );
367 if (op == 0) /* get */
369 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
371 else /* set */
373 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
374 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
375 wine_ldt_set_entry( sel, &entry );
376 return 0;
381 /***********************************************************************
382 * IsBadCodePtr16 (KERNEL.336)
384 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
386 WORD sel;
387 LDT_ENTRY entry;
389 sel = SELECTOROF(lpfn);
390 if (!sel) return TRUE;
391 if (IS_SELECTOR_FREE(sel)) return TRUE;
392 wine_ldt_get_entry( sel, &entry );
393 /* check for code segment, ignoring conforming, read-only and accessed bits */
394 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
395 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
396 return FALSE;
400 /***********************************************************************
401 * IsBadStringPtr16 (KERNEL.337)
403 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
405 WORD sel;
406 LDT_ENTRY entry;
408 sel = SELECTOROF(ptr);
409 if (!sel) return TRUE;
410 if (IS_SELECTOR_FREE(sel)) return TRUE;
411 wine_ldt_get_entry( sel, &entry );
412 /* check for data or readable code segment */
413 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
414 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
415 if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr)) + 1;
416 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
417 return FALSE;
421 /***********************************************************************
422 * IsBadHugeReadPtr16 (KERNEL.346)
424 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
426 WORD sel;
427 LDT_ENTRY entry;
429 sel = SELECTOROF(ptr);
430 if (!sel) return TRUE;
431 if (IS_SELECTOR_FREE(sel)) return TRUE;
432 wine_ldt_get_entry( sel, &entry );
433 /* check for data or readable code segment */
434 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
435 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
436 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
437 return FALSE;
441 /***********************************************************************
442 * IsBadHugeWritePtr16 (KERNEL.347)
444 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
446 WORD sel;
447 LDT_ENTRY entry;
449 sel = SELECTOROF(ptr);
450 if (!sel) return TRUE;
451 if (IS_SELECTOR_FREE(sel)) return TRUE;
452 wine_ldt_get_entry( sel, &entry );
453 /* check for writeable data segment, ignoring expand-down and accessed flags */
454 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
455 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
456 return FALSE;
459 /***********************************************************************
460 * IsBadReadPtr16 (KERNEL.334)
462 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
464 return IsBadHugeReadPtr16( ptr, size );
468 /***********************************************************************
469 * IsBadWritePtr16 (KERNEL.335)
471 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
473 return IsBadHugeWritePtr16( ptr, size );
477 /***********************************************************************
478 * IsBadFlatReadWritePtr16 (KERNEL.627)
480 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
482 return bWrite? IsBadHugeWritePtr16( ptr, size )
483 : IsBadHugeReadPtr16( ptr, size );
487 /***********************************************************************
488 * MemoryRead (TOOLHELP.78)
490 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
492 WORD index = sel >> __AHSHIFT;
494 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
495 if (offset > wine_ldt_copy.limit[index]) return 0;
496 if (offset + count > wine_ldt_copy.limit[index] + 1)
497 count = wine_ldt_copy.limit[index] + 1 - offset;
498 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
499 return count;
503 /***********************************************************************
504 * MemoryWrite (TOOLHELP.79)
506 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
508 WORD index = sel >> __AHSHIFT;
510 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
511 if (offset > wine_ldt_copy.limit[index]) return 0;
512 if (offset + count > wine_ldt_copy.limit[index] + 1)
513 count = wine_ldt_copy.limit[index] + 1 - offset;
514 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
515 return count;
518 /************************************* Win95 pointer mapping functions *
522 /***********************************************************************
523 * MapSL (KERNEL32.523)
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.524)
535 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
536 * unexpected linear address change when GlobalCompact() shuffles
537 * moveable blocks.
540 LPVOID WINAPI MapSLFix( SEGPTR sptr )
542 return (LPVOID)PTR_SEG_TO_LIN(sptr);
545 /***********************************************************************
546 * UnMapSLFixArray (KERNEL32.701)
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)
559 #ifdef __i386__
560 BOOL ret;
562 if (!(sel & 4)) /* GDT selector */
564 sel &= ~3; /* ignore RPL */
565 if (!sel) /* null selector */
567 memset( ldtent, 0, sizeof(*ldtent) );
568 return TRUE;
570 ldtent->BaseLow = 0;
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 */
587 return TRUE;
589 SetLastError( ERROR_NOACCESS );
590 return FALSE;
593 SERVER_START_REQ
595 struct get_selector_entry_request *req = server_alloc_req( sizeof(*req), 0 );
597 req->handle = hthread;
598 req->entry = sel >> __AHSHIFT;
599 if ((ret = !server_call( REQ_GET_SELECTOR_ENTRY )))
601 if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
603 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
604 ret = FALSE;
606 else
608 wine_ldt_set_base( ldtent, (void *)req->base );
609 wine_ldt_set_limit( ldtent, req->limit );
610 wine_ldt_set_flags( ldtent, req->flags );
614 SERVER_END_REQ;
615 return ret;
616 #else
617 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
618 return FALSE;
619 #endif
623 /**********************************************************************
624 * SMapLS* (KERNEL32)
625 * These functions map linear pointers at [EBP+xxx] to segmented pointers
626 * and return them.
627 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
628 * unravel them at SUnMapLS. We just store the segmented pointer there.
630 static void
631 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
632 DWORD val,ptr;
634 val =*(DWORD*)(context->Ebp + argoff);
635 if (val<0x10000) {
636 ptr=val;
637 *(DWORD*)(context->Ebp + argoff) = 0;
638 } else {
639 ptr = MapLS((LPVOID)val);
640 *(DWORD*)(context->Ebp + argoff) = ptr;
642 context->Eax = ptr;
645 /***********************************************************************
646 * SMapLS_IP_EBP_8 (KERNEL32.601)
648 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
650 /***********************************************************************
651 * SMapLS_IP_EBP_12 (KERNEL32.593)
653 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
655 /***********************************************************************
656 * SMapLS_IP_EBP_16 (KERNEL32.594)
658 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
660 /***********************************************************************
661 * SMapLS_IP_EBP_20 (KERNEL32.595)
663 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
665 /***********************************************************************
666 * SMapLS_IP_EBP_24 (KERNEL32.596)
668 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
670 /***********************************************************************
671 * SMapLS_IP_EBP_28 (KERNEL32.597)
673 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
675 /***********************************************************************
676 * SMapLS_IP_EBP_32 (KERNEL32.598)
678 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
680 /***********************************************************************
681 * SMapLS_IP_EBP_36 (KERNEL32.599)
683 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
685 /***********************************************************************
686 * SMapLS_IP_EBP_40 (KERNEL32.600)
688 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
690 /***********************************************************************
691 * SMapLS (KERNEL32.592)
693 void WINAPI SMapLS( CONTEXT86 *context )
695 if (HIWORD(context->Eax))
697 context->Eax = MapLS( (LPVOID)context->Eax );
698 context->Edx = context->Eax;
699 } else {
700 context->Edx = 0;
704 /***********************************************************************
705 * SUnMapLS (KERNEL32.602)
708 void WINAPI SUnMapLS( CONTEXT86 *context )
710 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
713 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
715 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
716 if (*ptr)
718 UnMapLS( *ptr );
719 *ptr = 0;
723 /***********************************************************************
724 * SUnMapLS_IP_EBP_8 (KERNEL32.611)
726 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
728 /***********************************************************************
729 * SUnMapLS_IP_EBP_12 (KERNEL32.603)
731 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
733 /***********************************************************************
734 * SUnMapLS_IP_EBP_16 (KERNEL32.604)
736 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
738 /***********************************************************************
739 * SUnMapLS_IP_EBP_20 (KERNEL32.605)
741 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
743 /***********************************************************************
744 * SUnMapLS_IP_EBP_24 (KERNEL32.606)
746 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
748 /***********************************************************************
749 * SUnMapLS_IP_EBP_28 (KERNEL32.607)
751 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
753 /***********************************************************************
754 * SUnMapLS_IP_EBP_32 (KERNEL32.608)
756 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
758 /***********************************************************************
759 * SUnMapLS_IP_EBP_36 (KERNEL32.609)
761 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
763 /***********************************************************************
764 * SUnMapLS_IP_EBP_40 (KERNEL32.610)
766 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
768 /**********************************************************************
769 * AllocMappedBuffer (KERNEL32.38)
771 * This is a undocumented KERNEL32 function that
772 * SMapLS's a GlobalAlloc'ed buffer.
774 * Input: EDI register: size of buffer to allocate
775 * Output: EDI register: pointer to buffer
777 * Note: The buffer is preceeded by 8 bytes:
778 * ...
779 * edi+0 buffer
780 * edi-4 SEGPTR to buffer
781 * edi-8 some magic Win95 needs for SUnMapLS
782 * (we use it for the memory handle)
784 * The SEGPTR is used by the caller!
787 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
789 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
790 DWORD *buffer = (DWORD *)GlobalLock(handle);
791 SEGPTR ptr = 0;
793 if (buffer)
794 if (!(ptr = MapLS(buffer + 2)))
796 GlobalUnlock(handle);
797 GlobalFree(handle);
800 if (!ptr)
801 context->Eax = context->Edi = 0;
802 else
804 buffer[0] = handle;
805 buffer[1] = ptr;
807 context->Eax = (DWORD) ptr;
808 context->Edi = (DWORD)(buffer + 2);
812 /**********************************************************************
813 * FreeMappedBuffer (KERNEL32.39)
815 * Free a buffer allocated by AllocMappedBuffer
817 * Input: EDI register: pointer to buffer
820 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
822 if (context->Edi)
824 DWORD *buffer = (DWORD *)context->Edi - 2;
826 UnMapLS(buffer[1]);
828 GlobalUnlock(buffer[0]);
829 GlobalFree(buffer[0]);
833 #ifdef __i386__
834 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
835 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
836 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
837 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
838 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
839 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
840 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
841 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
842 #endif