2 * Copyright (c) 2007 The Hewlett-Packard Development Company
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "arch/x86/pagetable.hh"
41 #include "arch/x86/pagetable_walker.hh"
42 #include "arch/x86/tlb.hh"
43 #include "arch/x86/vtophys.hh"
44 #include "base/bitfield.hh"
45 #include "cpu/base.hh"
46 #include "cpu/thread_context.hh"
47 #include "debug/PageTableWalker.hh"
48 #include "mem/packet_access.hh"
49 #include "mem/request.hh"
50 #include "sim/system.hh"
54 // Unfortunately, the placement of the base field in a page table entry is
55 // very erratic and would make a mess here. It might be moved here at some
56 // point in the future.
57 BitUnion64(PageTableEntry
)
69 EndBitUnion(PageTableEntry
)
72 Walker::start(ThreadContext
* _tc
, BaseTLB::Translation
*_translation
,
73 RequestPtr _req
, BaseTLB::Mode _mode
)
75 // TODO: in timing mode, instead of blocking when there are other
76 // outstanding requests, see if this request can be coalesced with
77 // another one (i.e. either coalesce or start walk)
78 WalkerState
* newState
= new WalkerState(this, _translation
, _req
);
79 newState
->initState(_tc
, _mode
, sys
->getMemoryMode() == Enums::timing
);
80 if (currStates
.size()) {
81 assert(newState
->isTiming());
82 DPRINTF(PageTableWalker
, "Walks in progress: %d\n", currStates
.size());
83 currStates
.push_back(newState
);
86 currStates
.push_back(newState
);
87 Fault fault
= newState
->startWalk();
88 if (!newState
->isTiming()) {
89 currStates
.pop_front();
97 Walker::startFunctional(ThreadContext
* _tc
, Addr
&addr
, Addr
&pageSize
,
100 funcState
.initState(_tc
, _mode
);
101 return funcState
.startFunctional(addr
, pageSize
);
105 Walker::WalkerPort::recvTiming(PacketPtr pkt
)
107 return walker
->recvTiming(pkt
);
111 Walker::recvTiming(PacketPtr pkt
)
113 if (pkt
->isResponse() || pkt
->wasNacked()) {
114 WalkerSenderState
* senderState
=
115 dynamic_cast<WalkerSenderState
*>(pkt
->senderState
);
116 pkt
->senderState
= senderState
->saved
;
117 WalkerState
* senderWalk
= senderState
->senderWalk
;
118 bool walkComplete
= senderWalk
->recvPacket(pkt
);
121 std::list
<WalkerState
*>::iterator iter
;
122 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
123 WalkerState
* walkerState
= *(iter
);
124 if (walkerState
== senderWalk
) {
125 iter
= currStates
.erase(iter
);
130 // Since we block requests when another is outstanding, we
131 // need to check if there is a waiting request to be serviced
132 if (currStates
.size()) {
133 WalkerState
* newState
= currStates
.front();
134 if (!newState
->wasStarted())
135 newState
->startWalk();
139 DPRINTF(PageTableWalker
, "Received strange packet\n");
145 Walker::WalkerPort::recvAtomic(PacketPtr pkt
)
151 Walker::WalkerPort::recvFunctional(PacketPtr pkt
)
157 Walker::WalkerPort::recvRangeChange()
162 Walker::WalkerPort::recvRetry()
170 std::list
<WalkerState
*>::iterator iter
;
171 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
172 WalkerState
* walkerState
= *(iter
);
173 if (walkerState
->isRetrying()) {
174 walkerState
->retry();
179 bool Walker::sendTiming(WalkerState
* sendingState
, PacketPtr pkt
)
181 pkt
->senderState
= new WalkerSenderState(sendingState
, pkt
->senderState
);
182 return port
.sendTiming(pkt
);
186 Walker::getPort(const std::string
&if_name
, int idx
)
188 if (if_name
== "port")
191 panic("No page table walker port named %s!\n", if_name
);
195 Walker::WalkerState::initState(ThreadContext
* _tc
,
196 BaseTLB::Mode _mode
, bool _isTiming
)
198 assert(state
== Ready
);
206 Walker::WalkerState::startWalk()
208 Fault fault
= NoFault
;
209 assert(started
== false);
211 setupWalk(req
->getVaddr());
215 timingFault
= NoFault
;
219 walker
->port
.sendAtomic(read
);
220 PacketPtr write
= NULL
;
221 fault
= stepWalk(write
);
222 assert(fault
== NoFault
|| read
== NULL
);
226 walker
->port
.sendAtomic(write
);
235 Walker::WalkerState::startFunctional(Addr
&addr
, Addr
&pageSize
)
237 Fault fault
= NoFault
;
238 assert(started
== false);
243 walker
->port
.sendFunctional(read
);
244 // On a functional access (page table lookup), writes should
245 // not happen so this pointer is ignored after stepWalk
246 PacketPtr write
= NULL
;
247 fault
= stepWalk(write
);
248 assert(fault
== NoFault
|| read
== NULL
);
252 pageSize
= entry
.size
;
259 Walker::WalkerState::stepWalk(PacketPtr
&write
)
261 assert(state
!= Ready
&& state
!= Waiting
);
262 Fault fault
= NoFault
;
266 pte
= read
->get
<uint64_t>();
268 pte
= read
->get
<uint32_t>();
269 VAddr vaddr
= entry
.vaddr
;
270 bool uncacheable
= pte
.pcd
;
272 bool doWrite
= false;
273 bool doTLBInsert
= false;
274 bool doEndWalk
= false;
275 bool badNX
= pte
.nx
&& mode
== BaseTLB::Execute
&& enableNX
;
278 DPRINTF(PageTableWalker
,
279 "Got long mode PML4 entry %#016x.\n", (uint64_t)pte
);
280 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl3
* dataSize
;
283 entry
.writable
= pte
.w
;
285 if (badNX
|| !pte
.p
) {
287 fault
= pageFault(pte
.p
);
290 entry
.noExec
= pte
.nx
;
294 DPRINTF(PageTableWalker
,
295 "Got long mode PDP entry %#016x.\n", (uint64_t)pte
);
296 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl2
* dataSize
;
299 entry
.writable
= entry
.writable
&& pte
.w
;
300 entry
.user
= entry
.user
&& pte
.u
;
301 if (badNX
|| !pte
.p
) {
303 fault
= pageFault(pte
.p
);
309 DPRINTF(PageTableWalker
,
310 "Got long mode PD entry %#016x.\n", (uint64_t)pte
);
313 entry
.writable
= entry
.writable
&& pte
.w
;
314 entry
.user
= entry
.user
&& pte
.u
;
315 if (badNX
|| !pte
.p
) {
317 fault
= pageFault(pte
.p
);
322 entry
.size
= 4 * (1 << 10);
324 ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl1
* dataSize
;
329 entry
.size
= 2 * (1 << 20);
330 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
331 entry
.uncacheable
= uncacheable
;
332 entry
.global
= pte
.g
;
333 entry
.patBit
= bits(pte
, 12);
334 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
340 DPRINTF(PageTableWalker
,
341 "Got long mode PTE entry %#016x.\n", (uint64_t)pte
);
344 entry
.writable
= entry
.writable
&& pte
.w
;
345 entry
.user
= entry
.user
&& pte
.u
;
346 if (badNX
|| !pte
.p
) {
348 fault
= pageFault(pte
.p
);
351 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
352 entry
.uncacheable
= uncacheable
;
353 entry
.global
= pte
.g
;
354 entry
.patBit
= bits(pte
, 12);
355 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
360 DPRINTF(PageTableWalker
,
361 "Got legacy mode PAE PDP entry %#08x.\n", (uint32_t)pte
);
362 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael2
* dataSize
;
365 fault
= pageFault(pte
.p
);
371 DPRINTF(PageTableWalker
,
372 "Got legacy mode PAE PD entry %#08x.\n", (uint32_t)pte
);
375 entry
.writable
= pte
.w
;
377 if (badNX
|| !pte
.p
) {
379 fault
= pageFault(pte
.p
);
384 entry
.size
= 4 * (1 << 10);
385 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael1
* dataSize
;
390 entry
.size
= 2 * (1 << 20);
391 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
392 entry
.uncacheable
= uncacheable
;
393 entry
.global
= pte
.g
;
394 entry
.patBit
= bits(pte
, 12);
395 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
401 DPRINTF(PageTableWalker
,
402 "Got legacy mode PAE PTE entry %#08x.\n", (uint32_t)pte
);
405 entry
.writable
= entry
.writable
&& pte
.w
;
406 entry
.user
= entry
.user
&& pte
.u
;
407 if (badNX
|| !pte
.p
) {
409 fault
= pageFault(pte
.p
);
412 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
413 entry
.uncacheable
= uncacheable
;
414 entry
.global
= pte
.g
;
415 entry
.patBit
= bits(pte
, 7);
416 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
421 DPRINTF(PageTableWalker
,
422 "Got legacy mode PSE PD entry %#08x.\n", (uint32_t)pte
);
425 entry
.writable
= pte
.w
;
429 fault
= pageFault(pte
.p
);
434 entry
.size
= 4 * (1 << 10);
436 ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
441 entry
.size
= 4 * (1 << 20);
442 entry
.paddr
= bits(pte
, 20, 13) << 32 | bits(pte
, 31, 22) << 22;
443 entry
.uncacheable
= uncacheable
;
444 entry
.global
= pte
.g
;
445 entry
.patBit
= bits(pte
, 12);
446 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 20)) - 1);
452 DPRINTF(PageTableWalker
,
453 "Got legacy mode PD entry %#08x.\n", (uint32_t)pte
);
456 entry
.writable
= pte
.w
;
460 fault
= pageFault(pte
.p
);
464 entry
.size
= 4 * (1 << 10);
465 nextRead
= ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
469 DPRINTF(PageTableWalker
,
470 "Got legacy mode PTE entry %#08x.\n", (uint32_t)pte
);
473 entry
.writable
= pte
.w
;
477 fault
= pageFault(pte
.p
);
480 entry
.paddr
= (uint64_t)pte
& (mask(20) << 12);
481 entry
.uncacheable
= uncacheable
;
482 entry
.global
= pte
.g
;
483 entry
.patBit
= bits(pte
, 7);
484 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
489 panic("Unknown page table walker state %d!\n");
494 walker
->tlb
->insert(entry
.vaddr
, entry
);
497 PacketPtr oldRead
= read
;
498 //If we didn't return, we're setting up another read.
499 Request::Flags flags
= oldRead
->req
->getFlags();
500 flags
.set(Request::UNCACHEABLE
, uncacheable
);
502 new Request(nextRead
, oldRead
->getSize(), flags
);
503 read
= new Packet(request
, MemCmd::ReadReq
, Packet::Broadcast
);
505 // If we need to write, adjust the read packet to write the modified
506 // value back to memory.
509 write
->set
<uint64_t>(pte
);
510 write
->cmd
= MemCmd::WriteReq
;
511 write
->setDest(Packet::Broadcast
);
522 Walker::WalkerState::endWalk()
531 Walker::WalkerState::setupWalk(Addr vaddr
)
534 CR3 cr3
= tc
->readMiscRegNoEffect(MISCREG_CR3
);
535 // Check if we're in long mode or not
536 Efer efer
= tc
->readMiscRegNoEffect(MISCREG_EFER
);
542 topAddr
= (cr3
.longPdtb
<< 12) + addr
.longl4
* dataSize
;
545 // We're in some flavor of legacy mode.
546 CR4 cr4
= tc
->readMiscRegNoEffect(MISCREG_CR4
);
550 topAddr
= (cr3
.paePdtb
<< 5) + addr
.pael3
* dataSize
;
554 topAddr
= (cr3
.pdtb
<< 12) + addr
.norml2
* dataSize
;
559 // Do legacy non PSE.
569 Request::Flags flags
= Request::PHYSICAL
;
571 flags
.set(Request::UNCACHEABLE
);
572 RequestPtr request
= new Request(topAddr
, dataSize
, flags
);
573 read
= new Packet(request
, MemCmd::ReadReq
, Packet::Broadcast
);
578 Walker::WalkerState::recvPacket(PacketPtr pkt
)
580 if (pkt
->isResponse() && !pkt
->wasNacked()) {
582 assert(state
== Waiting
);
588 PacketPtr write
= NULL
;
590 timingFault
= stepWalk(write
);
592 assert(timingFault
== NoFault
|| read
== NULL
);
594 writes
.push_back(write
);
600 if (inflight
== 0 && read
== NULL
&& writes
.size() == 0) {
603 if (timingFault
== NoFault
) {
605 * Finish the translation. Now that we now the right entry is
606 * in the TLB, this should work with no memory accesses.
607 * There could be new faults unrelated to the table walk like
608 * permissions violations, so we'll need the return value as
611 bool delayedResponse
;
612 Fault fault
= walker
->tlb
->translate(req
, tc
, NULL
, mode
,
613 delayedResponse
, true);
614 assert(!delayedResponse
);
615 // Let the CPU continue.
616 translation
->finish(fault
, req
, tc
, mode
);
618 // There was a fault during the walk. Let the CPU know.
619 translation
->finish(timingFault
, req
, tc
, mode
);
623 } else if (pkt
->wasNacked()) {
624 DPRINTF(PageTableWalker
, "Request was nacked. Entering retry state\n");
626 if (!walker
->sendTiming(this, pkt
)) {
629 if (pkt
->isWrite()) {
630 writes
.push_back(pkt
);
641 Walker::WalkerState::sendPackets()
643 //If we're already waiting for the port to become available, just return.
647 //Reads always have priority
649 PacketPtr pkt
= read
;
652 if (!walker
->sendTiming(this, pkt
)) {
659 //Send off as many of the writes as we can.
660 while (writes
.size()) {
661 PacketPtr write
= writes
.back();
664 if (!walker
->sendTiming(this, write
)) {
666 writes
.push_back(write
);
674 Walker::WalkerState::isRetrying()
680 Walker::WalkerState::isTiming()
686 Walker::WalkerState::wasStarted()
692 Walker::WalkerState::retry()
699 Walker::WalkerState::pageFault(bool present
)
701 DPRINTF(PageTableWalker
, "Raising page fault.\n");
702 HandyM5Reg m5reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
703 if (mode
== BaseTLB::Execute
&& !enableNX
)
704 mode
= BaseTLB::Read
;
705 return new PageFault(entry
.vaddr
, present
, mode
, m5reg
.cpl
== 3, false);
708 /* end namespace X86ISA */ }
711 X86PagetableWalkerParams::create()
713 return new X86ISA::Walker(this);