multi_level_page_table.hh revision 12446
110298Salexandru.dutu@amd.com/*
210298Salexandru.dutu@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
310298Salexandru.dutu@amd.com * All rights reserved.
410298Salexandru.dutu@amd.com *
510298Salexandru.dutu@amd.com * Redistribution and use in source and binary forms, with or without
610298Salexandru.dutu@amd.com * modification, are permitted provided that the following conditions are
710298Salexandru.dutu@amd.com * met: redistributions of source code must retain the above copyright
810298Salexandru.dutu@amd.com * notice, this list of conditions and the following disclaimer;
910298Salexandru.dutu@amd.com * redistributions in binary form must reproduce the above copyright
1010298Salexandru.dutu@amd.com * notice, this list of conditions and the following disclaimer in the
1110298Salexandru.dutu@amd.com * documentation and/or other materials provided with the distribution;
1210298Salexandru.dutu@amd.com * neither the name of the copyright holders nor the names of its
1310298Salexandru.dutu@amd.com * contributors may be used to endorse or promote products derived from
1410298Salexandru.dutu@amd.com * this software without specific prior written permission.
1510298Salexandru.dutu@amd.com *
1610298Salexandru.dutu@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710298Salexandru.dutu@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810298Salexandru.dutu@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910298Salexandru.dutu@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010298Salexandru.dutu@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110298Salexandru.dutu@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210298Salexandru.dutu@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310298Salexandru.dutu@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410298Salexandru.dutu@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510298Salexandru.dutu@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610298Salexandru.dutu@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710298Salexandru.dutu@amd.com *
2810298Salexandru.dutu@amd.com * Authors: Alexandru Dutu
2910298Salexandru.dutu@amd.com */
3010298Salexandru.dutu@amd.com
3110298Salexandru.dutu@amd.com/**
3210298Salexandru.dutu@amd.com * @file
3310298Salexandru.dutu@amd.com * Declaration of a multi-level page table.
3410298Salexandru.dutu@amd.com */
3510298Salexandru.dutu@amd.com
3610298Salexandru.dutu@amd.com#ifndef __MEM_MULTI_LEVEL_PAGE_TABLE_HH__
3710298Salexandru.dutu@amd.com#define __MEM_MULTI_LEVEL_PAGE_TABLE_HH__
3810298Salexandru.dutu@amd.com
3910298Salexandru.dutu@amd.com#include <string>
4010298Salexandru.dutu@amd.com
4110298Salexandru.dutu@amd.com#include "base/types.hh"
4210298Salexandru.dutu@amd.com#include "config/the_isa.hh"
4310298Salexandru.dutu@amd.com#include "mem/page_table.hh"
4411800Sbrandon.potter@amd.com
4511800Sbrandon.potter@amd.comclass System;
4610298Salexandru.dutu@amd.com
4710298Salexandru.dutu@amd.com/**
4810298Salexandru.dutu@amd.com * This class implements an in-memory multi-level page table that can be
4910298Salexandru.dutu@amd.com * configured to follow ISA specifications. It can be used instead of the
5010298Salexandru.dutu@amd.com * PageTable class in SE mode to allow CPU models (e.g. X86KvmCPU)
5110298Salexandru.dutu@amd.com * to do a normal page table walk.
5210298Salexandru.dutu@amd.com *
5310298Salexandru.dutu@amd.com * To reduce memory required to store the page table, a multi-level page
5410298Salexandru.dutu@amd.com * table stores its translations similarly with a radix tree. Let n be
5510298Salexandru.dutu@amd.com * the number of levels and {Ln, Ln-1, ..., L1, L0} a set that specifies
5610298Salexandru.dutu@amd.com * the number of entries for each level as base 2 logarithm values. A
5710298Salexandru.dutu@amd.com * multi-level page table will store its translations at level 0 (the
5810298Salexandru.dutu@amd.com * leaves of the tree) and it will be layed out in memory in the
5910298Salexandru.dutu@amd.com * following way:
6010298Salexandru.dutu@amd.com *
6110298Salexandru.dutu@amd.com *                              +------------------------------+
6210298Salexandru.dutu@amd.com * level n                      |Ln-1_E0|Ln-1_E1|...|Ln-1_E2^Ln|
6310298Salexandru.dutu@amd.com *                              +------------------------------+
6410298Salexandru.dutu@amd.com *                                 /       \
6510298Salexandru.dutu@amd.com *            +------------------------+   +------------------------+
6610298Salexandru.dutu@amd.com * level n-1  |Ln-2_E0|...|Ln-2_E2^Ln-1|   |Ln-2_E0|...|Ln-2_E2^Ln-1|
6710298Salexandru.dutu@amd.com *            +------------------------+   +------------------------+
6810298Salexandru.dutu@amd.com *                /            \             /              \
6910298Salexandru.dutu@amd.com *                                  .
7010298Salexandru.dutu@amd.com *                                  .
7110298Salexandru.dutu@amd.com *                                  .
7210298Salexandru.dutu@amd.com *               /                      /               \
7310298Salexandru.dutu@amd.com *          +------------------+   +------------+     +------------+
7410298Salexandru.dutu@amd.com * level 1  |L0_E1|...|L0_E2^L1|   |...|L0_E2^L1| ... |...|L0_E2^L1|
7510298Salexandru.dutu@amd.com *          +------------------+   +------------+     +------------+
7610298Salexandru.dutu@amd.com * , where
7710298Salexandru.dutu@amd.com * +------------------------------+
7810298Salexandru.dutu@amd.com * |Lk-1_E0|Lk-1_E1|...|Lk-1_E2^Lk|
7910298Salexandru.dutu@amd.com * +------------------------------+
8010298Salexandru.dutu@amd.com * is a level k entry that holds 2^Lk entries in Lk-1 level.
8110298Salexandru.dutu@amd.com *
8210298Salexandru.dutu@amd.com * Essentially, a level n entry will contain 2^Ln level n-1 entries,
8310298Salexandru.dutu@amd.com * a level n-1 entry will hold 2^Ln-1 level n-2 entries etc.
8410298Salexandru.dutu@amd.com *
8510298Salexandru.dutu@amd.com * The virtual address is split into offsets that index into the
8610298Salexandru.dutu@amd.com * different levels of the page table.
8710298Salexandru.dutu@amd.com *
8810298Salexandru.dutu@amd.com * +--------------------------------+
8910298Salexandru.dutu@amd.com * |LnOffset|...|L1Offset|PageOffset|
9010298Salexandru.dutu@amd.com * +--------------------------------+
9110298Salexandru.dutu@amd.com *
9210298Salexandru.dutu@amd.com * For example L0Offset will be formed by the bits in range
9310298Salexandru.dutu@amd.com * [log2(PageOffset), log2(PageOffset)+L0].
9410298Salexandru.dutu@amd.com *
9510298Salexandru.dutu@amd.com * For every level of the page table, from n to 1, the base address
9610298Salexandru.dutu@amd.com * of the entry is loaded, the offset in the virtual address for
9710298Salexandru.dutu@amd.com * that particular level is used to index into the entry which
9810298Salexandru.dutu@amd.com * will reveal the memory address of the entry in the next level.
9910298Salexandru.dutu@amd.com *
10010298Salexandru.dutu@amd.com * @see MultiLevelPageTable
10110298Salexandru.dutu@amd.com */
10210298Salexandru.dutu@amd.comtemplate <class ISAOps>
10312446Sgabeblack@google.comclass MultiLevelPageTable : public FuncPageTable
10410298Salexandru.dutu@amd.com{
10510298Salexandru.dutu@amd.com    /**
10610298Salexandru.dutu@amd.com     * ISA specific operations
10710298Salexandru.dutu@amd.com     */
10810298Salexandru.dutu@amd.com    ISAOps pTableISAOps;
10910298Salexandru.dutu@amd.com
11010298Salexandru.dutu@amd.com    /**
11110298Salexandru.dutu@amd.com     * Pointer to System object
11210298Salexandru.dutu@amd.com     */
11310298Salexandru.dutu@amd.com    System *system;
11410298Salexandru.dutu@amd.com
11510298Salexandru.dutu@amd.com    /**
11610298Salexandru.dutu@amd.com     * Physical address to the last level of the page table
11710298Salexandru.dutu@amd.com     */
11810298Salexandru.dutu@amd.com    Addr basePtr;
11910298Salexandru.dutu@amd.com
12010298Salexandru.dutu@amd.com    /**
12110298Salexandru.dutu@amd.com     * Vector with sizes of all levels in base 2 logarithmic
12210298Salexandru.dutu@amd.com     */
12310298Salexandru.dutu@amd.com    const std::vector<uint8_t> logLevelSize;
12410298Salexandru.dutu@amd.com
12510298Salexandru.dutu@amd.com    /**
12610298Salexandru.dutu@amd.com     * Number of levels contained by the page table
12710298Salexandru.dutu@amd.com     */
12810298Salexandru.dutu@amd.com    const uint64_t numLevels;
12910298Salexandru.dutu@amd.com
13010298Salexandru.dutu@amd.com    /**
13110298Salexandru.dutu@amd.com     * Method for walking the page table
13210298Salexandru.dutu@amd.com     *
13310298Salexandru.dutu@amd.com     * @param vaddr Virtual address that is being looked-up
13410298Salexandru.dutu@amd.com     * @param allocate Specifies whether memory should be allocated while
13510298Salexandru.dutu@amd.com     *                  walking the page table
13610298Salexandru.dutu@amd.com     * @return PTE_addr The address of the found PTE
13710298Salexandru.dutu@amd.com     */
13812446Sgabeblack@google.com    void walk(Addr vaddr, bool allocate, Addr &PTE_addr);
13910298Salexandru.dutu@amd.com
14010298Salexandru.dutu@amd.compublic:
14110556Salexandru.dutu@amd.com    MultiLevelPageTable(const std::string &__name, uint64_t _pid,
14212432Sgabeblack@google.com                        System *_sys, Addr pageSize);
14310298Salexandru.dutu@amd.com    ~MultiLevelPageTable();
14410298Salexandru.dutu@amd.com
14511175Sandreas.hansson@arm.com    void initState(ThreadContext* tc) override;
14610298Salexandru.dutu@amd.com
14710558Salexandru.dutu@amd.com    void map(Addr vaddr, Addr paddr, int64_t size,
14811175Sandreas.hansson@arm.com             uint64_t flags = 0) override;
14911175Sandreas.hansson@arm.com    void remap(Addr vaddr, int64_t size, Addr new_vaddr) override;
15011175Sandreas.hansson@arm.com    void unmap(Addr vaddr, int64_t size) override;
15111168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
15211168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
15310298Salexandru.dutu@amd.com};
15410298Salexandru.dutu@amd.com#endif // __MEM_MULTI_LEVEL_PAGE_TABLE_HH__
155