lg.h 8.6 KB

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