lg.h 8.1 KB

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