aboutsummaryrefslogtreecommitdiffstats
path: root/tools/internal/dom0_defs.h
blob: 2a59c2553b97b2e338fa31269d9046b4100634a4 (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
#ifndef __DOM0_DEFS_H__
#define __DOM0_DEFS_H__

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>

typedef unsigned char      u8;
typedef unsigned short     u16;
typedef unsigned long      u32;
typedef unsigned long long u64;
typedef signed char        s8;
typedef signed short       s16;
typedef signed long        s32;
typedef signed long long   s64;

#include "mem_defs.h"
#include <asm-xeno/proc_cmd.h>
#include <hypervisor-ifs/hypervisor-if.h>
#include <hypervisor-ifs/dom0_ops.h>

#define ERROR(_m)  \
    fprintf(stderr, "ERROR: %s\n", (_m))

#define PERROR(_m) \
    fprintf(stderr, "ERROR: %s (%d = %s)\n", (_m), errno, strerror(errno))

static inline int do_privcmd(unsigned int cmd, unsigned long data)
{
    int fd, ret;

    if ( (fd = open("/proc/xeno/privcmd", O_RDWR)) < 0 )
    {
        PERROR("Could not open proc interface");
        return -1;
    }

    if ( (ret = ioctl(fd, cmd, data)) < 0 )
    {
#ifndef SILENT_ERRORS_FROM_XEN
        PERROR("Error when executing privileged control ioctl");
#endif
        close(fd);
        return -1;
    }

    close(fd);
    return ret;
}

static inline int xldev_to_physdev(int xldev)
{
    return do_privcmd(IOCTL_PRIVCMD_LINDEV_TO_XENDEV, 
                      (unsigned long)xldev);
}

static inline int physdev_to_xldev(int physdev)
{
    return do_privcmd(IOCTL_PRIVCMD_XENDEV_TO_LINDEV, 
                      (unsigned long)physdev);
}

static inline int do_xen_blkmsg(privcmd_blkmsg_t *blkmsg)
{
    return do_privcmd(IOCTL_PRIVCMD_BLKMSG, (unsigned long)blkmsg);
}

static inline int do_xen_hypercall(privcmd_hypercall_t *hypercall)
{
    return do_privcmd(IOCTL_PRIVCMD_HYPERCALL, (unsigned long)hypercall);
}

static inline int do_dom0_op(dom0_op_t *op)
{
    int ret = -1;
    privcmd_hypercall_t hypercall;

    hypercall.op     = __HYPERVISOR_dom0_op;
    hypercall.arg[0] = (unsigned long)op;

    if ( mlock(op, sizeof(*op)) != 0 )
    {
        PERROR("Could not lock memory for Xen hypercall");
        goto out1;
    }

    if ( do_xen_hypercall(&hypercall) < 0 )
        goto out2;

    ret = 0;

 out2: (void)munlock(op, sizeof(*op));
 out1: return ret;
}

static inline int do_network_op(network_op_t *op)
{
    int ret = -1;
    privcmd_hypercall_t hypercall;

    hypercall.op     = __HYPERVISOR_network_op;
    hypercall.arg[0] = (unsigned long)op;

    if ( mlock(op, sizeof(*op)) != 0 )
    {
        PERROR("Could not lock memory for Xen hypercall");
        goto out1;
    }

    if ( do_xen_hypercall(&hypercall) < 0 )
        goto out2;

    ret = 0;

 out2: (void)munlock(op, sizeof(*op));
 out1: return ret;
}

#endif /* __DOM0_DEFS_H__ */