Make COMSPEC point to wcmd.exe, not command.com.
[wine/multimedia.git] / msdos / dosmem.c
blob7cf86f3b5676de99483d3b24777d3461f7235ee1
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
33 #include "winbase.h"
34 #include "wine/winbase16.h"
36 #include "global.h"
37 #include "selectors.h"
38 #include "miscemu.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
42 WINE_DECLARE_DEBUG_CHANNEL(selector);
44 WORD DOSMEM_0000H; /* segment at 0:0 */
45 WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
46 WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
48 DWORD DOSMEM_CollateTable;
50 /* use 2 low bits of 'size' for the housekeeping */
52 #define DM_BLOCK_DEBUG 0xABE00000
53 #define DM_BLOCK_TERMINAL 0x00000001
54 #define DM_BLOCK_FREE 0x00000002
55 #define DM_BLOCK_MASK 0x001FFFFC
58 #define __DOSMEM_DEBUG__
61 typedef struct {
62 unsigned size;
63 } dosmem_entry;
65 typedef struct {
66 unsigned blocks;
67 unsigned free;
68 } dosmem_info;
70 #define NEXT_BLOCK(block) \
71 (dosmem_entry*)(((char*)(block)) + \
72 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
74 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
75 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
77 /* DOS memory base */
78 static char *DOSMEM_dosmem;
79 /* DOS system base (for interrupt vector table and BIOS data area)
80 * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
81 * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
82 * and only relocated to NULL when absolutely necessary */
83 static char *DOSMEM_sysmem;
85 /* various real-mode code stubs */
86 struct DPMI_segments DOSMEM_dpmi_segments;
88 /***********************************************************************
89 * DOSMEM_GetDPMISegments
91 const struct DPMI_segments *DOSMEM_GetDPMISegments(void)
93 return &DOSMEM_dpmi_segments;
96 /***********************************************************************
97 * DOSMEM_MemoryTop
99 * Gets the DOS memory top.
101 static char *DOSMEM_MemoryTop(void)
103 return DOSMEM_dosmem+0x9FFFC; /* 640K */
106 /***********************************************************************
107 * DOSMEM_InfoBlock
109 * Gets the DOS memory info block.
111 static dosmem_info *DOSMEM_InfoBlock(void)
113 return (dosmem_info*)(DOSMEM_dosmem+0x10000); /* 64K */
116 /***********************************************************************
117 * DOSMEM_RootBlock
119 * Gets the DOS memory root block.
121 static dosmem_entry *DOSMEM_RootBlock(void)
123 /* first block has to be paragraph-aligned */
124 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
125 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
128 /***********************************************************************
129 * DOSMEM_FillIsrTable
131 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
133 * NOTES:
134 * Linux normally only traps INTs performed from or destined to BIOSSEG
135 * for us to handle, if the int_revectored table is empty. Filling the
136 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
137 * to hook interrupts, as well as use their familiar retf tricks to call
138 * them, AND let Wine handle any unhooked interrupts transparently.
140 static void DOSMEM_FillIsrTable(void)
142 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
143 int x;
145 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
148 static void DOSMEM_MakeIsrStubs(void)
150 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
151 int x;
153 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
156 /***********************************************************************
157 * DOSMEM_InitDPMI
159 * Allocate the global DPMI RMCB wrapper.
161 static void DOSMEM_InitDPMI(void)
163 LPSTR ptr;
164 int i;
166 static const char wrap_code[]={
167 0xCD,0x31, /* int $0x31 */
168 0xCB /* lret */
171 static const char enter_xms[]=
173 /* XMS hookable entry point */
174 0xEB,0x03, /* jmp entry */
175 0x90,0x90,0x90, /* nop;nop;nop */
176 /* entry: */
177 /* real entry point */
178 /* for simplicity, we'll just use the same hook as DPMI below */
179 0xCD,0x31, /* int $0x31 */
180 0xCB /* lret */
183 static const char enter_pm[]=
185 0x50, /* pushw %ax */
186 0x52, /* pushw %dx */
187 0x55, /* pushw %bp */
188 0x89,0xE5, /* movw %sp,%bp */
189 /* get return CS */
190 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
191 /* just call int 31 here to get into protected mode... */
192 /* it'll check whether it was called from dpmi_seg... */
193 0xCD,0x31, /* int $0x31 */
194 /* we are now in the context of a 16-bit relay call */
195 /* need to fixup our stack;
196 * 16-bit relay return address will be lost, but we won't worry quite yet */
197 0x8E,0xD0, /* movw %ax,%ss */
198 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
199 /* set return CS */
200 0x89,0x56,0x08, /* movw %dx,8(%bp) */
201 0x5D, /* popw %bp */
202 0x5A, /* popw %dx */
203 0x58, /* popw %ax */
204 0xCB /* lret */
207 ptr = DOSMEM_GetBlock( sizeof(wrap_code), &DOSMEM_dpmi_segments.wrap_seg );
208 memcpy( ptr, wrap_code, sizeof(wrap_code) );
209 ptr = DOSMEM_GetBlock( sizeof(enter_xms), &DOSMEM_dpmi_segments.xms_seg );
210 memcpy( ptr, enter_xms, sizeof(enter_xms) );
211 ptr = DOSMEM_GetBlock( sizeof(enter_pm), &DOSMEM_dpmi_segments.dpmi_seg );
212 memcpy( ptr, enter_pm, sizeof(enter_pm) );
213 DOSMEM_dpmi_segments.dpmi_sel = SELECTOR_AllocBlock( ptr, sizeof(enter_pm), WINE_LDT_FLAGS_CODE );
215 ptr = DOSMEM_GetBlock( 6 * 256, &DOSMEM_dpmi_segments.int48_seg );
216 for(i=0; i<256; i++) {
218 * Each 32-bit interrupt handler is 6 bytes:
219 * 0xCD,<i> = int <i> (nested 16-bit interrupt)
220 * 0x66,0xCA,0x04,0x00 = ret 4 (32-bit far return and pop 4 bytes)
222 ptr[i * 6 + 0] = 0xCD;
223 ptr[i * 6 + 1] = i;
224 ptr[i * 6 + 2] = 0x66;
225 ptr[i * 6 + 3] = 0xCA;
226 ptr[i * 6 + 4] = 0x04;
227 ptr[i * 6 + 5] = 0x00;
229 DOSMEM_dpmi_segments.int48_sel = SELECTOR_AllocBlock( ptr, 6 * 256, WINE_LDT_FLAGS_CODE );
232 static BIOSDATA * DOSMEM_BiosData(void)
234 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
238 /***********************************************************************
239 * DOSMEM_FillBiosSegments
241 * Fill the BIOS data segment with dummy values.
243 static void DOSMEM_FillBiosSegments(void)
245 BYTE *pBiosSys = DOSMEM_dosmem + 0xf0000;
246 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
247 BIOS_EXTRA *extra = (BIOS_EXTRA *)(DOSMEM_dosmem + (int)BIOS_EXTRA_PTR);
248 BIOSDATA *pBiosData = DOSMEM_BiosData();
250 /* Supported VESA mode, see int10.c */
251 WORD ConstVesaModeList[]={0x00,0x01,0x02,0x03,0x07,0x0D,0x0E,0x10,0x12,0x13,
252 0x100,0x101,0x102,0x103,0x104,0x105,0x106,0x107,0x10D,0x10E,
253 0x10F,0x110,0x111,0x112,0x113,0x114,0x115,0x116,0x117,0x118,
254 0x119,0x11A,0x11B,0xFFFF};
255 char * ConstVesaString = "WINE SVGA BOARD";
256 int i;
258 VIDEOFUNCTIONALITY *pVidFunc = &extra->vid_func;
259 VIDEOSTATE *pVidState = &extra->vid_state;
260 VESAINFO *pVesaInfo = &extra->vesa_info;
261 char * VesaString = extra->vesa_string;
262 WORD * VesaModeList = extra->vesa_modes;
264 /* Clear all unused values */
265 memset( pBiosData, 0, sizeof(*pBiosData) );
266 memset( pVidFunc, 0, sizeof(*pVidFunc ) );
267 memset( pVidState, 0, sizeof(*pVidState) );
268 memset( pVesaInfo, 0, sizeof(*pVesaInfo) );
270 /* FIXME: should check the number of configured drives and ports */
271 pBiosData->Com1Addr = 0x3f8;
272 pBiosData->Com2Addr = 0x2f8;
273 pBiosData->Lpt1Addr = 0x378;
274 pBiosData->Lpt2Addr = 0x278;
275 pBiosData->InstalledHardware = 0x5463;
276 pBiosData->MemSize = 640;
277 pBiosData->NextKbdCharPtr = 0x1e;
278 pBiosData->FirstKbdCharPtr = 0x1e;
279 pBiosData->VideoMode = 3;
280 pBiosData->VideoColumns = 80;
281 pBiosData->VideoPageSize = 80 * 25 * 2;
282 pBiosData->VideoPageStartAddr = 0xb800;
283 pBiosData->VideoCtrlAddr = 0x3d4;
284 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
285 pBiosData->NbHardDisks = 2;
286 pBiosData->KbdBufferStart = 0x1e;
287 pBiosData->KbdBufferEnd = 0x3e;
288 pBiosData->RowsOnScreenMinus1 = 24;
289 pBiosData->BytesPerChar = 0x10;
290 pBiosData->ModeOptions = 0x64;
291 pBiosData->FeatureBitsSwitches = 0xf9;
292 pBiosData->VGASettings = 0x51;
293 pBiosData->DisplayCombination = 0x08;
294 pBiosData->DiskDataRate = 0;
296 /* fill ROM configuration table (values from Award) */
297 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
298 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
299 *(pBiosROMTable+0x2) = 0xfc; /* model */
300 *(pBiosROMTable+0x3) = 0x01; /* submodel */
301 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
302 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
303 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
304 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
305 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
306 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
309 for (i = 0; i < 7; i++)
310 pVidFunc->ModeSupport[i] = 0xff;
312 pVidFunc->ScanlineSupport = 7;
313 pVidFunc->NumberCharBlocks = 0;
314 pVidFunc->ActiveCharBlocks = 0;
315 pVidFunc->MiscFlags = 0x8ff;
316 pVidFunc->SavePointerFlags = 0x3f;
318 /* FIXME: always real mode ? */
319 pVidState->StaticFuncTable = BIOS_EXTRA_SEGPTR + offsetof(BIOS_EXTRA,vid_func);
320 pVidState->VideoMode = pBiosData->VideoMode; /* needs updates! */
321 pVidState->NumberColumns = pBiosData->VideoColumns; /* needs updates! */
322 pVidState->RegenBufLen = 0;
323 pVidState->RegenBufAddr = 0;
325 for (i = 0; i < 8; i++)
326 pVidState->CursorPos[i] = 0;
328 pVidState->CursorType = 0x0a0b; /* start/end line */
329 pVidState->ActivePage = 0;
330 pVidState->CRTCPort = 0x3da;
331 pVidState->Port3x8 = 0;
332 pVidState->Port3x9 = 0;
333 pVidState->NumberRows = 23; /* number of rows - 1 */
334 pVidState->BytesPerChar = 0x10;
335 pVidState->DCCActive = pBiosData->DisplayCombination;
336 pVidState->DCCAlternate = 0;
337 pVidState->NumberColors = 16;
338 pVidState->NumberPages = 1;
339 pVidState->NumberScanlines = 3; /* (0,1,2,3) = (200,350,400,480) */
340 pVidState->CharBlockPrimary = 0;
341 pVidState->CharBlockSecondary = 0;
342 pVidState->MiscFlags =
343 (pBiosData->VGASettings & 0x0f)
344 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
345 pVidState->NonVGASupport = 0;
346 pVidState->VideoMem = (pBiosData->ModeOptions & 0x60 >> 5);
347 pVidState->SavePointerState = 0;
348 pVidState->DisplayStatus = 4;
350 /* SVGA structures */
351 pVesaInfo->Signature = *(DWORD*)"VESA";
352 pVesaInfo->Major = 2;
353 pVesaInfo->Minor = 0;
354 /* FIXME: always real mode ? */
355 pVesaInfo->StaticVendorString = BIOS_EXTRA_SEGPTR + offsetof(BIOS_EXTRA,vesa_string);
356 pVesaInfo->CapabilitiesFlags = 0xfffffffd; /* FIXME: not really supported */
357 /* FIXME: always real mode ? */
358 pVesaInfo->StaticModeList = BIOS_EXTRA_SEGPTR + offsetof(BIOS_EXTRA,vesa_modes);
360 strcpy(VesaString,ConstVesaString);
361 memcpy(VesaModeList,ConstVesaModeList,sizeof(ConstVesaModeList));
363 /* BIOS date string */
364 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
366 /* BIOS ID */
367 *(pBiosSys+0xfffe) = 0xfc;
370 /***********************************************************************
371 * DOSMEM_InitCollateTable
373 * Initialises the collate table (character sorting, language dependent)
375 static void DOSMEM_InitCollateTable()
377 DWORD x;
378 unsigned char *tbl;
379 int i;
381 x = GlobalDOSAlloc16(258);
382 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
383 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
384 *(WORD*)tbl = 0x100;
385 tbl += 2;
386 for ( i = 0; i < 0x100; i++) *tbl++ = i;
389 /***********************************************************************
390 * DOSMEM_InitErrorTable
392 * Initialises the error tables (DOS 5+)
394 static void DOSMEM_InitErrorTable()
396 #if 0 /* no longer used */
397 DWORD x;
398 char *call;
400 /* We will use a snippet of real mode code that calls */
401 /* a WINE-only interrupt to handle moving the requested */
402 /* message into the buffer... */
404 /* FIXME - There is still something wrong... */
406 /* FIXME - Find hex values for opcodes...
408 (On call, AX contains message number
409 DI contains 'offset' (??)
410 Resturn, ES:DI points to counted string )
412 PUSH BX
413 MOV BX, AX
414 MOV AX, (arbitrary subfunction number)
415 INT (WINE-only interrupt)
416 POP BX
421 const int code = 4;
422 const int buffer = 80;
423 const int SIZE_TO_ALLOCATE = code + buffer;
425 /* FIXME - Complete rewrite of the table system to save */
426 /* precious DOS space. Now, we return the 0001:???? as */
427 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
428 /* as a special case and programs will use the alternate */
429 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
430 /* 0x08) which lets us have a smaller memory footprint anyway. */
432 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
434 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
435 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
437 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
439 memset(call, 0, SIZE_TO_ALLOCATE);
440 #endif
441 /* FIXME - Copy assembly into buffer here */
444 /***********************************************************************
445 * DOSMEM_InitMemory
447 * Initialises the DOS memory structures.
449 static void DOSMEM_InitMemory(void)
451 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
452 * 1000:0000 and ends at 9FFF:FFEF. */
454 dosmem_info* info_block = DOSMEM_InfoBlock();
455 dosmem_entry* root_block = DOSMEM_RootBlock();
456 dosmem_entry* dm;
458 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
460 info_block->blocks = 0;
461 info_block->free = root_block->size;
463 dm = NEXT_BLOCK(root_block);
464 dm->size = DM_BLOCK_TERMINAL;
465 root_block->size |= DM_BLOCK_FREE
466 #ifdef __DOSMEM_DEBUG__
467 | DM_BLOCK_DEBUG
468 #endif
472 /**********************************************************************
473 * setup_dos_mem
475 * Setup the first megabyte for DOS memory access
477 static void setup_dos_mem( int dos_init )
479 int sys_offset = 0;
480 int page_size = getpagesize();
481 void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
482 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
483 if (addr == (void *)page_size) /* we got what we wanted */
485 /* now map from address 0 */
486 addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
487 if (addr)
489 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
490 ExitProcess(1);
493 /* inform the memory manager that there is a mapping here */
494 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
496 /* protect the first 64K to catch NULL pointers */
497 if (!dos_init)
499 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
500 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
501 sys_offset += 0xf0000;
504 else
506 ERR("Cannot use first megabyte for DOS address space, please report\n" );
507 if (dos_init) ExitProcess(1);
508 /* allocate the DOS area somewhere else */
509 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
510 if (!addr)
512 ERR( "Cannot allocate DOS memory\n" );
513 ExitProcess(1);
516 DOSMEM_dosmem = addr;
517 DOSMEM_sysmem = (char*)addr + sys_offset;
521 /***********************************************************************
522 * DOSMEM_Init
524 * Create the dos memory segments, and store them into the KERNEL
525 * exported values.
527 BOOL DOSMEM_Init(BOOL dos_init)
529 static int already_done, already_mapped;
531 if (!already_done)
533 setup_dos_mem( dos_init );
535 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
536 0x10000, 0, WINE_LDT_FLAGS_DATA );
537 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
538 0x100, 0, WINE_LDT_FLAGS_DATA );
539 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
540 0x10000, 0, WINE_LDT_FLAGS_DATA );
541 DOSMEM_FillBiosSegments();
542 DOSMEM_FillIsrTable();
543 DOSMEM_InitMemory();
544 DOSMEM_InitCollateTable();
545 DOSMEM_InitErrorTable();
546 DOSMEM_InitDPMI();
547 already_done = 1;
549 else if (dos_init && !already_mapped)
551 if (DOSMEM_dosmem)
553 ERR( "Needs access to the first megabyte for DOS mode\n" );
554 ExitProcess(1);
556 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
557 " NULL pointer accesses will no longer be caught.\n" );
558 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
559 /* copy the BIOS and ISR area down */
560 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
561 DOSMEM_sysmem = DOSMEM_dosmem;
562 SetSelectorBase( DOSMEM_0000H, 0 );
563 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
564 /* we may now need the actual interrupt stubs, and since we've just moved the
565 * interrupt vector table away, we can fill the area with stubs instead... */
566 DOSMEM_MakeIsrStubs();
567 already_mapped = 1;
569 return TRUE;
573 /***********************************************************************
574 * DOSMEM_Tick
576 * Increment the BIOS tick counter. Called by timer signal handler.
578 void DOSMEM_Tick( WORD timer )
580 BIOSDATA *pBiosData = DOSMEM_BiosData();
581 if (pBiosData) pBiosData->Ticks++;
584 /***********************************************************************
585 * DOSMEM_GetBlock
587 * Carve a chunk of the DOS memory block (without selector).
589 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
591 UINT blocksize;
592 char *block = NULL;
593 dosmem_info *info_block = DOSMEM_InfoBlock();
594 dosmem_entry *dm;
595 #ifdef __DOSMEM_DEBUG_
596 dosmem_entry *prev = NULL;
597 #endif
599 if( size > info_block->free ) return NULL;
600 dm = DOSMEM_RootBlock();
602 while (dm && dm->size != DM_BLOCK_TERMINAL)
604 #ifdef __DOSMEM_DEBUG__
605 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
607 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
608 return NULL;
610 prev = dm;
611 #endif
612 if( dm->size & DM_BLOCK_FREE )
614 dosmem_entry *next = NEXT_BLOCK(dm);
616 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
618 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
619 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
620 next = NEXT_BLOCK(dm);
623 blocksize = dm->size & DM_BLOCK_MASK;
624 if( blocksize >= size )
626 block = ((char*)dm) + sizeof(dosmem_entry);
627 if( blocksize - size > 0x20 )
629 /* split dm so that the next one stays
630 * paragraph-aligned (and dm loses free bit) */
632 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
633 sizeof(dosmem_entry));
634 next = (dosmem_entry*)(((char*)dm) +
635 sizeof(dosmem_entry) + dm->size);
636 next->size = (blocksize - (dm->size +
637 sizeof(dosmem_entry))) | DM_BLOCK_FREE
638 #ifdef __DOSMEM_DEBUG__
639 | DM_BLOCK_DEBUG
640 #endif
642 } else dm->size &= DM_BLOCK_MASK;
644 info_block->blocks++;
645 info_block->free -= dm->size;
646 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
647 #ifdef __DOSMEM_DEBUG__
648 dm->size |= DM_BLOCK_DEBUG;
649 #endif
650 break;
652 dm = next;
654 else dm = NEXT_BLOCK(dm);
656 return (LPVOID)block;
659 /***********************************************************************
660 * DOSMEM_FreeBlock
662 BOOL DOSMEM_FreeBlock(void* ptr)
664 dosmem_info *info_block = DOSMEM_InfoBlock();
666 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
667 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
668 - DOSMEM_dosmem) & 0xf) )
670 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
672 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
673 #ifdef __DOSMEM_DEBUG__
674 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
675 #endif
678 info_block->blocks--;
679 info_block->free += dm->size;
681 dm->size |= DM_BLOCK_FREE;
682 return TRUE;
685 return FALSE;
688 /***********************************************************************
689 * DOSMEM_ResizeBlock
691 LPVOID DOSMEM_ResizeBlock(void* ptr, UINT size, UINT16* pseg)
693 char *block = NULL;
694 dosmem_info *info_block = DOSMEM_InfoBlock();
696 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
697 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
698 - DOSMEM_dosmem) & 0xf) )
700 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
702 if( pseg ) *pseg = ((char*)ptr - DOSMEM_dosmem) >> 4;
704 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
707 dosmem_entry *next = NEXT_BLOCK(dm);
708 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
710 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
712 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
713 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
714 next = NEXT_BLOCK(dm);
717 blocksize = dm->size & DM_BLOCK_MASK;
718 if (blocksize >= size)
720 block = ((char*)dm) + sizeof(dosmem_entry);
721 if( blocksize - size > 0x20 )
723 /* split dm so that the next one stays
724 * paragraph-aligned (and next gains free bit) */
726 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
727 sizeof(dosmem_entry));
728 next = (dosmem_entry*)(((char*)dm) +
729 sizeof(dosmem_entry) + dm->size);
730 next->size = (blocksize - (dm->size +
731 sizeof(dosmem_entry))) | DM_BLOCK_FREE
733 } else dm->size &= DM_BLOCK_MASK;
735 info_block->free += orgsize - dm->size;
736 } else {
737 /* the collapse didn't help, try getting a new block */
738 block = DOSMEM_GetBlock(size, pseg);
739 if (block) {
740 /* we got one, copy the old data there (we do need to, right?) */
741 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
742 (size<orgsize) ? size : orgsize);
743 /* free old block */
744 info_block->blocks--;
745 info_block->free += dm->size;
747 dm->size |= DM_BLOCK_FREE;
748 } else {
749 /* and Bill Gates said 640K should be enough for everyone... */
751 /* need to split original and collapsed blocks apart again,
752 * and free the collapsed blocks again, before exiting */
753 if( blocksize - orgsize > 0x20 )
755 /* split dm so that the next one stays
756 * paragraph-aligned (and next gains free bit) */
758 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
759 sizeof(dosmem_entry));
760 next = (dosmem_entry*)(((char*)dm) +
761 sizeof(dosmem_entry) + dm->size);
762 next->size = (blocksize - (dm->size +
763 sizeof(dosmem_entry))) | DM_BLOCK_FREE
765 } else dm->size &= DM_BLOCK_MASK;
770 return (LPVOID)block;
774 /***********************************************************************
775 * DOSMEM_Available
777 UINT DOSMEM_Available(void)
779 UINT blocksize, available = 0;
780 dosmem_entry *dm;
782 dm = DOSMEM_RootBlock();
784 while (dm && dm->size != DM_BLOCK_TERMINAL)
786 #ifdef __DOSMEM_DEBUG__
787 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
789 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
790 return NULL;
792 prev = dm;
793 #endif
794 if( dm->size & DM_BLOCK_FREE )
796 dosmem_entry *next = NEXT_BLOCK(dm);
798 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
800 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
801 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
802 next = NEXT_BLOCK(dm);
805 blocksize = dm->size & DM_BLOCK_MASK;
806 if ( blocksize > available ) available = blocksize;
807 dm = next;
809 else dm = NEXT_BLOCK(dm);
811 return available;
815 /***********************************************************************
816 * DOSMEM_MapLinearToDos
818 * Linear address to the DOS address space.
820 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
822 if (((char*)ptr >= DOSMEM_dosmem) &&
823 ((char*)ptr < DOSMEM_dosmem + 0x100000))
824 return (UINT)ptr - (UINT)DOSMEM_dosmem;
825 return (UINT)ptr;
829 /***********************************************************************
830 * DOSMEM_MapDosToLinear
832 * DOS linear address to the linear address space.
834 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
836 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
837 return (LPVOID)ptr;
841 /***********************************************************************
842 * DOSMEM_MapRealToLinear
844 * Real mode DOS address into a linear pointer
846 LPVOID DOSMEM_MapRealToLinear(DWORD x)
848 LPVOID lin;
850 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
851 TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
852 return lin;
855 /***********************************************************************
856 * DOSMEM_AllocSelector
858 * Allocates a protected mode selector for a realmode segment.
860 WORD DOSMEM_AllocSelector(WORD realsel)
862 HMODULE16 hModule = GetModuleHandle16("KERNEL");
863 WORD sel;
865 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
866 hModule, WINE_LDT_FLAGS_DATA );
867 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
868 return sel;