]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ia64/ia64/mca.c
Populate the sysctl tree with any MCA records we collected.
[FreeBSD/FreeBSD.git] / sys / ia64 / ia64 / mca.c
1 /*-
2  * Copyright (c) 2002-2010 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
35 #include <sys/sysctl.h>
36 #include <sys/uuid.h>
37 #include <vm/vm.h>
38 #include <vm/vm_kern.h>
39 #include <machine/mca.h>
40 #include <machine/pal.h>
41 #include <machine/sal.h>
42 #include <machine/smp.h>
43
44 MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
45
46 struct mca_info {
47         STAILQ_ENTRY(mca_info) mi_link;
48         u_long  mi_seqnr;
49         u_int   mi_cpuid;
50         size_t  mi_recsz;
51         char    mi_record[0];
52 };
53
54 STAILQ_HEAD(mca_info_list, mca_info);
55
56 static int64_t          mca_info_size[SAL_INFO_TYPES];
57 static vm_offset_t      mca_info_block;
58 static struct mtx       mca_info_block_lock;
59
60 SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RW, NULL, "MCA container");
61
62 static int mca_count;           /* Number of records stored. */
63 static int mca_first;           /* First (lowest) record ID. */
64 static int mca_last;            /* Last (highest) record ID. */
65
66 SYSCTL_INT(_hw_mca, OID_AUTO, count, CTLFLAG_RD, &mca_count, 0,
67     "Record count");
68 SYSCTL_INT(_hw_mca, OID_AUTO, first, CTLFLAG_RD, &mca_first, 0,
69     "First record id");
70 SYSCTL_INT(_hw_mca, OID_AUTO, last, CTLFLAG_RD, &mca_last, 0,
71     "Last record id");
72
73 static struct mtx mca_sysctl_lock;
74
75 static int
76 mca_sysctl_inject(SYSCTL_HANDLER_ARGS)
77 {
78         struct ia64_pal_result res;
79         u_int val;
80         int error;
81
82         val = 0;
83         error = sysctl_wire_old_buffer(req, sizeof(u_int));
84         if (!error)
85                 error = sysctl_handle_int(oidp, &val, 0, req);
86
87         if (error != 0 || req->newptr == NULL)
88                 return (error);
89
90         /* For example: val=137 causes a fatal CPU error. */
91         res = ia64_call_pal_stacked(PAL_MC_ERROR_INJECT, val, 0, 0);
92         printf("%s: %#lx, %#lx, %#lx, %#lx\n", __func__, res.pal_status,
93             res.pal_result[0], res.pal_result[1], res.pal_result[2]);
94         return (0);
95 }
96 SYSCTL_PROC(_hw_mca, OID_AUTO, inject, CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
97     mca_sysctl_inject, "I", "set to trigger a MCA");
98
99 static int
100 mca_sysctl_handler(SYSCTL_HANDLER_ARGS)
101 {
102         int error = 0;
103
104         if (!arg1)
105                 return (EINVAL);
106         error = SYSCTL_OUT(req, arg1, arg2);
107
108         if (error || !req->newptr)
109                 return (error);
110
111         error = SYSCTL_IN(req, arg1, arg2);
112         return (error);
113 }
114
115 static void
116 ia64_mca_collect_state(int type, struct mca_info_list *reclst)
117 {
118         struct ia64_sal_result result;
119         struct mca_record_header *hdr;
120         struct mca_info *rec;
121         uint64_t seqnr;
122         size_t recsz;
123
124         /*
125          * Don't try to get the state if we couldn't get the size of
126          * the state information previously.
127          */
128         if (mca_info_size[type] == -1)
129                 return;
130
131         if (mca_info_block == 0)
132                 return;
133
134         while (1) {
135                 mtx_lock_spin(&mca_info_block_lock);
136                 result = ia64_sal_entry(SAL_GET_STATE_INFO, type, 0,
137                     mca_info_block, 0, 0, 0, 0);
138                 if (result.sal_status < 0) {
139                         mtx_unlock_spin(&mca_info_block_lock);
140                         break;
141                 }
142
143                 hdr = (struct mca_record_header *)mca_info_block;
144                 recsz = hdr->rh_length;
145                 seqnr = hdr->rh_seqnr;
146
147                 mtx_unlock_spin(&mca_info_block_lock);
148
149                 rec = malloc(sizeof(struct mca_info) + recsz, M_MCA,
150                     M_NOWAIT | M_ZERO);
151                 if (rec == NULL)
152                         /* XXX: Not sure what to do. */
153                         break;
154
155                 rec->mi_seqnr = seqnr;
156                 rec->mi_cpuid = PCPU_GET(cpuid);
157
158                 mtx_lock_spin(&mca_info_block_lock);
159
160                 /*
161                  * If the info block doesn't have our record anymore because
162                  * we temporarily unlocked it, get it again from SAL. I assume
163                  * that it's possible that we could get a different record.
164                  * I expect this to happen in a SMP configuration where the
165                  * record has been cleared by a different processor. So, if
166                  * we get a different record we simply abort with this record
167                  * and start over.
168                  */
169                 if (seqnr != hdr->rh_seqnr) {
170                         result = ia64_sal_entry(SAL_GET_STATE_INFO, type, 0,
171                             mca_info_block, 0, 0, 0, 0);
172                         if (seqnr != hdr->rh_seqnr) {
173                                 mtx_unlock_spin(&mca_info_block_lock);
174                                 free(rec, M_MCA);
175                                 continue;
176                         }
177                 }
178
179                 rec->mi_recsz = recsz;
180                 bcopy((char*)mca_info_block, rec->mi_record, recsz);
181
182                 /*
183                  * Clear the state so that we get any other records when
184                  * they exist.
185                  */
186                 result = ia64_sal_entry(SAL_CLEAR_STATE_INFO, type, 0, 0, 0,
187                     0, 0, 0);
188
189                 mtx_unlock_spin(&mca_info_block_lock);
190
191                 STAILQ_INSERT_TAIL(reclst, rec, mi_link);
192         }
193 }
194
195 void
196 ia64_mca_save_state(int type)
197 {
198         char name[64];
199         struct mca_info_list reclst = STAILQ_HEAD_INITIALIZER(reclst);
200         struct mca_info *rec;
201         struct sysctl_oid *oid;
202
203         ia64_mca_collect_state(type, &reclst);
204
205         STAILQ_FOREACH(rec, &reclst, mi_link) {
206                 sprintf(name, "%lu", rec->mi_seqnr);
207                 oid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca),
208                     OID_AUTO, name, CTLFLAG_RW, NULL, name);
209                 if (oid == NULL)
210                         continue;
211
212                 mtx_lock(&mca_sysctl_lock);
213                 if (mca_count > 0) {
214                         if (rec->mi_seqnr < mca_first)
215                                 mca_first = rec->mi_seqnr;
216                         else if (rec->mi_seqnr > mca_last)
217                                 mca_last = rec->mi_seqnr;
218                 } else
219                         mca_first = mca_last = rec->mi_seqnr;
220                 mca_count++;
221                 mtx_unlock(&mca_sysctl_lock);
222
223                 sprintf(name, "%u", rec->mi_cpuid);
224                 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(oid), rec->mi_cpuid,
225                     name, CTLTYPE_OPAQUE | CTLFLAG_RD, rec->mi_record,
226                     rec->mi_recsz, mca_sysctl_handler, "S,MCA", "MCA record");
227         }
228 }
229
230 void
231 ia64_mca_init(void)
232 {
233         struct ia64_sal_result result;
234         uint64_t max_size;
235         char *p;
236         int i;
237
238         /*
239          * Get the sizes of the state information we can get from SAL and
240          * allocate a common block (forgive me my Fortran :-) for use by
241          * support functions. We create a region 7 address to make it
242          * easy on the OS_MCA or OS_INIT handlers to get the state info
243          * under unreliable conditions.
244          */
245         max_size = 0;
246         for (i = 0; i < SAL_INFO_TYPES; i++) {
247                 result = ia64_sal_entry(SAL_GET_STATE_INFO_SIZE, i, 0, 0, 0,
248                     0, 0, 0);
249                 if (result.sal_status == 0) {
250                         mca_info_size[i] = result.sal_result[0];
251                         if (mca_info_size[i] > max_size)
252                                 max_size = mca_info_size[i];
253                 } else
254                         mca_info_size[i] = -1;
255         }
256         max_size = round_page(max_size);
257
258         p = (max_size) ? contigmalloc(max_size, M_TEMP, 0, 0ul,
259             256*1024*1024 - 1, PAGE_SIZE, 256*1024*1024) : NULL;
260         if (p != NULL) {
261                 mca_info_block = IA64_PHYS_TO_RR7(ia64_tpa((u_int64_t)p));
262
263                 if (bootverbose)
264                         printf("MCA: allocated %ld bytes for state info.\n",
265                             max_size);
266         }
267
268         /*
269          * Initialize the spin lock used to protect the info block. When APs
270          * get launched, there's a short moment of contention, but in all other
271          * cases it's not a hot spot. I think it's possible to have the MCA
272          * handler be called on multiple processors at the same time, but that
273          * should be rare. On top of that, performance is not an issue when
274          * dealing with machine checks...
275          */
276         mtx_init(&mca_info_block_lock, "MCA info lock", NULL, MTX_SPIN);
277
278         /*
279          * Serialize sysctl operations with a sleep lock. Note that this
280          * implies that we update the sysctl tree in a context that allows
281          * sleeping.
282          */
283         mtx_init(&mca_sysctl_lock, "MCA sysctl lock", NULL, MTX_DEF);
284
285         /*
286          * Get and save any processor and platfom error records. Note that in
287          * a SMP configuration the processor records are for the BSP only. We
288          * let the APs get and save their own records when we wake them up.
289          */
290         for (i = 0; i < SAL_INFO_TYPES; i++)
291                 ia64_mca_save_state(i);
292 }