switcher.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*P:900 This is the Switcher: code which sits at 0xFFC00000 to do the low-level
  2. * Guest<->Host switch. It is as simple as it can be made, but it's naturally
  3. * very specific to x86.
  4. *
  5. * You have now completed Preparation. If this has whet your appetite; if you
  6. * are feeling invigorated and refreshed then the next, more challenging stage
  7. * can be found in "make Guest". :*/
  8. /*S:100
  9. * Welcome to the Switcher itself!
  10. *
  11. * This file contains the low-level code which changes the CPU to run the Guest
  12. * code, and returns to the Host when something happens. Understand this, and
  13. * you understand the heart of our journey.
  14. *
  15. * Because this is in assembler rather than C, our tale switches from prose to
  16. * verse. First I tried limericks:
  17. *
  18. * There once was an eax reg,
  19. * To which our pointer was fed,
  20. * It needed an add,
  21. * Which asm-offsets.h had
  22. * But this limerick is hurting my head.
  23. *
  24. * Next I tried haikus, but fitting the required reference to the seasons in
  25. * every stanza was quickly becoming tiresome:
  26. *
  27. * The %eax reg
  28. * Holds "struct lguest_pages" now:
  29. * Cherry blossoms fall.
  30. *
  31. * Then I started with Heroic Verse, but the rhyming requirement leeched away
  32. * the content density and led to some uniquely awful oblique rhymes:
  33. *
  34. * These constants are coming from struct offsets
  35. * For use within the asm switcher text.
  36. *
  37. * Finally, I settled for something between heroic hexameter, and normal prose
  38. * with inappropriate linebreaks. Anyway, it aint no Shakespeare.
  39. */
  40. // Not all kernel headers work from assembler
  41. // But these ones are needed: the ENTRY() define
  42. // And constants extracted from struct offsets
  43. // To avoid magic numbers and breakage:
  44. // Should they change the compiler can't save us
  45. // Down here in the depths of assembler code.
  46. #include <linux/linkage.h>
  47. #include <asm/asm-offsets.h>
  48. #include "lg.h"
  49. // We mark the start of the code to copy
  50. // It's placed in .text tho it's never run here
  51. // You'll see the trick macro at the end
  52. // Which interleaves data and text to effect.
  53. .text
  54. ENTRY(start_switcher_text)
  55. // When we reach switch_to_guest we have just left
  56. // The safe and comforting shores of C code
  57. // %eax has the "struct lguest_pages" to use
  58. // Where we save state and still see it from the Guest
  59. // And %ebx holds the Guest shadow pagetable:
  60. // Once set we have truly left Host behind.
  61. ENTRY(switch_to_guest)
  62. // We told gcc all its regs could fade,
  63. // Clobbered by our journey into the Guest
  64. // We could have saved them, if we tried
  65. // But time is our master and cycles count.
  66. // Segment registers must be saved for the Host
  67. // We push them on the Host stack for later
  68. pushl %es
  69. pushl %ds
  70. pushl %gs
  71. pushl %fs
  72. // But the compiler is fickle, and heeds
  73. // No warning of %ebp clobbers
  74. // When frame pointers are used. That register
  75. // Must be saved and restored or chaos strikes.
  76. pushl %ebp
  77. // The Host's stack is done, now save it away
  78. // In our "struct lguest_pages" at offset
  79. // Distilled into asm-offsets.h
  80. movl %esp, LGUEST_PAGES_host_sp(%eax)
  81. // All saved and there's now five steps before us:
  82. // Stack, GDT, IDT, TSS
  83. // And last of all the page tables are flipped.
  84. // Yet beware that our stack pointer must be
  85. // Always valid lest an NMI hits
  86. // %edx does the duty here as we juggle
  87. // %eax is lguest_pages: our stack lies within.
  88. movl %eax, %edx
  89. addl $LGUEST_PAGES_regs, %edx
  90. movl %edx, %esp
  91. // The Guest's GDT we so carefully
  92. // Placed in the "struct lguest_pages" before
  93. lgdt LGUEST_PAGES_guest_gdt_desc(%eax)
  94. // The Guest's IDT we did partially
  95. // Move to the "struct lguest_pages" as well.
  96. lidt LGUEST_PAGES_guest_idt_desc(%eax)
  97. // The TSS entry which controls traps
  98. // Must be loaded up with "ltr" now:
  99. // For after we switch over our page tables
  100. // It (as the rest) will be writable no more.
  101. // (The GDT entry TSS needs
  102. // Changes type when we load it: damn Intel!)
  103. movl $(GDT_ENTRY_TSS*8), %edx
  104. ltr %dx
  105. // Look back now, before we take this last step!
  106. // The Host's TSS entry was also marked used;
  107. // Let's clear it again, ere we return.
  108. // The GDT descriptor of the Host
  109. // Points to the table after two "size" bytes
  110. movl (LGUEST_PAGES_host_gdt_desc+2)(%eax), %edx
  111. // Clear the type field of "used" (byte 5, bit 2)
  112. andb $0xFD, (GDT_ENTRY_TSS*8 + 5)(%edx)
  113. // Once our page table's switched, the Guest is live!
  114. // The Host fades as we run this final step.
  115. // Our "struct lguest_pages" is now read-only.
  116. movl %ebx, %cr3
  117. // The page table change did one tricky thing:
  118. // The Guest's register page has been mapped
  119. // Writable onto our %esp (stack) --
  120. // We can simply pop off all Guest regs.
  121. popl %ebx
  122. popl %ecx
  123. popl %edx
  124. popl %esi
  125. popl %edi
  126. popl %ebp
  127. popl %gs
  128. popl %eax
  129. popl %fs
  130. popl %ds
  131. popl %es
  132. // Near the base of the stack lurk two strange fields
  133. // Which we fill as we exit the Guest
  134. // These are the trap number and its error
  135. // We can simply step past them on our way.
  136. addl $8, %esp
  137. // The last five stack slots hold return address
  138. // And everything needed to change privilege
  139. // Into the Guest privilege level of 1,
  140. // And the stack where the Guest had last left it.
  141. // Interrupts are turned back on: we are Guest.
  142. iret
  143. // There are two paths where we switch to the Host
  144. // So we put the routine in a macro.
  145. // We are on our way home, back to the Host
  146. // Interrupted out of the Guest, we come here.
  147. #define SWITCH_TO_HOST \
  148. /* We save the Guest state: all registers first \
  149. * Laid out just as "struct lguest_regs" defines */ \
  150. pushl %es; \
  151. pushl %ds; \
  152. pushl %fs; \
  153. pushl %eax; \
  154. pushl %gs; \
  155. pushl %ebp; \
  156. pushl %edi; \
  157. pushl %esi; \
  158. pushl %edx; \
  159. pushl %ecx; \
  160. pushl %ebx; \
  161. /* Our stack and our code are using segments \
  162. * Set in the TSS and IDT \
  163. * Yet if we were to touch data we'd use \
  164. * Whatever data segment the Guest had. \
  165. * Load the lguest ds segment for now. */ \
  166. movl $(LGUEST_DS), %eax; \
  167. movl %eax, %ds; \
  168. /* So where are we? Which CPU, which struct? \
  169. * The stack is our clue: our TSS sets \
  170. * It at the end of "struct lguest_pages" \
  171. * And we then pushed and pushed and pushed Guest regs: \
  172. * Now stack points atop the "struct lguest_regs". \
  173. * Subtract that offset, and we find our struct. */ \
  174. movl %esp, %eax; \
  175. subl $LGUEST_PAGES_regs, %eax; \
  176. /* Save our trap number: the switch will obscure it \
  177. * (The Guest regs are not mapped here in the Host) \
  178. * %ebx holds it safe for deliver_to_host */ \
  179. movl LGUEST_PAGES_regs_trapnum(%eax), %ebx; \
  180. /* The Host GDT, IDT and stack! \
  181. * All these lie safely hidden from the Guest: \
  182. * We must return to the Host page tables \
  183. * (Hence that was saved in struct lguest_pages) */ \
  184. movl LGUEST_PAGES_host_cr3(%eax), %edx; \
  185. movl %edx, %cr3; \
  186. /* As before, when we looked back at the Host \
  187. * As we left and marked TSS unused \
  188. * So must we now for the Guest left behind. */ \
  189. andb $0xFD, (LGUEST_PAGES_guest_gdt+GDT_ENTRY_TSS*8+5)(%eax); \
  190. /* Switch to Host's GDT, IDT. */ \
  191. lgdt LGUEST_PAGES_host_gdt_desc(%eax); \
  192. lidt LGUEST_PAGES_host_idt_desc(%eax); \
  193. /* Restore the Host's stack where it's saved regs lie */ \
  194. movl LGUEST_PAGES_host_sp(%eax), %esp; \
  195. /* Last the TSS: our Host is complete */ \
  196. movl $(GDT_ENTRY_TSS*8), %edx; \
  197. ltr %dx; \
  198. /* Restore now the regs saved right at the first. */ \
  199. popl %ebp; \
  200. popl %fs; \
  201. popl %gs; \
  202. popl %ds; \
  203. popl %es
  204. // Here's where we come when the Guest has just trapped:
  205. // (Which trap we'll see has been pushed on the stack).
  206. // We need only switch back, and the Host will decode
  207. // Why we came home, and what needs to be done.
  208. return_to_host:
  209. SWITCH_TO_HOST
  210. iret
  211. // An interrupt, with some cause external
  212. // Has ajerked us rudely from the Guest's code
  213. // Again we must return home to the Host
  214. deliver_to_host:
  215. SWITCH_TO_HOST
  216. // But now we must go home via that place
  217. // Where that interrupt was supposed to go
  218. // Had we not been ensconced, running the Guest.
  219. // Here we see the cleverness of our stack:
  220. // The Host stack is formed like an interrupt
  221. // With EIP, CS and EFLAGS layered.
  222. // Interrupt handlers end with "iret"
  223. // And that will take us home at long long last.
  224. // But first we must find the handler to call!
  225. // The IDT descriptor for the Host
  226. // Has two bytes for size, and four for address:
  227. // %edx will hold it for us for now.
  228. movl (LGUEST_PAGES_host_idt_desc+2)(%eax), %edx
  229. // We now know the table address we need,
  230. // And saved the trap's number inside %ebx.
  231. // Yet the pointer to the handler is smeared
  232. // Across the bits of the table entry.
  233. // What oracle can tell us how to extract
  234. // From such a convoluted encoding?
  235. // I consulted gcc, and it gave
  236. // These instructions, which I gladly credit:
  237. leal (%edx,%ebx,8), %eax
  238. movzwl (%eax),%edx
  239. movl 4(%eax), %eax
  240. xorw %ax, %ax
  241. orl %eax, %edx
  242. // Now the address of the handler's in %edx
  243. // We call it now: its "iret" takes us home.
  244. jmp *%edx
  245. // Every interrupt can come to us here
  246. // But we must truly tell each apart.
  247. // They number two hundred and fifty six
  248. // And each must land in a different spot,
  249. // Push its number on stack, and join the stream.
  250. // And worse, a mere six of the traps stand apart
  251. // And push on their stack an addition:
  252. // An error number, thirty two bits long
  253. // So we punish the other two fifty
  254. // And make them push a zero so they match.
  255. // Yet two fifty six entries is long
  256. // And all will look most the same as the last
  257. // So we create a macro which can make
  258. // As many entries as we need to fill.
  259. // Note the change to .data then .text:
  260. // We plant the address of each entry
  261. // Into a (data) table for the Host
  262. // To know where each Guest interrupt should go.
  263. .macro IRQ_STUB N TARGET
  264. .data; .long 1f; .text; 1:
  265. // Trap eight, ten through fourteen and seventeen
  266. // Supply an error number. Else zero.
  267. .if (\N <> 8) && (\N < 10 || \N > 14) && (\N <> 17)
  268. pushl $0
  269. .endif
  270. pushl $\N
  271. jmp \TARGET
  272. ALIGN
  273. .endm
  274. // This macro creates numerous entries
  275. // Using GAS macros which out-power C's.
  276. .macro IRQ_STUBS FIRST LAST TARGET
  277. irq=\FIRST
  278. .rept \LAST-\FIRST+1
  279. IRQ_STUB irq \TARGET
  280. irq=irq+1
  281. .endr
  282. .endm
  283. // Here's the marker for our pointer table
  284. // Laid in the data section just before
  285. // Each macro places the address of code
  286. // Forming an array: each one points to text
  287. // Which handles interrupt in its turn.
  288. .data
  289. .global default_idt_entries
  290. default_idt_entries:
  291. .text
  292. // The first two traps go straight back to the Host
  293. IRQ_STUBS 0 1 return_to_host
  294. // We'll say nothing, yet, about NMI
  295. IRQ_STUB 2 handle_nmi
  296. // Other traps also return to the Host
  297. IRQ_STUBS 3 31 return_to_host
  298. // All interrupts go via their handlers
  299. IRQ_STUBS 32 127 deliver_to_host
  300. // 'Cept system calls coming from userspace
  301. // Are to go to the Guest, never the Host.
  302. IRQ_STUB 128 return_to_host
  303. IRQ_STUBS 129 255 deliver_to_host
  304. // The NMI, what a fabulous beast
  305. // Which swoops in and stops us no matter that
  306. // We're suspended between heaven and hell,
  307. // (Or more likely between the Host and Guest)
  308. // When in it comes! We are dazed and confused
  309. // So we do the simplest thing which one can.
  310. // Though we've pushed the trap number and zero
  311. // We discard them, return, and hope we live.
  312. handle_nmi:
  313. addl $8, %esp
  314. iret
  315. // We are done; all that's left is Mastery
  316. // And "make Mastery" is a journey long
  317. // Designed to make your fingers itch to code.
  318. // Here ends the text, the file and poem.
  319. ENTRY(end_switcher_text)