2 * Copyright (c) 2012 ARM Limited
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 * Copyright (c) 2007 The Hewlett-Packard Development Company
15 * All rights reserved.
17 * The license below extends only to copyright in the software and shall
18 * not be construed as granting a license to any other intellectual
19 * property including but not limited to intellectual property relating
20 * to a hardware implementation of the functionality of the software
21 * licensed hereunder. You may use the software subject to the license
22 * terms below provided that you ensure that this notice is replicated
23 * unmodified and in its entirety in all distributions of the software,
24 * modified or unmodified, in source code or in binary form.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are
28 * met: redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer;
30 * redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution;
33 * neither the name of the copyright holders nor the names of its
34 * contributors may be used to endorse or promote products derived from
35 * this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 #include "arch/x86/pagetable_walker.hh"
56 #include "arch/x86/pagetable.hh"
57 #include "arch/x86/tlb.hh"
58 #include "arch/x86/vtophys.hh"
59 #include "base/bitfield.hh"
60 #include "base/trie.hh"
61 #include "cpu/base.hh"
62 #include "cpu/thread_context.hh"
63 #include "debug/PageTableWalker.hh"
64 #include "mem/packet_access.hh"
65 #include "mem/request.hh"
70 Walker::start(ThreadContext
* _tc
, BaseTLB::Translation
*_translation
,
71 const RequestPtr
&_req
, BaseTLB::Mode _mode
)
73 // TODO: in timing mode, instead of blocking when there are other
74 // outstanding requests, see if this request can be coalesced with
75 // another one (i.e. either coalesce or start walk)
76 WalkerState
* newState
= new WalkerState(this, _translation
, _req
);
77 newState
->initState(_tc
, _mode
, sys
->isTimingMode());
78 if (currStates
.size()) {
79 assert(newState
->isTiming());
80 DPRINTF(PageTableWalker
, "Walks in progress: %d\n", currStates
.size());
81 currStates
.push_back(newState
);
84 currStates
.push_back(newState
);
85 Fault fault
= newState
->startWalk();
86 if (!newState
->isTiming()) {
87 currStates
.pop_front();
95 Walker::startFunctional(ThreadContext
* _tc
, Addr
&addr
, unsigned &logBytes
,
98 funcState
.initState(_tc
, _mode
);
99 return funcState
.startFunctional(addr
, logBytes
);
103 Walker::WalkerPort::recvTimingResp(PacketPtr pkt
)
105 return walker
->recvTimingResp(pkt
);
109 Walker::recvTimingResp(PacketPtr pkt
)
111 WalkerSenderState
* senderState
=
112 dynamic_cast<WalkerSenderState
*>(pkt
->popSenderState());
113 WalkerState
* senderWalk
= senderState
->senderWalk
;
114 bool walkComplete
= senderWalk
->recvPacket(pkt
);
117 std::list
<WalkerState
*>::iterator iter
;
118 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
119 WalkerState
* walkerState
= *(iter
);
120 if (walkerState
== senderWalk
) {
121 iter
= currStates
.erase(iter
);
126 // Since we block requests when another is outstanding, we
127 // need to check if there is a waiting request to be serviced
128 if (currStates
.size() && !startWalkWrapperEvent
.scheduled())
129 // delay sending any new requests until we are finished
130 // with the responses
131 schedule(startWalkWrapperEvent
, clockEdge());
137 Walker::WalkerPort::recvReqRetry()
139 walker
->recvReqRetry();
143 Walker::recvReqRetry()
145 std::list
<WalkerState
*>::iterator iter
;
146 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
147 WalkerState
* walkerState
= *(iter
);
148 if (walkerState
->isRetrying()) {
149 walkerState
->retry();
154 bool Walker::sendTiming(WalkerState
* sendingState
, PacketPtr pkt
)
156 WalkerSenderState
* walker_state
= new WalkerSenderState(sendingState
);
157 pkt
->pushSenderState(walker_state
);
158 if (port
.sendTimingReq(pkt
)) {
161 // undo the adding of the sender state and delete it, as we
162 // will do it again the next time we attempt to send it
163 pkt
->popSenderState();
171 Walker::getPort(const std::string
&if_name
, PortID idx
)
173 if (if_name
== "port")
176 return ClockedObject::getPort(if_name
, idx
);
180 Walker::WalkerState::initState(ThreadContext
* _tc
,
181 BaseTLB::Mode _mode
, bool _isTiming
)
183 assert(state
== Ready
);
191 Walker::startWalkWrapper()
193 unsigned num_squashed
= 0;
194 WalkerState
*currState
= currStates
.front();
195 while ((num_squashed
< numSquashable
) && currState
&&
196 currState
->translation
->squashed()) {
197 currStates
.pop_front();
200 DPRINTF(PageTableWalker
, "Squashing table walk for address %#x\n",
201 currState
->req
->getVaddr());
203 // finish the translation which will delete the translation object
204 currState
->translation
->finish(
205 std::make_shared
<UnimpFault
>("Squashed Inst"),
206 currState
->req
, currState
->tc
, currState
->mode
);
208 // delete the current request if there are no inflight packets.
209 // if there is something in flight, delete when the packets are
210 // received and inflight is zero.
211 if (currState
->numInflight() == 0) {
217 // check the next translation request, if it exists
218 if (currStates
.size())
219 currState
= currStates
.front();
223 if (currState
&& !currState
->wasStarted())
224 currState
->startWalk();
228 Walker::WalkerState::startWalk()
230 Fault fault
= NoFault
;
233 setupWalk(req
->getVaddr());
237 timingFault
= NoFault
;
241 walker
->port
.sendAtomic(read
);
242 PacketPtr write
= NULL
;
243 fault
= stepWalk(write
);
244 assert(fault
== NoFault
|| read
== NULL
);
248 walker
->port
.sendAtomic(write
);
257 Walker::WalkerState::startFunctional(Addr
&addr
, unsigned &logBytes
)
259 Fault fault
= NoFault
;
265 walker
->port
.sendFunctional(read
);
266 // On a functional access (page table lookup), writes should
267 // not happen so this pointer is ignored after stepWalk
268 PacketPtr write
= NULL
;
269 fault
= stepWalk(write
);
270 assert(fault
== NoFault
|| read
== NULL
);
274 logBytes
= entry
.logBytes
;
281 Walker::WalkerState::stepWalk(PacketPtr
&write
)
283 assert(state
!= Ready
&& state
!= Waiting
);
284 Fault fault
= NoFault
;
288 pte
= read
->getLE
<uint64_t>();
290 pte
= read
->getLE
<uint32_t>();
291 VAddr vaddr
= entry
.vaddr
;
292 bool uncacheable
= pte
.pcd
;
294 bool doWrite
= false;
295 bool doTLBInsert
= false;
296 bool doEndWalk
= false;
297 bool badNX
= pte
.nx
&& mode
== BaseTLB::Execute
&& enableNX
;
300 DPRINTF(PageTableWalker
,
301 "Got long mode PML4 entry %#016x.\n", (uint64_t)pte
);
302 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl3
* dataSize
;
305 entry
.writable
= pte
.w
;
307 if (badNX
|| !pte
.p
) {
309 fault
= pageFault(pte
.p
);
312 entry
.noExec
= pte
.nx
;
316 DPRINTF(PageTableWalker
,
317 "Got long mode PDP entry %#016x.\n", (uint64_t)pte
);
318 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl2
* dataSize
;
321 entry
.writable
= entry
.writable
&& pte
.w
;
322 entry
.user
= entry
.user
&& pte
.u
;
323 if (badNX
|| !pte
.p
) {
325 fault
= pageFault(pte
.p
);
331 DPRINTF(PageTableWalker
,
332 "Got long mode PD entry %#016x.\n", (uint64_t)pte
);
335 entry
.writable
= entry
.writable
&& pte
.w
;
336 entry
.user
= entry
.user
&& pte
.u
;
337 if (badNX
|| !pte
.p
) {
339 fault
= pageFault(pte
.p
);
346 ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl1
* dataSize
;
352 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
353 entry
.uncacheable
= uncacheable
;
354 entry
.global
= pte
.g
;
355 entry
.patBit
= bits(pte
, 12);
356 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
362 DPRINTF(PageTableWalker
,
363 "Got long mode PTE entry %#016x.\n", (uint64_t)pte
);
366 entry
.writable
= entry
.writable
&& pte
.w
;
367 entry
.user
= entry
.user
&& pte
.u
;
368 if (badNX
|| !pte
.p
) {
370 fault
= pageFault(pte
.p
);
373 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
374 entry
.uncacheable
= uncacheable
;
375 entry
.global
= pte
.g
;
376 entry
.patBit
= bits(pte
, 12);
377 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
382 DPRINTF(PageTableWalker
,
383 "Got legacy mode PAE PDP entry %#08x.\n", (uint32_t)pte
);
384 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael2
* dataSize
;
387 fault
= pageFault(pte
.p
);
393 DPRINTF(PageTableWalker
,
394 "Got legacy mode PAE PD entry %#08x.\n", (uint32_t)pte
);
397 entry
.writable
= pte
.w
;
399 if (badNX
|| !pte
.p
) {
401 fault
= pageFault(pte
.p
);
407 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael1
* dataSize
;
413 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
414 entry
.uncacheable
= uncacheable
;
415 entry
.global
= pte
.g
;
416 entry
.patBit
= bits(pte
, 12);
417 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
423 DPRINTF(PageTableWalker
,
424 "Got legacy mode PAE PTE entry %#08x.\n", (uint32_t)pte
);
427 entry
.writable
= entry
.writable
&& pte
.w
;
428 entry
.user
= entry
.user
&& pte
.u
;
429 if (badNX
|| !pte
.p
) {
431 fault
= pageFault(pte
.p
);
434 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
435 entry
.uncacheable
= uncacheable
;
436 entry
.global
= pte
.g
;
437 entry
.patBit
= bits(pte
, 7);
438 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
443 DPRINTF(PageTableWalker
,
444 "Got legacy mode PSE PD entry %#08x.\n", (uint32_t)pte
);
447 entry
.writable
= pte
.w
;
451 fault
= pageFault(pte
.p
);
458 ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
464 entry
.paddr
= bits(pte
, 20, 13) << 32 | bits(pte
, 31, 22) << 22;
465 entry
.uncacheable
= uncacheable
;
466 entry
.global
= pte
.g
;
467 entry
.patBit
= bits(pte
, 12);
468 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 20)) - 1);
474 DPRINTF(PageTableWalker
,
475 "Got legacy mode PD entry %#08x.\n", (uint32_t)pte
);
478 entry
.writable
= pte
.w
;
482 fault
= pageFault(pte
.p
);
487 nextRead
= ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
491 DPRINTF(PageTableWalker
,
492 "Got legacy mode PTE entry %#08x.\n", (uint32_t)pte
);
495 entry
.writable
= pte
.w
;
499 fault
= pageFault(pte
.p
);
502 entry
.paddr
= (uint64_t)pte
& (mask(20) << 12);
503 entry
.uncacheable
= uncacheable
;
504 entry
.global
= pte
.g
;
505 entry
.patBit
= bits(pte
, 7);
506 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
511 panic("Unknown page table walker state %d!\n");
516 walker
->tlb
->insert(entry
.vaddr
, entry
);
519 PacketPtr oldRead
= read
;
520 //If we didn't return, we're setting up another read.
521 Request::Flags flags
= oldRead
->req
->getFlags();
522 flags
.set(Request::UNCACHEABLE
, uncacheable
);
523 RequestPtr request
= std::make_shared
<Request
>(
524 nextRead
, oldRead
->getSize(), flags
, walker
->masterId
);
525 read
= new Packet(request
, MemCmd::ReadReq
);
527 // If we need to write, adjust the read packet to write the modified
528 // value back to memory.
531 write
->setLE
<uint64_t>(pte
);
532 write
->cmd
= MemCmd::WriteReq
;
542 Walker::WalkerState::endWalk()
550 Walker::WalkerState::setupWalk(Addr vaddr
)
553 CR3 cr3
= tc
->readMiscRegNoEffect(MISCREG_CR3
);
554 // Check if we're in long mode or not
555 Efer efer
= tc
->readMiscRegNoEffect(MISCREG_EFER
);
561 topAddr
= (cr3
.longPdtb
<< 12) + addr
.longl4
* dataSize
;
564 // We're in some flavor of legacy mode.
565 CR4 cr4
= tc
->readMiscRegNoEffect(MISCREG_CR4
);
569 topAddr
= (cr3
.paePdtb
<< 5) + addr
.pael3
* dataSize
;
573 topAddr
= (cr3
.pdtb
<< 12) + addr
.norml2
* dataSize
;
578 // Do legacy non PSE.
588 Request::Flags flags
= Request::PHYSICAL
;
590 flags
.set(Request::UNCACHEABLE
);
592 RequestPtr request
= std::make_shared
<Request
>(
593 topAddr
, dataSize
, flags
, walker
->masterId
);
595 read
= new Packet(request
, MemCmd::ReadReq
);
600 Walker::WalkerState::recvPacket(PacketPtr pkt
)
602 assert(pkt
->isResponse());
604 assert(state
== Waiting
);
607 // if were were squashed, return true once inflight is zero and
608 // this WalkerState will be freed there.
609 return (inflight
== 0);
612 // should not have a pending read it we also had one outstanding
615 // @todo someone should pay for this
616 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
620 PacketPtr write
= NULL
;
622 timingFault
= stepWalk(write
);
624 assert(timingFault
== NoFault
|| read
== NULL
);
626 writes
.push_back(write
);
632 if (inflight
== 0 && read
== NULL
&& writes
.size() == 0) {
635 if (timingFault
== NoFault
) {
637 * Finish the translation. Now that we know the right entry is
638 * in the TLB, this should work with no memory accesses.
639 * There could be new faults unrelated to the table walk like
640 * permissions violations, so we'll need the return value as
643 bool delayedResponse
;
644 Fault fault
= walker
->tlb
->translate(req
, tc
, NULL
, mode
,
645 delayedResponse
, true);
646 assert(!delayedResponse
);
647 // Let the CPU continue.
648 translation
->finish(fault
, req
, tc
, mode
);
650 // There was a fault during the walk. Let the CPU know.
651 translation
->finish(timingFault
, req
, tc
, mode
);
660 Walker::WalkerState::sendPackets()
662 //If we're already waiting for the port to become available, just return.
666 //Reads always have priority
668 PacketPtr pkt
= read
;
671 if (!walker
->sendTiming(this, pkt
)) {
678 //Send off as many of the writes as we can.
679 while (writes
.size()) {
680 PacketPtr write
= writes
.back();
683 if (!walker
->sendTiming(this, write
)) {
685 writes
.push_back(write
);
693 Walker::WalkerState::numInflight() const
699 Walker::WalkerState::isRetrying()
705 Walker::WalkerState::isTiming()
711 Walker::WalkerState::wasStarted()
717 Walker::WalkerState::squash()
723 Walker::WalkerState::retry()
730 Walker::WalkerState::pageFault(bool present
)
732 DPRINTF(PageTableWalker
, "Raising page fault.\n");
733 HandyM5Reg m5reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
734 if (mode
== BaseTLB::Execute
&& !enableNX
)
735 mode
= BaseTLB::Read
;
736 return std::make_shared
<PageFault
>(entry
.vaddr
, present
, mode
,
737 m5reg
.cpl
== 3, false);
740 /* end namespace X86ISA */ }
743 X86PagetableWalkerParams::create()
745 return new X86ISA::Walker(this);