12379SN/A/*
210298Salexandru.dutu@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
32379SN/A * Copyright (c) 2003 The Regents of The University of Michigan
42379SN/A * All rights reserved.
52379SN/A *
62379SN/A * Redistribution and use in source and binary forms, with or without
72379SN/A * modification, are permitted provided that the following conditions are
82379SN/A * met: redistributions of source code must retain the above copyright
92379SN/A * notice, this list of conditions and the following disclaimer;
102379SN/A * redistributions in binary form must reproduce the above copyright
112379SN/A * notice, this list of conditions and the following disclaimer in the
122379SN/A * documentation and/or other materials provided with the distribution;
132379SN/A * neither the name of the copyright holders nor the names of its
142379SN/A * contributors may be used to endorse or promote products derived from
152379SN/A * this software without specific prior written permission.
162379SN/A *
172379SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182379SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192379SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202379SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212379SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222379SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232379SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242379SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252379SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262379SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272379SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
302379SN/A */
312379SN/A
322379SN/A/**
332379SN/A * @file
3410298Salexandru.dutu@amd.com * Declarations of a non-full system Page Table.
352379SN/A */
362379SN/A
376216Snate@binkert.org#ifndef __MEM_PAGE_TABLE_HH__
386216Snate@binkert.org#define __MEM_PAGE_TABLE_HH__
392379SN/A
402379SN/A#include <string>
4111168Sandreas.hansson@arm.com#include <unordered_map>
422379SN/A
4311800Sbrandon.potter@amd.com#include "base/intmath.hh"
446216Snate@binkert.org#include "base/types.hh"
452394SN/A#include "mem/request.hh"
465004Sgblack@eecs.umich.edu#include "sim/serialize.hh"
4710298Salexandru.dutu@amd.com
4810298Salexandru.dutu@amd.comclass ThreadContext;
492379SN/A
5012448Sgabeblack@google.comclass EmulationPageTable : public Serializable
512379SN/A{
5212461Sgabeblack@google.com  public:
5312461Sgabeblack@google.com    struct Entry
5412461Sgabeblack@google.com    {
5512461Sgabeblack@google.com        Addr paddr;
5612461Sgabeblack@google.com        uint64_t flags;
5712461Sgabeblack@google.com
5812461Sgabeblack@google.com        Entry(Addr paddr, uint64_t flags) : paddr(paddr), flags(flags) {}
5912461Sgabeblack@google.com        Entry() {}
6012461Sgabeblack@google.com    };
6112461Sgabeblack@google.com
622379SN/A  protected:
6312461Sgabeblack@google.com    typedef std::unordered_map<Addr, Entry> PTable;
6412448Sgabeblack@google.com    typedef PTable::iterator PTableItr;
6512448Sgabeblack@google.com    PTable pTable;
662379SN/A
672399SN/A    const Addr pageSize;
682399SN/A    const Addr offsetMask;
692399SN/A
7012461Sgabeblack@google.com    const uint64_t _pid;
718601Ssteve.reinhardt@amd.com    const std::string _name;
722379SN/A
732379SN/A  public:
742379SN/A
7512448Sgabeblack@google.com    EmulationPageTable(
7612448Sgabeblack@google.com            const std::string &__name, uint64_t _pid, Addr _pageSize) :
7712448Sgabeblack@google.com            pageSize(_pageSize), offsetMask(mask(floorLog2(_pageSize))),
7813867Salexandru.dutu@amd.com            _pid(_pid), _name(__name), shared(false)
7910298Salexandru.dutu@amd.com    {
8010298Salexandru.dutu@amd.com        assert(isPowerOf2(pageSize));
8110298Salexandru.dutu@amd.com    }
822379SN/A
8312461Sgabeblack@google.com    uint64_t pid() const { return _pid; };
8412461Sgabeblack@google.com
8512461Sgabeblack@google.com    virtual ~EmulationPageTable() {};
8610298Salexandru.dutu@amd.com
8710558Salexandru.dutu@amd.com    /* generic page table mapping flags
8810558Salexandru.dutu@amd.com     *              unset | set
8910558Salexandru.dutu@amd.com     * bit 0 - no-clobber | clobber
9010558Salexandru.dutu@amd.com     * bit 2 - cacheable  | uncacheable
9110558Salexandru.dutu@amd.com     * bit 3 - read-write | read-only
9210558Salexandru.dutu@amd.com     */
9310558Salexandru.dutu@amd.com    enum MappingFlags : uint32_t {
9410558Salexandru.dutu@amd.com        Clobber     = 1,
9510558Salexandru.dutu@amd.com        Uncacheable = 4,
9610558Salexandru.dutu@amd.com        ReadOnly    = 8,
9710558Salexandru.dutu@amd.com    };
9810558Salexandru.dutu@amd.com
9913867Salexandru.dutu@amd.com    // flag which marks the page table as shared among software threads
10013867Salexandru.dutu@amd.com    bool shared;
10113867Salexandru.dutu@amd.com
10214138Sbrandon.potter@amd.com    virtual void initState() {};
1032379SN/A
1048601Ssteve.reinhardt@amd.com    // for DPRINTF compatibility
1058601Ssteve.reinhardt@amd.com    const std::string name() const { return _name; }
1068601Ssteve.reinhardt@amd.com
1072399SN/A    Addr pageAlign(Addr a)  { return (a & ~offsetMask); }
1082399SN/A    Addr pageOffset(Addr a) { return (a &  offsetMask); }
1092379SN/A
11010558Salexandru.dutu@amd.com    /**
11110558Salexandru.dutu@amd.com     * Maps a virtual memory region to a physical memory region.
11210558Salexandru.dutu@amd.com     * @param vaddr The starting virtual address of the region.
11310558Salexandru.dutu@amd.com     * @param paddr The starting physical address where the region is mapped.
11410558Salexandru.dutu@amd.com     * @param size The length of the region.
11510558Salexandru.dutu@amd.com     * @param flags Generic mapping flags that can be set by or-ing values
11610558Salexandru.dutu@amd.com     *              from MappingFlags enum.
11710558Salexandru.dutu@amd.com     */
11812448Sgabeblack@google.com    virtual void map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags = 0);
11912448Sgabeblack@google.com    virtual void remap(Addr vaddr, int64_t size, Addr new_vaddr);
12012448Sgabeblack@google.com    virtual void unmap(Addr vaddr, int64_t size);
1212379SN/A
1225004Sgblack@eecs.umich.edu    /**
1238600Ssteve.reinhardt@amd.com     * Check if any pages in a region are already allocated
1248600Ssteve.reinhardt@amd.com     * @param vaddr The starting virtual address of the region.
1258600Ssteve.reinhardt@amd.com     * @param size The length of the region.
1268600Ssteve.reinhardt@amd.com     * @return True if no pages in the region are mapped.
1278600Ssteve.reinhardt@amd.com     */
12812448Sgabeblack@google.com    virtual bool isUnmapped(Addr vaddr, int64_t size);
1298600Ssteve.reinhardt@amd.com
1308600Ssteve.reinhardt@amd.com    /**
1315004Sgblack@eecs.umich.edu     * Lookup function
1325004Sgblack@eecs.umich.edu     * @param vaddr The virtual address.
13312455Sgabeblack@google.com     * @return The page table entry corresponding to vaddr.
1345004Sgblack@eecs.umich.edu     */
13512461Sgabeblack@google.com    const Entry *lookup(Addr vaddr);
1362399SN/A
1372379SN/A    /**
1382379SN/A     * Translate function
1392379SN/A     * @param vaddr The virtual address.
1405748SSteve.Reinhardt@amd.com     * @param paddr Physical address from translation.
1415748SSteve.Reinhardt@amd.com     * @return True if translation exists
1422379SN/A     */
1432399SN/A    bool translate(Addr vaddr, Addr &paddr);
1442379SN/A
1452379SN/A    /**
1465748SSteve.Reinhardt@amd.com     * Simplified translate function (just check for translation)
1475748SSteve.Reinhardt@amd.com     * @param vaddr The virtual address.
1485748SSteve.Reinhardt@amd.com     * @return True if translation exists
1495748SSteve.Reinhardt@amd.com     */
1505748SSteve.Reinhardt@amd.com    bool translate(Addr vaddr) { Addr dummy; return translate(vaddr, dummy); }
1515748SSteve.Reinhardt@amd.com
1525748SSteve.Reinhardt@amd.com    /**
1532399SN/A     * Perform a translation on the memory request, fills in paddr
1545004Sgblack@eecs.umich.edu     * field of req.
1552379SN/A     * @param req The memory request.
1562379SN/A     */
15712749Sgiacomo.travaglini@arm.com    Fault translate(const RequestPtr &req);
1582379SN/A
15912448Sgabeblack@google.com    void getMappings(std::vector<std::pair<Addr, Addr>> *addr_mappings);
16010298Salexandru.dutu@amd.com
16111168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
16211168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
1632379SN/A};
1642379SN/A
1656216Snate@binkert.org#endif // __MEM_PAGE_TABLE_HH__
166