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
23 #include "wine/port.h"
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
34 #include "wine/winbase16.h"
37 #include "selectors.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__
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 */
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 */
93 /***********************************************************************
96 * Gets the virtual DOS memory base (interrupt table).
98 char *DOSMEM_SystemBase(void)
100 return DOSMEM_sysmem
;
103 /***********************************************************************
106 * Gets the DOS memory base.
108 char *DOSMEM_MemoryBase(void)
110 return DOSMEM_dosmem
;
113 /***********************************************************************
116 * Gets the DOS memory top.
118 static char *DOSMEM_MemoryTop(void)
120 return DOSMEM_dosmem
+0x9FFFC; /* 640K */
123 /***********************************************************************
126 * Gets the DOS memory info block.
128 static dosmem_info
*DOSMEM_InfoBlock(void)
130 return (dosmem_info
*)(DOSMEM_dosmem
+0x10000); /* 64K */
133 /***********************************************************************
136 * Gets the DOS memory root block.
138 static dosmem_entry
*DOSMEM_RootBlock(void)
140 /* first block has to be paragraph-aligned */
141 return (dosmem_entry
*)(((char*)DOSMEM_InfoBlock()) +
142 ((((sizeof(dosmem_info
) + 0xf) & ~0xf) - sizeof(dosmem_entry
))));
145 /***********************************************************************
146 * DOSMEM_FillIsrTable
148 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
151 * Linux normally only traps INTs performed from or destined to BIOSSEG
152 * for us to handle, if the int_revectored table is empty. Filling the
153 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
154 * to hook interrupts, as well as use their familiar retf tricks to call
155 * them, AND let Wine handle any unhooked interrupts transparently.
157 static void DOSMEM_FillIsrTable(void)
159 SEGPTR
*isr
= (SEGPTR
*)DOSMEM_sysmem
;
162 for (x
=0; x
<256; x
++) isr
[x
]=MAKESEGPTR(VM_STUB_SEGMENT
,x
*4);
165 static void DOSMEM_MakeIsrStubs(void)
167 DWORD
*stub
= (DWORD
*)(DOSMEM_dosmem
+ (VM_STUB_SEGMENT
<< 4));
170 for (x
=0; x
<256; x
++) stub
[x
]=VM_STUB(x
);
173 /***********************************************************************
176 * Allocate the global DPMI RMCB wrapper.
178 static void DOSMEM_InitDPMI(void)
182 static const char wrap_code
[]={
183 0xCD,0x31, /* int $0x31 */
187 static const char enter_xms
[]=
189 /* XMS hookable entry point */
190 0xEB,0x03, /* jmp entry */
191 0x90,0x90,0x90, /* nop;nop;nop */
193 /* real entry point */
194 /* for simplicity, we'll just use the same hook as DPMI below */
195 0xCD,0x31, /* int $0x31 */
199 static const char enter_pm
[]=
201 0x50, /* pushw %ax */
202 0x52, /* pushw %dx */
203 0x55, /* pushw %bp */
204 0x89,0xE5, /* movw %sp,%bp */
206 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
207 /* just call int 31 here to get into protected mode... */
208 /* it'll check whether it was called from dpmi_seg... */
209 0xCD,0x31, /* int $0x31 */
210 /* we are now in the context of a 16-bit relay call */
211 /* need to fixup our stack;
212 * 16-bit relay return address will be lost, but we won't worry quite yet */
213 0x8E,0xD0, /* movw %ax,%ss */
214 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
216 0x89,0x56,0x08, /* movw %dx,8(%bp) */
223 ptr
= DOSMEM_GetBlock( sizeof(wrap_code
), &DOSMEM_wrap_seg
);
224 memcpy( ptr
, wrap_code
, sizeof(wrap_code
) );
225 ptr
= DOSMEM_GetBlock( sizeof(enter_xms
), &DOSMEM_xms_seg
);
226 memcpy( ptr
, enter_xms
, sizeof(enter_xms
) );
227 ptr
= DOSMEM_GetBlock( sizeof(enter_pm
), &DOSMEM_dpmi_seg
);
228 memcpy( ptr
, enter_pm
, sizeof(enter_pm
) );
229 DOSMEM_dpmi_sel
= SELECTOR_AllocBlock( ptr
, sizeof(enter_pm
), WINE_LDT_FLAGS_CODE
);
232 BIOSDATA
* DOSMEM_BiosData()
234 return (BIOSDATA
*)(DOSMEM_sysmem
+ 0x400);
237 BYTE
* DOSMEM_BiosSys()
239 return DOSMEM_dosmem
+0xf0000;
242 /***********************************************************************
243 * DOSMEM_FillBiosSegments
245 * Fill the BIOS data segment with dummy values.
247 static void DOSMEM_FillBiosSegments(void)
249 BYTE
*pBiosSys
= DOSMEM_BiosSys();
250 BYTE
*pBiosROMTable
= pBiosSys
+0xe6f5;
251 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
253 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
254 VIDEOFUNCTIONALITY
*pVidFunc
= (VIDEOFUNCTIONALITY
*)(pBiosSys
+0xe000);
255 VIDEOSTATE
*pVidState
= (VIDEOSTATE
*)(pBiosSys
+0xe010);
258 /* Clear all unused values */
259 memset( pBiosData
, 0, sizeof(*pBiosData
) );
260 memset( pVidFunc
, 0, sizeof(*pVidFunc
) );
261 memset( pVidState
, 0, sizeof(*pVidState
) );
263 /* FIXME: should check the number of configured drives and ports */
265 pBiosData
->Com1Addr
= 0x3f8;
266 pBiosData
->Com2Addr
= 0x2f8;
267 pBiosData
->Lpt1Addr
= 0x378;
268 pBiosData
->Lpt2Addr
= 0x278;
269 pBiosData
->InstalledHardware
= 0x5463;
270 pBiosData
->MemSize
= 640;
271 pBiosData
->NextKbdCharPtr
= 0x1e;
272 pBiosData
->FirstKbdCharPtr
= 0x1e;
273 pBiosData
->VideoMode
= 3;
274 pBiosData
->VideoColumns
= 80;
275 pBiosData
->VideoPageSize
= 80 * 25 * 2;
276 pBiosData
->VideoPageStartAddr
= 0xb800;
277 pBiosData
->VideoCtrlAddr
= 0x3d4;
278 pBiosData
->Ticks
= INT1A_GetTicksSinceMidnight();
279 pBiosData
->NbHardDisks
= 2;
280 pBiosData
->KbdBufferStart
= 0x1e;
281 pBiosData
->KbdBufferEnd
= 0x3e;
282 pBiosData
->RowsOnScreenMinus1
= 23;
283 pBiosData
->BytesPerChar
= 0x10;
284 pBiosData
->ModeOptions
= 0x64;
285 pBiosData
->FeatureBitsSwitches
= 0xf9;
286 pBiosData
->VGASettings
= 0x51;
287 pBiosData
->DisplayCombination
= 0x08;
288 pBiosData
->DiskDataRate
= 0;
290 /* fill ROM configuration table (values from Award) */
291 *(pBiosROMTable
+0x0) = 0x08; /* number of bytes following LO */
292 *(pBiosROMTable
+0x1) = 0x00; /* number of bytes following HI */
293 *(pBiosROMTable
+0x2) = 0xfc; /* model */
294 *(pBiosROMTable
+0x3) = 0x01; /* submodel */
295 *(pBiosROMTable
+0x4) = 0x00; /* BIOS revision */
296 *(pBiosROMTable
+0x5) = 0x74; /* feature byte 1 */
297 *(pBiosROMTable
+0x6) = 0x00; /* feature byte 2 */
298 *(pBiosROMTable
+0x7) = 0x00; /* feature byte 3 */
299 *(pBiosROMTable
+0x8) = 0x00; /* feature byte 4 */
300 *(pBiosROMTable
+0x9) = 0x00; /* feature byte 5 */
303 for (i
= 0; i
< 7; i
++)
304 pVidFunc
->ModeSupport
[i
] = 0xff;
306 pVidFunc
->ScanlineSupport
= 7;
307 pVidFunc
->NumberCharBlocks
= 0;
308 pVidFunc
->ActiveCharBlocks
= 0;
309 pVidFunc
->MiscFlags
= 0x8ff;
310 pVidFunc
->SavePointerFlags
= 0x3f;
312 pVidState
->StaticFuncTable
= 0xf000e000; /* FIXME: always real mode ? */
313 pVidState
->VideoMode
= pBiosData
->VideoMode
; /* needs updates! */
314 pVidState
->NumberColumns
= pBiosData
->VideoColumns
; /* needs updates! */
315 pVidState
->RegenBufLen
= 0;
316 pVidState
->RegenBufAddr
= 0;
318 for (i
= 0; i
< 8; i
++)
319 pVidState
->CursorPos
[i
] = 0;
321 pVidState
->CursorType
= 0x0a0b; /* start/end line */
322 pVidState
->ActivePage
= 0;
323 pVidState
->CRTCPort
= 0x3da;
324 pVidState
->Port3x8
= 0;
325 pVidState
->Port3x9
= 0;
326 pVidState
->NumberRows
= 23; /* number of rows - 1 */
327 pVidState
->BytesPerChar
= 0x10;
328 pVidState
->DCCActive
= pBiosData
->DisplayCombination
;
329 pVidState
->DCCAlternate
= 0;
330 pVidState
->NumberColors
= 16;
331 pVidState
->NumberPages
= 1;
332 pVidState
->NumberScanlines
= 3; /* (0,1,2,3) = (200,350,400,480) */
333 pVidState
->CharBlockPrimary
= 0;
334 pVidState
->CharBlockSecondary
= 0;
335 pVidState
->MiscFlags
=
336 (pBiosData
->VGASettings
& 0x0f)
337 | ((pBiosData
->ModeOptions
& 1) << 4); /* cursor emulation */
338 pVidState
->NonVGASupport
= 0;
339 pVidState
->VideoMem
= (pBiosData
->ModeOptions
& 0x60 >> 5);
340 pVidState
->SavePointerState
= 0;
341 pVidState
->DisplayStatus
= 4;
343 /* BIOS date string */
344 strcpy((char *)pBiosSys
+0xfff5, "13/01/99");
347 *(pBiosSys
+0xfffe) = 0xfc;
350 /***********************************************************************
351 * DOSMEM_InitCollateTable
353 * Initialises the collate table (character sorting, language dependent)
355 static void DOSMEM_InitCollateTable()
361 x
= GlobalDOSAlloc16(258);
362 DOSMEM_CollateTable
= MAKELONG(0,(x
>>16));
363 tbl
= DOSMEM_MapRealToLinear(DOSMEM_CollateTable
);
366 for ( i
= 0; i
< 0x100; i
++) *tbl
++ = i
;
369 /***********************************************************************
370 * DOSMEM_InitErrorTable
372 * Initialises the error tables (DOS 5+)
374 static void DOSMEM_InitErrorTable()
376 #if 0 /* no longer used */
380 /* We will use a snippet of real mode code that calls */
381 /* a WINE-only interrupt to handle moving the requested */
382 /* message into the buffer... */
384 /* FIXME - There is still something wrong... */
386 /* FIXME - Find hex values for opcodes...
388 (On call, AX contains message number
389 DI contains 'offset' (??)
390 Resturn, ES:DI points to counted string )
394 MOV AX, (arbitrary subfunction number)
395 INT (WINE-only interrupt)
402 const int buffer
= 80;
403 const int SIZE_TO_ALLOCATE
= code
+ buffer
;
405 /* FIXME - Complete rewrite of the table system to save */
406 /* precious DOS space. Now, we return the 0001:???? as */
407 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
408 /* as a special case and programs will use the alternate */
409 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
410 /* 0x08) which lets us have a smaller memory footprint anyway. */
412 x
= GlobalDOSAlloc16(SIZE_TO_ALLOCATE
);
414 DOSMEM_ErrorCall
= MAKELONG(0,(x
>>16));
415 DOSMEM_ErrorBuffer
= DOSMEM_ErrorCall
+ code
;
417 call
= DOSMEM_MapRealToLinear(DOSMEM_ErrorCall
);
419 memset(call
, 0, SIZE_TO_ALLOCATE
);
421 /* FIXME - Copy assembly into buffer here */
424 /***********************************************************************
427 * Initialises the DOS memory structures.
429 static void DOSMEM_InitMemory(void)
431 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
432 * 1000:0000 and ends at 9FFF:FFEF. */
434 dosmem_info
* info_block
= DOSMEM_InfoBlock();
435 dosmem_entry
* root_block
= DOSMEM_RootBlock();
438 root_block
->size
= DOSMEM_MemoryTop() - (((char*)root_block
) + sizeof(dosmem_entry
));
440 info_block
->blocks
= 0;
441 info_block
->free
= root_block
->size
;
443 dm
= NEXT_BLOCK(root_block
);
444 dm
->size
= DM_BLOCK_TERMINAL
;
445 root_block
->size
|= DM_BLOCK_FREE
446 #ifdef __DOSMEM_DEBUG__
452 /**********************************************************************
455 * Setup the first megabyte for DOS memory access
457 static void setup_dos_mem( int dos_init
)
460 int page_size
= getpagesize();
461 void *addr
= wine_anon_mmap( (void *)page_size
, 0x110000-page_size
,
462 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
463 if (addr
== (void *)page_size
) /* we got what we wanted */
465 /* now map from address 0 */
466 addr
= wine_anon_mmap( NULL
, 0x110000, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_FIXED
);
469 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
473 /* inform the memory manager that there is a mapping here */
474 VirtualAlloc( addr
, 0x110000, MEM_RESERVE
| MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
476 /* protect the first 64K to catch NULL pointers */
479 VirtualProtect( addr
, 0x10000, PAGE_NOACCESS
, NULL
);
480 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
481 sys_offset
+= 0xf0000;
486 ERR("Cannot use first megabyte for DOS address space, please report\n" );
487 if (dos_init
) ExitProcess(1);
488 /* allocate the DOS area somewhere else */
489 addr
= VirtualAlloc( NULL
, 0x110000, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
492 ERR( "Cannot allocate DOS memory\n" );
496 DOSMEM_dosmem
= addr
;
497 DOSMEM_sysmem
= (char*)addr
+ sys_offset
;
501 /***********************************************************************
504 * Create the dos memory segments, and store them into the KERNEL
507 BOOL
DOSMEM_Init(BOOL dos_init
)
509 static int already_done
, already_mapped
;
513 setup_dos_mem( dos_init
);
515 DOSMEM_0000H
= GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_sysmem
,
516 0x10000, 0, WINE_LDT_FLAGS_DATA
);
517 DOSMEM_BiosDataSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_sysmem
+ 0x400,
518 0x100, 0, WINE_LDT_FLAGS_DATA
);
519 DOSMEM_BiosSysSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_dosmem
+0xf0000,
520 0x10000, 0, WINE_LDT_FLAGS_DATA
);
521 DOSMEM_FillBiosSegments();
522 DOSMEM_FillIsrTable();
524 DOSMEM_InitCollateTable();
525 DOSMEM_InitErrorTable();
529 else if (dos_init
&& !already_mapped
)
533 ERR( "Needs access to the first megabyte for DOS mode\n" );
536 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
537 " NULL pointer accesses will no longer be caught.\n" );
538 VirtualProtect( NULL
, 0x10000, PAGE_EXECUTE_READWRITE
, NULL
);
539 /* copy the BIOS and ISR area down */
540 memcpy( DOSMEM_dosmem
, DOSMEM_sysmem
, 0x400 + 0x100 );
541 DOSMEM_sysmem
= DOSMEM_dosmem
;
542 SetSelectorBase( DOSMEM_0000H
, 0 );
543 SetSelectorBase( DOSMEM_BiosDataSeg
, 0x400 );
544 /* we may now need the actual interrupt stubs, and since we've just moved the
545 * interrupt vector table away, we can fill the area with stubs instead... */
546 DOSMEM_MakeIsrStubs();
553 /***********************************************************************
556 * Increment the BIOS tick counter. Called by timer signal handler.
558 void DOSMEM_Tick( WORD timer
)
560 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
561 if (pBiosData
) pBiosData
->Ticks
++;
564 /***********************************************************************
567 * Carve a chunk of the DOS memory block (without selector).
569 LPVOID
DOSMEM_GetBlock(UINT size
, UINT16
* pseg
)
573 dosmem_info
*info_block
= DOSMEM_InfoBlock();
575 #ifdef __DOSMEM_DEBUG_
576 dosmem_entry
*prev
= NULL
;
579 if( size
> info_block
->free
) return NULL
;
580 dm
= DOSMEM_RootBlock();
582 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
584 #ifdef __DOSMEM_DEBUG__
585 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
587 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
592 if( dm
->size
& DM_BLOCK_FREE
)
594 dosmem_entry
*next
= NEXT_BLOCK(dm
);
596 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
598 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
599 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
600 next
= NEXT_BLOCK(dm
);
603 blocksize
= dm
->size
& DM_BLOCK_MASK
;
604 if( blocksize
>= size
)
606 block
= ((char*)dm
) + sizeof(dosmem_entry
);
607 if( blocksize
- size
> 0x20 )
609 /* split dm so that the next one stays
610 * paragraph-aligned (and dm loses free bit) */
612 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
613 sizeof(dosmem_entry
));
614 next
= (dosmem_entry
*)(((char*)dm
) +
615 sizeof(dosmem_entry
) + dm
->size
);
616 next
->size
= (blocksize
- (dm
->size
+
617 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
618 #ifdef __DOSMEM_DEBUG__
622 } else dm
->size
&= DM_BLOCK_MASK
;
624 info_block
->blocks
++;
625 info_block
->free
-= dm
->size
;
626 if( pseg
) *pseg
= (block
- DOSMEM_dosmem
) >> 4;
627 #ifdef __DOSMEM_DEBUG__
628 dm
->size
|= DM_BLOCK_DEBUG
;
634 else dm
= NEXT_BLOCK(dm
);
636 return (LPVOID
)block
;
639 /***********************************************************************
642 BOOL
DOSMEM_FreeBlock(void* ptr
)
644 dosmem_info
*info_block
= DOSMEM_InfoBlock();
646 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
647 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
648 - DOSMEM_dosmem
) & 0xf) )
650 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
652 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
653 #ifdef __DOSMEM_DEBUG__
654 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
658 info_block
->blocks
--;
659 info_block
->free
+= dm
->size
;
661 dm
->size
|= DM_BLOCK_FREE
;
668 /***********************************************************************
671 LPVOID
DOSMEM_ResizeBlock(void* ptr
, UINT size
, UINT16
* pseg
)
674 dosmem_info
*info_block
= DOSMEM_InfoBlock();
676 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
677 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
678 - DOSMEM_dosmem
) & 0xf) )
680 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
682 if( pseg
) *pseg
= ((char*)ptr
- DOSMEM_dosmem
) >> 4;
684 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
687 dosmem_entry
*next
= NEXT_BLOCK(dm
);
688 UINT blocksize
, orgsize
= dm
->size
& DM_BLOCK_MASK
;
690 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
692 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
693 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
694 next
= NEXT_BLOCK(dm
);
697 blocksize
= dm
->size
& DM_BLOCK_MASK
;
698 if (blocksize
>= size
)
700 block
= ((char*)dm
) + sizeof(dosmem_entry
);
701 if( blocksize
- size
> 0x20 )
703 /* split dm so that the next one stays
704 * paragraph-aligned (and next gains free bit) */
706 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
707 sizeof(dosmem_entry
));
708 next
= (dosmem_entry
*)(((char*)dm
) +
709 sizeof(dosmem_entry
) + dm
->size
);
710 next
->size
= (blocksize
- (dm
->size
+
711 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
713 } else dm
->size
&= DM_BLOCK_MASK
;
715 info_block
->free
+= orgsize
- dm
->size
;
717 /* the collapse didn't help, try getting a new block */
718 block
= DOSMEM_GetBlock(size
, pseg
);
720 /* we got one, copy the old data there (we do need to, right?) */
721 memcpy(block
, ((char*)dm
) + sizeof(dosmem_entry
),
722 (size
<orgsize
) ? size
: orgsize
);
724 info_block
->blocks
--;
725 info_block
->free
+= dm
->size
;
727 dm
->size
|= DM_BLOCK_FREE
;
729 /* and Bill Gates said 640K should be enough for everyone... */
731 /* need to split original and collapsed blocks apart again,
732 * and free the collapsed blocks again, before exiting */
733 if( blocksize
- orgsize
> 0x20 )
735 /* split dm so that the next one stays
736 * paragraph-aligned (and next gains free bit) */
738 dm
->size
= (((orgsize
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
739 sizeof(dosmem_entry
));
740 next
= (dosmem_entry
*)(((char*)dm
) +
741 sizeof(dosmem_entry
) + dm
->size
);
742 next
->size
= (blocksize
- (dm
->size
+
743 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
745 } else dm
->size
&= DM_BLOCK_MASK
;
750 return (LPVOID
)block
;
754 /***********************************************************************
757 UINT
DOSMEM_Available(void)
759 UINT blocksize
, available
= 0;
762 dm
= DOSMEM_RootBlock();
764 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
766 #ifdef __DOSMEM_DEBUG__
767 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
769 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
774 if( dm
->size
& DM_BLOCK_FREE
)
776 dosmem_entry
*next
= NEXT_BLOCK(dm
);
778 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
780 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
781 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
782 next
= NEXT_BLOCK(dm
);
785 blocksize
= dm
->size
& DM_BLOCK_MASK
;
786 if ( blocksize
> available
) available
= blocksize
;
789 else dm
= NEXT_BLOCK(dm
);
795 /***********************************************************************
796 * DOSMEM_MapLinearToDos
798 * Linear address to the DOS address space.
800 UINT
DOSMEM_MapLinearToDos(LPVOID ptr
)
802 if (((char*)ptr
>= DOSMEM_dosmem
) &&
803 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
804 return (UINT
)ptr
- (UINT
)DOSMEM_dosmem
;
809 /***********************************************************************
810 * DOSMEM_MapDosToLinear
812 * DOS linear address to the linear address space.
814 LPVOID
DOSMEM_MapDosToLinear(UINT ptr
)
816 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT
)DOSMEM_dosmem
);
821 /***********************************************************************
822 * DOSMEM_MapRealToLinear
824 * Real mode DOS address into a linear pointer
826 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
830 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
831 TRACE_(selector
)("(0x%08lx) returns %p.\n", x
, lin
);
835 /***********************************************************************
836 * DOSMEM_AllocSelector
838 * Allocates a protected mode selector for a realmode segment.
840 WORD
DOSMEM_AllocSelector(WORD realsel
)
842 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
845 sel
=GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_dosmem
+realsel
*16, 0x10000,
846 hModule
, WINE_LDT_FLAGS_DATA
);
847 TRACE_(selector
)("(0x%04x) returns 0x%04x.\n", realsel
,sel
);