push 5c57339901c8d80068fee18365e7574a1e8d922a
[wine/hacks.git] / dlls / winedos / dosmem.c
blob06823f854c35a98da557b9e20b2ba801e9619d94
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "excpt.h"
38 #include "winternl.h"
39 #include "wine/winbase16.h"
41 #include "dosexe.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;
55 * Memory Control Block (MCB) definition
56 * FIXME: implement Allocation Strategy
59 #define MCB_DUMP(mc) \
60 TRACE ("MCB_DUMP base=%p type=%02xh psp=%04xh size=%04xh\n", mc, mc->type, mc->psp , mc->size )
62 #define MCB_NEXT(mc) \
63 (MCB*) ((mc->type==MCB_TYPE_LAST) ? NULL : (char*)(mc) + ((mc->size + 1) << 4) )
65 /* FIXME: should we check more? */
66 #define MCB_VALID(mc) \
67 ((mc->type==MCB_TYPE_NORMAL) || (mc->type==MCB_TYPE_LAST))
70 #define MCB_TYPE_NORMAL 0x4d
71 #define MCB_TYPE_LAST 0x5a
73 #define MCB_PSP_DOS 0x0060
74 #define MCB_PSP_FREE 0
76 #include "pshpack1.h"
77 typedef struct {
78 BYTE type;
79 WORD psp; /* segment of owner psp */
80 WORD size; /* in paragraphs */
81 BYTE pad[3];
82 BYTE name[8];
83 } MCB;
84 #include "poppack.h"
87 #define __DOSMEM_DEBUG__
90 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
91 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
93 /* FIXME: this should be moved to the LOL */
94 static MCB* DOSMEM_root_block;
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_FillIsrTable
109 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
111 * NOTES:
112 * Linux normally only traps INTs performed from or destined to BIOSSEG
113 * for us to handle, if the int_revectored table is empty. Filling the
114 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
115 * to hook interrupts, as well as use their familiar retf tricks to call
116 * them, AND let Wine handle any unhooked interrupts transparently.
118 static void DOSMEM_FillIsrTable(void)
120 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
121 int x;
123 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
126 static void DOSMEM_MakeIsrStubs(void)
128 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
129 int x;
131 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
134 BIOSDATA* DOSVM_BiosData(void)
136 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
139 /**********************************************************************
140 * DOSMEM_GetTicksSinceMidnight
142 * Return number of clock ticks since midnight.
144 static DWORD DOSMEM_GetTicksSinceMidnight(void)
146 SYSTEMTIME time;
148 /* This should give us the (approximately) correct
149 * 18.206 clock ticks per second since midnight.
152 GetLocalTime( &time );
154 return (((time.wHour * 3600 + time.wMinute * 60 +
155 time.wSecond) * 18206) / 1000) +
156 (time.wMilliseconds * 1000 / 54927);
159 /***********************************************************************
160 * DOSMEM_FillBiosSegments
162 * Fill the BIOS data segment with dummy values.
164 static void DOSMEM_FillBiosSegments(void)
166 BYTE *pBiosSys = (BYTE*)DOSMEM_dosmem + 0xf0000;
167 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
168 BIOSDATA *pBiosData = DOSVM_BiosData();
169 static const char bios_date[] = "13/01/99";
171 /* Clear all unused values */
172 memset( pBiosData, 0, sizeof(*pBiosData) );
174 /* FIXME: should check the number of configured drives and ports */
175 pBiosData->Com1Addr = 0x3f8;
176 pBiosData->Com2Addr = 0x2f8;
177 pBiosData->Lpt1Addr = 0x378;
178 pBiosData->Lpt2Addr = 0x278;
179 pBiosData->InstalledHardware = 0x5463;
180 pBiosData->MemSize = 640;
181 pBiosData->NextKbdCharPtr = 0x1e;
182 pBiosData->FirstKbdCharPtr = 0x1e;
183 pBiosData->VideoMode = 3;
184 pBiosData->VideoColumns = 80;
185 pBiosData->VideoPageSize = 80 * 25 * 2;
186 pBiosData->VideoPageStartAddr = 0xb800;
187 pBiosData->VideoCtrlAddr = 0x3d4;
188 pBiosData->Ticks = DOSMEM_GetTicksSinceMidnight();
189 pBiosData->NbHardDisks = 2;
190 pBiosData->KbdBufferStart = 0x1e;
191 pBiosData->KbdBufferEnd = 0x3e;
192 pBiosData->RowsOnScreenMinus1 = 24;
193 pBiosData->BytesPerChar = 0x10;
194 pBiosData->ModeOptions = 0x64;
195 pBiosData->FeatureBitsSwitches = 0xf9;
196 pBiosData->VGASettings = 0x51;
197 pBiosData->DisplayCombination = 0x08;
198 pBiosData->DiskDataRate = 0;
200 /* fill ROM configuration table (values from Award) */
201 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
202 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
203 *(pBiosROMTable+0x2) = 0xfc; /* model */
204 *(pBiosROMTable+0x3) = 0x01; /* submodel */
205 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
206 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
207 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
208 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
209 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
210 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
212 /* BIOS date string */
213 memcpy(pBiosSys+0xfff5, bios_date, sizeof bios_date);
215 /* BIOS ID */
216 *(pBiosSys+0xfffe) = 0xfc;
218 /* Reboot vector (f000:fff0 or ffff:0000) */
219 *(DWORD*)(pBiosSys + 0xfff0) = VM_STUB(0x19);
222 /***********************************************************************
223 * BiosTick
225 * Increment the BIOS tick counter. Called by timer signal handler.
227 static void CALLBACK BiosTick( LPVOID arg, DWORD low, DWORD high )
229 BIOSDATA *pBiosData = arg;
230 pBiosData->Ticks++;
233 /***********************************************************************
234 * timer_thread
236 static DWORD CALLBACK timer_thread( void *arg )
238 LARGE_INTEGER when;
239 HANDLE timer;
241 if (!(timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
243 when.u.LowPart = when.u.HighPart = 0;
244 SetWaitableTimer( timer, &when, 55 /* actually 54.925 */, BiosTick, arg, FALSE );
245 for (;;) SleepEx( INFINITE, TRUE );
248 /***********************************************************************
249 * DOSMEM_Collapse
251 * Helper function for internal use only.
252 * Attach all following free blocks to this one, even if this one is not free.
254 static void DOSMEM_Collapse( MCB* mcb )
256 MCB* next = MCB_NEXT( mcb );
258 while (next && next->psp == MCB_PSP_FREE)
260 mcb->size = mcb->size + next->size + 1;
261 mcb->type = next->type; /* make sure keeping MCB_TYPE_LAST */
262 next = MCB_NEXT( next );
266 /***********************************************************************
267 * DOSMEM_AllocBlock
269 * Carve a chunk of the DOS memory block (without selector).
271 LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg)
273 MCB *curr = DOSMEM_root_block;
274 MCB *next = NULL;
275 WORD psp = DOSVM_psp;
277 if (!psp)
278 psp = MCB_PSP_DOS;
280 *pseg = 0;
282 TRACE( "(%04xh)\n", size );
284 /* round up to paragraph */
285 size = (size + 15) >> 4;
287 #ifdef __DOSMEM_DEBUG__
288 DOSMEM_Available(); /* checks the whole MCB list */
289 #endif
291 /* loop over all MCB and search the next large enough MCB */
292 while (curr)
294 if (!MCB_VALID (curr))
296 ERR( "MCB List Corrupt\n" );
297 MCB_DUMP( curr );
298 return NULL;
300 if (curr->psp == MCB_PSP_FREE)
302 DOSMEM_Collapse( curr );
303 /* is it large enough (one paragraph for the MCB)? */
304 if (curr->size >= size)
306 if (curr->size > size)
308 /* split curr */
309 next = (MCB *) ((char*) curr + ((size+1) << 4));
310 next->psp = MCB_PSP_FREE;
311 next->size = curr->size - (size+1);
312 next->type = curr->type;
313 curr->type = MCB_TYPE_NORMAL;
314 curr->size = size;
316 /* curr is the found block */
317 curr->psp = psp;
318 if( pseg ) *pseg = (((char*)curr) + 16 - DOSMEM_dosmem) >> 4;
319 return (LPVOID) ((char*)curr + 16);
322 curr = MCB_NEXT(curr);
324 return NULL;
327 /***********************************************************************
328 * DOSMEM_FreeBlock
330 BOOL DOSMEM_FreeBlock(void* ptr)
332 MCB* mcb = (MCB*) ((char*)ptr - 16);
334 TRACE( "(%p)\n", ptr );
336 #ifdef __DOSMEM_DEBUG__
337 DOSMEM_Available();
338 #endif
340 if (!MCB_VALID (mcb))
342 ERR( "MCB invalid\n" );
343 MCB_DUMP( mcb );
344 return FALSE;
347 mcb->psp = MCB_PSP_FREE;
348 DOSMEM_Collapse( mcb );
349 return TRUE;
352 /***********************************************************************
353 * DOSMEM_ResizeBlock
355 * Resize DOS memory block in place. Returns block size or -1 on error.
357 * If exact is TRUE, returned value is either old or requested block
358 * size. If exact is FALSE, block is expanded even if there is not
359 * enough space for full requested block size.
361 * TODO: return also biggest block size
363 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
365 MCB* mcb = (MCB*) ((char*)ptr - 16);
366 MCB* next;
368 TRACE( "(%p,%04xh,%s)\n", ptr, size, exact ? "TRUE" : "FALSE" );
370 /* round up to paragraph */
371 size = (size + 15) >> 4;
373 #ifdef __DOSMEM_DEBUG__
374 DOSMEM_Available();
375 #endif
377 if (!MCB_VALID (mcb))
379 ERR( "MCB invalid\n" );
380 MCB_DUMP( mcb );
381 return -1;
384 /* resize needed? */
385 if (mcb->size == size)
386 return size << 4;
388 /* collapse free blocks */
389 DOSMEM_Collapse( mcb );
391 /* shrink mcb ? */
392 if (mcb->size > size)
394 next = (MCB *) ((char*)mcb + ((size+1) << 4));
395 next->type = mcb->type;
396 next->psp = MCB_PSP_FREE;
397 next->size = mcb->size - (size+1);
398 mcb->type = MCB_TYPE_NORMAL;
399 mcb->size = size;
400 return size << 4;
403 if (!exact)
405 return mcb->size << 4;
408 return -1;
411 /***********************************************************************
412 * DOSMEM_Available
414 UINT DOSMEM_Available(void)
416 UINT available = 0;
417 UINT total = 0;
418 MCB *curr = DOSMEM_root_block;
419 /* loop over all MCB and search the largest free MCB */
420 while (curr)
422 #ifdef __DOSMEM_DEBUG__
423 MCB_DUMP( curr );
424 #endif
425 if (!MCB_VALID (curr))
427 ERR( "MCB List Corrupt\n" );
428 MCB_DUMP( curr );
429 return 0;
431 if (curr->psp == MCB_PSP_FREE &&
432 curr->size > available )
433 available = curr->size;
435 total += curr->size + 1;
436 curr = MCB_NEXT( curr );
438 TRACE( " %04xh of %04xh paragraphs available\n", available, total );
439 return available << 4;
442 /***********************************************************************
443 * DOSMEM_InitMemory
445 * Initialises the DOS memory structures.
447 static void DOSMEM_InitMemory(char* addr)
449 DOSMEM_FillBiosSegments();
450 DOSMEM_FillIsrTable();
452 /* align root block to paragraph */
453 DOSMEM_root_block = (MCB*) (( (DWORD_PTR)(addr+0xf) >> 4) << 4);
454 DOSMEM_root_block->type = MCB_TYPE_LAST;
455 DOSMEM_root_block->psp = MCB_PSP_FREE;
456 DOSMEM_root_block->size = (DOSMEM_MemoryTop() - ((char*)DOSMEM_root_block)) >> 4;
458 TRACE("DOS conventional memory initialized, %d bytes free.\n",
459 DOSMEM_Available());
462 /******************************************************************
463 * DOSMEM_InitDosMemory
465 * When WineDOS is loaded, initializes the current DOS memory layout.
467 BOOL DOSMEM_InitDosMemory(void)
469 HMODULE16 hModule;
470 unsigned short sel;
471 LDT_ENTRY entry;
472 DWORD reserve;
474 if (!(hModule = GetModuleHandle16("KERNEL"))) return FALSE;
475 /* KERNEL.194: __F000H */
476 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(ULONG_PTR)194));
477 wine_ldt_get_entry(sel, &entry);
478 DOSMEM_dosmem = (char*)wine_ldt_get_base(&entry) - 0xF0000;
479 /* KERNEL.183: __0000H */
480 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
481 wine_ldt_get_entry(sel, &entry);
482 DOSMEM_sysmem = wine_ldt_get_base(&entry);
485 * Reserve either:
486 * - lowest 64k for NULL pointer catching (Win16)
487 * - lowest 1k for interrupt handlers and
488 * another 0.5k for BIOS, DOS and intra-application
489 * areas (DOS)
491 if (DOSMEM_dosmem != DOSMEM_sysmem)
492 reserve = 0x10000; /* 64k */
493 else
494 reserve = 0x600; /* 1.5k */
497 * Round to paragraph boundary in order to make
498 * sure the alignment is correct.
500 reserve = ((reserve + 15) >> 4) << 4;
503 * Set DOS memory base and initialize conventional memory.
505 DOSMEM_InitMemory(DOSMEM_dosmem + reserve);
507 CloseHandle( CreateThread( NULL, 0, timer_thread, DOSVM_BiosData(), 0, NULL ));
508 return TRUE;
511 /******************************************************************
512 * DOSMEM_MapDosLayout
514 * Initialize the first MB of memory to look like a real DOS setup
516 BOOL DOSMEM_MapDosLayout(void)
518 static int already_mapped;
520 if (!already_mapped)
522 HMODULE16 hModule;
523 unsigned short sel;
524 LDT_ENTRY entry;
526 if (DOSMEM_dosmem || !VirtualProtect( NULL, DOSMEM_SIZE, PAGE_EXECUTE_READWRITE, NULL ))
528 ERR( "Need full access to the first megabyte for DOS mode\n" );
529 ExitProcess(1);
531 /* copy the BIOS and ISR area down */
532 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
533 DOSMEM_sysmem = DOSMEM_dosmem;
534 hModule = GetModuleHandle16("KERNEL");
535 /* selector to 0000H */
536 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
537 wine_ldt_get_entry(sel, &entry);
538 wine_ldt_set_base(&entry, NULL);
539 wine_ldt_set_entry(sel, &entry);
540 /* selector to BiosData */
541 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)193));
542 wine_ldt_get_entry(sel, &entry);
543 wine_ldt_set_base(&entry, (const void*)0x400);
544 wine_ldt_set_entry(sel, &entry);
545 /* we may now need the actual interrupt stubs, and since we've just moved the
546 * interrupt vector table away, we can fill the area with stubs instead... */
547 DOSMEM_MakeIsrStubs();
548 already_mapped = 1;
550 return TRUE;