aboutsummaryrefslogtreecommitdiff
path: root/gdb/i386-fbsd-tdep.c
blob: d68498cd5e9d52c075707f0e6a1e36014b6288c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Target-dependent code for FreeBSD/i386.

   Copyright (C) 2003-2022 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "defs.h"
#include "osabi.h"
#include "regcache.h"
#include "regset.h"
#include "trad-frame.h"
#include "tramp-frame.h"
#include "i386-fbsd-tdep.h"
#include "gdbsupport/x86-xstate.h"

#include "i386-tdep.h"
#include "i387-tdep.h"
#include "fbsd-tdep.h"
#include "solib-svr4.h"
#include "inferior.h"

/* The general-purpose regset consists of 19 32-bit slots.  */
#define I386_FBSD_SIZEOF_GREGSET	(19 * 4)

/* The segment base register set consists of 2 32-bit registers.  */
#define I386_FBSD_SIZEOF_SEGBASES_REGSET	(2 * 4)

/* Register maps.  */

static const struct regcache_map_entry i386_fbsd_gregmap[] =
{
  { 1, I386_FS_REGNUM, 4 },
  { 1, I386_ES_REGNUM, 4 },
  { 1, I386_DS_REGNUM, 4 },
  { 1, I386_EDI_REGNUM, 0 },
  { 1, I386_ESI_REGNUM, 0 },
  { 1, I386_EBP_REGNUM, 0 },
  { 1, REGCACHE_MAP_SKIP, 4 },	/* isp */
  { 1, I386_EBX_REGNUM, 0 },
  { 1, I386_EDX_REGNUM, 0 },
  { 1, I386_ECX_REGNUM, 0 },
  { 1, I386_EAX_REGNUM, 0 },
  { 1, REGCACHE_MAP_SKIP, 4 },	/* trapno */
  { 1, REGCACHE_MAP_SKIP, 4 },	/* err */
  { 1, I386_EIP_REGNUM, 0 },
  { 1, I386_CS_REGNUM, 4 },
  { 1, I386_EFLAGS_REGNUM, 0 },
  { 1, I386_ESP_REGNUM, 0 },
  { 1, I386_SS_REGNUM, 4 },
  { 1, I386_GS_REGNUM, 4 },
  { 0 }
};

static const struct regcache_map_entry i386_fbsd_segbases_regmap[] =
{
  { 1, I386_FSBASE_REGNUM, 0 },
  { 1, I386_GSBASE_REGNUM, 0 },
  { 0 }
};

/* This layout including fsbase and gsbase was adopted in FreeBSD
   8.0.  */

static const struct regcache_map_entry i386_fbsd_mcregmap[] =
{
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_onstack */
  { 1, I386_GS_REGNUM, 4 },
  { 1, I386_FS_REGNUM, 4 },
  { 1, I386_ES_REGNUM, 4 },
  { 1, I386_DS_REGNUM, 4 },
  { 1, I386_EDI_REGNUM, 0 },
  { 1, I386_ESI_REGNUM, 0 },
  { 1, I386_EBP_REGNUM, 0 },
  { 1, REGCACHE_MAP_SKIP, 4 },	/* isp */
  { 1, I386_EBX_REGNUM, 0 },
  { 1, I386_EDX_REGNUM, 0 },
  { 1, I386_ECX_REGNUM, 0 },
  { 1, I386_EAX_REGNUM, 0 },
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_trapno */
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_err */
  { 1, I386_EIP_REGNUM, 0 },
  { 1, I386_CS_REGNUM, 4 },
  { 1, I386_EFLAGS_REGNUM, 0 },
  { 1, I386_ESP_REGNUM, 0 },
  { 1, I386_SS_REGNUM, 4 },
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_len */
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_fpformat */
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_ownedfp */
  { 1, REGCACHE_MAP_SKIP, 4 },	/* mc_flags */
  { 128, REGCACHE_MAP_SKIP, 4 },/* mc_fpstate */
  { 1, I386_FSBASE_REGNUM, 0 },
  { 1, I386_GSBASE_REGNUM, 0 },
  { 0 }
};

/* Register set definitions.  */

const struct regset i386_fbsd_gregset =
{
  i386_fbsd_gregmap, regcache_supply_regset, regcache_collect_regset
};

const struct regset i386_fbsd_segbases_regset =
{
  i386_fbsd_segbases_regmap, regcache_supply_regset, regcache_collect_regset
};

/* Support for signal handlers.  */

