Statistics
| Branch: | Revision:

root / xen-all.c @ 9f4facbc

History | View | Annotate | Download (28 kB)

1 3285cf4f Anthony PERARD
/*
2 3285cf4f Anthony PERARD
 * Copyright (C) 2010       Citrix Ltd.
3 3285cf4f Anthony PERARD
 *
4 3285cf4f Anthony PERARD
 * This work is licensed under the terms of the GNU GPL, version 2.  See
5 3285cf4f Anthony PERARD
 * the COPYING file in the top-level directory.
6 3285cf4f Anthony PERARD
 *
7 3285cf4f Anthony PERARD
 */
8 3285cf4f Anthony PERARD
9 9ce94e7c Arun Sharma
#include <sys/mman.h>
10 9ce94e7c Arun Sharma
11 41445300 Anthony PERARD
#include "hw/pci.h"
12 c9622478 Anthony PERARD
#include "hw/pc.h"
13 3285cf4f Anthony PERARD
#include "hw/xen_common.h"
14 3285cf4f Anthony PERARD
#include "hw/xen_backend.h"
15 3285cf4f Anthony PERARD
16 b4dd7802 Anthony PERARD
#include "range.h"
17 432d268c Jun Nakajima
#include "xen-mapcache.h"
18 432d268c Jun Nakajima
#include "trace.h"
19 432d268c Jun Nakajima
20 9ce94e7c Arun Sharma
#include <xen/hvm/ioreq.h>
21 9ce94e7c Arun Sharma
#include <xen/hvm/params.h>
22 8a369e20 Anthony PERARD
#include <xen/hvm/e820.h>
23 9ce94e7c Arun Sharma
24 9ce94e7c Arun Sharma
//#define DEBUG_XEN
25 9ce94e7c Arun Sharma
26 9ce94e7c Arun Sharma
#ifdef DEBUG_XEN
27 9ce94e7c Arun Sharma
#define DPRINTF(fmt, ...) \
28 9ce94e7c Arun Sharma
    do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
29 9ce94e7c Arun Sharma
#else
30 9ce94e7c Arun Sharma
#define DPRINTF(fmt, ...) \
31 9ce94e7c Arun Sharma
    do { } while (0)
32 9ce94e7c Arun Sharma
#endif
33 9ce94e7c Arun Sharma
34 9ce94e7c Arun Sharma
/* Compatibility with older version */
35 9ce94e7c Arun Sharma
#if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
36 9ce94e7c Arun Sharma
static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
37 9ce94e7c Arun Sharma
{
38 9ce94e7c Arun Sharma
    return shared_page->vcpu_iodata[i].vp_eport;
39 9ce94e7c Arun Sharma
}
40 9ce94e7c Arun Sharma
static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
41 9ce94e7c Arun Sharma
{
42 9ce94e7c Arun Sharma
    return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
43 9ce94e7c Arun Sharma
}
44 9ce94e7c Arun Sharma
#  define FMT_ioreq_size PRIx64
45 9ce94e7c Arun Sharma
#else
46 9ce94e7c Arun Sharma
static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
47 9ce94e7c Arun Sharma
{
48 9ce94e7c Arun Sharma
    return shared_page->vcpu_ioreq[i].vp_eport;
49 9ce94e7c Arun Sharma
}
50 9ce94e7c Arun Sharma
static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
51 9ce94e7c Arun Sharma
{
52 9ce94e7c Arun Sharma
    return &shared_page->vcpu_ioreq[vcpu];
53 9ce94e7c Arun Sharma
}
54 9ce94e7c Arun Sharma
#  define FMT_ioreq_size "u"
55 9ce94e7c Arun Sharma
#endif
56 9ce94e7c Arun Sharma
57 9ce94e7c Arun Sharma
#define BUFFER_IO_MAX_DELAY  100
58 9ce94e7c Arun Sharma
59 b4dd7802 Anthony PERARD
typedef struct XenPhysmap {
60 b4dd7802 Anthony PERARD
    target_phys_addr_t start_addr;
61 b4dd7802 Anthony PERARD
    ram_addr_t size;
62 b4dd7802 Anthony PERARD
    target_phys_addr_t phys_offset;
63 b4dd7802 Anthony PERARD
64 b4dd7802 Anthony PERARD
    QLIST_ENTRY(XenPhysmap) list;
65 b4dd7802 Anthony PERARD
} XenPhysmap;
66 b4dd7802 Anthony PERARD
67 9ce94e7c Arun Sharma
typedef struct XenIOState {
68 9ce94e7c Arun Sharma
    shared_iopage_t *shared_page;
69 9ce94e7c Arun Sharma
    buffered_iopage_t *buffered_io_page;
70 9ce94e7c Arun Sharma
    QEMUTimer *buffered_io_timer;
71 9ce94e7c Arun Sharma
    /* the evtchn port for polling the notification, */
72 9ce94e7c Arun Sharma
    evtchn_port_t *ioreq_local_port;
73 9ce94e7c Arun Sharma
    /* the evtchn fd for polling */
74 9ce94e7c Arun Sharma
    XenEvtchn xce_handle;
75 9ce94e7c Arun Sharma
    /* which vcpu we are serving */
76 9ce94e7c Arun Sharma
    int send_vcpu;
77 9ce94e7c Arun Sharma
78 29321335 Anthony PERARD
    struct xs_handle *xenstore;
79 b4dd7802 Anthony PERARD
    CPUPhysMemoryClient client;
80 b4dd7802 Anthony PERARD
    QLIST_HEAD(, XenPhysmap) physmap;
81 b4dd7802 Anthony PERARD
    const XenPhysmap *log_for_dirtybit;
82 29321335 Anthony PERARD
83 9ce94e7c Arun Sharma
    Notifier exit;
84 9ce94e7c Arun Sharma
} XenIOState;
85 9ce94e7c Arun Sharma
86 41445300 Anthony PERARD
/* Xen specific function for piix pci */
87 41445300 Anthony PERARD
88 41445300 Anthony PERARD
int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
89 41445300 Anthony PERARD
{
90 41445300 Anthony PERARD
    return irq_num + ((pci_dev->devfn >> 3) << 2);
91 41445300 Anthony PERARD
}
92 41445300 Anthony PERARD
93 41445300 Anthony PERARD
void xen_piix3_set_irq(void *opaque, int irq_num, int level)
94 41445300 Anthony PERARD
{
95 41445300 Anthony PERARD
    xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
96 41445300 Anthony PERARD
                              irq_num & 3, level);
97 41445300 Anthony PERARD
}
98 41445300 Anthony PERARD
99 41445300 Anthony PERARD
void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
100 41445300 Anthony PERARD
{
101 41445300 Anthony PERARD
    int i;
102 41445300 Anthony PERARD
103 41445300 Anthony PERARD
    /* Scan for updates to PCI link routes (0x60-0x63). */
104 41445300 Anthony PERARD
    for (i = 0; i < len; i++) {
105 41445300 Anthony PERARD
        uint8_t v = (val >> (8 * i)) & 0xff;
106 41445300 Anthony PERARD
        if (v & 0x80) {
107 41445300 Anthony PERARD
            v = 0;
108 41445300 Anthony PERARD
        }
109 41445300 Anthony PERARD
        v &= 0xf;
110 41445300 Anthony PERARD
        if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
111 41445300 Anthony PERARD
            xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
112 41445300 Anthony PERARD
        }
113 41445300 Anthony PERARD
    }
