ia64/xen-unstable

diff tools/libxc/ia64/xc_ia64_linux_save.c @ 10692:306d7857928c

[IA64] Save & restore.

xc_ia64_linux_save.c and xc_ia64_linux_restore.c added.
vcpu context has more registers and states (eg: tr registers).
Per cpu irqs are deallocated when cpu is switched off.
#if/#endif added in reboot.c for ia64.

Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author awilliam@xenbuild.aw
date Tue Jul 11 12:51:18 2006 -0600 (2006-07-11)
parents
children 86e5d8458c08
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/libxc/ia64/xc_ia64_linux_save.c	Tue Jul 11 12:51:18 2006 -0600
     1.3 @@ -0,0 +1,295 @@
     1.4 +/******************************************************************************
     1.5 + * xc_ia64_linux_save.c
     1.6 + *
     1.7 + * Save the state of a running Linux session.
     1.8 + *
     1.9 + * Copyright (c) 2003, K A Fraser.
    1.10 + *  Rewritten for ia64 by Tristan Gingold <tristan.gingold@bull.net>
    1.11 + */
    1.12 +
    1.13 +#include <inttypes.h>
    1.14 +#include <time.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <unistd.h>
    1.17 +#include <sys/time.h>
    1.18 +
    1.19 +#include "xg_private.h"
    1.20 +
    1.21 +/* total number of pages used by the current guest */
    1.22 +static unsigned long max_pfn;
    1.23 +
    1.24 +static inline ssize_t
    1.25 +write_exact(int fd, void *buf, size_t count)
    1.26 +{
    1.27 +    if (write(fd, buf, count) != count)
    1.28 +        return 0;
    1.29 +    return 1;
    1.30 +}
    1.31 +
    1.32 +static int
    1.33 +suspend_and_state(int (*suspend)(int), int xc_handle, int io_fd,
    1.34 +                  int dom, xc_dominfo_t *info)
    1.35 +{
    1.36 +    int i = 0;
    1.37 +
    1.38 +    if (!(*suspend)(dom)) {
    1.39 +        ERR("Suspend request failed");
    1.40 +        return -1;
    1.41 +    }
    1.42 +
    1.43 +retry:
    1.44 +
    1.45 +    if (xc_domain_getinfo(xc_handle, dom, 1, info) != 1) {
    1.46 +        ERR("Could not get domain info");
    1.47 +        return -1;
    1.48 +    }
    1.49 +
    1.50 +    if (info->shutdown && info->shutdown_reason == SHUTDOWN_suspend)
    1.51 +        return 0; // success
    1.52 +
    1.53 +    if (info->paused) {
    1.54 +        // try unpausing domain, wait, and retest
    1.55 +        xc_domain_unpause(xc_handle, dom);
    1.56 +
    1.57 +        ERR("Domain was paused. Wait and re-test.");
    1.58 +        usleep(10000);  // 10ms
    1.59 +
    1.60 +        goto retry;
    1.61 +    }
    1.62 +
    1.63 +
    1.64 +    if(++i < 100) {
    1.65 +        ERR("Retry suspend domain.");
    1.66 +        usleep(10000);  // 10ms
    1.67 +        goto retry;
    1.68 +    }
    1.69 +
    1.70 +    ERR("Unable to suspend domain.");
    1.71 +
    1.72 +    return -1;
    1.73 +}
    1.74 +
    1.75 +int
    1.76 +xc_linux_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters,
    1.77 +              uint32_t max_factor, uint32_t flags, int (*suspend)(int))
    1.78 +{
    1.79 +    DECLARE_DOM0_OP;
    1.80 +    xc_dominfo_t info;
    1.81 +
    1.82 +    int rc = 1;
    1.83 +    unsigned long N;
    1.84 +
    1.85 +    //int live  = (flags & XCFLAGS_LIVE);
    1.86 +    int debug = (flags & XCFLAGS_DEBUG);
    1.87 +
    1.88 +    /* The new domain's shared-info frame number. */
    1.89 +    unsigned long shared_info_frame;
    1.90 +
    1.91 +    /* A copy of the CPU context of the guest. */
    1.92 +    vcpu_guest_context_t ctxt;
    1.93 +
    1.94 +    unsigned long *page_array = NULL;
    1.95 +
    1.96 +    /* Live mapping of shared info structure */
    1.97 +    shared_info_t *live_shinfo = NULL;
    1.98 +
    1.99 +    char *mem;
   1.100 +
   1.101 +    if (debug)
   1.102 +        fprintf (stderr, "xc_linux_save (ia64): started dom=%d\n", dom);
   1.103 +
   1.104 +    if (xc_domain_getinfo(xc_handle, dom, 1, &info) != 1) {
   1.105 +        ERR("Could not get domain info");
   1.106 +        return 1;
   1.107 +    }
   1.108 +
   1.109 +    shared_info_frame = info.shared_info_frame;
   1.110 +
   1.111 +#if 0
   1.112 +    /* cheesy sanity check */
   1.113 +    if ((info.max_memkb >> (PAGE_SHIFT - 10)) > max_mfn) {
   1.114 +        ERR("Invalid state record -- pfn count out of range: %lu",
   1.115 +            (info.max_memkb >> (PAGE_SHIFT - 10)));
   1.116 +        goto out;
   1.117 +     }
   1.118 +#endif
   1.119 +
   1.120 +    /* Map the shared info frame */
   1.121 +    live_shinfo = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
   1.122 +                                       PROT_READ, shared_info_frame);
   1.123 +    if (!live_shinfo) {
   1.124 +        ERR("Couldn't map live_shinfo");
   1.125 +        goto out;
   1.126 +    }
   1.127 +
   1.128 +    max_pfn = info.max_memkb >> (PAGE_SHIFT - 10);
   1.129 +
   1.130 +
   1.131 +    /* This is a non-live suspend. Issue the call back to get the
   1.132 +       domain suspended */
   1.133 +
   1.134 +    if (suspend_and_state(suspend, xc_handle, io_fd, dom, &info)) {
   1.135 +        ERR("Domain appears not to have suspended");
   1.136 +        goto out;
   1.137 +    }
   1.138 +
   1.139 +    page_array = malloc(max_pfn * sizeof(unsigned long));
   1.140 +    if (page_array == NULL) {
   1.141 +        ERR("Could not allocate memory");
   1.142 +        goto out;
   1.143 +    }
   1.144 +
   1.145 +    if (xc_ia64_get_pfn_list(xc_handle, dom, page_array,
   1.146 +                             0, max_pfn) != max_pfn) {
   1.147 +        ERR("Could not get the page frame list");
   1.148 +        goto out;
   1.149 +    }
   1.150 +
   1.151 +    /* This is expected by xm restore.  */
   1.152 +    if (!write_exact(io_fd, &max_pfn, sizeof(unsigned long))) {
   1.153 +        ERR("write: max_pfn");
   1.154 +        goto out;
   1.155 +    }
   1.156 +
   1.157 +    /* xc_linux_restore starts to read here.  */
   1.158 +    /* Write a version number.  This can avoid searching for a stupid bug
   1.159 +       if the format change.
   1.160 +       The version is hard-coded, don't forget to change the restore code
   1.161 +       too!  */
   1.162 +    N = 1;
   1.163 +    if (!write_exact(io_fd, &N, sizeof(unsigned long))) {
   1.164 +        ERR("write: version");
   1.165 +        goto out;
   1.166 +    }
   1.167 +
   1.168 +    op.cmd = DOM0_DOMAIN_SETUP;
   1.169 +    op.u.domain_setup.domain = (domid_t)dom;
   1.170 +    op.u.domain_setup.flags = XEN_DOMAINSETUP_query;
   1.171 +    if (xc_dom0_op(xc_handle, &op) < 0) {
   1.172 +        ERR("Could not get domain setup");
   1.173 +        goto out;
   1.174 +    }
   1.175 +    op.u.domain_setup.domain = 0;
   1.176 +    if (!write_exact(io_fd, &op.u.domain_setup, sizeof(op.u.domain_setup))) {
   1.177 +        ERR("write: domain setup");
   1.178 +        goto out;
   1.179 +    }
   1.180 +
   1.181 +    /* Start writing out the saved-domain record. */
   1.182 +    for (N = 0; N < max_pfn; N++) {
   1.183 +        if (page_array[N] == INVALID_MFN)
   1.184 +            continue;
   1.185 +        if (debug)
   1.186 +            fprintf (stderr, "xc_linux_save: page %lx (%lu/%lu)\n",
   1.187 +                     page_array[N], N, max_pfn);
   1.188 +
   1.189 +        if (!write_exact(io_fd, &N, sizeof(N))) {
   1.190 +            ERR("write: max_pfn");
   1.191 +            goto out;
   1.192 +        }
   1.193 +
   1.194 +        mem = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
   1.195 +                                   PROT_READ|PROT_WRITE, page_array[N]);
   1.196 +        if (mem == NULL) {
   1.197 +            ERR("cannot map page");
   1.198 +            goto out;
   1.199 +        }
   1.200 +        if (write(io_fd, mem, PAGE_SIZE) != PAGE_SIZE) {
   1.201 +            ERR("Error when writing to state file (5)");
   1.202 +            goto out;
   1.203 +        }
   1.204 +        munmap(mem, PAGE_SIZE);
   1.205 +    }
   1.206 +
   1.207 +    fprintf (stderr, "All memory is saved\n");
   1.208 +
   1.209 +    /* terminate */
   1.210 +    N = INVALID_MFN;
   1.211 +    if (!write_exact(io_fd, &N, sizeof(N))) {
   1.212 +        ERR("Error when writing to state file (6)");
   1.213 +        goto out;
   1.214 +    }
   1.215 +
   1.216 +    /* Send through a list of all the PFNs that were not in map at the close */
   1.217 +    {
   1.218 +        unsigned int i,j;
   1.219 +        unsigned long pfntab[1024];
   1.220 +
   1.221 +        for (i = 0, j = 0; i < max_pfn; i++) {
   1.222 +            if (page_array[i] == INVALID_MFN)
   1.223 +                j++;
   1.224 +        }
   1.225 +
   1.226 +        if (!write_exact(io_fd, &j, sizeof(unsigned int))) {
   1.227 +            ERR("Error when writing to state file (6a)");
   1.228 +            goto out;
   1.229 +        }
   1.230 +
   1.231 +        for (i = 0, j = 0; i < max_pfn; ) {
   1.232 +
   1.233 +            if (page_array[i] == INVALID_MFN)
   1.234 +                pfntab[j++] = i;
   1.235 +
   1.236 +            i++;
   1.237 +            if (j == 1024 || i == max_pfn) {
   1.238 +                if (!write_exact(io_fd, &pfntab, sizeof(unsigned long)*j)) {
   1.239 +                    ERR("Error when writing to state file (6b)");
   1.240 +                    goto out;
   1.241 +                }
   1.242 +                j = 0;
   1.243 +            }
   1.244 +        }
   1.245 +
   1.246 +    }
   1.247 +
   1.248 +    if (xc_vcpu_getcontext(xc_handle, dom, 0, &ctxt)) {
   1.249 +        ERR("Could not get vcpu context");
   1.250 +        goto out;
   1.251 +    }
   1.252 +
   1.253 +    if (!write_exact(io_fd, &ctxt, sizeof(ctxt))) {
   1.254 +        ERR("Error when writing to state file (1)");
   1.255 +        goto out;
   1.256 +    }
   1.257 +
   1.258 +    mem = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
   1.259 +                               PROT_READ|PROT_WRITE, ctxt.privregs_pfn);
   1.260 +    if (mem == NULL) {
   1.261 +        ERR("cannot map privreg page");
   1.262 +        goto out;
   1.263 +    }
   1.264 +    if (write(io_fd, mem, PAGE_SIZE) != PAGE_SIZE) {
   1.265 +        ERR("Error when writing privreg to state file (5)");
   1.266 +        goto out;
   1.267 +    }
   1.268 +    munmap(mem, PAGE_SIZE);    
   1.269 +
   1.270 +    if (!write_exact(io_fd, live_shinfo, PAGE_SIZE)) {
   1.271 +        ERR("Error when writing to state file (1)");
   1.272 +        goto out;
   1.273 +    }
   1.274 +
   1.275 +    /* Success! */
   1.276 +    rc = 0;
   1.277 +
   1.278 + out:
   1.279 +
   1.280 +    free (page_array);
   1.281 +
   1.282 +    if (live_shinfo)
   1.283 +        munmap(live_shinfo, PAGE_SIZE);
   1.284 +
   1.285 +    fprintf(stderr,"Save exit rc=%d\n",rc);
   1.286 +
   1.287 +    return !!rc;
   1.288 +}
   1.289 +
   1.290 +/*
   1.291 + * Local variables:
   1.292 + * mode: C
   1.293 + * c-set-style: "BSD"
   1.294 + * c-basic-offset: 4
   1.295 + * tab-width: 4
   1.296 + * indent-tabs-mode: nil
   1.297 + * End:
   1.298 + */