| 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: ems.cpp,v 1.65 2009-10-11 17:11:52 c2woody Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include "dosbox.h" |
| 24 | #include "callback.h" |
| 25 | #include "mem.h" |
| 26 | #include "paging.h" |
| 27 | #include "bios.h" |
| 28 | #include "keyboard.h" |
| 29 | #include "regs.h" |
| 30 | #include "inout.h" |
| 31 | #include "dos_inc.h" |
| 32 | #include "setup.h" |
| 33 | #include "support.h" |
| 34 | #include "cpu.h" |
| 35 | |
| 36 | #define EMM_PAGEFRAME 0xE000 |
| 37 | #define EMM_PAGEFRAME4K ((EMM_PAGEFRAME*16)/4096) |
| 38 | #define EMM_MAX_HANDLES 200 /* 255 Max */ |
| 39 | #define EMM_PAGE_SIZE (16*1024U) |
| 40 | #define EMM_MAX_PAGES (32 * 1024 / 16 ) |
| 41 | #define EMM_MAX_PHYS 4 /* 4 16kb pages in pageframe */ |
| 42 | |
| 43 | #define EMM_VERSION 0x40 |
| 44 | #define EMM_MINOR_VERSION 0x00 |
| 45 | //#define EMM_MINOR_VERSION 0x30 // emm386 4.48 |
| 46 | #define GEMMIS_VERSION 0x0001 // Version 1.0 |
| 47 | |
| 48 | #define EMM_SYSTEM_HANDLE 0x0000 |
| 49 | #define NULL_HANDLE 0xffff |
| 50 | #define NULL_PAGE 0xffff |
| 51 | |
| 52 | #define ENABLE_VCPI 1 |
| 53 | #define ENABLE_V86_STARTUP 0 |
| 54 | |
| 55 | |
| 56 | /* EMM errors */ |
| 57 | #define EMM_NO_ERROR 0x00 |
| 58 | #define EMM_SOFT_MAL 0x80 |
| 59 | #define EMM_HARD_MAL 0x81 |
| 60 | #define EMM_INVALID_HANDLE 0x83 |
| 61 | #define EMM_FUNC_NOSUP 0x84 |
| 62 | #define EMM_OUT_OF_HANDLES 0x85 |
| 63 | #define EMM_SAVEMAP_ERROR 0x86 |
| 64 | #define EMM_OUT_OF_PHYS 0x87 |
| 65 | #define EMM_OUT_OF_LOG 0x88 |
| 66 | #define EMM_ZERO_PAGES 0x89 |
| 67 | #define EMM_LOG_OUT_RANGE 0x8a |
| 68 | #define EMM_ILL_PHYS 0x8b |
| 69 | #define EMM_PAGE_MAP_SAVED 0x8d |
| 70 | #define EMM_NO_SAVED_PAGE_MAP 0x8e |
| 71 | #define EMM_INVALID_SUB 0x8f |
| 72 | #define EMM_FEAT_NOSUP 0x91 |
| 73 | #define EMM_MOVE_OVLAP 0x92 |
| 74 | #define EMM_MOVE_OVLAPI 0x97 |
| 75 | #define EMM_NOT_FOUND 0xa0 |
| 76 | |
| 77 | |
| 78 | struct EMM_Mapping { |
| 79 | Bit16u handle; |
| 80 | Bit16u page; |
| 81 | }; |
| 82 | |
| 83 | struct EMM_Handle { |
| 84 | Bit16u pages; |
| 85 | MemHandle mem; |
| 86 | char name[8]; |
| 87 | bool saved_page_map; |
| 88 | EMM_Mapping page_map[EMM_MAX_PHYS]; |
| 89 | }; |
| 90 | |
| 91 | static EMM_Handle emm_handles[EMM_MAX_HANDLES]; |
| 92 | static EMM_Mapping emm_mappings[EMM_MAX_PHYS]; |
| 93 | static EMM_Mapping emm_segmentmappings[0x40]; |
| 94 | |
| 95 | |
| 96 | static Bit16u GEMMIS_seg; |
| 97 | |
| 98 | class device_EMM : public DOS_Device { |
| 99 | public: |
| 100 | device_EMM() { |
| 101 | SetName("EMMXXXX0"); |
| 102 | GEMMIS_seg=0; |
| 103 | } |
| 104 | bool Read(Bit8u * /*data*/,Bit16u * /*size*/) { return false;} |
| 105 | bool Write(Bit8u * /*data*/,Bit16u * /*size*/){ |
| 106 | LOG(LOG_IOCTL,LOG_NORMAL)("EMS:Write to device"); |
| 107 | return false; |
| 108 | } |
| 109 | bool Seek(Bit32u * /*pos*/,Bit32u /*type*/){return false;} |
| 110 | bool Close(){return false;} |
| 111 | Bit16u GetInformation(void){return 0xc080;} |
| 112 | bool ReadFromControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode); |
| 113 | bool WriteToControlChannel(PhysPt /*bufptr*/,Bit16u /*size*/,Bit16u * /*retcode*/){return true;} |
| 114 | private: |
| 115 | Bit8u cache; |
| 116 | }; |
| 117 | |
| 118 | bool device_EMM::ReadFromControlChannel(PhysPt bufptr,Bit16u size,Bit16u * retcode) { |
| 119 | Bitu subfct=mem_readb(bufptr); |
| 120 | switch (subfct) { |
| 121 | case 0x00: |
| 122 | if (size!=6) return false; |
| 123 | mem_writew(bufptr+0x00,0x0023); // ID |
| 124 | mem_writed(bufptr+0x02,0); // private API entry point |
| 125 | *retcode=6; |
| 126 | return true; |
| 127 | case 0x01: { |
| 128 | if (size!=6) return false; |
| 129 | if (GEMMIS_seg==0) GEMMIS_seg=DOS_GetMemory(0x20); |
| 130 | PhysPt GEMMIS_addr=PhysMake(GEMMIS_seg,0); |
| 131 | |
| 132 | mem_writew(GEMMIS_addr+0x00,0x0004); // flags |
| 133 | mem_writew(GEMMIS_addr+0x02,0x019d); // size of this structure |
| 134 | mem_writew(GEMMIS_addr+0x04,GEMMIS_VERSION); // version 1.0 (provide ems information only) |
| 135 | mem_writed(GEMMIS_addr+0x06,0); // reserved |
| 136 | |
| 137 | /* build non-EMS frames (0-0xe000) */ |
| 138 | for (Bitu frct=0; frct<EMM_PAGEFRAME4K/4; frct++) { |
| 139 | mem_writeb(GEMMIS_addr+0x0a+frct*6,0x00); // frame type: NONE |
| 140 | mem_writeb(GEMMIS_addr+0x0b+frct*6,0xff); // owner: NONE |
| 141 | mem_writew(GEMMIS_addr+0x0c+frct*6,0xffff); // non-EMS frame |
| 142 | mem_writeb(GEMMIS_addr+0x0e + frct*6,0xff); // EMS page number (NONE) |
| 143 | mem_writeb(GEMMIS_addr+0x0f+frct*6,0xaa); // flags: direct mapping |
| 144 | } |
| 145 | /* build EMS page frame (0xe000-0xf000) */ |
| 146 | for (Bitu frct=0; frct<0x10/4; frct++) { |
| 147 | Bitu frnr=(frct+EMM_PAGEFRAME4K/4)*6; |
| 148 | mem_writeb(GEMMIS_addr+0x0a+frnr,0x03); // frame type: EMS frame in 64k page |
| 149 | mem_writeb(GEMMIS_addr+0x0b+frnr,0xff); // owner: NONE |
| 150 | mem_writew(GEMMIS_addr+0x0c+frnr,0x7fff); // no logical page number |
| 151 | mem_writeb(GEMMIS_addr+0x0e + frnr,(Bit8u)(frct&0xff)); // physical EMS page number |
| 152 | mem_writeb(GEMMIS_addr+0x0f+frnr,0x00); // EMS frame |
| 153 | } |
| 154 | /* build non-EMS ROM frames (0xf000-0x10000) */ |
| 155 | for (Bitu frct=(EMM_PAGEFRAME4K+0x10)/4; frct<0xf0/4; frct++) { |
| 156 | mem_writeb(GEMMIS_addr+0x0a+frct*6,0x00); // frame type: NONE |
| 157 | mem_writeb(GEMMIS_addr+0x0b+frct*6,0xff); // owner: NONE |
| 158 | mem_writew(GEMMIS_addr+0x0c+frct*6,0xffff); // non-EMS frame |
| 159 | mem_writeb(GEMMIS_addr+0x0e + frct*6,0xff); // EMS page number (NONE) |
| 160 | mem_writeb(GEMMIS_addr+0x0f+frct*6,0xaa); // flags: direct mapping |
| 161 | } |
| 162 | |
| 163 | mem_writeb(GEMMIS_addr+0x18a,0x74); // ??? |
| 164 | mem_writeb(GEMMIS_addr+0x18b,0x00); // no UMB descriptors following |
| 165 | mem_writeb(GEMMIS_addr+0x18c,0x01); // 1 EMS handle info recort |
| 166 | mem_writew(GEMMIS_addr+0x18d,0x0000); // system handle |
| 167 | mem_writed(GEMMIS_addr+0x18f,0); // handle name |
| 168 | mem_writed(GEMMIS_addr+0x193,0); // handle name |
| 169 | if (emm_handles[EMM_SYSTEM_HANDLE].pages != NULL_HANDLE) { |
| 170 | mem_writew(GEMMIS_addr+0x197,(emm_handles[EMM_SYSTEM_HANDLE].pages+3)/4); |
| 171 | mem_writed(GEMMIS_addr+0x199,emm_handles[EMM_SYSTEM_HANDLE].mem<<12); // physical address |
| 172 | } else { |
| 173 | mem_writew(GEMMIS_addr+0x197,0x0001); // system handle |
| 174 | mem_writed(GEMMIS_addr+0x199,0x00110000); // physical address |
| 175 | } |
| 176 | |
| 177 | /* fill buffer with import structure */ |
| 178 | mem_writed(bufptr+0x00,GEMMIS_seg<<4); |
| 179 | mem_writew(bufptr+0x04,GEMMIS_VERSION); |
| 180 | *retcode=6; |
| 181 | return true; |
| 182 | } |
| 183 | case 0x02: |
| 184 | if (size!=2) return false; |
| 185 | mem_writeb(bufptr+0x00,EMM_VERSION>>4); // version 4 |
| 186 | mem_writeb(bufptr+0x01,EMM_MINOR_VERSION); |
| 187 | *retcode=2; |
| 188 | return true; |
| 189 | case 0x03: |
| 190 | if (EMM_MINOR_VERSION < 0x2d) return false; |
| 191 | if (size!=4) return false; |
| 192 | mem_writew(bufptr+0x00,(Bit16u)(MEM_TotalPages()*4)); // max size (kb) |
| 193 | mem_writew(bufptr+0x02,0x80); // min size (kb) |
| 194 | *retcode=2; |
| 195 | return true; |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | static struct { |
| 201 | bool enabled; |
| 202 | Bit16u ems_handle; |
| 203 | Bitu pm_interface; |
| 204 | MemHandle private_area; |
| 205 | Bit8u pic1_remapping,pic2_remapping; |
| 206 | } vcpi ; |
| 207 | |
| 208 | struct MoveRegion { |
| 209 | Bit32u bytes; |
| 210 | Bit8u src_type; |
| 211 | Bit16u src_handle; |
| 212 | Bit16u src_offset; |
| 213 | Bit16u src_page_seg; |
| 214 | Bit8u dest_type; |
| 215 | Bit16u dest_handle; |
| 216 | Bit16u dest_offset; |
| 217 | Bit16u dest_page_seg; |
| 218 | }; |
| 219 | |
| 220 | static Bit16u EMM_GetFreePages(void) { |
| 221 | Bitu count=MEM_FreeTotal()/4; |
| 222 | if (count>0x7fff) count=0x7fff; |
| 223 | return (Bit16u)count; |
| 224 | } |
| 225 | |
| 226 | static bool INLINE ValidHandle(Bit16u handle) { |
| 227 | if (handle>=EMM_MAX_HANDLES) return false; |
| 228 | if (emm_handles[handle].pages==NULL_HANDLE) return false; |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | static Bit8u EMM_AllocateMemory(Bit16u pages,Bit16u & dhandle,bool can_allocate_zpages) { |
| 233 | /* Check for 0 page allocation */ |
| 234 | if (!pages) { |
| 235 | if (!can_allocate_zpages) return EMM_ZERO_PAGES; |
| 236 | } |
| 237 | /* Check for enough free pages */ |
| 238 | if ((MEM_FreeTotal()/ 4) < pages) { return EMM_OUT_OF_LOG;} |
| 239 | Bit16u handle = 1; |
| 240 | /* Check for a free handle */ |
| 241 | while (emm_handles[handle].pages != NULL_HANDLE) { |
| 242 | if (++handle >= EMM_MAX_HANDLES) {return EMM_OUT_OF_HANDLES;} |
| 243 | } |
| 244 | MemHandle mem = 0; |
| 245 | if (pages) { |
| 246 | mem = MEM_AllocatePages(pages*4,false); |
| 247 | if (!mem) E_Exit("EMS:Memory allocation failure"); |
| 248 | } |
| 249 | emm_handles[handle].pages = pages; |
| 250 | emm_handles[handle].mem = mem; |
| 251 | /* Change handle only if there is no error. */ |
| 252 | dhandle = handle; |
| 253 | return EMM_NO_ERROR; |
| 254 | } |
| 255 | |
| 256 | static Bit8u EMM_AllocateSystemHandle(Bit16u pages) { |
| 257 | /* Check for enough free pages */ |
| 258 | if ((MEM_FreeTotal()/ 4) < pages) { return EMM_OUT_OF_LOG;} |
| 259 | Bit16u handle = EMM_SYSTEM_HANDLE; // emm system handle (reserved for OS usage) |
| 260 | /* Release memory if already allocated */ |
| 261 | if (emm_handles[handle].pages != NULL_HANDLE) { |
| 262 | MEM_ReleasePages(emm_handles[handle].mem); |
| 263 | } |
| 264 | MemHandle mem = MEM_AllocatePages(pages*4,false); |
| 265 | if (!mem) E_Exit("EMS:System handle memory allocation failure"); |
| 266 | emm_handles[handle].pages = pages; |
| 267 | emm_handles[handle].mem = mem; |
| 268 | return EMM_NO_ERROR; |
| 269 | } |
| 270 | |
| 271 | static Bit8u EMM_ReallocatePages(Bit16u handle,Bit16u & pages) { |
| 272 | /* Check for valid handle */ |
| 273 | if (!ValidHandle(handle)) return EMM_INVALID_HANDLE; |
| 274 | if (emm_handles[handle].pages != 0) { |
| 275 | /* Check for enough pages */ |
| 276 | if (!MEM_ReAllocatePages(emm_handles[handle].mem,pages*4,false)) return EMM_OUT_OF_LOG; |
| 277 | } else { |
| 278 | MemHandle mem = MEM_AllocatePages(pages*4,false); |
| 279 | if (!mem) E_Exit("EMS:Memory allocation failure during reallocation"); |
| 280 | emm_handles[handle].mem = mem; |
| 281 | } |
| 282 | /* Update size */ |
| 283 | emm_handles[handle].pages=pages; |
| 284 | return EMM_NO_ERROR; |
| 285 | } |
| 286 | |
| 287 | static Bit8u EMM_MapPage(Bitu phys_page,Bit16u handle,Bit16u log_page) { |
| 288 | // LOG_MSG("EMS MapPage handle %d phys %d log %d",handle,phys_page,log_page); |
| 289 | /* Check for too high physical page */ |
| 290 | if (phys_page>=EMM_MAX_PHYS) return EMM_ILL_PHYS; |
| 291 | |
| 292 | /* unmapping doesn't need valid handle (as handle isn't used) */ |
| 293 | if (log_page==NULL_PAGE) { |
| 294 | /* Unmapping */ |
| 295 | emm_mappings[phys_page].handle=NULL_HANDLE; |
| 296 | emm_mappings[phys_page].page=NULL_PAGE; |
| 297 | for (Bitu i=0;i<4;i++) |
| 298 | PAGING_MapPage(EMM_PAGEFRAME4K+phys_page*4+i,EMM_PAGEFRAME4K+phys_page*4+i); |
| 299 | PAGING_ClearTLB(); |
| 300 | return EMM_NO_ERROR; |
| 301 | } |
| 302 | /* Check for valid handle */ |
| 303 | if (!ValidHandle(handle)) return EMM_INVALID_HANDLE; |
| 304 | |
| 305 | if (log_page<emm_handles[handle].pages) { |
| 306 | /* Mapping it is */ |
| 307 | emm_mappings[phys_page].handle=handle; |
| 308 | emm_mappings[phys_page].page=log_page; |
| 309 | |
| 310 | MemHandle memh=MEM_NextHandleAt(emm_handles[handle].mem,log_page*4);; |
| 311 | for (Bitu i=0;i<4;i++) { |
| 312 | PAGING_MapPage(EMM_PAGEFRAME4K+phys_page*4+i,memh); |
| 313 | memh=MEM_NextHandle(memh); |
| 314 | } |
| 315 | PAGING_ClearTLB(); |
| 316 | return EMM_NO_ERROR; |
| 317 | } else { |
| 318 | /* Illegal logical page it is */ |
| 319 | return EMM_LOG_OUT_RANGE; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static Bit8u EMM_MapSegment(Bitu segment,Bit16u handle,Bit16u log_page) { |
| 324 | // LOG_MSG("EMS MapSegment handle %d segment %d log %d",handle,segment,log_page); |
| 325 | |
| 326 | if (((segment>=0xa000) && (segment<0xb000)) || ((segment>=EMM_PAGEFRAME-0x1000) && (segment<EMM_PAGEFRAME+0x1000))) { |
| 327 | Bit32s tphysPage = ((Bit32s)segment-EMM_PAGEFRAME)/(0x1000/EMM_MAX_PHYS); |
| 328 | |
| 329 | /* unmapping doesn't need valid handle (as handle isn't used) */ |
| 330 | if (log_page==NULL_PAGE) { |
| 331 | /* Unmapping */ |
| 332 | if ((tphysPage>=0) && (tphysPage<EMM_MAX_PHYS)) { |
| 333 | emm_mappings[tphysPage].handle=NULL_HANDLE; |
| 334 | emm_mappings[tphysPage].page=NULL_PAGE; |
| 335 | } else { |
| 336 | emm_segmentmappings[segment>>10].handle=NULL_HANDLE; |
| 337 | emm_segmentmappings[segment>>10].page=NULL_PAGE; |
| 338 | } |
| 339 | for (Bitu i=0;i<4;i++) |
| 340 | PAGING_MapPage(segment*16/4096+i,segment*16/4096+i); |
| 341 | PAGING_ClearTLB(); |
| 342 | return EMM_NO_ERROR; |
| 343 | } |
| 344 | /* Check for valid handle */ |
| 345 | if (!ValidHandle(handle)) return EMM_INVALID_HANDLE; |
| 346 | |
| 347 | if (log_page<emm_handles[handle].pages) { |
| 348 | /* Mapping it is */ |
| 349 | if ((tphysPage>=0) && (tphysPage<EMM_MAX_PHYS)) { |
| 350 | emm_mappings[tphysPage].handle=handle; |
| 351 | emm_mappings[tphysPage].page=log_page; |
| 352 | } else { |
| 353 | emm_segmentmappings[segment>>10].handle=handle; |
| 354 | emm_segmentmappings[segment>>10].page=log_page; |
| 355 | } |
| 356 | |
| 357 | MemHandle memh=MEM_NextHandleAt(emm_handles[handle].mem,log_page*4);; |
| 358 | for (Bitu i=0;i<4;i++) { |
| 359 | PAGING_MapPage(segment*16/4096+i,memh); |
| 360 | memh=MEM_NextHandle(memh); |
| 361 | } |
| 362 | PAGING_ClearTLB(); |
| 363 | return EMM_NO_ERROR; |
| 364 | } else { |
| 365 | /* Illegal logical page it is */ |
| 366 | return EMM_LOG_OUT_RANGE; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return EMM_ILL_PHYS; |
| 371 | } |
| 372 | |
| 373 | static Bit8u EMM_ReleaseMemory(Bit16u handle) { |
| 374 | /* Check for valid handle */ |
| 375 | if (!ValidHandle(handle)) return EMM_INVALID_HANDLE; |
| 376 | |
| 377 | // should check for saved_page_map flag here, returning an error if it's true |
| 378 | // as apps are required to restore the pagemap beforehand; to be checked |
| 379 | // if (emm_handles[handle].saved_page_map) return EMM_SAVEMAP_ERROR; |
| 380 | |
| 381 | if (emm_handles[handle].pages != 0) { |
| 382 | MEM_ReleasePages(emm_handles[handle].mem); |
| 383 | } |
| 384 | /* Reset handle */ |
| 385 | emm_handles[handle].mem=0; |
| 386 | if (handle==0) { |
| 387 | emm_handles[handle].pages=0; // OS handle is NEVER deallocated |
| 388 | } else { |
| 389 | emm_handles[handle].pages=NULL_HANDLE; |
| 390 | } |
| 391 | emm_handles[handle].saved_page_map=false; |
| 392 | memset(&emm_handles[handle].name,0,8); |
| 393 | return EMM_NO_ERROR; |
| 394 | } |
| 395 | |
| 396 | static Bit8u EMM_SavePageMap(Bit16u handle) { |
| 397 | /* Check for valid handle */ |
| 398 | if (handle>=EMM_MAX_HANDLES || emm_handles[handle].pages==NULL_HANDLE) { |
| 399 | if (handle!=0) return EMM_INVALID_HANDLE; |
| 400 | } |
| 401 | /* Check for previous save */ |
| 402 | if (emm_handles[handle].saved_page_map) return EMM_PAGE_MAP_SAVED; |
| 403 | /* Copy the mappings over */ |
| 404 | for (Bitu i=0;i<EMM_MAX_PHYS;i++) { |
| 405 | emm_handles[handle].page_map[i].page=emm_mappings[i].page; |
| 406 | emm_handles[handle].page_map[i].handle=emm_mappings[i].handle; |
| 407 | } |
| 408 | emm_handles[handle].saved_page_map=true; |
| 409 | return EMM_NO_ERROR; |
| 410 | } |
| 411 | |
| 412 | static Bit8u EMM_RestoreMappingTable(void) { |
| 413 | Bit8u result; |
| 414 | /* Move through the mappings table and setup mapping accordingly */ |
| 415 | for (Bitu i=0;i<0x40;i++) { |
| 416 | /* Skip the pageframe */ |
| 417 | if ((i>=EMM_PAGEFRAME/0x400) && (i<(EMM_PAGEFRAME/0x400)+EMM_MAX_PHYS)) continue; |
| 418 | result=EMM_MapSegment(i<<10,emm_segmentmappings[i].handle,emm_segmentmappings[i].page); |
| 419 | } |
| 420 | for (Bitu i=0;i<EMM_MAX_PHYS;i++) { |
| 421 | result=EMM_MapPage(i,emm_mappings[i].handle,emm_mappings[i].page); |
| 422 | } |
| 423 | return EMM_NO_ERROR; |
| 424 | } |
| 425 | static Bit8u EMM_RestorePageMap(Bit16u handle) { |
| 426 | /* Check for valid handle */ |
| 427 | if (handle>=EMM_MAX_HANDLES || emm_handles[handle].pages==NULL_HANDLE) { |
| 428 | if (handle!=0) return EMM_INVALID_HANDLE; |
| 429 | } |
| 430 | /* Check for previous save */ |
| 431 | if (!emm_handles[handle].saved_page_map) return EMM_NO_SAVED_PAGE_MAP; |
| 432 | /* Restore the mappings */ |
| 433 | emm_handles[handle].saved_page_map=false; |
| 434 | for (Bitu i=0;i<EMM_MAX_PHYS;i++) { |
| 435 | emm_mappings[i].page=emm_handles[handle].page_map[i].page; |
| 436 | emm_mappings[i].handle=emm_handles[handle].page_map[i].handle; |
| 437 | } |
| 438 | return EMM_RestoreMappingTable(); |
| 439 | } |
| 440 | |
| 441 | static Bit8u EMM_GetPagesForAllHandles(PhysPt table,Bit16u & handles) { |
| 442 | handles=0; |
| 443 | for (Bit16u i=0;i<EMM_MAX_HANDLES;i++) { |
| 444 | if (emm_handles[i].pages!=NULL_HANDLE) { |
| 445 | handles++; |
| 446 | mem_writew(table,i); |
| 447 | mem_writew(table+2,emm_handles[i].pages); |
| 448 | table+=4; |
| 449 | } |
| 450 | } |
| 451 | return EMM_NO_ERROR; |
| 452 | } |
| 453 | |
| 454 | static Bit8u EMM_PartialPageMapping(void) { |
| 455 | PhysPt list,data;Bit16u count; |
| 456 | switch (reg_al) { |
| 457 | case 0x00: /* Save Partial Page Map */ |
| 458 | list = SegPhys(ds)+reg_si; |
| 459 | data = SegPhys(es)+reg_di; |
| 460 | count=mem_readw(list);list+=2; |
| 461 | mem_writew(data,count);data+=2; |
| 462 | for (;count>0;count--) { |
| 463 | Bit16u segment=mem_readw(list);list+=2; |
| 464 | if ((segment>=EMM_PAGEFRAME) && (segment<EMM_PAGEFRAME+0x1000)) { |
| 465 | Bit16u page = (segment-EMM_PAGEFRAME) / (EMM_PAGE_SIZE>>4); |
| 466 | mem_writew(data,segment);data+=2; |
| 467 | MEM_BlockWrite(data,&emm_mappings[page],sizeof(EMM_Mapping)); |
| 468 | data+=sizeof(EMM_Mapping); |
| 469 | } else if (((segment>=EMM_PAGEFRAME-0x1000) && (segment<EMM_PAGEFRAME)) || ((segment>=0xa000) && (segment<0xb000))) { |
| 470 | mem_writew(data,segment);data+=2; |
| 471 | MEM_BlockWrite(data,&emm_segmentmappings[segment>>10],sizeof(EMM_Mapping)); |
| 472 | data+=sizeof(EMM_Mapping); |
| 473 | } else { |
| 474 | return EMM_ILL_PHYS; |
| 475 | } |
| 476 | } |
| 477 | break; |
| 478 | case 0x01: /* Restore Partial Page Map */ |
| 479 | data = SegPhys(ds)+reg_si; |
| 480 | count= mem_readw(data);data+=2; |
| 481 | for (;count>0;count--) { |
| 482 | Bit16u segment=mem_readw(data);data+=2; |
| 483 | if ((segment>=EMM_PAGEFRAME) && (segment<EMM_PAGEFRAME+0x1000)) { |
| 484 | Bit16u page = (segment-EMM_PAGEFRAME) / (EMM_PAGE_SIZE>>4); |
| 485 | MEM_BlockRead(data,&emm_mappings[page],sizeof(EMM_Mapping)); |
| 486 | } else if (((segment>=EMM_PAGEFRAME-0x1000) && (segment<EMM_PAGEFRAME)) || ((segment>=0xa000) && (segment<0xb000))) { |
| 487 | MEM_BlockRead(data,&emm_segmentmappings[segment>>10],sizeof(EMM_Mapping)); |
| 488 | } else { |
| 489 | return EMM_ILL_PHYS; |
| 490 | } |
| 491 | data+=sizeof(EMM_Mapping); |
| 492 | } |
| 493 | return EMM_RestoreMappingTable(); |
| 494 | break; |
| 495 | case 0x02: /* Get Partial Page Map Array Size */ |
| 496 | reg_al=(Bit8u)(2+reg_bx*(2+sizeof(EMM_Mapping))); |
| 497 | break; |
| 498 | default: |
| 499 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X Subfunction %2X not supported",reg_ah,reg_al); |
| 500 | return EMM_FUNC_NOSUP; |
| 501 | } |
| 502 | return EMM_NO_ERROR; |
| 503 | } |
| 504 | |
| 505 | static Bit8u HandleNameSearch(void) { |
| 506 | char name[9]; |
| 507 | Bit16u handle=0;PhysPt data; |
| 508 | switch (reg_al) { |
| 509 | case 0x00: /* Get all handle names */ |
| 510 | reg_al=0;data=SegPhys(es)+reg_di; |
| 511 | for (handle=0;handle<EMM_MAX_HANDLES;handle++) { |
| 512 | if (emm_handles[handle].pages!=NULL_HANDLE) { |
| 513 | reg_al++; |
| 514 | mem_writew(data,handle); |
| 515 | MEM_BlockWrite(data+2,emm_handles[handle].name,8); |
| 516 | data+=10; |
| 517 | } |
| 518 | } |
| 519 | break; |
| 520 | case 0x01: /* Search for a handle name */ |
| 521 | MEM_StrCopy(SegPhys(ds)+reg_si,name,8);name[8]=0; |
| 522 | for (handle=0;handle<EMM_MAX_HANDLES;handle++) { |
| 523 | if (emm_handles[handle].pages!=NULL_HANDLE) { |
| 524 | if (!strncmp(name,emm_handles[handle].name,8)) { |
| 525 | reg_dx=handle; |
| 526 | return EMM_NO_ERROR; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | return EMM_NOT_FOUND; |
| 531 | break; |
| 532 | case 0x02: /* Get Total number of handles */ |
| 533 | reg_bx=EMM_MAX_HANDLES; |
| 534 | break; |
| 535 | default: |
| 536 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X Subfunction %2X not supported",reg_ah,reg_al); |
| 537 | return EMM_INVALID_SUB; |
| 538 | } |
| 539 | return EMM_NO_ERROR; |
| 540 | } |
| 541 | |
| 542 | static Bit8u GetSetHandleName(void) { |
| 543 | Bit16u handle=reg_dx; |
| 544 | switch (reg_al) { |
| 545 | case 0x00: /* Get Handle Name */ |
| 546 | if (handle>=EMM_MAX_HANDLES || emm_handles[handle].pages==NULL_HANDLE) return EMM_INVALID_HANDLE; |
| 547 | MEM_BlockWrite(SegPhys(es)+reg_di,emm_handles[handle].name,8); |
| 548 | break; |
| 549 | case 0x01: /* Set Handle Name */ |
| 550 | if (handle>=EMM_MAX_HANDLES || emm_handles[handle].pages==NULL_HANDLE) return EMM_INVALID_HANDLE; |
| 551 | MEM_BlockRead(SegPhys(es)+reg_di,emm_handles[handle].name,8); |
| 552 | break; |
| 553 | default: |
| 554 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X Subfunction %2X not supported",reg_ah,reg_al); |
| 555 | return EMM_INVALID_SUB; |
| 556 | } |
| 557 | return EMM_NO_ERROR; |
| 558 | |
| 559 | } |
| 560 | |
| 561 | |
| 562 | static void LoadMoveRegion(PhysPt data,MoveRegion & region) { |
| 563 | region.bytes=mem_readd(data+0x0); |
| 564 | |
| 565 | region.src_type=mem_readb(data+0x4); |
| 566 | region.src_handle=mem_readw(data+0x5); |
| 567 | region.src_offset=mem_readw(data+0x7); |
| 568 | region.src_page_seg=mem_readw(data+0x9); |
| 569 | |
| 570 | region.dest_type=mem_readb(data+0xb); |
| 571 | region.dest_handle=mem_readw(data+0xc); |
| 572 | region.dest_offset=mem_readw(data+0xe); |
| 573 | region.dest_page_seg=mem_readw(data+0x10); |
| 574 | } |
| 575 | |
| 576 | static Bit8u MemoryRegion(void) { |
| 577 | MoveRegion region; |
| 578 | Bit8u buf_src[MEM_PAGE_SIZE]; |
| 579 | Bit8u buf_dest[MEM_PAGE_SIZE]; |
| 580 | if (reg_al>1) { |
| 581 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X Subfunction %2X not supported",reg_ah,reg_al); |
| 582 | return EMM_FUNC_NOSUP; |
| 583 | } |
| 584 | LoadMoveRegion(SegPhys(ds)+reg_si,region); |
| 585 | /* Parse the region for information */ |
| 586 | PhysPt src_mem = 0,dest_mem = 0; |
| 587 | MemHandle src_handle = 0,dest_handle = 0; |
| 588 | Bitu src_off = 0,dest_off = 0 ;Bitu src_remain = 0,dest_remain = 0; |
| 589 | if (!region.src_type) { |
| 590 | src_mem=region.src_page_seg*16+region.src_offset; |
| 591 | } else { |
| 592 | if (!ValidHandle(region.src_handle)) return EMM_INVALID_HANDLE; |
| 593 | if ((emm_handles[region.src_handle].pages*EMM_PAGE_SIZE) < ((region.src_page_seg*EMM_PAGE_SIZE)+region.src_offset+region.bytes)) return EMM_LOG_OUT_RANGE; |
| 594 | src_handle=emm_handles[region.src_handle].mem; |
| 595 | Bitu pages=region.src_page_seg*4+(region.src_offset/MEM_PAGE_SIZE); |
| 596 | for (;pages>0;pages--) src_handle=MEM_NextHandle(src_handle); |
| 597 | src_off=region.src_offset&(MEM_PAGE_SIZE-1); |
| 598 | src_remain=MEM_PAGE_SIZE-src_off; |
| 599 | } |
| 600 | if (!region.dest_type) { |
| 601 | dest_mem=region.dest_page_seg*16+region.dest_offset; |
| 602 | } else { |
| 603 | if (!ValidHandle(region.dest_handle)) return EMM_INVALID_HANDLE; |
| 604 | if (emm_handles[region.dest_handle].pages*EMM_PAGE_SIZE < (region.dest_page_seg*EMM_PAGE_SIZE)+region.dest_offset+region.bytes) return EMM_LOG_OUT_RANGE; |
| 605 | dest_handle=emm_handles[region.dest_handle].mem; |
| 606 | Bitu pages=region.dest_page_seg*4+(region.dest_offset/MEM_PAGE_SIZE); |
| 607 | for (;pages>0;pages--) dest_handle=MEM_NextHandle(dest_handle); |
| 608 | dest_off=region.dest_offset&(MEM_PAGE_SIZE-1); |
| 609 | dest_remain=MEM_PAGE_SIZE-dest_off; |
| 610 | } |
| 611 | Bitu toread; |
| 612 | while (region.bytes>0) { |
| 613 | if (region.bytes>MEM_PAGE_SIZE) toread=MEM_PAGE_SIZE; |
| 614 | else toread=region.bytes; |
| 615 | /* Read from the source */ |
| 616 | if (!region.src_type) { |
| 617 | MEM_BlockRead(src_mem,buf_src,toread); |
| 618 | } else { |
| 619 | if (toread<src_remain) { |
| 620 | MEM_BlockRead((src_handle*MEM_PAGE_SIZE)+src_off,buf_src,toread); |
| 621 | } else { |
| 622 | MEM_BlockRead((src_handle*MEM_PAGE_SIZE)+src_off,buf_src,src_remain); |
| 623 | MEM_BlockRead((MEM_NextHandle(src_handle)*MEM_PAGE_SIZE),&buf_src[src_remain],toread-src_remain); |
| 624 | } |
| 625 | } |
| 626 | /* Check for a move */ |
| 627 | if (reg_al==1) { |
| 628 | /* Read from the destination */ |
| 629 | if (!region.dest_type) { |
| 630 | MEM_BlockRead(dest_mem,buf_dest,toread); |
| 631 | } else { |
| 632 | if (toread<dest_remain) { |
| 633 | MEM_BlockRead((dest_handle*MEM_PAGE_SIZE)+dest_off,buf_dest,toread); |
| 634 | } else { |
| 635 | MEM_BlockRead((dest_handle*MEM_PAGE_SIZE)+dest_off,buf_dest,dest_remain); |
| 636 | MEM_BlockRead((MEM_NextHandle(dest_handle)*MEM_PAGE_SIZE),&buf_dest[dest_remain],toread-dest_remain); |
| 637 | } |
| 638 | } |
| 639 | /* Write to the source */ |
| 640 | if (!region.src_type) { |
| 641 | MEM_BlockWrite(src_mem,buf_dest,toread); |
| 642 | } else { |
| 643 | if (toread<src_remain) { |
| 644 | MEM_BlockWrite((src_handle*MEM_PAGE_SIZE)+src_off,buf_dest,toread); |
| 645 | } else { |
| 646 | MEM_BlockWrite((src_handle*MEM_PAGE_SIZE)+src_off,buf_dest,src_remain); |
| 647 | MEM_BlockWrite((MEM_NextHandle(src_handle)*MEM_PAGE_SIZE),&buf_dest[src_remain],toread-src_remain); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | /* Write to the destination */ |
| 652 | if (!region.dest_type) { |
| 653 | MEM_BlockWrite(dest_mem,buf_src,toread); |
| 654 | } else { |
| 655 | if (toread<dest_remain) { |
| 656 | MEM_BlockWrite((dest_handle*MEM_PAGE_SIZE)+dest_off,buf_src,toread); |
| 657 | } else { |
| 658 | MEM_BlockWrite((dest_handle*MEM_PAGE_SIZE)+dest_off,buf_src,dest_remain); |
| 659 | MEM_BlockWrite((MEM_NextHandle(dest_handle)*MEM_PAGE_SIZE),&buf_src[dest_remain],toread-dest_remain); |
| 660 | } |
| 661 | } |
| 662 | /* Advance the pointers */ |
| 663 | if (!region.src_type) src_mem+=toread; |
| 664 | else src_handle=MEM_NextHandle(src_handle); |
| 665 | if (!region.dest_type) dest_mem+=toread; |
| 666 | else dest_handle=MEM_NextHandle(dest_handle); |
| 667 | region.bytes-=toread; |
| 668 | } |
| 669 | return EMM_NO_ERROR; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | static Bitu INT67_Handler(void) { |
| 674 | Bitu i; |
| 675 | switch (reg_ah) { |
| 676 | case 0x40: /* Get Status */ |
| 677 | reg_ah=EMM_NO_ERROR; |
| 678 | break; |
| 679 | case 0x41: /* Get PageFrame Segment */ |
| 680 | reg_bx=EMM_PAGEFRAME; |
| 681 | reg_ah=EMM_NO_ERROR; |
| 682 | break; |
| 683 | case 0x42: /* Get number of pages */ |
| 684 | reg_dx=(Bit16u)(MEM_TotalPages()/4); //Not entirely correct but okay |
| 685 | reg_bx=EMM_GetFreePages(); |
| 686 | reg_ah=EMM_NO_ERROR; |
| 687 | break; |
| 688 | case 0x43: /* Get Handle and Allocate Pages */ |
| 689 | reg_ah=EMM_AllocateMemory(reg_bx,reg_dx,false); |
| 690 | break; |
| 691 | case 0x44: /* Map Expanded Memory Page */ |
| 692 | reg_ah=EMM_MapPage(reg_al,reg_dx,reg_bx); |
| 693 | break; |
| 694 | case 0x45: /* Release handle and free pages */ |
| 695 | reg_ah=EMM_ReleaseMemory(reg_dx); |
| 696 | break; |
| 697 | case 0x46: /* Get EMM Version */ |
| 698 | reg_ah=EMM_NO_ERROR; |
| 699 | reg_al=EMM_VERSION; |
| 700 | break; |
| 701 | case 0x47: /* Save Page Map */ |
| 702 | reg_ah=EMM_SavePageMap(reg_dx); |
| 703 | break; |
| 704 | case 0x48: /* Restore Page Map */ |
| 705 | reg_ah=EMM_RestorePageMap(reg_dx); |
| 706 | break; |
| 707 | case 0x4b: /* Get Handle Count */ |
| 708 | reg_bx=0; |
| 709 | for (i=0;i<EMM_MAX_HANDLES;i++) if (emm_handles[i].pages!=NULL_HANDLE) reg_bx++; |
| 710 | reg_ah=EMM_NO_ERROR; |
| 711 | break; |
| 712 | case 0x4c: /* Get Pages for one Handle */ |
| 713 | if (!ValidHandle(reg_dx)) {reg_ah=EMM_INVALID_HANDLE;break;} |
| 714 | reg_bx=emm_handles[reg_dx].pages; |
| 715 | reg_ah=EMM_NO_ERROR; |
| 716 | break; |
| 717 | case 0x4d: /* Get Pages for all Handles */ |
| 718 | reg_ah=EMM_GetPagesForAllHandles(SegPhys(es)+reg_di,reg_bx); |
| 719 | break; |
| 720 | case 0x4e: /*Save/Restore Page Map */ |
| 721 | switch (reg_al) { |
| 722 | case 0x00: /* Save Page Map */ |
| 723 | MEM_BlockWrite(SegPhys(es)+reg_di,emm_mappings,sizeof(emm_mappings)); |
| 724 | reg_ah=EMM_NO_ERROR; |
| 725 | break; |
| 726 | case 0x01: /* Restore Page Map */ |
| 727 | MEM_BlockRead(SegPhys(ds)+reg_si,emm_mappings,sizeof(emm_mappings)); |
| 728 | reg_ah=EMM_RestoreMappingTable(); |
| 729 | break; |
| 730 | case 0x02: /* Save and Restore Page Map */ |
| 731 | MEM_BlockWrite(SegPhys(es)+reg_di,emm_mappings,sizeof(emm_mappings)); |
| 732 | MEM_BlockRead(SegPhys(ds)+reg_si,emm_mappings,sizeof(emm_mappings)); |
| 733 | reg_ah=EMM_RestoreMappingTable(); |
| 734 | break; |
| 735 | case 0x03: /* Get Page Map Array Size */ |
| 736 | reg_al=sizeof(emm_mappings); |
| 737 | reg_ah=EMM_NO_ERROR; |
| 738 | break; |
| 739 | default: |
| 740 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X Subfunction %2X not supported",reg_ah,reg_al); |
| 741 | reg_ah=EMM_INVALID_SUB; |
| 742 | break; |
| 743 | } |
| 744 | break; |
| 745 | case 0x4f: /* Save/Restore Partial Page Map */ |
| 746 | reg_ah=EMM_PartialPageMapping(); |
| 747 | break; |
| 748 | case 0x50: /* Map/Unmap multiple handle pages */ |
| 749 | reg_ah = EMM_NO_ERROR; |
| 750 | switch (reg_al) { |
| 751 | case 0x00: // use physical page numbers |
| 752 | { PhysPt data = SegPhys(ds)+reg_si; |
| 753 | for (int i=0; i<reg_cx; i++) { |
| 754 | Bit16u logPage = mem_readw(data); data+=2; |
| 755 | Bit16u physPage = mem_readw(data); data+=2; |
| 756 | reg_ah = EMM_MapPage(physPage,reg_dx,logPage); |
| 757 | if (reg_ah!=EMM_NO_ERROR) break; |
| 758 | }; |
| 759 | } break; |
| 760 | case 0x01: // use segment address |
| 761 | { PhysPt data = SegPhys(ds)+reg_si; |
| 762 | for (int i=0; i<reg_cx; i++) { |
| 763 | Bit16u logPage = mem_readw(data); data+=2; |
| 764 | reg_ah = EMM_MapSegment(mem_readw(data),reg_dx,logPage); data+=2; |
| 765 | if (reg_ah!=EMM_NO_ERROR) break; |
| 766 | }; |
| 767 | } |
| 768 | break; |
| 769 | default: |
| 770 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X Subfunction %2X not supported",reg_ah,reg_al); |
| 771 | reg_ah=EMM_INVALID_SUB; |
| 772 | break; |
| 773 | } |
| 774 | break; |
| 775 | case 0x51: /* Reallocate Pages */ |
| 776 | reg_ah=EMM_ReallocatePages(reg_dx,reg_bx); |
| 777 | break; |
| 778 | case 0x53: // Set/Get Handlename |
| 779 | reg_ah=GetSetHandleName(); |
| 780 | break; |
| 781 | case 0x54: /* Handle Functions */ |
| 782 | reg_ah=HandleNameSearch(); |
| 783 | break; |
| 784 | case 0x57: /* Memory region */ |
| 785 | reg_ah=MemoryRegion(); |
| 786 | if (reg_ah) LOG(LOG_MISC,LOG_ERROR)("EMS:Function 57 move failed"); |
| 787 | break; |
| 788 | case 0x58: // Get mappable physical array address array |
| 789 | if (reg_al==0x00) { |
| 790 | PhysPt data = SegPhys(es)+reg_di; |
| 791 | Bit16u step = 0x1000 / EMM_MAX_PHYS; |
| 792 | for (Bit16u i=0; i<EMM_MAX_PHYS; i++) { |
| 793 | mem_writew(data,EMM_PAGEFRAME+step*i); data+=2; |
| 794 | mem_writew(data,i); data+=2; |
| 795 | }; |
| 796 | }; |
| 797 | // Set number of pages |
| 798 | reg_cx = EMM_MAX_PHYS; |
| 799 | reg_ah = EMM_NO_ERROR; |
| 800 | break; |
| 801 | case 0x5A: /* Allocate standard/raw Pages */ |
| 802 | if (reg_al<=0x01) { |
| 803 | reg_ah=EMM_AllocateMemory(reg_bx,reg_dx,true); // can allocate 0 pages |
| 804 | } else { |
| 805 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call 5A subfct %2X not supported",reg_al); |
| 806 | reg_ah=EMM_INVALID_SUB; |
| 807 | }; |
| 808 | break; |
| 809 | case 0xDE: /* VCPI Functions */ |
| 810 | if (!vcpi.enabled) { |
| 811 | LOG(LOG_MISC,LOG_ERROR)("EMS:VCPI Call %2X not supported",reg_al); |
| 812 | reg_ah=EMM_FUNC_NOSUP; |
| 813 | } else { |
| 814 | switch (reg_al) { |
| 815 | case 0x00: /* VCPI Installation Check */ |
| 816 | if (((reg_cx==0) && (reg_di==0x0012)) || (cpu.pmode && (reg_flags & FLAG_VM))) { |
| 817 | /* JEMM detected or already in v86 mode */ |
| 818 | reg_ah=EMM_NO_ERROR; |
| 819 | reg_bx=0x100; |
| 820 | } else { |
| 821 | reg_ah=EMM_FUNC_NOSUP; |
| 822 | } |
| 823 | break; |
| 824 | case 0x01: { /* VCPI Get Protected Mode Interface */ |
| 825 | Bit16u ct; |
| 826 | /* Set up page table buffer */ |
| 827 | for (ct=0; ct<0xff; ct++) { |
| 828 | real_writeb(SegValue(es),reg_di+ct*4+0x00,0x67); // access bits |
| 829 | real_writew(SegValue(es),reg_di+ct*4+0x01,ct*0x10); // mapping |
| 830 | real_writeb(SegValue(es),reg_di+ct*4+0x03,0x00); |
| 831 | } |
| 832 | for (ct=0xff; ct<0x100; ct++) { |
| 833 | real_writeb(SegValue(es),reg_di+ct*4+0x00,0x67); // access bits |
| 834 | real_writew(SegValue(es),reg_di+ct*4+0x01,(ct-0xff)*0x10+0x1100); // mapping |
| 835 | real_writeb(SegValue(es),reg_di+ct*4+0x03,0x00); |
| 836 | } |
| 837 | /* adjust paging entries for page frame (if mapped) */ |
| 838 | for (ct=0; ct<4; ct++) { |
| 839 | Bit16u handle=emm_mappings[ct].handle; |
| 840 | if (handle!=0xffff) { |
| 841 | Bit16u memh=(Bit16u)MEM_NextHandleAt(emm_handles[handle].mem,emm_mappings[ct].page*4); |
| 842 | Bit16u entry_addr=reg_di+(EMM_PAGEFRAME>>6)+(ct*0x10); |
| 843 | real_writew(SegValue(es),entry_addr+0x00+0x01,(memh+0)*0x10); // mapping of 1/4 of page |
| 844 | real_writew(SegValue(es),entry_addr+0x04+0x01,(memh+1)*0x10); // mapping of 2/4 of page |
| 845 | real_writew(SegValue(es),entry_addr+0x08+0x01,(memh+2)*0x10); // mapping of 3/4 of page |
| 846 | real_writew(SegValue(es),entry_addr+0x0c+0x01,(memh+3)*0x10); // mapping of 4/4 of page |
| 847 | } |
| 848 | } |
| 849 | reg_di+=0x400; // advance pointer by 0x100*4 |
| 850 | |
| 851 | /* Set up three descriptor table entries */ |
| 852 | Bit32u cbseg_low=(CALLBACK_GetBase()&0xffff)<<16; |
| 853 | Bit32u cbseg_high=(CALLBACK_GetBase()&0x1f0000)>>16; |
| 854 | /* Descriptor 1 (code segment, callback segment) */ |
| 855 | real_writed(SegValue(ds),reg_si+0x00,0x0000ffff|cbseg_low); |
| 856 | real_writed(SegValue(ds),reg_si+0x04,0x00009a00|cbseg_high); |
| 857 | /* Descriptor 2 (data segment, full access) */ |
| 858 | real_writed(SegValue(ds),reg_si+0x08,0x0000ffff); |
| 859 | real_writed(SegValue(ds),reg_si+0x0c,0x00009200); |
| 860 | /* Descriptor 3 (full access) */ |
| 861 | real_writed(SegValue(ds),reg_si+0x10,0x0000ffff); |
| 862 | real_writed(SegValue(ds),reg_si+0x14,0x00009200); |
| 863 | |
| 864 | reg_ebx=(vcpi.pm_interface&0xffff); |
| 865 | reg_ah=EMM_NO_ERROR; |
| 866 | break; |
| 867 | } |
| 868 | case 0x02: /* VCPI Maximum Physical Address */ |
| 869 | reg_edx=((MEM_TotalPages()*MEM_PAGESIZE)-1)&0xfffff000; |
| 870 | reg_ah=EMM_NO_ERROR; |
| 871 | break; |
| 872 | case 0x03: /* VCPI Get Number of Free Pages */ |
| 873 | reg_edx=MEM_FreeTotal(); |
| 874 | reg_ah=EMM_NO_ERROR; |
| 875 | break; |
| 876 | case 0x04: { /* VCPI Allocate one Page */ |
| 877 | MemHandle mem = MEM_AllocatePages(1,false); |
| 878 | if (mem) { |
| 879 | reg_edx=mem<<12; |
| 880 | reg_ah=EMM_NO_ERROR; |
| 881 | } else { |
| 882 | reg_ah=EMM_OUT_OF_LOG; |
| 883 | } |
| 884 | break; |
| 885 | } |
| 886 | case 0x05: /* VCPI Free Page */ |
| 887 | MEM_ReleasePages(reg_edx>>12); |
| 888 | reg_ah=EMM_NO_ERROR; |
| 889 | break; |
| 890 | case 0x06: { /* VCPI Get Physical Address of Page in 1st MB */ |
| 891 | if (((reg_cx<<8)>=EMM_PAGEFRAME) && ((reg_cx<<8)<EMM_PAGEFRAME+0x1000)) { |
| 892 | /* Page is in Pageframe, so check what EMS-page it is |
| 893 | and return the physical address */ |
| 894 | Bit8u phys_page; |
| 895 | Bit16u mem_seg=reg_cx<<8; |
| 896 | if (mem_seg<EMM_PAGEFRAME+0x400) phys_page=0; |
| 897 | else if (mem_seg<EMM_PAGEFRAME+0x800) phys_page=1; |
| 898 | else if (mem_seg<EMM_PAGEFRAME+0xc00) phys_page=2; |
| 899 | else phys_page=3; |
| 900 | Bit16u handle=emm_mappings[phys_page].handle; |
| 901 | if (handle==0xffff) { |
| 902 | reg_ah=EMM_ILL_PHYS; |
| 903 | break; |
| 904 | } else { |
| 905 | MemHandle memh=MEM_NextHandleAt( |
| 906 | emm_handles[handle].mem, |
| 907 | emm_mappings[phys_page].page*4); |
| 908 | reg_edx=(memh+(reg_cx&3))<<12; |
| 909 | } |
| 910 | } else { |
| 911 | /* Page not in Pageframe, so just translate into physical address */ |
| 912 | reg_edx=reg_cx<<12; |
| 913 | } |
| 914 | |
| 915 | reg_ah=EMM_NO_ERROR; |
| 916 | } |
| 917 | break; |
| 918 | case 0x0a: /* VCPI Get PIC Vector Mappings */ |
| 919 | reg_bx=vcpi.pic1_remapping; // master PIC |
| 920 | reg_cx=vcpi.pic2_remapping; // slave PIC |
| 921 | reg_ah=EMM_NO_ERROR; |
| 922 | break; |
| 923 | case 0x0b: /* VCPI Set PIC Vector Mappings */ |
| 924 | reg_flags&=(~FLAG_IF); |
| 925 | vcpi.pic1_remapping=reg_bx&0xff; |
| 926 | vcpi.pic2_remapping=reg_cx&0xff; |
| 927 | reg_ah=EMM_NO_ERROR; |
| 928 | break; |
| 929 | case 0x0c: { /* VCPI Switch from V86 to Protected Mode */ |
| 930 | reg_flags&=(~FLAG_IF); |
| 931 | cpu.cpl=0; |
| 932 | |
| 933 | /* Read data from ESI (linear address) */ |
| 934 | Bit32u new_cr3=mem_readd(reg_esi); |
| 935 | Bit32u new_gdt_addr=mem_readd(reg_esi+4); |
| 936 | Bit32u new_idt_addr=mem_readd(reg_esi+8); |
| 937 | Bit16u new_ldt=mem_readw(reg_esi+0x0c); |
| 938 | Bit16u new_tr=mem_readw(reg_esi+0x0e); |
| 939 | Bit32u new_eip=mem_readd(reg_esi+0x10); |
| 940 | Bit16u new_cs=mem_readw(reg_esi+0x14); |
| 941 | |
| 942 | /* Get GDT and IDT entries */ |
| 943 | Bit16u new_gdt_limit=mem_readw(new_gdt_addr); |
| 944 | Bit32u new_gdt_base=mem_readd(new_gdt_addr+2); |
| 945 | Bit16u new_idt_limit=mem_readw(new_idt_addr); |
| 946 | Bit32u new_idt_base=mem_readd(new_idt_addr+2); |
| 947 | |
| 948 | /* Switch to protected mode, paging enabled if necessary */ |
| 949 | Bit32u new_cr0=CPU_GET_CRX(0)|1; |
| 950 | if (new_cr3!=0) new_cr0|=0x80000000; |
| 951 | CPU_SET_CRX(0, new_cr0); |
| 952 | CPU_SET_CRX(3, new_cr3); |
| 953 | |
| 954 | PhysPt tbaddr=new_gdt_base+(new_tr&0xfff8)+5; |
| 955 | Bit8u tb=mem_readb(tbaddr); |
| 956 | mem_writeb(tbaddr, tb&0xfd); |
| 957 | |
| 958 | /* Load tables and initialize segment registers */ |
| 959 | CPU_LGDT(new_gdt_limit, new_gdt_base); |
| 960 | CPU_LIDT(new_idt_limit, new_idt_base); |
| 961 | if (CPU_LLDT(new_ldt)) LOG_MSG("VCPI:Could not load LDT with %x",new_ldt); |
| 962 | if (CPU_LTR(new_tr)) LOG_MSG("VCPI:Could not load TR with %x",new_tr); |
| 963 | |
| 964 | CPU_SetSegGeneral(ds,0); |
| 965 | CPU_SetSegGeneral(es,0); |
| 966 | CPU_SetSegGeneral(fs,0); |
| 967 | CPU_SetSegGeneral(gs,0); |
| 968 | |
| 969 | // MEM_A20_Enable(true); |
| 970 | |
| 971 | /* Switch to protected mode */ |
| 972 | reg_flags&=(~(FLAG_VM|FLAG_NT)); |
| 973 | reg_flags|=0x3000; |
| 974 | CPU_JMP(true, new_cs, new_eip, 0); |
| 975 | } |
| 976 | break; |
| 977 | default: |
| 978 | LOG(LOG_MISC,LOG_ERROR)("EMS:VCPI Call %x not supported",reg_ax); |
| 979 | reg_ah=EMM_FUNC_NOSUP; |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | break; |
| 984 | default: |
| 985 | LOG(LOG_MISC,LOG_ERROR)("EMS:Call %2X not supported",reg_ah); |
| 986 | reg_ah=EMM_FUNC_NOSUP; |
| 987 | break; |
| 988 | } |
| 989 | return CBRET_NONE; |
| 990 | } |
| 991 | |
| 992 | static Bitu VCPI_PM_Handler() { |
| 993 | // LOG_MSG("VCPI PMODE handler, function %x",reg_ax); |
| 994 | switch (reg_ax) { |
| 995 | case 0xDE03: /* VCPI Get Number of Free Pages */ |
| 996 | reg_edx=MEM_FreeTotal(); |
| 997 | reg_ah=EMM_NO_ERROR; |
| 998 | break; |
| 999 | case 0xDE04: { /* VCPI Allocate one Page */ |
| 1000 | MemHandle mem = MEM_AllocatePages(1,false); |
| 1001 | if (mem) { |
| 1002 | reg_edx=mem<<12; |
| 1003 | reg_ah=EMM_NO_ERROR; |
| 1004 | } else { |
| 1005 | reg_ah=EMM_OUT_OF_LOG; |
| 1006 | } |
| 1007 | break; |
| 1008 | } |
| 1009 | case 0xDE05: /* VCPI Free Page */ |
| 1010 | MEM_ReleasePages(reg_edx>>12); |
| 1011 | reg_ah=EMM_NO_ERROR; |
| 1012 | break; |
| 1013 | case 0xDE0C: { /* VCPI Switch from Protected Mode to V86 */ |
| 1014 | reg_flags&=(~FLAG_IF); |
| 1015 | |
| 1016 | /* Flags need to be filled in, VM=true, IOPL=3 */ |
| 1017 | mem_writed(SegPhys(ss) + (reg_esp & cpu.stack.mask)+0x10, 0x23002); |
| 1018 | |
| 1019 | /* Disable Paging */ |
| 1020 | CPU_SET_CRX(0, CPU_GET_CRX(0)&0x7ffffff7); |
| 1021 | CPU_SET_CRX(3, 0); |
| 1022 | |
| 1023 | PhysPt tbaddr=vcpi.private_area+0x0000+(0x10&0xfff8)+5; |
| 1024 | Bit8u tb=mem_readb(tbaddr); |
| 1025 | mem_writeb(tbaddr, tb&0xfd); |
| 1026 | |
| 1027 | /* Load descriptor table registers */ |
| 1028 | CPU_LGDT(0xff, vcpi.private_area+0x0000); |
| 1029 | CPU_LIDT(0x7ff, vcpi.private_area+0x2000); |
| 1030 | if (CPU_LLDT(0x08)) LOG_MSG("VCPI:Could not load LDT"); |
| 1031 | if (CPU_LTR(0x10)) LOG_MSG("VCPI:Could not load TR"); |
| 1032 | |
| 1033 | reg_flags&=(~FLAG_NT); |
| 1034 | reg_esp+=8; // skip interrupt return information |
| 1035 | // MEM_A20_Enable(false); |
| 1036 | |
| 1037 | /* Switch to v86-task */ |
| 1038 | CPU_IRET(true,0); |
| 1039 | } |
| 1040 | break; |
| 1041 | default: |
| 1042 | LOG(LOG_MISC,LOG_WARN)("Unhandled VCPI-function %x in protected mode",reg_al); |
| 1043 | break; |
| 1044 | } |
| 1045 | return CBRET_NONE; |
| 1046 | } |
| 1047 | |
| 1048 | static Bitu V86_Monitor() { |
| 1049 | /* Calculate which interrupt did occur */ |
| 1050 | Bitu int_num=(mem_readw(SegPhys(ss)+(reg_esp & cpu.stack.mask))-0x2803); |
| 1051 | |
| 1052 | /* See if Exception 0x0d and not Interrupt 0x0d */ |
| 1053 | if ((int_num==(0x0d*4)) && ((reg_sp&0xffff)!=0x1fda)) { |
| 1054 | /* Protection violation during V86-execution, |
| 1055 | needs intervention by monitor (depends on faulting opcode) */ |
| 1056 | |
| 1057 | reg_esp+=6; // skip ip of CALL and error code of EXCEPTION 0x0d |
| 1058 | |
| 1059 | /* Get adress of faulting instruction */ |
| 1060 | Bit16u v86_cs=mem_readw(SegPhys(ss)+((reg_esp+4) & cpu.stack.mask)); |
| 1061 | Bit16u v86_ip=mem_readw(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask)); |
| 1062 | Bit8u v86_opcode=mem_readb((v86_cs<<4)+v86_ip); |
| 1063 | // LOG_MSG("v86 monitor caught protection violation at %x:%x, opcode=%x",v86_cs,v86_ip,v86_opcode); |
| 1064 | switch (v86_opcode) { |
| 1065 | case 0x0f: // double byte opcode |
| 1066 | v86_opcode=mem_readb((v86_cs<<4)+v86_ip+1); |
| 1067 | switch (v86_opcode) { |
| 1068 | case 0x20: { // mov reg,CRx |
| 1069 | Bitu rm_val=mem_readb((v86_cs<<4)+v86_ip+2); |
| 1070 | Bitu which=(rm_val >> 3) & 7; |
| 1071 | if ((rm_val<0xc0) || (rm_val>=0xe8)) |
| 1072 | E_Exit("Invalid opcode 0x0f 0x20 %x caused a protection fault!",static_cast<unsigned int>(rm_val)); |
| 1073 | Bit32u crx=CPU_GET_CRX(which); |
| 1074 | switch (rm_val&7) { |
| 1075 | case 0: reg_eax=crx; break; |
| 1076 | case 1: reg_ecx=crx; break; |
| 1077 | case 2: reg_edx=crx; break; |
| 1078 | case 3: reg_ebx=crx; break; |
| 1079 | case 4: reg_esp=crx; break; |
| 1080 | case 5: reg_ebp=crx; break; |
| 1081 | case 6: reg_esi=crx; break; |
| 1082 | case 7: reg_edi=crx; break; |
| 1083 | } |
| 1084 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+3); |
| 1085 | } |
| 1086 | break; |
| 1087 | case 0x22: { // mov CRx,reg |
| 1088 | Bitu rm_val=mem_readb((v86_cs<<4)+v86_ip+2); |
| 1089 | Bitu which=(rm_val >> 3) & 7; |
| 1090 | if ((rm_val<0xc0) || (rm_val>=0xe8)) |
| 1091 | E_Exit("Invalid opcode 0x0f 0x22 %x caused a protection fault!",static_cast<unsigned int>(rm_val)); |
| 1092 | Bit32u crx=0; |
| 1093 | switch (rm_val&7) { |
| 1094 | case 0: crx=reg_eax; break; |
| 1095 | case 1: crx=reg_ecx; break; |
| 1096 | case 2: crx=reg_edx; break; |
| 1097 | case 3: crx=reg_ebx; break; |
| 1098 | case 4: crx=reg_esp; break; |
| 1099 | case 5: crx=reg_ebp; break; |
| 1100 | case 6: crx=reg_esi; break; |
| 1101 | case 7: crx=reg_edi; break; |
| 1102 | } |
| 1103 | if (which==0) crx|=1; // protection bit always on |
| 1104 | CPU_SET_CRX(which,crx); |
| 1105 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+3); |
| 1106 | } |
| 1107 | break; |
| 1108 | default: |
| 1109 | E_Exit("Unhandled opcode 0x0f %x caused a protection fault!",v86_opcode); |
| 1110 | } |
| 1111 | break; |
| 1112 | case 0xe4: // IN AL,Ib |
| 1113 | reg_al=(Bit8u)(IO_ReadB(mem_readb((v86_cs<<4)+v86_ip+1))&0xff); |
| 1114 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+2); |
| 1115 | break; |
| 1116 | case 0xe5: // IN AX,Ib |
| 1117 | reg_ax=(Bit16u)(IO_ReadW(mem_readb((v86_cs<<4)+v86_ip+1))&0xffff); |
| 1118 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+2); |
| 1119 | break; |
| 1120 | case 0xe6: // OUT Ib,AL |
| 1121 | IO_WriteB(mem_readb((v86_cs<<4)+v86_ip+1),reg_al); |
| 1122 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+2); |
| 1123 | break; |
| 1124 | case 0xe7: // OUT Ib,AX |
| 1125 | IO_WriteW(mem_readb((v86_cs<<4)+v86_ip+1),reg_ax); |
| 1126 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+2); |
| 1127 | break; |
| 1128 | case 0xec: // IN AL,DX |
| 1129 | reg_al=(Bit8u)(IO_ReadB(reg_dx)&0xff); |
| 1130 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+1); |
| 1131 | break; |
| 1132 | case 0xed: // IN AX,DX |
| 1133 | reg_ax=(Bit16u)(IO_ReadW(reg_dx)&0xffff); |
| 1134 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+1); |
| 1135 | break; |
| 1136 | case 0xee: // OUT DX,AL |
| 1137 | IO_WriteB(reg_dx,reg_al); |
| 1138 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+1); |
| 1139 | break; |
| 1140 | case 0xef: // OUT DX,AX |
| 1141 | IO_WriteW(reg_dx,reg_ax); |
| 1142 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+1); |
| 1143 | break; |
| 1144 | case 0xf0: // LOCK prefix |
| 1145 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+1); |
| 1146 | break; |
| 1147 | case 0xf4: // HLT |
| 1148 | reg_flags|=FLAG_IF; |
| 1149 | CPU_HLT(reg_eip); |
| 1150 | mem_writew(SegPhys(ss)+((reg_esp+0) & cpu.stack.mask),v86_ip+1); |
| 1151 | break; |
| 1152 | default: |
| 1153 | E_Exit("Unhandled opcode %x caused a protection fault!",v86_opcode); |
| 1154 | } |
| 1155 | return CBRET_NONE; |
| 1156 | } |
| 1157 | |
| 1158 | /* Get address to interrupt handler */ |
| 1159 | Bit16u vint_vector_seg=mem_readw(SegValue(ds)+int_num+2); |
| 1160 | Bit16u vint_vector_ofs=mem_readw(int_num); |
| 1161 | if (reg_sp!=0x1fda) reg_esp+=(2+3*4); // Interrupt from within protected mode |
| 1162 | else reg_esp+=2; |
| 1163 | |
| 1164 | /* Read entries that were pushed onto the stack by the interrupt */ |
| 1165 | Bit16u return_ip=mem_readw(SegPhys(ss)+(reg_esp & cpu.stack.mask)); |
| 1166 | Bit16u return_cs=mem_readw(SegPhys(ss)+((reg_esp+4) & cpu.stack.mask)); |
| 1167 | Bit32u return_eflags=mem_readd(SegPhys(ss)+((reg_esp+8) & cpu.stack.mask)); |
| 1168 | |
| 1169 | /* Modify stack to call v86-interrupt handler */ |
| 1170 | mem_writed(SegPhys(ss)+(reg_esp & cpu.stack.mask),vint_vector_ofs); |
| 1171 | mem_writed(SegPhys(ss)+((reg_esp+4) & cpu.stack.mask),vint_vector_seg); |
| 1172 | mem_writed(SegPhys(ss)+((reg_esp+8) & cpu.stack.mask),return_eflags&(~(FLAG_IF|FLAG_TF))); |
| 1173 | |
| 1174 | /* Adjust SP of v86-stack */ |
| 1175 | Bit16u v86_ss=mem_readw(SegPhys(ss)+((reg_esp+0x10) & cpu.stack.mask)); |
| 1176 | Bit16u v86_sp=mem_readw(SegPhys(ss)+((reg_esp+0x0c) & cpu.stack.mask))-6; |
| 1177 | mem_writew(SegPhys(ss)+((reg_esp+0x0c) & cpu.stack.mask),v86_sp); |
| 1178 | |
| 1179 | /* Return to original code after v86-interrupt handler */ |
| 1180 | mem_writew((v86_ss<<4)+v86_sp+0,return_ip); |
| 1181 | mem_writew((v86_ss<<4)+v86_sp+2,return_cs); |
| 1182 | mem_writew((v86_ss<<4)+v86_sp+4,(Bit16u)(return_eflags&0xffff)); |
| 1183 | return CBRET_NONE; |
| 1184 | } |
| 1185 | |
| 1186 | static void SetupVCPI() { |
| 1187 | vcpi.enabled=false; |
| 1188 | |
| 1189 | vcpi.ems_handle=0; // use EMM system handle for VCPI data |
| 1190 | |
| 1191 | vcpi.enabled=true; |
| 1192 | |
| 1193 | vcpi.pic1_remapping=0x08; // master PIC base |
| 1194 | vcpi.pic2_remapping=0x70; // slave PIC base |
| 1195 | |
| 1196 | vcpi.private_area=emm_handles[vcpi.ems_handle].mem<<12; |
| 1197 | |
| 1198 | /* GDT */ |
| 1199 | mem_writed(vcpi.private_area+0x0000,0x00000000); // descriptor 0 |
| 1200 | mem_writed(vcpi.private_area+0x0004,0x00000000); // descriptor 0 |
| 1201 | |
| 1202 | Bit32u ldt_address=(vcpi.private_area+0x1000); |
| 1203 | Bit16u ldt_limit=0xff; |
| 1204 | Bit32u ldt_desc_part=((ldt_address&0xffff)<<16)|ldt_limit; |
| 1205 | mem_writed(vcpi.private_area+0x0008,ldt_desc_part); // descriptor 1 (LDT) |
| 1206 | ldt_desc_part=((ldt_address&0xff0000)>>16)|(ldt_address&0xff000000)|0x8200; |
| 1207 | mem_writed(vcpi.private_area+0x000c,ldt_desc_part); // descriptor 1 |
| 1208 | |
| 1209 | Bit32u tss_address=(vcpi.private_area+0x3000); |
| 1210 | Bit32u tss_desc_part=((tss_address&0xffff)<<16)|(0x0068+0x200); |
| 1211 | mem_writed(vcpi.private_area+0x0010,tss_desc_part); // descriptor 2 (TSS) |
| 1212 | tss_desc_part=((tss_address&0xff0000)>>16)|(tss_address&0xff000000)|0x8900; |
| 1213 | mem_writed(vcpi.private_area+0x0014,tss_desc_part); // descriptor 2 |
| 1214 | |
| 1215 | /* LDT */ |
| 1216 | mem_writed(vcpi.private_area+0x1000,0x00000000); // descriptor 0 |
| 1217 | mem_writed(vcpi.private_area+0x1004,0x00000000); // descriptor 0 |
| 1218 | Bit32u cs_desc_part=((vcpi.private_area&0xffff)<<16)|0xffff; |
| 1219 | mem_writed(vcpi.private_area+0x1008,cs_desc_part); // descriptor 1 (code) |
| 1220 | cs_desc_part=((vcpi.private_area&0xff0000)>>16)|(vcpi.private_area&0xff000000)|0x9a00; |
| 1221 | mem_writed(vcpi.private_area+0x100c,cs_desc_part); // descriptor 1 |
| 1222 | Bit32u ds_desc_part=((vcpi.private_area&0xffff)<<16)|0xffff; |
| 1223 | mem_writed(vcpi.private_area+0x1010,ds_desc_part); // descriptor 2 (data) |
| 1224 | ds_desc_part=((vcpi.private_area&0xff0000)>>16)|(vcpi.private_area&0xff000000)|0x9200; |
| 1225 | mem_writed(vcpi.private_area+0x1014,ds_desc_part); // descriptor 2 |
| 1226 | |
| 1227 | /* IDT setup */ |
| 1228 | for (Bit16u int_ct=0; int_ct<0x100; int_ct++) { |
| 1229 | /* build a CALL NEAR V86MON, the value of IP pushed by the |
| 1230 | CALL is used to identify the interrupt number */ |
| 1231 | mem_writeb(vcpi.private_area+0x2800+int_ct*4+0,0xe8); // call |
| 1232 | mem_writew(vcpi.private_area+0x2800+int_ct*4+1,0x05fd-(int_ct*4)); |
| 1233 | mem_writeb(vcpi.private_area+0x2800+int_ct*4+3,0xcf); // iret (dummy) |
| 1234 | |
| 1235 | /* put a Gate-Descriptor into the IDT */ |
| 1236 | mem_writed(vcpi.private_area+0x2000+int_ct*8+0,0x000c0000|(0x2800+int_ct*4)); |
| 1237 | mem_writed(vcpi.private_area+0x2000+int_ct*8+4,0x0000ee00); |
| 1238 | } |
| 1239 | |
| 1240 | /* TSS */ |
| 1241 | for (Bitu tse_ct=0; tse_ct<0x68+0x200; tse_ct++) { |
| 1242 | /* clear the TSS as most entries are not used here */ |
| 1243 | mem_writeb(vcpi.private_area+0x3000,0); |
| 1244 | } |
| 1245 | /* Set up the ring0-stack */ |
| 1246 | mem_writed(vcpi.private_area+0x3004,0x00002000); // esp |
| 1247 | mem_writed(vcpi.private_area+0x3008,0x00000014); // ss |
| 1248 | |
| 1249 | mem_writed(vcpi.private_area+0x3066,0x0068); // io-map base (map follows, all zero) |
| 1250 | } |
| 1251 | |
| 1252 | static Bitu INT4B_Handler() { |
| 1253 | switch (reg_ah) { |
| 1254 | case 0x81: |
| 1255 | CALLBACK_SCF(true); |
| 1256 | reg_ax=0x1; |
| 1257 | break; |
| 1258 | default: |
| 1259 | LOG(LOG_MISC,LOG_WARN)("Unhandled interrupt 4B function %x",reg_ah); |
| 1260 | break; |
| 1261 | } |
| 1262 | return CBRET_NONE; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | class EMS: public Module_base { |
| 1267 | private: |
| 1268 | /* location in protected unfreeable memory where the ems name and callback are |
| 1269 | * stored 32 bytes.*/ |
| 1270 | static Bit16u ems_baseseg; |
| 1271 | RealPt old4b_pointer,old67_pointer; |
| 1272 | CALLBACK_HandlerObject call_vdma,call_vcpi,call_v86mon; |
| 1273 | Bitu call_int67; |
| 1274 | |
| 1275 | public: |
| 1276 | EMS(Section* configuration):Module_base(configuration){ |
| 1277 | |
| 1278 | /* Virtual DMA interrupt callback */ |
| 1279 | call_vdma.Install(&INT4B_Handler,CB_IRET,"Int 4b vdma"); |
| 1280 | call_vdma.Set_RealVec(0x4b); |
| 1281 | |
| 1282 | vcpi.enabled=false; |
| 1283 | GEMMIS_seg=0; |
| 1284 | |
| 1285 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 1286 | if (!section->Get_bool("ems")) return; |
| 1287 | if (machine==MCH_PCJR) { |
| 1288 | LOG_MSG("EMS disabled for PCJr machine"); |
| 1289 | return; |
| 1290 | } |
| 1291 | BIOS_ZeroExtendedSize(true); |
| 1292 | |
| 1293 | if (!ems_baseseg) ems_baseseg=DOS_GetMemory(2); //We have 32 bytes |
| 1294 | |
| 1295 | /* Add a little hack so it appears that there is an actual ems device installed */ |
| 1296 | char const* emsname="EMMXXXX0"; |
| 1297 | MEM_BlockWrite(PhysMake(ems_baseseg,0xa),emsname,(Bitu)(strlen(emsname)+1)); |
| 1298 | |
| 1299 | call_int67=CALLBACK_Allocate(); |
| 1300 | CALLBACK_Setup(call_int67,&INT67_Handler,CB_IRET,PhysMake(ems_baseseg,4),"Int 67 ems"); |
| 1301 | RealSetVec(0x67,RealMake(ems_baseseg,4),old67_pointer); |
| 1302 | |
| 1303 | /* Register the ems device */ |
| 1304 | //TODO MAYBE put it in the class. |
| 1305 | DOS_Device * newdev = new device_EMM(); |
| 1306 | DOS_AddDevice(newdev); |
| 1307 | |
| 1308 | /* Clear handle and page tables */ |
| 1309 | Bitu i; |
| 1310 | for (i=0;i<EMM_MAX_HANDLES;i++) { |
| 1311 | emm_handles[i].mem=0; |
| 1312 | emm_handles[i].pages=NULL_HANDLE; |
| 1313 | memset(&emm_handles[i].name,0,8); |
| 1314 | } |
| 1315 | for (i=0;i<EMM_MAX_PHYS;i++) { |
| 1316 | emm_mappings[i].page=NULL_PAGE; |
| 1317 | emm_mappings[i].handle=NULL_HANDLE; |
| 1318 | } |
| 1319 | for (i=0;i<0x40;i++) { |
| 1320 | emm_segmentmappings[i].page=NULL_PAGE; |
| 1321 | emm_segmentmappings[i].handle=NULL_HANDLE; |
| 1322 | } |
| 1323 | |
| 1324 | EMM_AllocateSystemHandle(8); // allocate OS-dedicated handle (ems handle zero, 128kb) |
| 1325 | |
| 1326 | |
| 1327 | if (!ENABLE_VCPI) return; |
| 1328 | |
| 1329 | /* Install a callback that handles VCPI-requests in protected mode requests */ |
| 1330 | call_vcpi.Install(&VCPI_PM_Handler,CB_IRETD,"VCPI PM"); |
| 1331 | vcpi.pm_interface=(call_vcpi.Get_callback())*CB_SIZE; |
| 1332 | |
| 1333 | /* Initialize private data area and set up descriptor tables */ |
| 1334 | SetupVCPI(); |
| 1335 | |
| 1336 | if (!vcpi.enabled) return; |
| 1337 | |
| 1338 | /* Install v86-callback that handles interrupts occuring |
| 1339 | in v86 mode, including protection fault exceptions */ |
| 1340 | call_v86mon.Install(&V86_Monitor,CB_IRET,"V86 Monitor"); |
| 1341 | |
| 1342 | mem_writeb(vcpi.private_area+0x2e00,(Bit8u)0xFE); //GRP 4 |
| 1343 | mem_writeb(vcpi.private_area+0x2e01,(Bit8u)0x38); //Extra Callback instruction |
| 1344 | mem_writew(vcpi.private_area+0x2e02,call_v86mon.Get_callback()); //The immediate word |
| 1345 | mem_writeb(vcpi.private_area+0x2e04,(Bit8u)0x66); |
| 1346 | mem_writeb(vcpi.private_area+0x2e05,(Bit8u)0xCF); //A IRETD Instruction |
| 1347 | |
| 1348 | /* Testcode only, starts up dosbox in v86-mode */ |
| 1349 | if (ENABLE_V86_STARTUP) { |
| 1350 | /* Prepare V86-task */ |
| 1351 | CPU_SET_CRX(0, 1); |
| 1352 | CPU_LGDT(0xff, vcpi.private_area+0x0000); |
| 1353 | CPU_LIDT(0x7ff, vcpi.private_area+0x2000); |
| 1354 | if (CPU_LLDT(0x08)) LOG_MSG("VCPI:Could not load LDT"); |
| 1355 | if (CPU_LTR(0x10)) LOG_MSG("VCPI:Could not load TR"); |
| 1356 | |
| 1357 | CPU_Push32(SegValue(gs)); |
| 1358 | CPU_Push32(SegValue(fs)); |
| 1359 | CPU_Push32(SegValue(ds)); |
| 1360 | CPU_Push32(SegValue(es)); |
| 1361 | CPU_Push32(SegValue(ss)); |
| 1362 | CPU_Push32(0x23002); |
| 1363 | CPU_Push32(SegValue(cs)); |
| 1364 | CPU_Push32(reg_eip&0xffff); |
| 1365 | /* Switch to V86-mode */ |
| 1366 | cpu.cpl=0; |
| 1367 | CPU_IRET(true,0); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | ~EMS() { |
| 1372 | Section_prop * section=static_cast<Section_prop *>(m_configuration); |
| 1373 | if (!section->Get_bool("ems")) return; |
| 1374 | |
| 1375 | /* Undo Biosclearing */ |
| 1376 | BIOS_ZeroExtendedSize(false); |
| 1377 | |
| 1378 | /* Remove ems device */ |
| 1379 | device_EMM newdev; |
| 1380 | DOS_DelDevice(&newdev); |
| 1381 | GEMMIS_seg=0; |
| 1382 | |
| 1383 | /* Remove the emsname and callback hack */ |
| 1384 | char buf[32]= { 0 }; |
| 1385 | MEM_BlockWrite(PhysMake(ems_baseseg,0),buf,32); |
| 1386 | RealSetVec(0x67,old67_pointer); |
| 1387 | |
| 1388 | /* Release memory allocated to system handle */ |
| 1389 | if (emm_handles[EMM_SYSTEM_HANDLE].pages != NULL_HANDLE) { |
| 1390 | MEM_ReleasePages(emm_handles[EMM_SYSTEM_HANDLE].mem); |
| 1391 | } |
| 1392 | |
| 1393 | /* Clear handle and page tables */ |
| 1394 | //TODO |
| 1395 | |
| 1396 | if ((!ENABLE_VCPI) || (!vcpi.enabled)) return; |
| 1397 | |
| 1398 | if (cpu.pmode && GETFLAG(VM)) { |
| 1399 | /* Switch back to real mode if in v86-mode */ |
| 1400 | CPU_SET_CRX(0, 0); |
| 1401 | CPU_SET_CRX(3, 0); |
| 1402 | reg_flags&=(~(FLAG_IOPL|FLAG_VM)); |
| 1403 | CPU_LIDT(0x3ff, 0); |
| 1404 | cpu.cpl=0; |
| 1405 | } |
| 1406 | } |
| 1407 | }; |
| 1408 | |
| 1409 | static EMS* test; |
| 1410 | |
| 1411 | void EMS_ShutDown(Section* /*sec*/) { |
| 1412 | delete test; |
| 1413 | } |
| 1414 | |
| 1415 | void EMS_Init(Section* sec) { |
| 1416 | test = new EMS(sec); |
| 1417 | sec->AddDestroyFunction(&EMS_ShutDown,true); |
| 1418 | } |
| 1419 | |
| 1420 | //Initialize static members |
| 1421 | Bit16u EMS::ems_baseseg = 0; |