lg.h 8.0 KB

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