ia64/xen-unstable

view xen/arch/x86/x86_64/traps.c @ 3761:118e0a3af9b0

bitkeeper revision 1.1159.1.564 (420b44edsb8XzPev-TiGW16GSsCW6g)

More x86_64 stuff. Added hypercalls to register a user-space pagetable,
modify FS/GS base addresses, and switch to user mode. User mode switches
back to kernel mode automatically on executing SYSCALL instruction.
Still todo: 1. getdomaininfo needs to include pagetable_user
2. get writable and shadow pagetables working
3. testing
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@scramble.cl.cam.ac.uk
date Thu Feb 10 11:26:37 2005 +0000 (2005-02-10)
parents f5f2757b3aa2
children 0a4b76b6b5a0 d8506fb506ed
line source
2 #include <xen/config.h>
3 #include <xen/init.h>
4 #include <xen/sched.h>
5 #include <xen/lib.h>
6 #include <xen/errno.h>
7 #include <xen/mm.h>
8 #include <xen/irq.h>
9 #include <xen/console.h>
10 #include <xen/sched.h>
11 #include <asm/msr.h>
13 static int kstack_depth_to_print = 8*20;
15 static inline int kernel_text_address(unsigned long addr)
16 {
17 if (addr >= (unsigned long) &_stext &&
18 addr <= (unsigned long) &_etext)
19 return 1;
20 return 0;
22 }
24 void show_guest_stack(void)
25 {
26 int i;
27 execution_context_t *ec = get_execution_context();
28 unsigned long *stack = (unsigned long *)ec->rsp;
29 printk("Guest RIP is %lx\n ", ec->rip);
31 for ( i = 0; i < kstack_depth_to_print; i++ )
32 {
33 if ( ((long)stack & (STACK_SIZE-1)) == 0 )
34 break;
35 if ( i && ((i % 8) == 0) )
36 printk("\n ");
37 printk("%p ", *stack++);
38 }
39 printk("\n");
41 }
43 void show_trace(unsigned long *rsp)
44 {
45 unsigned long *stack, addr;
46 int i;
48 printk("Call Trace from RSP=%p:\n ", rsp);
49 stack = rsp;
50 i = 0;
51 while (((long) stack & (STACK_SIZE-1)) != 0) {
52 addr = *stack++;
53 if (kernel_text_address(addr)) {
54 if (i && ((i % 6) == 0))
55 printk("\n ");
56 printk("[<%p>] ", addr);
57 i++;
58 }
59 }
60 printk("\n");
61 }
63 void show_stack(unsigned long *rsp)
64 {
65 unsigned long *stack;
66 int i;
68 printk("Stack trace from RSP=%p:\n ", rsp);
70 stack = rsp;
71 for ( i = 0; i < kstack_depth_to_print; i++ )
72 {
73 if ( ((long)stack & (STACK_SIZE-1)) == 0 )
74 break;
75 if ( i && ((i % 8) == 0) )
76 printk("\n ");
77 if ( kernel_text_address(*stack) )
78 printk("[%p] ", *stack++);
79 else
80 printk("%p ", *stack++);
81 }
82 printk("\n");
84 show_trace(rsp);
85 }
87 void show_registers(struct xen_regs *regs)
88 {
89 printk("CPU: %d\nEIP: %04lx:[<%p>] \nEFLAGS: %p\n",
90 smp_processor_id(), 0xffff & regs->cs, regs->rip, regs->eflags);
91 printk("rax: %p rbx: %p rcx: %p rdx: %p\n",
92 regs->rax, regs->rbx, regs->rcx, regs->rdx);
93 printk("rsi: %p rdi: %p rbp: %p rsp: %p\n",
94 regs->rsi, regs->rdi, regs->rbp, regs->rsp);
95 printk("r8: %p r9: %p r10: %p r11: %p\n",
96 regs->r8, regs->r9, regs->r10, regs->r11);
97 printk("r12: %p r13: %p r14: %p r15: %p\n",
98 regs->r12, regs->r13, regs->r14, regs->r15);
100 show_stack((unsigned long *)regs->rsp);
101 }
103 void show_page_walk(unsigned long addr)
104 {
105 unsigned long page = read_cr3();
107 printk("Pagetable walk from %p:\n", addr);
109 page &= PAGE_MASK;
110 page = ((unsigned long *) __va(page))[l4_table_offset(addr)];
111 printk(" L4 = %p\n", page);
112 if ( !(page & _PAGE_PRESENT) )
113 return;
115 page &= PAGE_MASK;
116 page = ((unsigned long *) __va(page))[l3_table_offset(addr)];
117 printk(" L3 = %p\n", page);
118 if ( !(page & _PAGE_PRESENT) )
119 return;
121 page &= PAGE_MASK;
122 page = ((unsigned long *) __va(page))[l2_table_offset(addr)];
123 printk(" L2 = %p %s\n", page, (page & _PAGE_PSE) ? "(2MB)" : "");
124 if ( !(page & _PAGE_PRESENT) || (page & _PAGE_PSE) )
125 return;
127 page &= PAGE_MASK;
128 page = ((unsigned long *) __va(page))[l1_table_offset(addr)];
129 printk(" L1 = %p\n", page);
130 }
132 asmlinkage void double_fault(void);
133 asmlinkage void do_double_fault(struct xen_regs *regs)
134 {
135 /* Disable the NMI watchdog. It's useless now. */
136 watchdog_on = 0;
138 console_force_unlock();
140 /* Find information saved during fault and dump it to the console. */
141 printk("************************************\n");
142 show_registers(regs);
143 printk("************************************\n");
144 printk("CPU%d DOUBLE FAULT -- system shutdown\n", smp_processor_id());
145 printk("System needs manual reset.\n");
146 printk("************************************\n");
148 /* Lock up the console to prevent spurious output from other CPUs. */
149 console_force_lock();
151 /* Wait for manual reset. */
152 for ( ; ; )
153 __asm__ __volatile__ ( "hlt" );
154 }
156 asmlinkage void syscall_enter(void);
157 void __init percpu_traps_init(void)
158 {
159 char *stack_bottom, *stack;
160 int cpu = smp_processor_id();
162 stack_bottom = (char *)get_stack_bottom();
163 stack = (char *)((unsigned long)stack_bottom & ~(STACK_SIZE - 1));
165 /* Double-fault handler has its own per-CPU 1kB stack. */
166 init_tss[cpu].ist[0] = (unsigned long)&stack[1024];
167 set_intr_gate(TRAP_double_fault, &double_fault);
168 idt_tables[cpu][TRAP_double_fault].a |= 1UL << 32; /* IST1 */
170 /* NMI handler has its own per-CPU 1kB stack. */
171 init_tss[cpu].ist[1] = (unsigned long)&stack[2048];
172 idt_tables[cpu][TRAP_nmi].a |= 2UL << 32; /* IST2 */
174 /*
175 * Trampoline for SYSCALL entry from long mode.
176 */
178 /* Skip the NMI and DF stacks. */
179 stack = &stack[2048];
180 wrmsr(MSR_LSTAR, (unsigned long)stack, ((unsigned long)stack>>32));
182 /* movq %rsp, saversp(%rip) */
183 stack[0] = 0x48;
184 stack[1] = 0x89;
185 stack[2] = 0x25;
186 *(u32 *)&stack[3] = (stack_bottom - &stack[7]) - 16;
188 /* leaq saversp(%rip), %rsp */
189 stack[7] = 0x48;
190 stack[8] = 0x8d;
191 stack[9] = 0x25;
192 *(u32 *)&stack[10] = (stack_bottom - &stack[14]) - 16;
194 /* jmp syscall_enter */
195 stack[14] = 0xe9;
196 *(u32 *)&stack[15] = (char *)syscall_enter - &stack[19];
198 /*
199 * Trampoline for SYSCALL entry from compatibility mode.
200 */
202 /* Skip the long-mode entry trampoline. */
203 stack = &stack[19];
204 wrmsr(MSR_CSTAR, (unsigned long)stack, ((unsigned long)stack>>32));
206 /* movq %rsp, saversp(%rip) */
207 stack[0] = 0x48;
208 stack[1] = 0x89;
209 stack[2] = 0x25;
210 *(u32 *)&stack[3] = (stack_bottom - &stack[7]) - 16;
212 /* leaq saversp(%rip), %rsp */
213 stack[7] = 0x48;
214 stack[8] = 0x8d;
215 stack[9] = 0x25;
216 *(u32 *)&stack[10] = (stack_bottom - &stack[14]) - 16;
218 /* jmp syscall_enter */
219 stack[14] = 0xe9;
220 *(u32 *)&stack[15] = (char *)syscall_enter - &stack[19];
222 /*
223 * Common SYSCALL parameters.
224 */
226 wrmsr(MSR_STAR, 0, (FLAT_RING3_CS32<<16) | __HYPERVISOR_CS);
227 wrmsr(MSR_SYSCALL_MASK, ~EF_IE, 0U); /* disable interrupts */
228 }
230 void *decode_reg(struct xen_regs *regs, u8 b)
231 {
232 switch ( b )
233 {
234 case 0: return &regs->rax;
235 case 1: return &regs->rcx;
236 case 2: return &regs->rdx;
237 case 3: return &regs->rbx;
238 case 4: return &regs->rsp;
239 case 5: return &regs->rbp;
240 case 6: return &regs->rsi;
241 case 7: return &regs->rdi;
242 case 8: return &regs->r8;
243 case 9: return &regs->r9;
244 case 10: return &regs->r10;
245 case 11: return &regs->r11;
246 case 12: return &regs->r12;
247 case 13: return &regs->r13;
248 case 14: return &regs->r14;
249 case 15: return &regs->r15;
250 }
252 return NULL;
253 }