15222Sksewell@umich.edu/*
25254Sksewell@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
35254Sksewell@umich.edu * All rights reserved.
45222Sksewell@umich.edu *
55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are
75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
145254Sksewell@umich.edu * this software without specific prior written permission.
155222Sksewell@umich.edu *
165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275222Sksewell@umich.edu *
285254Sksewell@umich.edu * Authors: Nathan Binkert
295222Sksewell@umich.edu */
305222Sksewell@umich.edu
3111793Sbrandon.potter@amd.com#include "arch/mips/stacktrace.hh"
3211793Sbrandon.potter@amd.com
335222Sksewell@umich.edu#include <string>
345222Sksewell@umich.edu
355222Sksewell@umich.edu#include "arch/mips/isa_traits.hh"
365222Sksewell@umich.edu#include "arch/mips/vtophys.hh"
375222Sksewell@umich.edu#include "base/bitfield.hh"
385222Sksewell@umich.edu#include "base/trace.hh"
395222Sksewell@umich.edu#include "cpu/base.hh"
405222Sksewell@umich.edu#include "cpu/thread_context.hh"
418799Sgblack@eecs.umich.edu#include "mem/fs_translating_port_proxy.hh"
425222Sksewell@umich.edu#include "sim/system.hh"
435222Sksewell@umich.edu
445222Sksewell@umich.eduusing namespace MipsISA;
455222Sksewell@umich.edu
466378Sgblack@eecs.umich.eduProcessInfo::ProcessInfo(ThreadContext *_tc) : tc(_tc)
476378Sgblack@eecs.umich.edu{}
485222Sksewell@umich.edu
495222Sksewell@umich.eduAddr
505222Sksewell@umich.eduProcessInfo::task(Addr ksp) const
515222Sksewell@umich.edu{
525222Sksewell@umich.edu    Addr base = ksp & ~0x3fff;
535222Sksewell@umich.edu    if (base == ULL(0xfffffc0000000000))
545222Sksewell@umich.edu        return 0;
555222Sksewell@umich.edu
565222Sksewell@umich.edu    Addr tsk;
575222Sksewell@umich.edu
5814020Sgabeblack@google.com    PortProxy &vp = tc->getVirtProxy();
5913893Sgabeblack@google.com    tsk = vp.read<Addr>(base + task_off, GuestByteOrder);
605222Sksewell@umich.edu
615222Sksewell@umich.edu    return tsk;
625222Sksewell@umich.edu}
635222Sksewell@umich.edu
645222Sksewell@umich.eduint
655222Sksewell@umich.eduProcessInfo::pid(Addr ksp) const
665222Sksewell@umich.edu{
675222Sksewell@umich.edu    Addr task = this->task(ksp);
685222Sksewell@umich.edu    if (!task)
695222Sksewell@umich.edu        return -1;
705222Sksewell@umich.edu
715222Sksewell@umich.edu    uint16_t pd;
725222Sksewell@umich.edu
7314020Sgabeblack@google.com    PortProxy &vp = tc->getVirtProxy();
7413893Sgabeblack@google.com    pd = vp.read<uint16_t>(task + pid_off, GuestByteOrder);
755222Sksewell@umich.edu
765222Sksewell@umich.edu    return pd;
775222Sksewell@umich.edu}
785222Sksewell@umich.edu
7913893Sgabeblack@google.comstd::string
805222Sksewell@umich.eduProcessInfo::name(Addr ksp) const
815222Sksewell@umich.edu{
825222Sksewell@umich.edu    Addr task = this->task(ksp);
835222Sksewell@umich.edu    if (!task)
845222Sksewell@umich.edu        return "console";
855222Sksewell@umich.edu
865222Sksewell@umich.edu    char comm[256];
8714018Sgabeblack@google.com    tc->getVirtProxy().readString(comm, task + name_off, sizeof(comm));
885222Sksewell@umich.edu    if (!comm[0])
895222Sksewell@umich.edu        return "startup";
905222Sksewell@umich.edu
915222Sksewell@umich.edu    return comm;
925222Sksewell@umich.edu}
935222Sksewell@umich.edu
945222Sksewell@umich.eduStackTrace::StackTrace()
955222Sksewell@umich.edu    : tc(0), stack(64)
965222Sksewell@umich.edu{
975222Sksewell@umich.edu}
985222Sksewell@umich.edu
9910417Sandreas.hansson@arm.comStackTrace::StackTrace(ThreadContext *_tc, const StaticInstPtr &inst)
1005222Sksewell@umich.edu    : tc(0), stack(64)
1015222Sksewell@umich.edu{
1025222Sksewell@umich.edu    trace(_tc, inst);
1035222Sksewell@umich.edu}
1045222Sksewell@umich.edu
1055222Sksewell@umich.eduStackTrace::~StackTrace()
1065222Sksewell@umich.edu{
1075222Sksewell@umich.edu}
1085222Sksewell@umich.edu
1095222Sksewell@umich.eduvoid
1105222Sksewell@umich.eduStackTrace::trace(ThreadContext *_tc, bool is_call)
1115222Sksewell@umich.edu{
1125222Sksewell@umich.edu    tc = _tc;
1135222Sksewell@umich.edu    bool usermode = 0;
1145222Sksewell@umich.edu
1155222Sksewell@umich.edu    if (usermode) {
1165222Sksewell@umich.edu        stack.push_back(user);
1175222Sksewell@umich.edu        return;
1185222Sksewell@umich.edu    }
1195222Sksewell@umich.edu}
1205222Sksewell@umich.edu
1215222Sksewell@umich.edubool
1225222Sksewell@umich.eduStackTrace::isEntry(Addr addr)
1235222Sksewell@umich.edu{
1245222Sksewell@umich.edu    return false;
1255222Sksewell@umich.edu}
1265222Sksewell@umich.edu
1275222Sksewell@umich.edubool
1285222Sksewell@umich.eduStackTrace::decodeStack(MachInst inst, int &disp)
1295222Sksewell@umich.edu{
1305222Sksewell@umich.edu    // lda $sp, -disp($sp)
1315222Sksewell@umich.edu    //
1325222Sksewell@umich.edu    // Opcode<31:26> == 0x08
1335222Sksewell@umich.edu    // RA<25:21> == 30
1345222Sksewell@umich.edu    // RB<20:16> == 30
1355222Sksewell@umich.edu    // Disp<15:0>
1365222Sksewell@umich.edu    const MachInst mem_mask = 0xffff0000;
1375222Sksewell@umich.edu    const MachInst lda_pattern = 0x23de0000;
1385222Sksewell@umich.edu    const MachInst lda_disp_mask = 0x0000ffff;
1395222Sksewell@umich.edu
1405222Sksewell@umich.edu    // subq $sp, disp, $sp
1415222Sksewell@umich.edu    // addq $sp, disp, $sp
1425222Sksewell@umich.edu    //
1435222Sksewell@umich.edu    // Opcode<31:26> == 0x10
1445222Sksewell@umich.edu    // RA<25:21> == 30
1455222Sksewell@umich.edu    // Lit<20:13>
1465222Sksewell@umich.edu    // One<12> = 1
1475222Sksewell@umich.edu    // Func<11:5> == 0x20 (addq)
1485222Sksewell@umich.edu    // Func<11:5> == 0x29 (subq)
1495222Sksewell@umich.edu    // RC<4:0> == 30
1505222Sksewell@umich.edu    const MachInst intop_mask = 0xffe01fff;
1515222Sksewell@umich.edu    const MachInst addq_pattern = 0x43c0141e;
1525222Sksewell@umich.edu    const MachInst subq_pattern = 0x43c0153e;
1535222Sksewell@umich.edu    const MachInst intop_disp_mask = 0x001fe000;
1545222Sksewell@umich.edu    const int intop_disp_shift = 13;
1555222Sksewell@umich.edu
1565222Sksewell@umich.edu    if ((inst & mem_mask) == lda_pattern)
1575222Sksewell@umich.edu        disp = -sext<16>(inst & lda_disp_mask);
1585222Sksewell@umich.edu    else if ((inst & intop_mask) == addq_pattern)
1595222Sksewell@umich.edu        disp = -int((inst & intop_disp_mask) >> intop_disp_shift);
1605222Sksewell@umich.edu    else if ((inst & intop_mask) == subq_pattern)
1615222Sksewell@umich.edu        disp = int((inst & intop_disp_mask) >> intop_disp_shift);
1625222Sksewell@umich.edu    else
1635222Sksewell@umich.edu        return false;
1645222Sksewell@umich.edu
1655222Sksewell@umich.edu    return true;
1665222Sksewell@umich.edu}
1675222Sksewell@umich.edu
1685222Sksewell@umich.edubool
1695222Sksewell@umich.eduStackTrace::decodeSave(MachInst inst, int &reg, int &disp)
1705222Sksewell@umich.edu{
1715222Sksewell@umich.edu    // lda $stq, disp($sp)
1725222Sksewell@umich.edu    //
1735222Sksewell@umich.edu    // Opcode<31:26> == 0x08
1745222Sksewell@umich.edu    // RA<25:21> == ?
1755222Sksewell@umich.edu    // RB<20:16> == 30
1765222Sksewell@umich.edu    // Disp<15:0>
1775222Sksewell@umich.edu    const MachInst stq_mask = 0xfc1f0000;
1785222Sksewell@umich.edu    const MachInst stq_pattern = 0xb41e0000;
1795222Sksewell@umich.edu    const MachInst stq_disp_mask = 0x0000ffff;
1805222Sksewell@umich.edu    const MachInst reg_mask = 0x03e00000;
1815222Sksewell@umich.edu    const int reg_shift = 21;
1825222Sksewell@umich.edu
1835222Sksewell@umich.edu    if ((inst & stq_mask) == stq_pattern) {
1845222Sksewell@umich.edu        reg = (inst & reg_mask) >> reg_shift;
1855222Sksewell@umich.edu        disp = sext<16>(inst & stq_disp_mask);
1865222Sksewell@umich.edu    } else {
1875222Sksewell@umich.edu        return false;
1885222Sksewell@umich.edu    }
1895222Sksewell@umich.edu
1905222Sksewell@umich.edu    return true;
1915222Sksewell@umich.edu}
1925222Sksewell@umich.edu
1935222Sksewell@umich.edu/*
1945222Sksewell@umich.edu * Decode the function prologue for the function we're in, and note
1955222Sksewell@umich.edu * which registers are stored where, and how large the stack frame is.
1965222Sksewell@umich.edu */
1975222Sksewell@umich.edubool
1985222Sksewell@umich.eduStackTrace::decodePrologue(Addr sp, Addr callpc, Addr func,
1995222Sksewell@umich.edu                           int &size, Addr &ra)
2005222Sksewell@umich.edu{
2015222Sksewell@umich.edu    size = 0;
2025222Sksewell@umich.edu    ra = 0;
2035222Sksewell@umich.edu
2045222Sksewell@umich.edu    for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
20514018Sgabeblack@google.com        MachInst inst = tc->getVirtProxy().read<MachInst>(pc);
2065222Sksewell@umich.edu
2075222Sksewell@umich.edu        int reg, disp;
2085222Sksewell@umich.edu        if (decodeStack(inst, disp)) {
2095222Sksewell@umich.edu            if (size) {
2105222Sksewell@umich.edu                return true;
2115222Sksewell@umich.edu            }
2125222Sksewell@umich.edu            size += disp;
2135222Sksewell@umich.edu        } else if (decodeSave(inst, reg, disp)) {
2145222Sksewell@umich.edu            if (!ra && reg == ReturnAddressReg) {
21514018Sgabeblack@google.com                ra = tc->getVirtProxy().read<Addr>(sp + disp);
2165222Sksewell@umich.edu                if (!ra) {
2175222Sksewell@umich.edu                    return false;
2185222Sksewell@umich.edu                }
2195222Sksewell@umich.edu            }
2205222Sksewell@umich.edu        }
2215222Sksewell@umich.edu    }
2225222Sksewell@umich.edu
2235222Sksewell@umich.edu    return true;
2245222Sksewell@umich.edu}
2255222Sksewell@umich.edu
2265222Sksewell@umich.edu#if TRACING_ON
2275222Sksewell@umich.eduvoid
2285222Sksewell@umich.eduStackTrace::dump()
2295222Sksewell@umich.edu{
2306378Sgblack@eecs.umich.edu    panic("Stack trace dump not implemented.\n");
2315222Sksewell@umich.edu}
2325222Sksewell@umich.edu#endif
233