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