lg.h 9.5 KB

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