114 41445300 Anthony PERARD
}
115 41445300 Anthony PERARD
116 c9622478 Anthony PERARD
void xen_cmos_set_s3_resume(void *opaque, int irq, int level)
117 c9622478 Anthony PERARD
{
118 c9622478 Anthony PERARD
    pc_cmos_set_s3_resume(opaque, irq, level);
119 c9622478 Anthony PERARD
    if (level) {
120 c9622478 Anthony PERARD
        xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
121 c9622478 Anthony PERARD
    }
122 c9622478 Anthony PERARD
}
123 c9622478 Anthony PERARD
124 9c11a8ac Anthony PERARD
/* Xen Interrupt Controller */
125 9c11a8ac Anthony PERARD
126 9c11a8ac Anthony PERARD
static void xen_set_irq(void *opaque, int irq, int level)
127 9c11a8ac Anthony PERARD
{
128 9c11a8ac Anthony PERARD
    xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
129 9c11a8ac Anthony PERARD
}
130 9c11a8ac Anthony PERARD
131 9c11a8ac Anthony PERARD
qemu_irq *xen_interrupt_controller_init(void)
132 9c11a8ac Anthony PERARD
{
133 9c11a8ac Anthony PERARD
    return qemu_allocate_irqs(xen_set_irq, NULL, 16);
134 9c11a8ac Anthony PERARD
}
135 9c11a8ac Anthony PERARD
136 432d268c Jun Nakajima
/* Memory Ops */
137 432d268c Jun Nakajima
138 432d268c Jun Nakajima
static void xen_ram_init(ram_addr_t ram_size)
139 432d268c Jun Nakajima
{
140 432d268c Jun Nakajima
    RAMBlock *new_block;
141 432d268c Jun Nakajima
    ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
142 432d268c Jun Nakajima
143 7267c094 Anthony Liguori
    new_block = g_malloc0(sizeof (*new_block));
144 432d268c Jun Nakajima
    pstrcpy(new_block->idstr, sizeof (new_block->idstr), "xen.ram");
145 432d268c Jun Nakajima
    new_block->host = NULL;
146 432d268c Jun Nakajima
    new_block->offset = 0;
147 432d268c Jun Nakajima
    new_block->length = ram_size;
148 8a369e20 Anthony PERARD
    if (ram_size >= HVM_BELOW_4G_RAM_END) {
149 8a369e20 Anthony PERARD
        /* Xen does not allocate the memory continuously, and keep a hole at
150 8a369e20 Anthony PERARD
         * HVM_BELOW_4G_MMIO_START of HVM_BELOW_4G_MMIO_LENGTH
151 8a369e20 Anthony PERARD
         */
152 8a369e20 Anthony PERARD
        new_block->length += HVM_BELOW_4G_MMIO_LENGTH;
153 8a369e20 Anthony PERARD
    }
154 432d268c Jun Nakajima
155 432d268c Jun Nakajima
    QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
156 432d268c Jun Nakajima
157 7267c094 Anthony Liguori
    ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
158 432d268c Jun Nakajima
                                       new_block->length >> TARGET_PAGE_BITS);
159 432d268c Jun Nakajima
    memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
160 432d268c Jun Nakajima
           0xff, new_block->length >> TARGET_PAGE_BITS);
161 432d268c Jun Nakajima
162 8a369e20 Anthony PERARD
    if (ram_size >= HVM_BELOW_4G_RAM_END) {
163 8a369e20 Anthony PERARD
        above_4g_mem_size = ram_size - HVM_BELOW_4G_RAM_END;
164 8a369e20 Anthony PERARD
        below_4g_mem_size = HVM_BELOW_4G_RAM_END;
165 432d268c Jun Nakajima
    } else {
166 432d268c Jun Nakajima
        below_4g_mem_size = ram_size;
167 432d268c Jun Nakajima
    }
168 432d268c Jun Nakajima
169 8a369e20 Anthony PERARD
    cpu_register_physical_memory(0, 0xa0000, 0);
170 8a369e20 Anthony PERARD
    /* Skip of the VGA IO memory space, it will be registered later by the VGA
171 8a369e20 Anthony PERARD
     * emulated device.
172 8a369e20 Anthony PERARD
     *
173 8a369e20 Anthony PERARD
     * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
174 8a369e20 Anthony PERARD
     * the Options ROM, so it is registered here as RAM.
175 8a369e20 Anthony PERARD
     */
176 8a369e20 Anthony PERARD
    cpu_register_physical_memory(0xc0000, below_4g_mem_size - 0xc0000,
177 8a369e20 Anthony PERARD
                                 0xc0000);
178 432d268c Jun Nakajima
    if (above_4g_mem_size > 0) {
179 432d268c Jun Nakajima
        cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
180 8a369e20 Anthony PERARD
                                     0x100000000ULL);
181 432d268c Jun Nakajima
    }
182 432d268c Jun Nakajima
}
183 432d268c Jun Nakajima
184 432d268c Jun Nakajima
void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size)
185 432d268c Jun Nakajima
{
186 432d268c Jun Nakajima
    unsigned long nr_pfn;
187 432d268c Jun Nakajima
    xen_pfn_t *pfn_list;
188 432d268c Jun Nakajima
    int i;
189 432d268c Jun Nakajima
190 432d268c Jun Nakajima
    trace_xen_ram_alloc(ram_addr, size);
191 432d268c Jun Nakajima
192 432d268c Jun Nakajima
    nr_pfn = size >> TARGET_PAGE_BITS;
193 7267c094 Anthony Liguori
    pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
194 432d268c Jun Nakajima
195 432d268c Jun Nakajima
    for (i = 0; i < nr_pfn; i++) {
196 432d268c Jun Nakajima
        pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
197 432d268c Jun Nakajima
    }
198 432d268c Jun Nakajima
199 432d268c Jun Nakajima
    if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
200 f15fbc4b Anthony PERARD
        hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
201 432d268c Jun Nakajima
    }
202 432d268c Jun Nakajima
203 7267c094 Anthony Liguori
    g_free(pfn_list);
204 432d268c Jun Nakajima
}
205 432d268c Jun Nakajima
206 b4dd7802 Anthony PERARD
static XenPhysmap *get_physmapping(XenIOState *state,
207 b4dd7802 Anthony PERARD
                                   target_phys_addr_t start_addr, ram_addr_t size)
208 b4dd7802 Anthony PERARD
{
209 b4dd7802 Anthony PERARD
    XenPhysmap *physmap = NULL;
210 b4dd7802 Anthony PERARD
211 b4dd7802 Anthony PERARD
    start_addr &= TARGET_PAGE_MASK;
212 b4dd7802 Anthony PERARD
213 b4dd7802 Anthony PERARD
    QLIST_FOREACH(physmap, &state->physmap, list) {
214 b4dd7802 Anthony PERARD
        if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
215 b4dd7802 Anthony PERARD
            return physmap;
216 b4dd7802 Anthony PERARD
        }
217 b4dd7802 Anthony PERARD
    }
218 b4dd7802 Anthony PERARD
    return NULL;
219 b4dd7802 Anthony PERARD
}
220 b4dd7802 Anthony PERARD
221 b4dd7802 Anthony PERARD
#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
222 b4dd7802 Anthony PERARD
static int xen_add_to_physmap(XenIOState *state,
223 b4dd7802 Anthony PERARD
                              target_phys_addr_t start_addr,
224 b4dd7802 Anthony PERARD
                              ram_addr_t size,
225 b4dd7802 Anthony PERARD
                              target_phys_addr_t phys_offset)
226 b4dd7802 Anthony PERARD
{
227 b4dd7802 Anthony PERARD
    unsigned long i = 0;
228 b4dd7802 Anthony PERARD
    int rc = 0;
229 b4dd7802 Anthony PERARD
    XenPhysmap *physmap = NULL;
230 b4dd7802 Anthony PERARD
    target_phys_addr_t pfn, start_gpfn;
231 ebed8505 Stefano Stabellini
    RAMBlock *block;
232 b4dd7802 Anthony PERARD
233 b4dd7802 Anthony PERARD
    if (get_physmapping(state, start_addr, size)) {
234 b4dd7802 Anthony PERARD
        return 0;
235 b4dd7802 Anthony PERARD
    }
236 b4dd7802 Anthony PERARD
    if (size <= 0) {
237 b4dd7802 Anthony PERARD
        return -1;
238 b4dd7802 Anthony PERARD
    }
239 b4dd7802 Anthony PERARD
240 ebed8505 Stefano Stabellini
    /* Xen can only handle a single dirty log region for now and we want
241 ebed8505 Stefano Stabellini
     * the linear framebuffer to be that region.
242 ebed8505 Stefano Stabellini
     * Avoid tracking any regions that is not videoram and avoid tracking
243 ebed8505 Stefano Stabellini
     * the legacy vga region. */
244 ebed8505 Stefano Stabellini
    QLIST_FOREACH(block, &ram_list.blocks, next) {
245 ebed8505 Stefano Stabellini
        if (!strcmp(block->idstr, "vga.vram") && block->offset == phys_offset
246 ebed8505 Stefano Stabellini
                && start_addr > 0xbffff) {
247 ebed8505 Stefano Stabellini
            goto go_physmap;
248 ebed8505 Stefano Stabellini
        }
249 ebed8505 Stefano Stabellini
    }
250 ebed8505 Stefano Stabellini
    return -1;
251 ebed8505 Stefano Stabellini
252 ebed8505 Stefano Stabellini
go_physmap:
253 b4dd7802 Anthony PERARD
    DPRINTF("mapping vram to %llx - %llx, from %llx\n",
254 b4dd7802 Anthony PERARD
            start_addr, start_addr + size, phys_offset);
255 b4dd7802 Anthony PERARD
256 b4dd7802 Anthony PERARD
    pfn = phys_offset >> TARGET_PAGE_BITS;
257 b4dd7802 Anthony PERARD
    start_gpfn = start_addr >> TARGET_PAGE_BITS;
258 b4dd7802 Anthony PERARD
    for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
259 b4dd7802 Anthony PERARD
        unsigned long idx = pfn + i;
260 b4dd7802 Anthony PERARD
        xen_pfn_t gpfn = start_gpfn + i;
261 b4dd7802 Anthony PERARD
262 b4dd7802 Anthony PERARD
        rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
263 b4dd7802 Anthony PERARD
        if (rc) {
264 b4dd7802 Anthony PERARD
            DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
265 b4dd7802 Anthony PERARD
                    PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
266 b4dd7802 Anthony PERARD
            return -rc;
267 b4dd7802 Anthony PERARD
        }
268 b4dd7802 Anthony PERARD
    }
