| 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: cache.h,v 1.20 2009-07-12 20:13:05 c2woody Exp $ */ |
| 20 |
|
| 21 |
class CacheBlock { |
| 22 |
public: |
| 23 |
void Clear(void); |
| 24 |
void LinkTo(Bitu index,CacheBlock * toblock) { |
| 25 |
assert(toblock); |
| 26 |
link[index].to=toblock; |
| 27 |
link[index].next=toblock->link[index].from; |
| 28 |
toblock->link[index].from=this; |
| 29 |
} |
| 30 |
struct { |
| 31 |
Bit16u start,end; //Where the page is the original code |
| 32 |
CodePageHandler * handler; //Page containing this code |
| 33 |
} page; |
| 34 |
struct { |
| 35 |
Bit8u * start; //Where in the cache are we |
| 36 |
Bitu size; |
| 37 |
CacheBlock * next; |
| 38 |
Bit8u * wmapmask; |
| 39 |
Bit16u maskstart; |
| 40 |
Bit16u masklen; |
| 41 |
} cache; |
| 42 |
struct { |
| 43 |
Bitu index; |
| 44 |
CacheBlock * next; |
| 45 |
} hash; |
| 46 |
struct { |
| 47 |
CacheBlock * to; |
| 48 |
CacheBlock * next; |
| 49 |
CacheBlock * from; |
| 50 |
} link[2]; |
| 51 |
CacheBlock * crossblock; |
| 52 |
}; |
| 53 |
|
| 54 |
static struct { |
| 55 |
struct { |
| 56 |
CacheBlock * first; |
| 57 |
CacheBlock * active; |
| 58 |
CacheBlock * free; |
| 59 |
CacheBlock * running; |
| 60 |
} block; |
| 61 |
Bit8u * pos; |
| 62 |
CodePageHandler * free_pages; |
| 63 |
CodePageHandler * used_pages; |
| 64 |
CodePageHandler * last_page; |
| 65 |
} cache; |
| 66 |
|
| 67 |
static CacheBlock link_blocks[2]; |
| 68 |
|
| 69 |
class CodePageHandler : public PageHandler { |
| 70 |
public: |
| 71 |
CodePageHandler() { |
| 72 |
invalidation_map=NULL; |
| 73 |
} |
| 74 |
void SetupAt(Bitu _phys_page,PageHandler * _old_pagehandler) { |
| 75 |
phys_page=_phys_page; |
| 76 |
old_pagehandler=_old_pagehandler; |
| 77 |
flags=old_pagehandler->flags|PFLAG_HASCODE; |
| 78 |
flags&=~PFLAG_WRITEABLE; |
| 79 |
active_blocks=0; |
| 80 |
active_count=16; |
| 81 |
memset(&hash_map,0,sizeof(hash_map)); |
| 82 |
memset(&write_map,0,sizeof(write_map)); |
| 83 |
if (invalidation_map!=NULL) { |
| 84 |
free(invalidation_map); |
| 85 |
invalidation_map=NULL; |
| 86 |
} |
| 87 |
} |
| 88 |
bool InvalidateRange(Bitu start,Bitu end) { |
| 89 |
Bits index=1+(end>>DYN_HASH_SHIFT); |
| 90 |
bool is_current_block=false; |
| 91 |
Bit32u ip_point=SegPhys(cs)+reg_eip; |
| 92 |
ip_point=(PAGING_GetPhysicalPage(ip_point)-(phys_page<<12))+(ip_point&0xfff); |
| 93 |
while (index>=0) { |
| 94 |
Bitu map=0; |
| 95 |
for (Bitu count=start;count<=end;count++) map+=write_map[count]; |
| 96 |
if (!map) return is_current_block; |
| 97 |
CacheBlock * block=hash_map[index]; |
| 98 |
while (block) { |
| 99 |
CacheBlock * nextblock=block->hash.next; |
| 100 |
if (start<=block->page.end && end>=block->page.start) { |
| 101 |
if (ip_point<=block->page.end && ip_point>=block->page.start) is_current_block=true; |
| 102 |
block->Clear(); |
| 103 |
} |
| 104 |
block=nextblock; |
| 105 |
} |
| 106 |
index--; |
| 107 |
} |
| 108 |
return is_current_block; |
| 109 |
} |
| 110 |
void writeb(PhysPt addr,Bitu val){ |
| 111 |
if (GCC_UNLIKELY(old_pagehandler->flags&PFLAG_HASROM)) return; |
| 112 |
if (GCC_UNLIKELY((old_pagehandler->flags&PFLAG_READABLE)!=PFLAG_READABLE)) { |
| 113 |
E_Exit("wb:non-readable code page found that is no ROM page"); |
| 114 |
} |
| 115 |
addr&=4095; |
| 116 |
if (host_readb(hostmem+addr)==(Bit8u)val) return; |
| 117 |
host_writeb(hostmem+addr,val); |
| 118 |
if (!*(Bit8u*)&write_map[addr]) { |
| 119 |
if (active_blocks) return; |
| 120 |
active_count--; |
| 121 |
if (!active_count) Release(); |
| 122 |
return; |
| 123 |
} else if (!invalidation_map) { |
| 124 |
invalidation_map=(Bit8u*)malloc(4096); |
| 125 |
memset(invalidation_map,0,4096); |
| 126 |
} |
| 127 |
invalidation_map[addr]++; |
| 128 |
InvalidateRange(addr,addr); |
| 129 |
} |
| 130 |
void writew(PhysPt addr,Bitu val){ |
| 131 |
if (GCC_UNLIKELY(old_pagehandler->flags&PFLAG_HASROM)) return; |
| 132 |
if (GCC_UNLIKELY((old_pagehandler->flags&PFLAG_READABLE)!=PFLAG_READABLE)) { |
| 133 |
E_Exit("ww:non-readable code page found that is no ROM page"); |
| 134 |
} |
| 135 |
addr&=4095; |
| 136 |
if (host_readw(hostmem+addr)==(Bit16u)val) return; |
| 137 |
host_writew(hostmem+addr,val); |
| 138 |
if (!*(Bit16u*)&write_map[addr]) { |
| 139 |
if (active_blocks) return; |
| 140 |
active_count--; |
| 141 |
if (!active_count) Release(); |
| 142 |
return; |
| 143 |
} else if (!invalidation_map) { |
| 144 |
invalidation_map=(Bit8u*)malloc(4096); |
| 145 |
memset(invalidation_map,0,4096); |
| 146 |
} |
| 147 |
(*(Bit16u*)&invalidation_map[addr])+=0x101; |
| 148 |
InvalidateRange(addr,addr+1); |
| 149 |
} |
| 150 |
void writed(PhysPt addr,Bitu val){ |
| 151 |
if (GCC_UNLIKELY(old_pagehandler->flags&PFLAG_HASROM)) return; |
| 152 |
if (GCC_UNLIKELY((old_pagehandler->flags&PFLAG_READABLE)!=PFLAG_READABLE)) { |
| 153 |
E_Exit("wd:non-readable code page found that is no ROM page"); |
| 154 |
} |
| 155 |
addr&=4095; |
| 156 |
if (host_readd(hostmem+addr)==(Bit32u)val) return; |
| 157 |
host_writed(hostmem+addr,val); |
| 158 |
if (!*(Bit32u*)&write_map[addr]) { |
| 159 |
if (active_blocks) return; |
| 160 |
active_count--; |
| 161 |
if (!active_count) Release(); |
| 162 |
return; |
| 163 |
} else if (!invalidation_map) { |
| 164 |
invalidation_map=(Bit8u*)malloc(4096); |
| 165 |
memset(invalidation_map,0,4096); |
| 166 |
} |
| 167 |
(*(Bit32u*)&invalidation_map[addr])+=0x1010101; |
| 168 |
InvalidateRange(addr,addr+3); |
| 169 |
} |
| 170 |
bool writeb_checked(PhysPt addr,Bitu val) { |
| 171 |
if (GCC_UNLIKELY(old_pagehandler->flags&PFLAG_HASROM)) return false; |
| 172 |
if (GCC_UNLIKELY((old_pagehandler->flags&PFLAG_READABLE)!=PFLAG_READABLE)) { |
| 173 |
E_Exit("cb:non-readable code page found that is no ROM page"); |
| 174 |
} |
| 175 |
addr&=4095; |
| 176 |
if (host_readb(hostmem+addr)==(Bit8u)val) return false; |
| 177 |
if (!*(Bit8u*)&write_map[addr]) { |
| 178 |
if (!active_blocks) { |
| 179 |
active_count--; |
| 180 |
if (!active_count) Release(); |
| 181 |
} |
| 182 |
} else { |
| 183 |
if (!invalidation_map) { |
| 184 |
invalidation_map=(Bit8u*)malloc(4096); |
| 185 |
memset(invalidation_map,0,4096); |
| 186 |
} |
| 187 |
invalidation_map[addr]++; |
| 188 |
if (InvalidateRange(addr,addr)) { |
| 189 |
cpu.exception.which=SMC_CURRENT_BLOCK; |
| 190 |
return true; |
| 191 |
} |
| 192 |
} |
| 193 |
host_writeb(hostmem+addr,val); |
| 194 |
return false; |
| 195 |
} |
| 196 |
bool writew_checked(PhysPt addr,Bitu val) { |
| 197 |
if (GCC_UNLIKELY(old_pagehandler->flags&PFLAG_HASROM)) return false; |
| 198 |
if (GCC_UNLIKELY((old_pagehandler->flags&PFLAG_READABLE)!=PFLAG_READABLE)) { |
| 199 |
E_Exit("cw:non-readable code page found that is no ROM page"); |
| 200 |
} |
| 201 |
addr&=4095; |
| 202 |
if (host_readw(hostmem+addr)==(Bit16u)val) return false; |
| 203 |
if (!*(Bit16u*)&write_map[addr]) { |
| 204 |
if (!active_blocks) { |
| 205 |
active_count--; |
| 206 |
if (!active_count) Release(); |
| 207 |
} |
| 208 |
} else { |
| 209 |
if (!invalidation_map) { |
| 210 |
invalidation_map=(Bit8u*)malloc(4096); |
| 211 |
memset(invalidation_map,0,4096); |
| 212 |
} |
| 213 |
(*(Bit16u*)&invalidation_map[addr])+=0x101; |
| 214 |
if (InvalidateRange(addr,addr+1)) { |
| 215 |
cpu.exception.which=SMC_CURRENT_BLOCK; |
| 216 |
return true; |
| 217 |
} |
| 218 |
} |
| 219 |
host_writew(hostmem+addr,val); |
| 220 |
return false; |
| 221 |
} |
| 222 |
bool writed_checked(PhysPt addr,Bitu val) { |
| 223 |
if (GCC_UNLIKELY(old_pagehandler->flags&PFLAG_HASROM)) return false; |
| 224 |
if (GCC_UNLIKELY((old_pagehandler->flags&PFLAG_READABLE)!=PFLAG_READABLE)) { |
| 225 |
E_Exit("cd:non-readable code page found that is no ROM page"); |
| 226 |
} |
| 227 |
addr&=4095; |
| 228 |
if (host_readd(hostmem+addr)==(Bit32u)val) return false; |
| 229 |
if (!*(Bit32u*)&write_map[addr]) { |
| 230 |
if (!active_blocks) { |
| 231 |
active_count--; |
| 232 |
if (!active_count) Release(); |
| 233 |
} |
| 234 |
} else { |
| 235 |
if (!invalidation_map) { |
| 236 |
invalidation_map=(Bit8u*)malloc(4096); |
| 237 |
memset(invalidation_map,0,4096); |
| 238 |
} |
| 239 |
(*(Bit32u*)&invalidation_map[addr])+=0x1010101; |
| 240 |
if (InvalidateRange(addr,addr+3)) { |
| 241 |
cpu.exception.which=SMC_CURRENT_BLOCK; |
| 242 |
return true; |
| 243 |
} |
| 244 |
} |
| 245 |
host_writed(hostmem+addr,val); |
| 246 |
return false; |
| 247 |
} |
| 248 |
void AddCacheBlock(CacheBlock * block) { |
| 249 |
Bitu index=1+(block->page.start>>DYN_HASH_SHIFT); |
| 250 |
block->hash.next=hash_map[index]; |
| 251 |
block->hash.index=index; |
| 252 |
hash_map[index]=block; |
| 253 |
block->page.handler=this; |
| 254 |
active_blocks++; |
| 255 |
} |
| 256 |
void AddCrossBlock(CacheBlock * block) { |
| 257 |
block->hash.next=hash_map[0]; |
| 258 |
block->hash.index=0; |
| 259 |
hash_map[0]=block; |
| 260 |
block->page.handler=this; |
| 261 |
active_blocks++; |
| 262 |
} |
| 263 |
void DelCacheBlock(CacheBlock * block) { |
| 264 |
active_blocks--; |
| 265 |
active_count=16; |
| 266 |
CacheBlock * * where=&hash_map[block->hash.index]; |
| 267 |
while (*where!=block) { |
| 268 |
where=&((*where)->hash.next); |
| 269 |
//Will crash if a block isn't found, which should never happen. |
| 270 |
} |
| 271 |
*where=block->hash.next; |
| 272 |
if (GCC_UNLIKELY(block->cache.wmapmask!=NULL)) { |
| 273 |
for (Bitu i=block->page.start;i<block->cache.maskstart;i++) { |
| 274 |
if (write_map[i]) write_map[i]--; |
| 275 |
} |
| 276 |
Bitu maskct=0; |
| 277 |
for (Bitu i=block->cache.maskstart;i<=block->page.end;i++,maskct++) { |
| 278 |
if (write_map[i]) { |
| 279 |
if ((maskct>=block->cache.masklen) || (!block->cache.wmapmask[maskct])) write_map[i]--; |
| 280 |
} |
| 281 |
} |
| 282 |
free(block->cache.wmapmask); |
| 283 |
block->cache.wmapmask=NULL; |
| 284 |
} else { |
| 285 |
for (Bitu i=block->page.start;i<=block->page.end;i++) { |
| 286 |
if (write_map[i]) write_map[i]--; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
void Release(void) { |
| 291 |
MEM_SetPageHandler(phys_page,1,old_pagehandler); |
| 292 |
PAGING_ClearTLB(); |
| 293 |
if (prev) prev->next=next; |
| 294 |
else cache.used_pages=next; |
| 295 |
if (next) next->prev=prev; |
| 296 |
else cache.last_page=prev; |
| 297 |
next=cache.free_pages; |
| 298 |
cache.free_pages=this; |
| 299 |
prev=0; |
| 300 |
} |
| 301 |
void ClearRelease(void) { |
| 302 |
for (Bitu index=0;index<(1+DYN_PAGE_HASH);index++) { |
| 303 |
CacheBlock * block=hash_map[index]; |
| 304 |
while (block) { |
| 305 |
CacheBlock * nextblock=block->hash.next; |
| 306 |
block->page.handler=0; //No need, full clear |
| 307 |
block->Clear(); |
| 308 |
block=nextblock; |
| 309 |
} |
| 310 |
} |
| 311 |
Release(); |
| 312 |
} |
| 313 |
CacheBlock * FindCacheBlock(Bitu start) { |
| 314 |
CacheBlock * block=hash_map[1+(start>>DYN_HASH_SHIFT)]; |
| 315 |
while (block) { |
| 316 |
if (block->page.start==start) return block; |
| 317 |
block=block->hash.next; |
| 318 |
} |
| 319 |
return 0; |
| 320 |
} |
| 321 |
HostPt GetHostReadPt(Bitu phys_page) { |
| 322 |
hostmem=old_pagehandler->GetHostReadPt(phys_page); |
| 323 |
return hostmem; |
| 324 |
} |
| 325 |
HostPt GetHostWritePt(Bitu phys_page) { |
| 326 |
return GetHostReadPt( phys_page ); |
| 327 |
} |
| 328 |
public: |
| 329 |
Bit8u write_map[4096]; |
| 330 |
Bit8u * invalidation_map; |
| 331 |
CodePageHandler * next, * prev; |
| 332 |
private: |
| 333 |
PageHandler * old_pagehandler; |
| 334 |
CacheBlock * hash_map[1+DYN_PAGE_HASH]; |
| 335 |
Bitu active_blocks; |
| 336 |
Bitu active_count; |
| 337 |
HostPt hostmem; |
| 338 |
Bitu phys_page; |
| 339 |
}; |
| 340 |
|
| 341 |
|
| 342 |
static INLINE void cache_addunsedblock(CacheBlock * block) { |
| 343 |
block->cache.next=cache.block.free; |
| 344 |
cache.block.free=block; |
| 345 |
} |
| 346 |
|
| 347 |
static CacheBlock * cache_getblock(void) { |
| 348 |
CacheBlock * ret=cache.block.free; |
| 349 |
if (!ret) E_Exit("Ran out of CacheBlocks" ); |
| 350 |
cache.block.free=ret->cache.next; |
| 351 |
ret->cache.next=0; |
| 352 |
return ret; |
| 353 |
} |
| 354 |
|
| 355 |
void CacheBlock::Clear(void) { |
| 356 |
Bitu ind; |
| 357 |
/* Check if this is not a cross page block */ |
| 358 |
if (hash.index) for (ind=0;ind<2;ind++) { |
| 359 |
CacheBlock * fromlink=link[ind].from; |
| 360 |
link[ind].from=0; |
| 361 |
while (fromlink) { |
| 362 |
CacheBlock * nextlink=fromlink->link[ind].next; |
| 363 |
fromlink->link[ind].next=0; |
| 364 |
fromlink->link[ind].to=&link_blocks[ind]; |
| 365 |
fromlink=nextlink; |
| 366 |
} |
| 367 |
if (link[ind].to!=&link_blocks[ind]) { |
| 368 |
CacheBlock * * wherelink=&link[ind].to->link[ind].from; |
| 369 |
while (*wherelink != this && *wherelink) { |
| 370 |
wherelink = &(*wherelink)->link[ind].next; |
| 371 |
} |
| 372 |
if(*wherelink) |
| 373 |
*wherelink = (*wherelink)->link[ind].next; |
| 374 |
else |
| 375 |
LOG(LOG_CPU,LOG_ERROR)("Cache anomaly. please investigate"); |
| 376 |
} |
| 377 |
} else |
| 378 |
cache_addunsedblock(this); |
| 379 |
if (crossblock) { |
| 380 |
crossblock->crossblock=0; |
| 381 |
crossblock->Clear(); |
| 382 |
crossblock=0; |
| 383 |
} |
| 384 |
if (page.handler) { |
| 385 |
page.handler->DelCacheBlock(this); |
| 386 |
page.handler=0; |
| 387 |
} |
| 388 |
if (cache.wmapmask){ |
| 389 |
free(cache.wmapmask); |
| 390 |
cache.wmapmask=NULL; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
static CacheBlock * cache_openblock(void) { |
| 396 |
CacheBlock * block=cache.block.active; |
| 397 |
/* check for enough space in this block */ |
| 398 |
Bitu size=block->cache.size; |
| 399 |
CacheBlock * nextblock=block->cache.next; |
| 400 |
if (block->page.handler) |
| 401 |
block->Clear(); |
| 402 |
while (size<CACHE_MAXSIZE) { |
| 403 |
if (!nextblock) |
| 404 |
goto skipresize; |
| 405 |
size+=nextblock->cache.size; |
| 406 |
CacheBlock * tempblock=nextblock->cache.next; |
| 407 |
if (nextblock->page.handler) |
| 408 |
nextblock->Clear(); |
| 409 |
cache_addunsedblock(nextblock); |
| 410 |
nextblock=tempblock; |
| 411 |
} |
| 412 |
skipresize: |
| 413 |
block->cache.size=size; |
| 414 |
block->cache.next=nextblock; |
| 415 |
cache.pos=block->cache.start; |
| 416 |
return block; |
| 417 |
} |
| 418 |
|
| 419 |
static void cache_closeblock(void) { |
| 420 |
CacheBlock * block=cache.block.active; |
| 421 |
block->link[0].to=&link_blocks[0]; |
| 422 |
block->link[1].to=&link_blocks[1]; |
| 423 |
block->link[0].from=0; |
| 424 |
block->link[1].from=0; |
| 425 |
block->link[0].next=0; |
| 426 |
block->link[1].next=0; |
| 427 |
/* Close the block with correct alignments */ |
| 428 |
Bitu written=cache.pos-block->cache.start; |
| 429 |
if (written>block->cache.size) { |
| 430 |
if (!block->cache.next) { |
| 431 |
if (written>block->cache.size+CACHE_MAXSIZE) E_Exit("CacheBlock overrun 1 %d",written-block->cache.size); |
| 432 |
} else E_Exit("CacheBlock overrun 2 written %d size %d",written,block->cache.size); |
| 433 |
} else { |
| 434 |
Bitu new_size; |
| 435 |
Bitu left=block->cache.size-written; |
| 436 |
/* Smaller than cache align then don't bother to resize */ |
| 437 |
if (left>CACHE_ALIGN) { |
| 438 |
new_size=((written-1)|(CACHE_ALIGN-1))+1; |
| 439 |
CacheBlock * newblock=cache_getblock(); |
| 440 |
newblock->cache.start=block->cache.start+new_size; |
| 441 |
newblock->cache.size=block->cache.size-new_size; |
| 442 |
newblock->cache.next=block->cache.next; |
| 443 |
block->cache.next=newblock; |
| 444 |
block->cache.size=new_size; |
| 445 |
} |
| 446 |
} |
| 447 |
/* Advance the active block pointer */ |
| 448 |
if (!block->cache.next) { |
| 449 |
// LOG_MSG("Cache full restarting"); |
| 450 |
cache.block.active=cache.block.first; |
| 451 |
} else { |
| 452 |
cache.block.active=block->cache.next; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
static INLINE void cache_addb(Bit8u val) { |
| 457 |
*cache.pos++=val; |
| 458 |
} |
| 459 |
|
| 460 |
static INLINE void cache_addw(Bit16u val) { |
| 461 |
*(Bit16u*)cache.pos=val; |
| 462 |
cache.pos+=2; |
| 463 |
} |
| 464 |
|
| 465 |
static INLINE void cache_addd(Bit32u val) { |
| 466 |
*(Bit32u*)cache.pos=val; |
| 467 |
cache.pos+=4; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
static void gen_return(BlockReturn retcode); |
| 472 |
|
| 473 |
static Bit8u * cache_code_start_ptr=NULL; |
| 474 |
static Bit8u * cache_code=NULL; |
| 475 |
static Bit8u * cache_code_link_blocks=NULL; |
| 476 |
static CacheBlock * cache_blocks=NULL; |
| 477 |
|
| 478 |
/* Define temporary pagesize so the MPROTECT case and the regular case share as much code as possible */ |
| 479 |
#if (C_HAVE_MPROTECT) |
| 480 |
#define PAGESIZE_TEMP PAGESIZE |
| 481 |
#else |
| 482 |
#define PAGESIZE_TEMP 4096 |
| 483 |
#endif |
| 484 |
|
| 485 |
static bool cache_initialized = false; |
| 486 |
|
| 487 |
static void cache_init(bool enable) { |
| 488 |
Bits i; |
| 489 |
if (enable) { |
| 490 |
if (cache_initialized) return; |
| 491 |
cache_initialized = true; |
| 492 |
if (cache_blocks == NULL) { |
| 493 |
cache_blocks=(CacheBlock*)malloc(CACHE_BLOCKS*sizeof(CacheBlock)); |
| 494 |
if(!cache_blocks) E_Exit("Allocating cache_blocks has failed"); |
| 495 |
memset(cache_blocks,0,sizeof(CacheBlock)*CACHE_BLOCKS); |
| 496 |
cache.block.free=&cache_blocks[0]; |
| 497 |
for (i=0;i<CACHE_BLOCKS-1;i++) { |
| 498 |
cache_blocks[i].link[0].to=(CacheBlock *)1; |
| 499 |
cache_blocks[i].link[1].to=(CacheBlock *)1; |
| 500 |
cache_blocks[i].cache.next=&cache_blocks[i+1]; |
| 501 |
} |
| 502 |
} |
| 503 |
if (cache_code_start_ptr==NULL) { |
| 504 |
#if defined (WIN32) |
| 505 |
cache_code_start_ptr=(Bit8u*)VirtualAlloc(0,CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP, |
| 506 |
MEM_COMMIT,PAGE_EXECUTE_READWRITE); |
| 507 |
if (!cache_code_start_ptr) |
| 508 |
cache_code_start_ptr=(Bit8u*)malloc(CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP); |
| 509 |
#else |
| 510 |
cache_code_start_ptr=(Bit8u*)malloc(CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP); |
| 511 |
#endif |
| 512 |
if(!cache_code_start_ptr) E_Exit("Allocating dynamic core cache memory failed"); |
| 513 |
|
| 514 |
cache_code=(Bit8u*)(((Bitu)cache_code_start_ptr + PAGESIZE_TEMP-1) & ~(PAGESIZE_TEMP-1)); //Bitu is same size as a pointer. |
| 515 |
|
| 516 |
cache_code_link_blocks=cache_code; |
| 517 |
cache_code+=PAGESIZE_TEMP; |
| 518 |
|
| 519 |
#if (C_HAVE_MPROTECT) |
| 520 |
if(mprotect(cache_code_link_blocks,CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP,PROT_WRITE|PROT_READ|PROT_EXEC)) |
| 521 |
LOG_MSG("Setting excute permission on the code cache has failed!"); |
| 522 |
#endif |
| 523 |
CacheBlock * block=cache_getblock(); |
| 524 |
cache.block.first=block; |
| 525 |
cache.block.active=block; |
| 526 |
block->cache.start=&cache_code[0]; |
| 527 |
block->cache.size=CACHE_TOTAL; |
| 528 |
block->cache.next=0; //Last block in the list |
| 529 |
} |
| 530 |
/* Setup the default blocks for block linkage returns */ |
| 531 |
cache.pos=&cache_code_link_blocks[0]; |
| 532 |
link_blocks[0].cache.start=cache.pos; |
| 533 |
gen_return(BR_Link1); |
| 534 |
cache.pos=&cache_code_link_blocks[32]; |
| 535 |
link_blocks[1].cache.start=cache.pos; |
| 536 |
gen_return(BR_Link2); |
| 537 |
cache.free_pages=0; |
| 538 |
cache.last_page=0; |
| 539 |
cache.used_pages=0; |
| 540 |
/* Setup the code pages */ |
| 541 |
for (i=0;i<CACHE_PAGES;i++) { |
| 542 |
CodePageHandler * newpage=new CodePageHandler(); |
| 543 |
newpage->next=cache.free_pages; |
| 544 |
cache.free_pages=newpage; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
static void cache_close(void) { |
| 550 |
/* for (;;) { |
| 551 |
if (cache.used_pages) { |
| 552 |
CodePageHandler * cpage=cache.used_pages; |
| 553 |
CodePageHandler * npage=cache.used_pages->next; |
| 554 |
cpage->ClearRelease(); |
| 555 |
delete cpage; |
| 556 |
cache.used_pages=npage; |
| 557 |
} else break; |
| 558 |
} |
| 559 |
if (cache_blocks != NULL) { |
| 560 |
free(cache_blocks); |
| 561 |
cache_blocks = NULL; |
| 562 |
} |
| 563 |
if (cache_code_start_ptr != NULL) { |
| 564 |
### care: under windows VirtualFree() has to be used if |
| 565 |
### VirtualAlloc was used for memory allocation |
| 566 |
free(cache_code_start_ptr); |
| 567 |
cache_code_start_ptr = NULL; |
| 568 |
} |
| 569 |
cache_code = NULL; |
| 570 |
cache_code_link_blocks = NULL; |
| 571 |
cache_initialized = false; */ |
| 572 |
} |
| 573 |
|
| 574 |
static void cache_reset(void) { |
| 575 |
if (cache_initialized) { |
| 576 |
for (;;) { |
| 577 |
if (cache.used_pages) { |
| 578 |
CodePageHandler * cpage=cache.used_pages; |
| 579 |
CodePageHandler * npage=cache.used_pages->next; |
| 580 |
cpage->ClearRelease(); |
| 581 |
delete cpage; |
| 582 |
cache.used_pages=npage; |
| 583 |
} else break; |
| 584 |
} |
| 585 |
|
| 586 |
if (cache_blocks == NULL) { |
| 587 |
cache_blocks=(CacheBlock*)malloc(CACHE_BLOCKS*sizeof(CacheBlock)); |
| 588 |
if(!cache_blocks) E_Exit("Allocating cache_blocks has failed"); |
| 589 |
} |
| 590 |
memset(cache_blocks,0,sizeof(CacheBlock)*CACHE_BLOCKS); |
| 591 |
cache.block.free=&cache_blocks[0]; |
| 592 |
for (Bits i=0;i<CACHE_BLOCKS-1;i++) { |
| 593 |
cache_blocks[i].link[0].to=(CacheBlock *)1; |
| 594 |
cache_blocks[i].link[1].to=(CacheBlock *)1; |
| 595 |
cache_blocks[i].cache.next=&cache_blocks[i+1]; |
| 596 |
} |
| 597 |
|
| 598 |
if (cache_code_start_ptr==NULL) { |
| 599 |
#if defined (WIN32) |
| 600 |
cache_code_start_ptr=(Bit8u*)VirtualAlloc(0,CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP, |
| 601 |
MEM_COMMIT,PAGE_EXECUTE_READWRITE); |
| 602 |
if (!cache_code_start_ptr) |
| 603 |
cache_code_start_ptr=(Bit8u*)malloc(CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP); |
| 604 |
#else |
| 605 |
cache_code_start_ptr=(Bit8u*)malloc(CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP-1+PAGESIZE_TEMP); |
| 606 |
#endif |
| 607 |
if (!cache_code_start_ptr) E_Exit("Allocating dynamic core cache memory failed"); |
| 608 |
|
| 609 |
cache_code=(Bit8u*)(((Bitu)cache_code_start_ptr + PAGESIZE_TEMP-1) & ~(PAGESIZE_TEMP-1)); //Bitu is same size as a pointer. |
| 610 |
|
| 611 |
cache_code_link_blocks=cache_code; |
| 612 |
cache_code+=PAGESIZE_TEMP; |
| 613 |
|
| 614 |
#if (C_HAVE_MPROTECT) |
| 615 |
if(mprotect(cache_code_link_blocks,CACHE_TOTAL+CACHE_MAXSIZE+PAGESIZE_TEMP,PROT_WRITE|PROT_READ|PROT_EXEC)) |
| 616 |
LOG_MSG("Setting excute permission on the code cache has failed!"); |
| 617 |
#endif |
| 618 |
} |
| 619 |
|
| 620 |
CacheBlock * block=cache_getblock(); |
| 621 |
cache.block.first=block; |
| 622 |
cache.block.active=block; |
| 623 |
block->cache.start=&cache_code[0]; |
| 624 |
block->cache.size=CACHE_TOTAL; |
| 625 |
block->cache.next=0; //Last block in the list |
| 626 |
|
| 627 |
/* Setup the default blocks for block linkage returns */ |
| 628 |
cache.pos=&cache_code_link_blocks[0]; |
| 629 |
link_blocks[0].cache.start=cache.pos; |
| 630 |
gen_return(BR_Link1); |
| 631 |
cache.pos=&cache_code_link_blocks[32]; |
| 632 |
link_blocks[1].cache.start=cache.pos; |
| 633 |
gen_return(BR_Link2); |
| 634 |
cache.free_pages=0; |
| 635 |
cache.last_page=0; |
| 636 |
cache.used_pages=0; |
| 637 |
/* Setup the code pages */ |
| 638 |
for (Bitu i=0;i<CACHE_PAGES;i++) { |
| 639 |
CodePageHandler * newpage=new CodePageHandler(); |
| 640 |
newpage->next=cache.free_pages; |
| 641 |
cache.free_pages=newpage; |
| 642 |
} |
| 643 |
} |
| 644 |
} |