1/*
2 * Copyright 2015 LabWare
3 * Copyright 2014 Google, Inc.
4 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Authors: Nathan Binkert
31 *          Boris Shingarov
32 */
33
34/*
35 * Copyright (c) 1990, 1993 The Regents of the University of California
36 * All rights reserved
37 *
38 * This software was developed by the Computer Systems Engineering group
39 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
40 * contributed to Berkeley.
41 *
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 *      This product includes software developed by the University of
45 *      California, Lawrence Berkeley Laboratories.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 *    notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 *    notice, this list of conditions and the following disclaimer in the
54 *    documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 *    must display the following acknowledgement:
57 *      This product includes software developed by the University of
58 *      California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 *    may be used to endorse or promote products derived from this software
61 *    without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
76 */
77
78/*-
79 * Copyright (c) 2001 The NetBSD Foundation, Inc.
80 * All rights reserved.
81 *
82 * This code is derived from software contributed to The NetBSD Foundation
83 * by Jason R. Thorpe.
84 *
85 * Redistribution and use in source and binary forms, with or without
86 * modification, are permitted provided that the following conditions
87 * are met:
88 * 1. Redistributions of source code must retain the above copyright
89 *    notice, this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright
91 *    notice, this list of conditions and the following disclaimer in the
92 *    documentation and/or other materials provided with the distribution.
93 * 3. All advertising materials mentioning features or use of this software
94 *    must display the following acknowledgement:
95 *      This product includes software developed by the NetBSD
96 *      Foundation, Inc. and its contributors.
97 * 4. Neither the name of The NetBSD Foundation nor the names of its
98 *    contributors may be used to endorse or promote products derived
99 *    from this software without specific prior written permission.
100 *
101 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
102 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
103 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
104 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
105 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
106 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
107 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
108 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
109 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
110 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
111 * POSSIBILITY OF SUCH DAMAGE.
112 */
113
114/*
115 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
116 *
117 * Taken from NetBSD
118 *
119 * "Stub" to allow remote cpu to debug over a serial line using gdb.
120 */
121
122#include "arch/sparc/remote_gdb.hh"
123
124#include <sys/signal.h>
125#include <unistd.h>
126
127#include <csignal>
128#include <string>
129
130#include "arch/vtophys.hh"
131#include "base/intmath.hh"
132#include "base/remote_gdb.hh"
133#include "base/socket.hh"
134#include "base/trace.hh"
135#include "cpu/static_inst.hh"
136#include "cpu/thread_context.hh"
137#include "debug/GDBAcc.hh"
138#include "debug/GDBRead.hh"
139#include "mem/page_table.hh"
140#include "mem/physical.hh"
141#include "mem/port.hh"
142#include "sim/byteswap.hh"
143#include "sim/full_system.hh"
144#include "sim/process.hh"
145#include "sim/system.hh"
146
147using namespace std;
148using namespace SparcISA;
149
150RemoteGDB::RemoteGDB(System *_system, ThreadContext *c, int _port)
151    : BaseRemoteGDB(_system, c, _port), regCache32(this), regCache64(this)
152{}
153
154///////////////////////////////////////////////////////////
155// RemoteGDB::acc
156//
157//      Determine if the mapping at va..(va+len) is valid.
158//
159bool
160RemoteGDB::acc(Addr va, size_t len)
161{
162    //@Todo In NetBSD, this function checks if all addresses
163    // from va to va + len have valid page map entries. Not
164    // sure how this will work for other OSes or in general.
165    if (FullSystem) {
166        return va != 0;
167    } else {
168        // Check to make sure the first byte is mapped into the processes
169        // address space.
170        return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
171    }
172}
173
174void
175RemoteGDB::SPARCGdbRegCache::getRegs(ThreadContext *context)
176{
177    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
178    for (int i = 0; i < 32; i++) r.gpr[i] = htobe((uint32_t)context->readIntReg(i));
179    PCState pc = context->pcState();
180    r.pc = htobe((uint32_t)pc.pc());
181    r.npc = htobe((uint32_t)pc.npc());
182    r.y = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 1));
183    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
184    r.psr = htobe((uint32_t)pstate);
185    r.fsr = htobe((uint32_t)context->readMiscReg(MISCREG_FSR));
186    r.csr = htobe((uint32_t)context->readIntReg(NumIntArchRegs + 2));
187}
188
189void
190RemoteGDB::SPARC64GdbRegCache::getRegs(ThreadContext *context)
191{
192    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
193    for (int i = 0; i < 32; i++) r.gpr[i] = htobe(context->readIntReg(i));
194    for (int i = 0; i < 32; i++) r.fpr[i] = 0;
195    PCState pc = context->pcState();
196    r.pc = htobe(pc.pc());
197    r.npc = htobe(pc.npc());
198    r.fsr = htobe(context->readMiscReg(MISCREG_FSR));
199    r.fprs = htobe(context->readMiscReg(MISCREG_FPRS));
200    r.y = htobe(context->readIntReg(NumIntArchRegs + 1));
201    PSTATE pstate = context->readMiscReg(MISCREG_PSTATE);
202    r.state = htobe(
203        context->readMiscReg(MISCREG_CWP) |
204        pstate << 8 |
205        context->readMiscReg(MISCREG_ASI) << 24 |
206        context->readIntReg(NumIntArchRegs + 2) << 32);
207}
208
209void
210RemoteGDB::SPARCGdbRegCache::setRegs(ThreadContext *context) const
211{
212    for (int i = 0; i < 32; i++) context->setIntReg(i, r.gpr[i]);
213    PCState pc;
214    pc.pc(r.pc);
215    pc.npc(r.npc);
216    pc.nnpc(pc.npc() + sizeof(MachInst));
217    pc.upc(0);
218    pc.nupc(1);
219    context->pcState(pc);
220    // Floating point registers are left at 0 in netbsd
221    // All registers other than the pc, npc and int regs
222    // are ignored as well.
223}
224
225void
226RemoteGDB::SPARC64GdbRegCache::setRegs(ThreadContext *context) const
227{
228    for (int i = 0; i < 32; i++) context->setIntReg(i, r.gpr[i]);
229    PCState pc;
230    pc.pc(r.pc);
231    pc.npc(r.npc);
232    pc.nnpc(pc.npc() + sizeof(MachInst));
233    pc.upc(0);
234    pc.nupc(1);
235    context->pcState(pc);
236    // Floating point registers are left at 0 in netbsd
237    // All registers other than the pc, npc and int regs
238    // are ignored as well.
239}
240
241
242BaseGdbRegCache*
243RemoteGDB::gdbRegs()
244{
245    PSTATE pstate = context()->readMiscReg(MISCREG_PSTATE);
246    if (pstate.am) {
247        return &regCache32;
248    } else {
249        return &regCache64;
250    }
251}
252