Xen Test Framework
decode.c
Go to the documentation of this file.
1 
6 #include <xtf/lib.h>
7 #include <xtf/libc.h>
8 
9 #include <arch/decode.h>
10 #include <arch/processor.h>
11 
12 const char *x86_vendor_name(enum x86_vendor v)
13 {
14  static const char *const names[] =
15  {
16  [X86_VENDOR_INTEL] = "Intel",
17  [X86_VENDOR_AMD] = "AMD",
18  };
19 
20  return (v < ARRAY_SIZE(names) && names[v]) ? names[v] : "Unknown";
21 }
22 
23 const char *x86_exc_short_name(unsigned int exc)
24 {
25  static const char *const names[] =
26  {
28 #define NAME(x) [X86_EXC_ ## x] = "#" #x
29  NAME(DE), NAME(DB), NAME(NMI), NAME(BP), NAME(OF), NAME(BR),
30  NAME(UD), NAME(NM), NAME(DF), NAME(CSO), NAME(TS), NAME(NP),
31  NAME(SS), NAME(GP), NAME(PF), NAME(SPV), NAME(MF), NAME(AC),
32  NAME(MC), NAME(XM), NAME(VE),
33 #undef NAME
34 
35  };
36 
37  return (exc < ARRAY_SIZE(names) && names[exc]) ? names[exc] : "??";
38 }
39 
40 static int x86_exc_decode_ec(char *buf, size_t bufsz,
41  unsigned int ev, unsigned int ec)
42 {
43  switch ( ev )
44  {
45  case X86_EXC_PF:
46  return snprintf(buf, bufsz, "%c%c%c%c%c%c",
47  ec & X86_PFEC_PK ? 'K' : '-',
48  ec & X86_PFEC_INSN ? 'I' : 'd',
49  ec & X86_PFEC_RSVD ? 'R' : '-',
50  ec & X86_PFEC_USER ? 'U' : 's',
51  ec & X86_PFEC_WRITE ? 'W' : 'r',
52  ec & X86_PFEC_PRESENT ? 'P' : '-');
53 
54  case X86_EXC_TS: case X86_EXC_NP: case X86_EXC_SS:
55  case X86_EXC_GP: case X86_EXC_AC:
56  if ( ec != 0 )
57  {
58  return snprintf(buf, bufsz,
59  "%cDT[%u]%s",
60  ec & X86_EC_IDT ? 'I' :
61  (ec & X86_EC_TI ? 'L' : 'G'),
62  ec >> X86_EC_SEL_SHIFT,
63  ec & X86_EC_EXT ? ",EXT" : "");
64  }
65 
66  /* Fallthrough */
67  default:
68  return snprintf(buf, bufsz, "%04x", ec);
69  }
70 }
71 
72 static int x86_decode_exinfo(char *buf, size_t bufsz, exinfo_t info)
73 {
74  if ( !info )
75  return snprintf(buf, bufsz, "nothing");
76 
77  unsigned int vec = exinfo_vec(info), ec = exinfo_ec(info);
78 
79  if ( ec || ((vec < 32) && ((1u << vec) & X86_EXC_HAVE_EC)) )
80  {
81  char ecstr[16];
82 
83  x86_exc_decode_ec(ecstr, ARRAY_SIZE(ecstr), vec, ec);
84 
85  return snprintf(buf, bufsz, "%s[%s]", x86_exc_short_name(vec), ecstr);
86  }
87  else
88  return snprintf(buf, bufsz, "%s", x86_exc_short_name(vec));
89 }
90 
92  char **str_ptr, char *end, const char **fmt_ptr, const void *arg,
93  int width, int precision, unsigned int flags)
94 {
95  const char *fmt = *fmt_ptr;
96  char *str = *str_ptr;
97 
98  switch ( fmt[1] )
99  {
100  case 'e':
101  {
102  exinfo_t ex = _u(arg);
103  char buf[16];
104 
105  /* Consumed 'e' from the format string. */
106  ++*fmt_ptr;
107 
108  x86_decode_exinfo(buf, sizeof buf, ex);
109 
110  str = fmt_string(str, end, buf, width, precision, flags);
111  break;
112  }
113 
114  default:
115  return false;
116  }
117 
118  *str_ptr = str;
119  return true;
120 }
121 
122 /*
123  * Local variables:
124  * mode: C
125  * c-file-style: "BSD"
126  * c-basic-offset: 4
127  * tab-width: 4
128  * indent-tabs-mode: nil
129  * End:
130  */
unsigned int exinfo_t
Packed exception and error code information.
Definition: exinfo.h:19
bool arch_fmt_pointer(char **str_ptr, char *end, const char **fmt_ptr, const void *arg, int width, int precision, unsigned int flags)
Definition: decode.c:91
int snprintf(char *buf, size_t size, const char *fmt,...)
Definition: stdio.c:3
#define X86_EXC_HAVE_EC
Definition: processor.h:125
#define ARRAY_SIZE(a)
Definition: lib.h:8
static unsigned int exinfo_vec(exinfo_t info)
Definition: exinfo.h:31
#define X86_EXC_GP
Definition: processor.h:115
#define X86_EC_SEL_SHIFT
Definition: processor.h:161
#define _u(v)
Express an arbitrary value v as unsigned long.
Definition: numbers.h:53
Helper routines for decoding x86 state.
const char * x86_vendor_name(enum x86_vendor v)
String of the indentified vendor v.
Definition: decode.c:12
#define X86_PFEC_USER
Definition: processor.h:169
static int x86_exc_decode_ec(char *buf, size_t bufsz, unsigned int ev, unsigned int ec)
Definition: decode.c:40
#define X86_EC_EXT
Definition: processor.h:155
char * fmt_string(char *str, char *end, const char *val, int width, int precision, unsigned int flags)
Definition: vsnprintf.c:171
#define X86_EC_TI
Definition: processor.h:157
x86_vendor
Definition: cpuid.h:16
#define SS
#define X86_PFEC_PK
Definition: processor.h:172
#define X86_EC_IDT
Definition: processor.h:156
#define GP
#define X86_PFEC_RSVD
Definition: processor.h:170
static unsigned int str(void)
Definition: lib.h:366
#define X86_EXC_NP
Definition: processor.h:113
#define X86_PFEC_INSN
Definition: processor.h:171
#define X86_EXC_AC
Definition: processor.h:119
static unsigned int exinfo_ec(exinfo_t info)
Definition: exinfo.h:36
#define X86_PFEC_PRESENT
Definition: processor.h:167
static int x86_decode_exinfo(char *buf, size_t bufsz, exinfo_t info)
Definition: decode.c:72
const char * x86_exc_short_name(unsigned int exc)
String abbreviation of ev.
Definition: decode.c:23
#define X86_EXC_SS
Definition: processor.h:114
#define X86_EXC_TS
Definition: processor.h:112
#define X86_EXC_PF
Definition: processor.h:116
#define X86_PFEC_WRITE
Definition: processor.h:168