Debian Codegraph @

src/cpu/paging.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: paging.cpp,v 1.36 2009-05-27 09:15:41 qbix79 Exp $ */
20
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <string.h>
24
25 #include "dosbox.h"
26 #include "mem.h"
27 #include "paging.h"
28 #include "regs.h"
29 #include "lazyflags.h"
30 #include "cpu.h"
31 #include "debug.h"
32 #include "setup.h"
33
34 #define LINK_TOTAL (64*1024)
35
36 #define USERWRITE_PROHIBITED ((cpu.cpl&cpu.mpl)==3)
37
38
39 PagingBlock paging;
40
41
42 Bitu PageHandler::readb(PhysPt addr) {
43 E_Exit("No byte handler for read from %d",addr);
44 return 0;
45 }
46 Bitu PageHandler::readw(PhysPt addr) {
47 Bitu ret = (readb(addr+0) << 0);
48 ret |= (readb(addr+1) << 8);
49 return ret;
50 }
51 Bitu PageHandler::readd(PhysPt addr) {
52 Bitu ret = (readb(addr+0) << 0);
53 ret |= (readb(addr+1) << 8);
54 ret |= (readb(addr+2) << 16);
55 ret |= (readb(addr+3) << 24);
56 return ret;
57 }
58
59 void PageHandler::writeb(PhysPt addr,Bitu /*val*/) {
60 E_Exit("No byte handler for write to %d",addr);
61 }
62
63 void PageHandler::writew(PhysPt addr,Bitu val) {
64 writeb(addr+0,(Bit8u) (val >> 0));
65 writeb(addr+1,(Bit8u) (val >> 8));
66 }
67 void PageHandler::writed(PhysPt addr,Bitu val) {
68 writeb(addr+0,(Bit8u) (val >> 0));
69 writeb(addr+1,(Bit8u) (val >> 8));
70 writeb(addr+2,(Bit8u) (val >> 16));
71 writeb(addr+3,(Bit8u) (val >> 24));
72 }
73
74 HostPt PageHandler::GetHostReadPt(Bitu /*phys_page*/) {
75 return 0;
76 }
77
78 HostPt PageHandler::GetHostWritePt(Bitu /*phys_page*/) {
79 return 0;
80 }
81
82 bool PageHandler::readb_checked(PhysPt addr, Bit8u * val) {
83 *val=(Bit8u)readb(addr); return false;
84 }
85 bool PageHandler::readw_checked(PhysPt addr, Bit16u * val) {
86 *val=(Bit16u)readw(addr); return false;
87 }
88 bool PageHandler::readd_checked(PhysPt addr, Bit32u * val) {
89 *val=(Bit32u)readd(addr); return false;
90 }
91 bool PageHandler::writeb_checked(PhysPt addr,Bitu val) {
92 writeb(addr,val); return false;
93 }
94 bool PageHandler::writew_checked(PhysPt addr,Bitu val) {
95 writew(addr,val); return false;
96 }
97 bool PageHandler::writed_checked(PhysPt addr,Bitu val) {
98 writed(addr,val); return false;
99 }
100
101
102
103 struct PF_Entry {
104 Bitu cs;
105 Bitu eip;
106 Bitu page_addr;
107 Bitu mpl;
108 };
109
110 #define PF_QUEUESIZE 16
111 static struct {
112 Bitu used;
113 PF_Entry entries[PF_QUEUESIZE];
114 } pf_queue;
115
116 static Bits PageFaultCore(void) {
117 CPU_CycleLeft+=CPU_Cycles;
118 CPU_Cycles=1;
119 Bits ret=CPU_Core_Full_Run();
120 CPU_CycleLeft+=CPU_Cycles;
121 if (ret<0) E_Exit("Got a dosbox close machine in pagefault core?");
122 if (ret)
123 return ret;
124 if (!pf_queue.used) E_Exit("PF Core without PF");
125 PF_Entry * entry=&pf_queue.entries[pf_queue.used-1];
126 X86PageEntry pentry;
127 pentry.load=phys_readd(entry->page_addr);
128 if (pentry.block.p && entry->cs == SegValue(cs) && entry->eip==reg_eip) {
129 cpu.mpl=entry->mpl;
130 return -1;
131 }
132 return 0;
133 }
134 #if C_DEBUG
135 Bitu DEBUG_EnableDebugger(void);
136 #endif
137
138 bool first=false;
139
140 void PAGING_PageFault(PhysPt lin_addr,Bitu page_addr,Bitu faultcode) {
141 /* Save the state of the cpu cores */
142 LazyFlags old_lflags;
143 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
144 CPU_Decoder * old_cpudecoder;
145 old_cpudecoder=cpudecoder;
146 cpudecoder=&PageFaultCore;
147 paging.cr2=lin_addr;
148 PF_Entry * entry=&pf_queue.entries[pf_queue.used++];
149 LOG(LOG_PAGING,LOG_NORMAL)("PageFault at %X type [%x] queue %d",lin_addr,faultcode,pf_queue.used);
150 // LOG_MSG("EAX:%04X ECX:%04X EDX:%04X EBX:%04X",reg_eax,reg_ecx,reg_edx,reg_ebx);
151 // LOG_MSG("CS:%04X EIP:%08X SS:%04x SP:%08X",SegValue(cs),reg_eip,SegValue(ss),reg_esp);
152 entry->cs=SegValue(cs);
153 entry->eip=reg_eip;
154 entry->page_addr=page_addr;
155 entry->mpl=cpu.mpl;
156 cpu.mpl=3;
157
158 CPU_Exception(EXCEPTION_PF,faultcode);
159 #if C_DEBUG
160 // DEBUG_EnableDebugger();
161 #endif
162 DOSBOX_RunMachine();
163 pf_queue.used--;
164 LOG(LOG_PAGING,LOG_NORMAL)("Left PageFault for %x queue %d",lin_addr,pf_queue.used);
165 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
166 cpudecoder=old_cpudecoder;
167 // LOG_MSG("SS:%04x SP:%08X",SegValue(ss),reg_esp);
168 }
169
170 static INLINE void InitPageUpdateLink(Bitu relink,PhysPt addr) {
171 if (relink==0) return;
172 if (paging.links.used) {
173 if (paging.links.entries[paging.links.used-1]==(addr>>12)) {
174 paging.links.used--;
175 PAGING_UnlinkPages(addr>>12,1);
176 }
177 }
178 if (relink>1) PAGING_LinkPage_ReadOnly(addr>>12,relink);
179 }
180
181 static INLINE void InitPageCheckPresence(PhysPt lin_addr,bool writing,X86PageEntry& table,X86PageEntry& entry) {
182 Bitu lin_page=lin_addr >> 12;
183 Bitu d_index=lin_page >> 10;
184 Bitu t_index=lin_page & 0x3ff;
185 Bitu table_addr=(paging.base.page<<12)+d_index*4;
186 table.load=phys_readd(table_addr);
187 if (!table.block.p) {
188 LOG(LOG_PAGING,LOG_NORMAL)("NP Table");
189 PAGING_PageFault(lin_addr,table_addr,
190 (writing?0x02:0x00) | (((cpu.cpl&cpu.mpl)==0)?0x00:0x04));
191 table.load=phys_readd(table_addr);
192 if (GCC_UNLIKELY(!table.block.p))
193 E_Exit("Pagefault didn't correct table");
194 }
195 Bitu entry_addr=(table.block.base<<12)+t_index*4;
196 entry.load=phys_readd(entry_addr);
197 if (!entry.block.p) {
198 // LOG(LOG_PAGING,LOG_NORMAL)("NP Page");
199 PAGING_PageFault(lin_addr,entry_addr,
200 (writing?0x02:0x00) | (((cpu.cpl&cpu.mpl)==0)?0x00:0x04));
201 entry.load=phys_readd(entry_addr);
202 if (GCC_UNLIKELY(!entry.block.p))
203 E_Exit("Pagefault didn't correct page");
204 }
205 }
206
207 static INLINE bool InitPageCheckPresence_CheckOnly(PhysPt lin_addr,bool writing,X86PageEntry& table,X86PageEntry& entry) {
208 Bitu lin_page=lin_addr >> 12;
209 Bitu d_index=lin_page >> 10;
210 Bitu t_index=lin_page & 0x3ff;
211 Bitu table_addr=(paging.base.page<<12)+d_index*4;
212 table.load=phys_readd(table_addr);
213 if (!table.block.p) {
214 paging.cr2=lin_addr;
215 cpu.exception.which=EXCEPTION_PF;
216 cpu.exception.error=(writing?0x02:0x00) | (((cpu.cpl&cpu.mpl)==0)?0x00:0x04);
217 return false;
218 }
219 Bitu entry_addr=(table.block.base<<12)+t_index*4;
220 entry.load=phys_readd(entry_addr);
221 if (!entry.block.p) {
222 paging.cr2=lin_addr;
223 cpu.exception.which=EXCEPTION_PF;
224 cpu.exception.error=(writing?0x02:0x00) | (((cpu.cpl&cpu.mpl)==0)?0x00:0x04);
225 return false;
226 }
227 return true;
228 }
229
230 // check if a user-level memory access would trigger a privilege page fault
231 static INLINE bool InitPage_CheckUseraccess(Bitu u1,Bitu u2) {
232 switch (CPU_ArchitectureType) {
233 case CPU_ARCHTYPE_MIXED:
234 case CPU_ARCHTYPE_386SLOW:
235 case CPU_ARCHTYPE_386FAST:
236 default:
237 return ((u1)==0) && ((u2)==0);
238 case CPU_ARCHTYPE_486OLDSLOW:
239 case CPU_ARCHTYPE_486NEWSLOW:
240 case CPU_ARCHTYPE_PENTIUMSLOW:
241 return ((u1)==0) || ((u2)==0);
242 }
243 }
244
245
246 class InitPageHandler : public PageHandler {
247 public:
248 InitPageHandler() {
249 flags=PFLAG_INIT|PFLAG_NOCODE;
250 }
251 Bitu readb(PhysPt addr) {
252 Bitu needs_reset=InitPage(addr,false);
253 Bit8u val=mem_readb(addr);
254 InitPageUpdateLink(needs_reset,addr);
255 return val;
256 }
257 Bitu readw(PhysPt addr) {
258 Bitu needs_reset=InitPage(addr,false);
259 Bit16u val=mem_readw(addr);
260 InitPageUpdateLink(needs_reset,addr);
261 return val;
262 }
263 Bitu readd(PhysPt addr) {
264 Bitu needs_reset=InitPage(addr,false);
265 Bit32u val=mem_readd(addr);
266 InitPageUpdateLink(needs_reset,addr);
267 return val;
268 }
269 void writeb(PhysPt addr,Bitu val) {
270 Bitu needs_reset=InitPage(addr,true);
271 mem_writeb(addr,val);
272 InitPageUpdateLink(needs_reset,addr);
273 }
274 void writew(PhysPt addr,Bitu val) {
275 Bitu needs_reset=InitPage(addr,true);
276 mem_writew(addr,val);
277 InitPageUpdateLink(needs_reset,addr);
278 }
279 void writed(PhysPt addr,Bitu val) {
280 Bitu needs_reset=InitPage(addr,true);
281 mem_writed(addr,val);
282 InitPageUpdateLink(needs_reset,addr);
283 }
284 bool readb_checked(PhysPt addr, Bit8u * val) {
285 if (InitPageCheckOnly(addr,false)) {
286 *val=mem_readb(addr);
287 return false;
288 } else return true;
289 }
290 bool readw_checked(PhysPt addr, Bit16u * val) {
291 if (InitPageCheckOnly(addr,false)){
292 *val=mem_readw(addr);
293 return false;
294 } else return true;
295 }
296 bool readd_checked(PhysPt addr, Bit32u * val) {
297 if (InitPageCheckOnly(addr,false)) {
298 *val=mem_readd(addr);
299 return false;
300 } else return true;
301 }
302 bool writeb_checked(PhysPt addr,Bitu val) {
303 if (InitPageCheckOnly(addr,true)) {
304 mem_writeb(addr,val);
305 return false;
306 } else return true;
307 }
308 bool writew_checked(PhysPt addr,Bitu val) {
309 if (InitPageCheckOnly(addr,true)) {
310 mem_writew(addr,val);
311 return false;
312 } else return true;
313 }
314 bool writed_checked(PhysPt addr,Bitu val) {
315 if (InitPageCheckOnly(addr,true)) {
316 mem_writed(addr,val);
317 return false;
318 } else return true;
319 }
320 Bitu InitPage(Bitu lin_addr,bool writing) {
321 Bitu lin_page=lin_addr >> 12;
322 Bitu phys_page;
323 if (paging.enabled) {
324 X86PageEntry table;
325 X86PageEntry entry;
326 InitPageCheckPresence(lin_addr,writing,table,entry);
327
328 // 0: no action
329 // 1: can (but currently does not) fail a user-level access privilege check
330 // 2: can (but currently does not) fail a write privilege check
331 // 3: fails a privilege check
332 Bitu priv_check=0;
333 if (InitPage_CheckUseraccess(entry.block.us,table.block.us)) {
334 if ((cpu.cpl&cpu.mpl)==3) priv_check=3;
335 else {
336 switch (CPU_ArchitectureType) {
337 case CPU_ARCHTYPE_MIXED:
338 case CPU_ARCHTYPE_386FAST:
339 default:
340 // priv_check=0; // default
341 break;
342 case CPU_ARCHTYPE_386SLOW:
343 case CPU_ARCHTYPE_486OLDSLOW:
344 case CPU_ARCHTYPE_486NEWSLOW:
345 case CPU_ARCHTYPE_PENTIUMSLOW:
346 priv_check=1;
347 break;
348 }
349 }
350 }
351 if ((entry.block.wr==0) || (table.block.wr==0)) {
352 // page is write-protected for user mode
353 if (priv_check==0) {
354 switch (CPU_ArchitectureType) {
355 case CPU_ARCHTYPE_MIXED:
356 case CPU_ARCHTYPE_386FAST:
357 default:
358 // priv_check=0; // default
359 break;
360 case CPU_ARCHTYPE_386SLOW:
361 case CPU_ARCHTYPE_486OLDSLOW:
362 case CPU_ARCHTYPE_486NEWSLOW:
363 case CPU_ARCHTYPE_PENTIUMSLOW:
364 priv_check=2;
365 break;
366 }
367 }
368 // check if actually failing the write-protected check
369 if (writing && USERWRITE_PROHIBITED) priv_check=3;
370 }
371 if (priv_check==3) {
372 LOG(LOG_PAGING,LOG_NORMAL)("Page access denied: cpl=%i, %x:%x:%x:%x",
373 cpu.cpl,entry.block.us,table.block.us,entry.block.wr,table.block.wr);
374 PAGING_PageFault(lin_addr,(table.block.base<<12)+(lin_page & 0x3ff)*4,0x05 | (writing?0x02:0x00));
375 priv_check=0;
376 }
377
378 if (!table.block.a) {
379 table.block.a=1; // set page table accessed
380 phys_writed((paging.base.page<<12)+(lin_page >> 10)*4,table.load);
381 }
382 if ((!entry.block.a) || (!entry.block.d)) {
383 entry.block.a=1; // set page accessed
384
385 // page is dirty if we're writing to it, or if we're reading but the
386 // page will be fully linked so we can't track later writes
387 if (writing || (priv_check==0)) entry.block.d=1; // mark page as dirty
388
389 phys_writed((table.block.base<<12)+(lin_page & 0x3ff)*4,entry.load);
390 }
391
392 phys_page=entry.block.base;
393
394 // now see how the page should be linked best, if we need to catch privilege
395 // checks later on it should be linked as read-only page
396 if (priv_check==0) {
397 // if reading we could link the page as read-only to later cacth writes,
398 // will slow down pretty much but allows catching all dirty events
399 PAGING_LinkPage(lin_page,phys_page);
400 } else {
401 if (priv_check==1) {
402 PAGING_LinkPage(lin_page,phys_page);
403 return 1;
404 } else if (writing) {
405 PageHandler * handler=MEM_GetPageHandler(phys_page);
406 PAGING_LinkPage(lin_page,phys_page);
407 if (!(handler->flags & PFLAG_READABLE)) return 1;
408 if (!(handler->flags & PFLAG_WRITEABLE)) return 1;
409 if (get_tlb_read(lin_addr)!=get_tlb_write(lin_addr)) return 1;
410 if (phys_page>1) return phys_page;
411 else return 1;
412 } else {
413 PAGING_LinkPage_ReadOnly(lin_page,phys_page);
414 }
415 }
416 } else {
417 if (lin_page<LINK_START) phys_page=paging.firstmb[lin_page];
418 else phys_page=lin_page;
419 PAGING_LinkPage(lin_page,phys_page);
420 }
421 return 0;
422 }
423 bool InitPageCheckOnly(Bitu lin_addr,bool writing) {
424 Bitu lin_page=lin_addr >> 12;
425 if (paging.enabled) {
426 X86PageEntry table;
427 X86PageEntry entry;
428 if (!InitPageCheckPresence_CheckOnly(lin_addr,writing,table,entry)) return false;
429
430 if (!USERWRITE_PROHIBITED) return true;
431
432 if (InitPage_CheckUseraccess(entry.block.us,table.block.us) ||
433 (((entry.block.wr==0) || (table.block.wr==0)) && writing)) {
434 LOG(LOG_PAGING,LOG_NORMAL)("Page access denied: cpl=%i, %x:%x:%x:%x",
435 cpu.cpl,entry.block.us,table.block.us,entry.block.wr,table.block.wr);
436 paging.cr2=lin_addr;
437 cpu.exception.which=EXCEPTION_PF;
438 cpu.exception.error=0x05 | (writing?0x02:0x00);
439 return false;
440 }
441 } else {
442 Bitu phys_page;
443 if (lin_page<LINK_START) phys_page=paging.firstmb[lin_page];
444 else phys_page=lin_page;
445 PAGING_LinkPage(lin_page,phys_page);
446 }
447 return true;
448 }
449 void InitPageForced(Bitu lin_addr) {
450 Bitu lin_page=lin_addr >> 12;
451 Bitu phys_page;
452 if (paging.enabled) {
453 X86PageEntry table;
454 X86PageEntry entry;
455 InitPageCheckPresence(lin_addr,false,table,entry);
456
457 if (!table.block.a) {
458 table.block.a=1; //Set access
459 phys_writed((paging.base.page<<12)+(lin_page >> 10)*4,table.load);
460 }
461 if (!entry.block.a) {
462 entry.block.a=1; //Set access
463 phys_writed((table.block.base<<12)+(lin_page & 0x3ff)*4,entry.load);
464 }
465 phys_page=entry.block.base;
466 // maybe use read-only page here if possible
467 } else {
468 if (lin_page<LINK_START) phys_page=paging.firstmb[lin_page];
469 else phys_page=lin_page;
470 }
471 PAGING_LinkPage(lin_page,phys_page);
472 }
473 };
474
475 class InitPageUserROHandler : public PageHandler {
476 public:
477 InitPageUserROHandler() {
478 flags=PFLAG_INIT|PFLAG_NOCODE;
479 }
480 void writeb(PhysPt addr,Bitu val) {
481 InitPage(addr,(Bit8u)(val&0xff));
482 host_writeb(get_tlb_read(addr)+addr,(Bit8u)(val&0xff));
483 }
484 void writew(PhysPt addr,Bitu val) {
485 InitPage(addr,(Bit16u)(val&0xffff));
486 host_writew(get_tlb_read(addr)+addr,(Bit16u)(val&0xffff));
487 }
488 void writed(PhysPt addr,Bitu val) {
489 InitPage(addr,(Bit32u)val);
490 host_writed(get_tlb_read(addr)+addr,(Bit32u)val);
491 }
492 bool writeb_checked(PhysPt addr,Bitu val) {
493 Bitu writecode=InitPageCheckOnly(addr,(Bit8u)(val&0xff));
494 if (writecode) {
495 HostPt tlb_addr;
496 if (writecode>1) tlb_addr=get_tlb_read(addr);
497 else tlb_addr=get_tlb_write(addr);
498 host_writeb(tlb_addr+addr,(Bit8u)(val&0xff));
499 return false;
500 }
501 return true;
502 }
503 bool writew_checked(PhysPt addr,Bitu val) {
504 Bitu writecode=InitPageCheckOnly(addr,(Bit16u)(val&0xffff));
505 if (writecode) {
506 HostPt tlb_addr;
507 if (writecode>1) tlb_addr=get_tlb_read(addr);
508 else tlb_addr=get_tlb_write(addr);
509 host_writew(tlb_addr+addr,(Bit16u)(val&0xffff));
510 return false;
511 }
512 return true;
513 }
514 bool writed_checked(PhysPt addr,Bitu val) {
515 Bitu writecode=InitPageCheckOnly(addr,(Bit32u)val);
516 if (writecode) {
517 HostPt tlb_addr;
518 if (writecode>1) tlb_addr=get_tlb_read(addr);
519 else tlb_addr=get_tlb_write(addr);
520 host_writed(tlb_addr+addr,(Bit32u)val);
521 return false;
522 }
523 return true;
524 }
525 void InitPage(Bitu lin_addr,Bitu val) {
526 Bitu lin_page=lin_addr >> 12;
527 Bitu phys_page;
528 if (paging.enabled) {
529 if (!USERWRITE_PROHIBITED) return;
530
531 X86PageEntry table;
532 X86PageEntry entry;
533 InitPageCheckPresence(lin_addr,true,table,entry);
534
535 LOG(LOG_PAGING,LOG_NORMAL)("Page access denied: cpl=%i, %x:%x:%x:%x",
536 cpu.cpl,entry.block.us,table.block.us,entry.block.wr,table.block.wr);
537 PAGING_PageFault(lin_addr,(table.block.base<<12)+(lin_page & 0x3ff)*4,0x07);
538
539 if (!table.block.a) {
540 table.block.a=1; //Set access
541 phys_writed((paging.base.page<<12)+(lin_page >> 10)*4,table.load);
542 }
543 if ((!entry.block.a) || (!entry.block.d)) {
544 entry.block.a=1; //Set access
545 entry.block.d=1; //Set dirty
546 phys_writed((table.block.base<<12)+(lin_page & 0x3ff)*4,entry.load);
547 }
548 phys_page=entry.block.base;
549 PAGING_LinkPage(lin_page,phys_page);
550 } else {
551 if (lin_page<LINK_START) phys_page=paging.firstmb[lin_page];
552 else phys_page=lin_page;
553 PAGING_LinkPage(lin_page,phys_page);
554 }
555 }
556 Bitu InitPageCheckOnly(Bitu lin_addr,Bitu val) {
557 Bitu lin_page=lin_addr >> 12;
558 if (paging.enabled) {
559 if (!USERWRITE_PROHIBITED) return 2;
560
561 X86PageEntry table;
562 X86PageEntry entry;
563 if (!InitPageCheckPresence_CheckOnly(lin_addr,true,table,entry)) return 0;
564
565 if (InitPage_CheckUseraccess(entry.block.us,table.block.us) || (((entry.block.wr==0) || (table.block.wr==0)))) {
566 LOG(LOG_PAGING,LOG_NORMAL)("Page access denied: cpl=%i, %x:%x:%x:%x",
567 cpu.cpl,entry.block.us,table.block.us,entry.block.wr,table.block.wr);
568 paging.cr2=lin_addr;
569 cpu.exception.which=EXCEPTION_PF;
570 cpu.exception.error=0x07;
571 return 0;
572 }
573 PAGING_LinkPage(lin_page,entry.block.base);
574 } else {
575 Bitu phys_page;
576 if (lin_page<LINK_START) phys_page=paging.firstmb[lin_page];
577 else phys_page=lin_page;
578 PAGING_LinkPage(lin_page,phys_page);
579 }
580 return 1;
581 }
582 void InitPageForced(Bitu lin_addr) {
583 Bitu lin_page=lin_addr >> 12;
584 Bitu phys_page;
585 if (paging.enabled) {
586 X86PageEntry table;
587 X86PageEntry entry;
588 InitPageCheckPresence(lin_addr,true,table,entry);
589
590 if (!table.block.a) {
591 table.block.a=1; //Set access
592 phys_writed((paging.base.page<<12)+(lin_page >> 10)*4,table.load);
593 }
594 if (!entry.block.a) {
595 entry.block.a=1; //Set access
596 phys_writed((table.block.base<<12)+(lin_page & 0x3ff)*4,entry.load);
597 }
598 phys_page=entry.block.base;
599 } else {
600 if (lin_page<LINK_START) phys_page=paging.firstmb[lin_page];
601 else phys_page=lin_page;
602 }
603 PAGING_LinkPage(lin_page,phys_page);
604 }
605 };
606
607
608 bool PAGING_MakePhysPage(Bitu & page) {
609 if (paging.enabled) {
610 Bitu d_index=page >> 10;
611 Bitu t_index=page & 0x3ff;
612 X86PageEntry table;
613 table.load=phys_readd((paging.base.page<<12)+d_index*4);
614 if (!table.block.p) return false;
615 X86PageEntry entry;
616 entry.load=phys_readd((table.block.base<<12)+t_index*4);
617 if (!entry.block.p) return false;
618 page=entry.block.base;
619 } else {
620 if (page<LINK_START) page=paging.firstmb[page];
621 //Else keep it the same
622 }
623 return true;
624 }
625
626 static InitPageHandler init_page_handler;
627 static InitPageUserROHandler init_page_handler_userro;
628
629
630 Bitu PAGING_GetDirBase(void) {
631 return paging.cr3;
632 }
633
634 bool PAGING_ForcePageInit(Bitu lin_addr) {
635 PageHandler * handler=get_tlb_readhandler(lin_addr);
636 if (handler==&init_page_handler) {
637 init_page_handler.InitPageForced(lin_addr);
638 return true;
639 } else if (handler==&init_page_handler_userro) {
640 PAGING_UnlinkPages(lin_addr>>12,1);
641 init_page_handler_userro.InitPageForced(lin_addr);
642 return true;
643 }
644 return false;
645 }
646
647 #if defined(USE_FULL_TLB)
648 void PAGING_InitTLB(void) {
649 for (Bitu i=0;i<TLB_SIZE;i++) {
650 paging.tlb.read[i]=0;
651 paging.tlb.write[i]=0;
652 paging.tlb.readhandler[i]=&init_page_handler;
653 paging.tlb.writehandler[i]=&init_page_handler;
654 }
655 paging.links.used=0;
656 }
657
658 void PAGING_ClearTLB(void) {
659 Bit32u * entries=&paging.links.entries[0];
660 for (;paging.links.used>0;paging.links.used--) {
661 Bitu page=*entries++;
662 paging.tlb.read[page]=0;
663 paging.tlb.write[page]=0;
664 paging.tlb.readhandler[page]=&init_page_handler;
665 paging.tlb.writehandler[page]=&init_page_handler;
666 }
667 paging.links.used=0;
668 }
669
670 void PAGING_UnlinkPages(Bitu lin_page,Bitu pages) {
671 for (;pages>0;pages--) {
672 paging.tlb.read[lin_page]=0;
673 paging.tlb.write[lin_page]=0;
674 paging.tlb.readhandler[lin_page]=&init_page_handler;
675 paging.tlb.writehandler[lin_page]=&init_page_handler;
676 lin_page++;
677 }
678 }
679
680 void PAGING_MapPage(Bitu lin_page,Bitu phys_page) {
681 if (lin_page<LINK_START) {
682 paging.firstmb[lin_page]=phys_page;
683 paging.tlb.read[lin_page]=0;
684 paging.tlb.write[lin_page]=0;
685 paging.tlb.readhandler[lin_page]=&init_page_handler;
686 paging.tlb.writehandler[lin_page]=&init_page_handler;
687 } else {
688 PAGING_LinkPage(lin_page,phys_page);
689 }
690 }
691
692 void PAGING_LinkPage(Bitu lin_page,Bitu phys_page) {
693 PageHandler * handler=MEM_GetPageHandler(phys_page);
694 Bitu lin_base=lin_page << 12;
695 if (lin_page>=TLB_SIZE || phys_page>=TLB_SIZE)
696 E_Exit("Illegal page");
697
698 if (paging.links.used>=PAGING_LINKS) {
699 LOG(LOG_PAGING,LOG_NORMAL)("Not enough paging links, resetting cache");
700 PAGING_ClearTLB();
701 }
702
703 paging.tlb.phys_page[lin_page]=phys_page;
704 if (handler->flags & PFLAG_READABLE) paging.tlb.read[lin_page]=handler->GetHostReadPt(phys_page)-lin_base;
705 else paging.tlb.read[lin_page]=0;
706 if (handler->flags & PFLAG_WRITEABLE) paging.tlb.write[lin_page]=handler->GetHostWritePt(phys_page)-lin_base;
707 else paging.tlb.write[lin_page]=0;
708
709 paging.links.entries[paging.links.used++]=lin_page;
710 paging.tlb.readhandler[lin_page]=handler;
711 paging.tlb.writehandler[lin_page]=handler;
712 }
713
714 void PAGING_LinkPage_ReadOnly(Bitu lin_page,Bitu phys_page) {
715 PageHandler * handler=MEM_GetPageHandler(phys_page);
716 Bitu lin_base=lin_page << 12;
717 if (lin_page>=TLB_SIZE || phys_page>=TLB_SIZE)
718 E_Exit("Illegal page");
719
720 if (paging.links.used>=PAGING_LINKS) {
721 LOG(LOG_PAGING,LOG_NORMAL)("Not enough paging links, resetting cache");
722 PAGING_ClearTLB();
723 }
724
725 paging.tlb.phys_page[lin_page]=phys_page;
726 if (handler->flags & PFLAG_READABLE) paging.tlb.read[lin_page]=handler->GetHostReadPt(phys_page)-lin_base;
727 else paging.tlb.read[lin_page]=0;
728 paging.tlb.write[lin_page]=0;
729
730 paging.links.entries[paging.links.used++]=lin_page;
731 paging.tlb.readhandler[lin_page]=handler;
732 paging.tlb.writehandler[lin_page]=&init_page_handler_userro;
733 }
734
735 #else
736
737 static INLINE void InitTLBInt(tlb_entry *bank) {
738 for (Bitu i=0;i<TLB_SIZE;i++) {
739 bank[i].read=0;
740 bank[i].write=0;
741 bank[i].readhandler=&init_page_handler;
742 bank[i].writehandler=&init_page_handler;
743 }
744 }
745
746 void PAGING_InitTLBBank(tlb_entry **bank) {
747 *bank = (tlb_entry *)malloc(sizeof(tlb_entry)*TLB_SIZE);
748 if(!*bank) E_Exit("Out of Memory");
749 InitTLBInt(*bank);
750 }
751
752 void PAGING_InitTLB(void) {
753 InitTLBInt(paging.tlbh);
754 paging.links.used=0;
755 }
756
757 void PAGING_ClearTLB(void) {
758 Bit32u * entries=&paging.links.entries[0];
759 for (;paging.links.used>0;paging.links.used--) {
760 Bitu page=*entries++;
761 tlb_entry *entry = get_tlb_entry(page<<12);
762 entry->read=0;
763 entry->write=0;
764 entry->readhandler=&init_page_handler;
765 entry->writehandler=&init_page_handler;
766 }
767 paging.links.used=0;
768 }
769
770 void PAGING_UnlinkPages(Bitu lin_page,Bitu pages) {
771 for (;pages>0;pages--) {
772 tlb_entry *entry = get_tlb_entry(lin_page<<12);
773 entry->read=0;
774 entry->write=0;
775 entry->readhandler=&init_page_handler;
776 entry->writehandler=&init_page_handler;
777 lin_page++;
778 }
779 }
780
781 void PAGING_MapPage(Bitu lin_page,Bitu phys_page) {
782 if (lin_page<LINK_START) {
783 paging.firstmb[lin_page]=phys_page;
784 paging.tlbh[lin_page].read=0;
785 paging.tlbh[lin_page].write=0;
786 paging.tlbh[lin_page].readhandler=&init_page_handler;
787 paging.tlbh[lin_page].writehandler=&init_page_handler;
788 } else {
789 PAGING_LinkPage(lin_page,phys_page);
790 }
791 }
792
793 void PAGING_LinkPage(Bitu lin_page,Bitu phys_page) {
794 PageHandler * handler=MEM_GetPageHandler(phys_page);
795 Bitu lin_base=lin_page << 12;
796 if (lin_page>=(TLB_SIZE*(TLB_BANKS+1)) || phys_page>=(TLB_SIZE*(TLB_BANKS+1)))
797 E_Exit("Illegal page");
798
799 if (paging.links.used>=PAGING_LINKS) {
800 LOG(LOG_PAGING,LOG_NORMAL)("Not enough paging links, resetting cache");
801 PAGING_ClearTLB();
802 }
803
804 tlb_entry *entry = get_tlb_entry(lin_base);
805 entry->phys_page=phys_page;
806 if (handler->flags & PFLAG_READABLE) entry->read=handler->GetHostReadPt(phys_page)-lin_base;
807 else entry->read=0;
808 if (handler->flags & PFLAG_WRITEABLE) entry->write=handler->GetHostWritePt(phys_page)-lin_base;
809 else entry->write=0;
810
811 paging.links.entries[paging.links.used++]=lin_page;
812 entry->readhandler=handler;
813 entry->writehandler=handler;
814 }
815
816 void PAGING_LinkPage_ReadOnly(Bitu lin_page,Bitu phys_page) {
817 PageHandler * handler=MEM_GetPageHandler(phys_page);
818 Bitu lin_base=lin_page << 12;
819 if (lin_page>=(TLB_SIZE*(TLB_BANKS+1)) || phys_page>=(TLB_SIZE*(TLB_BANKS+1)))
820 E_Exit("Illegal page");
821
822 if (paging.links.used>=PAGING_LINKS) {
823 LOG(LOG_PAGING,LOG_NORMAL)("Not enough paging links, resetting cache");
824 PAGING_ClearTLB();
825 }
826
827 tlb_entry *entry = get_tlb_entry(lin_base);
828 entry->phys_page=phys_page;
829 if (handler->flags & PFLAG_READABLE) entry->read=handler->GetHostReadPt(phys_page)-lin_base;
830 else entry->read=0;
831 entry->write=0;
832
833 paging.links.entries[paging.links.used++]=lin_page;
834 entry->readhandler=handler;
835 entry->writehandler=&init_page_handler_userro;
836 }
837
838 #endif
839
840
841 void PAGING_SetDirBase(Bitu cr3) {
842 paging.cr3=cr3;
843
844 paging.base.page=cr3 >> 12;
845 paging.base.addr=cr3 & ~4095;
846 // LOG(LOG_PAGING,LOG_NORMAL)("CR3:%X Base %X",cr3,paging.base.page);
847 if (paging.enabled) {
848 PAGING_ClearTLB();
849 }
850 }
851
852 void PAGING_Enable(bool enabled) {
853 /* If paging is disable we work from a default paging table */
854 if (paging.enabled==enabled) return;
855 paging.enabled=enabled;
856 if (enabled) {
857 if (GCC_UNLIKELY(cpudecoder==CPU_Core_Simple_Run)) {
858 // LOG_MSG("CPU core simple won't run this game,switching to normal");
859 cpudecoder=CPU_Core_Normal_Run;
860 CPU_CycleLeft+=CPU_Cycles;
861 CPU_Cycles=0;
862 }
863 // LOG(LOG_PAGING,LOG_NORMAL)("Enabled");
864 PAGING_SetDirBase(paging.cr3);
865 }
866 PAGING_ClearTLB();
867 }
868
869 bool PAGING_Enabled(void) {
870 return paging.enabled;
871 }
872
873 class PAGING:public Module_base{
874 public:
875 PAGING(Section* configuration):Module_base(configuration){
876 /* Setup default Page Directory, force it to update */
877 paging.enabled=false;
878 PAGING_InitTLB();
879 Bitu i;
880 for (i=0;i<LINK_START;i++) {
881 paging.firstmb[i]=i;
882 }
883 pf_queue.used=0;
884 }
885 ~PAGING(){}
886 };
887
888 static PAGING* test;
889 void PAGING_Init(Section * sec) {
890 test = new PAGING(sec);
891 }