lg.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #ifndef _LGUEST_H
  2. #define _LGUEST_H
  3. #include <asm/desc.h>
  4. #define GDT_ENTRY_LGUEST_CS 10
  5. #define GDT_ENTRY_LGUEST_DS 11
  6. #define LGUEST_CS (GDT_ENTRY_LGUEST_CS * 8)
  7. #define LGUEST_DS (GDT_ENTRY_LGUEST_DS * 8)
  8. #ifndef __ASSEMBLY__
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/stringify.h>
  12. #include <linux/futex.h>
  13. #include <linux/lguest.h>
  14. #include <linux/lguest_launcher.h>
  15. #include <linux/wait.h>
  16. #include <linux/err.h>
  17. #include <asm/semaphore.h>
  18. #include "irq_vectors.h"
  19. #define GUEST_PL 1
  20. struct lguest_regs
  21. {
  22. /* Manually saved part. */
  23. unsigned long ebx, ecx, edx;
  24. unsigned long esi, edi, ebp;
  25. unsigned long gs;
  26. unsigned long eax;
  27. unsigned long fs, ds, es;
  28. unsigned long trapnum, errcode;
  29. /* Trap pushed part */
  30. unsigned long eip;
  31. unsigned long cs;
  32. unsigned long eflags;
  33. unsigned long esp;
  34. unsigned long ss;
  35. };
  36. void free_pagetables(void);
  37. int init_pagetables(struct page **switcher_page, unsigned int pages);
  38. /* Full 4G segment descriptors, suitable for CS and DS. */
  39. #define FULL_EXEC_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9b00})
  40. #define FULL_SEGMENT ((struct desc_struct){0x0000ffff, 0x00cf9300})
  41. struct lguest_dma_info
  42. {
  43. struct list_head list;
  44. union futex_key key;
  45. unsigned long dmas;
  46. struct lguest *owner;
  47. u16 next_dma;
  48. u16 num_dmas;
  49. u8 interrupt; /* 0 when not registered */
  50. };
  51. /*H:310 The page-table code owes a great debt of gratitude to Andi Kleen. He
  52. * reviewed the original code which used "u32" for all page table entries, and
  53. * insisted that it would be far clearer with explicit typing. I thought it
  54. * was overkill, but he was right: it is much clearer than it was before.
  55. *
  56. * We have separate types for the Guest's ptes & pgds and the shadow ptes &
  57. * pgds. There's already a Linux type for these (pte_t and pgd_t) but they
  58. * change depending on kernel config options (PAE). */
  59. /* Each entry is identical: lower 12 bits of flags and upper 20 bits for the
  60. * "page frame number" (0 == first physical page, etc). They are different
  61. * types so the compiler will warn us if we mix them improperly. */
  62. typedef union {
  63. struct { unsigned flags:12, pfn:20; };
  64. struct { unsigned long val; } raw;
  65. } spgd_t;
  66. typedef union {
  67. struct { unsigned flags:12, pfn:20; };
  68. struct { unsigned long val; } raw;
  69. } spte_t;
  70. typedef union {
  71. struct { unsigned flags:12, pfn:20; };
  72. struct { unsigned long val; } raw;
  73. } gpgd_t;
  74. typedef union {
  75. struct { unsigned flags:12, pfn:20; };
  76. struct { unsigned long val; } raw;
  77. } gpte_t;
  78. /* We have two convenient macros to convert a "raw" value as handed to us by
  79. * the Guest into the correct Guest PGD or PTE type. */
  80. #define mkgpte(_val) ((gpte_t){.raw.val = _val})
  81. #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
  82. /*:*/
  83. struct pgdir
  84. {
  85. unsigned long cr3;
  86. spgd_t *pgdir;
  87. };
  88. /* This is a guest-specific page (mapped ro) into the guest. */
  89. struct lguest_ro_state
  90. {
  91. /* Host information we need to restore when we switch back. */
  92. u32 host_cr3;
  93. struct Xgt_desc_struct host_idt_desc;
  94. struct Xgt_desc_struct host_gdt_desc;
  95. u32 host_sp;
  96. /* Fields which are used when guest is running. */
  97. struct Xgt_desc_struct guest_idt_desc;
  98. struct Xgt_desc_struct guest_gdt_desc;
  99. struct i386_hw_tss guest_tss;
  100. struct desc_struct guest_idt[IDT_ENTRIES];
  101. struct desc_struct guest_gdt[GDT_ENTRIES];
  102. };
  103. /* We have two pages shared with guests, per cpu. */
  104. struct lguest_pages
  105. {
  106. /* This is the stack page mapped rw in guest */
  107. char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
  108. struct lguest_regs regs;
  109. /* This is the host state & guest descriptor page, ro in guest */
  110. struct lguest_ro_state state;
  111. } __attribute__((aligned(PAGE_SIZE)));
  112. #define CHANGED_IDT 1
  113. #define CHANGED_GDT 2
  114. #define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
  115. #define CHANGED_ALL 3
  116. /* The private info the thread maintains about the guest. */
  117. struct lguest
  118. {
  119. /* At end of a page shared mapped over lguest_pages in guest. */
  120. unsigned long regs_page;
  121. struct lguest_regs *regs;
  122. struct lguest_data __user *lguest_data;
  123. struct task_struct *tsk;
  124. struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
  125. u32 pfn_limit;
  126. /* This provides the offset to the base of guest-physical
  127. * memory in the Launcher. */
  128. void __user *mem_base;
  129. u32 page_offset;
  130. u32 cr2;
  131. int halted;
  132. int ts;
  133. u32 next_hcall;
  134. u32 esp1;
  135. u8 ss1;
  136. /* Do we need to stop what we're doing and return to userspace? */
  137. int break_out;
  138. wait_queue_head_t break_wq;
  139. /* Bitmap of what has changed: see CHANGED_* above. */
  140. int changed;
  141. struct lguest_pages *last_pages;
  142. /* We keep a small number of these. */
  143. u32 pgdidx;
  144. struct pgdir pgdirs[4];
  145. /* Cached wakeup: we hold a reference to this task. */
  146. struct task_struct *wake;
  147. unsigned long noirq_start, noirq_end;
  148. int dma_is_pending;
  149. unsigned long pending_dma; /* struct lguest_dma */
  150. unsigned long pending_key; /* address they're sending to */
  151. unsigned int stack_pages;
  152. u32 tsc_khz;
  153. struct lguest_dma_info dma[LGUEST_MAX_DMA];
  154. /* Dead? */
  155. const char *dead;
  156. /* The GDT entries copied into lguest_ro_state when running. */
  157. struct desc_struct gdt[GDT_ENTRIES];
  158. /* The IDT entries: some copied into lguest_ro_state when running. */
  159. struct desc_struct idt[IDT_ENTRIES];
  160. /* Virtual clock device */
  161. struct hrtimer hrt;
  162. /* Pending virtual interrupts */
  163. DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
  164. };
  165. extern struct mutex lguest_lock;
  166. /* core.c: */
  167. u32 lgread_u32(struct lguest *lg, unsigned long addr);
  168. void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
  169. void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
  170. void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
  171. int lguest_address_ok(const struct lguest *lg,
  172. unsigned long addr, unsigned long len);
  173. int run_guest(struct lguest *lg, unsigned long __user *user);
  174. /* interrupts_and_traps.c: */
  175. void maybe_do_interrupt(struct lguest *lg);
  176. int deliver_trap(struct lguest *lg, unsigned int num);
  177. void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
  178. void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
  179. void pin_stack_pages(struct lguest *lg);
  180. void setup_default_idt_entries(struct lguest_ro_state *state,
  181. const unsigned long *def);
  182. void copy_traps(const struct lguest *lg, struct desc_struct *idt,
  183. const unsigned long *def);
  184. void guest_set_clockevent(struct lguest *lg, unsigned long delta);
  185. void init_clockdev(struct lguest *lg);
  186. /* segments.c: */
  187. void setup_default_gdt_entries(struct lguest_ro_state *state);
  188. void setup_guest_gdt(struct lguest *lg);
  189. void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
  190. void guest_load_tls(struct lguest *lg, unsigned long tls_array);
  191. void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
  192. void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
  193. /* page_tables.c: */
  194. int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
  195. void free_guest_pagetable(struct lguest *lg);
  196. void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
  197. void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
  198. void guest_pagetable_clear_all(struct lguest *lg);
  199. void guest_pagetable_flush_user(struct lguest *lg);
  200. void guest_set_pte(struct lguest *lg, unsigned long cr3,
  201. unsigned long vaddr, gpte_t val);
  202. void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
  203. int demand_page(struct lguest *info, unsigned long cr2, int errcode);
  204. void pin_page(struct lguest *lg, unsigned long vaddr);
  205. /* lguest_user.c: */
  206. int lguest_device_init(void);
  207. void lguest_device_remove(void);
  208. /* io.c: */
  209. void lguest_io_init(void);
  210. int bind_dma(struct lguest *lg,
  211. unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
  212. void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
  213. void release_all_dma(struct lguest *lg);
  214. unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
  215. unsigned long *interrupt);
  216. /* hypercalls.c: */
  217. void do_hypercalls(struct lguest *lg);
  218. void write_timestamp(struct lguest *lg);
  219. /*L:035
  220. * Let's step aside for the moment, to study one important routine that's used
  221. * widely in the Host code.
  222. *
  223. * There are many cases where the Guest does something invalid, like pass crap
  224. * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
  225. * acceptable to simply terminate the Guest and give the Launcher a nicely
  226. * formatted reason. It's also simpler for the Guest itself, which doesn't
  227. * need to check most hypercalls for "success"; if you're still running, it
  228. * succeeded.
  229. *
  230. * Once this is called, the Guest will never run again, so most Host code can
  231. * call this then continue as if nothing had happened. This means many
  232. * functions don't have to explicitly return an error code, which keeps the
  233. * code simple.
  234. *
  235. * It also means that this can be called more than once: only the first one is
  236. * remembered. The only trick is that we still need to kill the Guest even if
  237. * we can't allocate memory to store the reason. Linux has a neat way of
  238. * packing error codes into invalid pointers, so we use that here.
  239. *
  240. * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
  241. * } while(0)".
  242. */
  243. #define kill_guest(lg, fmt...) \
  244. do { \
  245. if (!(lg)->dead) { \
  246. (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
  247. if (!(lg)->dead) \
  248. (lg)->dead = ERR_PTR(-ENOMEM); \
  249. } \
  250. } while(0)
  251. /* (End of aside) :*/
  252. static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
  253. {
  254. return vaddr - lg->page_offset;
  255. }
  256. #endif /* __ASSEMBLY__ */
  257. #endif /* _LGUEST_H */