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/faults.hh"
57 #include "arch/x86/pagetable.hh"
58 #include "arch/x86/tlb.hh"
59 #include "arch/x86/vtophys.hh"
60 #include "base/bitfield.hh"
61 #include "base/trie.hh"
62 #include "cpu/base.hh"
63 #include "cpu/thread_context.hh"
64 #include "debug/PageTableWalker.hh"
65 #include "mem/packet_access.hh"
66 #include "mem/request.hh"
71 Walker::start(ThreadContext
* _tc
, BaseTLB::Translation
*_translation
,
72 const RequestPtr
&_req
, BaseTLB::Mode _mode
)
74 // TODO: in timing mode, instead of blocking when there are other
75 // outstanding requests, see if this request can be coalesced with
76 // another one (i.e. either coalesce or start walk)
77 WalkerState
* newState
= new WalkerState(this, _translation
, _req
);
78 newState
->initState(_tc
, _mode
, sys
->isTimingMode());
79 if (currStates
.size()) {
80 assert(newState
->isTiming());
81 DPRINTF(PageTableWalker
, "Walks in progress: %d\n", currStates
.size());
82 currStates
.push_back(newState
);
85 currStates
.push_back(newState
);
86 Fault fault
= newState
->startWalk();
87 if (!newState
->isTiming()) {
88 currStates
.pop_front();
96 Walker::startFunctional(ThreadContext
* _tc
, Addr
&addr
, unsigned &logBytes
,
99 funcState
.initState(_tc
, _mode
);
100 return funcState
.startFunctional(addr
, logBytes
);
104 Walker::WalkerPort::recvTimingResp(PacketPtr pkt
)
106 return walker
->recvTimingResp(pkt
);
110 Walker::recvTimingResp(PacketPtr pkt
)
112 WalkerSenderState
* senderState
=
113 dynamic_cast<WalkerSenderState
*>(pkt
->popSenderState());
114 WalkerState
* senderWalk
= senderState
->senderWalk
;
115 bool walkComplete
= senderWalk
->recvPacket(pkt
);
118 std::list
<WalkerState
*>::iterator iter
;
119 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
120 WalkerState
* walkerState
= *(iter
);
121 if (walkerState
== senderWalk
) {
122 iter
= currStates
.erase(iter
);
127 // Since we block requests when another is outstanding, we
128 // need to check if there is a waiting request to be serviced
129 if (currStates
.size() && !startWalkWrapperEvent
.scheduled())
130 // delay sending any new requests until we are finished
131 // with the responses
132 schedule(startWalkWrapperEvent
, clockEdge());
138 Walker::WalkerPort::recvReqRetry()
140 walker
->recvReqRetry();
144 Walker::recvReqRetry()
146 std::list
<WalkerState
*>::iterator iter
;
147 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
148 WalkerState
* walkerState
= *(iter
);
149 if (walkerState
->isRetrying()) {
150 walkerState
->retry();
155 bool Walker::sendTiming(WalkerState
* sendingState
, PacketPtr pkt
)
157 WalkerSenderState
* walker_state
= new WalkerSenderState(sendingState
);
158 pkt
->pushSenderState(walker_state
);
159 if (port
.sendTimingReq(pkt
)) {
162 // undo the adding of the sender state and delete it, as we
163 // will do it again the next time we attempt to send it
164 pkt
->popSenderState();
172 Walker::getPort(const std::string
&if_name
, PortID idx
)
174 if (if_name
== "port")
177 return ClockedObject::getPort(if_name
, idx
);
181 Walker::WalkerState::initState(ThreadContext
* _tc
,
182 BaseTLB::Mode _mode
, bool _isTiming
)
184 assert(state
== Ready
);
192 Walker::startWalkWrapper()
194 unsigned num_squashed
= 0;
195 WalkerState
*currState
= currStates
.front();
196 while ((num_squashed
< numSquashable
) && currState
&&
197 currState
->translation
->squashed()) {
198 currStates
.pop_front();
201 DPRINTF(PageTableWalker
, "Squashing table walk for address %#x\n",
202 currState
->req
->getVaddr());
204 // finish the translation which will delete the translation object
205 currState
->translation
->finish(
206 std::make_shared
<UnimpFault
>("Squashed Inst"),
207 currState
->req
, currState
->tc
, currState
->mode
);
209 // delete the current request if there are no inflight packets.
210 // if there is something in flight, delete when the packets are
211 // received and inflight is zero.
212 if (currState
->numInflight() == 0) {
218 // check the next translation request, if it exists
219 if (currStates
.size())
220 currState
= currStates
.front();
224 if (currState
&& !currState
->wasStarted())
225 currState
->startWalk();
229 Walker::WalkerState::startWalk()
231 Fault fault
= NoFault
;
234 setupWalk(req
->getVaddr());
238 timingFault
= NoFault
;
242 walker
->port
.sendAtomic(read
);
243 PacketPtr write
= NULL
;
244 fault
= stepWalk(write
);
245 assert(fault
== NoFault
|| read
== NULL
);
249 walker
->port
.sendAtomic(write
);
258 Walker::WalkerState::startFunctional(Addr
&addr
, unsigned &logBytes
)
260 Fault fault
= NoFault
;
266 walker
->port
.sendFunctional(read
);
267 // On a functional access (page table lookup), writes should
268 // not happen so this pointer is ignored after stepWalk
269 PacketPtr write
= NULL
;
270 fault
= stepWalk(write
);
271 assert(fault
== NoFault
|| read
== NULL
);
275 logBytes
= entry
.logBytes
;
282 Walker::WalkerState::stepWalk(PacketPtr
&write
)
284 assert(state
!= Ready
&& state
!= Waiting
);
285 Fault fault
= NoFault
;
289 pte
= read
->getLE
<uint64_t>();
291 pte
= read
->getLE
<uint32_t>();
292 VAddr vaddr
= entry
.vaddr
;
293 bool uncacheable
= pte
.pcd
;
295 bool doWrite
= false;
296 bool doTLBInsert
= false;
297 bool doEndWalk
= false;
298 bool badNX
= pte
.nx
&& mode
== BaseTLB::Execute
&& enableNX
;
301 DPRINTF(PageTableWalker
,
302 "Got long mode PML4 entry %#016x.\n", (uint64_t)pte
);
303 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl3
* dataSize
;
306 entry
.writable
= pte
.w
;
308 if (badNX
|| !pte
.p
) {
310 fault
= pageFault(pte
.p
);
313 entry
.noExec
= pte
.nx
;
317 DPRINTF(PageTableWalker
,
318 "Got long mode PDP entry %#016x.\n", (uint64_t)pte
);
319 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl2
* dataSize
;
322 entry
.writable
= entry
.writable
&& pte
.w
;
323 entry
.user
= entry
.user
&& pte
.u
;
324 if (badNX
|| !pte
.p
) {
326 fault
= pageFault(pte
.p
);
332 DPRINTF(PageTableWalker
,
333 "Got long mode PD entry %#016x.\n", (uint64_t)pte
);
336 entry
.writable
= entry
.writable
&& pte
.w
;
337 entry
.user
= entry
.user
&& pte
.u
;
338 if (badNX
|| !pte
.p
) {
340 fault
= pageFault(pte
.p
);
347 ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl1
* dataSize
;
353 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
354 entry
.uncacheable
= uncacheable
;
355 entry
.global
= pte
.g
;
356 entry
.patBit
= bits(pte
, 12);
357 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
363 DPRINTF(PageTableWalker
,
364 "Got long mode PTE entry %#016x.\n", (uint64_t)pte
);
367 entry
.writable
= entry
.writable
&& pte
.w
;
368 entry
.user
= entry
.user
&& pte
.u
;
369 if (badNX
|| !pte
.p
) {
371 fault
= pageFault(pte
.p
);
374 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
375 entry
.uncacheable
= uncacheable
;
376 entry
.global
= pte
.g
;
377 entry
.patBit
= bits(pte
, 12);
378 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
383 DPRINTF(PageTableWalker
,
384 "Got legacy mode PAE PDP entry %#08x.\n", (uint32_t)pte
);
385 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael2
* dataSize
;
388 fault
= pageFault(pte
.p
);
394 DPRINTF(PageTableWalker
,
395 "Got legacy mode PAE PD entry %#08x.\n", (uint32_t)pte
);
398 entry
.writable
= pte
.w
;
400 if (badNX
|| !pte
.p
) {
402 fault
= pageFault(pte
.p
);
408 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael1
* dataSize
;
414 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
415 entry
.uncacheable
= uncacheable
;
416 entry
.global
= pte
.g
;
417 entry
.patBit
= bits(pte
, 12);
418 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
424 DPRINTF(PageTableWalker
,
425 "Got legacy mode PAE PTE entry %#08x.\n", (uint32_t)pte
);
428 entry
.writable
= entry
.writable
&& pte
.w
;
429 entry
.user
= entry
.user
&& pte
.u
;
430 if (badNX
|| !pte
.p
) {
432 fault
= pageFault(pte
.p
);
435 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
436 entry
.uncacheable
= uncacheable
;
437 entry
.global
= pte
.g
;
438 entry
.patBit
= bits(pte
, 7);
439 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
444 DPRINTF(PageTableWalker
,
445 "Got legacy mode PSE PD entry %#08x.\n", (uint32_t)pte
);
448 entry
.writable
= pte
.w
;
452 fault
= pageFault(pte
.p
);
459 ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
465 entry
.paddr
= bits(pte
, 20, 13) << 32 | bits(pte
, 31, 22) << 22;
466 entry
.uncacheable
= uncacheable
;
467 entry
.global
= pte
.g
;
468 entry
.patBit
= bits(pte
, 12);
469 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 20)) - 1);
475 DPRINTF(PageTableWalker
,
476 "Got legacy mode PD entry %#08x.\n", (uint32_t)pte
);
479 entry
.writable
= pte
.w
;
483 fault
= pageFault(pte
.p
);
488 nextRead
= ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
492 DPRINTF(PageTableWalker
,
493 "Got legacy mode PTE entry %#08x.\n", (uint32_t)pte
);
496 entry
.writable
= pte
.w
;
500 fault
= pageFault(pte
.p
);
503 entry
.paddr
= (uint64_t)pte
& (mask(20) << 12);
504 entry
.uncacheable
= uncacheable
;
505 entry
.global
= pte
.g
;
506 entry
.patBit
= bits(pte
, 7);
507 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
512 panic("Unknown page table walker state %d!\n");
517 walker
->tlb
->insert(entry
.vaddr
, entry
);
520 PacketPtr oldRead
= read
;
521 //If we didn't return, we're setting up another read.
522 Request::Flags flags
= oldRead
->req
->getFlags();
523 flags
.set(Request::UNCACHEABLE
, uncacheable
);
524 RequestPtr request
= std::make_shared
<Request
>(
525 nextRead
, oldRead
->getSize(), flags
, walker
->masterId
);
526 read
= new Packet(request
, MemCmd::ReadReq
);
528 // If we need to write, adjust the read packet to write the modified
529 // value back to memory.
532 write
->setLE
<uint64_t>(pte
);
533 write
->cmd
= MemCmd::WriteReq
;
543 Walker::WalkerState::endWalk()
551 Walker::WalkerState::setupWalk(Addr vaddr
)
554 CR3 cr3
= tc
->readMiscRegNoEffect(MISCREG_CR3
);
555 // Check if we're in long mode or not
556 Efer efer
= tc
->readMiscRegNoEffect(MISCREG_EFER
);
562 topAddr
= (cr3
.longPdtb
<< 12) + addr
.longl4
* dataSize
;
565 // We're in some flavor of legacy mode.
566 CR4 cr4
= tc
->readMiscRegNoEffect(MISCREG_CR4
);
570 topAddr
= (cr3
.paePdtb
<< 5) + addr
.pael3
* dataSize
;
574 topAddr
= (cr3
.pdtb
<< 12) + addr
.norml2
* dataSize
;
579 // Do legacy non PSE.
589 Request::Flags flags
= Request::PHYSICAL
;
591 flags
.set(Request::UNCACHEABLE
);
593 RequestPtr request
= std::make_shared
<Request
>(
594 topAddr
, dataSize
, flags
, walker
->masterId
);
596 read
= new Packet(request
, MemCmd::ReadReq
);
601 Walker::WalkerState::recvPacket(PacketPtr pkt
)
603 assert(pkt
->isResponse());
605 assert(state
== Waiting
);
608 // if were were squashed, return true once inflight is zero and
609 // this WalkerState will be freed there.
610 return (inflight
== 0);
613 // should not have a pending read it we also had one outstanding
616 // @todo someone should pay for this
617 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
621 PacketPtr write
= NULL
;
623 timingFault
= stepWalk(write
);
625 assert(timingFault
== NoFault
|| read
== NULL
);
627 writes
.push_back(write
);
633 if (inflight
== 0 && read
== NULL
&& writes
.size() == 0) {
636 if (timingFault
== NoFault
) {
638 * Finish the translation. Now that we know the right entry is
639 * in the TLB, this should work with no memory accesses.
640 * There could be new faults unrelated to the table walk like
641 * permissions violations, so we'll need the return value as
644 bool delayedResponse
;
645 Fault fault
= walker
->tlb
->translate(req
, tc
, NULL
, mode
,
646 delayedResponse
, true);
647 assert(!delayedResponse
);
648 // Let the CPU continue.
649 translation
->finish(fault
, req
, tc
, mode
);
651 // There was a fault during the walk. Let the CPU know.
652 translation
->finish(timingFault
, req
, tc
, mode
);
661 Walker::WalkerState::sendPackets()
663 //If we're already waiting for the port to become available, just return.
667 //Reads always have priority
669 PacketPtr pkt
= read
;
672 if (!walker
->sendTiming(this, pkt
)) {
679 //Send off as many of the writes as we can.
680 while (writes
.size()) {
681 PacketPtr write
= writes
.back();
684 if (!walker
->sendTiming(this, write
)) {
686 writes
.push_back(write
);
694 Walker::WalkerState::numInflight() const
700 Walker::WalkerState::isRetrying()
706 Walker::WalkerState::isTiming()
712 Walker::WalkerState::wasStarted()
718 Walker::WalkerState::squash()
724 Walker::WalkerState::retry()
731 Walker::WalkerState::pageFault(bool present
)
733 DPRINTF(PageTableWalker
, "Raising page fault.\n");
734 HandyM5Reg m5reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
735 if (mode
== BaseTLB::Execute
&& !enableNX
)
736 mode
= BaseTLB::Read
;
737 return std::make_shared
<PageFault
>(entry
.vaddr
, present
, mode
,
738 m5reg
.cpl
== 3, false);
741 /* end namespace X86ISA */ }
744 X86PagetableWalkerParams::create()
746 return new X86ISA::Walker(this);