/* In a signal frame, esp points to a 'struct sigframe' which is
   defined as:

   struct sigframe {
	register_t	sf_signum;
	register_t	sf_siginfo;
	register_t	sf_ucontext;
	register_t	sf_addr;
	union {
		__siginfohandler_t	*sf_action;
		__sighandler_t		*sf_handler;
	} sf_ahu;
	ucontext_t	sf_uc;
        ...
   }

   ucontext_t is defined as:

   struct __ucontext {
	   sigset_t	uc_sigmask;
	   mcontext_t	uc_mcontext;
	   ...
   };

   The mcontext_t contains the general purpose register set as well
   as the floating point or XSAVE state.  */

/* NB: There is a 12 byte padding hole between sf_ahu and sf_uc. */
#define I386_SIGFRAME_UCONTEXT_OFFSET 		32
#define I386_UCONTEXT_MCONTEXT_OFFSET		16
#define I386_SIZEOF_MCONTEXT_T			640

/* Implement the "init" method of struct tramp_frame.  */

static void
i386_fbsd_sigframe_init (const struct tramp_frame *self,
			 struct frame_info *this_frame,
			 struct trad_frame_cache *this_cache,
			 CORE_ADDR func)
{
  CORE_ADDR sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
  CORE_ADDR mcontext_addr
    = (sp
       + I386_SIGFRAME_UCONTEXT_OFFSET
       + I386_UCONTEXT_MCONTEXT_OFFSET);

  trad_frame_set_reg_regmap (this_cache, i386_fbsd_mcregmap, mcontext_addr,
			     I386_SIZEOF_MCONTEXT_T);

  /* Don't bother with floating point or XSAVE state for now.  The
     current helper routines for parsing FXSAVE and XSAVE state only
     work with regcaches.  This could perhaps create a temporary
     regcache, collect the register values from mc_fpstate and
     mc_xfpustate, and then set register values in the trad_frame.  */

  trad_frame_set_id (this_cache, frame_id_build (sp, func));
}

static const struct tramp_frame i386_fbsd_sigframe =
{
  SIGTRAMP_FRAME,
  1,
  {
    {0x8d, ULONGEST_MAX},		/* lea     SIGF_UC(%esp),%eax */
    {0x44, ULONGEST_MAX},
    {0x24, ULONGEST_MAX},
    {0x20, ULONGEST_MAX},
    {0x50, ULONGEST_MAX},		/* pushl   %eax */
    {0xf7, ULONGEST_MAX},		/* testl   $PSL_VM,UC_EFLAGS(%eax) */
    {0x40, ULONGEST_MAX},
    {0x54, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x02, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x75, ULONGEST_MAX},		/* jne	   +3 */
    {0x03, ULONGEST_MAX},
    {0x8e, ULONGEST_MAX},		/* mov	   UC_GS(%eax),%gs */
    {0x68, ULONGEST_MAX},
    {0x14, ULONGEST_MAX},
    {0xb8, ULONGEST_MAX},		/* movl   $SYS_sigreturn,%eax */
    {0xa1, ULONGEST_MAX},
    {0x01, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x50, ULONGEST_MAX},		/* pushl   %eax */
    {0xcd, ULONGEST_MAX},		/* int	   $0x80 */
    {0x80, ULONGEST_MAX},
    {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  },
  i386_fbsd_sigframe_init
};

/* FreeBSD/i386 binaries running under an amd64 kernel use a different
   trampoline.  This trampoline differs from the i386 kernel trampoline
   in that it omits a middle section that conditionally restores
   %gs.  */

static const struct tramp_frame i386_fbsd64_sigframe =
{
  SIGTRAMP_FRAME,
  1,
  {
    {0x8d, ULONGEST_MAX},		/* lea     SIGF_UC(%esp),%eax */
    {0x44, ULONGEST_MAX},
    {0x24, ULONGEST_MAX},
    {0x20, ULONGEST_MAX},
    {0x50, ULONGEST_MAX},		/* pushl   %eax */
    {0xb8, ULONGEST_MAX},		/* movl   $SYS_sigreturn,%eax */
    {0xa1, ULONGEST_MAX},
    {0x01, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x00, ULONGEST_MAX},
    {0x50, ULONGEST_MAX},		/* pushl   %eax */
    {0xcd, ULONGEST_MAX},		/* int	   $0x80 */
    {0x80, ULONGEST_MAX},
    {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
  },
  i386_fbsd_sigframe_init
};

/* Get XSAVE extended state xcr0 from core dump.  */

uint64_t
i386fbsd_core_read_xcr0 (bfd *abfd)
{
  asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
  uint64_t xcr0;

  if (xstate)
    {
      size_t size = bfd_section_size (xstate);

      /* Check extended state size.  */
      if (size < X86_XSTATE_AVX_SIZE)
	xcr0 = X86_XSTATE_SSE_MASK;
      else
	{
	  char contents[8];

	  if (! bfd_get_section_contents (abfd, xstate, contents,
					  I386_FBSD_XSAVE_XCR0_OFFSET,
					  8))
	    {
	      warning (_("Couldn't read `xcr0' bytes from "
			 "`.reg-xstate' section in core file."));
	      return X86_XSTATE_SSE_MASK;
	    }

	  xcr0 = bfd_get_64 (abfd, contents);
	}
    }
  else
    xcr0 = X86_XSTATE_SSE_MASK;

  return xcr0;
}

/* Implement the core_read_description gdbarch method.  */

static const struct target_desc *
i386fbsd_core_read_description (struct gdbarch *gdbarch,
				struct target_ops *target,
				bfd *abfd)
{
  return i386_target_description (i386fbsd_core_read_xcr0 (abfd), true);
}

/* Similar to i386_supply_fpregset, but use XSAVE extended state.  */

static void
i386fbsd_supply_xstateregset (const struct regset *regset,
			      struct regcache *regcache, int regnum,
			      const void *xstateregs, size_t len)
{
  i387_supply_xsave (regcache, regnum, xstateregs);
}

/* Similar to i386_collect_fpregset, but use XSAVE extended state.  */

static void
i386fbsd_collect_xstateregset (const struct regset *regset,
			       const struct regcache *regcache,
			       int regnum, void *xstateregs, size_t len)
{
  i387_collect_xsave (regcache, regnum, xstateregs, 1);
}

/* Register set definitions.  */

static const struct regset i386fbsd_xstateregset =
  {
    NULL,
    i386fbsd_supply_xstateregset,
    i386fbsd_collect_xstateregset
  };

/* Iterate over core file register note sections.  */

static void
i386fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
				       iterate_over_regset_sections_cb *cb,
				       void *cb_data,
				       const struct regcache *regcache)
{
  i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);

  cb (".reg", I386_FBSD_SIZEOF_GREGSET, I386_FBSD_SIZEOF_GREGSET,
      &i386_fbsd_gregset, NULL, cb_data);
  cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &i386_fpregset,
      NULL, cb_data);
  cb (".reg-x86-segbases", I386_FBSD_SIZEOF_SEGBASES_REGSET,
      I386_FBSD_SIZEOF_SEGBASES_REGSET, &i386_fbsd_segbases_regset,
      "segment bases", cb_data);

  if (tdep->xcr0 & X86_XSTATE_AVX)
    cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0),
	X86_XSTATE_SIZE (tdep->xcr0), &i386fbsd_xstateregset,
	"XSAVE extended state", cb_data);
}

