Debian Codegraph @

src/ints/xms.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: xms.cpp,v 1.55 2009-05-27 09:15:42 qbix79 Exp $ */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "dosbox.h"
24 #include "callback.h"
25 #include "mem.h"
26 #include "regs.h"
27 #include "dos_inc.h"
28 #include "setup.h"
29 #include "inout.h"
30 #include "xms.h"
31 #include "bios.h"
32
33 #define XMS_HANDLES 50 /* 50 XMS Memory Blocks */
34 #define XMS_VERSION 0x0300 /* version 3.00 */
35 #define XMS_DRIVER_VERSION 0x0301 /* my driver version 3.01 */
36
37 #define XMS_GET_VERSION 0x00
38 #define XMS_ALLOCATE_HIGH_MEMORY 0x01
39 #define XMS_FREE_HIGH_MEMORY 0x02
40 #define XMS_GLOBAL_ENABLE_A20 0x03
41 #define XMS_GLOBAL_DISABLE_A20 0x04
42 #define XMS_LOCAL_ENABLE_A20 0x05
43 #define XMS_LOCAL_DISABLE_A20 0x06
44 #define XMS_QUERY_A20 0x07
45 #define XMS_QUERY_FREE_EXTENDED_MEMORY 0x08
46 #define XMS_ALLOCATE_EXTENDED_MEMORY 0x09
47 #define XMS_FREE_EXTENDED_MEMORY 0x0a
48 #define XMS_MOVE_EXTENDED_MEMORY_BLOCK 0x0b
49 #define XMS_LOCK_EXTENDED_MEMORY_BLOCK 0x0c
50 #define XMS_UNLOCK_EXTENDED_MEMORY_BLOCK 0x0d
51 #define XMS_GET_EMB_HANDLE_INFORMATION 0x0e
52 #define XMS_RESIZE_EXTENDED_MEMORY_BLOCK 0x0f
53 #define XMS_ALLOCATE_UMB 0x10
54 #define XMS_DEALLOCATE_UMB 0x11
55 #define XMS_QUERY_ANY_FREE_MEMORY 0x88
56 #define XMS_ALLOCATE_ANY_MEMORY 0x89
57 #define XMS_GET_EMB_HANDLE_INFORMATION_EXT 0x8e
58 #define XMS_RESIZE_ANY_EXTENDED_MEMORY_BLOCK 0x8f
59
60 #define XMS_FUNCTION_NOT_IMPLEMENTED 0x80
61 #define HIGH_MEMORY_NOT_EXIST 0x90
62 #define HIGH_MEMORY_IN_USE 0x91
63 #define HIGH_MEMORY_NOT_ALLOCATED 0x93
64 #define XMS_OUT_OF_SPACE 0xa0
65 #define XMS_OUT_OF_HANDLES 0xa1
66 #define XMS_INVALID_HANDLE 0xa2
67 #define XMS_INVALID_SOURCE_HANDLE 0xa3
68 #define XMS_INVALID_SOURCE_OFFSET 0xa4
69 #define XMS_INVALID_DEST_HANDLE 0xa5
70 #define XMS_INVALID_DEST_OFFSET 0xa6
71 #define XMS_INVALID_LENGTH 0xa7
72 #define XMS_BLOCK_NOT_LOCKED 0xaa
73 #define XMS_BLOCK_LOCKED 0xab
74 #define UMB_ONLY_SMALLER_BLOCK 0xb0
75 #define UMB_NO_BLOCKS_AVAILABLE 0xb1
76
77 struct XMS_Block {
78 Bitu size;
79 MemHandle mem;
80 Bit8u locked;
81 bool free;
82 };
83
84 #ifdef _MSC_VER
85 #pragma pack (1)
86 #endif
87 struct XMS_MemMove{
88 Bit32u length;
89 Bit16u src_handle;
90 union {
91 RealPt realpt;
92 Bit32u offset;
93 } src;
94 Bit16u dest_handle;
95 union {
96 RealPt realpt;
97 Bit32u offset;
98 } dest;
99
100 } GCC_ATTRIBUTE(packed);
101 #ifdef _MSC_VER
102 #pragma pack ()
103 #endif
104
105
106 Bitu XMS_EnableA20(bool enable) {
107 Bit8u val = IO_Read (0x92);
108 if (enable) IO_Write(0x92,val | 2);
109 else IO_Write(0x92,val & ~2);
110 return 0;
111 }
112
113 Bitu XMS_GetEnabledA20(void) {
114 return (IO_Read(0x92)&2)>0;
115 }
116
117 static RealPt xms_callback;
118 static bool umb_available;
119
120 static XMS_Block xms_handles[XMS_HANDLES];
121
122 static INLINE bool InvalidHandle(Bitu handle) {
123 return (!handle || (handle>=XMS_HANDLES) || xms_handles[handle].free);
124 }
125
126 Bitu XMS_QueryFreeMemory(Bit16u& largestFree, Bit16u& totalFree) {
127 /* Scan the tree for free memory and find largest free block */
128 totalFree=(Bit16u)(MEM_FreeTotal()*4);
129 largestFree=(Bit16u)(MEM_FreeLargest()*4);
130 if (!totalFree) return XMS_OUT_OF_SPACE;
131 return 0;
132 }
133
134 Bitu XMS_AllocateMemory(Bitu size, Bit16u& handle) { // size = kb
135 /* Find free handle */
136 Bit16u index=1;
137 while (!xms_handles[index].free) {
138 if (++index>=XMS_HANDLES) return XMS_OUT_OF_HANDLES;
139 }
140 MemHandle mem;
141 if (size!=0) {
142 Bitu pages=(size/4) + ((size & 3) ? 1 : 0);
143 mem=MEM_AllocatePages(pages,true);
144 if (!mem) return XMS_OUT_OF_SPACE;
145 } else {
146 mem=MEM_GetNextFreePage();
147 if (mem==0) LOG(LOG_MISC,LOG_ERROR)("XMS:Allocate zero pages with no memory left");
148 }
149 xms_handles[index].free=false;
150 xms_handles[index].mem=mem;
151 xms_handles[index].locked=0;
152 xms_handles[index].size=size;
153 handle=index;
154 return 0;
155 }
156
157 Bitu XMS_FreeMemory(Bitu handle) {
158 if (InvalidHandle(handle)) return XMS_INVALID_HANDLE;
159 MEM_ReleasePages(xms_handles[handle].mem);
160 xms_handles[handle].mem=-1;
161 xms_handles[handle].size=0;
162 xms_handles[handle].free=true;
163 return 0;
164 }
165
166 Bitu XMS_MoveMemory(PhysPt bpt) {
167 /* Read the block with mem_read's */
168 Bitu length=mem_readd(bpt+offsetof(XMS_MemMove,length));
169 Bitu src_handle=mem_readw(bpt+offsetof(XMS_MemMove,src_handle));
170 union {
171 RealPt realpt;
172 Bit32u offset;
173 } src,dest;
174 src.offset=mem_readd(bpt+offsetof(XMS_MemMove,src.offset));
175 Bitu dest_handle=mem_readw(bpt+offsetof(XMS_MemMove,dest_handle));
176 dest.offset=mem_readd(bpt+offsetof(XMS_MemMove,dest.offset));
177 PhysPt srcpt,destpt;
178 if (src_handle) {
179 if (InvalidHandle(src_handle)) {
180 return XMS_INVALID_SOURCE_HANDLE;
181 }
182 if (src.offset>=(xms_handles[src_handle].size*1024U)) {
183 return XMS_INVALID_SOURCE_OFFSET;
184 }
185 if (length>xms_handles[src_handle].size*1024U-src.offset) {
186 return XMS_INVALID_LENGTH;
187 }
188 srcpt=(xms_handles[src_handle].mem*4096)+src.offset;
189 } else {
190 srcpt=Real2Phys(src.realpt);
191 }
192 if (dest_handle) {
193 if (InvalidHandle(dest_handle)) {
194 return XMS_INVALID_DEST_HANDLE;
195 }
196 if (dest.offset>=(xms_handles[dest_handle].size*1024U)) {
197 return XMS_INVALID_DEST_OFFSET;
198 }
199 if (length>xms_handles[dest_handle].size*1024U-dest.offset) {
200 return XMS_INVALID_LENGTH;
201 }
202 destpt=(xms_handles[dest_handle].mem*4096)+dest.offset;
203 } else {
204 destpt=Real2Phys(dest.realpt);
205 }
206 // LOG_MSG("XMS move src %X dest %X length %X",srcpt,destpt,length);
207 mem_memcpy(destpt,srcpt,length);
208 return 0;
209 }
210
211 Bitu XMS_LockMemory(Bitu handle, Bit32u& address) {
212 if (InvalidHandle(handle)) return XMS_INVALID_HANDLE;
213 if (xms_handles[handle].locked<255) xms_handles[handle].locked++;
214 address = xms_handles[handle].mem*4096;
215 return 0;
216 }
217
218 Bitu XMS_UnlockMemory(Bitu handle) {
219 if (InvalidHandle(handle)) return XMS_INVALID_HANDLE;
220 if (xms_handles[handle].locked) {
221 xms_handles[handle].locked--;
222 return 0;
223 }
224 return XMS_BLOCK_NOT_LOCKED;
225 }
226
227 Bitu XMS_GetHandleInformation(Bitu handle, Bit8u& lockCount, Bit8u& numFree, Bit16u& size) {
228 if (InvalidHandle(handle)) return XMS_INVALID_HANDLE;
229 lockCount = xms_handles[handle].locked;
230 /* Find available blocks */
231 numFree=0;
232 for (Bitu i=1;i<XMS_HANDLES;i++) {
233 if (xms_handles[i].free) numFree++;
234 }
235 size=(Bit16u)(xms_handles[handle].size);
236 return 0;
237 }
238
239 Bitu XMS_ResizeMemory(Bitu handle, Bitu newSize) {
240 if (InvalidHandle(handle)) return XMS_INVALID_HANDLE;
241 // Block has to be unlocked
242 if (xms_handles[handle].locked>0) return XMS_BLOCK_LOCKED;
243 Bitu pages=newSize/4 + ((newSize & 3) ? 1 : 0);
244 if (MEM_ReAllocatePages(xms_handles[handle].mem,pages,true)) {
245 xms_handles[handle].size = newSize;
246 return 0;
247 } else return XMS_OUT_OF_SPACE;
248 }
249
250 static bool multiplex_xms(void) {
251 switch (reg_ax) {
252 case 0x4300: /* XMS installed check */
253 reg_al=0x80;
254 return true;
255 case 0x4310: /* XMS handler seg:offset */
256 SegSet16(es,RealSeg(xms_callback));
257 reg_bx=RealOff(xms_callback);
258 return true;
259 }
260 return false;
261
262 }
263
264 INLINE void SET_RESULT(Bitu res,bool touch_bl_on_succes=true) {
265 if(touch_bl_on_succes || res) reg_bl = (Bit8u)res;
266 reg_ax = (res==0);
267 }
268
269 Bitu XMS_Handler(void) {
270 // LOG(LOG_MISC,LOG_ERROR)("XMS: CALL %02X",reg_ah);
271 switch (reg_ah) {
272 case XMS_GET_VERSION: /* 00 */
273 reg_ax=XMS_VERSION;
274 reg_bx=XMS_DRIVER_VERSION;
275 reg_dx=0; /* No we don't have HMA */
276 break;
277 case XMS_ALLOCATE_HIGH_MEMORY: /* 01 */
278 reg_ax=0;
279 reg_bl=HIGH_MEMORY_NOT_EXIST;
280 break;
281 case XMS_FREE_HIGH_MEMORY: /* 02 */
282 reg_ax=0;
283 reg_bl=HIGH_MEMORY_NOT_EXIST;
284 break;
285
286 case XMS_GLOBAL_ENABLE_A20: /* 03 */
287 case XMS_LOCAL_ENABLE_A20: /* 05 */
288 SET_RESULT(XMS_EnableA20(true));
289 break;
290 case XMS_GLOBAL_DISABLE_A20: /* 04 */
291 case XMS_LOCAL_DISABLE_A20: /* 06 */
292 SET_RESULT(XMS_EnableA20(false));
293 break;
294 case XMS_QUERY_A20: /* 07 */
295 reg_ax = XMS_GetEnabledA20();
296 reg_bl = 0;
297 break;
298 case XMS_QUERY_FREE_EXTENDED_MEMORY: /* 08 */
299 reg_bl = XMS_QueryFreeMemory(reg_ax,reg_dx);
300 break;
301 case XMS_ALLOCATE_ANY_MEMORY: /* 89 */
302 reg_edx &= 0xffff;
303 // fall through
304 case XMS_ALLOCATE_EXTENDED_MEMORY: /* 09 */
305 {
306 Bit16u handle = 0;
307 SET_RESULT(XMS_AllocateMemory(reg_dx,handle));
308 reg_dx = handle;
309 }; break;
310 case XMS_FREE_EXTENDED_MEMORY: /* 0a */
311 SET_RESULT(XMS_FreeMemory(reg_dx));
312 break;
313 case XMS_MOVE_EXTENDED_MEMORY_BLOCK: /* 0b */
314 SET_RESULT(XMS_MoveMemory(SegPhys(ds)+reg_si),false);
315 break;
316 case XMS_LOCK_EXTENDED_MEMORY_BLOCK: { /* 0c */
317 Bit32u address;
318 Bitu res = XMS_LockMemory(reg_dx, address);
319 if(res) reg_bl = (Bit8u)res;
320 reg_ax = (res==0);
321 if (res==0) { // success
322 reg_bx=(Bit16u)(address & 0xFFFF);
323 reg_dx=(Bit16u)(address >> 16);
324 };
325 }; break;
326 case XMS_UNLOCK_EXTENDED_MEMORY_BLOCK: /* 0d */
327 SET_RESULT(XMS_UnlockMemory(reg_dx));
328 break;
329 case XMS_GET_EMB_HANDLE_INFORMATION: /* 0e */
330 SET_RESULT(XMS_GetHandleInformation(reg_dx,reg_bh,reg_bl,reg_dx),false);
331 break;
332 case XMS_RESIZE_ANY_EXTENDED_MEMORY_BLOCK: /* 0x8f */
333 if(reg_ebx > reg_bx) LOG_MSG("64MB memory limit!");
334 //fall through
335 case XMS_RESIZE_EXTENDED_MEMORY_BLOCK: /* 0f */
336 SET_RESULT(XMS_ResizeMemory(reg_dx, reg_bx));
337 break;
338 case XMS_ALLOCATE_UMB: { /* 10 */
339 if (!umb_available) {
340 reg_ax=0;
341 reg_bl=XMS_FUNCTION_NOT_IMPLEMENTED;
342 break;
343 }
344 Bit16u umb_start=dos_infoblock.GetStartOfUMBChain();
345 if (umb_start==0xffff) {
346 reg_ax=0;
347 reg_bl=UMB_NO_BLOCKS_AVAILABLE;
348 reg_dx=0; // no upper memory available
349 break;
350 }
351 /* Save status and linkage of upper UMB chain and link upper
352 memory to the regular MCB chain */
353 Bit8u umb_flag=dos_infoblock.GetUMBChainState();
354 if ((umb_flag&1)==0) DOS_LinkUMBsToMemChain(1);
355 Bit8u old_memstrat=DOS_GetMemAllocStrategy()&0xff;
356 DOS_SetMemAllocStrategy(0x40); // search in UMBs only
357
358 Bit16u size=reg_dx;Bit16u seg;
359 if (DOS_AllocateMemory(&seg,&size)) {
360 reg_ax=1;
361 reg_bx=seg;
362 } else {
363 reg_ax=0;
364 if (size==0) reg_bl=UMB_NO_BLOCKS_AVAILABLE;
365 else reg_bl=UMB_ONLY_SMALLER_BLOCK;
366 reg_dx=size; // size of largest available UMB
367 }
368
369 /* Restore status and linkage of upper UMB chain */
370 Bit8u current_umb_flag=dos_infoblock.GetUMBChainState();
371 if ((current_umb_flag&1)!=(umb_flag&1)) DOS_LinkUMBsToMemChain(umb_flag);
372 DOS_SetMemAllocStrategy(old_memstrat);
373 }
374 break;
375 case XMS_DEALLOCATE_UMB: /* 11 */
376 if (!umb_available) {
377 reg_ax=0;
378 reg_bl=XMS_FUNCTION_NOT_IMPLEMENTED;
379 break;
380 }
381 if (dos_infoblock.GetStartOfUMBChain()!=0xffff) {
382 if (DOS_FreeMemory(reg_dx)) {
383 reg_ax=0x0001;
384 break;
385 }
386 }
387 reg_ax=0x0000;
388 reg_bl=UMB_NO_BLOCKS_AVAILABLE;
389 break;
390 case XMS_QUERY_ANY_FREE_MEMORY: /* 88 */
391 reg_bl = XMS_QueryFreeMemory(reg_ax,reg_dx);
392 reg_eax &= 0xffff;
393 reg_edx &= 0xffff;
394 reg_ecx = (MEM_TotalPages()*MEM_PAGESIZE)-1; // highest known physical memory address
395 break;
396 case XMS_GET_EMB_HANDLE_INFORMATION_EXT: { /* 8e */
397 Bit8u free_handles;
398 Bitu result = XMS_GetHandleInformation(reg_dx,reg_bh,free_handles,reg_dx);
399 if (result != 0) reg_bl = result;
400 else {
401 reg_edx &= 0xffff;
402 reg_cx = free_handles;
403 }
404 reg_ax = (result==0);
405 } break;
406 default:
407 LOG(LOG_MISC,LOG_ERROR)("XMS: unknown function %02X",reg_ah);
408 reg_ax=0;
409 reg_bl=XMS_FUNCTION_NOT_IMPLEMENTED;
410 }
411 // LOG(LOG_MISC,LOG_ERROR)("XMS: CALL Result: %02X",reg_bl);
412 return CBRET_NONE;
413 }
414
415 class XMS: public Module_base {
416 private:
417 CALLBACK_HandlerObject callbackhandler;
418 public:
419 XMS(Section* configuration):Module_base(configuration){
420 Section_prop * section=static_cast<Section_prop *>(configuration);
421 umb_available=false;
422 if (!section->Get_bool("xms")) return;
423 Bitu i;
424 BIOS_ZeroExtendedSize(true);
425 DOS_AddMultiplexHandler(multiplex_xms);
426
427 /* place hookable callback in writable memory area */
428 xms_callback=RealMake(DOS_GetMemory(0x1)-1,0x10);
429 callbackhandler.Install(&XMS_Handler,CB_HOOKABLE,Real2Phys(xms_callback),"XMS Handler");
430 // pseudocode for CB_HOOKABLE:
431 // jump near skip
432 // nop,nop,nop
433 // label skip:
434 // callback XMS_Handler
435 // retf
436
437 for (i=0;i<XMS_HANDLES;i++) {
438 xms_handles[i].free=true;
439 xms_handles[i].mem=-1;
440 xms_handles[i].size=0;
441 xms_handles[i].locked=0;
442 }
443 /* Disable the 0 handle */
444 xms_handles[0].free = false;
445
446 /* Set up UMB chain */
447 umb_available=section->Get_bool("umb");
448 DOS_BuildUMBChain(section->Get_bool("umb"),section->Get_bool("ems"));
449 }
450
451 ~XMS(){
452 Section_prop * section = static_cast<Section_prop *>(m_configuration);
453 /* Remove upper memory information */
454 dos_infoblock.SetStartOfUMBChain(0xffff);
455 if (umb_available) {
456 dos_infoblock.SetUMBChainState(0);
457 umb_available=false;
458 }
459
460 if (!section->Get_bool("xms")) return;
461 /* Undo biosclearing */
462 BIOS_ZeroExtendedSize(false);
463
464 /* Remove Multiplex */
465 DOS_DelMultiplexHandler(multiplex_xms);
466
467 /* Free used memory while skipping the 0 handle */
468 for (Bitu i = 1;i<XMS_HANDLES;i++)
469 if(!xms_handles[i].free) XMS_FreeMemory(i);
470 }
471
472 };
473 static XMS* test;
474
475 void XMS_ShutDown(Section* /*sec*/) {
476 delete test;
477 }
478
479 void XMS_Init(Section* sec) {
480 test = new XMS(sec);
481 sec->AddDestroyFunction(&XMS_ShutDown,true);
482 }