4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_MMAN_H
15 # include <sys/mman.h>
19 #include "wine/winbase16.h"
20 #include "wine/port.h"
23 #include "selectors.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(dosmem);
30 DECLARE_DEBUG_CHANNEL(selector);
32 WORD DOSMEM_0000H; /* segment at 0:0 */
33 WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
34 WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
36 DWORD DOSMEM_CollateTable;
38 /* use 2 low bits of 'size' for the housekeeping */
40 #define DM_BLOCK_DEBUG 0xABE00000
41 #define DM_BLOCK_TERMINAL 0x00000001
42 #define DM_BLOCK_FREE 0x00000002
43 #define DM_BLOCK_MASK 0x001FFFFC
46 #define __DOSMEM_DEBUG__
58 #define NEXT_BLOCK(block) \
59 (dosmem_entry*)(((char*)(block)) + \
60 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
62 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
63 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
66 static char *DOSMEM_dosmem;
67 /* DOS system base (for interrupt vector table and BIOS data area)
68 * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
69 * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
70 * and only relocated to NULL when absolutely necessary */
71 static char *DOSMEM_sysmem;
73 /* various real-mode code stubs */
79 /***********************************************************************
82 * Gets the virtual DOS memory base (interrupt table).
84 char *DOSMEM_SystemBase(void)
89 /***********************************************************************
92 * Gets the DOS memory base.
94 char *DOSMEM_MemoryBase(void)
99 /***********************************************************************
102 * Gets the DOS memory top.
104 static char *DOSMEM_MemoryTop(void)
106 return DOSMEM_dosmem+0x9FFFC; /* 640K */
109 /***********************************************************************
112 * Gets the DOS memory info block.
114 static dosmem_info *DOSMEM_InfoBlock(void)
116 return (dosmem_info*)(DOSMEM_dosmem+0x10000); /* 64K */
119 /***********************************************************************
122 * Gets the DOS memory root block.
124 static dosmem_entry *DOSMEM_RootBlock(void)
126 /* first block has to be paragraph-aligned */
127 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
128 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
131 /***********************************************************************
132 * DOSMEM_FillIsrTable
134 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
137 * Linux normally only traps INTs performed from or destined to BIOSSEG
138 * for us to handle, if the int_revectored table is empty. Filling the
139 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
140 * to hook interrupts, as well as use their familiar retf tricks to call
141 * them, AND let Wine handle any unhooked interrupts transparently.
143 static void DOSMEM_FillIsrTable(void)
145 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
148 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
151 static void DOSMEM_MakeIsrStubs(void)
153 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
156 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
159 /***********************************************************************
162 * Allocate the global DPMI RMCB wrapper.
164 static void DOSMEM_InitDPMI(void)
168 static const char wrap_code[]={
169 0xCD,0x31, /* int $0x31 */
173 static const char enter_xms[]=
175 /* XMS hookable entry point */
176 0xEB,0x03, /* jmp entry */
177 0x90,0x90,0x90, /* nop;nop;nop */
179 /* real entry point */
180 /* for simplicity, we'll just use the same hook as DPMI below */
181 0xCD,0x31, /* int $0x31 */
185 static const char enter_pm[]=
187 0x50, /* pushw %ax */
188 0x52, /* pushw %dx */
189 0x55, /* pushw %bp */
190 0x89,0xE5, /* movw %sp,%bp */
192 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
193 /* just call int 31 here to get into protected mode... */
194 /* it'll check whether it was called from dpmi_seg... */
195 0xCD,0x31, /* int $0x31 */
196 /* we are now in the context of a 16-bit relay call */
197 /* need to fixup our stack;
198 * 16-bit relay return address will be lost, but we won't worry quite yet */
199 0x8E,0xD0, /* movw %ax,%ss */
200 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
202 0x89,0x56,0x08, /* movw %dx,8(%bp) */
209 ptr = DOSMEM_GetBlock( sizeof(wrap_code), &DOSMEM_wrap_seg );
210 memcpy( ptr, wrap_code, sizeof(wrap_code) );
211 ptr = DOSMEM_GetBlock( sizeof(enter_xms), &DOSMEM_xms_seg );
212 memcpy( ptr, enter_xms, sizeof(enter_xms) );
213 ptr = DOSMEM_GetBlock( sizeof(enter_pm), &DOSMEM_dpmi_seg );
214 memcpy( ptr, enter_pm, sizeof(enter_pm) );
215 DOSMEM_dpmi_sel = SELECTOR_AllocBlock( ptr, sizeof(enter_pm), WINE_LDT_FLAGS_CODE );
218 BIOSDATA * DOSMEM_BiosData()
220 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
223 BYTE * DOSMEM_BiosSys()
225 return DOSMEM_dosmem+0xf0000;
228 struct _DOS_LISTOFLISTS * DOSMEM_LOL()
230 return (struct _DOS_LISTOFLISTS *)DOSMEM_MapRealToLinear
231 (MAKESEGPTR(HIWORD(DOS_LOLSeg),0));
234 /***********************************************************************
235 * DOSMEM_FillBiosSegments
237 * Fill the BIOS data segment with dummy values.
239 static void DOSMEM_FillBiosSegments(void)
241 BYTE *pBiosSys = DOSMEM_BiosSys();
242 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
243 BIOSDATA *pBiosData = DOSMEM_BiosData();
245 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
246 VIDEOFUNCTIONALITY *pVidFunc = (VIDEOFUNCTIONALITY *)(pBiosSys+0xe000);
247 VIDEOSTATE *pVidState = (VIDEOSTATE *)(pBiosSys+0xe010);
250 /* Clear all unused values */
251 memset( pBiosData, 0, sizeof(*pBiosData) );
252 memset( pVidFunc, 0, sizeof(*pVidFunc ) );
253 memset( pVidState, 0, sizeof(*pVidState) );
255 /* FIXME: should check the number of configured drives and ports */
257 pBiosData->Com1Addr = 0x3f8;
258 pBiosData->Com2Addr = 0x2f8;
259 pBiosData->Lpt1Addr = 0x378;
260 pBiosData->Lpt2Addr = 0x278;
261 pBiosData->InstalledHardware = 0x5463;
262 pBiosData->MemSize = 640;
263 pBiosData->NextKbdCharPtr = 0x1e;
264 pBiosData->FirstKbdCharPtr = 0x1e;
265 pBiosData->VideoMode = 3;
266 pBiosData->VideoColumns = 80;
267 pBiosData->VideoPageSize = 80 * 25 * 2;
268 pBiosData->VideoPageStartAddr = 0xb800;
269 pBiosData->VideoCtrlAddr = 0x3d4;
270 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
271 pBiosData->NbHardDisks = 2;
272 pBiosData->KbdBufferStart = 0x1e;
273 pBiosData->KbdBufferEnd = 0x3e;
274 pBiosData->RowsOnScreenMinus1 = 23;
275 pBiosData->BytesPerChar = 0x10;
276 pBiosData->ModeOptions = 0x64;
277 pBiosData->FeatureBitsSwitches = 0xf9;
278 pBiosData->VGASettings = 0x51;
279 pBiosData->DisplayCombination = 0x08;
280 pBiosData->DiskDataRate = 0;
282 /* fill ROM configuration table (values from Award) */
283 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
284 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
285 *(pBiosROMTable+0x2) = 0xfc; /* model */
286 *(pBiosROMTable+0x3) = 0x01; /* submodel */
287 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
288 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
289 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
290 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
291 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
292 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
295 for (i = 0; i < 7; i++)
296 pVidFunc->ModeSupport[i] = 0xff;
298 pVidFunc->ScanlineSupport = 7;
299 pVidFunc->NumberCharBlocks = 0;
300 pVidFunc->ActiveCharBlocks = 0;
301 pVidFunc->MiscFlags = 0x8ff;
302 pVidFunc->SavePointerFlags = 0x3f;
304 pVidState->StaticFuncTable = 0xf000e000; /* FIXME: always real mode ? */
305 pVidState->VideoMode = pBiosData->VideoMode; /* needs updates! */
306 pVidState->NumberColumns = pBiosData->VideoColumns; /* needs updates! */
307 pVidState->RegenBufLen = 0;
308 pVidState->RegenBufAddr = 0;
310 for (i = 0; i < 8; i++)
311 pVidState->CursorPos[i] = 0;
313 pVidState->CursorType = 0x0a0b; /* start/end line */
314 pVidState->ActivePage = 0;
315 pVidState->CRTCPort = 0x3da;
316 pVidState->Port3x8 = 0;
317 pVidState->Port3x9 = 0;
318 pVidState->NumberRows = 23; /* number of rows - 1 */
319 pVidState->BytesPerChar = 0x10;
320 pVidState->DCCActive = pBiosData->DisplayCombination;
321 pVidState->DCCAlternate = 0;
322 pVidState->NumberColors = 16;
323 pVidState->NumberPages = 1;
324 pVidState->NumberScanlines = 3; /* (0,1,2,3) = (200,350,400,480) */
325 pVidState->CharBlockPrimary = 0;
326 pVidState->CharBlockSecondary = 0;
327 pVidState->MiscFlags =
328 (pBiosData->VGASettings & 0x0f)
329 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
330 pVidState->NonVGASupport = 0;
331 pVidState->VideoMem = (pBiosData->ModeOptions & 0x60 >> 5);
332 pVidState->SavePointerState = 0;
333 pVidState->DisplayStatus = 4;
335 /* BIOS date string */
336 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
339 *(pBiosSys+0xfffe) = 0xfc;
342 /***********************************************************************
343 * DOSMEM_InitCollateTable
345 * Initialises the collate table (character sorting, language dependent)
347 static void DOSMEM_InitCollateTable()
353 x = GlobalDOSAlloc16(258);
354 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
355 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
358 for ( i = 0; i < 0x100; i++) *tbl++ = i;
361 /***********************************************************************
362 * DOSMEM_InitErrorTable
364 * Initialises the error tables (DOS 5+)
366 static void DOSMEM_InitErrorTable()
368 #if 0 /* no longer used */
372 /* We will use a snippet of real mode code that calls */
373 /* a WINE-only interrupt to handle moving the requested */
374 /* message into the buffer... */
376 /* FIXME - There is still something wrong... */
378 /* FIXME - Find hex values for opcodes...
380 (On call, AX contains message number
381 DI contains 'offset' (??)
382 Resturn, ES:DI points to counted string )
386 MOV AX, (arbitrary subfunction number)
387 INT (WINE-only interrupt)
394 const int buffer = 80;
395 const int SIZE_TO_ALLOCATE = code + buffer;
397 /* FIXME - Complete rewrite of the table system to save */
398 /* precious DOS space. Now, we return the 0001:???? as */
399 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
400 /* as a special case and programs will use the alternate */
401 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
402 /* 0x08) which lets us have a smaller memory footprint anyway. */
404 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
406 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
407 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
409 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
411 memset(call, 0, SIZE_TO_ALLOCATE);
413 /* Fixme - Copy assembly into buffer here */
416 /***********************************************************************
419 * Initialises the DOS memory structures.
421 static void DOSMEM_InitMemory(void)
423 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
424 * 1000:0000 and ends at 9FFF:FFEF. */
426 dosmem_info* info_block = DOSMEM_InfoBlock();
427 dosmem_entry* root_block = DOSMEM_RootBlock();
430 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
432 info_block->blocks = 0;
433 info_block->free = root_block->size;
435 dm = NEXT_BLOCK(root_block);
436 dm->size = DM_BLOCK_TERMINAL;
437 root_block->size |= DM_BLOCK_FREE
438 #ifdef __DOSMEM_DEBUG__
444 /**********************************************************************
447 * Setup the first megabyte for DOS memory access
449 static void setup_dos_mem( int dos_init )
452 int page_size = getpagesize();
453 void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
454 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
455 if (addr == (void *)page_size) /* we got what we wanted */
457 /* now map from address 0 */
458 addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
461 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
465 /* inform the memory manager that there is a mapping here */
466 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
468 /* protect the first 64K to catch NULL pointers */
471 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
472 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
473 sys_offset += 0xf0000;
478 ERR("Cannot use first megabyte for DOS address space, please report\n" );
479 if (dos_init) ExitProcess(1);
480 /* allocate the DOS area somewhere else */
481 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
484 ERR( "Cannot allocate DOS memory\n" );
488 DOSMEM_dosmem = addr;
489 DOSMEM_sysmem = (char*)addr + sys_offset;
493 /***********************************************************************
496 * Create the dos memory segments, and store them into the KERNEL
499 BOOL DOSMEM_Init(BOOL dos_init)
501 static int already_done, already_mapped;
505 setup_dos_mem( dos_init );
507 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
508 0x10000, 0, WINE_LDT_FLAGS_DATA );
509 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
510 0x100, 0, WINE_LDT_FLAGS_DATA );
511 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
512 0x10000, 0, WINE_LDT_FLAGS_DATA );
513 DOSMEM_FillBiosSegments();
514 DOSMEM_FillIsrTable();
516 DOSMEM_InitCollateTable();
517 DOSMEM_InitErrorTable();
519 DOSDEV_InstallDOSDevices();
522 else if (dos_init && !already_mapped)
526 ERR( "Needs access to the first megabyte for DOS mode\n" );
529 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
530 " NULL pointer accesses will no longer be caught.\n" );
531 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
532 /* copy the BIOS and ISR area down */
533 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
534 DOSMEM_sysmem = DOSMEM_dosmem;
535 SetSelectorBase( DOSMEM_0000H, 0 );
536 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
537 /* we may now need the actual interrupt stubs, and since we've just moved the
538 * interrupt vector table away, we can fill the area with stubs instead... */
539 DOSMEM_MakeIsrStubs();
546 /***********************************************************************
549 * Increment the BIOS tick counter. Called by timer signal handler.
551 void DOSMEM_Tick( WORD timer )
553 BIOSDATA *pBiosData = DOSMEM_BiosData();
554 if (pBiosData) pBiosData->Ticks++;
557 /***********************************************************************
560 * Carve a chunk of the DOS memory block (without selector).
562 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
566 dosmem_info *info_block = DOSMEM_InfoBlock();
568 #ifdef __DOSMEM_DEBUG_
569 dosmem_entry *prev = NULL;
572 if( size > info_block->free ) return NULL;
573 dm = DOSMEM_RootBlock();
575 while (dm && dm->size != DM_BLOCK_TERMINAL)
577 #ifdef __DOSMEM_DEBUG__
578 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
580 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
585 if( dm->size & DM_BLOCK_FREE )
587 dosmem_entry *next = NEXT_BLOCK(dm);
589 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
591 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
592 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
593 next = NEXT_BLOCK(dm);
596 blocksize = dm->size & DM_BLOCK_MASK;
597 if( blocksize >= size )
599 block = ((char*)dm) + sizeof(dosmem_entry);
600 if( blocksize - size > 0x20 )
602 /* split dm so that the next one stays
603 * paragraph-aligned (and dm loses free bit) */
605 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
606 sizeof(dosmem_entry));
607 next = (dosmem_entry*)(((char*)dm) +
608 sizeof(dosmem_entry) + dm->size);
609 next->size = (blocksize - (dm->size +
610 sizeof(dosmem_entry))) | DM_BLOCK_FREE
611 #ifdef __DOSMEM_DEBUG__
615 } else dm->size &= DM_BLOCK_MASK;
617 info_block->blocks++;
618 info_block->free -= dm->size;
619 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
620 #ifdef __DOSMEM_DEBUG__
621 dm->size |= DM_BLOCK_DEBUG;
627 else dm = NEXT_BLOCK(dm);
629 return (LPVOID)block;
632 /***********************************************************************
635 BOOL DOSMEM_FreeBlock(void* ptr)
637 dosmem_info *info_block = DOSMEM_InfoBlock();
639 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
640 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
641 - DOSMEM_dosmem) & 0xf) )
643 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
645 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
646 #ifdef __DOSMEM_DEBUG__
647 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
651 info_block->blocks--;
652 info_block->free += dm->size;
654 dm->size |= DM_BLOCK_FREE;
661 /***********************************************************************
664 LPVOID DOSMEM_ResizeBlock(void* ptr, UINT size, UINT16* pseg)
667 dosmem_info *info_block = DOSMEM_InfoBlock();
669 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
670 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
671 - DOSMEM_dosmem) & 0xf) )
673 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
675 if( pseg ) *pseg = ((char*)ptr - DOSMEM_dosmem) >> 4;
677 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
680 dosmem_entry *next = NEXT_BLOCK(dm);
681 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
683 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
685 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
686 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
687 next = NEXT_BLOCK(dm);
690 blocksize = dm->size & DM_BLOCK_MASK;
691 if (blocksize >= size)
693 block = ((char*)dm) + sizeof(dosmem_entry);
694 if( blocksize - size > 0x20 )
696 /* split dm so that the next one stays
697 * paragraph-aligned (and next gains free bit) */
699 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
700 sizeof(dosmem_entry));
701 next = (dosmem_entry*)(((char*)dm) +
702 sizeof(dosmem_entry) + dm->size);
703 next->size = (blocksize - (dm->size +
704 sizeof(dosmem_entry))) | DM_BLOCK_FREE
706 } else dm->size &= DM_BLOCK_MASK;
708 info_block->free += orgsize - dm->size;
710 /* the collapse didn't help, try getting a new block */
711 block = DOSMEM_GetBlock(size, pseg);
713 /* we got one, copy the old data there (we do need to, right?) */
714 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
715 (size<orgsize) ? size : orgsize);
717 info_block->blocks--;
718 info_block->free += dm->size;
720 dm->size |= DM_BLOCK_FREE;
722 /* and Bill Gates said 640K should be enough for everyone... */
724 /* need to split original and collapsed blocks apart again,
725 * and free the collapsed blocks again, before exiting */
726 if( blocksize - orgsize > 0x20 )
728 /* split dm so that the next one stays
729 * paragraph-aligned (and next gains free bit) */
731 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
732 sizeof(dosmem_entry));
733 next = (dosmem_entry*)(((char*)dm) +
734 sizeof(dosmem_entry) + dm->size);
735 next->size = (blocksize - (dm->size +
736 sizeof(dosmem_entry))) | DM_BLOCK_FREE
738 } else dm->size &= DM_BLOCK_MASK;
743 return (LPVOID)block;
747 /***********************************************************************
750 UINT DOSMEM_Available(void)
752 UINT blocksize, available = 0;
755 dm = DOSMEM_RootBlock();
757 while (dm && dm->size != DM_BLOCK_TERMINAL)
759 #ifdef __DOSMEM_DEBUG__
760 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
762 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
767 if( dm->size & DM_BLOCK_FREE )
769 dosmem_entry *next = NEXT_BLOCK(dm);
771 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
773 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
774 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
775 next = NEXT_BLOCK(dm);
778 blocksize = dm->size & DM_BLOCK_MASK;
779 if ( blocksize > available ) available = blocksize;
782 else dm = NEXT_BLOCK(dm);
788 /***********************************************************************
789 * DOSMEM_MapLinearToDos
791 * Linear address to the DOS address space.
793 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
795 if (((char*)ptr >= DOSMEM_dosmem) &&
796 ((char*)ptr < DOSMEM_dosmem + 0x100000))
797 return (UINT)ptr - (UINT)DOSMEM_dosmem;
802 /***********************************************************************
803 * DOSMEM_MapDosToLinear
805 * DOS linear address to the linear address space.
807 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
809 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
814 /***********************************************************************
815 * DOSMEM_MapRealToLinear
817 * Real mode DOS address into a linear pointer
819 LPVOID DOSMEM_MapRealToLinear(DWORD x)
823 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
824 TRACE_(selector)("(0x%08lx) returns 0x%p.\n", x, lin );
828 /***********************************************************************
829 * DOSMEM_AllocSelector
831 * Allocates a protected mode selector for a realmode segment.
833 WORD DOSMEM_AllocSelector(WORD realsel)
835 HMODULE16 hModule = GetModuleHandle16("KERNEL");
838 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
839 hModule, WINE_LDT_FLAGS_DATA );
840 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);