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>
39 #include "wine/winbase16.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
46 /* DOS memory highest address (including HMA) */
47 #define DOSMEM_SIZE 0x110000
48 #define DOSMEM_64KB 0x10000
50 /* see dlls/kernel/dosmem.c for the details */
51 static char *DOSMEM_dosmem;
52 static char *DOSMEM_sysmem;
54 /* use 2 low bits of 'size' for the housekeeping */
55 #define DM_BLOCK_DEBUG 0xABE00000
56 #define DM_BLOCK_TERMINAL 0x00000001
57 #define DM_BLOCK_FREE 0x00000002
58 #define DM_BLOCK_MASK 0x001FFFFC
61 #define __DOSMEM_DEBUG__
73 static inline dosmem_entry* next_block(dosmem_entry* block)
75 return (dosmem_entry*)((char*)block +
76 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 */
82 /* FIXME: this should be moved to the LOL, and the whole allocation strategy
85 static dosmem_info* DOSMEM_info_block;
87 /***********************************************************************
90 * Gets the DOS memory top.
92 static char *DOSMEM_MemoryTop(void)
94 return DOSMEM_dosmem+0x9FFFC; /* 640K */
97 /***********************************************************************
100 * Gets the DOS memory root block.
102 static dosmem_entry *DOSMEM_RootBlock(void)
104 /* first block has to be paragraph-aligned */
105 return (dosmem_entry*)(((char*)DOSMEM_info_block) +
106 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
109 /***********************************************************************
110 * DOSMEM_FillIsrTable
112 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
115 * Linux normally only traps INTs performed from or destined to BIOSSEG
116 * for us to handle, if the int_revectored table is empty. Filling the
117 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
118 * to hook interrupts, as well as use their familiar retf tricks to call
119 * them, AND let Wine handle any unhooked interrupts transparently.
121 static void DOSMEM_FillIsrTable(void)
123 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
126 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
129 static void DOSMEM_MakeIsrStubs(void)
131 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
134 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
137 BIOSDATA* DOSVM_BiosData(void)
139 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
142 /**********************************************************************
143 * DOSMEM_GetTicksSinceMidnight
145 * Return number of clock ticks since midnight.
147 static DWORD DOSMEM_GetTicksSinceMidnight(void)
151 /* This should give us the (approximately) correct
152 * 18.206 clock ticks per second since midnight.
155 GetLocalTime( &time );
157 return (((time.wHour * 3600 + time.wMinute * 60 +
158 time.wSecond) * 18206) / 1000) +
159 (time.wMilliseconds * 1000 / 54927);
162 /***********************************************************************
163 * DOSMEM_FillBiosSegments
165 * Fill the BIOS data segment with dummy values.
167 static void DOSMEM_FillBiosSegments(void)
169 char *pBiosSys = DOSMEM_dosmem + 0xf0000;
170 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
171 BIOSDATA *pBiosData = DOSVM_BiosData();
173 /* Clear all unused values */
174 memset( pBiosData, 0, sizeof(*pBiosData) );
176 /* FIXME: should check the number of configured drives and ports */
177 pBiosData->Com1Addr = 0x3f8;
178 pBiosData->Com2Addr = 0x2f8;
179 pBiosData->Lpt1Addr = 0x378;
180 pBiosData->Lpt2Addr = 0x278;
181 pBiosData->InstalledHardware = 0x5463;
182 pBiosData->MemSize = 640;
183 pBiosData->NextKbdCharPtr = 0x1e;
184 pBiosData->FirstKbdCharPtr = 0x1e;
185 pBiosData->VideoMode = 3;
186 pBiosData->VideoColumns = 80;
187 pBiosData->VideoPageSize = 80 * 25 * 2;
188 pBiosData->VideoPageStartAddr = 0xb800;
189 pBiosData->VideoCtrlAddr = 0x3d4;
190 pBiosData->Ticks = DOSMEM_GetTicksSinceMidnight();
191 pBiosData->NbHardDisks = 2;
192 pBiosData->KbdBufferStart = 0x1e;
193 pBiosData->KbdBufferEnd = 0x3e;
194 pBiosData->RowsOnScreenMinus1 = 24;
195 pBiosData->BytesPerChar = 0x10;
196 pBiosData->ModeOptions = 0x64;
197 pBiosData->FeatureBitsSwitches = 0xf9;
198 pBiosData->VGASettings = 0x51;
199 pBiosData->DisplayCombination = 0x08;
200 pBiosData->DiskDataRate = 0;
202 /* fill ROM configuration table (values from Award) */
203 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
204 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
205 *(pBiosROMTable+0x2) = 0xfc; /* model */
206 *(pBiosROMTable+0x3) = 0x01; /* submodel */
207 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
208 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
209 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
210 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
211 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
212 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
214 /* BIOS date string */
215 strcpy(pBiosSys+0xfff5, "13/01/99");
218 *(pBiosSys+0xfffe) = 0xfc;
220 /* Reboot vector (f000:fff0 or ffff:0000) */
221 *(DWORD*)(pBiosSys + 0xfff0) = VM_STUB(0x19);
224 /***********************************************************************
227 * Increment the BIOS tick counter. Called by timer signal handler.
229 void BiosTick( WORD timer )
231 BIOSDATA *pBiosData = DOSVM_BiosData();
232 if (pBiosData) pBiosData->Ticks++;
235 /***********************************************************************
238 * Carve a chunk of the DOS memory block (without selector).
240 LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg)
244 dosmem_info *info_block = DOSMEM_info_block;
246 #ifdef __DOSMEM_DEBUG_
247 dosmem_entry *prev = NULL;
250 if( size > info_block->free ) return NULL;
251 dm = DOSMEM_RootBlock();
253 while (dm && dm->size != DM_BLOCK_TERMINAL)
255 #ifdef __DOSMEM_DEBUG__
256 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
258 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
263 if( dm->size & DM_BLOCK_FREE )
265 dosmem_entry *next = next_block(dm);
267 while ( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
269 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
270 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
271 next = next_block(dm);
274 blocksize = dm->size & DM_BLOCK_MASK;
275 if( blocksize >= size )
277 block = ((char*)dm) + sizeof(dosmem_entry);
278 if( blocksize - size > 0x20 )
280 /* split dm so that the next one stays
281 * paragraph-aligned (and dm loses free bit) */
283 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
284 sizeof(dosmem_entry));
285 next = (dosmem_entry*)(((char*)dm) +
286 sizeof(dosmem_entry) + dm->size);
287 next->size = (blocksize - (dm->size +
288 sizeof(dosmem_entry))) | DM_BLOCK_FREE
289 #ifdef __DOSMEM_DEBUG__
293 } else dm->size &= DM_BLOCK_MASK;
295 info_block->blocks++;
296 info_block->free -= dm->size;
297 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
298 #ifdef __DOSMEM_DEBUG__
299 dm->size |= DM_BLOCK_DEBUG;
305 else dm = next_block(dm);
307 return (LPVOID)block;
310 /***********************************************************************
313 BOOL DOSMEM_FreeBlock(void* ptr)
315 dosmem_info *info_block = DOSMEM_info_block;
317 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
318 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
319 - DOSMEM_dosmem) & 0xf) )
321 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
323 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
324 #ifdef __DOSMEM_DEBUG__
325 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
329 info_block->blocks--;
330 info_block->free += dm->size;
332 dm->size |= DM_BLOCK_FREE;
339 /***********************************************************************
342 * Resize DOS memory block in place. Returns block size or -1 on error.
344 * If exact is TRUE, returned value is either old or requested block
345 * size. If exact is FALSE, block is expanded even if there is not
346 * enough space for full requested block size.
348 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
351 dosmem_info *info_block = DOSMEM_info_block;
357 if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) ||
358 (ptr >= (void*)DOSMEM_MemoryTop()) ||
359 (((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) )
362 dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
363 if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) )
366 next = next_block(dm);
367 orgsize = dm->size & DM_BLOCK_MASK;
369 /* collapse free blocks */
370 while ( next->size & DM_BLOCK_FREE )
372 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
373 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
374 next = next_block(dm);
377 blocksize = dm->size & DM_BLOCK_MASK;
380 * If collapse didn't help we either expand block to maximum
381 * available size (exact == FALSE) or give collapsed blocks
382 * back to free storage (exact == TRUE).
384 if (blocksize < size)
385 size = exact ? orgsize : blocksize;
387 block = ((char*)dm) + sizeof(dosmem_entry);
388 if( blocksize - size > 0x20 )
391 * split dm so that the next one stays
392 * paragraph-aligned (and next gains free bit)
395 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
396 sizeof(dosmem_entry));
397 next = (dosmem_entry*)(((char*)dm) +
398 sizeof(dosmem_entry) + dm->size);
399 next->size = (blocksize - (dm->size +
400 sizeof(dosmem_entry))) | DM_BLOCK_FREE;
404 dm->size &= DM_BLOCK_MASK;
408 * Adjust available memory if block size changes.
410 info_block->free += orgsize - dm->size;
415 /***********************************************************************
418 UINT DOSMEM_Available(void)
420 UINT blocksize, available = 0;
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 > available ) available = blocksize;
450 else dm = next_block(dm);
455 /***********************************************************************
458 * Initialises the DOS memory structures.
460 static void DOSMEM_InitMemory(char* addr)
462 dosmem_entry* root_block;
465 DOSMEM_FillBiosSegments();
466 DOSMEM_FillIsrTable();
468 DOSMEM_info_block = (dosmem_info*)addr;
469 root_block = DOSMEM_RootBlock();
470 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
472 DOSMEM_info_block->blocks = 0;
473 DOSMEM_info_block->free = root_block->size;
475 dm = next_block(root_block);
476 dm->size = DM_BLOCK_TERMINAL;
477 root_block->size |= DM_BLOCK_FREE
478 #ifdef __DOSMEM_DEBUG__
483 TRACE("DOS conventional memory initialized, %d bytes free.\n",
487 /******************************************************************
488 * DOSMEM_InitDosMemory
490 * When WineDOS is loaded, initializes the current DOS memory layout.
492 void DOSMEM_InitDosMemory(void)
499 hModule = GetModuleHandle16("KERNEL");
500 /* KERNEL.194: __F000H */
501 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(ULONG_PTR)194));
502 wine_ldt_get_entry(sel, &entry);
503 DOSMEM_dosmem = (char*)wine_ldt_get_base(&entry) - 0xF0000;
504 /* KERNEL.183: __0000H */
505 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
506 wine_ldt_get_entry(sel, &entry);
507 DOSMEM_sysmem = wine_ldt_get_base(&entry);
511 * - lowest 64k for NULL pointer catching (Win16)
512 * - lowest 1k for interrupt handlers and
513 * another 0.5k for BIOS, DOS and intra-application
516 if (DOSMEM_dosmem != DOSMEM_sysmem)
517 reserve = 0x10000; /* 64k */
519 reserve = 0x600; /* 1.5k */
522 * Round to paragraph boundary in order to make
523 * sure the alignment is correct.
525 reserve = ((reserve + 15) >> 4) << 4;
528 * Set DOS memory base and initialize conventional memory.
530 DOSMEM_InitMemory(DOSMEM_dosmem + reserve);
533 /******************************************************************
534 * DOSMEM_MapDosLayout
536 * Initialize the first MB of memory to look like a real DOS setup
538 BOOL DOSMEM_MapDosLayout(void)
540 static int already_mapped;
550 ERR( "Needs access to the first megabyte for DOS mode\n" );
553 MESSAGE( "Warning: unprotecting memory to allow real-mode calls.\n"
554 " NULL pointer accesses will no longer be caught.\n" );
555 VirtualProtect( NULL, DOSMEM_SIZE, PAGE_EXECUTE_READWRITE, NULL );
556 /* copy the BIOS and ISR area down */
557 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
558 DOSMEM_sysmem = DOSMEM_dosmem;
559 hModule = GetModuleHandle16("KERNEL");
560 /* selector to 0000H */
561 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
562 wine_ldt_get_entry(sel, &entry);
563 wine_ldt_set_base(&entry, NULL);
564 wine_ldt_set_entry(sel, &entry);
565 /* selector to BiosData */
566 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)193));
567 wine_ldt_get_entry(sel, &entry);
568 wine_ldt_set_base(&entry, (const void*)0x400);
569 wine_ldt_set_entry(sel, &entry);
570 /* we may now need the actual interrupt stubs, and since we've just moved the
571 * interrupt vector table away, we can fill the area with stubs instead... */
572 DOSMEM_MakeIsrStubs();