269 b4dd7802 Anthony PERARD
270 7267c094 Anthony Liguori
    physmap = g_malloc(sizeof (XenPhysmap));
271 b4dd7802 Anthony PERARD
272 b4dd7802 Anthony PERARD
    physmap->start_addr = start_addr;
273 b4dd7802 Anthony PERARD
    physmap->size = size;
274 b4dd7802 Anthony PERARD
    physmap->phys_offset = phys_offset;
275 b4dd7802 Anthony PERARD
276 b4dd7802 Anthony PERARD
    QLIST_INSERT_HEAD(&state->physmap, physmap, list);
277 b4dd7802 Anthony PERARD
278 b4dd7802 Anthony PERARD
    xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
279 b4dd7802 Anthony PERARD
                                   start_addr >> TARGET_PAGE_BITS,
280 b4dd7802 Anthony PERARD
                                   (start_addr + size) >> TARGET_PAGE_BITS,
281 b4dd7802 Anthony PERARD
                                   XEN_DOMCTL_MEM_CACHEATTR_WB);
282 b4dd7802 Anthony PERARD
    return 0;
283 b4dd7802 Anthony PERARD
}
284 b4dd7802 Anthony PERARD
285 b4dd7802 Anthony PERARD
static int xen_remove_from_physmap(XenIOState *state,
286 b4dd7802 Anthony PERARD
                                   target_phys_addr_t start_addr,
287 b4dd7802 Anthony PERARD
                                   ram_addr_t size)
288 b4dd7802 Anthony PERARD
{
289 b4dd7802 Anthony PERARD
    unsigned long i = 0;
290 b4dd7802 Anthony PERARD
    int rc = 0;
291 b4dd7802 Anthony PERARD
    XenPhysmap *physmap = NULL;
292 b4dd7802 Anthony PERARD
    target_phys_addr_t phys_offset = 0;
293 b4dd7802 Anthony PERARD
294 b4dd7802 Anthony PERARD
    physmap = get_physmapping(state, start_addr, size);
295 b4dd7802 Anthony PERARD
    if (physmap == NULL) {
296 b4dd7802 Anthony PERARD
        return -1;
297 b4dd7802 Anthony PERARD
    }
298 b4dd7802 Anthony PERARD
299 b4dd7802 Anthony PERARD
    phys_offset = physmap->phys_offset;
300 b4dd7802 Anthony PERARD
    size = physmap->size;
301 b4dd7802 Anthony PERARD
302 b4dd7802 Anthony PERARD
    DPRINTF("unmapping vram to %llx - %llx, from %llx\n",
303 b4dd7802 Anthony PERARD
            phys_offset, phys_offset + size, start_addr);
304 b4dd7802 Anthony PERARD
305 b4dd7802 Anthony PERARD
    size >>= TARGET_PAGE_BITS;
306 b4dd7802 Anthony PERARD
    start_addr >>= TARGET_PAGE_BITS;
307 b4dd7802 Anthony PERARD
    phys_offset >>= TARGET_PAGE_BITS;
308 b4dd7802 Anthony PERARD
    for (i = 0; i < size; i++) {
309 b4dd7802 Anthony PERARD
        unsigned long idx = start_addr + i;
310 b4dd7802 Anthony PERARD
        xen_pfn_t gpfn = phys_offset + i;
311 b4dd7802 Anthony PERARD
312 b4dd7802 Anthony PERARD
        rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
313 b4dd7802 Anthony PERARD
        if (rc) {
314 b4dd7802 Anthony PERARD
            fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
315 b4dd7802 Anthony PERARD
                    PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
316 b4dd7802 Anthony PERARD
            return -rc;
317 b4dd7802 Anthony PERARD
        }
318 b4dd7802 Anthony PERARD
    }
319 b4dd7802 Anthony PERARD
320 b4dd7802 Anthony PERARD
    QLIST_REMOVE(physmap, list);
321 b4dd7802 Anthony PERARD
    if (state->log_for_dirtybit == physmap) {
322 b4dd7802 Anthony PERARD
        state->log_for_dirtybit = NULL;
323 b4dd7802 Anthony PERARD
    }
324 b4dd7802 Anthony PERARD
    free(physmap);
325 b4dd7802 Anthony PERARD
326 b4dd7802 Anthony PERARD
    return 0;
327 b4dd7802 Anthony PERARD
}
328 b4dd7802 Anthony PERARD
329 b4dd7802 Anthony PERARD
#else
330 b4dd7802 Anthony PERARD
static int xen_add_to_physmap(XenIOState *state,
331 b4dd7802 Anthony PERARD
                              target_phys_addr_t start_addr,
332 b4dd7802 Anthony PERARD
                              ram_addr_t size,
333 b4dd7802 Anthony PERARD
                              target_phys_addr_t phys_offset)
334 b4dd7802 Anthony PERARD
{
335 b4dd7802 Anthony PERARD
    return -ENOSYS;
336 b4dd7802 Anthony PERARD
}
337 b4dd7802 Anthony PERARD
338 b4dd7802 Anthony PERARD
static int xen_remove_from_physmap(XenIOState *state,
339 b4dd7802 Anthony PERARD
                                   target_phys_addr_t start_addr,
340 b4dd7802 Anthony PERARD
                                   ram_addr_t size)
341 b4dd7802 Anthony PERARD
{
342 b4dd7802 Anthony PERARD
    return -ENOSYS;
343 b4dd7802 Anthony PERARD
}
344 b4dd7802 Anthony PERARD
#endif
345 b4dd7802 Anthony PERARD
346 b4dd7802 Anthony PERARD
static void xen_client_set_memory(struct CPUPhysMemoryClient *client,
347 b4dd7802 Anthony PERARD
                                  target_phys_addr_t start_addr,
348 b4dd7802 Anthony PERARD
                                  ram_addr_t size,
349 b4dd7802 Anthony PERARD
                                  ram_addr_t phys_offset,
350 b4dd7802 Anthony PERARD
                                  bool log_dirty)
351 b4dd7802 Anthony PERARD
{
352 b4dd7802 Anthony PERARD
    XenIOState *state = container_of(client, XenIOState, client);
353 b4dd7802 Anthony PERARD
    ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
354 b4dd7802 Anthony PERARD
    hvmmem_type_t mem_type;
355 b4dd7802 Anthony PERARD
356 b4dd7802 Anthony PERARD
    if (!(start_addr != phys_offset
357 b4dd7802 Anthony PERARD
          && ( (log_dirty && flags < IO_MEM_UNASSIGNED)
358 b4dd7802 Anthony PERARD
               || (!log_dirty && flags == IO_MEM_UNASSIGNED)))) {
359 b4dd7802 Anthony PERARD
        return;
360 b4dd7802 Anthony PERARD
    }
361 b4dd7802 Anthony PERARD
362 b4dd7802 Anthony PERARD
    trace_xen_client_set_memory(start_addr, size, phys_offset, log_dirty);
363 b4dd7802 Anthony PERARD
364 b4dd7802 Anthony PERARD
    start_addr &= TARGET_PAGE_MASK;
365 b4dd7802 Anthony PERARD
    size = TARGET_PAGE_ALIGN(size);
366 b4dd7802 Anthony PERARD
    phys_offset &= TARGET_PAGE_MASK;
367 b4dd7802 Anthony PERARD
368 b4dd7802 Anthony PERARD
    switch (flags) {
369 b4dd7802 Anthony PERARD
    case IO_MEM_RAM:
370 b4dd7802 Anthony PERARD
        xen_add_to_physmap(state, start_addr, size, phys_offset);
371 b4dd7802 Anthony PERARD
        break;
372 b4dd7802 Anthony PERARD
    case IO_MEM_ROM:
373 b4dd7802 Anthony PERARD
        mem_type = HVMMEM_ram_ro;
374 b4dd7802 Anthony PERARD
        if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
375 b4dd7802 Anthony PERARD
                                start_addr >> TARGET_PAGE_BITS,
376 b4dd7802 Anthony PERARD
                                size >> TARGET_PAGE_BITS)) {
377 b4dd7802 Anthony PERARD
            DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
378 b4dd7802 Anthony PERARD
                    start_addr);
