direct-io.hg

view tools/libxc/xc_ptrace_core.c @ 12765:2dd4569e0640

[LIBXC] Add an error reporting API to the libxc library.

- An 'xc_error' struct is used to pass around error
details. Currently contains two members 'code' an enumeration of
error types, and 'message' a free text description of the specific
problem.

- The xc_get_last_error() method returns a const pointer to the
internal instance of this struct manged by libxc. By returning a
const pointer we can add extra members to the end of the struct at
any time without worrying about ABI of callers. This will let us
provide more fine-grained info if needed in the future.

- The xc_error instance is statically defined inside libxc and marked
__thread. This ensures that errors are recorded per-thread, and
that when dealing with errors we never need to call malloc - all
storage needed is statically allocated.

- The xc_clear_last_error() method resets any currently recorded
error details

- The xc_error_code_to_desc() method converts the integer error code
into a generic user facing messsage. eg "Invalid kernel". Together
with the 'message' field from xc_error, this provides the user
visible feedback. eg "Invalid kernel: Non PAE-kernel on PAE host."

- A callback can be registered with xc_set_error_handler to receive
notification whenever an error is recorded, rather than querying
for error details after the fact with xc_get_last_error

- If built with -DDEBUG set, a default error handler will be
registered which calls fprintf(stderr), thus maintaining current
behaviour of logging errors to stderr during developer builds.

- The python binding for libxc is updated to use xc_get_last_error
to pull out error details whenever appropriate, instead of
returning info based on 'errno'

- The xc_set_error method is private to libxc internals, and is used
for setting error details

- The ERROR and PERROR macros have been updated to call xc_set_error
automatically specifying XC_INTERNAL_ERROR as the error code. This
gives a generic error report for all current failure points

- Some uses of the ERROR macro have been replaced with explicit
calls to xc_set_error to enable finer grained error reporting. In
particular the code dealing with invalid kernel types uses this
to report about PAE/architecture/wordsize mismatches

The patch has been tested by calling xm create against a varietry of
config files defining invalid kernels of various kinds. It has also
been tested with libvirt talking to xend. In both cases the error
messages were propagated all the way back up the stack.

