aboutsummaryrefslogtreecommitdiff
path: root/gdb/s390-lk-tdep.c
blob: 7ff71b8518841eaac9d6166eb81d8f45ec5b9d41 (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
/* Target-dependent code for linux-kernel target on S390.
   Copyright (C) 2017 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 "gdbarch.h"
#include "gdbcore.h"
#include "gdbthread.h"
#include "lk-low.h"
#include "osabi.h"
#include "regcache.h"
#include "regset.h"
#include "s390-tdep.h"
#include "s390-lk-tdep.h"

#include "features/s390x-cr-linux64.c"
#include "features/s390x-vxcr-linux64.c"

/* Register maps and sets.  */

static const struct regcache_map_entry s390x_gregmap_lk[] =
  {
    { 10, S390_R6_REGNUM }, /* r0-r5 volatile */
    { -2, REGCACHE_MAP_SKIP, 8 },
    { 1, S390_PSWA_REGNUM }, /* Use r14 for PSWA.  */
    { 0 }
  };

static const struct regcache_map_entry s390x_regmap_cr [] =
  {
    { 16, S390_CR0_REGNUM, 8 },
    { 0 }
  };

static const struct regcache_map_entry s390x_regmap_timer [] =
  {
    { 1, S390_TIMER_REGNUM, 8 },
    { 0 }
  };

static const struct regcache_map_entry s390x_regmap_todcmp [] =
  {
    { 1, S390_TODCMP_REGNUM, 8 },
    { 0 }
  };

static const struct regcache_map_entry s390x_regmap_todpreg [] =
  {
    { 1, S390_TODPREG_REGNUM, 4 },
    { 0 }
  };

static const struct regcache_map_entry s390x_regmap_prefix [] =
  {
    { 1, S390_PREFIX_REGNUM, 4 },
    { 0 }
  };

const struct regset s390x_gregset_lk = {
  s390x_gregmap_lk,
  regcache_supply_regset,
  regcache_collect_regset
};

const struct regset s390x_cr_regset = {
  s390x_regmap_cr,
  regcache_supply_regset,
  regcache_collect_regset
};

const struct regset s390x_timer_regset = {
  s390x_regmap_timer,
  regcache_supply_regset,
  regcache_collect_regset
};

const struct regset s390x_todcmp_regset = {
  s390x_regmap_todcmp,
  regcache_supply_regset,
  regcache_collect_regset
};

const struct regset s390x_todpreg_regset = {
  s390x_regmap_todpreg,
  regcache_supply_regset,
  regcache_collect_regset
};

const struct regset s390x_prefix_regset = {
  s390x_regmap_prefix,
  regcache_supply_regset,
  regcache_collect_regset
};

/* Function for Linux kernel target get_registers hook.  Supplies gprs for
   task TASK to REGCACHE.  Uses r14 (back jump address) as current pswa.  */

void
s390_lk_get_registers (CORE_ADDR task, struct target_ops *target,
		       struct regcache *regcache, int regnum)
{
  const struct regset *regset;
  CORE_ADDR ksp, gprs, pswa;
  gdb_byte buf[80]; /* 80 = 10 (#registers saved) * 8 (64 bit width) */
  size_t size;

  regset = &s390x_gregset_lk;

  ksp = lk_read_addr (task + LK_OFFSET (task_struct, thread)
		      + LK_OFFSET (thread_struct, ksp));
  gprs = ksp + LK_OFFSET (stack_frame, gprs);
  size = FIELD_SIZE (LK_FIELD (stack_frame, gprs));
  gdb_assert (size <= sizeof (buf));

  read_memory (gprs, buf, size);
  regset->supply_regset (regset, regcache, -1, buf, size);
}

/* Function for Linux kernel target get_percpu_offset hook.  Returns the
   percpu_offset from lowcore for cpu CPU.  */

CORE_ADDR
s390_lk_get_percpu_offset (unsigned int cpu)
{
  CORE_ADDR lowcore_ptr, lowcore;
  size_t ptr_len = lk_builtin_type_size (unsigned_long);

  lowcore_ptr = LK_ADDR (lowcore_ptr) + (ptr_len * cpu);
  lowcore = lk_read_addr (lowcore_ptr);

  return lk_read_addr (lowcore + LK_OFFSET (lowcore, percpu_offset));
}

/* Function for Linux kernel target map_running_task_to_cpu hook.  */

unsigned int
s390_lk_map_running_task_to_cpu (struct thread_info *ti)
{
  struct regcache *regcache;
  enum register_status reg_status;
  CORE_ADDR lowcore;
  unsigned int cpu;

  regcache = get_thread_regcache (ti->ptid);
  reg_status = regcache_raw_read_unsigned (regcache, S390_PREFIX_REGNUM,
					   (ULONGEST *) &lowcore);
  if (reg_status != REG_VALID)
    error (_("Could not find prefix register for thread with pid %d, lwp %li."),
	   ti->ptid.pid, ti->ptid.lwp);

  cpu = lk_read_uint (lowcore + LK_OFFSET (lowcore, cpu_nr));

  return cpu;
}

/* Function for Linux kernel target is_kvaddr hook.  */

int
s390_lk_is_kvaddr (CORE_ADDR addr)
{
  return addr >= LK_ADDR (high_memory);
}

/* Read table entry from TABLE at offset OFFSET.  Helper for s390_lk_vtop.  */

static inline ULONGEST
s390_lk_read_table_entry (CORE_ADDR table, ULONGEST offset)
{
  return lk_read_ulong (table + offset * lk_builtin_type_size (unsigned_long));
}

/* Function for Linux kernel target vtop hook.  Assume 64 bit addresses.  */

CORE_ADDR
s390_lk_vtop (CORE_ADDR table, CORE_ADDR vaddr)
{
  ULONGEST entry, offset;
  CORE_ADDR paddr;
  unsigned int table_type;
  size_t addr_size = lk_builtin_type_size (unsigned_long);

  /* Read first entry in table to get its type.  As the Table-Type bits are
     the same in every table assume Region1-Table.  */
  entry = s390_lk_read_table_entry (table, 0);
  table_type = (entry & S390_LK_RFTE_TT) >> 2;

  switch (table_type)
    {
      case S390_LK_DAT_TT_REGION1:
	{
	  offset = (vaddr & S390_LK_VADDR_RFX) >> 53;
	  entry = s390_lk_read_table_entry (table, offset);

	  /* Do sanity checks.  */
	  if (!entry)
	    warning (_("Trying to translate address 0x%s with empty "\
		       "region-first-table entry."),
		   phex (vaddr, addr_size));
	  else if ((entry & S390_LK_RFTE_TT) >> 2 != S390_LK_DAT_TT_REGION1)
	    warning (_("Trying to translate address 0x%s with corrupt "\
		       "table type in region-first-table entry."),
		     phex (vaddr, addr_size));
	  else if (entry & S390_LK_RFTE_I)
	    warning (_("Translating address 0x%s with invalid bit set at "\
		       "region-first-table entry."),
		     phex (vaddr, addr_size));

	  table = entry & S390_LK_RFTE_O;
	}
	/* fall through */
      case S390_LK_DAT_TT_REGION2:
	{
	  offset = (vaddr & S390_LK_VADDR_RSX) >> 42;
	  entry = s390_lk_read_table_entry (table, offset);

	  /* Do sanity checks.  */
	  if (!entry)
	    warning (_("Trying to translate address 0x%s with empty "\
		       "region-second-table entry."),
		   phex (vaddr, addr_size));
	  else if ((entry & S390_LK_RSTE_TT) >> 2 != S390_LK_DAT_TT_REGION2)
	    warning (_("Trying to translate address 0x%s with corrupt "\
		       "table type in region-second-table entry."),
		     phex (vaddr, addr_size));
	  else if (entry & S390_LK_RSTE_I)
	    warning (_("Translating address 0x%s with invalid bit set at "\
		       "region-second-table entry."),
		     phex (vaddr, addr_size));

	  table = entry & S390_LK_RSTE_O;
	}
	/* fall through */
      case S390_LK_DAT_TT_REGION3:
	{
	  offset = (vaddr & S390_LK_VADDR_RTX) >> 31;
	  entry = s390_lk_read_table_entry (table, offset);

	  /* Do sanity checks.  */
	  if (!entry)
	    warning (_("Trying to translate address 0x%s with empty "\
		       "region-third-table entry."),
		   phex (vaddr, addr_size));
	  else if ((entry & S390_LK_RTTE_TT) >> 2 != S390_LK_DAT_TT_REGION3)
	    warning (_("Trying to translate address 0x%s with corrupt "\
		       "table type in region-third-table entry."),
		     phex (vaddr, addr_size));
	  else if (entry & S390_LK_RTTE_I)
	    warning (_("Translating address 0x%s with invalid bit set at "\
		       "region-third-table entry."),
		     phex (vaddr, addr_size));

	  /* Check for huge page.  */
	  if (entry & S390_LK_RTTE_FC)
	    {
	      paddr = ((entry & S390_LK_RTTE_RFAA)
		       + (vaddr & ~S390_LK_RTTE_RFAA));
	      return paddr;
	    }

	  table = entry & S390_LK_RTTE_O;
	}
	/* fall through */
      case S390_LK_DAT_TT_SEGMENT:
	{
	  offset = (vaddr & S390_LK_VADDR_SX) >> 20;
	  entry = s390_lk_read_table_entry (table, offset);

	  /* Do sanity checks.  */
	  if (!entry)
	    warning (_("Trying to translate address 0x%s with empty "\
		       "segment-table entry."),
		   phex (vaddr, addr_size));
	  else if ((entry & S390_LK_STE_TT) >> 2 != S390_LK_DAT_TT_SEGMENT)
	    warning (_("Trying to translate address 0x%s with corrupt "\
		       "table type in segment-table entry."),
		     phex (vaddr, addr_size));
	  else if (entry & S390_LK_STE_I)
	    warning (_("Translating address 0x%s with invalid bit set at "\
		       "segment-table entry."),
		     phex (vaddr, addr_size));

	  /* Check for large page.  */
	  if (entry & S390_LK_STE_FC)
	    {
	      paddr = ((entry & S390_LK_STE_SFAA)
		       + (vaddr & ~S390_LK_STE_SFAA));
	      return paddr;
	    }

	  table = entry & S390_LK_STE_O;
	  break;
	}
    } /* switch (table_type) */

  offset = (vaddr & S390_LK_VADDR_PX) >> 12;
  entry = s390_lk_read_table_entry (table, offset);

  /* Do sanity checks.  */
  if (!entry)
    warning (_("Trying to translate address 0x%s with empty page-table "\
	       "entry."),
	     phex (vaddr, addr_size));
  else if (entry & S390_LK_PTE_I)
    warning (_("Translating address 0x%s with invalid bit set at page-table "\
	       "entry."),
	     phex (vaddr, addr_size));

  paddr = ((entry & S390_LK_PTE_PFAA) + (vaddr & ~S390_LK_PTE_PFAA));

  return paddr;
}

/* Function for Linux kernel target get_module_text_offset hook.  */

CORE_ADDR
s390_lk_get_module_text_offset (CORE_ADDR mod)
{
  CORE_ADDR offset, mod_arch;

  mod_arch = mod + LK_OFFSET (module, arch);
  offset = lk_read_ulong (mod_arch + LK_OFFSET (mod_arch_specific, got_size));
  offset += lk_read_ulong (mod_arch + LK_OFFSET (mod_arch_specific, plt_size));

  return offset;
}

/* Initialize s390 dependent private data for linux kernel target.  */

static void
s390_lk_init_private (struct gdbarch *gdbarch)
{
  LK_DECLARE_FIELD (stack_frame, gprs);

  LK_DECLARE_FIELD (thread_struct, ksp);

  LK_DECLARE_STRUCT_ALIAS (_lowcore, lowcore); /* linux -4.4 */
  LK_DECLARE_STRUCT_ALIAS (lowcore, lowcore); /* linux 4.5+ */
  if (LK_STRUCT (lowcore) == NULL)
    error (_("Could not find struct lowcore.  Aborting."));
  LK_DECLARE_FIELD (lowcore, percpu_offset);
  LK_DECLARE_FIELD (lowcore, current_pid);
  LK_DECLARE_FIELD (lowcore, cpu_nr);

  LK_DECLARE_FIELD (mod_arch_specific, got_size);
  LK_DECLARE_FIELD (mod_arch_specific, plt_size);

  LK_DECLARE_ADDR (lowcore_ptr);
  LK_DECLARE_ADDR (high_memory);

  LK_HOOK->get_registers = s390_lk_get_registers;
  LK_HOOK->is_kvaddr = s390_lk_is_kvaddr;
  LK_HOOK->vtop = s390_lk_vtop;
  LK_HOOK->get_percpu_offset = s390_lk_get_percpu_offset;
  LK_HOOK->map_running_task_to_cpu = s390_lk_map_running_task_to_cpu;
  LK_HOOK->get_module_text_offset = s390_lk_get_module_text_offset;
}

/* Initialize Linux kernel specific gdbarch hooks.  */

void
s390_gdbarch_lk_init (struct gdbarch_info info, struct gdbarch *gdbarch)
{
  /* Support linux kernel debugging.  */
  set_gdbarch_lk_init_private (gdbarch, s390_lk_init_private);
}

extern initialize_file_ftype _initialize_s390_lk_tdep; /* -Wmissing-prototypes */

void
_initialize_s390_lk_tdep (void)
{
  /* Initialize the Linux kernel target descriptions.  */
  initialize_tdesc_s390x_cr_linux64 ();
  initialize_tdesc_s390x_vxcr_linux64 ();
}