379 b4dd7802 Anthony PERARD
        }
380 b4dd7802 Anthony PERARD
        break;
381 b4dd7802 Anthony PERARD
    case IO_MEM_UNASSIGNED:
382 b4dd7802 Anthony PERARD
        if (xen_remove_from_physmap(state, start_addr, size) < 0) {
383 b4dd7802 Anthony PERARD
            DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
384 b4dd7802 Anthony PERARD
        }
385 b4dd7802 Anthony PERARD
        break;
386 b4dd7802 Anthony PERARD
    }
387 b4dd7802 Anthony PERARD
}
388 b4dd7802 Anthony PERARD
389 b4dd7802 Anthony PERARD
static int xen_sync_dirty_bitmap(XenIOState *state,
390 b4dd7802 Anthony PERARD
                                 target_phys_addr_t start_addr,
391 b4dd7802 Anthony PERARD
                                 ram_addr_t size)
392 b4dd7802 Anthony PERARD
{
393 b4dd7802 Anthony PERARD
    target_phys_addr_t npages = size >> TARGET_PAGE_BITS;
394 b4dd7802 Anthony PERARD
    target_phys_addr_t vram_offset = 0;
395 b4dd7802 Anthony PERARD
    const int width = sizeof(unsigned long) * 8;
396 b4dd7802 Anthony PERARD
    unsigned long bitmap[(npages + width - 1) / width];
397 b4dd7802 Anthony PERARD
    int rc, i, j;
398 b4dd7802 Anthony PERARD
    const XenPhysmap *physmap = NULL;
399 b4dd7802 Anthony PERARD
400 b4dd7802 Anthony PERARD
    physmap = get_physmapping(state, start_addr, size);
401 b4dd7802 Anthony PERARD
    if (physmap == NULL) {
402 b4dd7802 Anthony PERARD
        /* not handled */
403 b4dd7802 Anthony PERARD
        return -1;
404 b4dd7802 Anthony PERARD
    }
405 b4dd7802 Anthony PERARD
406 b4dd7802 Anthony PERARD
    if (state->log_for_dirtybit == NULL) {
407 b4dd7802 Anthony PERARD
        state->log_for_dirtybit = physmap;
408 b4dd7802 Anthony PERARD
    } else if (state->log_for_dirtybit != physmap) {
409 b4dd7802 Anthony PERARD
        return -1;
410 b4dd7802 Anthony PERARD
    }
411 b4dd7802 Anthony PERARD
    vram_offset = physmap->phys_offset;
412 b4dd7802 Anthony PERARD
413 b4dd7802 Anthony PERARD
    rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
414 b4dd7802 Anthony PERARD
                                 start_addr >> TARGET_PAGE_BITS, npages,
415 b4dd7802 Anthony PERARD
                                 bitmap);
416 b4dd7802 Anthony PERARD
    if (rc) {
417 b4dd7802 Anthony PERARD
        return rc;
418 b4dd7802 Anthony PERARD
    }
419 b4dd7802 Anthony PERARD
420 b4dd7802 Anthony PERARD
    for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
421 b4dd7802 Anthony PERARD
        unsigned long map = bitmap[i];
422 b4dd7802 Anthony PERARD
        while (map != 0) {
423 b4dd7802 Anthony PERARD
            j = ffsl(map) - 1;
424 b4dd7802 Anthony PERARD
            map &= ~(1ul << j);
425 b4dd7802 Anthony PERARD
            cpu_physical_memory_set_dirty(vram_offset + (i * width + j) * TARGET_PAGE_SIZE);
426 b4dd7802 Anthony PERARD
        };
427 b4dd7802 Anthony PERARD
    }
428 b4dd7802 Anthony PERARD
429 b4dd7802 Anthony PERARD
    return 0;
