Small fixes.
[wine/multimedia.git] / msdos / dosmem.c
blobeaf67b5cae7d610cfc1c10e1fbb88f12b2d339d7
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
6 */
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "windows.h"
12 #include "winbase.h"
13 #include "global.h"
14 #include "ldt.h"
15 #include "miscemu.h"
16 #include "vga.h"
17 #include "module.h"
18 #include "task.h"
19 #include "debug.h"
21 HANDLE16 DOSMEM_BiosSeg; /* BIOS data segment at 0x40:0 */
23 #pragma pack(1)
25 typedef struct
27 WORD Com1Addr; /* 00: COM1 I/O address */
28 WORD Com2Addr; /* 02: COM2 I/O address */
29 WORD Com3Addr; /* 04: COM3 I/O address */
30 WORD Com4Addr; /* 06: COM4 I/O address */
31 WORD Lpt1Addr; /* 08: LPT1 I/O address */
32 WORD Lpt2Addr; /* 0a: LPT2 I/O address */
33 WORD Lpt3Addr; /* 0c: LPT3 I/O address */
34 WORD Lpt4Addr; /* 0e: LPT4 I/O address */
35 WORD InstalledHardware; /* 10: Installed hardware flags */
36 BYTE POSTstatus; /* 12: Power-On Self Test status */
37 WORD MemSize WINE_PACKED; /* 13: Base memory size in Kb */
38 WORD unused1 WINE_PACKED; /* 15: Manufacturing test scratch pad */
39 BYTE KbdFlags1; /* 17: Keyboard flags 1 */
40 BYTE KbdFlags2; /* 18: Keyboard flags 2 */
41 BYTE unused2; /* 19: Keyboard driver workspace */
42 WORD NextKbdCharPtr; /* 1a: Next character in kbd buffer */
43 WORD FirstKbdCharPtr; /* 1c: First character in kbd buffer */
44 WORD KbdBuffer[16]; /* 1e: Keyboard buffer */
45 BYTE DisketteStatus1; /* 3e: Diskette recalibrate status */
46 BYTE DisketteStatus2; /* 3f: Diskette motor status */
47 BYTE DisketteStatus3; /* 40: Diskette motor timeout */
48 BYTE DisketteStatus4; /* 41: Diskette last operation status */
49 BYTE DiskStatus[7]; /* 42: Disk status/command bytes */
50 BYTE VideoMode; /* 49: Video mode */
51 WORD VideoColumns; /* 4a: Number of columns */
52 WORD VideoPageSize; /* 4c: Video page size in bytes */
53 WORD VideoPageStartAddr; /* 4e: Video page start address */
54 BYTE VideoCursorPos[16]; /* 50: Cursor position for 8 pages */
55 WORD VideoCursorType; /* 60: Video cursor type */
56 BYTE VideoCurPage; /* 62: Video current page */
57 WORD VideoCtrlAddr WINE_PACKED; /* 63: Video controller address */
58 BYTE VideoReg1; /* 65: Video mode select register */
59 BYTE VideoReg2; /* 66: Video CGA palette register */
60 DWORD ResetEntry WINE_PACKED; /* 67: Warm reset entry point */
61 BYTE LastIRQ; /* 6b: Last unexpected interrupt */
62 DWORD Ticks; /* 6c: Ticks since midnight */
63 BYTE TicksOverflow; /* 70: Timer overflow if past midnight */
64 BYTE CtrlBreakFlag; /* 71: Ctrl-Break flag */
65 WORD ResetFlag; /* 72: POST Reset flag */
66 BYTE DiskOpStatus; /* 74: Last hard-disk operation status */
67 BYTE NbHardDisks; /* 75: Number of hard disks */
68 BYTE DiskCtrlByte; /* 76: Disk control byte */
69 BYTE DiskIOPort; /* 77: Disk I/O port offset */
70 BYTE LptTimeout[4]; /* 78: Timeouts for parallel ports */
71 BYTE ComTimeout[4]; /* 7c: Timeouts for serial ports */
72 WORD KbdBufferStart; /* 80: Keyboard buffer start */
73 WORD KbdBufferEnd; /* 82: Keyboard buffer end */
74 } BIOSDATA;
76 #pragma pack(4)
78 static BIOSDATA *pBiosData = NULL;
79 static char *DOSMEM_dosmem;
81 DWORD DOSMEM_CollateTable;
83 DWORD DOSMEM_ErrorCall;
84 DWORD DOSMEM_ErrorBuffer;
86 /* use 2 low bits of 'size' for the housekeeping */
88 #define DM_BLOCK_DEBUG 0xABE00000
89 #define DM_BLOCK_TERMINAL 0x00000001
90 #define DM_BLOCK_FREE 0x00000002
91 #define DM_BLOCK_MASK 0x001FFFFC
94 #define __DOSMEM_DEBUG__
97 typedef struct {
98 unsigned size;
99 } dosmem_entry;
101 typedef struct {
102 unsigned blocks;
103 unsigned free;
104 } dosmem_info;
106 #define NEXT_BLOCK(block) \
107 (dosmem_entry*)(((char*)(block)) + \
108 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
110 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
111 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
113 /***********************************************************************
114 * DOSMEM_MemoryBase
116 * Gets the DOS memory base.
118 char *DOSMEM_MemoryBase(HMODULE16 hModule)
120 TDB *pTask = hModule ? NULL : (TDB *)GlobalLock16( GetCurrentTask() );
121 NE_MODULE *pModule = (hModule || pTask) ? NE_GetPtr( hModule ? hModule : pTask->hModule ) : NULL;
123 GlobalUnlock16( GetCurrentTask() );
124 if (pModule && pModule->dos_image)
125 return pModule->dos_image;
126 else
127 return DOSMEM_dosmem;
130 /***********************************************************************
131 * DOSMEM_MemoryTop
133 * Gets the DOS memory top.
135 static char *DOSMEM_MemoryTop(HMODULE16 hModule)
137 return DOSMEM_MemoryBase(hModule)+0x9FFFC; /* 640K */
140 /***********************************************************************
141 * DOSMEM_InfoBlock
143 * Gets the DOS memory info block.
145 static dosmem_info *DOSMEM_InfoBlock(HMODULE16 hModule)
147 return (dosmem_info*)(DOSMEM_MemoryBase(hModule)+0x10000); /* 64K */
150 /***********************************************************************
151 * DOSMEM_RootBlock
153 * Gets the DOS memory root block.
155 static dosmem_entry *DOSMEM_RootBlock(HMODULE16 hModule)
157 /* first block has to be paragraph-aligned */
158 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock(hModule)) +
159 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
162 /***********************************************************************
163 * DOSMEM_FillIsrTable
165 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
167 * NOTES:
168 * Linux normally only traps INTs performed from or destined to BIOSSEG
169 * for us to handle, if the int_revectored table is empty. Filling the
170 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
171 * to hook interrupts, as well as use their familiar retf tricks to call
172 * them, AND let Wine handle any unhooked interrupts transparently.
174 static void DOSMEM_FillIsrTable(HMODULE16 hModule)
176 SEGPTR *isr = (SEGPTR*)DOSMEM_MemoryBase(hModule);
177 DWORD *stub = (DWORD*)((char*)isr + (VM_STUB_SEGMENT << 4));
178 int x;
180 for (x=0; x<256; x++) isr[x]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT,x*4);
181 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
184 /***********************************************************************
185 * DOSMEM_InitDPMI
187 * Allocate the global DPMI RMCB wrapper.
189 static void DOSMEM_InitDPMI(void)
191 extern UINT16 DPMI_wrap_seg;
192 static char wrap_code[]={
193 0xCD,0x31, /* int $0x31 */
194 0xCB /* lret */
196 LPSTR wrapper = (LPSTR)DOSMEM_GetBlock(0, sizeof(wrap_code), &DPMI_wrap_seg);
198 memcpy(wrapper, wrap_code, sizeof(wrap_code));
201 /***********************************************************************
202 * DOSMEM_FillBiosSegment
204 * Fill the BIOS data segment with dummy values.
206 static void DOSMEM_FillBiosSegment(void)
208 pBiosData = (BIOSDATA *)GlobalLock16( DOSMEM_BiosSeg );
210 /* Clear all unused values */
211 memset( pBiosData, 0, sizeof(*pBiosData) );
213 /* FIXME: should check the number of configured drives and ports */
215 pBiosData->Com1Addr = 0x3e8;
216 pBiosData->Com2Addr = 0x2e8;
217 pBiosData->Lpt1Addr = 0x378;
218 pBiosData->Lpt2Addr = 0x278;
219 pBiosData->InstalledHardware = 0x8443;
220 pBiosData->MemSize = 640;
221 pBiosData->NextKbdCharPtr = 0x1e;
222 pBiosData->FirstKbdCharPtr = 0x1e;
223 pBiosData->VideoMode = 3;
224 pBiosData->VideoColumns = 80;
225 pBiosData->VideoPageSize = 80 * 25 * 2;
226 pBiosData->VideoPageStartAddr = 0xb800;
227 pBiosData->VideoCtrlAddr = 0x3d4;
228 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
229 pBiosData->NbHardDisks = 2;
230 pBiosData->KbdBufferStart = 0x1e;
231 pBiosData->KbdBufferEnd = 0x3e;
234 /***********************************************************************
235 * DOSMEM_InitCollateTable
237 * Initialises the collate table (character sorting, language dependent)
239 static void DOSMEM_InitCollateTable()
241 DWORD x;
242 unsigned char *tbl;
243 int i;
245 x = GlobalDOSAlloc(258);
246 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
247 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
248 *(WORD*)tbl = 0x100;
249 tbl += 2;
250 for ( i = 0; i < 0x100; i++) *tbl++ = i;
253 /***********************************************************************
254 * DOSMEM_InitErrorTable
256 * Initialises the error tables (DOS 5+)
258 static void DOSMEM_InitErrorTable()
260 DWORD x;
261 char *call;
263 /* We will use a snippet of real mode code that calls */
264 /* a WINE-only interrupt to handle moving the requested */
265 /* message into the buffer... */
267 /* FIXME - There is still something wrong... */
269 /* FIXME - Find hex values for opcodes...
271 (On call, AX contains message number
272 DI contains 'offset' (??)
273 Resturn, ES:DI points to counted string )
275 PUSH BX
276 MOV BX, AX
277 MOV AX, (arbitrary subfunction number)
278 INT (WINE-only interrupt)
279 POP BX
284 const int code = 4;
285 const int buffer = 80;
286 const int SIZE_TO_ALLOCATE = code + buffer;
288 /* FIXME - Complete rewrite of the table system to save */
289 /* precious DOS space. Now, we return the 0001:???? as */
290 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
291 /* as a special case and programs will use the alternate */
292 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
293 /* 0x08) which lets us have a smaller memory footprint anyway. */
295 x = GlobalDOSAlloc(SIZE_TO_ALLOCATE);
297 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
298 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
300 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
302 memset(call, 0, SIZE_TO_ALLOCATE);
304 /* Fixme - Copy assembly into buffer here */
307 /***********************************************************************
308 * DOSMEM_InitMemory
310 * Initialises the DOS memory structures.
312 static void DOSMEM_InitMemory(HMODULE16 hModule)
314 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
315 * 1000:0000 and ends at 9FFF:FFEF. */
317 dosmem_info* info_block = DOSMEM_InfoBlock(hModule);
318 dosmem_entry* root_block = DOSMEM_RootBlock(hModule);
319 dosmem_entry* dm;
321 root_block->size = DOSMEM_MemoryTop(hModule) - (((char*)root_block) + sizeof(dosmem_entry));
323 info_block->blocks = 0;
324 info_block->free = root_block->size;
326 dm = NEXT_BLOCK(root_block);
327 dm->size = DM_BLOCK_TERMINAL;
328 root_block->size |= DM_BLOCK_FREE
329 #ifdef __DOSMEM_DEBUG__
330 | DM_BLOCK_DEBUG;
331 #endif
335 /***********************************************************************
336 * DOSMEM_Init
338 * Create the dos memory segments, and store them into the KERNEL
339 * exported values.
341 BOOL32 DOSMEM_Init(HMODULE16 hModule)
343 if (!hModule)
345 /* Allocate 1 MB dosmemory
346 * - it is mostly wasted but we use can some of it to
347 * store internal translation tables, etc...
349 DOSMEM_dosmem = VirtualAlloc( NULL, 0x100000, MEM_COMMIT,
350 PAGE_EXECUTE_READWRITE );
351 if (!DOSMEM_dosmem)
353 WARN(dosmem, "Could not allocate DOS memory.\n" );
354 return FALSE;
356 DOSMEM_BiosSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0x400,0x100,
357 0, FALSE, FALSE, FALSE, NULL );
358 DOSMEM_FillIsrTable(0);
359 DOSMEM_FillBiosSegment();
360 DOSMEM_InitMemory(0);
361 DOSMEM_InitCollateTable();
362 DOSMEM_InitErrorTable();
363 DOSMEM_InitDPMI();
365 else
367 #if 0
368 DOSMEM_FillIsrTable(hModule);
369 DOSMEM_InitMemory(hModule);
370 #else
371 /* bootstrap the new V86 task with a copy of the "system" memory */
372 memcpy(DOSMEM_MemoryBase(hModule), DOSMEM_dosmem, 0x100000);
373 #endif
375 return TRUE;
379 /***********************************************************************
380 * DOSMEM_Tick
382 * Increment the BIOS tick counter. Called by timer signal handler.
384 void DOSMEM_Tick( WORD timer )
386 if (pBiosData) pBiosData->Ticks++;
389 /***********************************************************************
390 * DOSMEM_GetBlock
392 * Carve a chunk of the DOS memory block (without selector).
394 LPVOID DOSMEM_GetBlock(HMODULE16 hModule, UINT32 size, UINT16* pseg)
396 UINT32 blocksize;
397 char *block = NULL;
398 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
399 dosmem_entry *dm;
400 #ifdef __DOSMEM_DEBUG_
401 dosmem_entry *prev = NULL;
402 #endif
404 if( size > info_block->free ) return NULL;
405 dm = DOSMEM_RootBlock(hModule);
407 while (dm && dm->size != DM_BLOCK_TERMINAL)
409 #ifdef __DOSMEM_DEBUG__
410 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
412 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT32)prev);
413 return NULL;
415 prev = dm;
416 #endif
417 if( dm->size & DM_BLOCK_FREE )
419 dosmem_entry *next = NEXT_BLOCK(dm);
421 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
423 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
424 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
425 next = NEXT_BLOCK(dm);
428 blocksize = dm->size & DM_BLOCK_MASK;
429 if( blocksize >= size )
431 block = ((char*)dm) + sizeof(dosmem_entry);
432 if( blocksize - size > 0x20 )
434 /* split dm so that the next one stays
435 * paragraph-aligned (and dm loses free bit) */
437 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
438 sizeof(dosmem_entry));
439 next = (dosmem_entry*)(((char*)dm) +
440 sizeof(dosmem_entry) + dm->size);
441 next->size = (blocksize - (dm->size +
442 sizeof(dosmem_entry))) | DM_BLOCK_FREE
443 #ifdef __DOSMEM_DEBUG__
444 | DM_BLOCK_DEBUG
445 #endif
447 } else dm->size &= DM_BLOCK_MASK;
449 info_block->blocks++;
450 info_block->free -= dm->size;
451 if( pseg ) *pseg = (block - DOSMEM_MemoryBase(hModule)) >> 4;
452 #ifdef __DOSMEM_DEBUG__
453 dm->size |= DM_BLOCK_DEBUG;
454 #endif
455 break;
457 dm = next;
459 else dm = NEXT_BLOCK(dm);
461 return (LPVOID)block;
464 /***********************************************************************
465 * DOSMEM_FreeBlock
467 BOOL32 DOSMEM_FreeBlock(HMODULE16 hModule, void* ptr)
469 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
471 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
472 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
473 - DOSMEM_MemoryBase(hModule)) & 0xf) )
475 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
477 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
478 #ifdef __DOSMEM_DEBUG__
479 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
480 #endif
483 info_block->blocks--;
484 info_block->free += dm->size;
486 dm->size |= DM_BLOCK_FREE;
487 return TRUE;
490 return FALSE;
493 /***********************************************************************
494 * DOSMEM_ResizeBlock
496 LPVOID DOSMEM_ResizeBlock(HMODULE16 hModule, void* ptr, UINT32 size, UINT16* pseg)
498 char *block = NULL;
499 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
501 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
502 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
503 - DOSMEM_MemoryBase(hModule)) & 0xf) )
505 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
507 if( pseg ) *pseg = ((char*)ptr - DOSMEM_MemoryBase(hModule)) >> 4;
509 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
512 dosmem_entry *next = NEXT_BLOCK(dm);
513 UINT32 blocksize, orgsize = dm->size & DM_BLOCK_MASK;
515 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
517 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
518 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
519 next = NEXT_BLOCK(dm);
522 blocksize = dm->size & DM_BLOCK_MASK;
523 if (blocksize >= size)
525 block = ((char*)dm) + sizeof(dosmem_entry);
526 if( blocksize - size > 0x20 )
528 /* split dm so that the next one stays
529 * paragraph-aligned (and next gains free bit) */
531 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
532 sizeof(dosmem_entry));
533 next = (dosmem_entry*)(((char*)dm) +
534 sizeof(dosmem_entry) + dm->size);
535 next->size = (blocksize - (dm->size +
536 sizeof(dosmem_entry))) | DM_BLOCK_FREE
538 } else dm->size &= DM_BLOCK_MASK;
540 info_block->free += orgsize - dm->size;
541 } else {
542 block = DOSMEM_GetBlock(hModule, size, pseg);
543 if (block) {
544 info_block->blocks--;
545 info_block->free += dm->size;
547 dm->size |= DM_BLOCK_FREE;
552 return (LPVOID)block;
556 /***********************************************************************
557 * DOSMEM_Available
559 UINT32 DOSMEM_Available(HMODULE16 hModule)
561 UINT32 blocksize, available = 0;
562 dosmem_entry *dm;
564 dm = DOSMEM_RootBlock(hModule);
566 while (dm && dm->size != DM_BLOCK_TERMINAL)
568 #ifdef __DOSMEM_DEBUG__
569 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
571 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT32)prev);
572 return NULL;
574 prev = dm;
575 #endif
576 if( dm->size & DM_BLOCK_FREE )
578 dosmem_entry *next = NEXT_BLOCK(dm);
580 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
582 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
583 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
584 next = NEXT_BLOCK(dm);
587 blocksize = dm->size & DM_BLOCK_MASK;
588 if ( blocksize > available ) available = blocksize;
589 dm = next;
591 else dm = NEXT_BLOCK(dm);
593 return available;
597 /***********************************************************************
598 * DOSMEM_MapLinearToDos
600 * Linear address to the DOS address space.
602 UINT32 DOSMEM_MapLinearToDos(LPVOID ptr)
604 if (((char*)ptr >= DOSMEM_MemoryBase(0)) &&
605 ((char*)ptr < DOSMEM_MemoryBase(0) + 0x100000))
606 return (UINT32)ptr - (UINT32)DOSMEM_MemoryBase(0);
607 return (UINT32)ptr;
611 /***********************************************************************
612 * DOSMEM_MapDosToLinear
614 * DOS linear address to the linear address space.
616 LPVOID DOSMEM_MapDosToLinear(UINT32 ptr)
618 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT32)DOSMEM_MemoryBase(0));
619 return (LPVOID)ptr;
623 /***********************************************************************
624 * DOSMEM_MapRealToLinear
626 * Real mode DOS address into a linear pointer
628 LPVOID DOSMEM_MapRealToLinear(DWORD x)
630 LPVOID lin;
632 lin=DOSMEM_MemoryBase(0)+(x&0xffff)+(((x&0xffff0000)>>16)*16);
633 TRACE(selector,"(0x%08lx) returns 0x%p.\n",
634 x,lin );
635 return lin;
638 /***********************************************************************
639 * DOSMEM_AllocSelector
641 * Allocates a protected mode selector for a realmode segment.
643 WORD DOSMEM_AllocSelector(WORD realsel)
645 HMODULE16 hModule = GetModuleHandle16("KERNEL");
646 WORD sel;
648 sel=GLOBAL_CreateBlock(
649 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
650 hModule,FALSE,FALSE,FALSE,NULL
652 TRACE(selector,"(0x%04x) returns 0x%04x.\n",
653 realsel,sel
655 return sel;