Xen Test Framework
lib.c
Go to the documentation of this file.
1#include <xtf/framework.h>
2#include <xtf/lib.h>
3#include <xtf/traps.h>
4#include <xtf/hypercall.h>
5#include <xtf/xenstore.h>
6
7#ifndef isdigit
8/* Avoid pulling in all of ctypes just for this. */
9static int isdigit(int c)
10{
11 return c >= '0' && c <= '9';
12}
13#endif
14
15void __noreturn panic(const char *fmt, ...)
16{
17 va_list args;
18
19 printk("******************************\n");
20
21 printk("PANIC: ");
22 va_start(args, fmt);
23 vprintk(fmt, args);
24 va_end(args);
25
26 printk("******************************\n");
27
30}
31
33{
34 int i;
35 xen_sysctl_t op = { .cmd = 0 };
36
37 for ( i = 0; i < 128; i++ )
38 {
39 op.interface_version = i;
40 if ( hypercall_sysctl(&op) != -EACCES )
41 return i;
42 }
43
44 return -1;
45}
46
47static int evtchn_get_domid(void)
48{
49 union {
50 struct evtchn_alloc_unbound alloc;
51 struct evtchn_status status;
52 } op;
54 int rc;
55
56 op.alloc.dom = DOMID_SELF;
57 op.alloc.remote_dom = DOMID_SELF;
58 rc = hypercall_evtchn_alloc_unbound(&op.alloc);
59 if ( rc )
60 return -1;
61
62 port = op.alloc.port;
63
64 op.status.dom = DOMID_SELF;
65 op.status.port = port;
66 rc = hypercall_evtchn_status(&op.status);
67
68 /* Clean up the allocated port, irrespective of other failures. */
70
71 if ( rc )
72 return -1;
73
74 return op.status.unbound.dom;
75}
76
77static int xenstore_get_domid(void)
78{
79 int rc = xenstore_init();
80
81 if ( rc )
82 return -1;
83
84 const char *str = xenstore_read("domid");
85 unsigned int domid = 0;
86
87 if ( !str || !isdigit(*str) )
88 return -1;
89
90 while ( isdigit(*str) )
91 {
92 domid = domid * 10 + (*str - '0');
93 str++;
94 }
95
96 if ( domid >= DOMID_FIRST_RESERVED )
97 return -1;
98
99 return domid;
100}
101
103{
104 int rc;
105
106 rc = arch_get_domid();
107 if ( rc >= 0 )
108 return rc;
109
110 rc = evtchn_get_domid();
111 if ( rc >= 0 )
112 return rc;
113
114 rc = xenstore_get_domid();
115 if ( rc >= 0 )
116 return rc;
117
118 return -1;
119}
120
121/*
122 * Local variables:
123 * mode: C
124 * c-file-style: "BSD"
125 * c-basic-offset: 4
126 * tab-width: 4
127 * indent-tabs-mode: nil
128 * End:
129 */
static unsigned int str(void)
Definition: lib.h:366
int arch_get_domid(void)
Definition: setup.c:282
#define __noreturn
Definition: compiler.h:10
void vprintk(const char *fmt, va_list args)
Definition: console.c:119
void printk(const char *fmt,...)
Definition: console.c:134
#define EACCES
Definition: errno.h:27
uint32_t evtchn_port_t
Definition: event_channel.h:13
Interfaces used by common code, needing to be implemented by arch/environment specific code.
void arch_crash_hard(void)
Definition: traps.c:142
static int hypercall_evtchn_alloc_unbound(struct evtchn_alloc_unbound *ub)
Definition: hypercall.h:239
static int hypercall_evtchn_close(evtchn_port_t port)
Definition: hypercall.h:224
static long hypercall_sysctl(xen_sysctl_t *arg)
Definition: hypercall.h:183
static int hypercall_evtchn_status(struct evtchn_status *status)
Definition: hypercall.h:234
static long hypercall_shutdown(unsigned int reason)
Definition: hypercall.h:202
int xtf_get_domid(void)
Obtain the current domid.
Definition: lib.c:102
static int evtchn_get_domid(void)
Definition: lib.c:47
static int xenstore_get_domid(void)
Definition: lib.c:77
int xtf_probe_sysctl_interface_version(void)
Probe for the SYSCTL_INTERFACE_VERSION in use by the hypervisor.
Definition: lib.c:32
void panic(const char *fmt,...)
Definition: lib.c:15
static int isdigit(int c)
Definition: lib.c:9
static enum test_status status
Current status of this test.
Definition: report.c:14
#define SHUTDOWN_crash
Definition: sched.h:29
#define va_end(v)
Definition: stdarg.h:11
#define va_start(v, l)
Definition: stdarg.h:10
__builtin_va_list va_list
Definition: stdarg.h:9
evtchn_port_t port
Definition: event_channel.h:19
uint32_t cmd
Definition: sysctl.h:191
uint32_t interface_version
Definition: sysctl.h:193
#define DOMID_SELF
Definition: xen.h:70
#define DOMID_FIRST_RESERVED
Definition: xen.h:69
const char * xenstore_read(const char *path)
Issue a XS_READ operation for key, waiting synchronously for the reply.
Definition: xenbus.c:139
int xenstore_init(void)
Initialise XTF ready for xenstore communication, and determine whether xenstored is listening.
Definition: xenbus.c:109
Xenstore driver.