430 b4dd7802 Anthony PERARD
}
431 b4dd7802 Anthony PERARD
432 b4dd7802 Anthony PERARD
static int xen_log_start(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
433 b4dd7802 Anthony PERARD
{
434 b4dd7802 Anthony PERARD
    XenIOState *state = container_of(client, XenIOState, client);
435 b4dd7802 Anthony PERARD
436 b4dd7802 Anthony PERARD
    return xen_sync_dirty_bitmap(state, phys_addr, size);
437 b4dd7802 Anthony PERARD
}
438 b4dd7802 Anthony PERARD
439 b4dd7802 Anthony PERARD
static int xen_log_stop(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size)
440 b4dd7802 Anthony PERARD
{
441 b4dd7802 Anthony PERARD
    XenIOState *state = container_of(client, XenIOState, client);
442 b4dd7802 Anthony PERARD
443 b4dd7802 Anthony PERARD
    state->log_for_dirtybit = NULL;
444 b4dd7802 Anthony PERARD
    /* Disable dirty bit tracking */
445 b4dd7802 Anthony PERARD
    return xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
446 b4dd7802 Anthony PERARD
}
447 b4dd7802 Anthony PERARD
448 b4dd7802 Anthony PERARD
static int xen_client_sync_dirty_bitmap(struct CPUPhysMemoryClient *client,
449 b4dd7802 Anthony PERARD
                                        target_phys_addr_t start_addr,
450 b4dd7802 Anthony PERARD
                                        target_phys_addr_t end_addr)
451 b4dd7802 Anthony PERARD
{
452 b4dd7802 Anthony PERARD
    XenIOState *state = container_of(client, XenIOState, client);
453 b4dd7802 Anthony PERARD
454 b4dd7802 Anthony PERARD
    return xen_sync_dirty_bitmap(state, start_addr, end_addr - start_addr);
455 b4dd7802 Anthony PERARD
}
456 b4dd7802 Anthony PERARD
457 b4dd7802 Anthony PERARD
static int xen_client_migration_log(struct CPUPhysMemoryClient *client,
458 b4dd7802 Anthony PERARD
                                    int enable)
459 b4dd7802 Anthony PERARD
{
460 b4dd7802 Anthony PERARD
    return 0;
461 b4dd7802 Anthony PERARD
}
462 b4dd7802 Anthony PERARD
463 b4dd7802 Anthony PERARD
static CPUPhysMemoryClient xen_cpu_phys_memory_client = {
464 b4dd7802 Anthony PERARD
    .set_memory = xen_client_set_memory,
465 b4dd7802 Anthony PERARD
    .sync_dirty_bitmap = xen_client_sync_dirty_bitmap,
466 b4dd7802 Anthony PERARD
    .migration_log = xen_client_migration_log,
467 b4dd7802 Anthony PERARD
    .log_start = xen_log_start,
468 b4dd7802 Anthony PERARD
    .log_stop = xen_log_stop,
469 b4dd7802 Anthony PERARD
};
470 432d268c Jun Nakajima
471 29d3ccde Anthony PERARD
/* VCPU Operations, MMIO, IO ring ... */
472 29d3ccde Anthony PERARD
473 29d3ccde Anthony PERARD
static void xen_reset_vcpu(void *opaque)
474 29d3ccde Anthony PERARD
{
475 29d3ccde Anthony PERARD
    CPUState *env = opaque;
476 29d3ccde Anthony PERARD
477 29d3ccde Anthony PERARD
    env->halted = 1;
478 29d3ccde Anthony PERARD
}
479 29d3ccde Anthony PERARD
480 29d3ccde Anthony PERARD
void xen_vcpu_init(void)
481 29d3ccde Anthony PERARD
{
482 29d3ccde Anthony PERARD
    CPUState *first_cpu;
483 29d3ccde Anthony PERARD
484 29d3ccde Anthony PERARD
    if ((first_cpu = qemu_get_cpu(0))) {
485 29d3ccde Anthony PERARD
        qemu_register_reset(xen_reset_vcpu, first_cpu);
486 29d3ccde Anthony PERARD
        xen_reset_vcpu(first_cpu);
487 29d3ccde Anthony PERARD
    }
488 29d3ccde Anthony PERARD
}
489 29d3ccde Anthony PERARD
490 9ce94e7c Arun Sharma
/* get the ioreq packets from share mem */
491 9ce94e7c Arun Sharma
static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
492 9ce94e7c Arun Sharma
{
493 9ce94e7c Arun Sharma
    ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
494 9ce94e7c Arun Sharma
495 9ce94e7c Arun Sharma
    if (req->state != STATE_IOREQ_READY) {
496 9ce94e7c Arun Sharma
        DPRINTF("I/O request not ready: "
497 9ce94e7c Arun Sharma
                "%x, ptr: %x, port: %"PRIx64", "
498 9ce94e7c Arun Sharma
                "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
499 9ce94e7c Arun Sharma
                req->state, req->data_is_ptr, req->addr,
500 9ce94e7c Arun Sharma
                req->data, req->count, req->size);
501 9ce94e7c Arun Sharma
        return NULL;
502 9ce94e7c Arun Sharma
    }
503 9ce94e7c Arun Sharma
504 9ce94e7c Arun Sharma
    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
505 9ce94e7c Arun Sharma
506 9ce94e7c Arun Sharma
    req->state = STATE_IOREQ_INPROCESS;
507 9ce94e7c Arun Sharma
    return req;
508 9ce94e7c Arun Sharma
}
509 9ce94e7c Arun Sharma
510 9ce94e7c Arun Sharma
/* use poll to get the port notification */
511 9ce94e7c Arun Sharma
/* ioreq_vec--out,the */
512 9ce94e7c Arun Sharma
/* retval--the number of ioreq packet */
513 9ce94e7c Arun Sharma
static ioreq_t *cpu_get_ioreq(XenIOState *state)
514 9ce94e7c Arun Sharma
{
515 9ce94e7c Arun Sharma
    int i;
516 9ce94e7c Arun Sharma
    evtchn_port_t port;
517 9ce94e7c Arun Sharma
518 9ce94e7c Arun Sharma
    port = xc_evtchn_pending(state->xce_handle);
519 9ce94e7c Arun Sharma
    if (port != -1) {
520 9ce94e7c Arun Sharma
        for (i = 0; i < smp_cpus; i++) {
521 9ce94e7c Arun Sharma
            if (state->ioreq_local_port[i] == port) {
522 9ce94e7c Arun Sharma
                break;
523 9ce94e7c Arun Sharma
            }
524 9ce94e7c Arun Sharma
        }
525 9ce94e7c Arun Sharma
526 9ce94e7c Arun Sharma
        if (i == smp_cpus) {
527 9ce94e7c Arun Sharma
            hw_error("Fatal error while trying to get io event!\n");
528 9ce94e7c Arun Sharma
        }
529 9ce94e7c Arun Sharma
530 9ce94e7c Arun Sharma
        /* unmask the wanted port again */
531 9ce94e7c Arun Sharma
        xc_evtchn_unmask(state->xce_handle, port);
532 9ce94e7c Arun Sharma
533 9ce94e7c Arun Sharma
        /* get the io packet from shared memory */
534 9ce94e7c Arun Sharma
        state->send_vcpu = i;
535 9ce94e7c Arun Sharma
        return cpu_get_ioreq_from_shared_memory(state, i);
536 9ce94e7c Arun Sharma
    }
537 9ce94e7c Arun Sharma
538 9ce94e7c Arun Sharma
    /* read error or read nothing */
539 9ce94e7c Arun Sharma
    return NULL;
540 9ce94e7c Arun Sharma
}
541 9ce94e7c Arun Sharma
542 9ce94e7c Arun Sharma
static uint32_t do_inp(pio_addr_t addr, unsigned long size)
543 9ce94e7c Arun Sharma
{
544 9ce94e7c Arun Sharma
    switch (size) {
545 9ce94e7c Arun Sharma
        case 1:
546 9ce94e7c Arun Sharma
            return cpu_inb(addr);
547 9ce94e7c Arun Sharma
        case 2:
548 9ce94e7c Arun Sharma
            return cpu_inw(addr);
549 9ce94e7c Arun Sharma
        case 4:
550 9ce94e7c Arun Sharma
            return cpu_inl(addr);
551 9ce94e7c Arun Sharma
        default:
552 9ce94e7c Arun Sharma
            hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
553 9ce94e7c Arun Sharma
    }
554 9ce94e7c Arun Sharma
}
555 9ce94e7c Arun Sharma
556 9ce94e7c Arun Sharma
static void do_outp(pio_addr_t addr,
557 9ce94e7c Arun Sharma
        unsigned long size, uint32_t val)
558 9ce94e7c Arun Sharma
{
559 9ce94e7c Arun Sharma
    switch (size) {
560 9ce94e7c Arun Sharma
        case 1:
561 9ce94e7c Arun Sharma
            return cpu_outb(addr, val);
562 9ce94e7c Arun Sharma
        case 2:
563 9ce94e7c Arun Sharma
            return cpu_outw(addr, val);
564 9ce94e7c Arun Sharma
        case 4:
565 9ce94e7c Arun Sharma
            return cpu_outl(addr, val);
566 9ce94e7c Arun Sharma
        default:
567 9ce94e7c Arun Sharma
            hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
568 9ce94e7c Arun Sharma
    }
569 9ce94e7c Arun Sharma
}
570 9ce94e7c Arun Sharma
571 9ce94e7c Arun Sharma
static void cpu_ioreq_pio(ioreq_t *req)
572 9ce94e7c Arun Sharma
{
573 9ce94e7c Arun Sharma
    int i, sign;
574 9ce94e7c Arun Sharma
575 9ce94e7c Arun Sharma
    sign = req->df ? -1 : 1;
576 9ce94e7c Arun Sharma
577 9ce94e7c Arun Sharma
    if (req->dir == IOREQ_READ) {
578 9ce94e7c Arun Sharma
        if (!req->data_is_ptr) {
579 9ce94e7c Arun Sharma
            req->data = do_inp(req->addr, req->size);
580 9ce94e7c Arun Sharma
        } else {
581 9ce94e7c Arun Sharma
            uint32_t tmp;
582 9ce94e7c Arun Sharma
583 9ce94e7c Arun Sharma
            for (i = 0; i < req->count; i++) {
584 9ce94e7c Arun Sharma
                tmp = do_inp(req->addr, req->size);
585 9ce94e7c Arun Sharma
                cpu_physical_memory_write(req->data + (sign * i * req->size),
586 9ce94e7c Arun Sharma
                        (uint8_t *) &tmp, req->size);
587 9ce94e7c Arun Sharma
            }
588 9ce94e7c Arun Sharma
        }
589 9ce94e7c Arun Sharma
    } else if (req->dir == IOREQ_WRITE) {
590 9ce94e7c Arun Sharma
        if (!req->data_is_ptr) {
591 9ce94e7c Arun Sharma
            do_outp(req->addr, req->size, req->data);
592 9ce94e7c Arun Sharma
        } else {
593 9ce94e7c Arun Sharma
            for (i = 0; i < req->count; i++) {
594 9ce94e7c Arun Sharma
                uint32_t tmp = 0;
595 9ce94e7c Arun Sharma
596 9ce94e7c Arun Sharma
                cpu_physical_memory_read(req->data + (sign * i * req->size),
597 9ce94e7c Arun Sharma
                        (uint8_t*) &tmp, req->size);
598 9ce94e7c Arun Sharma
                do_outp(req->addr, req->size, tmp);
599 9ce94e7c Arun Sharma
            }
600 9ce94e7c Arun Sharma
        }
601 9ce94e7c Arun Sharma
    }
602 9ce94e7c Arun Sharma
}
603 9ce94e7c Arun Sharma
604 9ce94e7c Arun Sharma
static void cpu_ioreq_move(ioreq_t *req)
605 9ce94e7c Arun Sharma
{
606 9ce94e7c Arun Sharma
    int i, sign;
607 9ce94e7c Arun Sharma
608 9ce94e7c Arun Sharma
    sign = req->df ? -1 : 1;
609 9ce94e7c Arun Sharma
610 9ce94e7c Arun Sharma
    if (!req->data_is_ptr) {
611 9ce94e7c Arun Sharma
        if (req->dir == IOREQ_READ) {
612 9ce94e7c Arun Sharma
            for (i = 0; i < req->count; i++) {
613 9ce94e7c Arun Sharma
                cpu_physical_memory_read(req->addr + (sign * i * req->size),
614 9ce94e7c Arun Sharma
                        (uint8_t *) &req->data, req->size);
615 9ce94e7c Arun Sharma
            }
616 9ce94e7c Arun Sharma
        } else if (req->dir == IOREQ_WRITE) {
617 9ce94e7c Arun Sharma
            for (i = 0; i < req->count; i++) {
618 9ce94e7c Arun Sharma
                cpu_physical_memory_write(req->addr + (sign * i * req->size),
619 9ce94e7c Arun Sharma
                        (uint8_t *) &req->data, req->size);
620 9ce94e7c Arun Sharma
            }
621 9ce94e7c Arun Sharma
        }
622 9ce94e7c Arun Sharma
    } else {
623 9ce94e7c Arun Sharma
        target_ulong tmp;
624 9ce94e7c Arun Sharma
625 9ce94e7c Arun Sharma
        if (req->dir == IOREQ_READ) {
626 9ce94e7c Arun Sharma
            for (i = 0; i < req->count; i++) {
627 9ce94e7c Arun Sharma
                cpu_physical_memory_read(req->addr + (sign * i * req->size),
628 9ce94e7c Arun Sharma
                        (uint8_t*) &tmp, req->size);
629 9ce94e7c Arun Sharma
                cpu_physical_memory_write(req->data + (sign * i * req->size),
630 9ce94e7c Arun Sharma
                        (uint8_t*) &tmp, req->size);
631 9ce94e7c Arun Sharma
            }
632 9ce94e7c Arun Sharma
        } else if (req->dir == IOREQ_WRITE) {
633 9ce94e7c Arun Sharma
            for (i = 0; i < req->count; i++) {
634 9ce94e7c Arun Sharma
                cpu_physical_memory_read(req->data + (sign * i * req->size),
635 9ce94e7c Arun Sharma
                        (uint8_t*) &tmp, req->size);
636 9ce94e7c Arun Sharma
                cpu_physical_memory_write(req->addr + (sign * i * req->size),
637 9ce94e7c Arun Sharma
                        (uint8_t*) &tmp, req->size);
638 9ce94e7c Arun Sharma
            }
639 9ce94e7c Arun Sharma
        }
640 9ce94e7c Arun Sharma
    }
641 9ce94e7c Arun Sharma
}
642 9ce94e7c Arun Sharma
643 9ce94e7c Arun Sharma
static void handle_ioreq(ioreq_t *req)
644 9ce94e7c Arun Sharma
{
645 9ce94e7c Arun Sharma
    if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
646 9ce94e7c Arun Sharma
            (req->size < sizeof (target_ulong))) {
647 9ce94e7c Arun Sharma
        req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
648 9ce94e7c Arun Sharma
    }
649 9ce94e7c Arun Sharma
650 9ce94e7c Arun Sharma
    switch (req->type) {
651 9ce94e7c Arun Sharma
        case IOREQ_TYPE_PIO:
652 9ce94e7c Arun Sharma
            cpu_ioreq_pio(req);
653 9ce94e7c Arun Sharma
            break;
654 9ce94e7c Arun Sharma
        case IOREQ_TYPE_COPY:
655 9ce94e7c Arun Sharma
            cpu_ioreq_move(req);
656 9ce94e7c Arun Sharma
            break;
657 9ce94e7c Arun Sharma
        case IOREQ_TYPE_TIMEOFFSET:
658 9ce94e7c Arun Sharma
            break;
659 9ce94e7c Arun Sharma
        case IOREQ_TYPE_INVALIDATE:
660 e41d7c69 Jan Kiszka
            xen_invalidate_map_cache();
661 9ce94e7c Arun Sharma
            break;
662 9ce94e7c Arun Sharma
        default:
663 9ce94e7c Arun Sharma
            hw_error("Invalid ioreq type 0x%x\n", req->type);
664 9ce94e7c Arun Sharma
    }
665 9ce94e7c Arun Sharma
}
666 9ce94e7c Arun Sharma
667 9ce94e7c Arun Sharma
static void handle_buffered_iopage(XenIOState *state)
668 9ce94e7c Arun Sharma
{
669 9ce94e7c Arun Sharma
    buf_ioreq_t *buf_req = NULL;
670 9ce94e7c Arun Sharma
    ioreq_t req;
671 9ce94e7c Arun Sharma
    int qw;
672 9ce94e7c Arun Sharma
673 9ce94e7c Arun Sharma
    if (!state->buffered_io_page) {
674 9ce94e7c Arun Sharma
        return;
675 9ce94e7c Arun Sharma
    }
676 9ce94e7c Arun Sharma
677 9ce94e7c Arun Sharma
    while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
678 9ce94e7c Arun Sharma
        buf_req = &state->buffered_io_page->buf_ioreq[
679 9ce94e7c Arun Sharma
            state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
680 9ce94e7c Arun Sharma
        req.size = 1UL << buf_req->size;
681 9ce94e7c Arun Sharma
        req.count = 1;
682 9ce94e7c Arun Sharma
        req.addr = buf_req->addr;
683 9ce94e7c Arun Sharma
        req.data = buf_req->data;
684 9ce94e7c Arun Sharma
        req.state = STATE_IOREQ_READY;
685 9ce94e7c Arun Sharma
        req.dir = buf_req->dir;
686 9ce94e7c Arun Sharma
        req.df = 1;
687 9ce94e7c Arun Sharma
        req.type = buf_req->type;
688 9ce94e7c Arun Sharma
        req.data_is_ptr = 0;
689 9ce94e7c Arun Sharma
        qw = (req.size == 8);
690 9ce94e7c Arun Sharma
        if (qw) {
691 9ce94e7c Arun Sharma
            buf_req = &state->buffered_io_page->buf_ioreq[
692 9ce94e7c Arun Sharma
                (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
693 9ce94e7c Arun Sharma
            req.data |= ((uint64_t)buf_req->data) << 32;
694 9ce94e7c Arun Sharma
        }
695 9ce94e7c Arun Sharma
696 9ce94e7c Arun Sharma
        handle_ioreq(&req);
697 9ce94e7c Arun Sharma
698 9ce94e7c Arun Sharma
        xen_mb();
699 9ce94e7c Arun Sharma
        state->buffered_io_page->read_pointer += qw ? 2 : 1;
700 9ce94e7c Arun Sharma
    }
701 9ce94e7c Arun Sharma
}
702 9ce94e7c Arun Sharma
703 9ce94e7c Arun Sharma
static void handle_buffered_io(void *opaque)
704 9ce94e7c Arun Sharma
{
705 9ce94e7c Arun Sharma
    XenIOState *state = opaque;
706 9ce94e7c Arun Sharma
707 9ce94e7c Arun Sharma
    handle_buffered_iopage(state);
708 9ce94e7c Arun Sharma
    qemu_mod_timer(state->buffered_io_timer,
709 9ce94e7c Arun Sharma
                   BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
710 9ce94e7c Arun Sharma
}
711 9ce94e7c Arun Sharma
712 9ce94e7c Arun Sharma
static void cpu_handle_ioreq(void *opaque)
713 9ce94e7c Arun Sharma
{
714 9ce94e7c Arun Sharma
    XenIOState *state = opaque;
715 9ce94e7c Arun Sharma
    ioreq_t *req = cpu_get_ioreq(state);
716 9ce94e7c Arun Sharma
717 9ce94e7c Arun Sharma
    handle_buffered_iopage(state);
718 9ce94e7c Arun Sharma
    if (req) {
719 9ce94e7c Arun Sharma
        handle_ioreq(req);
720 9ce94e7c Arun Sharma
721 9ce94e7c Arun Sharma
        if (req->state != STATE_IOREQ_INPROCESS) {
722 9ce94e7c Arun Sharma
            fprintf(stderr, "Badness in I/O request ... not in service?!: "
723 9ce94e7c Arun Sharma
                    "%x, ptr: %x, port: %"PRIx64", "
724 9ce94e7c Arun Sharma
                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
725 9ce94e7c Arun Sharma
                    req->state, req->data_is_ptr, req->addr,
726 9ce94e7c Arun Sharma
                    req->data, req->count, req->size);
727 9ce94e7c Arun Sharma
            destroy_hvm_domain();
728 9ce94e7c Arun Sharma
            return;
729 9ce94e7c Arun Sharma
        }
730 9ce94e7c Arun Sharma
731 9ce94e7c Arun Sharma
        xen_wmb(); /* Update ioreq contents /then/ update state. */
732 9ce94e7c Arun Sharma
733 9ce94e7c Arun Sharma
        /*
734 9ce94e7c Arun Sharma
         * We do this before we send the response so that the tools
735 9ce94e7c Arun Sharma
         * have the opportunity to pick up on the reset before the
736 9ce94e7c Arun Sharma
         * guest resumes and does a hlt with interrupts disabled which
737 9ce94e7c Arun Sharma
         * causes Xen to powerdown the domain.
738 9ce94e7c Arun Sharma
         */
739 9ce94e7c Arun Sharma
        if (vm_running) {
740 9ce94e7c Arun Sharma
            if (qemu_shutdown_requested_get()) {
741 9ce94e7c Arun Sharma
                destroy_hvm_domain();
742 9ce94e7c Arun Sharma
            }
743 9ce94e7c Arun Sharma
            if (qemu_reset_requested_get()) {
744 e063eb1f Jan Kiszka
                qemu_system_reset(VMRESET_REPORT);
745 9ce94e7c Arun Sharma
            }
746 9ce94e7c Arun Sharma
        }
747 9ce94e7c Arun Sharma
748 9ce94e7c Arun Sharma
        req->state = STATE_IORESP_READY;
749 9ce94e7c Arun Sharma
        xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
750 9ce94e7c Arun Sharma
    }
751 9ce94e7c Arun Sharma
}
752 9ce94e7c Arun Sharma
753 0f51726a Stefano Stabellini
static int store_dev_info(int domid, CharDriverState *cs, const char *string)
754 0f51726a Stefano Stabellini
{
755 0f51726a Stefano Stabellini
    struct xs_handle *xs = NULL;
756 0f51726a Stefano Stabellini
    char *path = NULL;
757 0f51726a Stefano Stabellini
    char *newpath = NULL;
758 0f51726a Stefano Stabellini
    char *pts = NULL;
759 0f51726a Stefano Stabellini
    int ret = -1;
760 0f51726a Stefano Stabellini
761 0f51726a Stefano Stabellini
    /* Only continue if we're talking to a pty. */
762 0f51726a Stefano Stabellini
    if (strncmp(cs->filename, "pty:", 4)) {
763 0f51726a Stefano Stabellini
        return 0;
764 0f51726a Stefano Stabellini
    }
765 0f51726a Stefano Stabellini
    pts = cs->filename + 4;
766 0f51726a Stefano Stabellini
767 0f51726a Stefano Stabellini
    /* We now have everything we need to set the xenstore entry. */
768 0f51726a Stefano Stabellini
    xs = xs_open(0);
769 0f51726a Stefano Stabellini
    if (xs == NULL) {
770 0f51726a Stefano Stabellini
        fprintf(stderr, "Could not contact XenStore\n");
771 0f51726a Stefano Stabellini
        goto out;
772 0f51726a Stefano Stabellini
    }
773 0f51726a Stefano Stabellini
774 0f51726a Stefano Stabellini
    path = xs_get_domain_path(xs, domid);
775 0f51726a Stefano Stabellini
    if (path == NULL) {
776 0f51726a Stefano Stabellini
        fprintf(stderr, "xs_get_domain_path() error\n");
777 0f51726a Stefano Stabellini
        goto out;
778 0f51726a Stefano Stabellini
    }
779 0f51726a Stefano Stabellini
    newpath = realloc(path, (strlen(path) + strlen(string) +
780 0f51726a Stefano Stabellini
                strlen("/tty") + 1));
781 0f51726a Stefano Stabellini
    if (newpath == NULL) {
782 0f51726a Stefano Stabellini
        fprintf(stderr, "realloc error\n");
783 0f51726a Stefano Stabellini
        goto out;
784 0f51726a Stefano Stabellini
    }
785 0f51726a Stefano Stabellini
    path = newpath;
786 0f51726a Stefano Stabellini
787 0f51726a Stefano Stabellini
    strcat(path, string);
788 0f51726a Stefano Stabellini
    strcat(path, "/tty");
789 0f51726a Stefano Stabellini
    if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
790 0f51726a Stefano Stabellini
        fprintf(stderr, "xs_write for '%s' fail", string);
791 0f51726a Stefano Stabellini
        goto out;
792 0f51726a Stefano Stabellini
    }
793 0f51726a Stefano Stabellini
    ret = 0;
794 0f51726a Stefano Stabellini
795 0f51726a Stefano Stabellini
out:
796 0f51726a Stefano Stabellini
    free(path);
797 0f51726a Stefano Stabellini
    xs_close(xs);
798 0f51726a Stefano Stabellini
799 0f51726a Stefano Stabellini
    return ret;
800 0f51726a Stefano Stabellini
}
801 0f51726a Stefano Stabellini
802 0f51726a Stefano Stabellini
void xenstore_store_pv_console_info(int i, CharDriverState *chr)
803 0f51726a Stefano Stabellini
{
804 0f51726a Stefano Stabellini
    if (i == 0) {
805 0f51726a Stefano Stabellini
        store_dev_info(xen_domid, chr, "/console");
806 0f51726a Stefano Stabellini
    } else {
807 0f51726a Stefano Stabellini
        char buf[32];
808 0f51726a Stefano Stabellini
        snprintf(buf, sizeof(buf), "/device/console/%d", i);
809 0f51726a Stefano Stabellini
        store_dev_info(xen_domid, chr, buf);
810 0f51726a Stefano Stabellini
    }
811 0f51726a Stefano Stabellini
}
812 0f51726a Stefano Stabellini
813 fb4bb2b5 Anthony PERARD
static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
814 29321335 Anthony PERARD
{
815 29321335 Anthony PERARD
    char path[50];
816 29321335 Anthony PERARD
817 fb4bb2b5 Anthony PERARD
    if (xs == NULL) {
818 fb4bb2b5 Anthony PERARD
        fprintf(stderr, "xenstore connection not initialized\n");
819 fb4bb2b5 Anthony PERARD
        exit(1);
820 fb4bb2b5 Anthony PERARD
    }
821 fb4bb2b5 Anthony PERARD
822 29321335 Anthony PERARD
    snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
823 fb4bb2b5 Anthony PERARD
    if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
824 29321335 Anthony PERARD
        fprintf(stderr, "error recording dm state\n");
825 29321335 Anthony PERARD
        exit(1);
826 29321335 Anthony PERARD
    }
827 29321335 Anthony PERARD
}
828 29321335 Anthony PERARD
829 9ce94e7c Arun Sharma
static void xen_main_loop_prepare(XenIOState *state)
830 9ce94e7c Arun Sharma
{
831 9ce94e7c Arun Sharma
    int evtchn_fd = -1;
832 9ce94e7c Arun Sharma
833 9ce94e7c Arun Sharma
    if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
834 9ce94e7c Arun Sharma
        evtchn_fd = xc_evtchn_fd(state->xce_handle);
835 9ce94e7c Arun Sharma
    }
836 9ce94e7c Arun Sharma
837 9ce94e7c Arun Sharma
    state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
838 9ce94e7c Arun Sharma
                                                 state);