/* Implement the get_thread_local_address gdbarch method.  */

static CORE_ADDR
i386fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
				   CORE_ADDR lm_addr, CORE_ADDR offset)
{
  struct regcache *regcache;

  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
				       ptid, gdbarch);

  target_fetch_registers (regcache, I386_GSBASE_REGNUM);

  ULONGEST gsbase;
  if (regcache->cooked_read (I386_GSBASE_REGNUM, &gsbase) != REG_VALID)
    error (_("Unable to fetch %%gsbase"));

  CORE_ADDR dtv_addr = gsbase + gdbarch_ptr_bit (gdbarch) / 8;
  return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
}

static void
i386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
  i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);

  /* Generic FreeBSD support. */
  fbsd_init_abi (info, gdbarch);

  /* Obviously FreeBSD is BSD-based.  */
  i386bsd_init_abi (info, gdbarch);

  /* FreeBSD reserves some space for its FPU emulator in
     `struct fpreg'.  */
  tdep->sizeof_fpregset = 176;

  /* FreeBSD uses -freg-struct-return by default.  */
  tdep->struct_return = reg_struct_return;

  tramp_frame_prepend_unwinder (gdbarch, &i386_fbsd_sigframe);
  tramp_frame_prepend_unwinder (gdbarch, &i386_fbsd64_sigframe);

  i386_elf_init_abi (info, gdbarch);

  tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;

  /* Iterate over core file register note sections.  */
  set_gdbarch_iterate_over_regset_sections
    (gdbarch, i386fbsd_iterate_over_regset_sections);

  set_gdbarch_core_read_description (gdbarch,
				     i386fbsd_core_read_description);

  /* FreeBSD uses SVR4-style shared libraries.  */
  set_solib_svr4_fetch_link_map_offsets
    (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  set_gdbarch_fetch_tls_load_module_address (gdbarch,
					     svr4_fetch_objfile_link_map);
  set_gdbarch_get_thread_local_address (gdbarch,
					i386fbsd_get_thread_local_address);
}

void _initialize_i386fbsd_tdep ();
void
_initialize_i386fbsd_tdep ()
{
  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_FREEBSD,
			  i386fbsd_init_abi);
}