lg.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/hrtimer.h>
  11. #include <linux/err.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. struct lguest;
  34. struct lg_cpu {
  35. unsigned int id;
  36. struct lguest *lg;
  37. struct task_struct *tsk;
  38. struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
  39. u32 cr2;
  40. int ts;
  41. u32 esp1;
  42. u16 ss1;
  43. /* Bitmap of what has changed: see CHANGED_* above. */
  44. int changed;
  45. unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */
  46. /* At end of a page shared mapped over lguest_pages in guest. */
  47. unsigned long regs_page;
  48. struct lguest_regs *regs;
  49. struct lguest_pages *last_pages;
  50. int cpu_pgd; /* which pgd this cpu is currently using */
  51. /* If a hypercall was asked for, this points to the arguments. */
  52. struct hcall_args *hcall;
  53. u32 next_hcall;
  54. /* Virtual clock device */
  55. struct hrtimer hrt;
  56. /* Do we need to stop what we're doing and return to userspace? */
  57. int break_out;
  58. wait_queue_head_t break_wq;
  59. int halted;
  60. /* Pending virtual interrupts */
  61. DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
  62. struct lg_cpu_arch arch;
  63. };
  64. /* The private info the thread maintains about the guest. */
  65. struct lguest
  66. {
  67. struct lguest_data __user *lguest_data;
  68. struct lg_cpu cpus[NR_CPUS];
  69. unsigned int nr_cpus;
  70. u32 pfn_limit;
  71. /* This provides the offset to the base of guest-physical
  72. * memory in the Launcher. */
  73. void __user *mem_base;
  74. unsigned long kernel_address;
  75. struct pgdir pgdirs[4];
  76. unsigned long noirq_start, noirq_end;
  77. unsigned int stack_pages;
  78. u32 tsc_khz;
  79. /* Dead? */
  80. const char *dead;
  81. };
  82. extern struct mutex lguest_lock;
  83. /* core.c: */
  84. bool lguest_address_ok(const struct lguest *lg,
  85. unsigned long addr, unsigned long len);
  86. void __lgread(struct lg_cpu *, void *, unsigned long, unsigned);
  87. void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned);
  88. /*H:035 Using memory-copy operations like that is usually inconvient, so we
  89. * have the following helper macros which read and write a specific type (often
  90. * an unsigned long).
  91. *
  92. * This reads into a variable of the given type then returns that. */
  93. #define lgread(cpu, addr, type) \
  94. ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; })
  95. /* This checks that the variable is of the given type, then writes it out. */
  96. #define lgwrite(cpu, addr, type, val) \
  97. do { \
  98. typecheck(type, val); \
  99. __lgwrite((cpu), (addr), &(val), sizeof(val)); \
  100. } while(0)
  101. /* (end of memory access helper routines) :*/
  102. int run_guest(struct lg_cpu *cpu, unsigned long __user *user);
  103. /* Helper macros to obtain the first 12 or the last 20 bits, this is only the
  104. * first step in the migration to the kernel types. pte_pfn is already defined
  105. * in the kernel. */
  106. #define pgd_flags(x) (pgd_val(x) & ~PAGE_MASK)
  107. #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT)
  108. #define pmd_flags(x) (pmd_val(x) & ~PAGE_MASK)
  109. #define pmd_pfn(x) (pmd_val(x) >> PAGE_SHIFT)
  110. /* interrupts_and_traps.c: */
  111. unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more);
  112. void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more);
  113. void set_interrupt(struct lg_cpu *cpu, unsigned int irq);
  114. bool deliver_trap(struct lg_cpu *cpu, unsigned int num);
  115. void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int i,
  116. u32 low, u32 hi);
  117. void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages);
  118. void pin_stack_pages(struct lg_cpu *cpu);
  119. void setup_default_idt_entries(struct lguest_ro_state *state,
  120. const unsigned long *def);
  121. void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt,
  122. const unsigned long *def);
  123. void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta);
  124. void init_clockdev(struct lg_cpu *cpu);
  125. bool check_syscall_vector(struct lguest *lg);
  126. int init_interrupts(void);
  127. void free_interrupts(void);
  128. /* segments.c: */
  129. void setup_default_gdt_entries(struct lguest_ro_state *state);
  130. void setup_guest_gdt(struct lg_cpu *cpu);
  131. void load_guest_gdt_entry(struct lg_cpu *cpu, unsigned int i,
  132. u32 low, u32 hi);
  133. void guest_load_tls(struct lg_cpu *cpu, unsigned long tls_array);
  134. void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt);
  135. void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt);
  136. /* page_tables.c: */
  137. int init_guest_pagetable(struct lguest *lg);
  138. void free_guest_pagetable(struct lguest *lg);
  139. void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable);
  140. void guest_set_pgd(struct lguest *lg, unsigned long gpgdir, u32 i);
  141. #ifdef CONFIG_X86_PAE
  142. void guest_set_pmd(struct lguest *lg, unsigned long gpgdir, u32 i);
  143. #endif
  144. void guest_pagetable_clear_all(struct lg_cpu *cpu);
  145. void guest_pagetable_flush_user(struct lg_cpu *cpu);
  146. void guest_set_pte(struct lg_cpu *cpu, unsigned long gpgdir,
  147. unsigned long vaddr, pte_t val);
  148. void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages);
  149. bool demand_page(struct lg_cpu *cpu, unsigned long cr2, int errcode);
  150. void pin_page(struct lg_cpu *cpu, unsigned long vaddr);
  151. unsigned long guest_pa(struct lg_cpu *cpu, unsigned long vaddr);
  152. void page_table_guest_data_init(struct lg_cpu *cpu);
  153. /* <arch>/core.c: */
  154. void lguest_arch_host_init(void);
  155. void lguest_arch_host_fini(void);
  156. void lguest_arch_run_guest(struct lg_cpu *cpu);
  157. void lguest_arch_handle_trap(struct lg_cpu *cpu);
  158. int lguest_arch_init_hypercalls(struct lg_cpu *cpu);
  159. int lguest_arch_do_hcall(struct lg_cpu *cpu, struct hcall_args *args);
  160. void lguest_arch_setup_regs(struct lg_cpu *cpu, unsigned long start);
  161. /* <arch>/switcher.S: */
  162. extern char start_switcher_text[], end_switcher_text[], switch_to_guest[];
  163. /* lguest_user.c: */
  164. int lguest_device_init(void);
  165. void lguest_device_remove(void);
  166. /* hypercalls.c: */
  167. void do_hypercalls(struct lg_cpu *cpu);
  168. void write_timestamp(struct lg_cpu *cpu);
  169. /*L:035
  170. * Let's step aside for the moment, to study one important routine that's used
  171. * widely in the Host code.
  172. *
  173. * There are many cases where the Guest can do something invalid, like pass crap
  174. * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
  175. * acceptable to simply terminate the Guest and give the Launcher a nicely
  176. * formatted reason. It's also simpler for the Guest itself, which doesn't
  177. * need to check most hypercalls for "success"; if you're still running, it
  178. * succeeded.
  179. *
  180. * Once this is called, the Guest will never run again, so most Host code can
  181. * call this then continue as if nothing had happened. This means many
  182. * functions don't have to explicitly return an error code, which keeps the
  183. * code simple.
  184. *
  185. * It also means that this can be called more than once: only the first one is
  186. * remembered. The only trick is that we still need to kill the Guest even if
  187. * we can't allocate memory to store the reason. Linux has a neat way of
  188. * packing error codes into invalid pointers, so we use that here.
  189. *
  190. * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
  191. * } while(0)".
  192. */
  193. #define kill_guest(cpu, fmt...) \
  194. do { \
  195. if (!(cpu)->lg->dead) { \
  196. (cpu)->lg->dead = kasprintf(GFP_ATOMIC, fmt); \
  197. if (!(cpu)->lg->dead) \
  198. (cpu)->lg->dead = ERR_PTR(-ENOMEM); \
  199. } \
  200. } while(0)
  201. /* (End of aside) :*/
  202. #endif /* __ASSEMBLY__ */
  203. #endif /* _LGUEST_H */