839 9ce94e7c Arun Sharma
    qemu_mod_timer(state->buffered_io_timer, qemu_get_clock_ms(rt_clock));
840 9ce94e7c Arun Sharma
841 9ce94e7c Arun Sharma
    if (evtchn_fd != -1) {
842 9ce94e7c Arun Sharma
        qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
843 9ce94e7c Arun Sharma
    }
844 9ce94e7c Arun Sharma
}
845 9ce94e7c Arun Sharma
846 9ce94e7c Arun Sharma
847 3285cf4f Anthony PERARD
/* Initialise Xen */
848 3285cf4f Anthony PERARD
849 fb4bb2b5 Anthony PERARD
static void xen_change_state_handler(void *opaque, int running, int reason)
850 fb4bb2b5 Anthony PERARD
{
851 fb4bb2b5 Anthony PERARD
    if (running) {
852 fb4bb2b5 Anthony PERARD
        /* record state running */
853 fb4bb2b5 Anthony PERARD
        xenstore_record_dm_state(xenstore, "running");
854 fb4bb2b5 Anthony PERARD
    }
855 fb4bb2b5 Anthony PERARD
}
856 fb4bb2b5 Anthony PERARD
857 fb4bb2b5 Anthony PERARD
static void xen_hvm_change_state_handler(void *opaque, int running, int reason)
858 9ce94e7c Arun Sharma
{
859 9ce94e7c Arun Sharma
    XenIOState *state = opaque;
860 9ce94e7c Arun Sharma
    if (running) {
861 9ce94e7c Arun Sharma
        xen_main_loop_prepare(state);
862 9ce94e7c Arun Sharma
    }
863 9ce94e7c Arun Sharma
}
864 9ce94e7c Arun Sharma
865 9e8dd451 Jan Kiszka
static void xen_exit_notifier(Notifier *n, void *data)
866 9ce94e7c Arun Sharma
{
867 9ce94e7c Arun Sharma
    XenIOState *state = container_of(n, XenIOState, exit);
868 9ce94e7c Arun Sharma
869 9ce94e7c Arun Sharma
    xc_evtchn_close(state->xce_handle);
870 29321335 Anthony PERARD
    xs_daemon_close(state->xenstore);
871 9ce94e7c Arun Sharma
}
872 9ce94e7c Arun Sharma
873 3285cf4f Anthony PERARD
int xen_init(void)
874 3285cf4f Anthony PERARD
{
875 3285cf4f Anthony PERARD
    xen_xc = xen_xc_interface_open(0, 0, 0);
876 3285cf4f Anthony PERARD
    if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
877 3285cf4f Anthony PERARD
        xen_be_printf(NULL, 0, "can't open xen interface\n");
878 3285cf4f Anthony PERARD
        return -1;
879 3285cf4f Anthony PERARD
    }
