forked from mchehab/rasdaemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitfield.c
105 lines (95 loc) · 2.58 KB
/
bitfield.c
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
/*
* Copyright (C) 2013 Mauro Carvalho Chehab <[email protected]>
*
* The code below were adapted from Andi Kleen/Intel/SuSe mcelog code,
* released under GNU Public General License, v.2
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <stdio.h>
#include "ras-mce-handler.h"
#include "bitfield.h"
unsigned bitfield_msg(char *buf, size_t len, const char **bitarray,
unsigned array_len,
unsigned bit_offset, unsigned ignore_bits,
uint64_t status)
{
int i, n;
char *p = buf;
len--;
for (i = 0; i < array_len; i++) {
if (status & ignore_bits)
continue;
if (status & (1 << (i + bit_offset))) {
if (p != buf) {
n = snprintf(p, len, ", ");
if (n < 0)
break;
len -= n;
p += n;
}
if (!bitarray[i])
n = snprintf(p, len, "BIT%d", i + bit_offset);
else
n = snprintf(p, len, "%s", bitarray[i]);
if (n < 0)
break;
len -= n;
p += n;
}
}
*p = 0;
return p - buf;
}
static uint64_t bitmask(uint64_t i)
{
uint64_t mask = 1;
while (mask < i)
mask = (mask << 1) | 1;
return mask;
}
void decode_bitfield(struct mce_event *e, uint64_t status,
struct field *fields)
{
struct field *f;
for (f = fields; f->str; f++) {
uint64_t v = (status >> f->start_bit) & bitmask(f->stringlen - 1);
char *s = NULL;
if (v < f->stringlen)
s = f->str[v];
if (!s) {
if (v == 0)
continue;
mce_snprintf(e->error_msg, "<%u:%llx>",
f->start_bit, (long long)v);
} else
mce_snprintf(e->error_msg, "%s", s);
}
}
void decode_numfield(struct mce_event *e, uint64_t status,
struct numfield *fields)
{
struct numfield *f;
for (f = fields; f->name; f++) {
uint64_t mask = (1ULL << (f->end - f->start + 1)) - 1;
uint64_t v = (status >> f->start) & mask;
if (v > 0 || f->force) {
char fmt[32] = {0};
snprintf(fmt, 32, "%%s: %s\n", f->fmt ? f->fmt : "%Lu");
mce_snprintf(e->error_msg, fmt, f->name, v);
}
}
}