ia64/xen-unstable

view tools/xenstat/libxenstat/src/xen-interface.c @ 6439:af7251014caf

Formatting fixes for xenstat and xentop.
Signed-off-by: Josh Triplett <josht@us.ibm.com>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Fri Aug 26 08:47:49 2005 +0000 (2005-08-26)
parents 522bc50588ed
children 9312a3e8a6f8
line source
1 /* xen-interface.c
2 *
3 * Copyright (C) International Business Machines Corp., 2005
4 * Authors: Josh Triplett <josht@us.ibm.com>
5 * Judy Fischbach <jfisch@us.ibm.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 */
18 #include "xen-interface.h"
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/mman.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include "version.h"
27 #include "privcmd.h"
28 #include "xen.h"
30 struct xi_handle {
31 int fd;
32 };
34 /* Initialize for xen-interface. Returns a handle to be used with subsequent
35 * calls to the xen-interface functions or NULL if an error occurs. */
36 xi_handle *xi_init()
37 {
38 xi_handle *handle;
40 handle = (xi_handle *)calloc(1, sizeof(xi_handle));
41 if (handle == NULL)
42 return NULL;
44 handle->fd = open("/proc/xen/privcmd", O_RDWR);
45 if (handle->fd < 0) {
46 perror("Couldn't open /proc/xen/privcmd");
47 free(handle);
48 return NULL;
49 }
51 return handle;
52 }
54 /* Release the handle to libxc, free resources, etc. */
55 void xi_uninit(xi_handle *handle)
56 {
57 close (handle->fd);
58 free (handle);
59 }
61 /* Make simple xen version hypervisor calls */
62 static int xi_make_xen_version_hypercall(xi_handle *handle, long *vnum,
63 xen_extraversion_t *ver)
64 {
65 privcmd_hypercall_t privcmd;
66 multicall_entry_t multicall[2];
67 int ret = 0;
69 /* set up for doing hypercall */
70 privcmd.op = __HYPERVISOR_multicall;
71 privcmd.arg[0] = (unsigned long)multicall;
72 privcmd.arg[1] = 2;
74 /* first one to get xen version number */
75 multicall[0].op = __HYPERVISOR_xen_version;
76 multicall[0].args[0] = (unsigned long)XENVER_version;
78 /* second to get xen version flag */
79 multicall[1].op = __HYPERVISOR_xen_version;
80 multicall[1].args[0] = (unsigned long)XENVER_extraversion;
81 multicall[1].args[1] = (unsigned long)ver;
83 if (mlock( &privcmd, sizeof(privcmd_hypercall_t)) < 0) {
84 perror("Failed to mlock privcmd structure");
85 return -1;
86 }
88 if (mlock( multicall, sizeof(multicall_entry_t)) < 0) {
89 perror("Failed to mlock multicall_entry structure");
90 munlock( &multicall, sizeof(multicall_entry_t));
91 return -1;
92 }
94 if (ioctl( handle->fd, IOCTL_PRIVCMD_HYPERCALL, &privcmd) < 0) {
95 perror("Hypercall failed");
96 ret = -1;
97 }
99 *vnum = multicall[0].result;
101 munlock( &privcmd, sizeof(privcmd_hypercall_t));
102 munlock( &multicall, sizeof(multicall_entry_t));
104 return ret;
105 }
107 /* Make Xen Dom0 op hypervisor call */
108 static int xi_make_dom0_op(xi_handle *handle, dom0_op_t *dom_op,
109 int dom_opcode)
110 {
111 privcmd_hypercall_t privcmd;
112 int ret = 0;
114 /* set up for doing hypercall */
115 privcmd.op = __HYPERVISOR_dom0_op;
116 privcmd.arg[0] = (unsigned long)dom_op;
117 dom_op->cmd = dom_opcode;
118 dom_op->interface_version = DOM0_INTERFACE_VERSION;
120 if (mlock( &privcmd, sizeof(privcmd_hypercall_t)) < 0) {
121 perror("Failed to mlock privcmd structure");
122 return -1;
123 }
125 if (mlock( dom_op, sizeof(dom0_op_t)) < 0) {
126 perror("Failed to mlock dom0_op structure");
127 munlock( &privcmd, sizeof(privcmd_hypercall_t));
128 return -1;
129 }
131 if (ioctl( handle->fd, IOCTL_PRIVCMD_HYPERCALL, &privcmd) < 0) {
132 perror("Hypercall failed");
133 ret = -1;
134 }
136 munlock( &privcmd, sizeof(privcmd_hypercall_t));
137 munlock( dom_op, sizeof(dom0_op_t));
139 return ret;
140 }
142 /* Obtain domain data from dom0 */
143 int xi_get_physinfo(xi_handle *handle, dom0_physinfo_t *physinfo)
144 {
145 dom0_op_t op;
147 if (xi_make_dom0_op(handle, &op, DOM0_PHYSINFO) < 0) {
148 perror("DOM0_PHYSINFO Hypercall failed");
149 return -1;
150 }
152 *physinfo = op.u.physinfo;
153 return 0;
154 }
156 /* Obtain domain data from dom0 */
157 int xi_get_domaininfolist(xi_handle *handle, dom0_getdomaininfo_t *info,
158 unsigned int first_domain, unsigned int max_domains)
159 {
160 dom0_op_t op;
161 op.u.getdomaininfolist.first_domain = first_domain;
162 op.u.getdomaininfolist.max_domains = max_domains;
163 op.u.getdomaininfolist.buffer = info;
165 if (mlock( info, max_domains * sizeof(dom0_getdomaininfo_t)) < 0) {
166 perror("Failed to mlock domaininfo array");
167 return -1;
168 }
170 if (xi_make_dom0_op(handle, &op, DOM0_GETDOMAININFOLIST) < 0) {
171 perror("DOM0_GETDOMAININFOLIST Hypercall failed");
172 return -1;
173 }
175 return op.u.getdomaininfolist.num_domains;
176 }
178 /* Returns cpu usage data from dom0 */
179 long long xi_get_vcpu_usage(xi_handle *handle, unsigned int domain,
180 unsigned int vcpu)
181 {
182 dom0_op_t op;
183 op.u.getvcpucontext.domain = domain;
184 op.u.getvcpucontext.vcpu = vcpu;
185 op.u.getvcpucontext.ctxt = NULL;
187 if (xi_make_dom0_op(handle, &op, DOM0_GETVCPUCONTEXT) < 0) {
188 perror("DOM0_GETVCPUCONTEXT Hypercall failed");
189 return -1;
190 }
192 return op.u.getvcpucontext.cpu_time;
193 }
195 /* gets xen version information from hypervisor */
196 int xi_get_xen_version(xi_handle *handle, long *vnum, xen_extraversion_t *ver)
197 {
198 /* gets the XENVER_version and XENVER_extraversion */
199 if (xi_make_xen_version_hypercall( handle, vnum, ver) < 0) {
200 perror("XEN VERSION Hypercall failed");
201 return -1;
202 }
204 return 0;
205 }