3 static int desc_ok(const struct desc_struct *gdt)
6 return ((gdt->b & 0x00209000) == 0x00009000);
9 static int segment_present(const struct desc_struct *gdt)
11 return gdt->b & 0x8000;
14 static int ignored_gdt(unsigned int num)
16 return (num == GDT_ENTRY_TSS
17 || num == GDT_ENTRY_LGUEST_CS
18 || num == GDT_ENTRY_LGUEST_DS
19 || num == GDT_ENTRY_DOUBLEFAULT_TSS);
22 /* We don't allow removal of CS, DS or SS; it doesn't make sense. */
23 static void check_segment_use(struct lguest *lg, unsigned int desc)
25 if (lg->regs->gs / 8 == desc)
27 if (lg->regs->fs / 8 == desc)
29 if (lg->regs->es / 8 == desc)
31 if (lg->regs->ds / 8 == desc
32 || lg->regs->cs / 8 == desc
33 || lg->regs->ss / 8 == desc)
34 kill_guest(lg, "Removed live GDT entry %u", desc);
37 static void fixup_gdt_table(struct lguest *lg, unsigned start, unsigned end)
41 for (i = start; i < end; i++) {
42 /* We never copy these ones to real gdt */
46 /* We could fault in switch_to_guest if they are using
47 * a removed segment. */
48 if (!segment_present(&lg->gdt[i])) {
49 check_segment_use(lg, i);
53 if (!desc_ok(&lg->gdt[i]))
54 kill_guest(lg, "Bad GDT descriptor %i", i);
56 /* DPL 0 presumably means "for use by guest". */
57 if ((lg->gdt[i].b & 0x00006000) == 0)
58 lg->gdt[i].b |= (GUEST_PL << 13);
60 /* Set accessed bit, since gdt isn't writable. */
61 lg->gdt[i].b |= 0x00000100;
65 void setup_default_gdt_entries(struct lguest_ro_state *state)
67 struct desc_struct *gdt = state->guest_gdt;
68 unsigned long tss = (unsigned long)&state->guest_tss;
70 /* Hypervisor segments. */
71 gdt[GDT_ENTRY_LGUEST_CS] = FULL_EXEC_SEGMENT;
72 gdt[GDT_ENTRY_LGUEST_DS] = FULL_SEGMENT;
74 /* This is the one which we *cannot* copy from guest, since tss
75 is depended on this lguest_ro_state, ie. this cpu. */
76 gdt[GDT_ENTRY_TSS].a = 0x00000067 | (tss << 16);
77 gdt[GDT_ENTRY_TSS].b = 0x00008900 | (tss & 0xFF000000)
78 | ((tss >> 16) & 0x000000FF);
81 void setup_guest_gdt(struct lguest *lg)
83 lg->gdt[GDT_ENTRY_KERNEL_CS] = FULL_EXEC_SEGMENT;
84 lg->gdt[GDT_ENTRY_KERNEL_DS] = FULL_SEGMENT;
85 lg->gdt[GDT_ENTRY_KERNEL_CS].b |= (GUEST_PL << 13);
86 lg->gdt[GDT_ENTRY_KERNEL_DS].b |= (GUEST_PL << 13);
89 /* This is a fast version for the common case where only the three TLS entries
91 void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt)
95 for (i = GDT_ENTRY_TLS_MIN; i <= GDT_ENTRY_TLS_MAX; i++)
99 void copy_gdt(const struct lguest *lg, struct desc_struct *gdt)
103 for (i = 0; i < GDT_ENTRIES; i++)
108 void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num)
110 if (num > ARRAY_SIZE(lg->gdt))
111 kill_guest(lg, "too many gdt entries %i", num);
113 lgread(lg, lg->gdt, table, num * sizeof(lg->gdt[0]));
114 fixup_gdt_table(lg, 0, ARRAY_SIZE(lg->gdt));
115 lg->changed |= CHANGED_GDT;
118 void guest_load_tls(struct lguest *lg, unsigned long gtls)
120 struct desc_struct *tls = &lg->gdt[GDT_ENTRY_TLS_MIN];
122 lgread(lg, tls, gtls, sizeof(*tls)*GDT_ENTRY_TLS_ENTRIES);
123 fixup_gdt_table(lg, GDT_ENTRY_TLS_MIN, GDT_ENTRY_TLS_MAX+1);
124 lg->changed |= CHANGED_GDT_TLS;