]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x64/misc.cc
Switch to a simple X11-style license.
[polintos/scott/priv.git] / kernel / arch / x64 / misc.cc
1 // arch/x64/misc.cc -- Misc. arch-specific stuff
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
21 // SOFTWARE.
22
23 #include <kern/types.h>
24 #include <kern/libc.h>
25 #include <kern/arch.h>
26 #include <kern/i8259.h>
27 #include <kern/time.h>
28 #include <kern/thread.h>
29 #include <kern/mem.h>
30
31 #include <arch/addrs.h>
32 #include <arch/multiboot.h>
33
34 extern u64 x64_init_ptbl_l4[512];
35
36 struct X64InitStack {
37         u8 stack[4096 - ::Threads::thread_size];
38         ::Threads::Thread thread;
39 } __attribute__((aligned(4096))) x64_init_stack;
40
41 namespace Arch {
42         namespace Priv {
43                 void set_idt();
44                 
45                 void show_regs(u64 *stack) {
46                         for (int i = 0; i < 16; i += 4)
47                                 printf("r%02d 0x%016llx 0x%016llx 0x%016llx 0x%016llx\n",
48                                        i, stack[i], stack[i + 1], stack[i + 2], stack[i + 3]);
49                         
50                         printf("orig rsp: 0x%016llx rflags: 0x%016llx\n",
51                                stack[19], stack[18]);
52                         
53                         printf("Thread %p (%s)\n", curthread, curthread->name);
54                         
55                         printf("Stack trace:       ");
56                         u64 *frame = (u64 *)stack[5];
57                         
58                         for (int i = 1; i < 32; i++) {
59                                 u64 stackptr = frame[1];
60                                 frame = (u64 *)frame[0];
61                                 
62                                 if ((u64)frame < 0xffff800000000000)
63                                         break;
64
65                                 if (!(i & 3))
66                                         printf("\n");
67                                 
68                                 printf("0x%016llx ", stackptr);
69                         }
70                 }
71
72                 struct TimerInt : public IRQ::Interrupt {
73                         bool action()
74                         {
75                                 Time::monotonic_timers->run();
76                                 return true;
77                         }
78                 };
79                 
80                 TimerInt timer_int;
81         }
82
83         using IRQ::i8259;
84         ::Threads::Thread *init_thread;
85                 
86         void arch_init()
87         {
88                 init_thread = &x64_init_stack.thread;
89                 Priv::early_adjust_mappings();
90                 Priv::set_idt();
91                 Priv::MultiBoot::process_info();
92                 i8259.init();
93                 
94                 u64 tss_addr = reinterpret_cast<u64>(&Priv::tss) + 4;
95                 x64_gdt[4].base_low = tss_addr & 0xffff;
96                 x64_gdt[4].base_mid = (tss_addr & 0xff0000) >> 16;
97                 x64_gdt[4].base_high = (tss_addr & 0xff000000) >> 24;
98                 
99                 asm volatile("ltr %w0" : : "r" (0x20) : "memory");
100                 init_thread->addr_space = new Mem::AddrSpace(x64_init_ptbl_l4);
101                 init_thread->active_addr_space = init_thread->addr_space;
102         }
103
104         void timer_init()
105         {
106                 IRQ::InterruptSlot *timer = i8259.get_slot(0);
107                 i8259.request_int(timer, &Priv::timer_int);
108         }
109         
110         void ArchThread::init(void *entry, void *arg)
111         {
112                 void **stack = reinterpret_cast<void **>(this);
113                 
114                 *--stack = arg;
115                 *--stack = entry;
116                 
117                 rsp = stack;
118                 rbp = 0;
119                 jump_to_init = 1;
120         }
121 }
122
123 using Arch::Priv::show_regs;
124
125 extern "C" void x64_do_diverr(u64 *stack)
126 {
127         printf("Division error at 0x%04llx:0x%016llx\n", stack[17], stack[16]);
128         show_regs(stack);
129         for(;;);
130 }
131
132 extern "C" void x64_do_invalid_insn(u64 *stack)
133 {
134         printf("Invalid instruction at 0x%04llx:0x%016llx\n", stack[17], stack[16]);
135         show_regs(stack);
136         for(;;);
137 }
138
139 extern "C" void x64_do_page_fault(u64 *stack, u64 fault_addr, u32 error_code)
140 {
141         Mem::AddrSpace *as;
142         
143         if (in_fault)
144                 for(;;);
145                 
146         // A reserved bit was set in the PTE; this is always a bug.
147         if (error_code & 8)
148                 goto bad_fault;
149         
150         // Don't try to fix up a page fault if interrupts were disabled.  It is an
151         // error to access non-locked pages with interrupts disabled.  Trying to
152         // fix it up in the case of an access that would be legitimate if interrupts
153         // were enabled would simply mask the loss of atomicity, and trying to grab
154         // locks to look up the address if it is a completely bad reference won't
155         // accomplish much other than decreasing the odds that the fault message
156         // gets out.
157         
158         if (!(stack[18] & 0x200))
159                 goto bad_fault;
160         
161         // Don't allow fault-ins using a borrowed addr-space.
162         as = curthread->addr_space;
163
164         if (!as || curthread == Arch::init_thread)
165                 goto bad_fault;
166         
167         ll_ints_on();
168         
169         // FIXME: no-exec
170         if (as->handle_fault(fault_addr, error_code & 2,
171                              false /* error_code & 16 */, error_code & 4))
172                 return;
173
174         // FIXME: throw exception to user
175
176 bad_fault:
177         ll_ints_off();
178         in_fault++;
179         
180         printf("Page fault at 0x%04llx:0x%016llx for 0x%016llx\n",
181                stack[17], stack[16], fault_addr);
182         printf("Error code: 0x%04x\n", error_code);
183         
184         show_regs(stack);
185
186         for(;;);
187 }
188
189 extern "C" void x64_do_gpf(u64 *stack, u32 error_code)
190 {
191         if (in_fault)
192                 for(;;);
193         
194         in_fault++;
195
196         printf("General protection fault at 0x%04llx:0x%016llx, "
197                "Error code: 0x%04x\n",
198                stack[17], stack[16], error_code);
199
200         show_regs(stack);
201         
202         for(;;);
203 }
204
205 extern "C" void x64_do_irq(int irq)
206 {
207         IRQ::i8259.handle_irq(irq - 0x20);
208 }