880 fb4bb2b5 Anthony PERARD
    qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
881 3285cf4f Anthony PERARD
882 3285cf4f Anthony PERARD
    return 0;
883 3285cf4f Anthony PERARD
}
884 29d3ccde Anthony PERARD
885 29d3ccde Anthony PERARD
int xen_hvm_init(void)
886 29d3ccde Anthony PERARD
{
887 9ce94e7c Arun Sharma
    int i, rc;
888 9ce94e7c Arun Sharma
    unsigned long ioreq_pfn;
889 9ce94e7c Arun Sharma
    XenIOState *state;
890 9ce94e7c Arun Sharma
891 7267c094 Anthony Liguori
    state = g_malloc0(sizeof (XenIOState));
892 9ce94e7c Arun Sharma
893 9ce94e7c Arun Sharma
    state->xce_handle = xen_xc_evtchn_open(NULL, 0);
894 9ce94e7c Arun Sharma
    if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
895 9ce94e7c Arun Sharma
        perror("xen: event channel open");
896 9ce94e7c Arun Sharma
        return -errno;
897 9ce94e7c Arun Sharma
    }
898 9ce94e7c Arun Sharma
899 29321335 Anthony PERARD
    state->xenstore = xs_daemon_open();
900 29321335 Anthony PERARD
    if (state->xenstore == NULL) {
901 29321335 Anthony PERARD
        perror("xen: xenstore open");
902 29321335 Anthony PERARD
        return -errno;
903 29321335 Anthony PERARD
    }
