lg.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* We have separate types for the guest's ptes & pgds and the shadow ptes &
  53. * pgds. Since this host might use three-level pagetables and the guest and
  54. * shadow pagetables don't, we can't use the normal pte_t/pgd_t. */
  55. typedef union {
  56. struct { unsigned flags:12, pfn:20; };
  57. struct { unsigned long val; } raw;
  58. } spgd_t;
  59. typedef union {
  60. struct { unsigned flags:12, pfn:20; };
  61. struct { unsigned long val; } raw;
  62. } spte_t;
  63. typedef union {
  64. struct { unsigned flags:12, pfn:20; };
  65. struct { unsigned long val; } raw;
  66. } gpgd_t;
  67. typedef union {
  68. struct { unsigned flags:12, pfn:20; };
  69. struct { unsigned long val; } raw;
  70. } gpte_t;
  71. #define mkgpte(_val) ((gpte_t){.raw.val = _val})
  72. #define mkgpgd(_val) ((gpgd_t){.raw.val = _val})
  73. struct pgdir
  74. {
  75. unsigned long cr3;
  76. spgd_t *pgdir;
  77. };
  78. /* This is a guest-specific page (mapped ro) into the guest. */
  79. struct lguest_ro_state
  80. {
  81. /* Host information we need to restore when we switch back. */
  82. u32 host_cr3;
  83. struct Xgt_desc_struct host_idt_desc;
  84. struct Xgt_desc_struct host_gdt_desc;
  85. u32 host_sp;
  86. /* Fields which are used when guest is running. */
  87. struct Xgt_desc_struct guest_idt_desc;
  88. struct Xgt_desc_struct guest_gdt_desc;
  89. struct i386_hw_tss guest_tss;
  90. struct desc_struct guest_idt[IDT_ENTRIES];
  91. struct desc_struct guest_gdt[GDT_ENTRIES];
  92. };
  93. /* We have two pages shared with guests, per cpu. */
  94. struct lguest_pages
  95. {
  96. /* This is the stack page mapped rw in guest */
  97. char spare[PAGE_SIZE - sizeof(struct lguest_regs)];
  98. struct lguest_regs regs;
  99. /* This is the host state & guest descriptor page, ro in guest */
  100. struct lguest_ro_state state;
  101. } __attribute__((aligned(PAGE_SIZE)));
  102. #define CHANGED_IDT 1
  103. #define CHANGED_GDT 2
  104. #define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
  105. #define CHANGED_ALL 3
  106. /* The private info the thread maintains about the guest. */
  107. struct lguest
  108. {
  109. /* At end of a page shared mapped over lguest_pages in guest. */
  110. unsigned long regs_page;
  111. struct lguest_regs *regs;
  112. struct lguest_data __user *lguest_data;
  113. struct task_struct *tsk;
  114. struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
  115. u16 guestid;
  116. u32 pfn_limit;
  117. u32 page_offset;
  118. u32 cr2;
  119. int halted;
  120. int ts;
  121. u32 next_hcall;
  122. u32 esp1;
  123. u8 ss1;
  124. /* Do we need to stop what we're doing and return to userspace? */
  125. int break_out;
  126. wait_queue_head_t break_wq;
  127. /* Bitmap of what has changed: see CHANGED_* above. */
  128. int changed;
  129. struct lguest_pages *last_pages;
  130. /* We keep a small number of these. */
  131. u32 pgdidx;
  132. struct pgdir pgdirs[4];
  133. /* Cached wakeup: we hold a reference to this task. */
  134. struct task_struct *wake;
  135. unsigned long noirq_start, noirq_end;
  136. int dma_is_pending;
  137. unsigned long pending_dma; /* struct lguest_dma */
  138. unsigned long pending_key; /* address they're sending to */
  139. unsigned int stack_pages;
  140. u32 tsc_khz;
  141. struct lguest_dma_info dma[LGUEST_MAX_DMA];
  142. /* Dead? */
  143. const char *dead;
  144. /* The GDT entries copied into lguest_ro_state when running. */
  145. struct desc_struct gdt[GDT_ENTRIES];
  146. /* The IDT entries: some copied into lguest_ro_state when running. */
  147. struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS];
  148. struct desc_struct syscall_idt;
  149. /* Virtual clock device */
  150. struct hrtimer hrt;
  151. /* Pending virtual interrupts */
  152. DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
  153. };
  154. extern struct lguest lguests[];
  155. extern struct mutex lguest_lock;
  156. /* core.c: */
  157. u32 lgread_u32(struct lguest *lg, unsigned long addr);
  158. void lgwrite_u32(struct lguest *lg, unsigned long addr, u32 val);
  159. void lgread(struct lguest *lg, void *buf, unsigned long addr, unsigned len);
  160. void lgwrite(struct lguest *lg, unsigned long, const void *buf, unsigned len);
  161. int find_free_guest(void);
  162. int lguest_address_ok(const struct lguest *lg,
  163. unsigned long addr, unsigned long len);
  164. int run_guest(struct lguest *lg, unsigned long __user *user);
  165. /* interrupts_and_traps.c: */
  166. void maybe_do_interrupt(struct lguest *lg);
  167. int deliver_trap(struct lguest *lg, unsigned int num);
  168. void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi);
  169. void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages);
  170. void pin_stack_pages(struct lguest *lg);
  171. void setup_default_idt_entries(struct lguest_ro_state *state,
  172. const unsigned long *def);
  173. void copy_traps(const struct lguest *lg, struct desc_struct *idt,
  174. const unsigned long *def);
  175. void guest_set_clockevent(struct lguest *lg, unsigned long delta);
  176. void init_clockdev(struct lguest *lg);
  177. /* segments.c: */
  178. void setup_default_gdt_entries(struct lguest_ro_state *state);
  179. void setup_guest_gdt(struct lguest *lg);
  180. void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num);
  181. void guest_load_tls(struct lguest *lg, unsigned long tls_array);
  182. void copy_gdt(const struct lguest *lg, struct desc_struct *gdt);
  183. void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt);
  184. /* page_tables.c: */
  185. int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
  186. void free_guest_pagetable(struct lguest *lg);
  187. void guest_new_pagetable(struct lguest *lg, unsigned long pgtable);
  188. void guest_set_pmd(struct lguest *lg, unsigned long cr3, u32 i);
  189. void guest_pagetable_clear_all(struct lguest *lg);
  190. void guest_pagetable_flush_user(struct lguest *lg);
  191. void guest_set_pte(struct lguest *lg, unsigned long cr3,
  192. unsigned long vaddr, gpte_t val);
  193. void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages);
  194. int demand_page(struct lguest *info, unsigned long cr2, int errcode);
  195. void pin_page(struct lguest *lg, unsigned long vaddr);
  196. /* lguest_user.c: */
  197. int lguest_device_init(void);
  198. void lguest_device_remove(void);
  199. /* io.c: */
  200. void lguest_io_init(void);
  201. int bind_dma(struct lguest *lg,
  202. unsigned long key, unsigned long udma, u16 numdmas, u8 interrupt);
  203. void send_dma(struct lguest *info, unsigned long key, unsigned long udma);
  204. void release_all_dma(struct lguest *lg);
  205. unsigned long get_dma_buffer(struct lguest *lg, unsigned long key,
  206. unsigned long *interrupt);
  207. /* hypercalls.c: */
  208. void do_hypercalls(struct lguest *lg);
  209. /*L:035
  210. * Let's step aside for the moment, to study one important routine that's used
  211. * widely in the Host code.
  212. *
  213. * There are many cases where the Guest does something invalid, like pass crap
  214. * to a hypercall. Since only the Guest kernel can make hypercalls, it's quite
  215. * acceptable to simply terminate the Guest and give the Launcher a nicely
  216. * formatted reason. It's also simpler for the Guest itself, which doesn't
  217. * need to check most hypercalls for "success"; if you're still running, it
  218. * succeeded.
  219. *
  220. * Once this is called, the Guest will never run again, so most Host code can
  221. * call this then continue as if nothing had happened. This means many
  222. * functions don't have to explicitly return an error code, which keeps the
  223. * code simple.
  224. *
  225. * It also means that this can be called more than once: only the first one is
  226. * remembered. The only trick is that we still need to kill the Guest even if
  227. * we can't allocate memory to store the reason. Linux has a neat way of
  228. * packing error codes into invalid pointers, so we use that here.
  229. *
  230. * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
  231. * } while(0)".
  232. */
  233. #define kill_guest(lg, fmt...) \
  234. do { \
  235. if (!(lg)->dead) { \
  236. (lg)->dead = kasprintf(GFP_ATOMIC, fmt); \
  237. if (!(lg)->dead) \
  238. (lg)->dead = ERR_PTR(-ENOMEM); \
  239. } \
  240. } while(0)
  241. /* (End of aside) :*/
  242. static inline unsigned long guest_pa(struct lguest *lg, unsigned long vaddr)
  243. {
  244. return vaddr - lg->page_offset;
  245. }
  246. #endif /* __ASSEMBLY__ */
  247. #endif /* _LGUEST_H */