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