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.
54 #include "arch/x86/pagetable.hh"
55 #include "arch/x86/pagetable_walker.hh"
56 #include "arch/x86/tlb.hh"
57 #include "arch/x86/vtophys.hh"
58 #include "base/bitfield.hh"
59 #include "base/trie.hh"
60 #include "cpu/base.hh"
61 #include "cpu/thread_context.hh"
62 #include "debug/PageTableWalker.hh"
63 #include "mem/packet_access.hh"
64 #include "mem/request.hh"
69 Walker::start(ThreadContext
* _tc
, BaseTLB::Translation
*_translation
,
70 RequestPtr _req
, BaseTLB::Mode _mode
)
72 // TODO: in timing mode, instead of blocking when there are other
73 // outstanding requests, see if this request can be coalesced with
74 // another one (i.e. either coalesce or start walk)
75 WalkerState
* newState
= new WalkerState(this, _translation
, _req
);
76 newState
->initState(_tc
, _mode
, sys
->isTimingMode());
77 if (currStates
.size()) {
78 assert(newState
->isTiming());
79 DPRINTF(PageTableWalker
, "Walks in progress: %d\n", currStates
.size());
80 currStates
.push_back(newState
);
83 currStates
.push_back(newState
);
84 Fault fault
= newState
->startWalk();
85 if (!newState
->isTiming()) {
86 currStates
.pop_front();
94 Walker::startFunctional(ThreadContext
* _tc
, Addr
&addr
, unsigned &logBytes
,
97 funcState
.initState(_tc
, _mode
);
98 return funcState
.startFunctional(addr
, logBytes
);
102 Walker::WalkerPort::recvTimingResp(PacketPtr pkt
)
104 return walker
->recvTimingResp(pkt
);
108 Walker::recvTimingResp(PacketPtr pkt
)
110 WalkerSenderState
* senderState
=
111 dynamic_cast<WalkerSenderState
*>(pkt
->popSenderState());
112 WalkerState
* senderWalk
= senderState
->senderWalk
;
113 bool walkComplete
= senderWalk
->recvPacket(pkt
);
116 std::list
<WalkerState
*>::iterator iter
;
117 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
118 WalkerState
* walkerState
= *(iter
);
119 if (walkerState
== senderWalk
) {
120 iter
= currStates
.erase(iter
);
125 // Since we block requests when another is outstanding, we
126 // need to check if there is a waiting request to be serviced
127 if (currStates
.size())
134 Walker::WalkerPort::recvRetry()
142 std::list
<WalkerState
*>::iterator iter
;
143 for (iter
= currStates
.begin(); iter
!= currStates
.end(); iter
++) {
144 WalkerState
* walkerState
= *(iter
);
145 if (walkerState
->isRetrying()) {
146 walkerState
->retry();
151 bool Walker::sendTiming(WalkerState
* sendingState
, PacketPtr pkt
)
153 WalkerSenderState
* walker_state
= new WalkerSenderState(sendingState
);
154 pkt
->pushSenderState(walker_state
);
155 if (port
.sendTimingReq(pkt
)) {
158 // undo the adding of the sender state and delete it, as we
159 // will do it again the next time we attempt to send it
160 pkt
->popSenderState();
168 Walker::getMasterPort(const std::string
&if_name
, PortID idx
)
170 if (if_name
== "port")
173 return MemObject::getMasterPort(if_name
, idx
);
177 Walker::WalkerState::initState(ThreadContext
* _tc
,
178 BaseTLB::Mode _mode
, bool _isTiming
)
180 assert(state
== Ready
);
188 Walker::startWalkWrapper()
190 unsigned num_squashed
= 0;
191 WalkerState
*currState
= currStates
.front();
192 while ((num_squashed
< numSquashable
) && currState
&&
193 currState
->translation
->squashed()) {
194 currStates
.pop_front();
197 DPRINTF(PageTableWalker
, "Squashing table walk for address %#x\n",
198 currState
->req
->getVaddr());
200 // finish the translation which will delete the translation object
201 currState
->translation
->finish(
202 std::make_shared
<UnimpFault
>("Squashed Inst"),
203 currState
->req
, currState
->tc
, currState
->mode
);
205 // delete the current request
208 // check the next translation request, if it exists
209 if (currStates
.size())
210 currState
= currStates
.front();
214 if (currState
&& !currState
->wasStarted())
215 currState
->startWalk();
219 Walker::WalkerState::startWalk()
221 Fault fault
= NoFault
;
224 setupWalk(req
->getVaddr());
228 timingFault
= NoFault
;
232 walker
->port
.sendAtomic(read
);
233 PacketPtr write
= NULL
;
234 fault
= stepWalk(write
);
235 assert(fault
== NoFault
|| read
== NULL
);
239 walker
->port
.sendAtomic(write
);
248 Walker::WalkerState::startFunctional(Addr
&addr
, unsigned &logBytes
)
250 Fault fault
= NoFault
;
256 walker
->port
.sendFunctional(read
);
257 // On a functional access (page table lookup), writes should
258 // not happen so this pointer is ignored after stepWalk
259 PacketPtr write
= NULL
;
260 fault
= stepWalk(write
);
261 assert(fault
== NoFault
|| read
== NULL
);
265 logBytes
= entry
.logBytes
;
272 Walker::WalkerState::stepWalk(PacketPtr
&write
)
274 assert(state
!= Ready
&& state
!= Waiting
);
275 Fault fault
= NoFault
;
279 pte
= read
->get
<uint64_t>();
281 pte
= read
->get
<uint32_t>();
282 VAddr vaddr
= entry
.vaddr
;
283 bool uncacheable
= pte
.pcd
;
285 bool doWrite
= false;
286 bool doTLBInsert
= false;
287 bool doEndWalk
= false;
288 bool badNX
= pte
.nx
&& mode
== BaseTLB::Execute
&& enableNX
;
291 DPRINTF(PageTableWalker
,
292 "Got long mode PML4 entry %#016x.\n", (uint64_t)pte
);
293 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl3
* dataSize
;
296 entry
.writable
= pte
.w
;
298 if (badNX
|| !pte
.p
) {
300 fault
= pageFault(pte
.p
);
303 entry
.noExec
= pte
.nx
;
307 DPRINTF(PageTableWalker
,
308 "Got long mode PDP entry %#016x.\n", (uint64_t)pte
);
309 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl2
* dataSize
;
312 entry
.writable
= entry
.writable
&& pte
.w
;
313 entry
.user
= entry
.user
&& pte
.u
;
314 if (badNX
|| !pte
.p
) {
316 fault
= pageFault(pte
.p
);
322 DPRINTF(PageTableWalker
,
323 "Got long mode PD entry %#016x.\n", (uint64_t)pte
);
326 entry
.writable
= entry
.writable
&& pte
.w
;
327 entry
.user
= entry
.user
&& pte
.u
;
328 if (badNX
|| !pte
.p
) {
330 fault
= pageFault(pte
.p
);
337 ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.longl1
* dataSize
;
343 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
344 entry
.uncacheable
= uncacheable
;
345 entry
.global
= pte
.g
;
346 entry
.patBit
= bits(pte
, 12);
347 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
353 DPRINTF(PageTableWalker
,
354 "Got long mode PTE entry %#016x.\n", (uint64_t)pte
);
357 entry
.writable
= entry
.writable
&& pte
.w
;
358 entry
.user
= entry
.user
&& pte
.u
;
359 if (badNX
|| !pte
.p
) {
361 fault
= pageFault(pte
.p
);
364 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
365 entry
.uncacheable
= uncacheable
;
366 entry
.global
= pte
.g
;
367 entry
.patBit
= bits(pte
, 12);
368 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
373 DPRINTF(PageTableWalker
,
374 "Got legacy mode PAE PDP entry %#08x.\n", (uint32_t)pte
);
375 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael2
* dataSize
;
378 fault
= pageFault(pte
.p
);
384 DPRINTF(PageTableWalker
,
385 "Got legacy mode PAE PD entry %#08x.\n", (uint32_t)pte
);
388 entry
.writable
= pte
.w
;
390 if (badNX
|| !pte
.p
) {
392 fault
= pageFault(pte
.p
);
398 nextRead
= ((uint64_t)pte
& (mask(40) << 12)) + vaddr
.pael1
* dataSize
;
404 entry
.paddr
= (uint64_t)pte
& (mask(31) << 21);
405 entry
.uncacheable
= uncacheable
;
406 entry
.global
= pte
.g
;
407 entry
.patBit
= bits(pte
, 12);
408 entry
.vaddr
= entry
.vaddr
& ~((2 * (1 << 20)) - 1);
414 DPRINTF(PageTableWalker
,
415 "Got legacy mode PAE PTE entry %#08x.\n", (uint32_t)pte
);
418 entry
.writable
= entry
.writable
&& pte
.w
;
419 entry
.user
= entry
.user
&& pte
.u
;
420 if (badNX
|| !pte
.p
) {
422 fault
= pageFault(pte
.p
);
425 entry
.paddr
= (uint64_t)pte
& (mask(40) << 12);
426 entry
.uncacheable
= uncacheable
;
427 entry
.global
= pte
.g
;
428 entry
.patBit
= bits(pte
, 7);
429 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
434 DPRINTF(PageTableWalker
,
435 "Got legacy mode PSE PD entry %#08x.\n", (uint32_t)pte
);
438 entry
.writable
= pte
.w
;
442 fault
= pageFault(pte
.p
);
449 ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
455 entry
.paddr
= bits(pte
, 20, 13) << 32 | bits(pte
, 31, 22) << 22;
456 entry
.uncacheable
= uncacheable
;
457 entry
.global
= pte
.g
;
458 entry
.patBit
= bits(pte
, 12);
459 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 20)) - 1);
465 DPRINTF(PageTableWalker
,
466 "Got legacy mode PD entry %#08x.\n", (uint32_t)pte
);
469 entry
.writable
= pte
.w
;
473 fault
= pageFault(pte
.p
);
478 nextRead
= ((uint64_t)pte
& (mask(20) << 12)) + vaddr
.norml2
* dataSize
;
482 DPRINTF(PageTableWalker
,
483 "Got legacy mode PTE entry %#08x.\n", (uint32_t)pte
);
486 entry
.writable
= pte
.w
;
490 fault
= pageFault(pte
.p
);
493 entry
.paddr
= (uint64_t)pte
& (mask(20) << 12);
494 entry
.uncacheable
= uncacheable
;
495 entry
.global
= pte
.g
;
496 entry
.patBit
= bits(pte
, 7);
497 entry
.vaddr
= entry
.vaddr
& ~((4 * (1 << 10)) - 1);
502 panic("Unknown page table walker state %d!\n");
507 walker
->tlb
->insert(entry
.vaddr
, entry
);
510 PacketPtr oldRead
= read
;
511 //If we didn't return, we're setting up another read.
512 Request::Flags flags
= oldRead
->req
->getFlags();
513 flags
.set(Request::UNCACHEABLE
, uncacheable
);
515 new Request(nextRead
, oldRead
->getSize(), flags
, walker
->masterId
);
516 read
= new Packet(request
, MemCmd::ReadReq
);
518 // If we need to write, adjust the read packet to write the modified
519 // value back to memory.
522 write
->set
<uint64_t>(pte
);
523 write
->cmd
= MemCmd::WriteReq
;
535 Walker::WalkerState::endWalk()
544 Walker::WalkerState::setupWalk(Addr vaddr
)
547 CR3 cr3
= tc
->readMiscRegNoEffect(MISCREG_CR3
);
548 // Check if we're in long mode or not
549 Efer efer
= tc
->readMiscRegNoEffect(MISCREG_EFER
);
555 topAddr
= (cr3
.longPdtb
<< 12) + addr
.longl4
* dataSize
;
558 // We're in some flavor of legacy mode.
559 CR4 cr4
= tc
->readMiscRegNoEffect(MISCREG_CR4
);
563 topAddr
= (cr3
.paePdtb
<< 5) + addr
.pael3
* dataSize
;
567 topAddr
= (cr3
.pdtb
<< 12) + addr
.norml2
* dataSize
;
572 // Do legacy non PSE.
582 Request::Flags flags
= Request::PHYSICAL
;
584 flags
.set(Request::UNCACHEABLE
);
585 RequestPtr request
= new Request(topAddr
, dataSize
, flags
,
587 read
= new Packet(request
, MemCmd::ReadReq
);
592 Walker::WalkerState::recvPacket(PacketPtr pkt
)
594 assert(pkt
->isResponse());
596 assert(state
== Waiting
);
599 // should not have a pending read it we also had one outstanding
602 // @todo someone should pay for this
603 pkt
->firstWordDelay
= pkt
->lastWordDelay
= 0;
607 PacketPtr write
= NULL
;
609 timingFault
= stepWalk(write
);
611 assert(timingFault
== NoFault
|| read
== NULL
);
613 writes
.push_back(write
);
619 if (inflight
== 0 && read
== NULL
&& writes
.size() == 0) {
622 if (timingFault
== NoFault
) {
624 * Finish the translation. Now that we now the right entry is
625 * in the TLB, this should work with no memory accesses.
626 * There could be new faults unrelated to the table walk like
627 * permissions violations, so we'll need the return value as
630 bool delayedResponse
;
631 Fault fault
= walker
->tlb
->translate(req
, tc
, NULL
, mode
,
632 delayedResponse
, true);
633 assert(!delayedResponse
);
634 // Let the CPU continue.
635 translation
->finish(fault
, req
, tc
, mode
);
637 // There was a fault during the walk. Let the CPU know.
638 translation
->finish(timingFault
, req
, tc
, mode
);
647 Walker::WalkerState::sendPackets()
649 //If we're already waiting for the port to become available, just return.
653 //Reads always have priority
655 PacketPtr pkt
= read
;
658 if (!walker
->sendTiming(this, pkt
)) {
665 //Send off as many of the writes as we can.
666 while (writes
.size()) {
667 PacketPtr write
= writes
.back();
670 if (!walker
->sendTiming(this, write
)) {
672 writes
.push_back(write
);
680 Walker::WalkerState::isRetrying()
686 Walker::WalkerState::isTiming()
692 Walker::WalkerState::wasStarted()
698 Walker::WalkerState::retry()
705 Walker::WalkerState::pageFault(bool present
)
707 DPRINTF(PageTableWalker
, "Raising page fault.\n");
708 HandyM5Reg m5reg
= tc
->readMiscRegNoEffect(MISCREG_M5_REG
);
709 if (mode
== BaseTLB::Execute
&& !enableNX
)
710 mode
= BaseTLB::Read
;
711 return std::make_shared
<PageFault
>(entry
.vaddr
, present
, mode
,
712 m5reg
.cpl
== 3, false);
715 /* end namespace X86ISA */ }
718 X86PagetableWalkerParams::create()
720 return new X86ISA::Walker(this);