Release 980726
[wine/multimedia.git] / memory / selector.c
blobdbe68fe648b827e440c9a7a71b64792451b5b90a
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;
165 STACK16FRAME *frame;
167 TRACE(selector, "(%04x,%d)\n", sel, count );
168 sel &= ~(__AHINCR - 1); /* clear bottom bits of selector */
169 nextsel = sel + (count << __AHSHIFT);
171 #ifdef __i386__
173 /* Check if we are freeing current %fs or %gs selector */
175 WORD fs, gs;
176 GET_FS(fs);
177 if ((fs >= sel) && (fs < nextsel))
179 WARN(selector, "Freeing %%fs selector (%04x), not good.\n", fs );
180 SET_FS( 0 );
182 fs = THREAD_Current()->saved_fs;
183 if ((fs >= sel) && (fs < nextsel)) THREAD_Current()->saved_fs = 0;
184 GET_GS(gs);
185 if ((gs >= sel) && (gs < nextsel)) SET_GS( 0 );
187 #endif /* __i386__ */
189 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
190 for (i = SELECTOR_TO_ENTRY(sel); count; i++, count--)
192 LDT_SetEntry( i, &entry );
193 ldt_flags_copy[i] &= ~LDT_FLAGS_ALLOCATED;
196 /* Clear the saved 16-bit selector */
197 frame = CURRENT_STACK16;
198 while (frame && frame->frame32)
200 if ((frame->ds >= sel) && (frame->ds < nextsel)) frame->ds = 0;
201 if ((frame->es >= sel) && (frame->es < nextsel)) frame->es = 0;
202 frame = PTR_SEG_TO_LIN( frame->frame32->frame16 );
207 /***********************************************************************
208 * SELECTOR_ReallocBlock
210 * Change the size of a block of selectors.
212 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size,
213 enum seg_type type, BOOL32 is32bit, BOOL32 readonly)
215 WORD i, oldcount, newcount;
217 if (!size) size = 1;
218 oldcount = (GET_SEL_LIMIT(sel) >> 16) + 1;
219 newcount = (size + 0xffff) >> 16;
221 if (oldcount < newcount) /* We need to add selectors */
223 /* Check if the next selectors are free */
224 if (SELECTOR_TO_ENTRY(sel) + newcount > LDT_SIZE) i = oldcount;
225 else
226 for (i = oldcount; i < newcount; i++)
227 if (!IS_LDT_ENTRY_FREE(SELECTOR_TO_ENTRY(sel)+i)) break;
229 if (i < newcount) /* they are not free */
231 SELECTOR_FreeBlock( sel, oldcount );
232 sel = AllocSelectorArray( newcount );
234 else /* mark the selectors as allocated */
236 for (i = oldcount; i < newcount; i++)
237 ldt_flags_copy[SELECTOR_TO_ENTRY(sel)+i] |=LDT_FLAGS_ALLOCATED;
240 else if (oldcount > newcount) /* We need to remove selectors */
242 SELECTOR_FreeBlock( ENTRY_TO_SELECTOR(SELECTOR_TO_ENTRY(sel)+newcount),
243 oldcount - newcount );
245 if (sel) SELECTOR_SetEntries( sel, base, size, type, is32bit, readonly );
246 return sel;
250 /***********************************************************************
251 * PrestoChangoSelector (KERNEL.177)
253 WORD WINAPI PrestoChangoSelector( WORD selSrc, WORD selDst )
255 ldt_entry entry;
256 LDT_GetEntry( SELECTOR_TO_ENTRY( selSrc ), &entry );
257 entry.type ^= SEGMENT_CODE; /* toggle the executable bit */
258 LDT_SetEntry( SELECTOR_TO_ENTRY( selDst ), &entry );
259 return selDst;
263 /***********************************************************************
264 * AllocCStoDSAlias (KERNEL.170)
266 WORD WINAPI AllocCStoDSAlias( WORD sel )
268 WORD newsel;
269 ldt_entry entry;
271 newsel = AllocSelectorArray( 1 );
272 TRACE(selector, "(%04x): returning %04x\n",
273 sel, newsel );
274 if (!newsel) return 0;
275 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
276 entry.type = SEGMENT_DATA;
277 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel), &entry );
278 return newsel;
282 /***********************************************************************
283 * AllocDStoCSAlias (KERNEL.171)
285 WORD WINAPI AllocDStoCSAlias( WORD sel )
287 WORD newsel;
288 ldt_entry entry;
290 newsel = AllocSelectorArray( 1 );
291 TRACE(selector, "(%04x): returning %04x\n",
292 sel, newsel );
293 if (!newsel) return 0;
294 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
295 entry.type = SEGMENT_CODE;
296 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel), &entry );
297 return newsel;
301 /***********************************************************************
302 * LongPtrAdd (KERNEL.180)
304 void WINAPI LongPtrAdd( DWORD ptr, DWORD add )
306 ldt_entry entry;
307 LDT_GetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr)), &entry );
308 entry.base += add;
309 LDT_SetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr)), &entry );
313 /***********************************************************************
314 * GetSelectorBase (KERNEL.186)
316 DWORD WINAPI GetSelectorBase( WORD sel )
318 DWORD base = GET_SEL_BASE(sel);
320 /* if base points into DOSMEM, assume we have to
321 * return pointer into physical lower 1MB */
323 return DOSMEM_MapLinearToDos( (LPVOID)base );
327 /***********************************************************************
328 * SetSelectorBase (KERNEL.187)
330 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
332 ldt_entry entry;
334 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
336 entry.base = (DWORD)DOSMEM_MapDosToLinear(base);
338 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
339 return sel;
343 /***********************************************************************
344 * GetSelectorLimit (KERNEL.188)
346 DWORD WINAPI GetSelectorLimit( WORD sel )
348 return GET_SEL_LIMIT(sel);
352 /***********************************************************************
353 * SetSelectorLimit (KERNEL.189)
355 WORD WINAPI SetSelectorLimit( WORD sel, DWORD limit )
357 ldt_entry entry;
358 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
359 entry.limit_in_pages = (limit >= 0x100000);
360 if (entry.limit_in_pages) entry.limit = limit >> 12;
361 else entry.limit = limit;
362 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
363 return sel;
367 /***********************************************************************
368 * SelectorAccessRights (KERNEL.196)
370 WORD WINAPI SelectorAccessRights( WORD sel, WORD op, WORD val )
372 ldt_entry entry;
373 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
374 if (op == 0) /* get */
376 return 0x01 | /* accessed */
377 0x10 | /* not system */
378 0x60 | /* DPL 3 */
379 0x80 | /* present */
380 ((entry.read_only == 0) << 1) |
381 (entry.type << 2) |
382 (entry.seg_32bit << 14) |
383 (entry.limit_in_pages << 15);
385 else /* set */
387 entry.read_only = ((val & 2) == 0);
388 entry.type = (val >> 2) & 3;
389 entry.seg_32bit = val & 0x4000;
390 entry.limit_in_pages = val & 0x8000;
391 LDT_SetEntry( SELECTOR_TO_ENTRY(sel), &entry );
392 return 0;
397 /***********************************************************************
398 * IsBadCodePtr16 (KERNEL.336)
400 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
402 WORD sel;
403 ldt_entry entry;
405 sel = SELECTOROF(lpfn);
406 if (!sel) return TRUE;
407 if (IS_SELECTOR_FREE(sel)) return TRUE;
408 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
409 if (entry.type != SEGMENT_CODE) return TRUE;
410 if (OFFSETOF(lpfn) > entry.limit) return TRUE;
411 return FALSE;
415 /***********************************************************************
416 * IsBadStringPtr16 (KERNEL.337)
418 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
420 WORD sel;
421 ldt_entry entry;
423 sel = SELECTOROF(ptr);
424 if (!sel) return TRUE;
425 if (IS_SELECTOR_FREE(sel)) return TRUE;
426 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
427 if ((entry.type == SEGMENT_CODE) && entry.read_only) return TRUE;
428 if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr));
429 if (OFFSETOF(ptr) + size - 1 > entry.limit) return TRUE;
430 return FALSE;
434 /***********************************************************************
435 * IsBadHugeReadPtr16 (KERNEL.346)
437 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
439 WORD sel;
440 ldt_entry entry;
442 sel = SELECTOROF(ptr);
443 if (!sel) return TRUE;
444 if (IS_SELECTOR_FREE(sel)) return TRUE;
445 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
446 if ((entry.type == SEGMENT_CODE) && entry.read_only) return TRUE;
447 if (OFFSETOF(ptr) + size - 1 > entry.limit) return TRUE;
448 return FALSE;
452 /***********************************************************************
453 * IsBadHugeWritePtr16 (KERNEL.347)
455 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
457 WORD sel;
458 ldt_entry entry;
460 sel = SELECTOROF(ptr);
461 if (!sel) return TRUE;
462 if (IS_SELECTOR_FREE(sel)) return TRUE;
463 LDT_GetEntry( SELECTOR_TO_ENTRY(sel), &entry );
464 if ((entry.type == SEGMENT_CODE) || entry.read_only) return TRUE;
465 if (OFFSETOF(ptr) + size - 1 > entry.limit) return TRUE;
466 return FALSE;
469 /***********************************************************************
470 * IsBadReadPtr16 (KERNEL.334)
472 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
474 return IsBadHugeReadPtr16( ptr, size );
478 /***********************************************************************
479 * IsBadWritePtr16 (KERNEL.335)
481 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
483 return IsBadHugeWritePtr16( ptr, size );
487 /***********************************************************************
488 * MemoryRead (TOOLHELP.78)
490 DWORD WINAPI MemoryRead( WORD sel, DWORD offset, void *buffer, DWORD count )
492 if (IS_SELECTOR_FREE(sel)) return 0;
493 if (offset > GET_SEL_LIMIT(sel)) return 0;
494 if (offset + count > GET_SEL_LIMIT(sel) + 1)
495 count = GET_SEL_LIMIT(sel) + 1 - offset;
496 memcpy( buffer, ((char *)GET_SEL_BASE(sel)) + offset, count );
497 return count;
501 /***********************************************************************
502 * MemoryWrite (TOOLHELP.79)
504 DWORD WINAPI MemoryWrite( WORD sel, DWORD offset, void *buffer, DWORD count )
506 if (IS_SELECTOR_FREE(sel)) return 0;
507 if (offset > GET_SEL_LIMIT(sel)) return 0;
508 if (offset + count > GET_SEL_LIMIT(sel) + 1)
509 count = GET_SEL_LIMIT(sel) + 1 - offset;
510 memcpy( ((char *)GET_SEL_BASE(sel)) + offset, buffer, count );
511 return count;
514 /************************************* Win95 pointer mapping functions *
518 /***********************************************************************
519 * MapSL (KERNEL32.523)
521 * Maps fixed segmented pointer to linear.
523 LPVOID WINAPI MapSL( SEGPTR sptr )
525 return (LPVOID)PTR_SEG_TO_LIN(sptr);
528 /***********************************************************************
529 * MapSLFix (KERNEL32.524)
531 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
532 * unexpected linear address change when GlobalCompact() shuffles
533 * moveable blocks.
536 LPVOID WINAPI MapSLFix( SEGPTR sptr )
538 return (LPVOID)PTR_SEG_TO_LIN(sptr);
541 /***********************************************************************
542 * UnMapSLFixArray (KERNEL32.701)
545 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT32 length )
549 /***********************************************************************
550 * MapLS (KERNEL32.522)
552 * Maps linear pointer to segmented.
554 SEGPTR WINAPI MapLS( LPVOID ptr )
556 if (!HIWORD(ptr))
557 return (SEGPTR)ptr;
558 else
560 WORD sel = SELECTOR_AllocBlock( ptr, 0x10000, SEGMENT_DATA, FALSE, FALSE );
561 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
566 /***********************************************************************
567 * UnMapLS (KERNEL32.700)
569 * Free mapped selector.
571 void WINAPI UnMapLS( SEGPTR sptr )
573 if (SELECTOROF(sptr))
574 SELECTOR_FreeBlock( SELECTOROF(sptr), 1 );
577 /***********************************************************************
578 * GetThreadSelectorEntry (KERNEL32)
579 * FIXME: add #ifdef i386 for non x86
581 BOOL32 WINAPI GetThreadSelectorEntry( HANDLE32 hthread, DWORD sel,
582 LPLDT_ENTRY ldtent)
584 ldt_entry ldtentry;
586 LDT_GetEntry(SELECTOR_TO_ENTRY(sel),&ldtentry);
587 ldtent->BaseLow = ldtentry.base & 0x0000ffff;
588 ldtent->HighWord.Bits.BaseMid = (ldtentry.base & 0x00ff0000) >> 16;
589 ldtent->HighWord.Bits.BaseHi = (ldtentry.base & 0xff000000) >> 24;
590 ldtent->LimitLow = ldtentry.limit & 0x0000ffff;
591 ldtent->HighWord.Bits.LimitHi = (ldtentry.limit & 0x00ff0000) >> 16;
592 ldtent->HighWord.Bits.Dpl = 3;
593 ldtent->HighWord.Bits.Sys = 0;
594 ldtent->HighWord.Bits.Pres = 1;
595 ldtent->HighWord.Bits.Type = 0x10|(ldtentry.type << 2);
596 if (ldtentry.read_only)
597 ldtent->HighWord.Bits.Type|=0x2;
598 ldtent->HighWord.Bits.Granularity = ldtentry.limit_in_pages;
599 ldtent->HighWord.Bits.Default_Big = ldtentry.seg_32bit;
600 return TRUE;
604 /**********************************************************************
605 * SMapLS* (KERNEL32)
606 * These functions map linear pointers at [EBP+xxx] to segmented pointers
607 * and return them.
608 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
609 * unravel them at SUnMapLS. We just store the segmented pointer there.
611 static void
612 x_SMapLS_IP_EBP_x(CONTEXT *context,int argoff) {
613 DWORD val,ptr;
615 val =*(DWORD*)(EBP_reg(context)+argoff);
616 if (val<0x10000) {
617 ptr=val;
618 *(DWORD*)(EBP_reg(context)+argoff) = 0;
619 } else {
620 ptr = MapLS((LPVOID)val);
621 *(DWORD*)(EBP_reg(context)+argoff) = ptr;
623 EAX_reg(context) = ptr;
626 REGS_ENTRYPOINT(SMapLS_IP_EBP_8) {x_SMapLS_IP_EBP_x(context,8);}
627 REGS_ENTRYPOINT(SMapLS_IP_EBP_12) {x_SMapLS_IP_EBP_x(context,12);}
628 REGS_ENTRYPOINT(SMapLS_IP_EBP_16) {x_SMapLS_IP_EBP_x(context,16);}
629 REGS_ENTRYPOINT(SMapLS_IP_EBP_20) {x_SMapLS_IP_EBP_x(context,20);}
630 REGS_ENTRYPOINT(SMapLS_IP_EBP_24) {x_SMapLS_IP_EBP_x(context,24);}
631 REGS_ENTRYPOINT(SMapLS_IP_EBP_28) {x_SMapLS_IP_EBP_x(context,28);}
632 REGS_ENTRYPOINT(SMapLS_IP_EBP_32) {x_SMapLS_IP_EBP_x(context,32);}
633 REGS_ENTRYPOINT(SMapLS_IP_EBP_36) {x_SMapLS_IP_EBP_x(context,36);}
634 REGS_ENTRYPOINT(SMapLS_IP_EBP_40) {x_SMapLS_IP_EBP_x(context,40);}
636 REGS_ENTRYPOINT(SMapLS)
638 if (EAX_reg(context)>=0x10000) {
639 EAX_reg(context) = MapLS((LPVOID)EAX_reg(context));
640 EDX_reg(context) = EAX_reg(context);
641 } else {
642 EDX_reg(context) = 0;
646 REGS_ENTRYPOINT(SUnMapLS)
648 if (EAX_reg(context)>=0x10000)
649 UnMapLS((SEGPTR)EAX_reg(context));
652 static void
653 x_SUnMapLS_IP_EBP_x(CONTEXT *context,int argoff) {
654 if (*(DWORD*)(EBP_reg(context)+argoff))
655 UnMapLS(*(DWORD*)(EBP_reg(context)+argoff));
656 *(DWORD*)(EBP_reg(context)+argoff)=0;
658 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_8) { x_SUnMapLS_IP_EBP_x(context,12); }
659 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_12) { x_SUnMapLS_IP_EBP_x(context,12); }
660 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_16) { x_SUnMapLS_IP_EBP_x(context,16); }
661 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_20) { x_SUnMapLS_IP_EBP_x(context,20); }
662 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_24) { x_SUnMapLS_IP_EBP_x(context,24); }
663 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_28) { x_SUnMapLS_IP_EBP_x(context,28); }
664 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_32) { x_SUnMapLS_IP_EBP_x(context,32); }
665 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_36) { x_SUnMapLS_IP_EBP_x(context,36); }
666 REGS_ENTRYPOINT(SUnMapLS_IP_EBP_40) { x_SUnMapLS_IP_EBP_x(context,40); }
668 /**********************************************************************
669 * AllocMappedBuffer (KERNEL32.38)
671 * This is a undocumented KERNEL32 function that
672 * SMapLS's a GlobalAlloc'ed buffer.
674 * Input: EDI register: size of buffer to allocate
675 * Output: EDI register: pointer to buffer
677 * Note: The buffer is preceeded by 8 bytes:
678 * ...
679 * edi+0 buffer
680 * edi-4 SEGPTR to buffer
681 * edi-8 some magic Win95 needs for SUnMapLS
682 * (we use it for the memory handle)
684 * The SEGPTR is used by the caller!
687 REGS_ENTRYPOINT(AllocMappedBuffer)
689 HGLOBAL32 handle = GlobalAlloc32(0, EDI_reg(context) + 8);
690 DWORD *buffer = (DWORD *)GlobalLock32(handle);
691 SEGPTR ptr = 0;
693 if (buffer)
694 if (!(ptr = MapLS(buffer + 2)))
696 GlobalUnlock32(handle);
697 GlobalFree32(handle);
700 if (!ptr)
701 EAX_reg(context) = EDI_reg(context) = 0;
702 else
704 buffer[0] = handle;
705 buffer[1] = ptr;
707 EAX_reg(context) = (DWORD) ptr;
708 EDI_reg(context) = (DWORD)(buffer + 2);
712 /**********************************************************************
713 * FreeMappedBuffer (KERNEL32.39)
715 * Free a buffer allocated by AllocMappedBuffer
717 * Input: EDI register: pointer to buffer
720 REGS_ENTRYPOINT(FreeMappedBuffer)
722 if (EDI_reg(context))
724 DWORD *buffer = (DWORD *)EDI_reg(context) - 2;
726 UnMapLS(buffer[1]);
728 GlobalUnlock32(buffer[0]);
729 GlobalFree32(buffer[0]);
733 /**********************************************************************
734 * WOWGetVDMPointer (KERNEL32.55)
735 * Get linear from segmented pointer. (MSDN lib)
737 LPVOID WINAPI WOWGetVDMPointer(DWORD vp,DWORD nrofbytes,BOOL32 protected)
739 /* FIXME: add size check too */
740 if (protected)
741 return PTR_SEG_TO_LIN(vp);
742 else
743 return DOSMEM_MapRealToLinear(vp);
746 /**********************************************************************
747 * GetVDMPointer32W (KERNEL.516)
749 LPVOID WINAPI GetVDMPointer32W(DWORD vp,DWORD mode)
751 return WOWGetVDMPointer(vp,0,mode);
754 /**********************************************************************
755 * WOWGetVDMPointerFix (KERNEL32.55)
756 * Dito, but fix heapsegment (MSDN lib)
758 LPVOID WINAPI WOWGetVDMPointerFix(DWORD vp,DWORD nrofbytes,BOOL32 protected)
760 /* FIXME: fix heapsegment */
761 return WOWGetVDMPointer(vp,nrofbytes,protected);
764 /**********************************************************************
765 * WOWGetVDMPointerUnFix (KERNEL32.56)
767 void WINAPI WOWGetVDMPointerUnfix(DWORD vp)
769 /* FIXME: unfix heapsegment */
772 /***********************************************************************
773 * UTSelectorOffsetToLinear (WIN32S16.48)
775 * rough guesswork, but seems to work (I had no "reasonable" docu)
777 LPVOID WINAPI UTSelectorOffsetToLinear(SEGPTR sptr)
779 return PTR_SEG_TO_LIN(sptr);
782 /***********************************************************************
783 * UTLinearToSelectorOffset (WIN32S16.49)
785 * FIXME: I don't know if that's the right way to do linear -> segmented
787 SEGPTR WINAPI UTLinearToSelectorOffset(LPVOID lptr)
789 return (SEGPTR)lptr;