gem5 v24.1.0.1
Loading...
Searching...
No Matches
remote_gdb.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Huawei International
3 * Copyright 2015 LabWare
4 * Copyright 2014 Google, Inc.
5 * Copyright (c) 2010 ARM Limited
6 * Copyright (c) 2020 Barkhausen Institut
7 * All rights reserved
8 *
9 * The license below extends only to copyright in the software and shall
10 * not be construed as granting a license to any other intellectual
11 * property including but not limited to intellectual property relating
12 * to a hardware implementation of the functionality of the software
13 * licensed hereunder. You may use the software subject to the license
14 * terms below provided that you ensure that this notice is replicated
15 * unmodified and in its entirety in all distributions of the software,
16 * modified or unmodified, in source code or in binary form.
17 *
18 * Copyright (c) 2017 The University of Virginia
19 * Copyright (c) 2002-2005 The Regents of The University of Michigan
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions are
24 * met: redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer;
26 * redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution;
29 * neither the name of the copyright holders nor the names of its
30 * contributors may be used to endorse or promote products derived from
31 * this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46/*
47 * Copyright (c) 1990, 1993 The Regents of the University of California
48 * All rights reserved
49 *
50 * This software was developed by the Computer Systems Engineering group
51 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
52 * contributed to Berkeley.
53 *
54 * All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Lawrence Berkeley Laboratories.
58 *
59 * Redistribution and use in source and binary forms, with or without
60 * modification, are permitted provided that the following conditions
61 * are met:
62 * 1. Redistributions of source code must retain the above copyright
63 * notice, this list of conditions and the following disclaimer.
64 * 2. Redistributions in binary form must reproduce the above copyright
65 * notice, this list of conditions and the following disclaimer in the
66 * documentation and/or other materials provided with the distribution.
67 * 3. All advertising materials mentioning features or use of this software
68 * must display the following acknowledgement:
69 * This product includes software developed by the University of
70 * California, Berkeley and its contributors.
71 * 4. Neither the name of the University nor the names of its contributors
72 * may be used to endorse or promote products derived from this software
73 * without specific prior written permission.
74 *
75 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 * SUCH DAMAGE.
86 *
87 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
88 */
89
90/*-
91 * Copyright (c) 2001 The NetBSD Foundation, Inc.
92 * All rights reserved.
93 *
94 * This code is derived from software contributed to The NetBSD Foundation
95 * by Jason R. Thorpe.
96 *
97 * Redistribution and use in source and binary forms, with or without
98 * modification, are permitted provided that the following conditions
99 * are met:
100 * 1. Redistributions of source code must retain the above copyright
101 * notice, this list of conditions and the following disclaimer.
102 * 2. Redistributions in binary form must reproduce the above copyright
103 * notice, this list of conditions and the following disclaimer in the
104 * documentation and/or other materials provided with the distribution.
105 * 3. All advertising materials mentioning features or use of this software
106 * must display the following acknowledgement:
107 * This product includes software developed by the NetBSD
108 * Foundation, Inc. and its contributors.
109 * 4. Neither the name of The NetBSD Foundation nor the names of its
110 * contributors may be used to endorse or promote products derived
111 * from this software without specific prior written permission.
112 *
113 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
114 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
115 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
116 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
117 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
118 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
119 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
120 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
121 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
122 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
123 * POSSIBILITY OF SUCH DAMAGE.
124 */
125
126/*
127 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
128 *
129 * Taken from NetBSD
130 *
131 * "Stub" to allow remote cpu to debug over a serial line using gdb.
132 */
133
135
136#include <string>
137
138#include "arch/riscv/gdb-xml/gdb_xml_riscv_32bit_cpu.hh"
139#include "arch/riscv/gdb-xml/gdb_xml_riscv_32bit_csr.hh"
140#include "arch/riscv/gdb-xml/gdb_xml_riscv_32bit_fpu.hh"
141#include "arch/riscv/gdb-xml/gdb_xml_riscv_32bit_target.hh"
142#include "arch/riscv/gdb-xml/gdb_xml_riscv_64bit_cpu.hh"
143#include "arch/riscv/gdb-xml/gdb_xml_riscv_64bit_csr.hh"
144#include "arch/riscv/gdb-xml/gdb_xml_riscv_64bit_fpu.hh"
145#include "arch/riscv/gdb-xml/gdb_xml_riscv_64bit_target.hh"
146#include "arch/riscv/mmu.hh"
149#include "arch/riscv/regs/int.hh"
151#include "arch/riscv/tlb.hh"
152#include "cpu/thread_state.hh"
153#include "debug/GDBAcc.hh"
154#include "mem/page_table.hh"
155#include "sim/full_system.hh"
156
157namespace gem5
158{
159
160using namespace RiscvISA;
161
162template <typename xint>
163static void
166 CSRIndex idx, xint val)
167{
168 RegVal oldVal, newVal;
169 RegVal mask = CSRMasks[type][pms].at(idx);
170 oldVal = context->readMiscRegNoEffect(CSRData.at(idx).physIndex);
171 newVal = (oldVal & ~mask) | (val & mask);
172 context->setMiscRegNoEffect(CSRData.at(idx).physIndex, newVal);
173}
174
175template <typename xint>
176static void
179 CSRIndex idx, xint val)
180{
181 RegVal oldVal, newVal;
182 RegVal mask = CSRMasks[type][pms].at(idx);
183 oldVal = context->readMiscReg(CSRData.at(idx).physIndex);
184 newVal = (oldVal & ~mask) | (val & mask);
185 context->setMiscReg(CSRData.at(idx).physIndex, newVal);
186}
187
188RemoteGDB::RemoteGDB(System *_system, ListenSocketConfig _listen_config)
189 : BaseRemoteGDB(_system, _listen_config),
190 regCache32(this), regCache64(this)
191{
192}
193
195RemoteGDB::getRvType(ThreadContext* tc)
196{
197 auto isa = dynamic_cast<ISA*>(tc->getIsaPtr());
198 panic_if(!isa, "Cannot derive rv_type from non-riscv isa");
199 return isa->rvType();
200}
201
203RemoteGDB::getPrivilegeModeSet(ThreadContext* tc)
204{
205 auto isa = dynamic_cast<ISA*>(tc->getIsaPtr());
206 panic_if(!isa, "Cannot derive rv_type from non-riscv isa");
207 return isa->getPrivilegeModeSet();
208}
209
210bool
211RemoteGDB::acc(Addr va, size_t len)
212{
213 if (FullSystem)
214 {
215 MMU *mmu = static_cast<MMU *>(context()->getMMUPtr());
216 unsigned logBytes;
217 Addr paddr = va;
218
219 PrivilegeMode pmode = mmu->getMemPriv(context(), BaseMMU::Read);
220 SATP satp = context()->readMiscReg(MISCREG_SATP);
221 MISA misa = tc->readMiscRegNoEffect(MISCREG_ISA);
222 if (misa.rvs && pmode != PrivilegeMode::PRV_M &&
223 satp.mode != AddrXlateMode::BARE) {
224 Walker *walker = mmu->getDataWalker();
225 Fault fault = walker->startFunctional(
226 context(), paddr, logBytes, BaseMMU::Read);
227 if (fault != NoFault)
228 return false;
229 }
230 return true;
231 }
232
233 return context()->getProcessPtr()->pTable->lookup(va) != nullptr;
234}
235
236void
237RemoteGDB::insertHardBreak(Addr addr, size_t kind)
238{
239 Addr realAddr = getRvType(context()) == RV64 ? addr : sext(addr, 32);
240 BaseRemoteGDB::insertHardBreak(realAddr, kind);
241}
242
243void
244RemoteGDB::removeHardBreak(Addr addr, size_t kind)
245{
246 Addr realAddr = getRvType(context()) == RV64 ? addr : sext(addr, 32);
247 BaseRemoteGDB::removeHardBreak(realAddr, kind);
248}
249
250void
251RemoteGDB::Riscv32GdbRegCache::getRegs(ThreadContext *context)
252{
253 DPRINTF(GDBAcc, "getregs in remotegdb, size %lu\n", size());
254 RemoteGDB* rv_gdb = dynamic_cast<RemoteGDB*>(gdb);
255 PrivilegeModeSet pms = enums::MSU;
256 if (rv_gdb != nullptr) {
257 pms = rv_gdb->getPrivilegeModeSet(context);
258 }
259 auto& RVxCSRMasks = CSRMasks[RV32][pms];
260
261 // General registers
262 for (int i = 0; i < int_reg::NumArchRegs; i++) {
263 r.gpr[i] = context->getReg(intRegClass[i]);
264 }
265 r.pc = context->pcState().instAddr();
266
267 // Floating point registers
268 for (int i = 0; i < float_reg::NumRegs; i++)
269 r.fpu[i] = context->getReg(floatRegClass[i]);
270 r.fflags = context->readMiscRegNoEffect(
271 CSRData.at(CSR_FFLAGS).physIndex) & RVxCSRMasks.at(CSR_FFLAGS);
272 r.frm = context->readMiscRegNoEffect(
273 CSRData.at(CSR_FRM).physIndex) & RVxCSRMasks.at(CSR_FRM);
274 r.fcsr = context->readMiscReg(
275 CSRData.at(CSR_FCSR).physIndex) & RVxCSRMasks.at(CSR_FCSR);
276
277 // CSR registers
278 r.cycle = context->readMiscRegNoEffect(
279 CSRData.at(CSR_CYCLE).physIndex);
280 r.cycleh = context->readMiscRegNoEffect(
281 CSRData.at(CSR_CYCLEH).physIndex);
282 r.time = context->readMiscRegNoEffect(
283 CSRData.at(CSR_TIME).physIndex);
284 r.timeh = context->readMiscRegNoEffect(
285 CSRData.at(CSR_TIMEH).physIndex);
286
287 // U mode CSR
288 r.ustatus = context->readMiscReg(
289 CSRData.at(CSR_USTATUS).physIndex) & RVxCSRMasks.at(CSR_USTATUS);
290 r.uie = context->readMiscReg(
291 CSRData.at(CSR_UIE).physIndex) & RVxCSRMasks.at(CSR_UIE);
292 r.utvec = context->readMiscRegNoEffect(
293 CSRData.at(CSR_UTVEC).physIndex);
294 r.uscratch = context->readMiscRegNoEffect(
295 CSRData.at(CSR_USCRATCH).physIndex);
296 r.uepc = context->readMiscRegNoEffect(
297 CSRData.at(CSR_UEPC).physIndex);
298 r.ucause = context->readMiscRegNoEffect(
299 CSRData.at(CSR_UCAUSE).physIndex);
300 r.utval = context->readMiscRegNoEffect(
301 CSRData.at(CSR_UTVAL).physIndex);
302 r.uip = context->readMiscReg(
303 CSRData.at(CSR_UIP).physIndex) & RVxCSRMasks.at(CSR_UIP);
304
305 // S mode CSR
306 r.sstatus = context->readMiscReg(
307 CSRData.at(CSR_SSTATUS).physIndex) & RVxCSRMasks.at(CSR_SSTATUS);
308 r.sedeleg = context->readMiscRegNoEffect(
309 CSRData.at(CSR_SEDELEG).physIndex);
310 r.sideleg = context->readMiscRegNoEffect(
311 CSRData.at(CSR_SIDELEG).physIndex);
312 r.sie = context->readMiscReg(
313 CSRData.at(CSR_SIE).physIndex) & RVxCSRMasks.at(CSR_SIE);
314 r.stvec = context->readMiscRegNoEffect(
315 CSRData.at(CSR_STVEC).physIndex);
316 r.scounteren = context->readMiscRegNoEffect(
317 CSRData.at(CSR_SCOUNTEREN).physIndex);
318 r.sscratch = context->readMiscRegNoEffect(
319 CSRData.at(CSR_SSCRATCH).physIndex);
320 r.sepc = context->readMiscReg(
321 CSRData.at(CSR_SEPC).physIndex);
322 r.scause = context->readMiscRegNoEffect(
323 CSRData.at(CSR_SCAUSE).physIndex);
324 r.stval = context->readMiscRegNoEffect(
325 CSRData.at(CSR_STVAL).physIndex);
326 r.sip = context->readMiscReg(
327 CSRData.at(CSR_SIP).physIndex) & RVxCSRMasks.at(CSR_SIP);
328 r.satp = context->readMiscRegNoEffect(
329 CSRData.at(CSR_SATP).physIndex);
330 r.senvcfg = context->readMiscRegNoEffect(
331 CSRData.at(CSR_SENVCFG).physIndex);
332
333 // M mode CSR
334 r.mvendorid = context->readMiscRegNoEffect(
335 CSRData.at(CSR_MVENDORID).physIndex);
336 r.marchid = context->readMiscRegNoEffect(
337 CSRData.at(CSR_MARCHID).physIndex);
338 r.mimpid = context->readMiscRegNoEffect(
339 CSRData.at(CSR_MIMPID).physIndex);
340 r.mhartid = context->contextId();
341 r.mstatus = context->readMiscReg(
342 CSRData.at(CSR_MSTATUS).physIndex) & RVxCSRMasks.at(CSR_MSTATUS);
343 r.misa = context->readMiscRegNoEffect(
344 CSRData.at(CSR_MISA).physIndex) & RVxCSRMasks.at(CSR_MISA);
345 r.medeleg = context->readMiscRegNoEffect(
346 CSRData.at(CSR_MEDELEG).physIndex);
347 r.mideleg = context->readMiscRegNoEffect(
348 CSRData.at(CSR_MIDELEG).physIndex);
349 r.mie = context->readMiscReg(
350 CSRData.at(CSR_MIE).physIndex) & RVxCSRMasks.at(CSR_MIE);
351 r.mtvec = context->readMiscRegNoEffect(
352 CSRData.at(CSR_MTVEC).physIndex);
353 r.mcounteren = context->readMiscRegNoEffect(
354 CSRData.at(CSR_MCOUNTEREN).physIndex);
355 r.mstatush = context->readMiscReg(
356 CSRData.at(CSR_MSTATUSH).physIndex) & RVxCSRMasks.at(CSR_MSTATUSH);
357 r.mscratch = context->readMiscRegNoEffect(
358 CSRData.at(CSR_MSCRATCH).physIndex);
359 r.mepc = context->readMiscReg(
360 CSRData.at(CSR_MEPC).physIndex);
361 r.mcause = context->readMiscRegNoEffect(
362 CSRData.at(CSR_MCAUSE).physIndex);
363 r.mtval = context->readMiscRegNoEffect(
364 CSRData.at(CSR_MTVAL).physIndex);
365 r.mip = context->readMiscReg(
366 CSRData.at(CSR_MIP).physIndex) & RVxCSRMasks.at(CSR_MIP);
367
368 // H mode CSR (to be implemented)
369}
370
371void
372RemoteGDB::Riscv32GdbRegCache::setRegs(ThreadContext *context) const
373{
374 DPRINTF(GDBAcc, "setregs in remotegdb \n");
375 RemoteGDB* rv_gdb = dynamic_cast<RemoteGDB*>(gdb);
376 PrivilegeModeSet pms = enums::MSU;
377 if (rv_gdb != nullptr) {
378 pms = rv_gdb->getPrivilegeModeSet(context);
379 }
380 for (int i = 0; i < int_reg::NumArchRegs; i++)
381 context->setReg(intRegClass[i], r.gpr[i]);
382 context->pcState(r.pc);
383
384 // Floating point registers
385 for (int i = 0; i < float_reg::NumRegs; i++)
386 context->setReg(floatRegClass[i], r.fpu[i]);
387
388 setRegNoEffectWithMask(context, RV32, pms, CSR_FFLAGS, r.fflags);
389 setRegNoEffectWithMask(context, RV32, pms, CSR_FRM, r.frm);
390 setRegWithMask(context, RV32, pms, CSR_FCSR, r.fcsr);
391
392 // TODO: implement CSR counter registers for mcycle(h), minstret(h)
393
394 // U mode CSR
395 setRegWithMask(context, RV32, pms, CSR_USTATUS, r.ustatus);
396 setRegWithMask(context, RV32, pms, CSR_UIE, r.uie);
397 setRegWithMask(context, RV32, pms, CSR_UIP, r.uip);
398 context->setMiscRegNoEffect(
399 CSRData.at(CSR_UTVEC).physIndex, r.utvec);
400 context->setMiscRegNoEffect(
401 CSRData.at(CSR_USCRATCH).physIndex, r.uscratch);
402 context->setMiscRegNoEffect(
403 CSRData.at(CSR_UEPC).physIndex, r.uepc);
404 context->setMiscRegNoEffect(
405 CSRData.at(CSR_UCAUSE).physIndex, r.ucause);
406 context->setMiscRegNoEffect(
407 CSRData.at(CSR_UTVAL).physIndex, r.utval);
408
409 // S mode CSR
410 setRegWithMask(context, RV32, pms, CSR_SSTATUS, r.sstatus);
411 setRegWithMask(context, RV32, pms, CSR_SIE, r.sie);
412 setRegWithMask(context, RV32, pms, CSR_SIP, r.sip);
413 context->setMiscRegNoEffect(
414 CSRData.at(CSR_SEDELEG).physIndex, r.sedeleg);
415 context->setMiscRegNoEffect(
416 CSRData.at(CSR_SIDELEG).physIndex, r.sideleg);
417 context->setMiscRegNoEffect(
418 CSRData.at(CSR_STVEC).physIndex, r.stvec);
419 context->setMiscRegNoEffect(
420 CSRData.at(CSR_SCOUNTEREN).physIndex, r.scounteren);
421 context->setMiscRegNoEffect(
422 CSRData.at(CSR_SSCRATCH).physIndex, r.sscratch);
423 context->setMiscRegNoEffect(
424 CSRData.at(CSR_SEPC).physIndex, r.sepc);
425 context->setMiscRegNoEffect(
426 CSRData.at(CSR_SCAUSE).physIndex, r.scause);
427 context->setMiscRegNoEffect(
428 CSRData.at(CSR_STVAL).physIndex, r.stval);
429 context->setMiscRegNoEffect(
430 CSRData.at(CSR_SATP).physIndex, r.satp);
431 context->setMiscRegNoEffect(
432 CSRData.at(CSR_SENVCFG).physIndex, r.senvcfg);
433
434 // M mode CSR
435 setRegWithMask(context, RV32, pms, CSR_MSTATUS, r.mstatus);
436 setRegNoEffectWithMask(context, RV32, pms, CSR_MISA, r.misa);
437 setRegWithMask(context, RV32, pms, CSR_MIE, r.mie);
438 setRegWithMask(context, RV32, pms, CSR_MIP, r.mip);
439 context->setMiscRegNoEffect(
440 CSRData.at(CSR_MEDELEG).physIndex, r.medeleg);
441 context->setMiscRegNoEffect(
442 CSRData.at(CSR_MIDELEG).physIndex, r.mideleg);
443 context->setMiscRegNoEffect(
444 CSRData.at(CSR_MTVEC).physIndex, r.mtvec);
445 context->setMiscRegNoEffect(
446 CSRData.at(CSR_MCOUNTEREN).physIndex, r.mcounteren);
447 context->setMiscRegNoEffect(
448 CSRData.at(CSR_MSCRATCH).physIndex, r.mscratch);
449 context->setMiscRegNoEffect(
450 CSRData.at(CSR_MEPC).physIndex, r.mepc);
451 context->setMiscRegNoEffect(
452 CSRData.at(CSR_MCAUSE).physIndex, r.mcause);
453 context->setMiscRegNoEffect(
454 CSRData.at(CSR_MTVAL).physIndex, r.mtval);
455
456 // H mode CSR (to be implemented)
457}
458
459void
460RemoteGDB::Riscv64GdbRegCache::getRegs(ThreadContext *context)
461{
462 DPRINTF(GDBAcc, "getregs in remotegdb, size %lu\n", size());
463 RemoteGDB* rv_gdb = dynamic_cast<RemoteGDB*>(gdb);
464 PrivilegeModeSet pms = enums::MSU;
465 if (rv_gdb != nullptr) {
466 pms = rv_gdb->getPrivilegeModeSet(context);
467 }
468 auto& RVxCSRMasks = CSRMasks[RV64][pms];
469
470 // General registers
471 for (int i = 0; i < int_reg::NumArchRegs; i++) {
472 r.gpr[i] = context->getReg(intRegClass[i]);
473 }
474 r.pc = context->pcState().instAddr();
475
476 // Floating point registers
477 for (int i = 0; i < float_reg::NumRegs; i++)
478 r.fpu[i] = context->getReg(floatRegClass[i]);
479 r.fflags = context->readMiscRegNoEffect(
480 CSRData.at(CSR_FFLAGS).physIndex) & RVxCSRMasks.at(CSR_FFLAGS);
481 r.frm = context->readMiscRegNoEffect(
482 CSRData.at(CSR_FRM).physIndex) & RVxCSRMasks.at(CSR_FRM);
483 r.fcsr = context->readMiscReg(
484 CSRData.at(CSR_FCSR).physIndex) & RVxCSRMasks.at(CSR_FCSR);
485
486 // CSR registers
487 r.cycle = context->readMiscRegNoEffect(
488 CSRData.at(CSR_CYCLE).physIndex);
489 r.time = context->readMiscRegNoEffect(
490 CSRData.at(CSR_TIME).physIndex);
491
492 // U mode CSR
493 r.ustatus = context->readMiscReg(
494 CSRData.at(CSR_USTATUS).physIndex) & RVxCSRMasks.at(CSR_USTATUS);
495 r.uie = context->readMiscReg(
496 CSRData.at(CSR_UIE).physIndex) & RVxCSRMasks.at(CSR_UIE);
497 r.utvec = context->readMiscRegNoEffect(
498 CSRData.at(CSR_UTVEC).physIndex);
499 r.uscratch = context->readMiscRegNoEffect(
500 CSRData.at(CSR_USCRATCH).physIndex);
501 r.uepc = context->readMiscRegNoEffect(
502 CSRData.at(CSR_UEPC).physIndex);
503 r.ucause = context->readMiscRegNoEffect(
504 CSRData.at(CSR_UCAUSE).physIndex);
505 r.utval = context->readMiscRegNoEffect(
506 CSRData.at(CSR_UTVAL).physIndex);
507 r.uip = context->readMiscReg(
508 CSRData.at(CSR_UIP).physIndex) & RVxCSRMasks.at(CSR_UIP);
509
510 // S mode CSR
511 r.sstatus = context->readMiscReg(
512 CSRData.at(CSR_SSTATUS).physIndex) & RVxCSRMasks.at(CSR_SSTATUS);
513 r.sedeleg = context->readMiscRegNoEffect(
514 CSRData.at(CSR_SEDELEG).physIndex);
515 r.sideleg = context->readMiscRegNoEffect(
516 CSRData.at(CSR_SIDELEG).physIndex);
517 r.sie = context->readMiscReg(
518 CSRData.at(CSR_SIE).physIndex) & RVxCSRMasks.at(CSR_SIE);
519 r.stvec = context->readMiscRegNoEffect(
520 CSRData.at(CSR_STVEC).physIndex);
521 r.scounteren = context->readMiscRegNoEffect(
522 CSRData.at(CSR_SCOUNTEREN).physIndex);
523 r.sscratch = context->readMiscRegNoEffect(
524 CSRData.at(CSR_SSCRATCH).physIndex);
525 r.sepc = context->readMiscReg(
526 CSRData.at(CSR_SEPC).physIndex);
527 r.scause = context->readMiscRegNoEffect(
528 CSRData.at(CSR_SCAUSE).physIndex);
529 r.stval = context->readMiscRegNoEffect(
530 CSRData.at(CSR_STVAL).physIndex);
531 r.sip = context->readMiscReg(
532 CSRData.at(CSR_SIP).physIndex) & RVxCSRMasks.at(CSR_SIP);
533 r.satp = context->readMiscRegNoEffect(
534 CSRData.at(CSR_SATP).physIndex);
535 r.senvcfg = context->readMiscRegNoEffect(
536 CSRData.at(CSR_SENVCFG).physIndex);
537
538 // M mode CSR
539 r.mvendorid = context->readMiscRegNoEffect(
540 CSRData.at(CSR_MVENDORID).physIndex);
541 r.marchid = context->readMiscRegNoEffect(
542 CSRData.at(CSR_MARCHID).physIndex);
543 r.mimpid = context->readMiscRegNoEffect(
544 CSRData.at(CSR_MIMPID).physIndex);
545 r.mhartid = context->contextId();
546 r.mstatus = context->readMiscReg(
547 CSRData.at(CSR_MSTATUS).physIndex) & RVxCSRMasks.at(CSR_MSTATUS);
548 r.misa = context->readMiscRegNoEffect(
549 CSRData.at(CSR_MISA).physIndex) & RVxCSRMasks.at(CSR_MISA);
550 r.medeleg = context->readMiscRegNoEffect(
551 CSRData.at(CSR_MEDELEG).physIndex);
552 r.mideleg = context->readMiscRegNoEffect(
553 CSRData.at(CSR_MIDELEG).physIndex);
554 r.mie = context->readMiscReg(
555 CSRData.at(CSR_MIE).physIndex) & RVxCSRMasks.at(CSR_MIE);
556 r.mtvec = context->readMiscRegNoEffect(
557 CSRData.at(CSR_MTVEC).physIndex);
558 r.mcounteren = context->readMiscRegNoEffect(
559 CSRData.at(CSR_MCOUNTEREN).physIndex);
560 r.mscratch = context->readMiscRegNoEffect(
561 CSRData.at(CSR_MSCRATCH).physIndex);
562 r.mepc = context->readMiscReg(
563 CSRData.at(CSR_MEPC).physIndex);
564 r.mcause = context->readMiscRegNoEffect(
565 CSRData.at(CSR_MCAUSE).physIndex);
566 r.mtval = context->readMiscRegNoEffect(
567 CSRData.at(CSR_MTVAL).physIndex);
568 r.mip = context->readMiscReg(
569 CSRData.at(CSR_MIP).physIndex) & RVxCSRMasks.at(CSR_MIP);
570
571 // H mode CSR (to be implemented)
572}
573
574void
575RemoteGDB::Riscv64GdbRegCache::setRegs(ThreadContext *context) const
576{
577 DPRINTF(GDBAcc, "setregs in remotegdb \n");
578 RemoteGDB* rv_gdb = dynamic_cast<RemoteGDB*>(gdb);
579 PrivilegeModeSet pms = enums::MSU;
580 if (rv_gdb != nullptr) {
581 pms = rv_gdb->getPrivilegeModeSet(context);
582 }
583 for (int i = 0; i < int_reg::NumArchRegs; i++)
584 context->setReg(intRegClass[i], r.gpr[i]);
585 context->pcState(r.pc);
586
587 // Floating point registers
588 for (int i = 0; i < float_reg::NumRegs; i++)
589 context->setReg(floatRegClass[i], r.fpu[i]);
590
591 setRegNoEffectWithMask(context, RV64, pms, CSR_FFLAGS, r.fflags);
592 setRegNoEffectWithMask(context, RV64, pms, CSR_FRM, r.frm);
593 setRegWithMask(context, RV64, pms, CSR_FCSR, r.fcsr);
594
595 // TODO: implement CSR counter registers for mcycle, minstret
596
597 // U mode CSR
598 setRegWithMask(context, RV64, pms, CSR_USTATUS, r.ustatus);
599 setRegWithMask(context, RV64, pms, CSR_UIE, r.uie);
600 setRegWithMask(context, RV64, pms, CSR_UIP, r.uip);
601 context->setMiscRegNoEffect(
602 CSRData.at(CSR_UTVEC).physIndex, r.utvec);
603 context->setMiscRegNoEffect(
604 CSRData.at(CSR_USCRATCH).physIndex, r.uscratch);
605 context->setMiscRegNoEffect(
606 CSRData.at(CSR_UEPC).physIndex, r.uepc);
607 context->setMiscRegNoEffect(
608 CSRData.at(CSR_UCAUSE).physIndex, r.ucause);
609 context->setMiscRegNoEffect(
610 CSRData.at(CSR_UTVAL).physIndex, r.utval);
611
612 // S mode CSR
613 setRegWithMask(context, RV64, pms, CSR_SSTATUS, r.sstatus);
614 setRegWithMask(context, RV64, pms, CSR_SIE, r.sie);
615 setRegWithMask(context, RV64, pms, CSR_SIP, r.sip);
616 context->setMiscRegNoEffect(
617 CSRData.at(CSR_SEDELEG).physIndex, r.sedeleg);
618 context->setMiscRegNoEffect(
619 CSRData.at(CSR_SIDELEG).physIndex, r.sideleg);
620 context->setMiscRegNoEffect(
621 CSRData.at(CSR_STVEC).physIndex, r.stvec);
622 context->setMiscRegNoEffect(
623 CSRData.at(CSR_SCOUNTEREN).physIndex, r.scounteren);
624 context->setMiscRegNoEffect(
625 CSRData.at(CSR_SSCRATCH).physIndex, r.sscratch);
626 context->setMiscRegNoEffect(
627 CSRData.at(CSR_SEPC).physIndex, r.sepc);
628 context->setMiscRegNoEffect(
629 CSRData.at(CSR_SCAUSE).physIndex, r.scause);
630 context->setMiscRegNoEffect(
631 CSRData.at(CSR_STVAL).physIndex, r.stval);
632 context->setMiscRegNoEffect(
633 CSRData.at(CSR_SATP).physIndex, r.satp);
634 context->setMiscRegNoEffect(
635 CSRData.at(CSR_SENVCFG).physIndex, r.senvcfg);
636 // M mode CSR
637 setRegWithMask(context, RV64, pms, CSR_MSTATUS, r.mstatus);
638 setRegNoEffectWithMask(context, RV64, pms, CSR_MISA, r.misa);
639 setRegWithMask(context, RV64, pms, CSR_MIE, r.mie);
640 setRegWithMask(context, RV64, pms, CSR_MIP, r.mip);
641 context->setMiscRegNoEffect(
642 CSRData.at(CSR_MEDELEG).physIndex, r.medeleg);
643 context->setMiscRegNoEffect(
644 CSRData.at(CSR_MIDELEG).physIndex, r.mideleg);
645 context->setMiscRegNoEffect(
646 CSRData.at(CSR_MTVEC).physIndex, r.mtvec);
647 context->setMiscRegNoEffect(
648 CSRData.at(CSR_MCOUNTEREN).physIndex, r.mcounteren);
649 context->setMiscRegNoEffect(
650 CSRData.at(CSR_MSCRATCH).physIndex, r.mscratch);
651 context->setMiscRegNoEffect(
652 CSRData.at(CSR_MEPC).physIndex, r.mepc);
653 context->setMiscRegNoEffect(
654 CSRData.at(CSR_MCAUSE).physIndex, r.mcause);
655 context->setMiscRegNoEffect(
656 CSRData.at(CSR_MTVAL).physIndex, r.mtval);
657
658 // H mode CSR (to be implemented)
659}
660
661bool
662RemoteGDB::getXferFeaturesRead(const std::string &annex, std::string &output)
663{
670#define GDB_XML(x, s) \
671 { \
672 x, std::string(reinterpret_cast<const char *>(Blobs::s), \
673 Blobs::s##_len) \
674 }
675 static const std::map<std::string, std::string> annexMaps[enums::Num_RiscvType] = {
676 [RV32] = {GDB_XML("target.xml", gdb_xml_riscv_32bit_target),
677 GDB_XML("riscv-32bit-cpu.xml", gdb_xml_riscv_32bit_cpu),
678 GDB_XML("riscv-32bit-fpu.xml", gdb_xml_riscv_32bit_fpu),
679 GDB_XML("riscv-32bit-csr.xml", gdb_xml_riscv_32bit_csr)},
680 [RV64] = {GDB_XML("target.xml", gdb_xml_riscv_64bit_target),
681 GDB_XML("riscv-64bit-cpu.xml", gdb_xml_riscv_64bit_cpu),
682 GDB_XML("riscv-64bit-fpu.xml", gdb_xml_riscv_64bit_fpu),
683 GDB_XML("riscv-64bit-csr.xml", gdb_xml_riscv_64bit_csr)},
684 };
685 auto& annexMap = annexMaps[getRvType(context())];
686 auto it = annexMap.find(annex);
687 if (it == annexMap.end())
688 return false;
689 output = it->second;
690 return true;
691}
692
693BaseGdbRegCache *
694RemoteGDB::gdbRegs()
695{
696 BaseGdbRegCache* regs[enums::Num_RiscvType] = {
697 [RV32] = &regCache32,
698 [RV64] = &regCache64,
699 };
700 return regs[getRvType(context())];
701}
702
703} // namespace gem5
#define GDB_XML(x, s)
#define DPRINTF(x,...)
Definition trace.hh:209
RemoteGDB(System *_system, ListenSocketConfig _listen_config)
Addr instAddr() const
Returns the memory address of the instruction this PC points to.
Definition pcstate.hh:108
Walker * getDataWalker()
Definition mmu.hh:95
PrivilegeMode getMemPriv(ThreadContext *tc, BaseMMU::Mode mode)
Definition mmu.hh:89
virtual PrivilegeModeSet getPrivilegeModeSet(ThreadContext *tc)
Fault startFunctional(ThreadContext *_tc, Addr &addr, unsigned &logBytes, BaseMMU::Mode mode)
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual RegVal readMiscReg(RegIndex misc_reg)=0
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
virtual RegVal getReg(const RegId &reg) const
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
virtual BaseISA * getIsaPtr() const =0
virtual void setReg(const RegId &reg, RegVal val)
virtual const PCStateBase & pcState() const =0
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
virtual ContextID contextId() const =0
constexpr uint64_t sext(uint64_t val)
Sign-extend an N-bit value to 64 bits.
Definition bitfield.hh:129
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
uint16_t len
Definition helpers.cc:83
Bitfield< 3, 0 > mask
Definition pcstate.hh:63
Bitfield< 7 > i
Definition misc_types.hh:67
constexpr RegClass intRegClass
Definition int.hh:173
Bitfield< 8 > va
constexpr enums::RiscvType RV32
Definition pcstate.hh:56
enums::RiscvType RiscvType
Definition pcstate.hh:55
const std::unordered_map< int, CSRMetadata > CSRData
Definition misc.hh:538
const std::unordered_map< int, RegVal > CSRMasks[enums::Num_RiscvType][enums::Num_PrivilegeModeSet]
Definition misc.hh:1522
constexpr enums::RiscvType RV64
Definition pcstate.hh:57
enums::PrivilegeModeSet PrivilegeModeSet
Definition pcstate.hh:59
Bitfield< 63 > val
Definition misc.hh:804
Bitfield< 3 > addr
Definition types.hh:84
constexpr RegClass floatRegClass
Definition float.hh:143
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::shared_ptr< FaultBase > Fault
Definition types.hh:249
static void setRegNoEffectWithMask(ThreadContext *context, RiscvType type, PrivilegeModeSet pms, CSRIndex idx, xint val)
static void setRegWithMask(ThreadContext *context, RiscvType type, PrivilegeModeSet pms, CSRIndex idx, xint val)
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
uint64_t RegVal
Definition types.hh:173
Declarations of a non-full system Page Table.

Generated on Mon Jan 13 2025 04:28:30 for gem5 by doxygen 1.9.8