Xen Test Framework
traps.c
Go to the documentation of this file.
1 #include <xtf/lib.h>
2 #include <xtf/traps.h>
3 #include <xtf/exlog.h>
4 
5 #include <arch/decode.h>
6 #include <arch/lib.h>
7 #include <arch/processor.h>
8 
9 /*
10  * Parameters for fine tuning the exec_user_*() behaviour. PV guests see the
11  * real interrupt flag, so mask it by default.
12  */
13 unsigned long exec_user_cs = __USER_CS;
14 unsigned long exec_user_ss = __USER_DS;
15 unsigned long exec_user_efl_and_mask =
16  ~(IS_DEFINED(CONFIG_PV) ? X86_EFLAGS_IF : 0);
17 unsigned long exec_user_efl_or_mask;
18 
19 /*
20  * C entry-point for exceptions, after the per-environment stubs have suitably
21  * adjusted the stack.
22  */
23 void do_exception(struct cpu_regs *regs)
24 {
25  const struct extable_entry *ex;
26  bool safe = false;
27 
29 
30  /* Look in the exception table to see if a redirection has been set up. */
31  if ( !safe && (ex = search_extable(regs->ip)) )
32  {
33  if ( ex->handler )
34  safe = ex->handler(regs, ex);
35  else
36  {
37  regs->ip = ex->fixup;
38  safe = true;
39  }
40  }
41 
42  /* Try the unhandled_exception() hook. */
43  if ( !safe )
44  safe = do_unhandled_exception(regs);
45 
46  /* Still unresolved? Give up and panic() with some relevent information. */
47  if ( !safe )
48  {
49  exinfo_t ex = EXINFO(regs->entry_vector, regs->error_code);
50 
51  if ( regs->entry_vector == X86_EXC_PF )
52  {
53  unsigned long cr2 = read_cr2();
54 
55  panic("Unhandled exception at %04x:%p\n"
56  "Vec %u %pe %%cr2 %p\n",
57  regs->cs, _p(regs->ip), regs->entry_vector, _p(ex), _p(cr2));
58  }
59  else
60  panic("Unhandled exception at %04x:%p\n"
61  "Vec %u %pe\n",
62  regs->cs, _p(regs->ip), regs->entry_vector, _p(ex));
63  }
64 }
65 
66 bool __weak do_unhandled_exception(struct cpu_regs *regs)
67 {
68  return false;
69 }
70 
71 void __weak do_syscall(struct cpu_regs *regs)
72 {
73  panic("Unhandled syscall\n");
74 }
75 
76 void __weak do_sysenter(struct cpu_regs *regs)
77 {
78  panic("Unhandled sysenter\n");
79 }
80 
81 void __weak do_evtchn(struct cpu_regs *regs)
82 {
83  panic("Unhandled evtchn upcall\n");
84 }
85 
86 /*
87  * Local variables:
88  * mode: C
89  * c-file-style: "BSD"
90  * c-basic-offset: 4
91  * tab-width: 4
92  * indent-tabs-mode: nil
93  * End:
94  */
bool do_unhandled_exception(struct cpu_regs *regs)
May be implemented by a guest to provide custom exception handling.
Definition: traps.c:66
unsigned int exinfo_t
Packed exception and error code information.
Definition: exinfo.h:19
unsigned long fixup
Fixup address.
Definition: extable.h:67
void do_syscall(struct cpu_regs *regs)
May be implemented by a guest to handle SYSCALL invocations.
Definition: traps.c:71
#define IS_DEFINED(x)
Evalute whether the CONFIG_ token x is defined.
Definition: macro_magic.h:67
#define EXINFO(vec, ec)
Definition: exinfo.h:26
bool(* handler)(struct cpu_regs *regs, const struct extable_entry *ex)
Optional custom handler.
Definition: extable.h:79
void panic(const char *fmt,...)
Definition: lib.c:15
Helper routines for decoding x86 state.
unsigned long exec_user_efl_or_mask
Definition: traps.c:17
#define X86_EFLAGS_IF
Definition: processor.h:14
static unsigned long read_cr2(void)
Definition: lib.h:234
unsigned long exec_user_cs
Definition: traps.c:13
const struct extable_entry * search_extable(unsigned long addr)
Search the exception table to find the entry associated with a specific faulting address.
Definition: extable.c:11
unsigned long exec_user_efl_and_mask
Definition: traps.c:15
Exception table entry.
Definition: extable.h:64
void do_sysenter(struct cpu_regs *regs)
May be implemented by a guest to handle SYSENTER invocations.
Definition: traps.c:76
#define _p(v)
Express an abitrary integer v as void *.
Definition: numbers.h:48
unsigned long exec_user_ss
Definition: traps.c:14
void do_exception(struct cpu_regs *regs)
Definition: traps.c:23
void xtf_exlog_log_exception(struct cpu_regs *regs)
Definition: exlog.c:39
#define X86_EXC_PF
Definition: processor.h:105
void do_evtchn(struct cpu_regs *regs)
May be implemented by a guest to handle Event Channel upcalls.
Definition: traps.c:81
#define __weak
Definition: compiler.h:15