aboutsummaryrefslogtreecommitdiffstats
path: root/tools/tests/mem-sharing/memshrtool.c
blob: b876619ac92050de391496526414814e3361bd18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * memshrtool.c
 *
 * Copyright 2011 GridCentric Inc. (Adin Scannell, Andres Lagar-Cavilla)
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>

#include "xenctrl.h"

static int usage(const char* prog)
{
    printf("usage: %s <command> [args...]\n", prog);
    printf("where <command> may be:\n");
    printf("  info                    - Display total sharing info.\n");
    printf("  enable                  - Enable sharing on a domain.\n");
    printf("  disable                 - Disable sharing on a domain.\n");
    printf("  nominate <domid> <gfn>  - Nominate a page for sharing.\n");
    printf("  share <domid> <gfn> <handle> <source> <source-gfn> <source-handle>\n");
    printf("                          - Share two pages.\n");
    printf("  unshare <domid> <gfn>   - Unshare a page by grabbing a writable map.\n");
    printf("  add-to-physmap <domid> <gfn> <source> <source-gfn> <source-handle>\n");
    printf("                          - Populate a page in a domain with a shared page.\n");
    printf("  debug-gfn <domid> <gfn> - Debug a particular domain and gfn.\n");
    return 1;
}

#define R(f) do { \
    int rc = f; \
    if ( rc < 0 ) { \
        printf("error executing %s: %s\n", #f, \
                ((errno * -1) == XEN_DOMCTL_MEM_SHARING_C_HANDLE_INVALID) ? \
                "problem with client handle" :\
                ((errno * -1) == XEN_DOMCTL_MEM_SHARING_S_HANDLE_INVALID) ? \
                "problem with source handle" : strerror(errno)); \
        return rc; \
    } \
} while(0)

int main(int argc, const char** argv)
{
    const char* cmd = NULL;
    xc_interface *xch = xc_interface_open(0, 0, 0);

    if( argc < 2 )
        return usage(argv[0]);

    cmd = argv[1];

    if( !strcasecmp(cmd, "info") )
    {
        if( argc != 2 )
            return usage(argv[0]);

        printf("used = %ld\n", xc_sharing_used_frames(xch));
        printf("freed = %ld\n", xc_sharing_freed_pages(xch));
    }
    else if( !strcasecmp(cmd, "enable") )
    {
        domid_t domid;

        if( argc != 3 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        R(xc_memshr_control(xch, domid, 1));
    }
    else if( !strcasecmp(cmd, "disable") )
    {
        domid_t domid;

        if( argc != 3 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        R(xc_memshr_control(xch, domid, 0));
    }
    else if( !strcasecmp(cmd, "nominate") )
    {
        domid_t domid;
        unsigned long gfn;
        uint64_t handle;

        if( argc != 4 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        gfn = strtol(argv[3], NULL, 0);
        R(xc_memshr_nominate_gfn(xch, domid, gfn, &handle));
        printf("handle = 0x%08llx\n", (unsigned long long) handle);
    }
    else if( !strcasecmp(cmd, "share") )
    {
        domid_t domid;
        unsigned long gfn;
        uint64_t handle;
        domid_t source_domid;
        unsigned long source_gfn;
        uint64_t source_handle;

        if( argc != 8 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        gfn = strtol(argv[3], NULL, 0);
        handle = strtol(argv[4], NULL, 0);
        source_domid = strtol(argv[5], NULL, 0);
        source_gfn = strtol(argv[6], NULL, 0);
        source_handle = strtol(argv[7], NULL, 0);
        R(xc_memshr_share_gfns(xch, source_domid, source_gfn, source_handle, domid, gfn, handle));
    }
    else if( !strcasecmp(cmd, "unshare") )
    {
        domid_t domid;
        unsigned long gfn;
        void *map;

        if( argc != 4 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        gfn = strtol(argv[3], NULL, 0);
        map = xc_map_foreign_range(xch, domid, 4096, PROT_WRITE, gfn);
        if( map )
            munmap(map, 4096);
        R((int)!map);
    }
    else if( !strcasecmp(cmd, "add-to-physmap") )
    {
        domid_t domid;
        unsigned long gfn;
        domid_t source_domid;
        unsigned long source_gfn;
        uint64_t source_handle;

        if( argc != 7 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        gfn = strtol(argv[3], NULL, 0);
        source_domid = strtol(argv[4], NULL, 0);
        source_gfn = strtol(argv[5], NULL, 0);
        source_handle = strtol(argv[6], NULL, 0);
        R(xc_memshr_add_to_physmap(xch, source_domid, source_gfn, source_handle, domid, gfn));
    }
    else if( !strcasecmp(cmd, "debug-gfn") )
    {
        domid_t domid;
        unsigned long gfn;

        if( argc != 4 )
            return usage(argv[0]);

        domid = strtol(argv[2], NULL, 0);
        gfn = strtol(argv[3], NULL, 0);
        R(xc_memshr_debug_gfn(xch, domid, gfn));
    }

    return 0;
}