faults.cc revision 13583
12221SN/A/*
22221SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32221SN/A * All rights reserved.
42221SN/A *
52221SN/A * Redistribution and use in source and binary forms, with or without
62221SN/A * modification, are permitted provided that the following conditions are
72221SN/A * met: redistributions of source code must retain the above copyright
82221SN/A * notice, this list of conditions and the following disclaimer;
92221SN/A * redistributions in binary form must reproduce the above copyright
102221SN/A * notice, this list of conditions and the following disclaimer in the
112221SN/A * documentation and/or other materials provided with the distribution;
122221SN/A * neither the name of the copyright holders nor the names of its
132221SN/A * contributors may be used to endorse or promote products derived from
142221SN/A * this software without specific prior written permission.
152221SN/A *
162221SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172221SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182221SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192221SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202221SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212221SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222221SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232221SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242221SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252221SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262221SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Gabe Black
292665Ssaidi@eecs.umich.edu *          Kevin Lim
302221SN/A */
312221SN/A
3211793Sbrandon.potter@amd.com#include "arch/sparc/faults.hh"
3311793Sbrandon.potter@amd.com
343415Sgblack@eecs.umich.edu#include <algorithm>
353415Sgblack@eecs.umich.edu
363415Sgblack@eecs.umich.edu#include "arch/sparc/isa_traits.hh"
378778Sgblack@eecs.umich.edu#include "arch/sparc/process.hh"
3812461Sgabeblack@google.com#include "arch/sparc/tlb.hh"
393578Sgblack@eecs.umich.edu#include "arch/sparc/types.hh"
403415Sgblack@eecs.umich.edu#include "base/bitfield.hh"
413415Sgblack@eecs.umich.edu#include "base/trace.hh"
423415Sgblack@eecs.umich.edu#include "cpu/base.hh"
432680Sktlim@umich.edu#include "cpu/thread_context.hh"
443415Sgblack@eecs.umich.edu#include "mem/page_table.hh"
4511793Sbrandon.potter@amd.com#include "sim/full_system.hh"
462800Ssaidi@eecs.umich.edu#include "sim/process.hh"
472221SN/A
483415Sgblack@eecs.umich.eduusing namespace std;
493415Sgblack@eecs.umich.edu
502223SN/Anamespace SparcISA
512221SN/A{
522221SN/A
533573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
5412517Srekai.gonzalezalberquilla@arm.com    SparcFault<PowerOnReset>::vals
5512572Sgabeblack@google.com("power_on_reset", 0x001, 0, {{H, H, H}});
562221SN/A
573573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
5812517Srekai.gonzalezalberquilla@arm.com    SparcFault<WatchDogReset>::vals
5912572Sgabeblack@google.com("watch_dog_reset", 0x002, 120, {{H, H, H}});
602221SN/A
613573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
6212517Srekai.gonzalezalberquilla@arm.com    SparcFault<ExternallyInitiatedReset>::vals
6312572Sgabeblack@google.com("externally_initiated_reset", 0x003, 110, {{H, H, H}});
642221SN/A
653573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
6612517Srekai.gonzalezalberquilla@arm.com    SparcFault<SoftwareInitiatedReset>::vals
6712572Sgabeblack@google.com("software_initiated_reset", 0x004, 130, {{SH, SH, H}});
682221SN/A
693573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
7012517Srekai.gonzalezalberquilla@arm.com    SparcFault<REDStateException>::vals
7112572Sgabeblack@google.com("RED_state_exception", 0x005, 1, {{H, H, H}});
722221SN/A
733573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
7412517Srekai.gonzalezalberquilla@arm.com    SparcFault<StoreError>::vals
7512572Sgabeblack@google.com("store_error", 0x007, 201, {{H, H, H}});
762221SN/A
773573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
7812517Srekai.gonzalezalberquilla@arm.com    SparcFault<InstructionAccessException>::vals
7912572Sgabeblack@google.com("instruction_access_exception", 0x008, 300, {{H, H, H}});
803576Sgblack@eecs.umich.edu
813576Sgblack@eecs.umich.edu//XXX This trap is apparently dropped from ua2005
823576Sgblack@eecs.umich.edu/*template<> SparcFaultBase::FaultVals
8312517Srekai.gonzalezalberquilla@arm.com    SparcFault<InstructionAccessMMUMiss>::vals
8412572Sgabeblack@google.com    ("inst_mmu", 0x009, 2, {{H, H, H}});*/
852221SN/A
863573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
8712517Srekai.gonzalezalberquilla@arm.com    SparcFault<InstructionAccessError>::vals
8812572Sgabeblack@google.com("instruction_access_error", 0x00A, 400, {{H, H, H}});
892221SN/A
903573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
9112517Srekai.gonzalezalberquilla@arm.com    SparcFault<IllegalInstruction>::vals
9212572Sgabeblack@google.com("illegal_instruction", 0x010, 620, {{H, H, H}});
932221SN/A
943573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
9512517Srekai.gonzalezalberquilla@arm.com    SparcFault<PrivilegedOpcode>::vals
9612572Sgabeblack@google.com("privileged_opcode", 0x011, 700, {{P, SH, SH}});
973576Sgblack@eecs.umich.edu
983576Sgblack@eecs.umich.edu//XXX This trap is apparently dropped from ua2005
993576Sgblack@eecs.umich.edu/*template<> SparcFaultBase::FaultVals
10012517Srekai.gonzalezalberquilla@arm.com    SparcFault<UnimplementedLDD>::vals
10112572Sgabeblack@google.com    ("unimp_ldd", 0x012, 6, {{H, H, H}});*/
1023576Sgblack@eecs.umich.edu
1033576Sgblack@eecs.umich.edu//XXX This trap is apparently dropped from ua2005
1043576Sgblack@eecs.umich.edu/*template<> SparcFaultBase::FaultVals
10512517Srekai.gonzalezalberquilla@arm.com    SparcFault<UnimplementedSTD>::vals
10612572Sgabeblack@google.com    ("unimp_std", 0x013, 6, {{H, H, H}});*/
1072221SN/A
1083573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
10912517Srekai.gonzalezalberquilla@arm.com    SparcFault<FpDisabled>::vals
11012572Sgabeblack@google.com("fp_disabled", 0x020, 800, {{P, P, H}});
1112221SN/A
11212110SRekai.GonzalezAlberquilla@arm.com/* SPARCv8 and SPARCv9 define just fp_disabled trap. SIMD is not contemplated
11312110SRekai.GonzalezAlberquilla@arm.com * as a separate part. Therefore, we use the same code and TT */
11412110SRekai.GonzalezAlberquilla@arm.comtemplate<> SparcFaultBase::FaultVals
11512572Sgabeblack@google.com    SparcFault<VecDisabled>::vals
11612572Sgabeblack@google.com("fp_disabled", 0x020, 800, {{P, P, H}});
11712110SRekai.GonzalezAlberquilla@arm.com
1183573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
11912517Srekai.gonzalezalberquilla@arm.com    SparcFault<FpExceptionIEEE754>::vals
12012572Sgabeblack@google.com("fp_exception_ieee_754", 0x021, 1110, {{P, P, H}});
1212221SN/A
1223573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
12312517Srekai.gonzalezalberquilla@arm.com    SparcFault<FpExceptionOther>::vals
12412572Sgabeblack@google.com("fp_exception_other", 0x022, 1110, {{P, P, H}});
1252221SN/A
1263573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
12712517Srekai.gonzalezalberquilla@arm.com    SparcFault<TagOverflow>::vals
12812572Sgabeblack@google.com("tag_overflow", 0x023, 1400, {{P, P, H}});
1292221SN/A
1303573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
13112517Srekai.gonzalezalberquilla@arm.com    SparcFault<CleanWindow>::vals
13212572Sgabeblack@google.com("clean_window", 0x024, 1010, {{P, P, H}});
1332221SN/A
1343573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
13512517Srekai.gonzalezalberquilla@arm.com    SparcFault<DivisionByZero>::vals
13612572Sgabeblack@google.com("division_by_zero", 0x028, 1500, {{P, P, H}});
1372223SN/A
1383573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
13912517Srekai.gonzalezalberquilla@arm.com    SparcFault<InternalProcessorError>::vals
14012572Sgabeblack@google.com("internal_processor_error", 0x029, 4, {{H, H, H}});
1412223SN/A
1423573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
14312517Srekai.gonzalezalberquilla@arm.com    SparcFault<InstructionInvalidTSBEntry>::vals
14412572Sgabeblack@google.com("instruction_invalid_tsb_entry", 0x02A, 210, {{H, H, SH}});
1452223SN/A
1463573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
14712517Srekai.gonzalezalberquilla@arm.com    SparcFault<DataInvalidTSBEntry>::vals
14812572Sgabeblack@google.com("data_invalid_tsb_entry", 0x02B, 1203, {{H, H, H}});
1492223SN/A
1503573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
15112517Srekai.gonzalezalberquilla@arm.com    SparcFault<DataAccessException>::vals
15212572Sgabeblack@google.com("data_access_exception", 0x030, 1201, {{H, H, H}});
1533576Sgblack@eecs.umich.edu
1543576Sgblack@eecs.umich.edu//XXX This trap is apparently dropped from ua2005
1553576Sgblack@eecs.umich.edu/*template<> SparcFaultBase::FaultVals
15612517Srekai.gonzalezalberquilla@arm.com    SparcFault<DataAccessMMUMiss>::vals
15712572Sgabeblack@google.com    ("data_mmu", 0x031, 12, {{H, H, H}});*/
1582223SN/A
1593573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
16012517Srekai.gonzalezalberquilla@arm.com    SparcFault<DataAccessError>::vals
16112572Sgabeblack@google.com("data_access_error", 0x032, 1210, {{H, H, H}});
1622223SN/A
1633573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
16412517Srekai.gonzalezalberquilla@arm.com    SparcFault<DataAccessProtection>::vals
16512572Sgabeblack@google.com("data_access_protection", 0x033, 1207, {{H, H, H}});
1662223SN/A
1673573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
16812517Srekai.gonzalezalberquilla@arm.com    SparcFault<MemAddressNotAligned>::vals
16912572Sgabeblack@google.com("mem_address_not_aligned", 0x034, 1020, {{H, H, H}});
1702223SN/A
1713573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
17212517Srekai.gonzalezalberquilla@arm.com    SparcFault<LDDFMemAddressNotAligned>::vals
17312572Sgabeblack@google.com("LDDF_mem_address_not_aligned", 0x035, 1010, {{H, H, H}});
1742223SN/A
1753573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
17612517Srekai.gonzalezalberquilla@arm.com    SparcFault<STDFMemAddressNotAligned>::vals
17712572Sgabeblack@google.com("STDF_mem_address_not_aligned", 0x036, 1010, {{H, H, H}});
1782223SN/A
1793573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
18012517Srekai.gonzalezalberquilla@arm.com    SparcFault<PrivilegedAction>::vals
18112572Sgabeblack@google.com("privileged_action", 0x037, 1110, {{H, H, SH}});
1822223SN/A
1833573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
18412517Srekai.gonzalezalberquilla@arm.com    SparcFault<LDQFMemAddressNotAligned>::vals
18512572Sgabeblack@google.com("LDQF_mem_address_not_aligned", 0x038, 1010, {{H, H, H}});
1862223SN/A
1873573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
18812517Srekai.gonzalezalberquilla@arm.com    SparcFault<STQFMemAddressNotAligned>::vals
18912572Sgabeblack@google.com("STQF_mem_address_not_aligned", 0x039, 1010, {{H, H, H}});
1902223SN/A
1913573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
19212517Srekai.gonzalezalberquilla@arm.com    SparcFault<InstructionRealTranslationMiss>::vals
19312572Sgabeblack@google.com("instruction_real_translation_miss", 0x03E, 208, {{H, H, SH}});
1942223SN/A
1953573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
19612517Srekai.gonzalezalberquilla@arm.com    SparcFault<DataRealTranslationMiss>::vals
19712572Sgabeblack@google.com("data_real_translation_miss", 0x03F, 1203, {{H, H, H}});
1982223SN/A
1993576Sgblack@eecs.umich.edu//XXX This trap is apparently dropped from ua2005
2003576Sgblack@eecs.umich.edu/*template<> SparcFaultBase::FaultVals
20112517Srekai.gonzalezalberquilla@arm.com    SparcFault<AsyncDataError>::vals
20212572Sgabeblack@google.com    ("async_data", 0x040, 2, {{H, H, H}});*/
2032527SN/A
2043573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
20512517Srekai.gonzalezalberquilla@arm.com    SparcFault<InterruptLevelN>::vals
20612572Sgabeblack@google.com("interrupt_level_n", 0x040, 0, {{P, P, SH}});
2072223SN/A
2083573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
20912517Srekai.gonzalezalberquilla@arm.com    SparcFault<HstickMatch>::vals
21012572Sgabeblack@google.com("hstick_match", 0x05E, 1601, {{H, H, H}});
2112223SN/A
2123573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
21312517Srekai.gonzalezalberquilla@arm.com    SparcFault<TrapLevelZero>::vals
21412572Sgabeblack@google.com("trap_level_zero", 0x05F, 202, {{H, H, SH}});
2152223SN/A
2163573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
21712517Srekai.gonzalezalberquilla@arm.com    SparcFault<InterruptVector>::vals
21812572Sgabeblack@google.com("interrupt_vector", 0x060, 2630, {{H, H, H}});
2194103Ssaidi@eecs.umich.edu
2204103Ssaidi@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
22112517Srekai.gonzalezalberquilla@arm.com    SparcFault<PAWatchpoint>::vals
22212572Sgabeblack@google.com("PA_watchpoint", 0x061, 1209, {{H, H, H}});
2232223SN/A
2243573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
22512517Srekai.gonzalezalberquilla@arm.com    SparcFault<VAWatchpoint>::vals
22612572Sgabeblack@google.com("VA_watchpoint", 0x062, 1120, {{P, P, SH}});
2272223SN/A
2283573Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
22912517Srekai.gonzalezalberquilla@arm.com    SparcFault<FastInstructionAccessMMUMiss>::vals
23012572Sgabeblack@google.com("fast_instruction_access_MMU_miss", 0x064, 208, {{H, H, SH}});
2313576Sgblack@eecs.umich.edu
2323576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
23312517Srekai.gonzalezalberquilla@arm.com    SparcFault<FastDataAccessMMUMiss>::vals
23412572Sgabeblack@google.com("fast_data_access_MMU_miss", 0x068, 1203, {{H, H, H}});
2353576Sgblack@eecs.umich.edu
2363576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
23712517Srekai.gonzalezalberquilla@arm.com    SparcFault<FastDataAccessProtection>::vals
23812572Sgabeblack@google.com("fast_data_access_protection", 0x06C, 1207, {{H, H, H}});
2393576Sgblack@eecs.umich.edu
2403576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
24112517Srekai.gonzalezalberquilla@arm.com    SparcFault<InstructionBreakpoint>::vals
24212572Sgabeblack@google.com("instruction_break", 0x076, 610, {{H, H, H}});
2433576Sgblack@eecs.umich.edu
2443576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
24512517Srekai.gonzalezalberquilla@arm.com    SparcFault<CpuMondo>::vals
24612572Sgabeblack@google.com("cpu_mondo", 0x07C, 1608, {{P, P, SH}});
2473576Sgblack@eecs.umich.edu
2483576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
24912517Srekai.gonzalezalberquilla@arm.com    SparcFault<DevMondo>::vals
25012572Sgabeblack@google.com("dev_mondo", 0x07D, 1611, {{P, P, SH}});
2513576Sgblack@eecs.umich.edu
2523576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
25312517Srekai.gonzalezalberquilla@arm.com    SparcFault<ResumableError>::vals
25412572Sgabeblack@google.com("resume_error", 0x07E, 3330, {{P, P, SH}});
2553576Sgblack@eecs.umich.edu
2563576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
25712517Srekai.gonzalezalberquilla@arm.com    SparcFault<SpillNNormal>::vals
25812572Sgabeblack@google.com("spill_n_normal", 0x080, 900, {{P, P, H}});
2593576Sgblack@eecs.umich.edu
2603576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
26112517Srekai.gonzalezalberquilla@arm.com    SparcFault<SpillNOther>::vals
26212572Sgabeblack@google.com("spill_n_other", 0x0A0, 900, {{P, P, H}});
2633576Sgblack@eecs.umich.edu
2643576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
26512517Srekai.gonzalezalberquilla@arm.com    SparcFault<FillNNormal>::vals
26612572Sgabeblack@google.com("fill_n_normal", 0x0C0, 900, {{P, P, H}});
2673576Sgblack@eecs.umich.edu
2683576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
26912517Srekai.gonzalezalberquilla@arm.com    SparcFault<FillNOther>::vals
27012572Sgabeblack@google.com("fill_n_other", 0x0E0, 900, {{P, P, H}});
2713576Sgblack@eecs.umich.edu
2723576Sgblack@eecs.umich.edutemplate<> SparcFaultBase::FaultVals
27312517Srekai.gonzalezalberquilla@arm.com    SparcFault<TrapInstruction>::vals
27412572Sgabeblack@google.com("trap_instruction", 0x100, 1602, {{P, P, H}});
2752223SN/A
2763415Sgblack@eecs.umich.edu/**
2773578Sgblack@eecs.umich.edu * This causes the thread context to enter RED state. This causes the side
2783578Sgblack@eecs.umich.edu * effects which go with entering RED state because of a trap.
2793415Sgblack@eecs.umich.edu */
2803415Sgblack@eecs.umich.edu
2817741Sgblack@eecs.umich.eduvoid
2827741Sgblack@eecs.umich.eduenterREDState(ThreadContext *tc)
2833415Sgblack@eecs.umich.edu{
2843578Sgblack@eecs.umich.edu    //@todo Disable the mmu?
2853578Sgblack@eecs.umich.edu    //@todo Disable watchpoints?
2868829Sgblack@eecs.umich.edu    HPSTATE hpstate= tc->readMiscRegNoEffect(MISCREG_HPSTATE);
2878829Sgblack@eecs.umich.edu    hpstate.red = 1;
2888829Sgblack@eecs.umich.edu    hpstate.hpriv = 1;
2898829Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_HPSTATE, hpstate);
2907741Sgblack@eecs.umich.edu    // PSTATE.priv is set to 1 here. The manual says it should be 0, but
2917741Sgblack@eecs.umich.edu    // Legion sets it to 1.
2928829Sgblack@eecs.umich.edu    PSTATE pstate = tc->readMiscRegNoEffect(MISCREG_PSTATE);
2938829Sgblack@eecs.umich.edu    pstate.priv = 1;
2948829Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_PSTATE, pstate);
2953578Sgblack@eecs.umich.edu}
2963578Sgblack@eecs.umich.edu
2973578Sgblack@eecs.umich.edu/**
2983578Sgblack@eecs.umich.edu * This sets everything up for a RED state trap except for actually jumping to
2993578Sgblack@eecs.umich.edu * the handler.
3003578Sgblack@eecs.umich.edu */
3013578Sgblack@eecs.umich.edu
3027741Sgblack@eecs.umich.eduvoid
3037741Sgblack@eecs.umich.edudoREDFault(ThreadContext *tc, TrapType tt)
3043578Sgblack@eecs.umich.edu{
30513583Sgabeblack@google.com    RegVal TL = tc->readMiscRegNoEffect(MISCREG_TL);
30613583Sgabeblack@google.com    RegVal TSTATE = tc->readMiscRegNoEffect(MISCREG_TSTATE);
3078829Sgblack@eecs.umich.edu    PSTATE pstate = tc->readMiscRegNoEffect(MISCREG_PSTATE);
3088829Sgblack@eecs.umich.edu    HPSTATE hpstate = tc->readMiscRegNoEffect(MISCREG_HPSTATE);
30913583Sgabeblack@google.com    RegVal CCR = tc->readIntReg(NumIntArchRegs + 2);
31013583Sgabeblack@google.com    RegVal ASI = tc->readMiscRegNoEffect(MISCREG_ASI);
31113583Sgabeblack@google.com    RegVal CWP = tc->readMiscRegNoEffect(MISCREG_CWP);
31213583Sgabeblack@google.com    RegVal CANSAVE = tc->readMiscRegNoEffect(NumIntArchRegs + 3);
31313583Sgabeblack@google.com    RegVal GL = tc->readMiscRegNoEffect(MISCREG_GL);
3147720Sgblack@eecs.umich.edu    PCState pc = tc->pcState();
3153578Sgblack@eecs.umich.edu
3163578Sgblack@eecs.umich.edu    TL++;
3173578Sgblack@eecs.umich.edu
3188829Sgblack@eecs.umich.edu    Addr pcMask = pstate.am ? mask(32) : mask(64);
3193928Ssaidi@eecs.umich.edu
3207741Sgblack@eecs.umich.edu    // set TSTATE.gl to gl
3213578Sgblack@eecs.umich.edu    replaceBits(TSTATE, 42, 40, GL);
3227741Sgblack@eecs.umich.edu    // set TSTATE.ccr to ccr
3233578Sgblack@eecs.umich.edu    replaceBits(TSTATE, 39, 32, CCR);
3247741Sgblack@eecs.umich.edu    // set TSTATE.asi to asi
3253578Sgblack@eecs.umich.edu    replaceBits(TSTATE, 31, 24, ASI);
3267741Sgblack@eecs.umich.edu    // set TSTATE.pstate to pstate
3278829Sgblack@eecs.umich.edu    replaceBits(TSTATE, 20, 8, pstate);
3287741Sgblack@eecs.umich.edu    // set TSTATE.cwp to cwp
3293578Sgblack@eecs.umich.edu    replaceBits(TSTATE, 4, 0, CWP);
3303578Sgblack@eecs.umich.edu
3317741Sgblack@eecs.umich.edu    // Write back TSTATE
3324172Ssaidi@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TSTATE, TSTATE);
3333578Sgblack@eecs.umich.edu
3347741Sgblack@eecs.umich.edu    // set TPC to PC
3357720Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TPC, pc.pc() & pcMask);
3367741Sgblack@eecs.umich.edu    // set TNPC to NPC
3377720Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TNPC, pc.npc() & pcMask);
3383578Sgblack@eecs.umich.edu
3397741Sgblack@eecs.umich.edu    // set HTSTATE.hpstate to hpstate
3408829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_HTSTATE, hpstate);
3413578Sgblack@eecs.umich.edu
3427741Sgblack@eecs.umich.edu    // TT = trap type;
3434172Ssaidi@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TT, tt);
3443578Sgblack@eecs.umich.edu
3457741Sgblack@eecs.umich.edu    // Update GL
3464172Ssaidi@eecs.umich.edu    tc->setMiscReg(MISCREG_GL, min<int>(GL+1, MaxGL));
3473578Sgblack@eecs.umich.edu
3488829Sgblack@eecs.umich.edu    bool priv = pstate.priv; // just save the priv bit
3498829Sgblack@eecs.umich.edu    pstate = 0;
3508829Sgblack@eecs.umich.edu    pstate.priv = priv;
3518829Sgblack@eecs.umich.edu    pstate.pef = 1;
3528829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_PSTATE, pstate);
3533578Sgblack@eecs.umich.edu
3548829Sgblack@eecs.umich.edu    hpstate.red = 1;
3558829Sgblack@eecs.umich.edu    hpstate.hpriv = 1;
3568829Sgblack@eecs.umich.edu    hpstate.ibe = 0;
3578829Sgblack@eecs.umich.edu    hpstate.tlz = 0;
3588829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_HPSTATE, hpstate);
3593578Sgblack@eecs.umich.edu
3603578Sgblack@eecs.umich.edu    bool changedCWP = true;
3613893Shsul@eecs.umich.edu    if (tt == 0x24)
3623415Sgblack@eecs.umich.edu        CWP++;
3633893Shsul@eecs.umich.edu    else if (0x80 <= tt && tt <= 0xbf)
3643415Sgblack@eecs.umich.edu        CWP += (CANSAVE + 2);
3653893Shsul@eecs.umich.edu    else if (0xc0 <= tt && tt <= 0xff)
3663415Sgblack@eecs.umich.edu        CWP--;
3673415Sgblack@eecs.umich.edu    else
3683415Sgblack@eecs.umich.edu        changedCWP = false;
3693420Sgblack@eecs.umich.edu
3707741Sgblack@eecs.umich.edu    if (changedCWP) {
3713415Sgblack@eecs.umich.edu        CWP = (CWP + NWindows) % NWindows;
3724172Ssaidi@eecs.umich.edu        tc->setMiscReg(MISCREG_CWP, CWP);
3733415Sgblack@eecs.umich.edu    }
3743415Sgblack@eecs.umich.edu}
3753415Sgblack@eecs.umich.edu
3767741Sgblack@eecs.umich.edu/**
3777741Sgblack@eecs.umich.edu * This sets everything up for a normal trap except for actually jumping to
3787741Sgblack@eecs.umich.edu * the handler.
3797741Sgblack@eecs.umich.edu */
3807741Sgblack@eecs.umich.edu
3817741Sgblack@eecs.umich.eduvoid
3827741Sgblack@eecs.umich.edudoNormalFault(ThreadContext *tc, TrapType tt, bool gotoHpriv)
3837741Sgblack@eecs.umich.edu{
38413583Sgabeblack@google.com    RegVal TL = tc->readMiscRegNoEffect(MISCREG_TL);
38513583Sgabeblack@google.com    RegVal TSTATE = tc->readMiscRegNoEffect(MISCREG_TSTATE);
3868829Sgblack@eecs.umich.edu    PSTATE pstate = tc->readMiscRegNoEffect(MISCREG_PSTATE);
3878829Sgblack@eecs.umich.edu    HPSTATE hpstate = tc->readMiscRegNoEffect(MISCREG_HPSTATE);
38813583Sgabeblack@google.com    RegVal CCR = tc->readIntReg(NumIntArchRegs + 2);
38913583Sgabeblack@google.com    RegVal ASI = tc->readMiscRegNoEffect(MISCREG_ASI);
39013583Sgabeblack@google.com    RegVal CWP = tc->readMiscRegNoEffect(MISCREG_CWP);
39113583Sgabeblack@google.com    RegVal CANSAVE = tc->readIntReg(NumIntArchRegs + 3);
39213583Sgabeblack@google.com    RegVal GL = tc->readMiscRegNoEffect(MISCREG_GL);
3937741Sgblack@eecs.umich.edu    PCState pc = tc->pcState();
3947741Sgblack@eecs.umich.edu
3957741Sgblack@eecs.umich.edu    // Increment the trap level
3967741Sgblack@eecs.umich.edu    TL++;
3977741Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TL, TL);
3987741Sgblack@eecs.umich.edu
3998829Sgblack@eecs.umich.edu    Addr pcMask = pstate.am ? mask(32) : mask(64);
4007741Sgblack@eecs.umich.edu
4017741Sgblack@eecs.umich.edu    // Save off state
4027741Sgblack@eecs.umich.edu
4037741Sgblack@eecs.umich.edu    // set TSTATE.gl to gl
4047741Sgblack@eecs.umich.edu    replaceBits(TSTATE, 42, 40, GL);
4057741Sgblack@eecs.umich.edu    // set TSTATE.ccr to ccr
4067741Sgblack@eecs.umich.edu    replaceBits(TSTATE, 39, 32, CCR);
4077741Sgblack@eecs.umich.edu    // set TSTATE.asi to asi
4087741Sgblack@eecs.umich.edu    replaceBits(TSTATE, 31, 24, ASI);
4097741Sgblack@eecs.umich.edu    // set TSTATE.pstate to pstate
4108829Sgblack@eecs.umich.edu    replaceBits(TSTATE, 20, 8, pstate);
4117741Sgblack@eecs.umich.edu    // set TSTATE.cwp to cwp
4127741Sgblack@eecs.umich.edu    replaceBits(TSTATE, 4, 0, CWP);
4137741Sgblack@eecs.umich.edu
4147741Sgblack@eecs.umich.edu    // Write back TSTATE
4157741Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TSTATE, TSTATE);
4167741Sgblack@eecs.umich.edu
4177741Sgblack@eecs.umich.edu    // set TPC to PC
4187741Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TPC, pc.pc() & pcMask);
4197741Sgblack@eecs.umich.edu    // set TNPC to NPC
4207741Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TNPC, pc.npc() & pcMask);
4217741Sgblack@eecs.umich.edu
4227741Sgblack@eecs.umich.edu    // set HTSTATE.hpstate to hpstate
4238829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_HTSTATE, hpstate);
4247741Sgblack@eecs.umich.edu
4257741Sgblack@eecs.umich.edu    // TT = trap type;
4267741Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TT, tt);
4277741Sgblack@eecs.umich.edu
4287741Sgblack@eecs.umich.edu    // Update the global register level
4297741Sgblack@eecs.umich.edu    if (!gotoHpriv)
4307741Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_GL, min<int>(GL + 1, MaxPGL));
4317741Sgblack@eecs.umich.edu    else
4327741Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_GL, min<int>(GL + 1, MaxGL));
4337741Sgblack@eecs.umich.edu
4348829Sgblack@eecs.umich.edu    // pstate.mm is unchanged
4358829Sgblack@eecs.umich.edu    pstate.pef = 1; // PSTATE.pef = whether or not an fpu is present
4368829Sgblack@eecs.umich.edu    pstate.am = 0;
4378829Sgblack@eecs.umich.edu    pstate.ie = 0;
4388829Sgblack@eecs.umich.edu    // pstate.tle is unchanged
4398829Sgblack@eecs.umich.edu    // pstate.tct = 0
4407741Sgblack@eecs.umich.edu
4417741Sgblack@eecs.umich.edu    if (gotoHpriv) {
4428829Sgblack@eecs.umich.edu        pstate.cle = 0;
4437741Sgblack@eecs.umich.edu        // The manual says PSTATE.priv should be 0, but Legion leaves it alone
4448829Sgblack@eecs.umich.edu        hpstate.red = 0;
4458829Sgblack@eecs.umich.edu        hpstate.hpriv = 1;
4468829Sgblack@eecs.umich.edu        hpstate.ibe = 0;
4478829Sgblack@eecs.umich.edu        // hpstate.tlz is unchanged
4488829Sgblack@eecs.umich.edu        tc->setMiscRegNoEffect(MISCREG_HPSTATE, hpstate);
4497741Sgblack@eecs.umich.edu    } else { // we are going to priv
4508829Sgblack@eecs.umich.edu        pstate.priv = 1;
4518829Sgblack@eecs.umich.edu        pstate.cle = pstate.tle;
4527741Sgblack@eecs.umich.edu    }
4538829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_PSTATE, pstate);
4547741Sgblack@eecs.umich.edu
4557741Sgblack@eecs.umich.edu
4567741Sgblack@eecs.umich.edu    bool changedCWP = true;
4577741Sgblack@eecs.umich.edu    if (tt == 0x24)
4587741Sgblack@eecs.umich.edu        CWP++;
4597741Sgblack@eecs.umich.edu    else if (0x80 <= tt && tt <= 0xbf)
4607741Sgblack@eecs.umich.edu        CWP += (CANSAVE + 2);
4617741Sgblack@eecs.umich.edu    else if (0xc0 <= tt && tt <= 0xff)
4627741Sgblack@eecs.umich.edu        CWP--;
4637741Sgblack@eecs.umich.edu    else
4647741Sgblack@eecs.umich.edu        changedCWP = false;
4657741Sgblack@eecs.umich.edu
4667741Sgblack@eecs.umich.edu    if (changedCWP) {
4677741Sgblack@eecs.umich.edu        CWP = (CWP + NWindows) % NWindows;
4687741Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CWP, CWP);
4697741Sgblack@eecs.umich.edu    }
4707741Sgblack@eecs.umich.edu}
4717741Sgblack@eecs.umich.edu
4727741Sgblack@eecs.umich.eduvoid
47313583Sgabeblack@google.comgetREDVector(RegVal TT, Addr &PC, Addr &NPC)
4743578Sgblack@eecs.umich.edu{
4753585Sgblack@eecs.umich.edu    //XXX The following constant might belong in a header file.
4763603Ssaidi@eecs.umich.edu    const Addr RSTVAddr = 0xFFF0000000ULL;
4773595Sgblack@eecs.umich.edu    PC = RSTVAddr | ((TT << 5) & 0xFF);
4783578Sgblack@eecs.umich.edu    NPC = PC + sizeof(MachInst);
4793578Sgblack@eecs.umich.edu}
4803578Sgblack@eecs.umich.edu
4817741Sgblack@eecs.umich.eduvoid
48213583Sgabeblack@google.comgetHyperVector(ThreadContext * tc, Addr &PC, Addr &NPC, RegVal TT)
4833578Sgblack@eecs.umich.edu{
4844172Ssaidi@eecs.umich.edu    Addr HTBA = tc->readMiscRegNoEffect(MISCREG_HTBA);
4853578Sgblack@eecs.umich.edu    PC = (HTBA & ~mask(14)) | ((TT << 5) & mask(14));
4863578Sgblack@eecs.umich.edu    NPC = PC + sizeof(MachInst);
4873578Sgblack@eecs.umich.edu}
4883578Sgblack@eecs.umich.edu
4897741Sgblack@eecs.umich.eduvoid
49013583Sgabeblack@google.comgetPrivVector(ThreadContext *tc, Addr &PC, Addr &NPC, RegVal TT, RegVal TL)
4913578Sgblack@eecs.umich.edu{
4924172Ssaidi@eecs.umich.edu    Addr TBA = tc->readMiscRegNoEffect(MISCREG_TBA);
4933578Sgblack@eecs.umich.edu    PC = (TBA & ~mask(15)) |
4943578Sgblack@eecs.umich.edu        (TL > 1 ? (1 << 14) : 0) |
4953578Sgblack@eecs.umich.edu        ((TT << 5) & mask(14));
4963578Sgblack@eecs.umich.edu    NPC = PC + sizeof(MachInst);
4973578Sgblack@eecs.umich.edu}
4983578Sgblack@eecs.umich.edu
4997741Sgblack@eecs.umich.eduvoid
50010417Sandreas.hansson@arm.comSparcFaultBase::invoke(ThreadContext * tc, const StaticInstPtr &inst)
5012221SN/A{
5022680Sktlim@umich.edu    FaultBase::invoke(tc);
5038750Sgblack@eecs.umich.edu    if (!FullSystem)
5048750Sgblack@eecs.umich.edu        return;
5058750Sgblack@eecs.umich.edu
5062223SN/A    countStat()++;
5072221SN/A
5087741Sgblack@eecs.umich.edu    // We can refer to this to see what the trap level -was-, but something
5097741Sgblack@eecs.umich.edu    // in the middle could change it in the regfile out from under us.
51013583Sgabeblack@google.com    RegVal tl = tc->readMiscRegNoEffect(MISCREG_TL);
51113583Sgabeblack@google.com    RegVal tt = tc->readMiscRegNoEffect(MISCREG_TT);
5128829Sgblack@eecs.umich.edu    PSTATE pstate = tc->readMiscRegNoEffect(MISCREG_PSTATE);
5138829Sgblack@eecs.umich.edu    HPSTATE hpstate = tc->readMiscRegNoEffect(MISCREG_HPSTATE);
5143578Sgblack@eecs.umich.edu
5153578Sgblack@eecs.umich.edu    Addr PC, NPC;
5163578Sgblack@eecs.umich.edu
5173578Sgblack@eecs.umich.edu    PrivilegeLevel current;
5188829Sgblack@eecs.umich.edu    if (hpstate.hpriv)
5193746Sgblack@eecs.umich.edu        current = Hyperprivileged;
5208829Sgblack@eecs.umich.edu    else if (pstate.priv)
5213578Sgblack@eecs.umich.edu        current = Privileged;
5223578Sgblack@eecs.umich.edu    else
5233746Sgblack@eecs.umich.edu        current = User;
5243578Sgblack@eecs.umich.edu
5253578Sgblack@eecs.umich.edu    PrivilegeLevel level = getNextLevel(current);
5263578Sgblack@eecs.umich.edu
5278829Sgblack@eecs.umich.edu    if (hpstate.red || (tl == MaxTL - 1)) {
5283595Sgblack@eecs.umich.edu        getREDVector(5, PC, NPC);
5293893Shsul@eecs.umich.edu        doREDFault(tc, tt);
5307741Sgblack@eecs.umich.edu        // This changes the hpstate and pstate, so we need to make sure we
5317741Sgblack@eecs.umich.edu        // save the old version on the trap stack in doREDFault.
5323578Sgblack@eecs.umich.edu        enterREDState(tc);
5333893Shsul@eecs.umich.edu    } else if (tl == MaxTL) {
5343825Ssaidi@eecs.umich.edu        panic("Should go to error state here.. crap\n");
5357741Sgblack@eecs.umich.edu        // Do error_state somehow?
5367741Sgblack@eecs.umich.edu        // Probably inject a WDR fault using the interrupt mechanism.
5377741Sgblack@eecs.umich.edu        // What should the PC and NPC be set to?
5383893Shsul@eecs.umich.edu    } else if (tl > MaxPTL && level == Privileged) {
5397741Sgblack@eecs.umich.edu        // guest_watchdog fault
5403578Sgblack@eecs.umich.edu        doNormalFault(tc, trapType(), true);
5413585Sgblack@eecs.umich.edu        getHyperVector(tc, PC, NPC, 2);
5423893Shsul@eecs.umich.edu    } else if (level == Hyperprivileged ||
5435570Snate@binkert.org               (level == Privileged && trapType() >= 384)) {
5443578Sgblack@eecs.umich.edu        doNormalFault(tc, trapType(), true);
5453585Sgblack@eecs.umich.edu        getHyperVector(tc, PC, NPC, trapType());
5463826Ssaidi@eecs.umich.edu    } else {
5473578Sgblack@eecs.umich.edu        doNormalFault(tc, trapType(), false);
5487741Sgblack@eecs.umich.edu        getPrivVector(tc, PC, NPC, trapType(), tl + 1);
5493578Sgblack@eecs.umich.edu    }
5503578Sgblack@eecs.umich.edu
5517720Sgblack@eecs.umich.edu    PCState pc;
5527720Sgblack@eecs.umich.edu    pc.pc(PC);
5537720Sgblack@eecs.umich.edu    pc.npc(NPC);
5547720Sgblack@eecs.umich.edu    pc.nnpc(NPC + sizeof(MachInst));
5557720Sgblack@eecs.umich.edu    pc.upc(0);
5567720Sgblack@eecs.umich.edu    pc.nupc(1);
5577720Sgblack@eecs.umich.edu    tc->pcState(pc);
5583420Sgblack@eecs.umich.edu}
5592221SN/A
5607741Sgblack@eecs.umich.eduvoid
56110417Sandreas.hansson@arm.comPowerOnReset::invoke(ThreadContext *tc, const StaticInstPtr &inst)
5623523Sgblack@eecs.umich.edu{
5637741Sgblack@eecs.umich.edu    // For SPARC, when a system is first started, there is a power
5647741Sgblack@eecs.umich.edu    // on reset Trap which sets the processor into the following state.
5657741Sgblack@eecs.umich.edu    // Bits that aren't set aren't defined on startup.
5663595Sgblack@eecs.umich.edu
5674172Ssaidi@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TL, MaxTL);
5684172Ssaidi@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TT, trapType());
5694172Ssaidi@eecs.umich.edu    tc->setMiscReg(MISCREG_GL, MaxGL);
5703595Sgblack@eecs.umich.edu
5718829Sgblack@eecs.umich.edu    PSTATE pstate = 0;
5728829Sgblack@eecs.umich.edu    pstate.pef = 1;
5738829Sgblack@eecs.umich.edu    pstate.priv = 1;
5748829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_PSTATE, pstate);
5753595Sgblack@eecs.umich.edu
5767741Sgblack@eecs.umich.edu    // Turn on red and hpriv, set everything else to 0
5778829Sgblack@eecs.umich.edu    HPSTATE hpstate = tc->readMiscRegNoEffect(MISCREG_HPSTATE);
5788829Sgblack@eecs.umich.edu    hpstate.red = 1;
5798829Sgblack@eecs.umich.edu    hpstate.hpriv = 1;
5808829Sgblack@eecs.umich.edu    hpstate.ibe = 0;
5818829Sgblack@eecs.umich.edu    hpstate.tlz = 0;
5828829Sgblack@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_HPSTATE, hpstate);
5833595Sgblack@eecs.umich.edu
5847741Sgblack@eecs.umich.edu    // The tick register is unreadable by nonprivileged software
5854172Ssaidi@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TICK, 1ULL << 63);
5863595Sgblack@eecs.umich.edu
5877741Sgblack@eecs.umich.edu    // Enter RED state. We do this last so that the actual state preserved in
5887741Sgblack@eecs.umich.edu    // the trap stack is the state from before this fault.
5893746Sgblack@eecs.umich.edu    enterREDState(tc);
5903746Sgblack@eecs.umich.edu
5913595Sgblack@eecs.umich.edu    Addr PC, NPC;
5923595Sgblack@eecs.umich.edu    getREDVector(trapType(), PC, NPC);
5937720Sgblack@eecs.umich.edu
5947720Sgblack@eecs.umich.edu    PCState pc;
5957720Sgblack@eecs.umich.edu    pc.pc(PC);
5967720Sgblack@eecs.umich.edu    pc.npc(NPC);
5977720Sgblack@eecs.umich.edu    pc.nnpc(NPC + sizeof(MachInst));
5987720Sgblack@eecs.umich.edu    pc.upc(0);
5997720Sgblack@eecs.umich.edu    pc.nupc(1);
6007720Sgblack@eecs.umich.edu    tc->pcState(pc);
6013595Sgblack@eecs.umich.edu
6027741Sgblack@eecs.umich.edu    // These registers are specified as "undefined" after a POR, and they
6037741Sgblack@eecs.umich.edu    // should have reasonable values after the miscregfile is reset
6043523Sgblack@eecs.umich.edu    /*
6053595Sgblack@eecs.umich.edu    // Clear all the soft interrupt bits
6063595Sgblack@eecs.umich.edu    softint = 0;
6073595Sgblack@eecs.umich.edu    // disable timer compare interrupts, reset tick_cmpr
6084172Ssaidi@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_
6093595Sgblack@eecs.umich.edu    tick_cmprFields.int_dis = 1;
6103523Sgblack@eecs.umich.edu    tick_cmprFields.tick_cmpr = 0; // Reset to 0 for pretty printing
6117741Sgblack@eecs.umich.edu    stickFields.npt = 1; // The TICK register is unreadable by by !priv
6123523Sgblack@eecs.umich.edu    stick_cmprFields.int_dis = 1; // disable timer compare interrupts
6133523Sgblack@eecs.umich.edu    stick_cmprFields.tick_cmpr = 0; // Reset to 0 for pretty printing
6143523Sgblack@eecs.umich.edu
6153523Sgblack@eecs.umich.edu    tt[tl] = _trapType;
6163523Sgblack@eecs.umich.edu
6173523Sgblack@eecs.umich.edu    hintp = 0; // no interrupts pending
6183523Sgblack@eecs.umich.edu    hstick_cmprFields.int_dis = 1; // disable timer compare interrupts
6193523Sgblack@eecs.umich.edu    hstick_cmprFields.tick_cmpr = 0; // Reset to 0 for pretty printing
6203523Sgblack@eecs.umich.edu    */
6212221SN/A}
6222221SN/A
6237741Sgblack@eecs.umich.eduvoid
62410417Sandreas.hansson@arm.comFastInstructionAccessMMUMiss::invoke(ThreadContext *tc,
62510417Sandreas.hansson@arm.com                                     const StaticInstPtr &inst)
6264997Sgblack@eecs.umich.edu{
6278767Sgblack@eecs.umich.edu    if (FullSystem) {
6288767Sgblack@eecs.umich.edu        SparcFaultBase::invoke(tc, inst);
6298806Sgblack@eecs.umich.edu        return;
6308806Sgblack@eecs.umich.edu    }
6318806Sgblack@eecs.umich.edu
6328806Sgblack@eecs.umich.edu    Process *p = tc->getProcessPtr();
63312461Sgabeblack@google.com    const EmulationPageTable::Entry *pte = p->pTable->lookup(vaddr);
63412461Sgabeblack@google.com    panic_if(!pte, "Tried to execute unmapped address %#x.\n", vaddr);
63511850Sbrandon.potter@amd.com
63612455Sgabeblack@google.com    Addr alignedvaddr = p->pTable->pageAlign(vaddr);
63711850Sbrandon.potter@amd.com
63812455Sgabeblack@google.com    // Grab fields used during instruction translation to figure out
63912455Sgabeblack@google.com    // which context to use.
64012455Sgabeblack@google.com    uint64_t tlbdata = tc->readMiscRegNoEffect(MISCREG_TLB_DATA);
64111850Sbrandon.potter@amd.com
64212455Sgabeblack@google.com    // Inside a VM, a real address is the address that guest OS would
64312455Sgabeblack@google.com    // interpret to be a physical address. To map to the physical address,
64412455Sgabeblack@google.com    // it still needs to undergo a translation. The instruction
64512455Sgabeblack@google.com    // translation code in the SPARC ITLB code assumes that the context is
64612455Sgabeblack@google.com    // zero (kernel-level) if real addressing is being used.
64712455Sgabeblack@google.com    bool is_real_address = !bits(tlbdata, 4);
64811850Sbrandon.potter@amd.com
64912455Sgabeblack@google.com    // The SPARC ITLB code assumes that traps are executed in context
65012455Sgabeblack@google.com    // zero so we carry that assumption through here.
65112455Sgabeblack@google.com    bool trapped = bits(tlbdata, 18, 16) > 0;
65211850Sbrandon.potter@amd.com
65312455Sgabeblack@google.com    // The primary context acts as a PASID. It allows the MMU to
65412455Sgabeblack@google.com    // distinguish between virtual addresses that would alias to the
65512455Sgabeblack@google.com    // same physical address (if two or more processes shared the same
65612455Sgabeblack@google.com    // virtual address mapping).
65712455Sgabeblack@google.com    int primary_context = bits(tlbdata, 47, 32);
65811850Sbrandon.potter@amd.com
65912455Sgabeblack@google.com    // The partition id distinguishes between virtualized environments.
66012455Sgabeblack@google.com    int const partition_id = 0;
66111850Sbrandon.potter@amd.com
66212455Sgabeblack@google.com    // Given the assumptions in the translateInst code in the SPARC ITLB,
66312455Sgabeblack@google.com    // the logic works out to the following for the context.
66412455Sgabeblack@google.com    int context_id = (is_real_address || trapped) ? 0 : primary_context;
66512455Sgabeblack@google.com
66612461Sgabeblack@google.com    TlbEntry entry(p->pTable->pid(), alignedvaddr, pte->paddr,
66712461Sgabeblack@google.com                   pte->flags & EmulationPageTable::Uncacheable,
66812461Sgabeblack@google.com                   pte->flags & EmulationPageTable::ReadOnly);
66912461Sgabeblack@google.com
67012455Sgabeblack@google.com    // Insert the TLB entry.
67112455Sgabeblack@google.com    // The entry specifying whether the address is "real" is set to
67212455Sgabeblack@google.com    // false for syscall emulation mode regardless of whether the
67312455Sgabeblack@google.com    // address is real in preceding code. Not sure sure that this is
67412455Sgabeblack@google.com    // correct, but also not sure if it matters at all.
67512455Sgabeblack@google.com    dynamic_cast<TLB *>(tc->getITBPtr())->
67612461Sgabeblack@google.com        insert(alignedvaddr, partition_id, context_id, false, entry.pte);
6774997Sgblack@eecs.umich.edu}
6784997Sgblack@eecs.umich.edu
6797741Sgblack@eecs.umich.eduvoid
68010417Sandreas.hansson@arm.comFastDataAccessMMUMiss::invoke(ThreadContext *tc, const StaticInstPtr &inst)
6814997Sgblack@eecs.umich.edu{
6828767Sgblack@eecs.umich.edu    if (FullSystem) {
6838767Sgblack@eecs.umich.edu        SparcFaultBase::invoke(tc, inst);
6848806Sgblack@eecs.umich.edu        return;
6858806Sgblack@eecs.umich.edu    }
6868806Sgblack@eecs.umich.edu
6878806Sgblack@eecs.umich.edu    Process *p = tc->getProcessPtr();
68812461Sgabeblack@google.com    const EmulationPageTable::Entry *pte = p->pTable->lookup(vaddr);
68912461Sgabeblack@google.com    if (!pte && p->fixupStackFault(vaddr))
69012461Sgabeblack@google.com        pte = p->pTable->lookup(vaddr);
69112461Sgabeblack@google.com    panic_if(!pte, "Tried to access unmapped address %#x.\n", vaddr);
69211850Sbrandon.potter@amd.com
69312455Sgabeblack@google.com    Addr alignedvaddr = p->pTable->pageAlign(vaddr);
69411850Sbrandon.potter@amd.com
69512455Sgabeblack@google.com    // Grab fields used during data translation to figure out
69612455Sgabeblack@google.com    // which context to use.
69712455Sgabeblack@google.com    uint64_t tlbdata = tc->readMiscRegNoEffect(MISCREG_TLB_DATA);
69811850Sbrandon.potter@amd.com
69912455Sgabeblack@google.com    // The primary context acts as a PASID. It allows the MMU to
70012455Sgabeblack@google.com    // distinguish between virtual addresses that would alias to the
70112455Sgabeblack@google.com    // same physical address (if two or more processes shared the same
70212455Sgabeblack@google.com    // virtual address mapping). There's a secondary context used in the
70312455Sgabeblack@google.com    // DTLB translation code, but it should __probably__ be zero for
70412455Sgabeblack@google.com    // syscall emulation code. (The secondary context is used by Solaris
70512455Sgabeblack@google.com    // to allow kernel privilege code to access user space code:
70612455Sgabeblack@google.com    // [ISBN 0-13-022496-0]:PG199.)
70712455Sgabeblack@google.com    int primary_context = bits(tlbdata, 47, 32);
70811850Sbrandon.potter@amd.com
70912455Sgabeblack@google.com    // "Hyper-Privileged Mode" is in use. There are three main modes of
71012455Sgabeblack@google.com    // operation for Sparc: Hyper-Privileged Mode, Privileged Mode, and
71112455Sgabeblack@google.com    // User Mode.
71212455Sgabeblack@google.com    int hpriv = bits(tlbdata, 0);
71311850Sbrandon.potter@amd.com
71412455Sgabeblack@google.com    // Reset, Error and Debug state is in use. Something horrible has
71512455Sgabeblack@google.com    // happened or the system is operating in Reset Mode.
71612455Sgabeblack@google.com    int red = bits(tlbdata, 1);
71711850Sbrandon.potter@amd.com
71812455Sgabeblack@google.com    // Inside a VM, a real address is the address that guest OS would
71912455Sgabeblack@google.com    // interpret to be a physical address. To map to the physical address,
72012455Sgabeblack@google.com    // it still needs to undergo a translation. The instruction
72112455Sgabeblack@google.com    // translation code in the SPARC ITLB code assumes that the context is
72212455Sgabeblack@google.com    // zero (kernel-level) if real addressing is being used.
72312455Sgabeblack@google.com    int is_real_address = !bits(tlbdata, 5);
72411850Sbrandon.potter@amd.com
72512455Sgabeblack@google.com    // Grab the address space identifier register from the thread context.
72612455Sgabeblack@google.com    // XXX: Inspecting how setMiscReg and setMiscRegNoEffect behave for
72712455Sgabeblack@google.com    // MISCREG_ASI causes me to think that the ASI register implementation
72812455Sgabeblack@google.com    // might be bugged. The NoEffect variant changes the ASI register
72912455Sgabeblack@google.com    // value in the architectural state while the normal variant changes
73012455Sgabeblack@google.com    // the context field in the thread context's currently decoded request
73112455Sgabeblack@google.com    // but does not directly affect the ASI register value in the
73212455Sgabeblack@google.com    // architectural state. The ASI values and the context field in the
73312455Sgabeblack@google.com    // request packet seem to have completely different uses.
73413583Sgabeblack@google.com    RegVal reg_asi = tc->readMiscRegNoEffect(MISCREG_ASI);
73512455Sgabeblack@google.com    ASI asi = static_cast<ASI>(reg_asi);
73611850Sbrandon.potter@amd.com
73712455Sgabeblack@google.com    // The SPARC DTLB code assumes that traps are executed in context
73812455Sgabeblack@google.com    // zero if the asi value is ASI_IMPLICIT (which is 0x0). There's also
73912455Sgabeblack@google.com    // an assumption that the nucleus address space is being used, but
74012455Sgabeblack@google.com    // the context is the relevant issue since we need to pass it to TLB.
74112455Sgabeblack@google.com    bool trapped = bits(tlbdata, 18, 16) > 0;
74211850Sbrandon.potter@amd.com
74312455Sgabeblack@google.com    // Given the assumptions in the translateData code in the SPARC DTLB,
74412455Sgabeblack@google.com    // the logic works out to the following for the context.
74512455Sgabeblack@google.com    int context_id = ((!hpriv && !red && is_real_address) ||
74612455Sgabeblack@google.com                      asiIsReal(asi) ||
74712455Sgabeblack@google.com                      (trapped && asi == ASI_IMPLICIT))
74812455Sgabeblack@google.com                     ? 0 : primary_context;
74911850Sbrandon.potter@amd.com
75012455Sgabeblack@google.com    // The partition id distinguishes between virtualized environments.
75112455Sgabeblack@google.com    int const partition_id = 0;
75212455Sgabeblack@google.com
75312461Sgabeblack@google.com    TlbEntry entry(p->pTable->pid(), alignedvaddr, pte->paddr,
75412461Sgabeblack@google.com                   pte->flags & EmulationPageTable::Uncacheable,
75512461Sgabeblack@google.com                   pte->flags & EmulationPageTable::ReadOnly);
75612461Sgabeblack@google.com
75712455Sgabeblack@google.com    // Insert the TLB entry.
75812455Sgabeblack@google.com    // The entry specifying whether the address is "real" is set to
75912455Sgabeblack@google.com    // false for syscall emulation mode regardless of whether the
76012455Sgabeblack@google.com    // address is real in preceding code. Not sure sure that this is
76112455Sgabeblack@google.com    // correct, but also not sure if it matters at all.
76212455Sgabeblack@google.com    dynamic_cast<TLB *>(tc->getDTBPtr())->
76312461Sgabeblack@google.com        insert(alignedvaddr, partition_id, context_id, false, entry.pte);
7644997Sgblack@eecs.umich.edu}
7654997Sgblack@eecs.umich.edu
7667741Sgblack@eecs.umich.eduvoid
76710417Sandreas.hansson@arm.comSpillNNormal::invoke(ThreadContext *tc, const StaticInstPtr &inst)
7683415Sgblack@eecs.umich.edu{
7698778Sgblack@eecs.umich.edu    if (FullSystem) {
7708778Sgblack@eecs.umich.edu        SparcFaultBase::invoke(tc, inst);
7718806Sgblack@eecs.umich.edu        return;
7728806Sgblack@eecs.umich.edu    }
7733415Sgblack@eecs.umich.edu
7748806Sgblack@eecs.umich.edu    doNormalFault(tc, trapType(), false);
7753415Sgblack@eecs.umich.edu
7768806Sgblack@eecs.umich.edu    Process *p = tc->getProcessPtr();
7773415Sgblack@eecs.umich.edu
77811851Sbrandon.potter@amd.com    SparcProcess *sp = dynamic_cast<SparcProcess *>(p);
77911851Sbrandon.potter@amd.com    assert(sp);
7808806Sgblack@eecs.umich.edu
7818806Sgblack@eecs.umich.edu    // Then adjust the PC and NPC
78211851Sbrandon.potter@amd.com    tc->pcState(sp->readSpillStart());
7833415Sgblack@eecs.umich.edu}
7843415Sgblack@eecs.umich.edu
7857741Sgblack@eecs.umich.eduvoid
78610417Sandreas.hansson@arm.comFillNNormal::invoke(ThreadContext *tc, const StaticInstPtr &inst)
7873415Sgblack@eecs.umich.edu{
7888778Sgblack@eecs.umich.edu    if (FullSystem) {
7898778Sgblack@eecs.umich.edu        SparcFaultBase::invoke(tc, inst);
7908806Sgblack@eecs.umich.edu        return;
7918806Sgblack@eecs.umich.edu    }
7923415Sgblack@eecs.umich.edu
7938806Sgblack@eecs.umich.edu    doNormalFault(tc, trapType(), false);
7943415Sgblack@eecs.umich.edu
7958806Sgblack@eecs.umich.edu    Process *p = tc->getProcessPtr();
7963415Sgblack@eecs.umich.edu
79711851Sbrandon.potter@amd.com    SparcProcess *sp = dynamic_cast<SparcProcess *>(p);
79811851Sbrandon.potter@amd.com    assert(sp);
7998806Sgblack@eecs.umich.edu
8008806Sgblack@eecs.umich.edu    // Then adjust the PC and NPC
80111851Sbrandon.potter@amd.com    tc->pcState(sp->readFillStart());
8023415Sgblack@eecs.umich.edu}
8033415Sgblack@eecs.umich.edu
8047741Sgblack@eecs.umich.eduvoid
80510417Sandreas.hansson@arm.comTrapInstruction::invoke(ThreadContext *tc, const StaticInstPtr &inst)
8064111Sgblack@eecs.umich.edu{
8078778Sgblack@eecs.umich.edu    if (FullSystem) {
8088778Sgblack@eecs.umich.edu        SparcFaultBase::invoke(tc, inst);
8098806Sgblack@eecs.umich.edu        return;
8108806Sgblack@eecs.umich.edu    }
8114111Sgblack@eecs.umich.edu
8128806Sgblack@eecs.umich.edu    // In SE, this mechanism is how the process requests a service from
8138806Sgblack@eecs.umich.edu    // the operating system. We'll get the process object from the thread
8148806Sgblack@eecs.umich.edu    // context and let it service the request.
8154111Sgblack@eecs.umich.edu
8168806Sgblack@eecs.umich.edu    Process *p = tc->getProcessPtr();
8174111Sgblack@eecs.umich.edu
81811851Sbrandon.potter@amd.com    SparcProcess *sp = dynamic_cast<SparcProcess *>(p);
81911851Sbrandon.potter@amd.com    assert(sp);
8204111Sgblack@eecs.umich.edu
82111877Sbrandon.potter@amd.com    Fault fault;
82211877Sbrandon.potter@amd.com    sp->handleTrap(_n, tc, &fault);
8238806Sgblack@eecs.umich.edu
8248806Sgblack@eecs.umich.edu    // We need to explicitly advance the pc, since that's not done for us
8258806Sgblack@eecs.umich.edu    // on a faulting instruction
8268806Sgblack@eecs.umich.edu    PCState pc = tc->pcState();
8278806Sgblack@eecs.umich.edu    pc.advance();
8288806Sgblack@eecs.umich.edu    tc->pcState(pc);
8294111Sgblack@eecs.umich.edu}
8304111Sgblack@eecs.umich.edu
8312223SN/A} // namespace SparcISA
8322221SN/A
833