lg.h 7.0 KB

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