lguest32 kallsyms backtrace of guest.
PATCH 1/1 lguest32 kallsyms backtrace of guest.
This is taken from the work I did on lguest64.
When killing a guest, we read the guest stack to do a nice back trace of the guest and send it via printk to the host.
So instead of just getting an error message from the lguest launcher of:
lguest: bad read address 537012178 len 1
I also get in my dmesg:
called from [<c0405f30>] show_trace_log_lvl+0x1a/0x2f
[<c04069aa>] show_trace+0x12/0x14 [<c0406a03>] dump_stack+0x16/0x18 [<f8c746de>] lguest_dump_lg_regs+0x22/0x13c [lg] [<f8c7131b>] lgread+0x59/0x90 [lg] [<f8c715bd>] run_guest+0x26b/0x406 [lg] [<f8c739be>] read+0x73/0x7d [lg] [<c04825e9>] vfs_read+0xad/0x161 [<c0482a75>] sys_read+0x3d/0x61 [<c0404f34>] syscall_call+0x7/0xb ======================= [<f8c7131b>] lgread+0x59/0x90 [lg]
Printing LG 0 regs cr3: 021eb000
EIP: 0061: [<00000000e00227d2>]
ESP: 0069:c236fe3c EFLAGS: 00010202
EAX: 00000004 EBX: e001fb20 ECX: 00000008 EDX: 000003f2
ESI: e001ee00 EDI: e001fb60 EBP: c236fea0
CR2: 1278000 lguest_data->cr2: 80011380
errcode: 0 trapnum: d
Stack Dump:
[<00000000c1042b7a>] trace_hardirqs_on+0x125/0x149 [<00000000c123b0ea>] wait_for_completion+0x90/0x98 [<00000000c123bddc>] __mutex_unlock_slowpath+0x129/0x13e [<00000000c1048769>] unlock_cpu_hotplug+0x62/0x64 [<00000000c104b5b6>] sys_init_module+0x14e3/0x162c [<00000000c1081d44>] do_sync_read+0xc2/0xff [<00000000c1004f85>] restore_nocheck+0x12/0x15 [<00000000c1004f34>] syscall_call+0x7/0xb
TODO:
- Clean up a little (still has stuff from lguest64 in it).
- Perhaps make a config option or runtime switch to turn it off.
- Send to the launcher the dump instead of printk.
- make modules work too.
Also I need to change the %u of the bad read print to a %x, because seeing 0x200227d2 is better than seeing 537012178 for addresses.
drivers/lguest/Makefile +1 -1
@@ -4,4 +4,4 @@ obj-$(CONFIG_LGUEST_GUEST) += lguest.o l # Host requires the other files, which can be a module. obj-$(CONFIG_LGUEST) += lg.o lg-objs := core.o hypercalls.o page_tables.o interrupts_and_traps.o \ - segments.o io.o lguest_user.o hypervisor.o + segments.o io.o lguest_user.o hypervisor.o lguest_debug.o
drivers/lguest/core.c +22 -0
@@ -210,6 +210,28 @@ int lguest_address_ok(const struct lgues }/* Just like get_user, but don't let guest access lguest binary. */ +u8 lgread_u8 (struct lguest *lg,u32 addr) +{ +u8 val =0 ; + +/* Don't let them access lguest binary */ +if (!lguest_address_ok (lg, addr) + ||get_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad read address %u" , addr); +return val; +} + +u16 lgread_u16 (struct lguest *lg,u32 addr) +{ +u16 val =0 ; + +/* Don't let them access lguest binary */ +if (!lguest_address_ok (lg, addr) + ||get_user (val, (u32 __user *)addr) !=0 ) +kill_guest (lg,"bad read address %u" , addr); +return val; +} +u32 lgread_u32 (struct lguest *lg,u32 addr) {u32 val =0 ;
drivers/lguest/lg.h +9 -0
@@ -176,6 +176,8 @@ extern struct mutex lguest_lock;/* core.c: */ /* Entry points in hypervisor */ const unsigned long *__lguest_default_idt_entries (void ); +u8 lgread_u8 (struct lguest *lg,u32 addr); +u16 lgread_u16 (struct lguest *lg,u32 addr);u32 lgread_u32 (struct lguest *lg,u32 addr);void lgwrite_u32 (struct lguest *lg,u32 val,u32 addr);void lgread (struct lguest *lg,void *buf,u32 addr,unsigned bytes);
@@ -238,6 +240,7 @@ int hypercall(struct lguest *info, struc #define kill_guest(lg, fmt...) \ do { \ if (!(lg)->dead) { \ + lguest_dump_lg_regs(lg); \ (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \ if (!(lg)->dead) \ (lg)->dead = (void *)1; \
@@ -248,5 +251,11 @@ static inline unsigned long guest_pa(str {return vaddr - lg->page_offset; } + +/* lguest_debug.c */ +void lguest_print_address (struct lguest *lg,unsigned long address); +void lguest_dump_trace (struct lguest *lg,struct lguest_regs *regs); +void lguest_dump_lg_regs (struct lguest *lg); +#endif /* __ASSEMBLY__ */ #endif /* _LGUEST_H */
drivers/lguest/lguest.c +25 -0
@@ -48,6 +48,14 @@ extern const char lgstart_iret[], lgend_ extern asmlinkage void lguest_maybe_init(void);extern void lguest_iret (void ); +/* For back tracing from the HV */ +extern const unsigned long kallsyms_addresses[] __attribute__((weak)); +extern const unsigned long kallsyms_num_syms __attribute__((weak)); +extern const u8 kallsyms_names[] __attribute__((weak)); +extern const u8 kallsyms_token_table[] __attribute__((weak)); +extern const u16 kallsyms_token_index[] __attribute__((weak)); +extern const unsigned long kallsyms_markers[] __attribute__((weak)); +struct lguest_data lguest_data = { .hcall_status = { [0 ...LHCALL_RING_SIZE -1 ] =0xFF }, .noirq_start = (u32 )lguest_noirq_start,
@@ -56,6 +64,7 @@ struct lguest_data lguest_data = { };struct lguest_device_desc *lguest_devices;static __initdata const struct lguest_boot_info *boot =__va (0 ); +static struct lguest_text_ptr code_stack[2 ];static int lazy_mode;static void lguest_lazy_mode (int mode)
@@ -468,6 +477,22 @@ __init void lguest_init(void) paravirt_ops.set_lazy_mode = lguest_lazy_mode; paravirt_ops.wbinvd = lguest_wbinvd; + code_stack[0 ].next =__pa (&code_stack[1 ]); + code_stack[0 ].start = (unsigned long )_stext; + code_stack[0 ].end = (unsigned long )_etext; + code_stack[1 ].next =0 ; + code_stack[1 ].start = (unsigned long )_sinittext; + code_stack[1 ].end = (unsigned long )_einittext; + + lguest_data.text =__pa (&code_stack[0 ]); + + lguest_data.kallsyms_addresses =__pa (&kallsyms_addresses); + lguest_data.kallsyms_num_syms = kallsyms_num_syms; + lguest_data.kallsyms_names =__pa (&kallsyms_names); + lguest_data.kallsyms_token_table =__pa (&kallsyms_token_table); + lguest_data.kallsyms_token_index =__pa (&kallsyms_token_index); + lguest_data.kallsyms_markers =__pa (&kallsyms_markers); +hcall (LHCALL_LGUEST_INIT ,__pa (&lguest_data),0 ,0 );strncpy (boot_command_line, boot->cmdline,COMMAND_LINE_SIZE );
drivers/lguest/lguest_debug.c new file +458 -0
458 lines not on this page · raw message
include/linux/lguest.h +17 -0
@@ -54,6 +54,13 @@ struct hcall_ringu32 eax, edx, ebx, ecx; }; +/* for kallsyms dumping */ +struct lguest_text_ptr { +unsigned long next;/* guest pa address of next pointer */ +unsigned long start; +unsigned long end; +}; +/* All the good stuff happens here: guest registers it with LGUEST_INIT */ struct lguest_data {
@@ -76,6 +83,16 @@ struct lguest_data/* ID of this guest (used by network driver to set ethernet address) */ u16 guestid; +/* pa address of lguest_text_ptr addresses */ +unsigned long text; +/* If the kernel has kallsyms, we can use it to do backtraces of a guest */ +unsigned long kallsyms_addresses; +unsigned long kallsyms_num_syms; +unsigned long kallsyms_names; +unsigned long kallsyms_token_table; +unsigned long kallsyms_token_index; +unsigned long kallsyms_markers; +/* Fields initialized by the guest at boot: */ /* Instruction range to suppress interrupts even if enabled */ unsigned long noirq_start, noirq_end;
Quoted 5 lines · Steven Rostedt
This is taken from the work I did on lguest64.
When killing a guest, we read the guest stack to do a nice back trace of the guest and send it via printk to the host.
Quoted 7 lines
So instead of just getting an error message from the lguest launcher of:
lguest: bad read address 537012178 len 1
I also get in my dmesg:
called from [<
c0405f30>]show_trace_log_lvl+0x1a/0x2f
Hi Steven,
This is a cool idea, but there are two issues with this patch. The
first is that it's 500 lines of code: that's around +10% on lguest's total code size! The second is that it conflicts with the medium-term plan to allow any user to run up lguests: this is why lg.ko never printk()s about problems with the guest.
While it is useful for cases where a guest dies mysteriously before it brings up the console, three alternatives come to mind:
1) Modify early_printk so Guests can use it.
2) Have a separate tool(-set?) for this kind of post-mortem. Then you just have to implement guest suspend! 8)
3) Put this in a CONFIG_LGUEST_DEBUG.
Note that options 1 or 2 make you do more work, but are probably better in the long term. I'm happy for #3 to sit as a patch in the tree for the duration, tho!
Cheers,
Rusty.
Quoted 6 lines · Rusty Russell
This is a cool idea, but there are two issues with this patch. The
first is that it's 500 lines of code: that's around +10% on lguest's total code size! The second is that it conflicts with the medium-term plan to allow any user to run up lguests: this is why lg.ko never printk()s about problems with the guest.
Not much I can do about the size, but it's in the debug section so hopefully it's not considered too bad :)
Quoted 4 lines
While it is useful for cases where a guest dies mysteriously before it brings up the console, three alternatives come to mind:
Quoted 8 lines
1) Modify
early_printkso Guests can use it.
2) Have a separate tool(-set?) for this kind of post-mortem. Then you just have to implement guest suspend! 8)
3) Put this in aCONFIG_LGUEST_DEBUG.Note that options 1 or 2 make you do more work, but are probably better in the long term. I'm happy for #3 to sit as a patch in the tree for the duration, tho!
OK, I'll make a #3 patch to send, but the #1 looks best. Not to mention that I still need to make it so that the console can read it.
-- Steve