Avoid a failure under Win9x.
[wine] / dlls / kernel / dosmem.c
1 /*
2  * DOS memory emulation
3  *
4  * Copyright 1995 Alexandre Julliard
5  * Copyright 1996 Marcus Meissner
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
32 #endif
33
34 #include <time.h>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 #endif
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wine/winbase16.h"
42
43 #include "kernel_private.h"
44 #include "toolhelp.h"
45 #include "miscemu.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
49 WINE_DECLARE_DEBUG_CHANNEL(selector);
50
51 WORD DOSMEM_0000H;        /* segment at 0:0 */
52 WORD DOSMEM_BiosDataSeg;  /* BIOS data segment at 0x40:0 */
53 WORD DOSMEM_BiosSysSeg;   /* BIOS ROM segment at 0xf000:0 */
54
55 /* use 2 low bits of 'size' for the housekeeping */
56
57 #define DM_BLOCK_DEBUG          0xABE00000
58 #define DM_BLOCK_TERMINAL       0x00000001
59 #define DM_BLOCK_FREE           0x00000002
60 #define DM_BLOCK_MASK           0x001FFFFC
61
62 /*
63 #define __DOSMEM_DEBUG__
64  */
65
66 typedef struct {
67    unsigned     size;
68 } dosmem_entry;
69
70 typedef struct {
71   unsigned      blocks;
72   unsigned      free;
73 } dosmem_info;
74
75 #define NEXT_BLOCK(block) \
76         (dosmem_entry*)(((char*)(block)) + \
77          sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
78
79 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
80 #define VM_STUB_SEGMENT 0xf000         /* BIOS segment */
81
82 /* DOS memory base */
83 static char *DOSMEM_dosmem;
84 /* DOS system base (for interrupt vector table and BIOS data area)
85  * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
86  * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
87  * and only relocated to NULL when absolutely necessary */
88 static char *DOSMEM_sysmem;
89
90 /* Start of DOS conventional memory */
91 static char *DOSMEM_membase;
92
93 static void DOSMEM_InitMemory(void);
94
95 /***********************************************************************
96  *           DOSMEM_MemoryTop
97  *
98  * Gets the DOS memory top.
99  */
100 static char *DOSMEM_MemoryTop(void)
101 {
102     return DOSMEM_dosmem+0x9FFFC; /* 640K */
103 }
104
105 /***********************************************************************
106  *           DOSMEM_InfoBlock
107  *
108  * Gets the DOS memory info block.
109  */
110 static dosmem_info *DOSMEM_InfoBlock(void)
111 {
112     if (!DOSMEM_membase)
113     {
114         DWORD         reserve;
115
116         /*
117          * Reserve either:
118          * - lowest 64k for NULL pointer catching (Win16)
119          * - lowest 1k for interrupt handlers and 
120          *   another 0.5k for BIOS, DOS and intra-application
121          *   areas (DOS)
122          */
123         if (DOSMEM_dosmem != DOSMEM_sysmem)
124             reserve = 0x10000; /* 64k */
125         else
126             reserve = 0x600; /* 1.5k */
127
128         /*
129          * Round to paragraph boundary in order to make 
130          * sure the alignment is correct.
131          */
132         reserve = ((reserve + 15) >> 4) << 4;
133
134         /*
135          * Set DOS memory base and initialize conventional memory.
136          */
137         DOSMEM_membase = DOSMEM_dosmem + reserve;
138         DOSMEM_InitMemory();
139     }
140
141     return (dosmem_info*)DOSMEM_membase;
142 }
143
144 /***********************************************************************
145  *           DOSMEM_RootBlock
146  *
147  * Gets the DOS memory root block.
148  */
149 static dosmem_entry *DOSMEM_RootBlock(void)
150 {
151     /* first block has to be paragraph-aligned */
152     return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
153                            ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
154 }
155
156 /***********************************************************************
157  *           DOSMEM_FillIsrTable
158  *
159  * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
160  *
161  * NOTES:
162  * Linux normally only traps INTs performed from or destined to BIOSSEG
163  * for us to handle, if the int_revectored table is empty. Filling the
164  * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
165  * to hook interrupts, as well as use their familiar retf tricks to call
166  * them, AND let Wine handle any unhooked interrupts transparently.
167  */
168 static void DOSMEM_FillIsrTable(void)
169 {
170     SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
171     int x;
172
173     for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
174 }
175
176 static void DOSMEM_MakeIsrStubs(void)
177 {
178     DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
179     int x;
180
181     for (x=0; x<256; x++) stub[x]=VM_STUB(x);
182 }
183
184 static BIOSDATA * DOSMEM_BiosData(void)
185 {
186     return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
187 }
188
189 /**********************************************************************
190  *          DOSMEM_GetTicksSinceMidnight
191  *
192  * Return number of clock ticks since midnight.
193  */
194 static DWORD DOSMEM_GetTicksSinceMidnight(void)
195 {
196     struct tm *bdtime;
197     struct timeval tvs;
198     time_t seconds;
199
200     /* This should give us the (approximately) correct
201      * 18.206 clock ticks per second since midnight.
202      */
203     gettimeofday( &tvs, NULL );
204     seconds = tvs.tv_sec;
205     bdtime = localtime( &seconds );
206     return (((bdtime->tm_hour * 3600 + bdtime->tm_min * 60 +
207               bdtime->tm_sec) * 18206) / 1000) +
208                   (tvs.tv_usec / 54927);
209 }
210
211 /***********************************************************************
212  *           DOSMEM_FillBiosSegments
213  *
214  * Fill the BIOS data segment with dummy values.
215  */
216 static void DOSMEM_FillBiosSegments(void)
217 {
218     BYTE *pBiosSys = DOSMEM_dosmem + 0xf0000;
219     BYTE *pBiosROMTable = pBiosSys+0xe6f5;
220     BIOSDATA *pBiosData = DOSMEM_BiosData();
221
222       /* Clear all unused values */
223     memset( pBiosData, 0, sizeof(*pBiosData) );
224
225     /* FIXME: should check the number of configured drives and ports */
226     pBiosData->Com1Addr             = 0x3f8;
227     pBiosData->Com2Addr             = 0x2f8;
228     pBiosData->Lpt1Addr             = 0x378;
229     pBiosData->Lpt2Addr             = 0x278;
230     pBiosData->InstalledHardware    = 0x5463;
231     pBiosData->MemSize              = 640;
232     pBiosData->NextKbdCharPtr       = 0x1e;
233     pBiosData->FirstKbdCharPtr      = 0x1e;
234     pBiosData->VideoMode            = 3;
235     pBiosData->VideoColumns         = 80;
236     pBiosData->VideoPageSize        = 80 * 25 * 2;
237     pBiosData->VideoPageStartAddr   = 0xb800;
238     pBiosData->VideoCtrlAddr        = 0x3d4;
239     pBiosData->Ticks                = DOSMEM_GetTicksSinceMidnight();
240     pBiosData->NbHardDisks          = 2;
241     pBiosData->KbdBufferStart       = 0x1e;
242     pBiosData->KbdBufferEnd         = 0x3e;
243     pBiosData->RowsOnScreenMinus1   = 24;
244     pBiosData->BytesPerChar         = 0x10;
245     pBiosData->ModeOptions          = 0x64;
246     pBiosData->FeatureBitsSwitches  = 0xf9;
247     pBiosData->VGASettings          = 0x51;
248     pBiosData->DisplayCombination   = 0x08;
249     pBiosData->DiskDataRate         = 0;
250
251     /* fill ROM configuration table (values from Award) */
252     *(pBiosROMTable+0x0)        = 0x08; /* number of bytes following LO */
253     *(pBiosROMTable+0x1)        = 0x00; /* number of bytes following HI */
254     *(pBiosROMTable+0x2)        = 0xfc; /* model */
255     *(pBiosROMTable+0x3)        = 0x01; /* submodel */
256     *(pBiosROMTable+0x4)        = 0x00; /* BIOS revision */
257     *(pBiosROMTable+0x5)        = 0x74; /* feature byte 1 */
258     *(pBiosROMTable+0x6)        = 0x00; /* feature byte 2 */
259     *(pBiosROMTable+0x7)        = 0x00; /* feature byte 3 */
260     *(pBiosROMTable+0x8)        = 0x00; /* feature byte 4 */
261     *(pBiosROMTable+0x9)        = 0x00; /* feature byte 5 */
262
263     /* BIOS date string */
264     strcpy((char *)pBiosSys+0xfff5, "13/01/99");
265
266     /* BIOS ID */
267     *(pBiosSys+0xfffe) = 0xfc;
268
269     /* Reboot vector (f000:fff0 or ffff:0000) */
270     *(DWORD*)(pBiosSys + 0xfff0) = VM_STUB(0x19);
271 }
272
273 /***********************************************************************
274  *           DOSMEM_InitMemory
275  *
276  * Initialises the DOS memory structures.
277  */
278 static void DOSMEM_InitMemory(void)
279 {
280     dosmem_info*        info_block = DOSMEM_InfoBlock();
281     dosmem_entry*       root_block = DOSMEM_RootBlock();
282     dosmem_entry*       dm;
283
284     root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
285
286     info_block->blocks = 0;
287     info_block->free = root_block->size;
288
289     dm = NEXT_BLOCK(root_block);
290     dm->size = DM_BLOCK_TERMINAL;
291     root_block->size |= DM_BLOCK_FREE
292 #ifdef __DOSMEM_DEBUG__
293                      | DM_BLOCK_DEBUG
294 #endif
295                      ;
296
297     TRACE( "DOS conventional memory initialized, %d bytes free.\n", 
298            DOSMEM_Available() );
299 }
300
301
302 /**********************************************************************
303  *              setup_dos_mem
304  *
305  * Setup the first megabyte for DOS memory access
306  */
307 static void setup_dos_mem( int dos_init )
308 {
309     int sys_offset = 0;
310     int page_size = getpagesize();
311     void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
312                                  PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
313     if (addr == (void *)page_size)  /* we got what we wanted */
314     {
315         /* now map from address 0 */
316         addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
317         if (addr)
318         {
319             ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
320             ExitProcess(1);
321         }
322
323         /* inform the memory manager that there is a mapping here */
324         VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
325
326         /* protect the first 64K to catch NULL pointers */
327         if (!dos_init)
328         {
329             VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
330             /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
331             sys_offset += 0xf0000;
332         }
333     }
334     else
335     {
336         ERR("Cannot use first megabyte for DOS address space, please report\n" );
337         if (dos_init) ExitProcess(1);
338         /* allocate the DOS area somewhere else */
339         addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
340         if (!addr)
341         {
342             ERR( "Cannot allocate DOS memory\n" );
343             ExitProcess(1);
344         }
345     }
346     DOSMEM_dosmem = addr;
347     DOSMEM_sysmem = (char*)addr + sys_offset;
348 }
349
350
351 /***********************************************************************
352  *           DOSMEM_Init
353  *
354  * Create the dos memory segments, and store them into the KERNEL
355  * exported values.
356  */
357 BOOL DOSMEM_Init(BOOL dos_init)
358 {
359     static int already_done, already_mapped;
360
361     if (!already_done)
362     {
363         setup_dos_mem( dos_init );
364
365         DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
366                                            0x10000, 0, WINE_LDT_FLAGS_DATA );
367         DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
368                                                 0x100, 0, WINE_LDT_FLAGS_DATA );
369         DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
370                                                0x10000, 0, WINE_LDT_FLAGS_DATA );
371         DOSMEM_FillBiosSegments();
372         DOSMEM_FillIsrTable();
373         already_done = 1;
374     }
375     else if (dos_init && !already_mapped)
376     {
377         if (DOSMEM_dosmem)
378         {
379             ERR( "Needs access to the first megabyte for DOS mode\n" );
380             ExitProcess(1);
381         }
382         MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
383                  "         NULL pointer accesses will no longer be caught.\n" );
384         VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
385         /* copy the BIOS and ISR area down */
386         memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
387         DOSMEM_sysmem = DOSMEM_dosmem;
388         SetSelectorBase( DOSMEM_0000H, 0 );
389         SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
390         /* we may now need the actual interrupt stubs, and since we've just moved the
391          * interrupt vector table away, we can fill the area with stubs instead... */
392         DOSMEM_MakeIsrStubs();
393         already_mapped = 1;
394     }
395     return TRUE;
396 }
397
398
399 /***********************************************************************
400  *           DOSMEM_Tick
401  *
402  * Increment the BIOS tick counter. Called by timer signal handler.
403  */
404 void DOSMEM_Tick( WORD timer )
405 {
406     BIOSDATA *pBiosData = DOSMEM_BiosData();
407     if (pBiosData) pBiosData->Ticks++;
408 }
409
410 /***********************************************************************
411  *           DOSMEM_GetBlock
412  *
413  * Carve a chunk of the DOS memory block (without selector).
414  */
415 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
416 {
417    UINT          blocksize;
418    char         *block = NULL;
419    dosmem_info  *info_block = DOSMEM_InfoBlock();
420    dosmem_entry *dm;
421 #ifdef __DOSMEM_DEBUG_
422    dosmem_entry *prev = NULL;
423 #endif
424
425    if( size > info_block->free ) return NULL;
426    dm = DOSMEM_RootBlock();
427
428    while (dm && dm->size != DM_BLOCK_TERMINAL)
429    {
430 #ifdef __DOSMEM_DEBUG__
431        if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
432        {
433             WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
434             return NULL;
435        }
436        prev = dm;
437 #endif
438        if( dm->size & DM_BLOCK_FREE )
439        {
440            dosmem_entry  *next = NEXT_BLOCK(dm);
441
442            while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
443            {
444                dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
445                next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
446                next = NEXT_BLOCK(dm);
447            }
448
449            blocksize = dm->size & DM_BLOCK_MASK;
450            if( blocksize >= size )
451            {
452                block = ((char*)dm) + sizeof(dosmem_entry);
453                if( blocksize - size > 0x20 )
454                {
455                    /* split dm so that the next one stays
456                     * paragraph-aligned (and dm loses free bit) */
457
458                    dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
459                                               sizeof(dosmem_entry));
460                    next = (dosmem_entry*)(((char*)dm) +
461                            sizeof(dosmem_entry) + dm->size);
462                    next->size = (blocksize - (dm->size +
463                            sizeof(dosmem_entry))) | DM_BLOCK_FREE
464 #ifdef __DOSMEM_DEBUG__
465                                                   | DM_BLOCK_DEBUG
466 #endif
467                                                   ;
468                } else dm->size &= DM_BLOCK_MASK;
469
470                info_block->blocks++;
471                info_block->free -= dm->size;
472                if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
473 #ifdef __DOSMEM_DEBUG__
474                dm->size |= DM_BLOCK_DEBUG;
475 #endif
476                break;
477            }
478            dm = next;
479        }
480        else dm = NEXT_BLOCK(dm);
481    }
482    return (LPVOID)block;
483 }
484
485 /***********************************************************************
486  *           DOSMEM_FreeBlock
487  */
488 BOOL DOSMEM_FreeBlock(void* ptr)
489 {
490    dosmem_info  *info_block = DOSMEM_InfoBlock();
491
492    if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
493        ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
494                   - DOSMEM_dosmem) & 0xf) )
495    {
496        dosmem_entry  *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
497
498        if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
499 #ifdef __DOSMEM_DEBUG__
500          && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
501 #endif
502          )
503        {
504              info_block->blocks--;
505              info_block->free += dm->size;
506
507              dm->size |= DM_BLOCK_FREE;
508              return TRUE;
509        }
510    }
511    return FALSE;
512 }
513
514 /***********************************************************************
515  *           DOSMEM_ResizeBlock
516  *
517  * Resize DOS memory block in place. Returns block size or -1 on error.
518  *
519  * If exact is TRUE, returned value is either old or requested block
520  * size. If exact is FALSE, block is expanded even if there is not
521  * enough space for full requested block size.
522  */
523 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
524 {
525    char         *block = NULL;
526    dosmem_info  *info_block = DOSMEM_InfoBlock();
527    dosmem_entry *dm;
528    dosmem_entry *next;
529    UINT blocksize;
530    UINT orgsize;
531
532    if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) ||
533        (ptr >= (void*)DOSMEM_MemoryTop()) ||
534        (((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) )
535      return (UINT)-1;
536
537    dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
538    if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) )
539        return (UINT)-1;
540
541    next = NEXT_BLOCK(dm);
542    orgsize = dm->size & DM_BLOCK_MASK;
543
544    /* collapse free blocks */
545    while( next->size & DM_BLOCK_FREE )
546    {
547        dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
548        next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
549        next = NEXT_BLOCK(dm);
550    }
551
552    blocksize = dm->size & DM_BLOCK_MASK;
553
554    /*
555     * If collapse didn't help we either expand block to maximum
556     * available size (exact == FALSE) or give collapsed blocks
557     * back to free storage (exact == TRUE).
558     */
559    if (blocksize < size)
560        size = exact ? orgsize : blocksize;
561
562    block = ((char*)dm) + sizeof(dosmem_entry);
563    if( blocksize - size > 0x20 )
564    {
565        /*
566         * split dm so that the next one stays
567         * paragraph-aligned (and next gains free bit) 
568         */
569
570        dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
571                    sizeof(dosmem_entry));
572        next = (dosmem_entry*)(((char*)dm) +
573                               sizeof(dosmem_entry) + dm->size);
574        next->size = (blocksize - (dm->size +
575                                   sizeof(dosmem_entry))) | DM_BLOCK_FREE;
576    } 
577    else 
578    {
579        dm->size &= DM_BLOCK_MASK;
580    }
581
582    /*
583     * Adjust available memory if block size changes.
584     */
585    info_block->free += orgsize - dm->size;
586
587    return size;
588 }
589
590 /***********************************************************************
591  *           DOSMEM_Available
592  */
593 UINT DOSMEM_Available(void)
594 {
595    UINT          blocksize, available = 0;
596    dosmem_entry *dm;
597
598    dm = DOSMEM_RootBlock();
599
600    while (dm && dm->size != DM_BLOCK_TERMINAL)
601    {
602 #ifdef __DOSMEM_DEBUG__
603        if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
604        {
605             WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
606             return NULL;
607        }
608        prev = dm;
609 #endif
610        if( dm->size & DM_BLOCK_FREE )
611        {
612            dosmem_entry  *next = NEXT_BLOCK(dm);
613
614            while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
615            {
616                dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
617                next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
618                next = NEXT_BLOCK(dm);
619            }
620
621            blocksize = dm->size & DM_BLOCK_MASK;
622            if ( blocksize > available ) available = blocksize;
623            dm = next;
624        }
625        else dm = NEXT_BLOCK(dm);
626    }
627    return available;
628 }
629
630
631 /***********************************************************************
632  *           DOSMEM_MapLinearToDos
633  *
634  * Linear address to the DOS address space.
635  */
636 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
637 {
638     if (((char*)ptr >= DOSMEM_dosmem) &&
639         ((char*)ptr < DOSMEM_dosmem + 0x100000))
640           return (UINT)ptr - (UINT)DOSMEM_dosmem;
641     return (UINT)ptr;
642 }
643
644
645 /***********************************************************************
646  *           DOSMEM_MapDosToLinear
647  *
648  * DOS linear address to the linear address space.
649  */
650 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
651 {
652     if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
653     return (LPVOID)ptr;
654 }
655
656
657 /***********************************************************************
658  *           DOSMEM_MapRealToLinear
659  *
660  * Real mode DOS address into a linear pointer
661  */
662 LPVOID DOSMEM_MapRealToLinear(DWORD x)
663 {
664    LPVOID       lin;
665
666    lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
667    TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
668    return lin;
669 }
670
671 /***********************************************************************
672  *           DOSMEM_AllocSelector
673  *
674  * Allocates a protected mode selector for a realmode segment.
675  */
676 WORD DOSMEM_AllocSelector(WORD realsel)
677 {
678         HMODULE16 hModule = GetModuleHandle16("KERNEL");
679         WORD    sel;
680
681         sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
682                                 hModule, WINE_LDT_FLAGS_DATA );
683         TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
684         return sel;
685 }