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"
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
36 #include "wine/winbase16.h"
38 #include "kernel_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dosmem
);
44 WINE_DECLARE_DEBUG_CHANNEL(selector
);
46 WORD DOSMEM_0000H
; /* segment at 0:0 */
47 WORD DOSMEM_BiosDataSeg
; /* BIOS data segment at 0x40:0 */
48 WORD DOSMEM_BiosSysSeg
; /* BIOS ROM segment at 0xf000:0 */
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 /* Start of DOS conventional memory */
86 static char *DOSMEM_membase
;
88 static void DOSMEM_InitMemory(void);
90 /***********************************************************************
93 * Gets the DOS memory top.
95 static char *DOSMEM_MemoryTop(void)
97 return DOSMEM_dosmem
+0x9FFFC; /* 640K */
100 /***********************************************************************
103 * Gets the DOS memory info block.
105 static dosmem_info
*DOSMEM_InfoBlock(void)
113 * - lowest 64k for NULL pointer catching (Win16)
114 * - lowest 1k for interrupt handlers and
115 * another 0.5k for BIOS, DOS and intra-application
118 if (DOSMEM_dosmem
!= DOSMEM_sysmem
)
119 reserve
= 0x10000; /* 64k */
121 reserve
= 0x600; /* 1.5k */
124 * Round to paragraph boundary in order to make
125 * sure the alignment is correct.
127 reserve
= ((reserve
+ 15) >> 4) << 4;
130 * Set DOS memory base and initialize conventional memory.
132 DOSMEM_membase
= DOSMEM_dosmem
+ reserve
;
136 return (dosmem_info
*)DOSMEM_membase
;
139 /***********************************************************************
142 * Gets the DOS memory root block.
144 static dosmem_entry
*DOSMEM_RootBlock(void)
146 /* first block has to be paragraph-aligned */
147 return (dosmem_entry
*)(((char*)DOSMEM_InfoBlock()) +
148 ((((sizeof(dosmem_info
) + 0xf) & ~0xf) - sizeof(dosmem_entry
))));
151 /***********************************************************************
152 * DOSMEM_FillIsrTable
154 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
157 * Linux normally only traps INTs performed from or destined to BIOSSEG
158 * for us to handle, if the int_revectored table is empty. Filling the
159 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
160 * to hook interrupts, as well as use their familiar retf tricks to call
161 * them, AND let Wine handle any unhooked interrupts transparently.
163 static void DOSMEM_FillIsrTable(void)
165 SEGPTR
*isr
= (SEGPTR
*)DOSMEM_sysmem
;
168 for (x
=0; x
<256; x
++) isr
[x
]=MAKESEGPTR(VM_STUB_SEGMENT
,x
*4);
171 static void DOSMEM_MakeIsrStubs(void)
173 DWORD
*stub
= (DWORD
*)(DOSMEM_dosmem
+ (VM_STUB_SEGMENT
<< 4));
176 for (x
=0; x
<256; x
++) stub
[x
]=VM_STUB(x
);
179 static BIOSDATA
* DOSMEM_BiosData(void)
181 return (BIOSDATA
*)(DOSMEM_sysmem
+ 0x400);
184 /**********************************************************************
185 * DOSMEM_GetTicksSinceMidnight
187 * Return number of clock ticks since midnight.
189 static DWORD
DOSMEM_GetTicksSinceMidnight(void)
193 /* This should give us the (approximately) correct
194 * 18.206 clock ticks per second since midnight.
197 GetLocalTime( &time
);
199 return (((time
.wHour
* 3600 + time
.wMinute
* 60 +
200 time
.wSecond
) * 18206) / 1000) +
201 (time
.wMilliseconds
* 1000 / 54927);
204 /***********************************************************************
205 * DOSMEM_FillBiosSegments
207 * Fill the BIOS data segment with dummy values.
209 static void DOSMEM_FillBiosSegments(void)
211 char *pBiosSys
= DOSMEM_dosmem
+ 0xf0000;
212 BYTE
*pBiosROMTable
= pBiosSys
+0xe6f5;
213 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
215 /* Clear all unused values */
216 memset( pBiosData
, 0, sizeof(*pBiosData
) );
218 /* FIXME: should check the number of configured drives and ports */
219 pBiosData
->Com1Addr
= 0x3f8;
220 pBiosData
->Com2Addr
= 0x2f8;
221 pBiosData
->Lpt1Addr
= 0x378;
222 pBiosData
->Lpt2Addr
= 0x278;
223 pBiosData
->InstalledHardware
= 0x5463;
224 pBiosData
->MemSize
= 640;
225 pBiosData
->NextKbdCharPtr
= 0x1e;
226 pBiosData
->FirstKbdCharPtr
= 0x1e;
227 pBiosData
->VideoMode
= 3;
228 pBiosData
->VideoColumns
= 80;
229 pBiosData
->VideoPageSize
= 80 * 25 * 2;
230 pBiosData
->VideoPageStartAddr
= 0xb800;
231 pBiosData
->VideoCtrlAddr
= 0x3d4;
232 pBiosData
->Ticks
= DOSMEM_GetTicksSinceMidnight();
233 pBiosData
->NbHardDisks
= 2;
234 pBiosData
->KbdBufferStart
= 0x1e;
235 pBiosData
->KbdBufferEnd
= 0x3e;
236 pBiosData
->RowsOnScreenMinus1
= 24;
237 pBiosData
->BytesPerChar
= 0x10;
238 pBiosData
->ModeOptions
= 0x64;
239 pBiosData
->FeatureBitsSwitches
= 0xf9;
240 pBiosData
->VGASettings
= 0x51;
241 pBiosData
->DisplayCombination
= 0x08;
242 pBiosData
->DiskDataRate
= 0;
244 /* fill ROM configuration table (values from Award) */
245 *(pBiosROMTable
+0x0) = 0x08; /* number of bytes following LO */
246 *(pBiosROMTable
+0x1) = 0x00; /* number of bytes following HI */
247 *(pBiosROMTable
+0x2) = 0xfc; /* model */
248 *(pBiosROMTable
+0x3) = 0x01; /* submodel */
249 *(pBiosROMTable
+0x4) = 0x00; /* BIOS revision */
250 *(pBiosROMTable
+0x5) = 0x74; /* feature byte 1 */
251 *(pBiosROMTable
+0x6) = 0x00; /* feature byte 2 */
252 *(pBiosROMTable
+0x7) = 0x00; /* feature byte 3 */
253 *(pBiosROMTable
+0x8) = 0x00; /* feature byte 4 */
254 *(pBiosROMTable
+0x9) = 0x00; /* feature byte 5 */
256 /* BIOS date string */
257 strcpy(pBiosSys
+0xfff5, "13/01/99");
260 *(pBiosSys
+0xfffe) = 0xfc;
262 /* Reboot vector (f000:fff0 or ffff:0000) */
263 *(DWORD
*)(pBiosSys
+ 0xfff0) = VM_STUB(0x19);
266 /***********************************************************************
269 * Initialises the DOS memory structures.
271 static void DOSMEM_InitMemory(void)
273 dosmem_info
* info_block
= DOSMEM_InfoBlock();
274 dosmem_entry
* root_block
= DOSMEM_RootBlock();
277 root_block
->size
= DOSMEM_MemoryTop() - (((char*)root_block
) + sizeof(dosmem_entry
));
279 info_block
->blocks
= 0;
280 info_block
->free
= root_block
->size
;
282 dm
= NEXT_BLOCK(root_block
);
283 dm
->size
= DM_BLOCK_TERMINAL
;
284 root_block
->size
|= DM_BLOCK_FREE
285 #ifdef __DOSMEM_DEBUG__
290 TRACE( "DOS conventional memory initialized, %d bytes free.\n",
291 DOSMEM_Available() );
295 /**********************************************************************
298 * Setup the first megabyte for DOS memory access
300 static void setup_dos_mem( int dos_init
)
303 int page_size
= getpagesize();
306 if (wine_mmap_is_in_reserved_area( NULL
, 0x110000 ) != 1)
308 addr
= wine_anon_mmap( (void *)page_size
, 0x110000-page_size
,
309 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
310 if (addr
== (void *)page_size
) addr
= NULL
; /* we got what we wanted */
311 else munmap( addr
, 0x110000 - page_size
);
316 /* now map from address 0 */
317 wine_anon_mmap( NULL
, 0x110000, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_FIXED
);
319 /* inform the memory manager that there is a mapping here */
320 VirtualAlloc( addr
, 0x110000, MEM_RESERVE
| MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
322 /* protect the first 64K to catch NULL pointers */
325 VirtualProtect( addr
, 0x10000, PAGE_NOACCESS
, NULL
);
326 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
327 sys_offset
+= 0xf0000;
332 ERR("Cannot use first megabyte for DOS address space, please report\n" );
333 if (dos_init
) ExitProcess(1);
334 /* allocate the DOS area somewhere else */
335 addr
= VirtualAlloc( NULL
, 0x110000, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
338 ERR( "Cannot allocate DOS memory\n" );
342 DOSMEM_dosmem
= addr
;
343 DOSMEM_sysmem
= (char*)addr
+ sys_offset
;
347 /***********************************************************************
350 * Create the dos memory segments, and store them into the KERNEL
353 BOOL
DOSMEM_Init(BOOL dos_init
)
355 static int already_done
, already_mapped
;
359 setup_dos_mem( dos_init
);
361 DOSMEM_0000H
= GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_sysmem
,
362 0x10000, 0, WINE_LDT_FLAGS_DATA
);
363 DOSMEM_BiosDataSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_sysmem
+ 0x400,
364 0x100, 0, WINE_LDT_FLAGS_DATA
);
365 DOSMEM_BiosSysSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_dosmem
+0xf0000,
366 0x10000, 0, WINE_LDT_FLAGS_DATA
);
367 DOSMEM_FillBiosSegments();
368 DOSMEM_FillIsrTable();
371 else if (dos_init
&& !already_mapped
)
375 ERR( "Needs access to the first megabyte for DOS mode\n" );
378 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
379 " NULL pointer accesses will no longer be caught.\n" );
380 VirtualProtect( NULL
, 0x10000, PAGE_EXECUTE_READWRITE
, NULL
);
381 /* copy the BIOS and ISR area down */
382 memcpy( DOSMEM_dosmem
, DOSMEM_sysmem
, 0x400 + 0x100 );
383 DOSMEM_sysmem
= DOSMEM_dosmem
;
384 SetSelectorBase( DOSMEM_0000H
, 0 );
385 SetSelectorBase( DOSMEM_BiosDataSeg
, 0x400 );
386 /* we may now need the actual interrupt stubs, and since we've just moved the
387 * interrupt vector table away, we can fill the area with stubs instead... */
388 DOSMEM_MakeIsrStubs();
395 /***********************************************************************
398 * Increment the BIOS tick counter. Called by timer signal handler.
400 void DOSMEM_Tick( WORD timer
)
402 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
403 if (pBiosData
) pBiosData
->Ticks
++;
406 /***********************************************************************
409 * Carve a chunk of the DOS memory block (without selector).
411 LPVOID
DOSMEM_GetBlock(UINT size
, UINT16
* pseg
)
415 dosmem_info
*info_block
= DOSMEM_InfoBlock();
417 #ifdef __DOSMEM_DEBUG_
418 dosmem_entry
*prev
= NULL
;
421 if( size
> info_block
->free
) return NULL
;
422 dm
= DOSMEM_RootBlock();
424 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
426 #ifdef __DOSMEM_DEBUG__
427 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
429 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
434 if( dm
->size
& DM_BLOCK_FREE
)
436 dosmem_entry
*next
= NEXT_BLOCK(dm
);
438 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
440 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
441 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
442 next
= NEXT_BLOCK(dm
);
445 blocksize
= dm
->size
& DM_BLOCK_MASK
;
446 if( blocksize
>= size
)
448 block
= ((char*)dm
) + sizeof(dosmem_entry
);
449 if( blocksize
- size
> 0x20 )
451 /* split dm so that the next one stays
452 * paragraph-aligned (and dm loses free bit) */
454 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
455 sizeof(dosmem_entry
));
456 next
= (dosmem_entry
*)(((char*)dm
) +
457 sizeof(dosmem_entry
) + dm
->size
);
458 next
->size
= (blocksize
- (dm
->size
+
459 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
460 #ifdef __DOSMEM_DEBUG__
464 } else dm
->size
&= DM_BLOCK_MASK
;
466 info_block
->blocks
++;
467 info_block
->free
-= dm
->size
;
468 if( pseg
) *pseg
= (block
- DOSMEM_dosmem
) >> 4;
469 #ifdef __DOSMEM_DEBUG__
470 dm
->size
|= DM_BLOCK_DEBUG
;
476 else dm
= NEXT_BLOCK(dm
);
478 return (LPVOID
)block
;
481 /***********************************************************************
484 BOOL
DOSMEM_FreeBlock(void* ptr
)
486 dosmem_info
*info_block
= DOSMEM_InfoBlock();
488 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
489 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
490 - DOSMEM_dosmem
) & 0xf) )
492 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
494 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
495 #ifdef __DOSMEM_DEBUG__
496 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
500 info_block
->blocks
--;
501 info_block
->free
+= dm
->size
;
503 dm
->size
|= DM_BLOCK_FREE
;
510 /***********************************************************************
513 * Resize DOS memory block in place. Returns block size or -1 on error.
515 * If exact is TRUE, returned value is either old or requested block
516 * size. If exact is FALSE, block is expanded even if there is not
517 * enough space for full requested block size.
519 UINT
DOSMEM_ResizeBlock(void *ptr
, UINT size
, BOOL exact
)
522 dosmem_info
*info_block
= DOSMEM_InfoBlock();
528 if( (ptr
< (void*)(sizeof(dosmem_entry
) + (char*)DOSMEM_RootBlock())) ||
529 (ptr
>= (void*)DOSMEM_MemoryTop()) ||
530 (((((char*)ptr
) - DOSMEM_dosmem
) & 0xf) != 0) )
533 dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
534 if( dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
) )
537 next
= NEXT_BLOCK(dm
);
538 orgsize
= dm
->size
& DM_BLOCK_MASK
;
540 /* collapse free blocks */
541 while( next
->size
& DM_BLOCK_FREE
)
543 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
544 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
545 next
= NEXT_BLOCK(dm
);
548 blocksize
= dm
->size
& DM_BLOCK_MASK
;
551 * If collapse didn't help we either expand block to maximum
552 * available size (exact == FALSE) or give collapsed blocks
553 * back to free storage (exact == TRUE).
555 if (blocksize
< size
)
556 size
= exact
? orgsize
: blocksize
;
558 block
= ((char*)dm
) + sizeof(dosmem_entry
);
559 if( blocksize
- size
> 0x20 )
562 * split dm so that the next one stays
563 * paragraph-aligned (and next gains free bit)
566 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
567 sizeof(dosmem_entry
));
568 next
= (dosmem_entry
*)(((char*)dm
) +
569 sizeof(dosmem_entry
) + dm
->size
);
570 next
->size
= (blocksize
- (dm
->size
+
571 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
;
575 dm
->size
&= DM_BLOCK_MASK
;
579 * Adjust available memory if block size changes.
581 info_block
->free
+= orgsize
- dm
->size
;
586 /***********************************************************************
589 UINT
DOSMEM_Available(void)
591 UINT blocksize
, available
= 0;
594 dm
= DOSMEM_RootBlock();
596 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
598 #ifdef __DOSMEM_DEBUG__
599 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
601 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
606 if( dm
->size
& DM_BLOCK_FREE
)
608 dosmem_entry
*next
= NEXT_BLOCK(dm
);
610 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
612 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
613 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
614 next
= NEXT_BLOCK(dm
);
617 blocksize
= dm
->size
& DM_BLOCK_MASK
;
618 if ( blocksize
> available
) available
= blocksize
;
621 else dm
= NEXT_BLOCK(dm
);
627 /***********************************************************************
628 * DOSMEM_MapLinearToDos
630 * Linear address to the DOS address space.
632 UINT
DOSMEM_MapLinearToDos(LPVOID ptr
)
634 if (((char*)ptr
>= DOSMEM_dosmem
) &&
635 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
636 return (UINT
)ptr
- (UINT
)DOSMEM_dosmem
;
641 /***********************************************************************
642 * DOSMEM_MapDosToLinear
644 * DOS linear address to the linear address space.
646 LPVOID
DOSMEM_MapDosToLinear(UINT ptr
)
648 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT
)DOSMEM_dosmem
);
653 /***********************************************************************
654 * DOSMEM_MapRealToLinear
656 * Real mode DOS address into a linear pointer
658 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
662 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
663 TRACE_(selector
)("(0x%08lx) returns %p.\n", x
, lin
);
667 /***********************************************************************
668 * DOSMEM_AllocSelector
670 * Allocates a protected mode selector for a realmode segment.
672 WORD
DOSMEM_AllocSelector(WORD realsel
)
674 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
677 sel
=GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_dosmem
+realsel
*16, 0x10000,
678 hModule
, WINE_LDT_FLAGS_DATA
);
679 TRACE_(selector
)("(0x%04x) returns 0x%04x.\n", realsel
,sel
);