xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/i386-netbsd-nat.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Native-dependent code for NetBSD/i386.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2004-2023 Free Software Foundation, Inc.
4*6881a400Schristos 
5*6881a400Schristos    This file is part of GDB.
6*6881a400Schristos 
7*6881a400Schristos    This program is free software; you can redistribute it and/or modify
8*6881a400Schristos    it under the terms of the GNU General Public License as published by
9*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
10*6881a400Schristos    (at your option) any later version.
11*6881a400Schristos 
12*6881a400Schristos    This program is distributed in the hope that it will be useful,
13*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*6881a400Schristos    GNU General Public License for more details.
16*6881a400Schristos 
17*6881a400Schristos    You should have received a copy of the GNU General Public License
18*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*6881a400Schristos 
20*6881a400Schristos #include "defs.h"
21*6881a400Schristos #include "gdbcore.h"
22*6881a400Schristos #include "regcache.h"
23*6881a400Schristos #include "target.h"
24*6881a400Schristos 
25*6881a400Schristos #include "i386-tdep.h"
26*6881a400Schristos #include "i386-bsd-nat.h"
27*6881a400Schristos 
28*6881a400Schristos /* Support for debugging kernel virtual memory images.  */
29*6881a400Schristos 
30*6881a400Schristos #include <sys/types.h>
31*6881a400Schristos #include <machine/frame.h>
32*6881a400Schristos #include <machine/pcb.h>
33*6881a400Schristos 
34*6881a400Schristos #include "netbsd-nat.h"
35*6881a400Schristos #include "bsd-kvm.h"
36*6881a400Schristos 
37*6881a400Schristos static int
38*6881a400Schristos i386nbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
39*6881a400Schristos {
40*6881a400Schristos   struct switchframe sf;
41*6881a400Schristos 
42*6881a400Schristos   /* The following is true for NetBSD 1.6.2:
43*6881a400Schristos 
44*6881a400Schristos      The pcb contains %esp and %ebp at the point of the context switch
45*6881a400Schristos      in cpu_switch().  At that point we have a stack frame as
46*6881a400Schristos      described by `struct switchframe', which for NetBSD 1.6.2 has the
47*6881a400Schristos      following layout:
48*6881a400Schristos 
49*6881a400Schristos      interrupt level
50*6881a400Schristos      %edi
51*6881a400Schristos      %esi
52*6881a400Schristos      %ebx
53*6881a400Schristos      %eip
54*6881a400Schristos 
55*6881a400Schristos      we reconstruct the register state as it would look when we just
56*6881a400Schristos      returned from cpu_switch().  */
57*6881a400Schristos 
58*6881a400Schristos   /* The stack pointer shouldn't be zero.  */
59*6881a400Schristos   if (pcb->pcb_esp == 0)
60*6881a400Schristos     return 0;
61*6881a400Schristos 
62*6881a400Schristos   read_memory (pcb->pcb_esp, (gdb_byte *)&sf, sizeof sf);
63*6881a400Schristos   pcb->pcb_esp += sizeof (struct switchframe);
64*6881a400Schristos   regcache->raw_supply (I386_EDI_REGNUM, &sf.sf_edi);
65*6881a400Schristos   regcache->raw_supply (I386_ESI_REGNUM, &sf.sf_esi);
66*6881a400Schristos   regcache->raw_supply (I386_EBP_REGNUM, &pcb->pcb_ebp);
67*6881a400Schristos   regcache->raw_supply (I386_ESP_REGNUM, &pcb->pcb_esp);
68*6881a400Schristos   regcache->raw_supply (I386_EBX_REGNUM, &sf.sf_ebx);
69*6881a400Schristos   regcache->raw_supply (I386_EIP_REGNUM, &sf.sf_eip);
70*6881a400Schristos 
71*6881a400Schristos   return 1;
72*6881a400Schristos }
73*6881a400Schristos 
74*6881a400Schristos static i386_bsd_nat_target<nbsd_nat_target> the_i386_nbsd_nat_target;
75*6881a400Schristos 
76*6881a400Schristos void _initialize_i386nbsd_nat ();
77*6881a400Schristos void
78*6881a400Schristos _initialize_i386nbsd_nat ()
79*6881a400Schristos {
80*6881a400Schristos   add_inf_child_target (&the_i386_nbsd_nat_target);
81*6881a400Schristos 
82*6881a400Schristos   /* Support debugging kernel virtual memory images.  */
83*6881a400Schristos   bsd_kvm_add_target (i386nbsd_supply_pcb);
84*6881a400Schristos }
85