1 /* S390-specific core note handling.
2    Copyright (C) 2012 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7 
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11 
12    or
13 
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17 
18    or both in parallel, as here.
19 
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24 
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 
33 #include <elf.h>
34 #include <inttypes.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <sys/time.h>
38 
39 #ifndef BITS
40 # define BITS 		32
41 # define BACKEND	s390_
42 #else
43 # define BITS 		64
44 # define BACKEND	s390x_
45 #endif
46 #include "libebl_CPU.h"
47 
48 static const Ebl_Register_Location prstatus_regs[] =
49   {
50 #define GR(at, n, dwreg, b...)						\
51     { .offset = at * BITS/8, .regno = dwreg, .count = n, .bits = b }
52 
53     GR ( 0,  1, 64, BITS),				/* pswm */
54     GR ( 1,  1, 65, BITS, .pc_register = true ),	/* pswa */
55     GR ( 2, 16,  0, BITS),				/* r0-r15 */
56     GR (18, 16, 48,   32),				/* ar0-ar15 */
57 
58 #undef	GR
59   };
60 
61   /* orig_r2 is at offset (BITS == 32 ? 34 * 4 : 26 * 8).  */
62 #define PRSTATUS_REGS_SIZE	(BITS / 8 * (BITS == 32 ? 35 : 27))
63 
64 static const Ebl_Register_Location fpregset_regs[] =
65   {
66 #define FPR(at, n, dwreg)						\
67     { .offset = at * 64/8, .regno = dwreg, .count = n, .bits = 64 }
68 
69     /* fpc is at offset 0, see fpregset_items, it has no assigned DWARF regno.
70        Bytes at offsets 4 to 7 are unused.  */
71     FPR (1 +  0, 1, 16),	/* f0 */
72     FPR (1 +  1, 1, 20),	/* f1 */
73     FPR (1 +  2, 1, 17),	/* f2 */
74     FPR (1 +  3, 1, 21),	/* f3 */
75     FPR (1 +  4, 1, 18),	/* f4 */
76     FPR (1 +  5, 1, 22),	/* f5 */
77     FPR (1 +  6, 1, 19),	/* f6 */
78     FPR (1 +  7, 1, 23),	/* f7 */
79     FPR (1 +  8, 1, 24),	/* f8 */
80     FPR (1 +  9, 1, 28),	/* f9 */
81     FPR (1 + 10, 1, 25),	/* f10 */
82     FPR (1 + 11, 1, 29),	/* f11 */
83     FPR (1 + 12, 1, 26),	/* f12 */
84     FPR (1 + 13, 1, 30),	/* f13 */
85     FPR (1 + 14, 1, 27),	/* f14 */
86     FPR (1 + 15, 1, 31),	/* f15 */
87 
88 #undef	FPR
89   };
90 
91 static const Ebl_Core_Item fpregset_items[] =
92   {
93     {
94       .name = "fpc", .group = "register", .offset = 0, .type = ELF_T_WORD,
95       .format = 'x',
96     },
97   };
98 
99 /* Do not set FPREGSET_SIZE so that we can supply fpregset_items.  */
100 #define EXTRA_NOTES_FPREGSET \
101     EXTRA_REGSET_ITEMS (NT_FPREGSET, 17 * 8, fpregset_regs, fpregset_items)
102 
103 #if BITS == 32
104 # define ULONG			uint32_t
105 # define ALIGN_ULONG		4
106 # define TYPE_ULONG		ELF_T_WORD
107 # define TYPE_LONG		ELF_T_SWORD
108 # define UID_T			uint16_t
109 # define GID_T			uint16_t
110 # define ALIGN_UID_T		2
111 # define ALIGN_GID_T		2
112 # define TYPE_UID_T		ELF_T_HALF
113 # define TYPE_GID_T		ELF_T_HALF
114 #else
115 # define ULONG			uint64_t
116 # define ALIGN_ULONG		8
117 # define TYPE_ULONG		ELF_T_XWORD
118 # define TYPE_LONG		ELF_T_SXWORD
119 # define UID_T			uint32_t
120 # define GID_T			uint32_t
121 # define ALIGN_UID_T		4
122 # define ALIGN_GID_T		4
123 # define TYPE_UID_T		ELF_T_WORD
124 # define TYPE_GID_T		ELF_T_WORD
125 #endif
126 #define PID_T			int32_t
127 #define ALIGN_PID_T		4
128 #define TYPE_PID_T		ELF_T_SWORD
129 /* s390 psw_compat_t has alignment 8 bytes where it is inherited from.  */
130 #define ALIGN_PR_REG		8
131 
132 #define PRSTATUS_REGSET_ITEMS					\
133   {								\
134     .name = "orig_r2", .type = TYPE_LONG, .format = 'd',	\
135     .offset = offsetof (struct EBLHOOK(prstatus),		\
136 			pr_reg[BITS == 32 ? 34 : 26]),		\
137     .group = "register"						\
138   }
139 
140 #if BITS == 32
141 
142 static const Ebl_Core_Item high_regs_items[] =
143   {
144 #define HR(n)								\
145     {									\
146       .name = "high_r" #n , .group = "register", .offset = (n) * 4,	\
147       .type = ELF_T_WORD, .format = 'x',				\
148     }
149 
150     /* Upper halves of r0-r15 are stored here.
151        FIXME: They are currently not combined with the r0-r15 lower halves.  */
152     HR (0), HR (1), HR (2), HR (3), HR (4), HR (5), HR (6), HR (7),
153     HR (8), HR (9), HR (10), HR (11), HR (12), HR (13), HR (14), HR (15)
154 
155 #undef HR
156   };
157 
158 #define EXTRA_NOTES_HIGH_GPRS \
159   EXTRA_ITEMS (NT_S390_HIGH_GPRS, 16 * 4, high_regs_items)
160 
161 #else /* BITS == 64 */
162 
163 #define EXTRA_NOTES_HIGH_GPRS
164 
165 #endif /* BITS == 64 */
166 
167 static const Ebl_Core_Item last_break_items[] =
168   {
169     {
170       .name = "last_break", .group = "system", .offset = BITS == 32 ? 4 : 0,
171       .type = BITS == 32 ? ELF_T_WORD : ELF_T_XWORD, .format = 'x',
172     },
173   };
174 
175 static const Ebl_Core_Item system_call_items[] =
176   {
177     {
178       .name = "system_call", .group = "system", .offset = 0, .type = ELF_T_WORD,
179       .format = 'd',
180     },
181   };
182 
183 #define	EXTRA_NOTES							  \
184   EXTRA_NOTES_FPREGSET							  \
185   EXTRA_NOTES_HIGH_GPRS							  \
186   EXTRA_ITEMS (NT_S390_LAST_BREAK, 8, last_break_items)	  \
187   EXTRA_ITEMS (NT_S390_SYSTEM_CALL, 4, system_call_items)
188 
189 #include "linux-core-note.c"
190