lg.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef _LGUEST_H
  2. #define _LGUEST_H
  3. #include <asm/desc.h>
  4. #define GDT_ENTRY_LGUEST_CS 10
  5. #define GDT_ENTRY_LGUEST_DS 11
  6. #define LGUEST_CS (GDT_ENTRY_LGUEST_CS * 8)
  7. #define LGUEST_DS (GDT_ENTRY_LGUEST_DS * 8)
  8. #ifndef __ASSEMBLY__
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/stringify.h>
  12. #include <linux/futex.h>
  13. #include <linux/lguest.h>
  14. #include <linux/lguest_launcher.h>
  15. #include <linux/wait.h>
  16. #include <linux/err.h>
  17. #include <asm/semaphore.h>
  18. #include "irq_vectors.h"
  19. #define GUEST_PL 1
  20. struct lguest_regs
  21. {
  22. /* Manually saved part. */
  23. unsigned long ebx, ecx, edx;
  24. unsigned long esi, edi, ebp;
  25. unsigned long gs;
  26. unsigned long eax;
  27. unsigned long fs, ds, es;
  28. unsigned long trapnum, errcode;
  29. /* Trap pushed part */
  30. unsigned long eip;
  31. unsigned long cs;
  32. unsigned long eflags;
  33. unsigned long esp;
  34. unsigned long ss;
  35. };
  36. void free_pagetables(void);
  37. int init_pagetables(struct page **switcher_page, unsigned int pages);
  38. /* Full 4G segment descriptors, suitable for CS and DS. */
  39. #define FULL_EXEC_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9b00})
  40. #define FULL_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9300})
  41. struct lguest_dma_info
  42. {
  43. struct list_head list;
  44. union futex_key key;
  45. unsigned long dmas;
  46. u16 next_dma;
  47. u16 num_dmas;
  48. u16 guestid;
  49. u8 interrupt; /* 0 when not registered */
  50. };
  51. /*H:310 The page-table code owes a great debt of gratitude to Andi Kleen. He
  52. * reviewed the original code which used "u32" for all page table entries, and
  53. * insisted that it would be far clearer with explicit typing. I thought it
  54. * was overkill, but he was right: it is much clearer than it was before.
  55. *
  56. * We have separate types for the Guest's ptes & pgds and the shadow ptes &
  57. * pgds. There's already a Linux type for these (pte_t and pgd_t) but they
  58. * change depending on kernel config options (PAE). */
  59. /* Each entry is identical: lower 12 bits of flags and upper 20 bits for the
  60. * "page frame number" (0 == first physical page, etc). They are different
  61. * types so the compiler will warn us if we mix them improperly. */
  62. typedef union {
  63. struct { unsigned flags:12, pfn:20; };
  64. struct { unsigned long val; } raw;
  65. } spgd_t;
  66. typedef union {
  67. struct { unsigned flags:12, pfn:20; };
  68. struct { unsigned long val; } raw;
  69. } spte_t;
  70. typedef union {
  71. struct { unsigned flags:12, pfn:20; };
  72. struct { unsigned long val; } raw;
  73. } gpgd_t;
  74. typedef union {
  75. struct { unsigned flags:12, pfn:20; };
  76. struct { unsigned long val; } raw;
  77. } gpte_t;
  78. /* We have two convenient macros to convert a "raw" value as handed to us by
  79. * the Guest into the correct Guest PGD or PTE type. */
  80. #define mkgpte(_val) ((gpte_t){.raw.val = _val})
  81. #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
  82. /*:*/
  83. struct pgdir
  84. {
  85. unsigned long cr3;
  86. spgd_t *pgdir;
  87. };
  88. /* This is a guest-specific page (mapped ro) into the guest. */
  89. struct lguest_ro_state
  90. {
  91. /* Host information we need to restore when we switch back. */
  92. u32 host_cr3;
  93. struct Xgt_desc_struct host_idt_desc;
  94. struct Xgt_desc_struct host_gdt_desc;
  95. u32 host_sp;
  96. /* Fields which are used when guest is running. */
  97. struct Xgt_desc_struct guest_idt_desc;
  98. struct Xgt_desc_struct guest_gdt_desc;
  99. struct i386_hw_tss guest_tss;
  100. struct desc_struct guest_idt[IDT_ENTRIES];
  101. struct desc_struct guest_gdt[GDT_ENTRIES];
  102. };
  103. /* We have two pages shared with guests, per cpu. */
  104. struct lguest_pages
  105. {
  106. /* This is the stack page mapped rw in guest */
  107. char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
  108. struct lguest_regs regs;
  109. /* This is the host state & guest descriptor page, ro in guest */
  110. struct lguest_ro_state state;
  111. } __attribute__((aligned(PAGE_SIZE)));
  112. #define CHANGED_IDT 1
  113. #define CHANGED_GDT 2
  114. #define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
  115. #define CHANGED_ALL 3
  116. /* The private info the thread maintains about the guest. */
  117. struct lguest
  118. {
  119. /* At end of a page shared mapped over lguest_pages in guest. */
  120. unsigned long regs_page;
  121. struct lguest_regs *regs;
  122. struct lguest_data __user *lguest_data;
  123. struct task_struct *tsk;
  124. struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
  125. u16 guestid;
  126. u32 pfn_limit;
  127. /* This provides the offset to the base of guest-physical
  128. * memory in the Launcher. */
  129. void __user *mem_base;
  130. u32 page_offset;
  131. u32 cr2;
  132. int halted;
  133. int ts;
  134. u32 next_hcall;
  135. u32 esp1;
  136. u8 ss1;
  137. /* Do we need to stop what we're doing and return to userspace? */
  138. int break_out;
  139. wait_queue_head_t break_wq;
  140. /* Bitmap of what has changed: see CHANGED_* above. */
  141. int changed;
  142. struct lguest_pages *last_pages;
  143. /* We keep a small number of these. */
  144. u32 pgdidx;
  145. struct pgdir pgdirs[4];
  146. /* Cached wakeup: we hold a reference to this task. */
  147. struct task_struct *wake;
  148. unsigned long noirq_start, noirq_end;
  149. int dma_is_pending;
  150. unsigned long pending_dma; /* struct lguest_dma */
  151. unsigned long pending_key; /* address they're sending to */
  152. unsigned int stack_pages;
  153. u32 tsc_khz;
  154. struct lguest_dma_info dma[LGUEST_MAX_DMA];
  155. /* Dead? */
  156. const char *dead;
  157. /* The GDT entries copied into lguest_ro_state when running. */
  158. struct desc_struct gdt[GDT_ENTRIES];
  159. /* The IDT entries: some copied into lguest_ro_state when running. */
  160. struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS];
  161. struct desc_struct syscall_idt;
  162. /* Virtual clock device */
  163. struct hrtimer hrt;
  164. /* Pending virtual interrupts */
  165. DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
  166. };
  167. extern struct lguest lguests[];
  168. extern struct mutex lguest_lock;
  169. /* core.c: */
  170. u32 lgread_u32(struct lguest *lg, unsigned long addr);
  171. void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
  172. void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
  173. void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
  174. int find_free_guest(void);
  175. int lguest_address_ok(const struct lguest *lg,
  176. unsigned long addr, unsigned long len);
  177. int run_guest(struct lguest *lg, unsigned long __user *user);
  178. /* interrupts_and_traps.c: */
  179. void maybe_do_interrupt(struct lguest *lg);
  180. int deliver_trap(struct lguest *lg, unsigned int num);
  181. void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
  182. void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
  183. void pin_stack_pages(struct lguest *lg);
  184. void setup_default_idt_entries(struct lguest_ro_state *state,
  185. const unsigned long *def);
  186. void copy_traps(const struct lguest *lg, struct desc_struct *idt,
  187. const unsigned long *def);
  188. void guest_set_clockevent(struct lguest *lg, unsigned long delta);
  189. void init_clockdev(struct lguest *lg);
  190. /* segments.c: */
  191. void setup_default_gdt_entries(struct lguest_ro_state *state);
  192. void setup_guest_gdt(struct lguest *lg);
  193. void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
  194. void guest_load_tls(struct lguest *lg, unsigned long tls_array);
  195. void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
  196. void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
  197. /* page_tables.c: */
  198. int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
  199. void free_guest_pagetable(struct lguest *lg);
  200. void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
  201. void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
  202. void guest_pagetable_clear_all(struct lguest *lg);
  203. void guest_pagetable_flush_user(struct lguest *lg);
  204. void guest_set_pte(struct lguest *lg, unsigned long cr3,
  205. unsigned long vaddr, gpte_t val);
  206. void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
  207. int demand_page(struct lguest *info, unsigned long cr2, int errcode);
  208. void pin_page(struct lguest *lg, unsigned long vaddr);
  209. /* lguest_user.c: */
  210. int lguest_device_init(void);
  211. void lguest_device_remove(void);
  212. /* io.c: */
  213. void lguest_io_init(void);
  214. int bind_dma(struct lguest *lg,
  215. unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
  216. void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
  217. void release_all_dma(struct lguest *lg);
  218. unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
  219. unsigned long *interrupt);
  220. /* hypercalls.c: */
  221. void do_hypercalls(struct lguest *lg);
  222. void write_timestamp(struct lguest *lg);
  223. /*L:035
  224. * Let's step aside for the moment, to study one important routine that's used
  225. * widely in the Host code.
  226. *
  227. * There are many cases where the Guest does something invalid, like pass crap
  228. * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
  229. * acceptable to simply terminate the Guest and give the Launcher a nicely
  230. * formatted reason. It's also simpler for the Guest itself, which doesn't
  231. * need to check most hypercalls for "success"; if you're still running, it
  232. * succeeded.
  233. *
  234. * Once this is called, the Guest will never run again, so most Host code can
  235. * call this then continue as if nothing had happened. This means many
  236. * functions don't have to explicitly return an error code, which keeps the
  237. * code simple.
  238. *
  239. * It also means that this can be called more than once: only the first one is
  240. * remembered. The only trick is that we still need to kill the Guest even if
  241. * we can't allocate memory to store the reason. Linux has a neat way of
  242. * packing error codes into invalid pointers, so we use that here.
  243. *
  244. * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
  245. * } while(0)".
  246. */
  247. #define kill_guest(lg, fmt...) \
  248. do { \
  249. if (!(lg)->dead) { \
  250. (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
  251. if (!(lg)->dead) \
  252. (lg)->dead = ERR_PTR(-ENOMEM); \
  253. } \
  254. } while(0)
  255. /* (End of aside) :*/
  256. static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
  257. {
  258. return vaddr - lg->page_offset;
  259. }
  260. #endif /* __ASSEMBLY__ */
  261. #endif /* _LGUEST_H */