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