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>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
41 #include "wine/winbase16.h"
44 #include "selectors.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
49 WINE_DECLARE_DEBUG_CHANNEL(selector);
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 */
55 /* use 2 low bits of 'size' for the housekeeping */
57 #define DM_BLOCK_DEBUG 0xABE00000
58 #define DM_BLOCK_TERMINAL 0x00000001
59 #define DM_BLOCK_FREE 0x00000002
60 #define DM_BLOCK_MASK 0x001FFFFC
63 #define __DOSMEM_DEBUG__
75 #define NEXT_BLOCK(block) \
76 (dosmem_entry*)(((char*)(block)) + \
77 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
79 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
80 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
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;
90 /* Start of DOS conventional memory */
91 static char *DOSMEM_membase;
93 static void DOSMEM_InitMemory(void);
95 /***********************************************************************
98 * Gets the DOS memory top.
100 static char *DOSMEM_MemoryTop(void)
102 return DOSMEM_dosmem+0x9FFFC; /* 640K */
105 /***********************************************************************
108 * Gets the DOS memory info block.
110 static dosmem_info *DOSMEM_InfoBlock(void)
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
123 if (DOSMEM_dosmem != DOSMEM_sysmem)
124 reserve = 0x10000; /* 64k */
126 reserve = 0x600; /* 1.5k */
129 * Round to paragraph boundary in order to make
130 * sure the alignment is correct.
132 reserve = ((reserve + 15) >> 4) << 4;
135 * Set DOS memory base and initialize conventional memory.
137 DOSMEM_membase = DOSMEM_dosmem + reserve;
141 return (dosmem_info*)DOSMEM_membase;
144 /***********************************************************************
147 * Gets the DOS memory root block.
149 static dosmem_entry *DOSMEM_RootBlock(void)
151 /* first block has to be paragraph-aligned */
152 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
153 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
156 /***********************************************************************
157 * DOSMEM_FillIsrTable
159 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
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.
168 static void DOSMEM_FillIsrTable(void)
170 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
173 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
176 static void DOSMEM_MakeIsrStubs(void)
178 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
181 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
184 static BIOSDATA * DOSMEM_BiosData(void)
186 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
189 /**********************************************************************
190 * DOSMEM_GetTicksSinceMidnight
192 * Return number of clock ticks since midnight.
194 static DWORD DOSMEM_GetTicksSinceMidnight(void)
200 /* This should give us the (approximately) correct
201 * 18.206 clock ticks per second since midnight.
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);
211 /***********************************************************************
212 * DOSMEM_FillBiosSegments
214 * Fill the BIOS data segment with dummy values.
216 static void DOSMEM_FillBiosSegments(void)
218 BYTE *pBiosSys = DOSMEM_dosmem + 0xf0000;
219 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
220 BIOSDATA *pBiosData = DOSMEM_BiosData();
222 /* Clear all unused values */
223 memset( pBiosData, 0, sizeof(*pBiosData) );
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;
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 */
263 /* BIOS date string */
264 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
267 *(pBiosSys+0xfffe) = 0xfc;
270 /***********************************************************************
273 * Initialises the DOS memory structures.
275 static void DOSMEM_InitMemory(void)
277 dosmem_info* info_block = DOSMEM_InfoBlock();
278 dosmem_entry* root_block = DOSMEM_RootBlock();
281 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
283 info_block->blocks = 0;
284 info_block->free = root_block->size;
286 dm = NEXT_BLOCK(root_block);
287 dm->size = DM_BLOCK_TERMINAL;
288 root_block->size |= DM_BLOCK_FREE
289 #ifdef __DOSMEM_DEBUG__
294 TRACE( "DOS conventional memory initialized, %d bytes free.\n",
295 DOSMEM_Available() );
299 /**********************************************************************
302 * Setup the first megabyte for DOS memory access
304 static void setup_dos_mem( int dos_init )
307 int page_size = getpagesize();
308 void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
309 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
310 if (addr == (void *)page_size) /* we got what we wanted */
312 /* now map from address 0 */
313 addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
316 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
320 /* inform the memory manager that there is a mapping here */
321 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
323 /* protect the first 64K to catch NULL pointers */
326 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
327 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
328 sys_offset += 0xf0000;
333 ERR("Cannot use first megabyte for DOS address space, please report\n" );
334 if (dos_init) ExitProcess(1);
335 /* allocate the DOS area somewhere else */
336 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
339 ERR( "Cannot allocate DOS memory\n" );
343 DOSMEM_dosmem = addr;
344 DOSMEM_sysmem = (char*)addr + sys_offset;
348 /***********************************************************************
351 * Create the dos memory segments, and store them into the KERNEL
354 BOOL DOSMEM_Init(BOOL dos_init)
356 static int already_done, already_mapped;
360 setup_dos_mem( dos_init );
362 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
363 0x10000, 0, WINE_LDT_FLAGS_DATA );
364 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
365 0x100, 0, WINE_LDT_FLAGS_DATA );
366 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
367 0x10000, 0, WINE_LDT_FLAGS_DATA );
368 DOSMEM_FillBiosSegments();
369 DOSMEM_FillIsrTable();
372 else if (dos_init && !already_mapped)
376 ERR( "Needs access to the first megabyte for DOS mode\n" );
379 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
380 " NULL pointer accesses will no longer be caught.\n" );
381 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
382 /* copy the BIOS and ISR area down */
383 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
384 DOSMEM_sysmem = DOSMEM_dosmem;
385 SetSelectorBase( DOSMEM_0000H, 0 );
386 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
387 /* we may now need the actual interrupt stubs, and since we've just moved the
388 * interrupt vector table away, we can fill the area with stubs instead... */
389 DOSMEM_MakeIsrStubs();
396 /***********************************************************************
399 * Increment the BIOS tick counter. Called by timer signal handler.
401 void DOSMEM_Tick( WORD timer )
403 BIOSDATA *pBiosData = DOSMEM_BiosData();
404 if (pBiosData) pBiosData->Ticks++;
407 /***********************************************************************
410 * Carve a chunk of the DOS memory block (without selector).
412 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
416 dosmem_info *info_block = DOSMEM_InfoBlock();
418 #ifdef __DOSMEM_DEBUG_
419 dosmem_entry *prev = NULL;
422 if( size > info_block->free ) return NULL;
423 dm = DOSMEM_RootBlock();
425 while (dm && dm->size != DM_BLOCK_TERMINAL)
427 #ifdef __DOSMEM_DEBUG__
428 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
430 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
435 if( dm->size & DM_BLOCK_FREE )
437 dosmem_entry *next = NEXT_BLOCK(dm);
439 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
441 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
442 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
443 next = NEXT_BLOCK(dm);
446 blocksize = dm->size & DM_BLOCK_MASK;
447 if( blocksize >= size )
449 block = ((char*)dm) + sizeof(dosmem_entry);
450 if( blocksize - size > 0x20 )
452 /* split dm so that the next one stays
453 * paragraph-aligned (and dm loses free bit) */
455 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
456 sizeof(dosmem_entry));
457 next = (dosmem_entry*)(((char*)dm) +
458 sizeof(dosmem_entry) + dm->size);
459 next->size = (blocksize - (dm->size +
460 sizeof(dosmem_entry))) | DM_BLOCK_FREE
461 #ifdef __DOSMEM_DEBUG__
465 } else dm->size &= DM_BLOCK_MASK;
467 info_block->blocks++;
468 info_block->free -= dm->size;
469 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
470 #ifdef __DOSMEM_DEBUG__
471 dm->size |= DM_BLOCK_DEBUG;
477 else dm = NEXT_BLOCK(dm);
479 return (LPVOID)block;
482 /***********************************************************************
485 BOOL DOSMEM_FreeBlock(void* ptr)
487 dosmem_info *info_block = DOSMEM_InfoBlock();
489 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
490 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
491 - DOSMEM_dosmem) & 0xf) )
493 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
495 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
496 #ifdef __DOSMEM_DEBUG__
497 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
501 info_block->blocks--;
502 info_block->free += dm->size;
504 dm->size |= DM_BLOCK_FREE;
511 /***********************************************************************
514 * Resize DOS memory block in place. Returns block size or -1 on error.
516 * If exact is TRUE, returned value is either old or requested block
517 * size. If exact is FALSE, block is expanded even if there is not
518 * enough space for full requested block size.
520 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
523 dosmem_info *info_block = DOSMEM_InfoBlock();
529 if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) ||
530 (ptr >= (void*)DOSMEM_MemoryTop()) ||
531 (((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) )
534 dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
535 if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) )
538 next = NEXT_BLOCK(dm);
539 orgsize = dm->size & DM_BLOCK_MASK;
541 /* collapse free blocks */
542 while( next->size & DM_BLOCK_FREE )
544 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
545 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
546 next = NEXT_BLOCK(dm);
549 blocksize = dm->size & DM_BLOCK_MASK;
552 * If collapse didn't help we either expand block to maximum
553 * available size (exact == FALSE) or give collapsed blocks
554 * back to free storage (exact == TRUE).
556 if (blocksize < size)
557 size = exact ? orgsize : blocksize;
559 block = ((char*)dm) + sizeof(dosmem_entry);
560 if( blocksize - size > 0x20 )
563 * split dm so that the next one stays
564 * paragraph-aligned (and next gains free bit)
567 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
568 sizeof(dosmem_entry));
569 next = (dosmem_entry*)(((char*)dm) +
570 sizeof(dosmem_entry) + dm->size);
571 next->size = (blocksize - (dm->size +
572 sizeof(dosmem_entry))) | DM_BLOCK_FREE;
576 dm->size &= DM_BLOCK_MASK;
580 * Adjust available memory if block size changes.
582 info_block->free += orgsize - dm->size;
587 /***********************************************************************
590 UINT DOSMEM_Available(void)
592 UINT blocksize, available = 0;
595 dm = DOSMEM_RootBlock();
597 while (dm && dm->size != DM_BLOCK_TERMINAL)
599 #ifdef __DOSMEM_DEBUG__
600 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
602 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
607 if( dm->size & DM_BLOCK_FREE )
609 dosmem_entry *next = NEXT_BLOCK(dm);
611 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
613 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
614 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
615 next = NEXT_BLOCK(dm);
618 blocksize = dm->size & DM_BLOCK_MASK;
619 if ( blocksize > available ) available = blocksize;
622 else dm = NEXT_BLOCK(dm);
628 /***********************************************************************
629 * DOSMEM_MapLinearToDos
631 * Linear address to the DOS address space.
633 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
635 if (((char*)ptr >= DOSMEM_dosmem) &&
636 ((char*)ptr < DOSMEM_dosmem + 0x100000))
637 return (UINT)ptr - (UINT)DOSMEM_dosmem;
642 /***********************************************************************
643 * DOSMEM_MapDosToLinear
645 * DOS linear address to the linear address space.
647 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
649 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
654 /***********************************************************************
655 * DOSMEM_MapRealToLinear
657 * Real mode DOS address into a linear pointer
659 LPVOID DOSMEM_MapRealToLinear(DWORD x)
663 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
664 TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
668 /***********************************************************************
669 * DOSMEM_AllocSelector
671 * Allocates a protected mode selector for a realmode segment.
673 WORD DOSMEM_AllocSelector(WORD realsel)
675 HMODULE16 hModule = GetModuleHandle16("KERNEL");
678 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
679 hModule, WINE_LDT_FLAGS_DATA );
680 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);