1/*
2 * Copyright 2015 LabWare
3 * Copyright 2014 Google, Inc.
4 * Copyright (c) 2010 ARM Limited
5 * All rights reserved
6 *
7 * The license below extends only to copyright in the software and shall
8 * not be construed as granting a license to any other intellectual
9 * property including but not limited to intellectual property relating
10 * to a hardware implementation of the functionality of the software
11 * licensed hereunder.  You may use the software subject to the license
12 * terms below provided that you ensure that this notice is replicated
13 * unmodified and in its entirety in all distributions of the software,
14 * modified or unmodified, in source code or in binary form.
15 *
16 * Copyright (c) 2002-2005 The Regents of The University of Michigan
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Authors: Nathan Binkert
43 *          William Wang
44 *          Deyuan Guo
45 *          Boris Shingarov
46 */
47
48/*
49 * Copyright (c) 1990, 1993 The Regents of the University of California
50 * All rights reserved
51 *
52 * This software was developed by the Computer Systems Engineering group
53 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
54 * contributed to Berkeley.
55 *
56 * All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 *      This product includes software developed by the University of
59 *      California, Lawrence Berkeley Laboratories.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 *    notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 *    notice, this list of conditions and the following disclaimer in the
68 *    documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 *    must display the following acknowledgement:
71 *      This product includes software developed by the University of
72 *      California, Berkeley and its contributors.
73 * 4. Neither the name of the University nor the names of its contributors
74 *    may be used to endorse or promote products derived from this software
75 *    without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
90 */
91
92/*-
93 * Copyright (c) 2001 The NetBSD Foundation, Inc.
94 * All rights reserved.
95 *
96 * This code is derived from software contributed to The NetBSD Foundation
97 * by Jason R. Thorpe.
98 *
99 * Redistribution and use in source and binary forms, with or without
100 * modification, are permitted provided that the following conditions
101 * are met:
102 * 1. Redistributions of source code must retain the above copyright
103 *    notice, this list of conditions and the following disclaimer.
104 * 2. Redistributions in binary form must reproduce the above copyright
105 *    notice, this list of conditions and the following disclaimer in the
106 *    documentation and/or other materials provided with the distribution.
107 * 3. All advertising materials mentioning features or use of this software
108 *    must display the following acknowledgement:
109 *      This product includes software developed by the NetBSD
110 *      Foundation, Inc. and its contributors.
111 * 4. Neither the name of The NetBSD Foundation nor the names of its
112 *    contributors may be used to endorse or promote products derived
113 *    from this software without specific prior written permission.
114 *
115 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
116 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
117 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
118 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
119 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
120 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
121 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
122 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
123 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
124 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
125 * POSSIBILITY OF SUCH DAMAGE.
126 */
127
128/*
129 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
130 *
131 * Taken from NetBSD
132 *
133 * "Stub" to allow remote cpu to debug over a serial line using gdb.
134 */
135
136
137#include "arch/power/remote_gdb.hh"
138
139#include <sys/signal.h>
140#include <unistd.h>
141
142#include <string>
143
144#include "arch/power/vtophys.hh"
145#include "cpu/thread_state.hh"
146#include "debug/GDBAcc.hh"
147#include "debug/GDBMisc.hh"
148#include "mem/page_table.hh"
149#include "sim/byteswap.hh"
150
151using namespace std;
152using namespace PowerISA;
153
154RemoteGDB::RemoteGDB(System *_system, ThreadContext *tc, int _port)
155    : BaseRemoteGDB(_system, tc, _port), regCache(this)
156{
157}
158
159/*
160 * Determine if the mapping at va..(va+len) is valid.
161 */
162bool
163RemoteGDB::acc(Addr va, size_t len)
164{
165    // Check to make sure the first byte is mapped into the processes address
166    // space.  At the time of this writing, the acc() check is used when
167    // processing the MemR/MemW packets before actually asking the translating
168    // port proxy to read/writeBlob.  I (bgs) am not convinced the first byte
169    // check is enough.
170    panic_if(FullSystem, "acc not implemented for POWER FS!");
171    return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
172}
173
174void
175RemoteGDB::PowerGdbRegCache::getRegs(ThreadContext *context)
176{
177    DPRINTF(GDBAcc, "getRegs in remotegdb \n");
178
179    // Default order on 32-bit PowerPC:
180    // R0-R31 (32-bit each), F0-F31 (64-bit IEEE754 double),
181    // PC, MSR, CR, LR, CTR, XER (32-bit each)
182
183    for (int i = 0; i < NumIntArchRegs; i++)
184        r.gpr[i] = htobe((uint32_t)context->readIntReg(i));
185
186    for (int i = 0; i < NumFloatArchRegs; i++)
187        r.fpr[i] = context->readFloatReg(i);
188
189    r.pc = htobe((uint32_t)context->pcState().pc());
190    r.msr = 0; // Is MSR modeled?
191    r.cr = htobe((uint32_t)context->readIntReg(INTREG_CR));
192    r.lr = htobe((uint32_t)context->readIntReg(INTREG_LR));
193    r.ctr = htobe((uint32_t)context->readIntReg(INTREG_CTR));
194    r.xer = htobe((uint32_t)context->readIntReg(INTREG_XER));
195}
196
197void
198RemoteGDB::PowerGdbRegCache::setRegs(ThreadContext *context) const
199{
200    DPRINTF(GDBAcc, "setRegs in remotegdb \n");
201
202    for (int i = 0; i < NumIntArchRegs; i++)
203        context->setIntReg(i, betoh(r.gpr[i]));
204
205    for (int i = 0; i < NumFloatArchRegs; i++)
206        context->setFloatReg(i, r.fpr[i]);
207
208    context->pcState(betoh(r.pc));
209    // Is MSR modeled?
210    context->setIntReg(INTREG_CR, betoh(r.cr));
211    context->setIntReg(INTREG_LR, betoh(r.lr));
212    context->setIntReg(INTREG_CTR, betoh(r.ctr));
213    context->setIntReg(INTREG_XER, betoh(r.xer));
214}
215
216BaseGdbRegCache*
217RemoteGDB::gdbRegs()
218{
219    return &regCache;
220}
221
222