lg.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. struct pgdir
  27. {
  28. unsigned long gpgdir;
  29. pgd_t *pgdir;
  30. };
  31. /* We have two pages shared with guests, per cpu. */
  32. struct lguest_pages
  33. {
  34. /* This is the stack page mapped rw in guest */
  35. char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
  36. struct lguest_regs regs;
  37. /* This is the host state & guest descriptor page, ro in guest */
  38. struct lguest_ro_state state;
  39. } __attribute__((aligned(PAGE_SIZE)));
  40. #define CHANGED_IDT 1
  41. #define CHANGED_GDT 2
  42. #define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
  43. #define CHANGED_ALL 3
  44. /* The private info the thread maintains about the guest. */
  45. struct lguest
  46. {
  47. /* At end of a page shared mapped over lguest_pages in guest. */
  48. unsigned long regs_page;
  49. struct lguest_regs *regs;
  50. struct lguest_data __user *lguest_data;
  51. struct task_struct *tsk;
  52. struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
  53. u32 pfn_limit;
  54. /* This provides the offset to the base of guest-physical
  55. * memory in the Launcher. */
  56. void __user *mem_base;
  57. u32 page_offset;
  58. u32 cr2;
  59. int halted;
  60. int ts;
  61. u32 next_hcall;
  62. u32 esp1;
  63. u8 ss1;
  64. /* If a hypercall was asked for, this points to the arguments. */
  65. struct hcall_args *hcall;
  66. /* Do we need to stop what we're doing and return to userspace? */
  67. int break_out;
  68. wait_queue_head_t break_wq;
  69. /* Bitmap of what has changed: see CHANGED_* above. */
  70. int changed;
  71. struct lguest_pages *last_pages;
  72. /* We keep a small number of these. */
  73. u32 pgdidx;
  74. struct pgdir pgdirs[4];
  75. /* Cached wakeup: we hold a reference to this task. */
  76. struct task_struct *wake;
  77. unsigned long noirq_start, noirq_end;
  78. int dma_is_pending;
  79. unsigned long pending_dma; /* struct lguest_dma */
  80. unsigned long pending_key; /* address they're sending to */
  81. unsigned int stack_pages;
  82. u32 tsc_khz;
  83. struct lguest_dma_info dma[LGUEST_MAX_DMA];
  84. /* Dead? */
  85. const char *dead;
  86. struct lguest_arch arch;
  87. /* Virtual clock device */
  88. struct hrtimer hrt;
  89. /* Pending virtual interrupts */
  90. DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
  91. };
  92. extern struct mutex lguest_lock;
  93. /* core.c: */
  94. u32 lgread_u32(struct lguest *lg, unsigned long addr);
  95. void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
  96. void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
  97. void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
  98. int lguest_address_ok(const struct lguest *lg,
  99. unsigned long addr, unsigned long len);
  100. int run_guest(struct lguest *lg, unsigned long __user *user);
  101. /* Helper macros to obtain the first 12 or the last 20 bits, this is only the
  102. * first step in the migration to the kernel types. pte_pfn is already defined
  103. * in the kernel. */
  104. #define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK)
  105. #define pte_flags(x) (pte_val(x) & ~PAGE_MASK)
  106. #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT)
  107. /* interrupts_and_traps.c: */
  108. void maybe_do_interrupt(struct lguest *lg);
  109. int deliver_trap(struct lguest *lg, unsigned int num);
  110. void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
  111. void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
  112. void pin_stack_pages(struct lguest *lg);
  113. void setup_default_idt_entries(struct lguest_ro_state *state,
  114. const unsigned long *def);
  115. void copy_traps(const struct lguest *lg, struct desc_struct *idt,
  116. const unsigned long *def);
  117. void guest_set_clockevent(struct lguest *lg, unsigned long delta);
  118. void init_clockdev(struct lguest *lg);
  119. /* segments.c: */
  120. void setup_default_gdt_entries(struct lguest_ro_state *state);
  121. void setup_guest_gdt(struct lguest *lg);
  122. void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
  123. void guest_load_tls(struct lguest *lg, unsigned long tls_array);
  124. void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
  125. void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
  126. /* page_tables.c: */
  127. int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
  128. void free_guest_pagetable(struct lguest *lg);
  129. void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
  130. void guest_set_pmd(struct lguest *lg, unsigned long gpgdir, u32 i);
  131. void guest_pagetable_clear_all(struct lguest *lg);
  132. void guest_pagetable_flush_user(struct lguest *lg);
  133. void guest_set_pte(struct lguest *lg, unsigned long gpgdir,
  134. unsigned long vaddr, pte_t val);
  135. void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
  136. int demand_page(struct lguest *info, unsigned long cr2, int errcode);
  137. void pin_page(struct lguest *lg, unsigned long vaddr);
  138. /* <arch>/core.c: */
  139. void lguest_arch_host_init(void);
  140. void lguest_arch_host_fini(void);
  141. void lguest_arch_run_guest(struct lguest *lg);
  142. void lguest_arch_handle_trap(struct lguest *lg);
  143. int lguest_arch_init_hypercalls(struct lguest *lg);
  144. int lguest_arch_do_hcall(struct lguest *lg, struct hcall_args *args);
  145. void lguest_arch_setup_regs(struct lguest *lg, unsigned long start);
  146. /* <arch>/switcher.S: */
  147. extern char start_switcher_text[], end_switcher_text[], switch_to_guest[];
  148. /* lguest_user.c: */
  149. int lguest_device_init(void);
  150. void lguest_device_remove(void);
  151. /* io.c: */
  152. void lguest_io_init(void);
  153. int bind_dma(struct lguest *lg,
  154. unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
  155. void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
  156. void release_all_dma(struct lguest *lg);
  157. unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
  158. unsigned long *interrupt);
  159. /* hypercalls.c: */
  160. void do_hypercalls(struct lguest *lg);
  161. void write_timestamp(struct lguest *lg);
  162. /*L:035
  163. * Let's step aside for the moment, to study one important routine that's used
  164. * widely in the Host code.
  165. *
  166. * There are many cases where the Guest does something invalid, like pass crap
  167. * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
  168. * acceptable to simply terminate the Guest and give the Launcher a nicely
  169. * formatted reason. It's also simpler for the Guest itself, which doesn't
  170. * need to check most hypercalls for "success"; if you're still running, it
  171. * succeeded.
  172. *
  173. * Once this is called, the Guest will never run again, so most Host code can
  174. * call this then continue as if nothing had happened. This means many
  175. * functions don't have to explicitly return an error code, which keeps the
  176. * code simple.
  177. *
  178. * It also means that this can be called more than once: only the first one is
  179. * remembered. The only trick is that we still need to kill the Guest even if
  180. * we can't allocate memory to store the reason. Linux has a neat way of
  181. * packing error codes into invalid pointers, so we use that here.
  182. *
  183. * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
  184. * } while(0)".
  185. */
  186. #define kill_guest(lg, fmt...) \
  187. do { \
  188. if (!(lg)->dead) { \
  189. (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
  190. if (!(lg)->dead) \
  191. (lg)->dead = ERR_PTR(-ENOMEM); \
  192. } \
  193. } while(0)
  194. /* (End of aside) :*/
  195. static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
  196. {
  197. return vaddr - lg->page_offset;
  198. }
  199. #endif /* __ASSEMBLY__ */
  200. #endif /* _LGUEST_H */