There is only one place where I need to do further work. The suspend
& restore APIs in Xend invoke external helper programs rather than
calling libxc directly. This means that error details are essentially
lost. Since there is already code in XenD which scans STDERR from
these programs I will investigate adapting this to extract actual
error messages from these helpers.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
author kfraser@localhost.localdomain
date Thu Dec 07 11:36:26 2006 +0000 (2006-12-07)
parents cfb1136ee8f7
children e2e6830269f7
line source
1 #include <sys/ptrace.h>
2 #include <sys/wait.h>
3 #include "xc_private.h"
4 #include "xg_private.h"
5 #include "xc_ptrace.h"
6 #include <time.h>
8 /* XXX application state */
10 static int current_is_hvm = 0;
11 static long nr_pages = 0;
12 static unsigned long *p2m_array = NULL;
13 static unsigned long *m2p_array = NULL;
14 static unsigned long pages_offset;
15 static unsigned long cr3[MAX_VIRT_CPUS];
17 /* --------------------- */
19 static unsigned long
20 map_mtop_offset(unsigned long ma)
21 {
22 return pages_offset + (m2p_array[ma >> PAGE_SHIFT] << PAGE_SHIFT);
23 return 0;
24 }
27 void *
28 map_domain_va_core(unsigned long domfd, int cpu, void *guest_va,
29 vcpu_guest_context_t *ctxt)
30 {
31 unsigned long pde, page;
32 unsigned long va = (unsigned long)guest_va;
33 void *v;
35 static unsigned long cr3_phys[MAX_VIRT_CPUS];
36 static unsigned long *cr3_virt[MAX_VIRT_CPUS];
37 static unsigned long pde_phys[MAX_VIRT_CPUS];
38 static unsigned long *pde_virt[MAX_VIRT_CPUS];
39 static unsigned long page_phys[MAX_VIRT_CPUS];
40 static unsigned long *page_virt[MAX_VIRT_CPUS];
42 if (cr3[cpu] != cr3_phys[cpu])
43 {
44 cr3_phys[cpu] = cr3[cpu];
45 if (cr3_virt[cpu])
46 munmap(cr3_virt[cpu], PAGE_SIZE);
47 v = mmap(
48 NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, domfd,
49 map_mtop_offset(xen_cr3_to_pfn(cr3_phys[cpu])));
50 if (v == MAP_FAILED)
51 {
52 perror("mmap failed");
53 return NULL;
54 }
55 cr3_virt[cpu] = v;
56 }
57 if ((pde = cr3_virt[cpu][l2_table_offset_i386(va)]) == 0) /* logical address */
58 return NULL;
59 if (current_is_hvm)
60 pde = p2m_array[pde >> PAGE_SHIFT] << PAGE_SHIFT;
61 if (pde != pde_phys[cpu])
62 {
63 pde_phys[cpu] = pde;
64 if (pde_virt[cpu])
65 munmap(pde_virt[cpu], PAGE_SIZE);
66 v = mmap(
67 NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, domfd,
68 map_mtop_offset(pde_phys[cpu]));
69 if (v == MAP_FAILED)
70 return NULL;
71 pde_virt[cpu] = v;
72 }
73 if ((page = pde_virt[cpu][l1_table_offset_i386(va)]) == 0) /* logical address */
74 return NULL;
75 if (current_is_hvm)
76 page = p2m_array[page >> PAGE_SHIFT] << PAGE_SHIFT;
77 if (page != page_phys[cpu])
78 {
79 page_phys[cpu] = page;
80 if (page_virt[cpu])
81 munmap(page_virt[cpu], PAGE_SIZE);
82 v = mmap(
83 NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, domfd,
84 map_mtop_offset(page_phys[cpu]));
85 if (v == MAP_FAILED)
86 {
87 IPRINTF("cr3 %lx pde %lx page %lx pti %lx\n", cr3[cpu], pde, page, l1_table_offset_i386(va));
88 page_phys[cpu] = 0;
89 return NULL;
90 }
91 page_virt[cpu] = v;
92 }
93 return (void *)(((unsigned long)page_virt[cpu]) | (va & BSD_PAGE_MASK));
94 }
96 int
97 xc_waitdomain_core(
98 int xc_handle,
99 int domfd,
100 int *status,
101 int options,
102 vcpu_guest_context_t *ctxt)
103 {
104 int nr_vcpus;
105 int i;
106 xc_core_header_t header;
108 if ( nr_pages == 0 )
109 {
110 if (read(domfd, &header, sizeof(header)) != sizeof(header))
111 return -1;
113 current_is_hvm = (header.xch_magic == XC_CORE_MAGIC_HVM);
114 if ( !current_is_hvm && (header.xch_magic != XC_CORE_MAGIC) )
115 {
116 IPRINTF("Magic number missmatch: 0x%08x (file) != "
117 " 0x%08x (code)\n", header.xch_magic,
118 XC_CORE_MAGIC);
119 return -1;
120 }
122 nr_pages = header.xch_nr_pages;
123 nr_vcpus = header.xch_nr_vcpus;
124 pages_offset = header.xch_pages_offset;
126 if (read(domfd, ctxt, sizeof(vcpu_guest_context_t)*nr_vcpus) !=
127 sizeof(vcpu_guest_context_t)*nr_vcpus)
128 return -1;
130 for (i = 0; i < nr_vcpus; i++)
131 cr3[i] = ctxt[i].ctrlreg[3];
133 if ((p2m_array = malloc(nr_pages * sizeof(unsigned long))) == NULL)
134 {
135 IPRINTF("Could not allocate p2m_array\n");
136 return -1;
137 }
139 if (read(domfd, p2m_array, sizeof(unsigned long)*nr_pages) !=
140 sizeof(unsigned long)*nr_pages)
141 return -1;
143 if ((m2p_array = malloc((1<<20) * sizeof(unsigned long))) == NULL)
144 {
145 IPRINTF("Could not allocate m2p array\n");
146 return -1;
147 }
148 bzero(m2p_array, sizeof(unsigned long)* 1 << 20);
150 for (i = 0; i < nr_pages; i++)
151 m2p_array[p2m_array[i]] = i;
152 }
153 return 0;
154 }
156 /*
157 * Local variables:
158 * mode: C
159 * c-set-style: "BSD"
160 * c-basic-offset: 4
161 * tab-width: 4
162 * indent-tabs-mode: nil
163 * End:
164 */