/*P:6001* The x86 architecture has segments, which involve a table of descriptors2* which can be used to do funky things with virtual address interpretation.3* We originally used to use segments so the Guest couldn't alter the4* Guest<->Host Switcher, and then we had to trim Guest segments, and restore5* for userspace per-thread segments, but trim again for on userspace->kernel6* transitions... This nightmarish creation was contained within this file,7* where we knew not to tread without heavy armament and a change of underwear.8*9* In these modern times, the segment handling code consists of simple sanity10* checks, and the worst you'll experience reading this code is butterfly-rash11* from frolicking through its parklike serenity.12:*/13#include "lg.h"1415/*H:60016* Segments & The Global Descriptor Table17*18* (That title sounds like a bad Nerdcore group. Not to suggest that there are19* any good Nerdcore groups, but in high school a friend of mine had a band20* called Joe Fish and the Chips, so there are definitely worse band names).21*22* To refresh: the GDT is a table of 8-byte values describing segments. Once23* set up, these segments can be loaded into one of the 6 "segment registers".24*25* GDT entries are passed around as "struct desc_struct"s, which like IDT26* entries are split into two 32-bit members, "a" and "b". One day, someone27* will clean that up, and be declared a Hero. (No pressure, I'm just saying).28*29* Anyway, the GDT entry contains a base (the start address of the segment), a30* limit (the size of the segment - 1), and some flags. Sounds simple, and it31* would be, except those zany Intel engineers decided that it was too boring32* to put the base at one end, the limit at the other, and the flags in33* between. They decided to shotgun the bits at random throughout the 8 bytes,34* like so:35*36* 0 16 40 48 52 56 6337* [ limit part 1 ][ base part 1 ][ flags ][li][fl][base ]38* mit ags part 239* part 240*41* As a result, this file contains a certain amount of magic numeracy. Let's42* begin.43*/4445/*46* There are several entries we don't let the Guest set. The TSS entry is the47* "Task State Segment" which controls all kinds of delicate things. The48* LGUEST_CS and LGUEST_DS entries are reserved for the Switcher, and the49* the Guest can't be trusted to deal with double faults.50*/51static bool ignored_gdt(unsigned int num)52{53return (num == GDT_ENTRY_TSS54|| num == GDT_ENTRY_LGUEST_CS55|| num == GDT_ENTRY_LGUEST_DS56|| num == GDT_ENTRY_DOUBLEFAULT_TSS);57}5859/*H:63060* Once the Guest gave us new GDT entries, we fix them up a little. We61* don't care if they're invalid: the worst that can happen is a General62* Protection Fault in the Switcher when it restores a Guest segment register63* which tries to use that entry. Then we kill the Guest for causing such a64* mess: the message will be "unhandled trap 256".65*/66static void fixup_gdt_table(struct lg_cpu *cpu, unsigned start, unsigned end)67{68unsigned int i;6970for (i = start; i < end; i++) {71/*72* We never copy these ones to real GDT, so we don't care what73* they say74*/75if (ignored_gdt(i))76continue;7778/*79* Segment descriptors contain a privilege level: the Guest is80* sometimes careless and leaves this as 0, even though it's81* running at privilege level 1. If so, we fix it here.82*/83if ((cpu->arch.gdt[i].b & 0x00006000) == 0)84cpu->arch.gdt[i].b |= (GUEST_PL << 13);8586/*87* Each descriptor has an "accessed" bit. If we don't set it88* now, the CPU will try to set it when the Guest first loads89* that entry into a segment register. But the GDT isn't90* writable by the Guest, so bad things can happen.91*/92cpu->arch.gdt[i].b |= 0x00000100;93}94}9596/*H:61097* Like the IDT, we never simply use the GDT the Guest gives us. We keep98* a GDT for each CPU, and copy across the Guest's entries each time we want to99* run the Guest on that CPU.100*101* This routine is called at boot or modprobe time for each CPU to set up the102* constant GDT entries: the ones which are the same no matter what Guest we're103* running.104*/105void setup_default_gdt_entries(struct lguest_ro_state *state)106{107struct desc_struct *gdt = state->guest_gdt;108unsigned long tss = (unsigned long)&state->guest_tss;109110/* The Switcher segments are full 0-4G segments, privilege level 0 */111gdt[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT;112gdt[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT;113114/*115* The TSS segment refers to the TSS entry for this particular CPU.116* Forgive the magic flags: the 0x8900 means the entry is Present, it's117* privilege level 0 Available 386 TSS system segment, and the 0x67118* means Saturn is eclipsed by Mercury in the twelfth house.119*/120gdt[GDT_ENTRY_TSS].a = 0x00000067 | (tss << 16);121gdt[GDT_ENTRY_TSS].b = 0x00008900 | (tss & 0xFF000000)122| ((tss >> 16) & 0x000000FF);123}124125/*126* This routine sets up the initial Guest GDT for booting. All entries start127* as 0 (unusable).128*/129void setup_guest_gdt(struct lg_cpu *cpu)130{131/*132* Start with full 0-4G segments...except the Guest is allowed to use133* them, so set the privilege level appropriately in the flags.134*/135cpu->arch.gdt[GDT_ENTRY_KERNEL_CS] = FULL_EXEC_SEGMENT;136cpu->arch.gdt[GDT_ENTRY_KERNEL_DS] = FULL_SEGMENT;137cpu->arch.gdt[GDT_ENTRY_KERNEL_CS].b |= (GUEST_PL << 13);138cpu->arch.gdt[GDT_ENTRY_KERNEL_DS].b |= (GUEST_PL << 13);139}140141/*H:650142* An optimization of copy_gdt(), for just the three "thead-local storage"143* entries.144*/145void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt)146{147unsigned int i;148149for (i = GDT_ENTRY_TLS_MIN; i <= GDT_ENTRY_TLS_MAX; i++)150gdt[i] = cpu->arch.gdt[i];151}152153/*H:640154* When the Guest is run on a different CPU, or the GDT entries have changed,155* copy_gdt() is called to copy the Guest's GDT entries across to this CPU's156* GDT.157*/158void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt)159{160unsigned int i;161162/*163* The default entries from setup_default_gdt_entries() are not164* replaced. See ignored_gdt() above.165*/166for (i = 0; i < GDT_ENTRIES; i++)167if (!ignored_gdt(i))168gdt[i] = cpu->arch.gdt[i];169}170171/*H:620172* This is where the Guest asks us to load a new GDT entry173* (LHCALL_LOAD_GDT_ENTRY). We tweak the entry and copy it in.174*/175void load_guest_gdt_entry(struct lg_cpu *cpu, u32 num, u32 lo, u32 hi)176{177/*178* We assume the Guest has the same number of GDT entries as the179* Host, otherwise we'd have to dynamically allocate the Guest GDT.180*/181if (num >= ARRAY_SIZE(cpu->arch.gdt)) {182kill_guest(cpu, "too many gdt entries %i", num);183return;184}185186/* Set it up, then fix it. */187cpu->arch.gdt[num].a = lo;188cpu->arch.gdt[num].b = hi;189fixup_gdt_table(cpu, num, num+1);190/*191* Mark that the GDT changed so the core knows it has to copy it again,192* even if the Guest is run on the same CPU.193*/194cpu->changed |= CHANGED_GDT;195}196197/*198* This is the fast-track version for just changing the three TLS entries.199* Remember that this happens on every context switch, so it's worth200* optimizing. But wouldn't it be neater to have a single hypercall to cover201* both cases?202*/203void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls)204{205struct desc_struct *tls = &cpu->arch.gdt[GDT_ENTRY_TLS_MIN];206207__lgread(cpu, tls, gtls, sizeof(*tls)*GDT_ENTRY_TLS_ENTRIES);208fixup_gdt_table(cpu, GDT_ENTRY_TLS_MIN, GDT_ENTRY_TLS_MAX+1);209/* Note that just the TLS entries have changed. */210cpu->changed |= CHANGED_GDT_TLS;211}212213/*H:660214* With this, we have finished the Host.215*216* Five of the seven parts of our task are complete. You have made it through217* the Bit of Despair (I think that's somewhere in the page table code,218* myself).219*220* Next, we examine "make Switcher". It's short, but intense.221*/222223224