Skip to contentSkip to conversation

i386 Vmi descriptor changes

RFC PATCH In review Zachary Amsden 4 participants 5 messages Mar 13, 2006 – Mar 23, 2006

lore
Share & tools
lore
#0 lore raw

RFC PATCH 10/24 i386 Vmi descriptor changes

Descriptor and trap table cleanups. Add cleanly written accessors for IDT and GDT gates so the subarch may override them. Note that this allows the hypervisor to transparently tweak the DPL of the descriptors as well as the RPL of segments in those descriptors, with no unnecessary kernel code modification. It also allows the hypervisor implementation of the VMI to tweak the gates, allowing for custom exception frames or extra layers of indirection above the guest fault / IRQ handlers.

arch/i386/kernel/traps.c +5 -19
Index: linux-2.6.16-rc5/arch/i386/kernel/traps.c
===================================================================
@@ -1043,20 +1043,6 @@ void __init trap_init_f00f_bug(void)
 }
 #endif
 
-#define _set_gate(gate_addr,type,dpl,addr,seg) \
-do { \
-  int __d0, __d1; \
-  __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
-	"movw %4,%%dx\n\t" \
-	"movl %%eax,%0\n\t" \
-	"movl %%edx,%1" \
-	:"=m" (*((long *) (gate_addr))), \
-	 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
-	:"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
-	 "3" ((char *) (addr)),"2" ((seg) << 16)); \
-} while (0)
-
-
 /*
  * This needs to use 'idt_table' rather than 'idt', and
  * thus use the _nonmapped_ version of the IDT, as the
@@ -1065,7 +1051,7 @@ do { \
  */
 void set_intr_gate(unsigned int n, void *addr)
 {
-	_set_gate(idt_table+n,14,0,addr,__KERNEL_CS);
+	_set_gate(n, DESCTYPE_INT, addr, __KERNEL_CS);
 }
 
 /*
@@ -1073,22 +1059,22 @@ void set_intr_gate(unsigned int n, void 
  */
 static inline void set_system_intr_gate(unsigned int n, void *addr)
 {
-	_set_gate(idt_table+n, 14, 3, addr, __KERNEL_CS);
+	_set_gate(n, DESCTYPE_INT | DESCTYPE_DPL3, addr, __KERNEL_CS);
 }
 
 static void __init set_trap_gate(unsigned int n, void *addr)
 {
-	_set_gate(idt_table+n,15,0,addr,__KERNEL_CS);
+	_set_gate(n, DESCTYPE_TRAP, addr, __KERNEL_CS);
 }
 
 static void __init set_system_gate(unsigned int n, void *addr)
 {
-	_set_gate(idt_table+n,15,3,addr,__KERNEL_CS);
+	_set_gate(n, DESCTYPE_TRAP | DESCTYPE_DPL3, addr, __KERNEL_CS);
 }
 
 static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
 {
-	_set_gate(idt_table+n,5,0,0,(gdt_entry<<3));
+	_set_gate(n, DESCTYPE_TASK, (void *)0, (gdt_entry<<3));
 }
 
 
include/asm-i386/desc.h +45 -47

119 lines not on this page · raw message

include/asm-i386/mach-vmi/mach_desc.h +171 -0

171 lines not on this page · raw message

include/asm-i386/mach-default/mach_desc.h +39 -0
Index: linux-2.6.16-rc5/include/asm-i386/mach-default/mach_desc.h
===================================================================
@@ -0,0 +1,39 @@
+#ifndef __MACH_DESC_H
+#define __MACH_DESC_H
+
+#define load_TR_desc() __asm__ __volatile__("ltr %w0"::"q" (GDT_ENTRY_TSS*8))
+#define load_LDT_desc() __asm__ __volatile__("lldt %w0"::"q" (GDT_ENTRY_LDT*8))
+
+#define load_gdt(dtr) __asm__ __volatile("lgdt %0"::"m" (*dtr))
+#define load_idt(dtr) __asm__ __volatile("lidt %0"::"m" (*dtr))
+#define load_tr(tr) __asm__ __volatile("ltr %0"::"m" (tr))
+#define load_ldt(ldt) __asm__ __volatile("lldt %0"::"m" (ldt))
+
+#define store_gdt(dtr) __asm__ ("sgdt %0":"=m" (*dtr))
+#define store_idt(dtr) __asm__ ("sidt %0":"=m" (*dtr))
+#define store_tr(tr) __asm__ ("str %0":"=m" (tr))
+#define store_ldt(ldt) __asm__ ("sldt %0":"=m" (ldt))
+
+#if TLS_SIZE != 24
+# error update this code.
+#endif
+
+static inline void load_TLS(struct thread_struct *t, unsigned int cpu)
+{
+#define C(i) get_cpu_gdt_table(cpu)[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]
+	C(0); C(1); C(2);
+#undef C
+}
+
+static inline void write_dt_entry(void *dt, int entry, __u32 entry_a, __u32 entry_b)
+{
+	__u32 *lp = (__u32 *)((char *)dt + entry*8);
+	*lp = entry_a;
+	*(lp+1) = entry_b;
+}
+
+#define write_ldt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
+#define write_gdt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
+#define write_idt_entry(dt, entry, a, b) write_dt_entry(dt, entry, a, b)
+
+#endif
#1 lore raw
Quoted 7 lines · * Zachary Amsden ([email protected]) wrote:

