xref: /freebsd/sys/xen/hypervisor.h (revision 7d0d268b8a67f28ccefdd0b8ce6fb38acac78d80)
1 /******************************************************************************
2  * hypervisor.h
3   *
4  * Linux-specific hypervisor handling.
5  *
6  * Copyright (c) 2002, K A Fraser
7  *
8  * $FreeBSD$
9  */
10 
11 #ifndef __HYPERVISOR_H__
12 #define __HYPERVISOR_H__
13 
14 #define is_running_on_xen() 1
15 
16 #ifdef PAE
17 #ifndef CONFIG_X86_PAE
18 #define CONFIG_X86_PAE
19 #endif
20 #endif
21 
22 #include <sys/cdefs.h>
23 #include <sys/systm.h>
24 #include <xen/interface/xen.h>
25 #include <xen/interface/platform.h>
26 #include <xen/interface/event_channel.h>
27 #include <xen/interface/physdev.h>
28 #include <xen/interface/sched.h>
29 #include <xen/interface/callback.h>
30 #include <machine/xen/hypercall.h>
31 
32 #if defined(__amd64__)
33 #define MULTI_UVMFLAGS_INDEX 2
34 #define MULTI_UVMDOMID_INDEX 3
35 #else
36 #define MULTI_UVMFLAGS_INDEX 3
37 #define MULTI_UVMDOMID_INDEX 4
38 #endif
39 
40 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
41 #define is_initial_xendomain() (xen_start_info->flags & SIF_INITDOMAIN)
42 #else
43 #define is_initial_xendomain() 0
44 #endif
45 
46 extern start_info_t *xen_start_info;
47 
48 extern uint64_t get_system_time(int ticks);
49 
50 static inline int
51 HYPERVISOR_console_write(char *str, int count)
52 {
53     return HYPERVISOR_console_io(CONSOLEIO_write, count, str);
54 }
55 
56 static inline void HYPERVISOR_crash(void) __dead2;
57 
58 static inline int
59 HYPERVISOR_yield(void)
60 {
61         int rc = HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
62 
63 #if CONFIG_XEN_COMPAT <= 0x030002
64 	if (rc == -ENOXENSYS)
65 		rc = HYPERVISOR_sched_op_compat(SCHEDOP_yield, 0);
66 #endif
67         return (rc);
68 }
69 
70 static inline int
71 HYPERVISOR_block(
72         void)
73 {
74         int rc = HYPERVISOR_sched_op(SCHEDOP_block, NULL);
75 
76 #if CONFIG_XEN_COMPAT <= 0x030002
77 	if (rc == -ENOXENSYS)
78 		rc = HYPERVISOR_sched_op_compat(SCHEDOP_block, 0);
79 #endif
80         return (rc);
81 }
82 
83 
84 static inline void
85 HYPERVISOR_shutdown(unsigned int reason)
86 {
87 	struct sched_shutdown sched_shutdown = {
88 		.reason = reason
89 	};
90 
91 	HYPERVISOR_sched_op(SCHEDOP_shutdown, &sched_shutdown);
92 #if CONFIG_XEN_COMPAT <= 0x030002
93 	HYPERVISOR_sched_op_compat(SCHEDOP_shutdown, reason);
94 #endif
95 }
96 
97 static inline void
98 HYPERVISOR_crash(void)
99 {
100         HYPERVISOR_shutdown(SHUTDOWN_crash);
101 	/* NEVER REACHED */
102         for (;;) ; /* eliminate noreturn error */
103 }
104 
105 /* Transfer control to hypervisor until an event is detected on one */
106 /* of the specified ports or the specified number of ticks elapse */
107 static inline int
108 HYPERVISOR_poll(
109 	evtchn_port_t *ports, unsigned int nr_ports, int ticks)
110 {
111 	int rc;
112 	struct sched_poll sched_poll = {
113 		.nr_ports = nr_ports,
114 		.timeout = get_system_time(ticks)
115 	};
116 	set_xen_guest_handle(sched_poll.ports, ports);
117 
118 	rc = HYPERVISOR_sched_op(SCHEDOP_poll, &sched_poll);
119 #if CONFIG_XEN_COMPAT <= 0x030002
120 	if (rc == -ENOXENSYS)
121 		rc = HYPERVISOR_sched_op_compat(SCHEDOP_yield, 0);
122 #endif
123 	return (rc);
124 }
125 
126 static inline void
127 MULTI_update_va_mapping(
128 	multicall_entry_t *mcl, unsigned long va,
129         uint64_t new_val, unsigned long flags)
130 {
131     mcl->op = __HYPERVISOR_update_va_mapping;
132     mcl->args[0] = va;
133 #if defined(__amd64__)
134     mcl->args[1] = new_val.pte;
135 #elif defined(PAE)
136     mcl->args[1] = (uint32_t)(new_val & 0xffffffff) ;
137     mcl->args[2] = (uint32_t)(new_val >> 32);
138 #else
139     mcl->args[1] = new_val;
140     mcl->args[2] = 0;
141 #endif
142     mcl->args[MULTI_UVMFLAGS_INDEX] = flags;
143 }
144 
145 #endif /* __HYPERVISOR_H__ */
146