multi_level_page_table.hh revision 12448
112922Sgabeblack@google.com/* 212922Sgabeblack@google.com * Copyright (c) 2014 Advanced Micro Devices, Inc. 312922Sgabeblack@google.com * All rights reserved. 412922Sgabeblack@google.com * 512922Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 612922Sgabeblack@google.com * modification, are permitted provided that the following conditions are 712922Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 812922Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 912922Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1012922Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1112922Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1212922Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1312922Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1412922Sgabeblack@google.com * this software without specific prior written permission. 1512922Sgabeblack@google.com * 1612922Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712922Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812922Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912922Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012922Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112922Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212922Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312922Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412922Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512922Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612922Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712922Sgabeblack@google.com * 2812922Sgabeblack@google.com * Authors: Alexandru Dutu 2912922Sgabeblack@google.com */ 3012922Sgabeblack@google.com 3112922Sgabeblack@google.com/** 3212922Sgabeblack@google.com * @file 3312922Sgabeblack@google.com * Declaration of a multi-level page table. 3412922Sgabeblack@google.com */ 3512922Sgabeblack@google.com 3612922Sgabeblack@google.com#ifndef __MEM_MULTI_LEVEL_PAGE_TABLE_HH__ 3712922Sgabeblack@google.com#define __MEM_MULTI_LEVEL_PAGE_TABLE_HH__ 3812922Sgabeblack@google.com 3912922Sgabeblack@google.com#include <string> 4012922Sgabeblack@google.com 4112922Sgabeblack@google.com#include "base/types.hh" 4212922Sgabeblack@google.com#include "config/the_isa.hh" 4312922Sgabeblack@google.com#include "mem/page_table.hh" 4412922Sgabeblack@google.com 4512922Sgabeblack@google.comclass System; 4612922Sgabeblack@google.com 4712922Sgabeblack@google.com/** 4812922Sgabeblack@google.com * This class implements an in-memory multi-level page table that can be 4912922Sgabeblack@google.com * configured to follow ISA specifications. It can be used instead of the 5012922Sgabeblack@google.com * PageTable class in SE mode to allow CPU models (e.g. X86KvmCPU) 5112922Sgabeblack@google.com * to do a normal page table walk. 5212922Sgabeblack@google.com * 5312922Sgabeblack@google.com * To reduce memory required to store the page table, a multi-level page 5412922Sgabeblack@google.com * table stores its translations similarly with a radix tree. Let n be 5512922Sgabeblack@google.com * the number of levels and {Ln, Ln-1, ..., L1, L0} a set that specifies 5612922Sgabeblack@google.com * the number of entries for each level as base 2 logarithm values. A 5712922Sgabeblack@google.com * multi-level page table will store its translations at level 0 (the 5812922Sgabeblack@google.com * leaves of the tree) and it will be layed out in memory in the 5912922Sgabeblack@google.com * following way: 6012922Sgabeblack@google.com * 6112922Sgabeblack@google.com * +------------------------------+ 6212922Sgabeblack@google.com * level n |Ln-1_E0|Ln-1_E1|...|Ln-1_E2^Ln| 6312922Sgabeblack@google.com * +------------------------------+ 6412922Sgabeblack@google.com * / \ 6512922Sgabeblack@google.com * +------------------------+ +------------------------+ 6612922Sgabeblack@google.com * level n-1 |Ln-2_E0|...|Ln-2_E2^Ln-1| |Ln-2_E0|...|Ln-2_E2^Ln-1| 6712922Sgabeblack@google.com * +------------------------+ +------------------------+ 6812922Sgabeblack@google.com * / \ / \ 6912922Sgabeblack@google.com * . 7012922Sgabeblack@google.com * . 7112922Sgabeblack@google.com * . 7212922Sgabeblack@google.com * / / \ 7312922Sgabeblack@google.com * +------------------+ +------------+ +------------+ 7412922Sgabeblack@google.com * level 1 |L0_E1|...|L0_E2^L1| |...|L0_E2^L1| ... |...|L0_E2^L1| 7512922Sgabeblack@google.com * +------------------+ +------------+ +------------+ 7612922Sgabeblack@google.com * , where 7712922Sgabeblack@google.com * +------------------------------+ 7812922Sgabeblack@google.com * |Lk-1_E0|Lk-1_E1|...|Lk-1_E2^Lk| 7912922Sgabeblack@google.com * +------------------------------+ 8012922Sgabeblack@google.com * is a level k entry that holds 2^Lk entries in Lk-1 level. 8112922Sgabeblack@google.com * 8212922Sgabeblack@google.com * Essentially, a level n entry will contain 2^Ln level n-1 entries, 8312922Sgabeblack@google.com * a level n-1 entry will hold 2^Ln-1 level n-2 entries etc. 8412922Sgabeblack@google.com * 8512922Sgabeblack@google.com * The virtual address is split into offsets that index into the 8612922Sgabeblack@google.com * different levels of the page table. 8712922Sgabeblack@google.com * 8812922Sgabeblack@google.com * +--------------------------------+ 8912922Sgabeblack@google.com * |LnOffset|...|L1Offset|PageOffset| 9012922Sgabeblack@google.com * +--------------------------------+ 9112922Sgabeblack@google.com * 9212922Sgabeblack@google.com * For example L0Offset will be formed by the bits in range 9312922Sgabeblack@google.com * [log2(PageOffset), log2(PageOffset)+L0]. 9412922Sgabeblack@google.com * 9512922Sgabeblack@google.com * For every level of the page table, from n to 1, the base address 9612922Sgabeblack@google.com * of the entry is loaded, the offset in the virtual address for 9712922Sgabeblack@google.com * that particular level is used to index into the entry which 9812922Sgabeblack@google.com * will reveal the memory address of the entry in the next level. 9912922Sgabeblack@google.com * 10012922Sgabeblack@google.com * @see MultiLevelPageTable 10112922Sgabeblack@google.com */ 10212922Sgabeblack@google.comtemplate <class ISAOps> 10312922Sgabeblack@google.comclass MultiLevelPageTable : public EmulationPageTable 10412922Sgabeblack@google.com{ 10512922Sgabeblack@google.com /** 10612922Sgabeblack@google.com * ISA specific operations 10712922Sgabeblack@google.com */ 10812922Sgabeblack@google.com ISAOps pTableISAOps; 10912922Sgabeblack@google.com 11012922Sgabeblack@google.com /** 11112922Sgabeblack@google.com * Pointer to System object 11212922Sgabeblack@google.com */ 11312922Sgabeblack@google.com System *system; 11412922Sgabeblack@google.com 11512922Sgabeblack@google.com /** 11612922Sgabeblack@google.com * Physical address to the last level of the page table 11712922Sgabeblack@google.com */ 11812922Sgabeblack@google.com Addr basePtr; 11912922Sgabeblack@google.com 12012922Sgabeblack@google.com /** 12112922Sgabeblack@google.com * Vector with sizes of all levels in base 2 logarithmic 12212922Sgabeblack@google.com */ 12312922Sgabeblack@google.com const std::vector<uint8_t> logLevelSize; 12412922Sgabeblack@google.com 12512922Sgabeblack@google.com /** 12612922Sgabeblack@google.com * Number of levels contained by the page table 12712922Sgabeblack@google.com */ 12812922Sgabeblack@google.com const uint64_t numLevels; 12912922Sgabeblack@google.com 13012922Sgabeblack@google.com /** 13112922Sgabeblack@google.com * Method for walking the page table 13212922Sgabeblack@google.com * 13312922Sgabeblack@google.com * @param vaddr Virtual address that is being looked-up 13412922Sgabeblack@google.com * @param allocate Specifies whether memory should be allocated while 13512922Sgabeblack@google.com * walking the page table 13612922Sgabeblack@google.com * @return PTE_addr The address of the found PTE 13712922Sgabeblack@google.com */ 13812922Sgabeblack@google.com void walk(Addr vaddr, bool allocate, Addr &PTE_addr); 13912922Sgabeblack@google.com 14012922Sgabeblack@google.compublic: 14112922Sgabeblack@google.com MultiLevelPageTable(const std::string &__name, uint64_t _pid, 14212922Sgabeblack@google.com System *_sys, Addr pageSize); 14312922Sgabeblack@google.com ~MultiLevelPageTable(); 14412922Sgabeblack@google.com 14512922Sgabeblack@google.com void initState(ThreadContext* tc) override; 14612922Sgabeblack@google.com 14712922Sgabeblack@google.com void map(Addr vaddr, Addr paddr, int64_t size, 14812922Sgabeblack@google.com uint64_t flags = 0) override; 14912922Sgabeblack@google.com void remap(Addr vaddr, int64_t size, Addr new_vaddr) override; 15012922Sgabeblack@google.com void unmap(Addr vaddr, int64_t size) override; 15112922Sgabeblack@google.com void serialize(CheckpointOut &cp) const override; 15212922Sgabeblack@google.com void unserialize(CheckpointIn &cp) override; 15312922Sgabeblack@google.com}; 15412922Sgabeblack@google.com#endif // __MEM_MULTI_LEVEL_PAGE_TABLE_HH__ 15512922Sgabeblack@google.com