943295a5fa29a30909d0d16079c204af5d80f795
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.
50 #include "arch/x86/pagetable_walker.hh"
54 #include "arch/x86/faults.hh"
55 #include "arch/x86/pagetable.hh"
56 #include "arch/x86/tlb.hh"
57 #include "base/bitfield.hh"
58 #include "base/trie.hh"
59 #include "cpu/base.hh"
60 #include "cpu/thread_context.hh"
61 #include "debug/PageTableWalker.hh"
62 #include "mem/packet_access.hh"
63 #include "mem/request.hh"
68 Walker::start(ThreadContext
* _tc
, BaseTLB::Translation
*_translation
,
69 const RequestPtr
&_req
, BaseTLB::Mode _mode
)
71 // TODO: in timing mode, instead of blocking when there are other
72 // outstanding requests, see if this request can be coalesced with
73 // another one (i.e. either coalesce or start walk)
74 WalkerState
* newState
= new WalkerState(this, _translation
, _req
);
75 newState
->initState(_tc
, _mode
, sys
->isTimingMode());
76 if (currStates
.size()) {
77 assert(newState
->isTiming());
78 DPRINTF(PageTableWalker
, "Walks in progress: %d\n", currStates
.size());
79 currStates
.push_back(newState
);
82 currStates
.push_back(newState
);
83 Fault fault
= newState
->startWalk();
84 if (!newState
->isTiming()) {
85 currStates
.pop_front();
93 Walker::startFunctional(ThreadContext
* _tc
, Addr
&addr
, unsigned &logBytes
,
96 funcState
.initState(_tc
, _mode
);
97 return funcState
.startFunctional(addr
, logBytes
);
101 Walker::WalkerPort::recvTimingResp(PacketPtr pkt
)
103 return walker
->recvTimingResp(pkt
);
107 Walker::recvTimingResp(PacketPtr pkt
)
109 WalkerSenderState
* senderState
=
110 dynamic_cast<WalkerSenderState
*>(pkt
->popSenderState());
111 WalkerState
* senderWalk
= senderState
->senderWalk
;
112 bool walkComplete
= senderWalk
->recvPacket(pkt
);
115 std::list
<WalkerState
*>::iterator iter
;
116 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
117 WalkerState
* walkerState
= *(iter
);
118 if (walkerState
== senderWalk
) {
119 iter
= currStates
.erase(iter
);
124 // Since we block requests when another is outstanding, we
125 // need to check if there is a waiting request to be serviced
126 if (currStates
.size() && !startWalkWrapperEvent
.scheduled())
127 // delay sending any new requests until we are finished
128 // with the responses
129 schedule(startWalkWrapperEvent
, clockEdge());
135 Walker::WalkerPort::recvReqRetry()
137 walker
->recvReqRetry();
141 Walker::recvReqRetry()
143 std::list
<WalkerState
*>::iterator iter
;
144 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
145 WalkerState
* walkerState
= *(iter
);
146 if (walkerState
->isRetrying()) {
147 walkerState
->retry();
152 bool Walker::sendTiming(WalkerState
* sendingState
, PacketPtr pkt
)
154 WalkerSenderState
* walker_state
= new WalkerSenderState(sendingState
);
155 pkt
->pushSenderState(walker_state
);
156 if (port
.sendTimingReq(pkt
)) {
159 // undo the adding of the sender state and delete it, as we
160 // will do it again the next time we attempt to send it
161 pkt
->popSenderState();
169 Walker::getPort(const std::string
&if_name
, PortID idx
)
171 if (if_name
== "port")
174 return ClockedObject::getPort(if_name
, idx
);
178 Walker::WalkerState::initState(ThreadContext
* _tc
,
179 BaseTLB::Mode _mode
, bool _isTiming
)
181 assert(state
== Ready
);
189 Walker::startWalkWrapper()
191 unsigned num_squashed
= 0;
192 WalkerState
*currState
= currStates
.front();
193 while ((num_squashed
< numSquashable
) && currState
&&
194 currState
->translation
->squashed()) {
195 currStates
.pop_front();
198 DPRINTF(PageTableWalker
, "Squashing table walk for address %#x\n",
199 currState
->req
->getVaddr());
201 // finish the translation which will delete the translation object
202 currState
->translation
->finish(
203 std::make_shared
<UnimpFault
>("Squashed Inst"),
204 currState
->req
, currState
->tc
, currState
->mode
);
206 // delete the current request if there are no inflight packets.
207 // if there is something in flight, delete when the packets are
208 // received and inflight is zero.
209 if (currState
->numInflight() == 0) {
215 // check the next translation request, if it exists
216 if (currStates
.size())
217 currState
= currStates
.front();
221 if (currState
&& !currState
->wasStarted())
222 currState
->startWalk();
226 Walker::WalkerState::startWalk()
228 Fault fault
= NoFault
;
231 setupWalk(req
->getVaddr());
235 timingFault
= NoFault
;
239 walker
->port
.sendAtomic(read
);
240 PacketPtr write
= NULL
;
241 fault
= stepWalk(write
);
242 assert(fault
== NoFault
|| read
== NULL
);
246 walker
->port
.sendAtomic(write
);
255 Walker::WalkerState::startFunctional(Addr
&addr
, unsigned &logBytes
)
257 Fault fault
= NoFault
;
263 walker
->port
.sendFunctional(read
);
264 // On a functional access (page table lookup), writes should
265 // not happen so this pointer is ignored after stepWalk
266 PacketPtr write
= NULL
;
267 fault
= stepWalk(write
);
268 assert(fault
== NoFault
|| read
== NULL
);
272 logBytes
= entry
.logBytes
;
279 Walker::WalkerState::stepWalk(PacketPtr
&write
)
281 assert(state
!= Ready
&& state
!= Waiting
);
282 Fault fault
= NoFault
;
286 pte
= read
->getLE
<uint64_t>();
288 pte
= read
->getLE
<uint32_t>();
289 VAddr vaddr
= entry
.vaddr
;
290 bool uncacheable
= pte
.pcd
;
292 bool doWrite
= false;
293 bool doTLBInsert
= false;
294 bool doEndWalk
= false;
295 bool badNX
= pte
.nx
&& mode
== BaseTLB::Execute
&& enableNX
;
298 DPRINTF(PageTableWalker
,
299 "Got long mode PML4 entry %#016x.\n", (uint64_t)pte
);
300 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl3
* dataSize
;
303 entry
.writable
= pte
.w
;
305 if (badNX
|| !pte
.p
) {
307 fault
= pageFault(pte
.p
);
310 entry
.noExec
= pte
.nx
;
314 DPRINTF(PageTableWalker
,
315 "Got long mode PDP entry %#016x.\n", (uint64_t)pte
);
316 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl2
* dataSize
;
319 entry
.writable
= entry
.writable
&& pte
.w
;
320 entry
.user
= entry
.user
&& pte
.u
;
321 if (badNX
|| !pte
.p
) {
323 fault
= pageFault(pte
.p
);
329 DPRINTF(PageTableWalker
,
330 "Got long mode PD entry %#016x.\n", (uint64_t)pte
);
333 entry
.writable
= entry
.writable
&& pte
.w
;
334 entry
.user
= entry
.user
&& pte
.u
;
335 if (badNX
|| !pte
.p
) {
337 fault
= pageFault(pte
.p
);
344 ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl1
* dataSize
;
350 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
351 entry
.uncacheable
= uncacheable
;
352 entry
.global
= pte
.g
;
353 entry
.patBit
= bits(pte
, 12);
354 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
360 DPRINTF(PageTableWalker
,
361 "Got long mode PTE entry %#016x.\n", (uint64_t)pte
);
364 entry
.writable
= entry
.writable
&& pte
.w
;
365 entry
.user
= entry
.user
&& pte
.u
;
366 if (badNX
|| !pte
.p
) {
368 fault
= pageFault(pte
.p
);
371 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
372 entry
.uncacheable
= uncacheable
;
373 entry
.global
= pte
.g
;
374 entry
.patBit
= bits(pte
, 12);
375 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
380 DPRINTF(PageTableWalker
,
381 "Got legacy mode PAE PDP entry %#08x.\n", (uint32_t)pte
);
382 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael2
* dataSize
;
385 fault
= pageFault(pte
.p
);
391 DPRINTF(PageTableWalker
,
392 "Got legacy mode PAE PD entry %#08x.\n", (uint32_t)pte
);
395 entry
.writable
= pte
.w
;
397 if (badNX
|| !pte
.p
) {
399 fault
= pageFault(pte
.p
);
405 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael1
* dataSize
;
411 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
412 entry
.uncacheable
= uncacheable
;
413 entry
.global
= pte
.g
;
414 entry
.patBit
= bits(pte
, 12);
415 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
421 DPRINTF(PageTableWalker
,
422 "Got legacy mode PAE PTE entry %#08x.\n", (uint32_t)pte
);
425 entry
.writable
= entry
.writable
&& pte
.w
;
426 entry
.user
= entry
.user
&& pte
.u
;
427 if (badNX
|| !pte
.p
) {
429 fault
= pageFault(pte
.p
);
432 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
433 entry
.uncacheable
= uncacheable
;
434 entry
.global
= pte
.g
;
435 entry
.patBit
= bits(pte
, 7);
436 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
441 DPRINTF(PageTableWalker
,
442 "Got legacy mode PSE PD entry %#08x.\n", (uint32_t)pte
);
445 entry
.writable
= pte
.w
;
449 fault
= pageFault(pte
.p
);
456 ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
462 entry
.paddr
= bits(pte
, 20, 13) << 32 | bits(pte
, 31, 22) << 22;
463 entry
.uncacheable
= uncacheable
;
464 entry
.global
= pte
.g
;
465 entry
.patBit
= bits(pte
, 12);
466 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 20)) - 1);
472 DPRINTF(PageTableWalker
,
473 "Got legacy mode PD entry %#08x.\n", (uint32_t)pte
);
476 entry
.writable
= pte
.w
;
480 fault
= pageFault(pte
.p
);
485 nextRead
= ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
489 DPRINTF(PageTableWalker
,
490 "Got legacy mode PTE entry %#08x.\n", (uint32_t)pte
);
493 entry
.writable
= pte
.w
;
497 fault
= pageFault(pte
.p
);
500 entry
.paddr
= (uint64_t)pte
& (mask(20) << 12);
501 entry
.uncacheable
= uncacheable
;
502 entry
.global
= pte
.g
;
503 entry
.patBit
= bits(pte
, 7);
504 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
509 panic("Unknown page table walker state %d!\n");
514 walker
->tlb
->insert(entry
.vaddr
, entry
);
517 PacketPtr oldRead
= read
;
518 //If we didn't return, we're setting up another read.
519 Request::Flags flags
= oldRead
->req
->getFlags();
520 flags
.set(Request::UNCACHEABLE
, uncacheable
);
521 RequestPtr request
= std::make_shared
<Request
>(
522 nextRead
, oldRead
->getSize(), flags
, walker
->requestorId
);
523 read
= new Packet(request
, MemCmd::ReadReq
);
525 // If we need to write, adjust the read packet to write the modified
526 // value back to memory.
529 write
->setLE
<uint64_t>(pte
);
530 write
->cmd
= MemCmd::WriteReq
;
540 Walker::WalkerState::endWalk()
548 Walker::WalkerState::setupWalk(Addr vaddr
)
551 CR3 cr3
= tc
->readMiscRegNoEffect(MISCREG_CR3
);
552 // Check if we're in long mode or not
553 Efer efer
= tc
->readMiscRegNoEffect(MISCREG_EFER
);
559 topAddr
= (cr3
.longPdtb
<< 12) + addr
.longl4
* dataSize
;
562 // We're in some flavor of legacy mode.
563 CR4 cr4
= tc
->readMiscRegNoEffect(MISCREG_CR4
);
567 topAddr
= (cr3
.paePdtb
<< 5) + addr
.pael3
* dataSize
;
571 topAddr
= (cr3
.pdtb
<< 12) + addr
.norml2
* dataSize
;
576 // Do legacy non PSE.
586 Request::Flags flags
= Request::PHYSICAL
;
588 flags
.set(Request::UNCACHEABLE
);
590 RequestPtr request
= std::make_shared
<Request
>(
591 topAddr
, dataSize
, flags
, walker
->requestorId
);
593 read
= new Packet(request
, MemCmd::ReadReq
);
598 Walker::WalkerState::recvPacket(PacketPtr pkt
)
600 assert(pkt
->isResponse());
602 assert(state
== Waiting
);
605 // if were were squashed, return true once inflight is zero and
606 // this WalkerState will be freed there.
607 return (inflight
== 0);
610 // should not have a pending read it we also had one outstanding
613 // @todo someone should pay for this
614 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
618 PacketPtr write
= NULL
;
620 timingFault
= stepWalk(write
);
622 assert(timingFault
== NoFault
|| read
== NULL
);
624 writes
.push_back(write
);
630 if (inflight
== 0 && read
== NULL
&& writes
.size() == 0) {
633 if (timingFault
== NoFault
) {
635 * Finish the translation. Now that we know the right entry is
636 * in the TLB, this should work with no memory accesses.
637 * There could be new faults unrelated to the table walk like
638 * permissions violations, so we'll need the return value as
641 bool delayedResponse
;
642 Fault fault
= walker
->tlb
->translate(req
, tc
, NULL
, mode
,
643 delayedResponse
, true);
644 assert(!delayedResponse
);
645 // Let the CPU continue.
646 translation
->finish(fault
, req
, tc
, mode
);
648 // There was a fault during the walk. Let the CPU know.
649 translation
->finish(timingFault
, req
, tc
, mode
);
658 Walker::WalkerState::sendPackets()
660 //If we're already waiting for the port to become available, just return.
664 //Reads always have priority
666 PacketPtr pkt
= read
;
669 if (!walker
->sendTiming(this, pkt
)) {
676 //Send off as many of the writes as we can.
677 while (writes
.size()) {
678 PacketPtr write
= writes
.back();
681 if (!walker
->sendTiming(this, write
)) {
683 writes
.push_back(write
);
691 Walker::WalkerState::numInflight() const
697 Walker::WalkerState::isRetrying()
703 Walker::WalkerState::isTiming()
709 Walker::WalkerState::wasStarted()
715 Walker::WalkerState::squash()
721 Walker::WalkerState::retry()
728 Walker::WalkerState::pageFault(bool present
)
730 DPRINTF(PageTableWalker
, "Raising page fault.\n");
731 HandyM5Reg m5reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
732 if (mode
== BaseTLB::Execute
&& !enableNX
)
733 mode
= BaseTLB::Read
;
734 return std::make_shared
<PageFault
>(entry
.vaddr
, present
, mode
,
735 m5reg
.cpl
== 3, false);
738 /* end namespace X86ISA */ }
741 X86PagetableWalkerParams::create() const
743 return new X86ISA::Walker(*this);