904 29321335 Anthony PERARD
905 9ce94e7c Arun Sharma
    state->exit.notify = xen_exit_notifier;
906 9ce94e7c Arun Sharma
    qemu_add_exit_notifier(&state->exit);
907 9ce94e7c Arun Sharma
908 9ce94e7c Arun Sharma
    xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
909 9ce94e7c Arun Sharma
    DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
910 9ce94e7c Arun Sharma
    state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
911 9ce94e7c Arun Sharma
                                              PROT_READ|PROT_WRITE, ioreq_pfn);
912 9ce94e7c Arun Sharma
    if (state->shared_page == NULL) {
913 9ce94e7c Arun Sharma
        hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
914 9ce94e7c Arun Sharma
                 errno, xen_xc);
915 9ce94e7c Arun Sharma
    }
916 9ce94e7c Arun Sharma
917 9ce94e7c Arun Sharma
    xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
918 9ce94e7c Arun Sharma
    DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
919 9ce94e7c Arun Sharma
    state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
920 9ce94e7c Arun Sharma
                                                   PROT_READ|PROT_WRITE, ioreq_pfn);
921 9ce94e7c Arun Sharma
    if (state->buffered_io_page == NULL) {
922 9ce94e7c Arun Sharma
        hw_error("map buffered IO page returned error %d", errno);
923 9ce94e7c Arun Sharma
    }
924 9ce94e7c Arun Sharma
925 7267c094 Anthony Liguori
    state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
926 9ce94e7c Arun Sharma
927 9ce94e7c Arun Sharma
    /* FIXME: how about if we overflow the page here? */
928 9ce94e7c Arun Sharma
    for (i = 0; i < smp_cpus; i++) {
929 9ce94e7c Arun Sharma
        rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
930 9ce94e7c Arun Sharma
                                        xen_vcpu_eport(state->shared_page, i));
931 9ce94e7c Arun Sharma
        if (rc == -1) {
932 9ce94e7c Arun Sharma
            fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
933 9ce94e7c Arun Sharma
            return -1;
934 9ce94e7c Arun Sharma
        }
935 9ce94e7c Arun Sharma
        state->ioreq_local_port[i] = rc;
936 9ce94e7c Arun Sharma
    }
937 9ce94e7c Arun Sharma
938 432d268c Jun Nakajima
    /* Init RAM management */
939 e41d7c69 Jan Kiszka
    xen_map_cache_init();
940 432d268c Jun Nakajima
    xen_ram_init(ram_size);
941 432d268c Jun Nakajima
942 fb4bb2b5 Anthony PERARD
    qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
943 9ce94e7c Arun Sharma
944 b4dd7802 Anthony PERARD
    state->client = xen_cpu_phys_memory_client;
945 b4dd7802 Anthony PERARD
    QLIST_INIT(&state->physmap);
946 b4dd7802 Anthony PERARD
    cpu_register_phys_memory_client(&state->client);
947 b4dd7802 Anthony PERARD
    state->log_for_dirtybit = NULL;
948 b4dd7802 Anthony PERARD
949 ad35a7da Stefano Stabellini
    /* Initialize backend core & drivers */
950 ad35a7da Stefano Stabellini
    if (xen_be_init() != 0) {
951 ad35a7da Stefano Stabellini
        fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
952 ad35a7da Stefano Stabellini
        exit(1);
953 ad35a7da Stefano Stabellini
    }
954 ad35a7da Stefano Stabellini
    xen_be_register("console", &xen_console_ops);
955 37cdfcf1 Stefano Stabellini
    xen_be_register("vkbd", &xen_kbdmouse_ops);
956 ad35a7da Stefano Stabellini
    xen_be_register("qdisk", &xen_blkdev_ops);
957 ad35a7da Stefano Stabellini
958 29d3ccde Anthony PERARD
    return 0;
959 29d3ccde Anthony PERARD
}
960 9ce94e7c Arun Sharma
961 9ce94e7c Arun Sharma
void destroy_hvm_domain(void)
962 9ce94e7c Arun Sharma
{
963 9ce94e7c Arun Sharma
    XenXC xc_handle;
964 9ce94e7c Arun Sharma
    int sts;
965 9ce94e7c Arun Sharma
966 9ce94e7c Arun Sharma
    xc_handle = xen_xc_interface_open(0, 0, 0);
967 9ce94e7c Arun Sharma
    if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
968 9ce94e7c Arun Sharma
        fprintf(stderr, "Cannot acquire xenctrl handle\n");
969 9ce94e7c Arun Sharma
    } else {
970 9ce94e7c Arun Sharma
        sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
971 9ce94e7c Arun Sharma
        if (sts != 0) {
972 9ce94e7c Arun Sharma
            fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
973 9ce94e7c Arun Sharma
                    "sts %d, %s\n", sts, strerror(errno));
974 9ce94e7c Arun Sharma
        } else {
975 9ce94e7c Arun Sharma
            fprintf(stderr, "Issued domain %d poweroff\n", xen_domid);
976 9ce94e7c Arun Sharma
        }
977 9ce94e7c Arun Sharma
        xc_interface_close(xc_handle);
978 9ce94e7c Arun Sharma
    }
979 9ce94e7c Arun Sharma
}