xen.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /******************************************************************************
  2. * xen.h
  3. *
  4. * Guest OS interface to Xen.
  5. *
  6. * Copyright (c) 2004, K A Fraser
  7. */
  8. #ifndef __XEN_PUBLIC_XEN_H__
  9. #define __XEN_PUBLIC_XEN_H__
  10. #include <asm/xen/interface.h>
  11. #include <asm/pvclock-abi.h>
  12. /*
  13. * XEN "SYSTEM CALLS" (a.k.a. HYPERCALLS).
  14. */
  15. /*
  16. * x86_32: EAX = vector; EBX, ECX, EDX, ESI, EDI = args 1, 2, 3, 4, 5.
  17. * EAX = return value
  18. * (argument registers may be clobbered on return)
  19. * x86_64: RAX = vector; RDI, RSI, RDX, R10, R8, R9 = args 1, 2, 3, 4, 5, 6.
  20. * RAX = return value
  21. * (argument registers not clobbered on return; RCX, R11 are)
  22. */
  23. #define __HYPERVISOR_set_trap_table 0
  24. #define __HYPERVISOR_mmu_update 1
  25. #define __HYPERVISOR_set_gdt 2
  26. #define __HYPERVISOR_stack_switch 3
  27. #define __HYPERVISOR_set_callbacks 4
  28. #define __HYPERVISOR_fpu_taskswitch 5
  29. #define __HYPERVISOR_sched_op 6
  30. #define __HYPERVISOR_dom0_op 7
  31. #define __HYPERVISOR_set_debugreg 8
  32. #define __HYPERVISOR_get_debugreg 9
  33. #define __HYPERVISOR_update_descriptor 10
  34. #define __HYPERVISOR_memory_op 12
  35. #define __HYPERVISOR_multicall 13
  36. #define __HYPERVISOR_update_va_mapping 14
  37. #define __HYPERVISOR_set_timer_op 15
  38. #define __HYPERVISOR_event_channel_op_compat 16
  39. #define __HYPERVISOR_xen_version 17
  40. #define __HYPERVISOR_console_io 18
  41. #define __HYPERVISOR_physdev_op_compat 19
  42. #define __HYPERVISOR_grant_table_op 20
  43. #define __HYPERVISOR_vm_assist 21
  44. #define __HYPERVISOR_update_va_mapping_otherdomain 22
  45. #define __HYPERVISOR_iret 23 /* x86 only */
  46. #define __HYPERVISOR_vcpu_op 24
  47. #define __HYPERVISOR_set_segment_base 25 /* x86/64 only */
  48. #define __HYPERVISOR_mmuext_op 26
  49. #define __HYPERVISOR_acm_op 27
  50. #define __HYPERVISOR_nmi_op 28
  51. #define __HYPERVISOR_sched_op_new 29
  52. #define __HYPERVISOR_callback_op 30
  53. #define __HYPERVISOR_xenoprof_op 31
  54. #define __HYPERVISOR_event_channel_op 32
  55. #define __HYPERVISOR_physdev_op 33
  56. #define __HYPERVISOR_hvm_op 34
  57. /* Architecture-specific hypercall definitions. */
  58. #define __HYPERVISOR_arch_0 48
  59. #define __HYPERVISOR_arch_1 49
  60. #define __HYPERVISOR_arch_2 50
  61. #define __HYPERVISOR_arch_3 51
  62. #define __HYPERVISOR_arch_4 52
  63. #define __HYPERVISOR_arch_5 53
  64. #define __HYPERVISOR_arch_6 54
  65. #define __HYPERVISOR_arch_7 55
  66. /*
  67. * VIRTUAL INTERRUPTS
  68. *
  69. * Virtual interrupts that a guest OS may receive from Xen.
  70. */
  71. #define VIRQ_TIMER 0 /* Timebase update, and/or requested timeout. */
  72. #define VIRQ_DEBUG 1 /* Request guest to dump debug info. */
  73. #define VIRQ_CONSOLE 2 /* (DOM0) Bytes received on emergency console. */
  74. #define VIRQ_DOM_EXC 3 /* (DOM0) Exceptional event for some domain. */
  75. #define VIRQ_DEBUGGER 6 /* (DOM0) A domain has paused for debugging. */
  76. /* Architecture-specific VIRQ definitions. */
  77. #define VIRQ_ARCH_0 16
  78. #define VIRQ_ARCH_1 17
  79. #define VIRQ_ARCH_2 18
  80. #define VIRQ_ARCH_3 19
  81. #define VIRQ_ARCH_4 20
  82. #define VIRQ_ARCH_5 21
  83. #define VIRQ_ARCH_6 22
  84. #define VIRQ_ARCH_7 23
  85. #define NR_VIRQS 24
  86. /*
  87. * MMU-UPDATE REQUESTS
  88. *
  89. * HYPERVISOR_mmu_update() accepts a list of (ptr, val) pairs.
  90. * A foreigndom (FD) can be specified (or DOMID_SELF for none).
  91. * Where the FD has some effect, it is described below.
  92. * ptr[1:0] specifies the appropriate MMU_* command.
  93. *
  94. * ptr[1:0] == MMU_NORMAL_PT_UPDATE:
  95. * Updates an entry in a page table. If updating an L1 table, and the new
  96. * table entry is valid/present, the mapped frame must belong to the FD, if
  97. * an FD has been specified. If attempting to map an I/O page then the
  98. * caller assumes the privilege of the FD.
  99. * FD == DOMID_IO: Permit /only/ I/O mappings, at the priv level of the caller.
  100. * FD == DOMID_XEN: Map restricted areas of Xen's heap space.
  101. * ptr[:2] -- Machine address of the page-table entry to modify.
  102. * val -- Value to write.
  103. *
  104. * ptr[1:0] == MMU_MACHPHYS_UPDATE:
  105. * Updates an entry in the machine->pseudo-physical mapping table.
  106. * ptr[:2] -- Machine address within the frame whose mapping to modify.
  107. * The frame must belong to the FD, if one is specified.
  108. * val -- Value to write into the mapping entry.
  109. *
  110. * ptr[1:0] == MMU_PT_UPDATE_PRESERVE_AD:
  111. * As MMU_NORMAL_PT_UPDATE above, but A/D bits currently in the PTE are ORed
  112. * with those in @val.
  113. */
  114. #define MMU_NORMAL_PT_UPDATE 0 /* checked '*ptr = val'. ptr is MA. */
  115. #define MMU_MACHPHYS_UPDATE 1 /* ptr = MA of frame to modify entry for */
  116. #define MMU_PT_UPDATE_PRESERVE_AD 2 /* atomically: *ptr = val | (*ptr&(A|D)) */
  117. /*
  118. * MMU EXTENDED OPERATIONS
  119. *
  120. * HYPERVISOR_mmuext_op() accepts a list of mmuext_op structures.
  121. * A foreigndom (FD) can be specified (or DOMID_SELF for none).
  122. * Where the FD has some effect, it is described below.
  123. *
  124. * cmd: MMUEXT_(UN)PIN_*_TABLE
  125. * mfn: Machine frame number to be (un)pinned as a p.t. page.
  126. * The frame must belong to the FD, if one is specified.
  127. *
  128. * cmd: MMUEXT_NEW_BASEPTR
  129. * mfn: Machine frame number of new page-table base to install in MMU.
  130. *
  131. * cmd: MMUEXT_NEW_USER_BASEPTR [x86/64 only]
  132. * mfn: Machine frame number of new page-table base to install in MMU
  133. * when in user space.
  134. *
  135. * cmd: MMUEXT_TLB_FLUSH_LOCAL
  136. * No additional arguments. Flushes local TLB.
  137. *
  138. * cmd: MMUEXT_INVLPG_LOCAL
  139. * linear_addr: Linear address to be flushed from the local TLB.
  140. *
  141. * cmd: MMUEXT_TLB_FLUSH_MULTI
  142. * vcpumask: Pointer to bitmap of VCPUs to be flushed.
  143. *
  144. * cmd: MMUEXT_INVLPG_MULTI
  145. * linear_addr: Linear address to be flushed.
  146. * vcpumask: Pointer to bitmap of VCPUs to be flushed.
  147. *
  148. * cmd: MMUEXT_TLB_FLUSH_ALL
  149. * No additional arguments. Flushes all VCPUs' TLBs.
  150. *
  151. * cmd: MMUEXT_INVLPG_ALL
  152. * linear_addr: Linear address to be flushed from all VCPUs' TLBs.
  153. *
  154. * cmd: MMUEXT_FLUSH_CACHE
  155. * No additional arguments. Writes back and flushes cache contents.
  156. *
  157. * cmd: MMUEXT_SET_LDT
  158. * linear_addr: Linear address of LDT base (NB. must be page-aligned).
  159. * nr_ents: Number of entries in LDT.
  160. */
  161. #define MMUEXT_PIN_L1_TABLE 0
  162. #define MMUEXT_PIN_L2_TABLE 1
  163. #define MMUEXT_PIN_L3_TABLE 2
  164. #define MMUEXT_PIN_L4_TABLE 3
  165. #define MMUEXT_UNPIN_TABLE 4
  166. #define MMUEXT_NEW_BASEPTR 5
  167. #define MMUEXT_TLB_FLUSH_LOCAL 6
  168. #define MMUEXT_INVLPG_LOCAL 7
  169. #define MMUEXT_TLB_FLUSH_MULTI 8
  170. #define MMUEXT_INVLPG_MULTI 9
  171. #define MMUEXT_TLB_FLUSH_ALL 10
  172. #define MMUEXT_INVLPG_ALL 11
  173. #define MMUEXT_FLUSH_CACHE 12
  174. #define MMUEXT_SET_LDT 13
  175. #define MMUEXT_NEW_USER_BASEPTR 15
  176. #ifndef __ASSEMBLY__
  177. struct mmuext_op {
  178. unsigned int cmd;
  179. union {
  180. /* [UN]PIN_TABLE, NEW_BASEPTR, NEW_USER_BASEPTR */
  181. unsigned long mfn;
  182. /* INVLPG_LOCAL, INVLPG_ALL, SET_LDT */
  183. unsigned long linear_addr;
  184. } arg1;
  185. union {
  186. /* SET_LDT */
  187. unsigned int nr_ents;
  188. /* TLB_FLUSH_MULTI, INVLPG_MULTI */
  189. void *vcpumask;
  190. } arg2;
  191. };
  192. DEFINE_GUEST_HANDLE_STRUCT(mmuext_op);
  193. #endif
  194. /* These are passed as 'flags' to update_va_mapping. They can be ORed. */
  195. /* When specifying UVMF_MULTI, also OR in a pointer to a CPU bitmap. */
  196. /* UVMF_LOCAL is merely UVMF_MULTI with a NULL bitmap pointer. */
  197. #define UVMF_NONE (0UL<<0) /* No flushing at all. */
  198. #define UVMF_TLB_FLUSH (1UL<<0) /* Flush entire TLB(s). */
  199. #define UVMF_INVLPG (2UL<<0) /* Flush only one entry. */
  200. #define UVMF_FLUSHTYPE_MASK (3UL<<0)
  201. #define UVMF_MULTI (0UL<<2) /* Flush subset of TLBs. */
  202. #define UVMF_LOCAL (0UL<<2) /* Flush local TLB. */
  203. #define UVMF_ALL (1UL<<2) /* Flush all TLBs. */
  204. /*
  205. * Commands to HYPERVISOR_console_io().
  206. */
  207. #define CONSOLEIO_write 0
  208. #define CONSOLEIO_read 1
  209. /*
  210. * Commands to HYPERVISOR_vm_assist().
  211. */
  212. #define VMASST_CMD_enable 0
  213. #define VMASST_CMD_disable 1
  214. #define VMASST_TYPE_4gb_segments 0
  215. #define VMASST_TYPE_4gb_segments_notify 1
  216. #define VMASST_TYPE_writable_pagetables 2
  217. #define VMASST_TYPE_pae_extended_cr3 3
  218. #define MAX_VMASST_TYPE 3
  219. #ifndef __ASSEMBLY__
  220. typedef uint16_t domid_t;
  221. /* Domain ids >= DOMID_FIRST_RESERVED cannot be used for ordinary domains. */
  222. #define DOMID_FIRST_RESERVED (0x7FF0U)
  223. /* DOMID_SELF is used in certain contexts to refer to oneself. */
  224. #define DOMID_SELF (0x7FF0U)
  225. /*
  226. * DOMID_IO is used to restrict page-table updates to mapping I/O memory.
  227. * Although no Foreign Domain need be specified to map I/O pages, DOMID_IO
  228. * is useful to ensure that no mappings to the OS's own heap are accidentally
  229. * installed. (e.g., in Linux this could cause havoc as reference counts
  230. * aren't adjusted on the I/O-mapping code path).
  231. * This only makes sense in MMUEXT_SET_FOREIGNDOM, but in that context can
  232. * be specified by any calling domain.
  233. */
  234. #define DOMID_IO (0x7FF1U)
  235. /*
  236. * DOMID_XEN is used to allow privileged domains to map restricted parts of
  237. * Xen's heap space (e.g., the machine_to_phys table).
  238. * This only makes sense in MMUEXT_SET_FOREIGNDOM, and is only permitted if
  239. * the caller is privileged.
  240. */
  241. #define DOMID_XEN (0x7FF2U)
  242. /*
  243. * Send an array of these to HYPERVISOR_mmu_update().
  244. * NB. The fields are natural pointer/address size for this architecture.
  245. */
  246. struct mmu_update {
  247. uint64_t ptr; /* Machine address of PTE. */
  248. uint64_t val; /* New contents of PTE. */
  249. };
  250. DEFINE_GUEST_HANDLE_STRUCT(mmu_update);
  251. /*
  252. * Send an array of these to HYPERVISOR_multicall().
  253. * NB. The fields are natural register size for this architecture.
  254. */
  255. struct multicall_entry {
  256. unsigned long op;
  257. long result;
  258. unsigned long args[6];
  259. };
  260. DEFINE_GUEST_HANDLE_STRUCT(multicall_entry);
  261. /*
  262. * Event channel endpoints per domain:
  263. * 1024 if a long is 32 bits; 4096 if a long is 64 bits.
  264. */
  265. #define NR_EVENT_CHANNELS (sizeof(unsigned long) * sizeof(unsigned long) * 64)
  266. struct vcpu_time_info {
  267. /*
  268. * Updates to the following values are preceded and followed
  269. * by an increment of 'version'. The guest can therefore
  270. * detect updates by looking for changes to 'version'. If the
  271. * least-significant bit of the version number is set then an
  272. * update is in progress and the guest must wait to read a
  273. * consistent set of values. The correct way to interact with
  274. * the version number is similar to Linux's seqlock: see the
  275. * implementations of read_seqbegin/read_seqretry.
  276. */
  277. uint32_t version;
  278. uint32_t pad0;
  279. uint64_t tsc_timestamp; /* TSC at last update of time vals. */
  280. uint64_t system_time; /* Time, in nanosecs, since boot. */
  281. /*
  282. * Current system time:
  283. * system_time + ((tsc - tsc_timestamp) << tsc_shift) * tsc_to_system_mul
  284. * CPU frequency (Hz):
  285. * ((10^9 << 32) / tsc_to_system_mul) >> tsc_shift
  286. */
  287. uint32_t tsc_to_system_mul;
  288. int8_t tsc_shift;
  289. int8_t pad1[3];
  290. }; /* 32 bytes */
  291. struct vcpu_info {
  292. /*
  293. * 'evtchn_upcall_pending' is written non-zero by Xen to indicate
  294. * a pending notification for a particular VCPU. It is then cleared
  295. * by the guest OS /before/ checking for pending work, thus avoiding
  296. * a set-and-check race. Note that the mask is only accessed by Xen
  297. * on the CPU that is currently hosting the VCPU. This means that the
  298. * pending and mask flags can be updated by the guest without special
  299. * synchronisation (i.e., no need for the x86 LOCK prefix).
  300. * This may seem suboptimal because if the pending flag is set by
  301. * a different CPU then an IPI may be scheduled even when the mask
  302. * is set. However, note:
  303. * 1. The task of 'interrupt holdoff' is covered by the per-event-
  304. * channel mask bits. A 'noisy' event that is continually being
  305. * triggered can be masked at source at this very precise
  306. * granularity.
  307. * 2. The main purpose of the per-VCPU mask is therefore to restrict
  308. * reentrant execution: whether for concurrency control, or to
  309. * prevent unbounded stack usage. Whatever the purpose, we expect
  310. * that the mask will be asserted only for short periods at a time,
  311. * and so the likelihood of a 'spurious' IPI is suitably small.
  312. * The mask is read before making an event upcall to the guest: a
  313. * non-zero mask therefore guarantees that the VCPU will not receive
  314. * an upcall activation. The mask is cleared when the VCPU requests
  315. * to block: this avoids wakeup-waiting races.
  316. */
  317. uint8_t evtchn_upcall_pending;
  318. uint8_t evtchn_upcall_mask;
  319. unsigned long evtchn_pending_sel;
  320. struct arch_vcpu_info arch;
  321. struct pvclock_vcpu_time_info time;
  322. }; /* 64 bytes (x86) */
  323. /*
  324. * Xen/kernel shared data -- pointer provided in start_info.
  325. * NB. We expect that this struct is smaller than a page.
  326. */
  327. struct shared_info {
  328. struct vcpu_info vcpu_info[MAX_VIRT_CPUS];
  329. /*
  330. * A domain can create "event channels" on which it can send and receive
  331. * asynchronous event notifications. There are three classes of event that
  332. * are delivered by this mechanism:
  333. * 1. Bi-directional inter- and intra-domain connections. Domains must
  334. * arrange out-of-band to set up a connection (usually by allocating
  335. * an unbound 'listener' port and avertising that via a storage service
  336. * such as xenstore).
  337. * 2. Physical interrupts. A domain with suitable hardware-access
  338. * privileges can bind an event-channel port to a physical interrupt
  339. * source.
  340. * 3. Virtual interrupts ('events'). A domain can bind an event-channel
  341. * port to a virtual interrupt source, such as the virtual-timer
  342. * device or the emergency console.
  343. *
  344. * Event channels are addressed by a "port index". Each channel is
  345. * associated with two bits of information:
  346. * 1. PENDING -- notifies the domain that there is a pending notification
  347. * to be processed. This bit is cleared by the guest.
  348. * 2. MASK -- if this bit is clear then a 0->1 transition of PENDING
  349. * will cause an asynchronous upcall to be scheduled. This bit is only
  350. * updated by the guest. It is read-only within Xen. If a channel
  351. * becomes pending while the channel is masked then the 'edge' is lost
  352. * (i.e., when the channel is unmasked, the guest must manually handle
  353. * pending notifications as no upcall will be scheduled by Xen).
  354. *
  355. * To expedite scanning of pending notifications, any 0->1 pending
  356. * transition on an unmasked channel causes a corresponding bit in a
  357. * per-vcpu selector word to be set. Each bit in the selector covers a
  358. * 'C long' in the PENDING bitfield array.
  359. */
  360. unsigned long evtchn_pending[sizeof(unsigned long) * 8];
  361. unsigned long evtchn_mask[sizeof(unsigned long) * 8];
  362. /*
  363. * Wallclock time: updated only by control software. Guests should base
  364. * their gettimeofday() syscall on this wallclock-base value.
  365. */
  366. struct pvclock_wall_clock wc;
  367. struct arch_shared_info arch;
  368. };
  369. /*
  370. * Start-of-day memory layout for the initial domain (DOM0):
  371. * 1. The domain is started within contiguous virtual-memory region.
  372. * 2. The contiguous region begins and ends on an aligned 4MB boundary.
  373. * 3. The region start corresponds to the load address of the OS image.
  374. * If the load address is not 4MB aligned then the address is rounded down.
  375. * 4. This the order of bootstrap elements in the initial virtual region:
  376. * a. relocated kernel image
  377. * b. initial ram disk [mod_start, mod_len]
  378. * c. list of allocated page frames [mfn_list, nr_pages]
  379. * d. start_info_t structure [register ESI (x86)]
  380. * e. bootstrap page tables [pt_base, CR3 (x86)]
  381. * f. bootstrap stack [register ESP (x86)]
  382. * 5. Bootstrap elements are packed together, but each is 4kB-aligned.
  383. * 6. The initial ram disk may be omitted.
  384. * 7. The list of page frames forms a contiguous 'pseudo-physical' memory
  385. * layout for the domain. In particular, the bootstrap virtual-memory
  386. * region is a 1:1 mapping to the first section of the pseudo-physical map.
  387. * 8. All bootstrap elements are mapped read-writable for the guest OS. The
  388. * only exception is the bootstrap page table, which is mapped read-only.
  389. * 9. There is guaranteed to be at least 512kB padding after the final
  390. * bootstrap element. If necessary, the bootstrap virtual region is
  391. * extended by an extra 4MB to ensure this.
  392. */
  393. #define MAX_GUEST_CMDLINE 1024
  394. struct start_info {
  395. /* THE FOLLOWING ARE FILLED IN BOTH ON INITIAL BOOT AND ON RESUME. */
  396. char magic[32]; /* "xen-<version>-<platform>". */
  397. unsigned long nr_pages; /* Total pages allocated to this domain. */
  398. unsigned long shared_info; /* MACHINE address of shared info struct. */
  399. uint32_t flags; /* SIF_xxx flags. */
  400. unsigned long store_mfn; /* MACHINE page number of shared page. */
  401. uint32_t store_evtchn; /* Event channel for store communication. */
  402. union {
  403. struct {
  404. unsigned long mfn; /* MACHINE page number of console page. */
  405. uint32_t evtchn; /* Event channel for console page. */
  406. } domU;
  407. struct {
  408. uint32_t info_off; /* Offset of console_info struct. */
  409. uint32_t info_size; /* Size of console_info struct from start.*/
  410. } dom0;
  411. } console;
  412. /* THE FOLLOWING ARE ONLY FILLED IN ON INITIAL BOOT (NOT RESUME). */
  413. unsigned long pt_base; /* VIRTUAL address of page directory. */
  414. unsigned long nr_pt_frames; /* Number of bootstrap p.t. frames. */
  415. unsigned long mfn_list; /* VIRTUAL address of page-frame list. */
  416. unsigned long mod_start; /* VIRTUAL address of pre-loaded module. */
  417. unsigned long mod_len; /* Size (bytes) of pre-loaded module. */
  418. int8_t cmd_line[MAX_GUEST_CMDLINE];
  419. };
  420. /* These flags are passed in the 'flags' field of start_info_t. */
  421. #define SIF_PRIVILEGED (1<<0) /* Is the domain privileged? */
  422. #define SIF_INITDOMAIN (1<<1) /* Is this the initial control domain? */
  423. typedef uint64_t cpumap_t;
  424. typedef uint8_t xen_domain_handle_t[16];
  425. /* Turn a plain number into a C unsigned long constant. */
  426. #define __mk_unsigned_long(x) x ## UL
  427. #define mk_unsigned_long(x) __mk_unsigned_long(x)
  428. #else /* __ASSEMBLY__ */
  429. /* In assembly code we cannot use C numeric constant suffixes. */
  430. #define mk_unsigned_long(x) x
  431. #endif /* !__ASSEMBLY__ */
  432. #endif /* __XEN_PUBLIC_XEN_H__ */