]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/kern/ia32/32/trap_state.cpp
update
[l4.git] / kernel / fiasco / src / kern / ia32 / 32 / trap_state.cpp
1
2 INTERFACE:
3
4 #include "l4_types.h"
5
6 class Trap_state
7 {
8   friend class Jdb_tcb;
9   friend class Jdb_stack_view;
10
11 public:
12   typedef FIASCO_FASTCALL int (*Handler)(Trap_state*, unsigned cpu);
13   static Handler base_handler asm ("BASE_TRAP_HANDLER");
14
15   // Saved segment registers
16   Mword  _es;
17   Mword  _ds;
18   Mword  _gs;                                     // => utcb->values[ 0]
19   Mword  _fs;                                     // => utcb->values[ 1]
20
21   // PUSHA register state frame
22   Mword  _di;                                     // => utcb->values[ 2]
23   Mword  _si;                                     // => utcb->values[ 3]
24   Mword  _bp;                                     // => utcb->values[ 4]
25   Mword  _cr2; // we save cr2 over esp for PFs    // => utcb->values[ 5]
26   Mword  _bx;                                     // => utcb->values[ 6]
27   Mword  _dx;                                     // => utcb->values[ 7]
28   Mword  _cx;                                     // => utcb->values[ 8]
29   Mword  _ax;                                     // => utcb->values[ 9]
30
31   // Processor trap number, 0-31
32   Mword  _trapno;                                 // => utcb->values[10]
33
34   // Error code pushed by the processor, 0 if none
35   Mword  _err;                                    // => utcb->values[11]
36
37 protected:
38   // Processor state frame
39   Mword  _ip;                                     // => utcb->values[12]
40   Mword  _cs;                                     // => utcb->values[13]
41   Mword  _flags;                                  // => utcb->values[14]
42   Mword  _sp;                                     // => utcb->values[15]
43   Mword  _ss;
44 };
45
46
47 //---------------------------------------------------------------------------
48 IMPLEMENTATION [ia32]:
49
50 #include "regdefs.h"
51 #include "gdt.h"
52
53 PUBLIC inline NEEDS["regdefs.h", "gdt.h"]
54 void
55 Trap_state::sanitize_user_state()
56 {
57   _cs = Gdt::gdt_code_user | Gdt::Selector_user;
58   _ss = Gdt::gdt_data_user | Gdt::Selector_user;
59   _flags = (_flags & ~(EFLAGS_IOPL | EFLAGS_NT)) | EFLAGS_IF;
60 }
61
62 //---------------------------------------------------------------------------
63 IMPLEMENTATION [ux]:
64
65 #include "emulation.h"
66 #include "regdefs.h"
67
68 PUBLIC inline NEEDS["emulation.h", "regdefs.h"]
69 void
70 Trap_state::sanitize_user_state()
71 {
72   _cs = Emulation::kernel_cs() & ~1;
73   _ss = Emulation::kernel_ss();
74   _flags = (_flags & ~(EFLAGS_IOPL | EFLAGS_NT)) | EFLAGS_IF;
75 }
76
77 //---------------------------------------------------------------------------
78 IMPLEMENTATION [ia32 || ux]:
79
80 #include <cstdio>
81 #include <panic.h>
82 #include "cpu.h"
83 #include "atomic.h"
84
85 Trap_state::Handler Trap_state::base_handler FIASCO_FASTCALL;
86
87 PUBLIC inline
88 void
89 Trap_state::set_ipc_upcall()
90 {
91   _err = 0;
92   _trapno = 0xfe;
93 }
94
95 PUBLIC inline
96 void
97 Trap_state::set_pagefault(Mword pfa, Mword error)
98 {
99   _cr2 = pfa;
100   _trapno = 0xe;
101   _err = error;
102 }
103
104 PUBLIC inline
105 Mword
106 Trap_state::trapno() const
107 { return _trapno; }
108
109 PUBLIC inline
110 Mword
111 Trap_state::error() const
112 { return _err; }
113
114 PUBLIC inline
115 Mword
116 Trap_state::ip() const
117 { return _ip; }
118
119 PUBLIC inline
120 Mword
121 Trap_state::cs() const
122 { return _cs; }
123
124 PUBLIC inline
125 Mword
126 Trap_state::flags() const
127 { return _flags; }
128
129 PUBLIC inline
130 Mword
131 Trap_state::sp() const
132 { return _sp; }
133
134 PUBLIC inline
135 Mword
136 Trap_state::ss() const
137 { return _ss; }
138
139 PUBLIC inline
140 Mword
141 Trap_state::value() const
142 { return _ax; }
143
144 PUBLIC inline
145 Mword
146 Trap_state::value2() const
147 { return _cx; }
148
149 PUBLIC inline
150 Mword
151 Trap_state::dx() const
152 { return _dx; }
153
154 PUBLIC inline
155 Mword
156 Trap_state::value3() const
157 { return _dx; }
158
159 PUBLIC inline
160 Mword
161 Trap_state::value4() const
162 { return _bx; }
163
164 PUBLIC inline
165 void
166 Trap_state::ip(Mword ip)
167 { _ip = ip; }
168
169 PUBLIC inline
170 void
171 Trap_state::cs(Mword cs)
172 { _cs = cs; }
173
174 PUBLIC inline
175 void
176 Trap_state::flags(Mword flags)
177 { _flags = flags; }
178
179 PUBLIC inline
180 void
181 Trap_state::sp(Mword sp)
182 { _sp = sp; }
183
184 PUBLIC inline
185 void
186 Trap_state::ss(Mword ss)
187 { _ss = ss; }
188
189 PUBLIC inline
190 void
191 Trap_state::value(Mword value)
192 { _ax = value; }
193
194 PUBLIC inline
195 void
196 Trap_state::value3(Mword value)
197 { _dx = value; }
198
199 PUBLIC inline NEEDS["atomic.h"] 
200 void
201 Trap_state::consume_instruction(unsigned count)
202 { cas ((Address*)(&_ip), _ip, _ip + count); }
203
204 PUBLIC inline
205 bool
206 Trap_state::is_debug_exception() const
207 { return _trapno == 1 || _trapno == 3; }
208
209 PUBLIC
210 void
211 Trap_state::dump()
212 {
213   int from_user = _cs & 3;
214
215   printf("EAX %08lx EBX %08lx ECX %08lx EDX %08lx\n"
216          "ESI %08lx EDI %08lx EBP %08lx ESP %08lx\n"
217          "EIP %08lx EFLAGS %08lx\n"
218          "CS %04lx SS %04lx DS %04lx ES %04lx FS %04lx GS %04lx\n"
219          "trap %ld (%s), error %08lx, from %s mode\n",
220          _ax, _bx, _cx, _dx,
221          _si, _di, _bp, from_user ? _sp : (Unsigned32)&_sp,
222          _ip, _flags,
223          _cs & 0xffff, from_user ? _ss & 0xffff : Cpu::get_ss() & 0xffff,
224          _ds & 0xffff, _es & 0xffff, _fs & 0xffff, _gs & 0xffff,
225          _trapno, Cpu::exception_string(_trapno), _err, from_user ? "user" : "kernel");
226
227   if (_trapno == 13)
228     {
229       if (_err & 1)
230         printf("(external event");
231       else
232         printf("(internal event");
233       if (_err & 2)
234         printf(" regarding IDT gate descriptor no. 0x%02lx)\n", _err >> 3);
235       else
236         printf(" regarding %s entry no. 0x%02lx)\n",
237               _err & 4 ? "LDT" : "GDT", _err >> 3);
238     }
239   else if (_trapno == 14)
240     printf("page fault linear address %08lx\n", _cr2);
241 }
242
243 extern "C" FIASCO_FASTCALL
244 void
245 trap_dump_panic(Trap_state *ts)
246 {
247   ts->dump();
248   panic("terminated due to trap");
249 }
250
251 PUBLIC inline
252 bool
253 Trap_state::exclude_logging()
254 { return _trapno == 1 || _trapno == 3; }