1/* Native-dependent code for OpenBSD/i386.
2
3   Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.  */
21
22#include "defs.h"
23#include "gdbcore.h"
24#include "regcache.h"
25#include "target.h"
26
27#include "i386-tdep.h"
28#include "obsd-nat.h"
29#include "i386bsd-nat.h"
30
31#include <sys/param.h>
32#include <sys/sysctl.h>
33
34/* Support for debugging kernel virtual memory images.  */
35
36#include <machine/frame.h>
37#include <machine/pcb.h>
38
39#include "bsd-kvm.h"
40
41static int
42i386obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
43{
44  struct switchframe sf;
45
46  /* The following is true for OpenBSD 4.3:
47
48     The pcb contains %esp and %ebp at the point of the context switch
49     in cpu_switchto().  At that point we have a stack frame as
50     described by `struct switchframe', which for OpenBSD 4.3 has the
51     following layout:
52
53     %edi
54     %esi
55     %ebx
56     %eip
57
58     we reconstruct the register state as it would look when we just
59     returned from cpu_switchto().
60
61     For crash dumps, the state is saved by savectx().  In that case
62     we just reconstruct the register state as if we just returned
63     from dumsys().  */
64
65  /* The stack pointer shouldn't be zero.  */
66  if (pcb->pcb_esp == 0)
67    return 0;
68
69  if ((pcb->pcb_flags & PCB_SAVECTX) == 0)
70    {
71      /* Yes, we have a frame that matches cpu_switchto().  */
72      read_memory (pcb->pcb_esp, (char *) &sf, sizeof sf);
73      pcb->pcb_esp += sizeof (struct switchframe);
74      regcache_raw_supply (regcache, I386_EDI_REGNUM, &sf.sf_edi);
75      regcache_raw_supply (regcache, I386_ESI_REGNUM, &sf.sf_esi);
76      regcache_raw_supply (regcache, I386_EBX_REGNUM, &sf.sf_ebx);
77      regcache_raw_supply (regcache, I386_EIP_REGNUM, &sf.sf_eip);
78    }
79  else
80    {
81      /* No, the pcb must have been last updated by savectx().  */
82      pcb->pcb_esp = pcb->pcb_ebp;
83      pcb->pcb_ebp = read_memory_integer(pcb->pcb_esp, 4);
84      sf.sf_eip = read_memory_integer(pcb->pcb_esp + 4, 4);
85      regcache_raw_supply (regcache, I386_EIP_REGNUM, &sf.sf_eip);
86    }
87
88  regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp);
89  regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp);
90
91  return 1;
92}
93
94
95/* Prevent warning from -Wmissing-prototypes.  */
96void _initialize_i386obsd_nat (void);
97
98void
99_initialize_i386obsd_nat (void)
100{
101  struct target_ops *t;
102
103  /* OpenBSD provides a vm.psstrings sysctl that we can use to locate
104     the sigtramp.  That way we can still recognize a sigtramp if its
105     location is changed in a new kernel.  This is especially
106     important for OpenBSD, since it uses a different memory layout
107     than NetBSD, yet we cannot distinguish between the two.
108
109     Of course this is still based on the assumption that the sigtramp
110     is placed directly under the location where the program arguments
111     and environment can be found.  */
112#ifdef VM_PSSTRINGS
113  {
114    struct _ps_strings _ps;
115    int mib[2];
116    size_t len;
117
118    mib[0] = CTL_VM;
119    mib[1] = VM_PSSTRINGS;
120    len = sizeof (_ps);
121    if (sysctl (mib, 2, &_ps, &len, NULL, 0) == 0)
122      {
123	i386obsd_sigtramp_start_addr = (CORE_ADDR)_ps.val - 128;
124	i386obsd_sigtramp_end_addr = (CORE_ADDR)_ps.val;
125      }
126  }
127#endif
128
129  /* Add some extra features to the common *BSD/i386 target.  */
130  t = i386bsd_target ();
131  t->to_pid_to_str = obsd_pid_to_str;
132  t->to_find_new_threads = obsd_find_new_threads;
133  t->to_wait = obsd_wait;
134  add_target (t);
135
136  /* Support debugging kernel virtual memory images.  */
137  bsd_kvm_add_target (i386obsd_supply_pcb);
138}
139