switcher_32.S 11 KB

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