Descriptor and trap table cleanups. Add cleanly written accessors for IDT and GDT gates so the subarch may override them. Note that this allows the hypervisor to transparently tweak the DPL of the descriptors as well as the RPL of segments in those descriptors, with no unnecessary kernel code modification. It also allows the hypervisor implementation of the VMI to tweak the gates, allowing for custom exception frames or extra layers of indirection above the guest fault / IRQ handlers.

Nice cleanups, VMI or not.

thanks,
-chris

#2 lore raw
In reply to Zachary Amsden

Hi!

include/asm-i386/mach-vmi/mach_desc.h line 123

Quoted 9 lines
include/asm-i386/mach-vmi/mach_desc.h +9 -0
+static inline void vmi_write_gdt(void *gdt, unsigned entry, u32 descLo, u32 descHi)
+{
+	vmi_wrap_call(
+		WriteGDTEntry, "movl %2, (%0,%1,8);"
+			       "movl %3, 4(%0,%1,8);",
+		VMI_NO_OUTPUT,
+		4, XCONC(VMI_IREG1(gdt), VMI_IREG2(entry), VMI_IREG3(descLo), VMI_IREG4(descHi)),
+		VMI_CLOBBER_EXTENDED(ZERO_RETURNS, "memory"));
+}

I'd say "not funny" here. Very little comments for very obscure code. "movl %3, 4(%0,%1,8);" is particulary "interesting".

include/asm-i386/mach-vmi/mach_desc.h line 160

Quoted 4 lines
include/asm-i386/mach-vmi/mach_desc.h +4 -0
+static inline void write_gdt_entry(void *gdt, int entry, __u32 entry_a, __u32 entry_b)
+{
+        vmi_write_gdt(gdt, entry, entry_a, entry_b);
+}

You should be able to use u32 (not __u32) here.
Pavel

Signature · 1 line
142:        byte [] Bytes = new byte[ 4 ];
#3 lore raw
In reply to Zachary Amsden

arch/i386/kernel/traps.c void __init trap_init_f00f_bug(void) · line 1057

Quoted 13 lines
arch/i386/kernel/traps.c +0 -12
-#define _set_gate(gate_addr,type,dpl,addr,seg) \
-do { \
-  int __d0, __d1; \
-  __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
-	"movw %4,%%dx\n\t" \
-	"movl %%eax,%0\n\t" \
-	"movl %%edx,%1" \
-	:"=m" (*((long *) (gate_addr))), \
-	 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
-	:"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
-	 "3" ((char *) (addr)),"2" ((seg) << 16)); \
-} while (0)

The ugly piece of code doesn't do anything priviledged. So why do you want to move it?

-Andi

#4 lore raw
Quoted 9 lines · Andi Kleen
Quoted 9 lines
arch/i386/kernel/traps.c +0 -9
-#define _set_gate(gate_addr,type,dpl,addr,seg) \
-do { \
-  int __d0, __d1; \
-  __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
-	"movw %4,%%dx\n\t" \
-	"movl %%eax,%0\n\t" \
-	"movl %%edx,%1" \
-	:"=m" (*((long *) (gate_addr))), \
-	 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
Quoted 10 lines

arch/i386/kernel/traps.c void __init trap_init_f00f_bug(void) · line 1057

Quoted 4 lines
arch/i386/kernel/traps.c +0 -3
-	:"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
-	 "3" ((char *) (addr)),"2" ((seg) << 16)); \
-} while (0)

The ugly piece of code doesn't do anything priviledged. So why do you want to move it?

I didn't think I moved it. I wrote a clean version instead. It does do something problematic for virtualization, by not allowing the VMI layer to override the DPL on descriptors or RPL of segments. It might be convenient for the hypervisor platform layer to do that.

Zach