Skip to contentSkip to conversation

Introducing paravirt_ops for x86_64

PATCH In review Glauber de Oliveira Costa 10 participants 187 messages Aug 8, 2007 – Aug 10, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi folks,

After some time away from it, and a big rebase as a consequence, here is the updated version of paravirt_ops for x86_64, heading to inclusion.

Your criticism is of course, very welcome.

Have fun
--

 arch/x86_64/Kconfig              |   11 
 arch/x86_64/ia32/syscall32.c     |    2 
 arch/x86_64/kernel/Makefile      |    1 
 arch/x86_64/kernel/apic.c        |    2 
 arch/x86_64/kernel/asm-offsets.c |   14 
 arch/x86_64/kernel/entry.S       |  125 +++--
 arch/x86_64/kernel/head.S        |   10 
 arch/x86_64/kernel/head64.c      |    2 
 arch/x86_64/kernel/i8259.c       |   15 
 arch/x86_64/kernel/ldt.c         |    6 
 arch/x86_64/kernel/paravirt.c    |  455 +++++++++++++++++++
 arch/x86_64/kernel/process.c     |    2 
 arch/x86_64/kernel/reboot.c      |    3 
 arch/x86_64/kernel/setup.c       |   41 +
 arch/x86_64/kernel/setup64.c     |   18 
 arch/x86_64/kernel/smp.c         |   10 
 arch/x86_64/kernel/smpboot.c     |   10 
 arch/x86_64/kernel/suspend.c     |   11 
 arch/x86_64/kernel/tce.c         |    2 
 arch/x86_64/kernel/time.c        |   37 +
 arch/x86_64/kernel/traps.c       |    1 
 arch/x86_64/kernel/tsc.c         |   42 +
 arch/x86_64/kernel/vmlinux.lds.S |    6 
 arch/x86_64/kernel/vsyscall.c    |    4 
 arch/x86_64/kernel/x8664_ksyms.c |    6 
 arch/x86_64/mm/pageattr.c        |    2 
 arch/x86_64/vdso/vgetcpu.c       |    4 
 include/asm-x86_64/alternative.h |    8 
 include/asm-x86_64/apic.h        |   13 
 include/asm-x86_64/desc.h        |  183 +++++--
 include/asm-x86_64/e820.h        |    6 
 include/asm-x86_64/irq.h         |    2 
 include/asm-x86_64/irqflags.h    |   32 +
 include/asm-x86_64/mmu_context.h |   23 
 include/asm-x86_64/msr.h         |  284 +++++++-----
 include/asm-x86_64/page.h        |   36 +
 include/asm-x86_64/paravirt.h    |  901 +++++++++++++++++++++++++++++++++++++++
 include/asm-x86_64/pgalloc.h     |    7 
 include/asm-x86_64/pgtable.h     |  152 +++---
 include/asm-x86_64/processor.h   |   71 ++-
 include/asm-x86_64/proto.h       |    3 
 include/asm-x86_64/segment.h     |    4 
 include/asm-x86_64/smp.h         |    8 
 include/asm-x86_64/spinlock.h    |   16 
 include/asm-x86_64/tlbflush.h    |   22 
 include/linux/mm.h               |   14 
 46 files changed, 2271 insertions(+), 356 deletions(-)
#1 lore raw

PATCH 1/25 header file move

Later on, the paravirt_ops patch will deference the vm_area_struct in asm/pgtable.h. It means this define must be after the struct definition

---
 include/linux/mm.h |   14 +++++++++-----
 1 files changed, 9 insertions(+), 5 deletions(-)
include/linux/mm.h +9 -5
index 655094d..c3f8561 100644
@@ -35,11 +35,6 @@ extern int sysctl_legacy_va_layout;
 #define sysctl_legacy_va_layout 0
 #endif
 
-#include <asm/page.h>
-#include <asm/pgtable.h>
-#include <asm/processor.h>
-
-#define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
 
 /*
  * Linux kernel virtual memory manager primitives.
@@ -113,6 +108,15 @@ struct vm_area_struct {
 #endif
 };
 
+#include <asm/page.h>
+/*
+ * pgtable.h must be included after the definition of vm_area_struct.
+ * x86_64 pgtable.h is one of the dereferencers of this struct
+ */
+#include <asm/pgtable.h>
+#include <asm/processor.h>
+
+#define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
 extern struct kmem_cache *vm_area_cachep;
 
 /*
Signature · 1 line
1.4.4.2
#2 lore raw

PATCH 2/25 tlb flushing routines

This patch turns the flush_tlb routines into native versions. In case paravirt is not defined, the natives are defined into the actually used ones. flush_tlb_others() goes in smp.c, unless we smp is not in the game

---
 arch/x86_64/kernel/smp.c      |   10 +++++++++-
 include/asm-x86_64/smp.h      |    8 ++++++++
 include/asm-x86_64/tlbflush.h |   22 ++++++++++++++++++----
 3 files changed, 35 insertions(+), 5 deletions(-)
arch/x86_64/kernel/smp.c +9 -1
index 673a300..39f5f6b 100644
@@ -165,7 +165,7 @@ out:
 	cpu_clear(cpu, f->flush_cpumask);
 }
 
-static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
+void native_flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
 						unsigned long va)
 {
 	int sender;
@@ -198,6 +198,14 @@ static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
 	spin_unlock(&f->tlbstate_lock);
 }
 
+/* Overriden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) flush_tlb_others(cpumask_t cpumask,
+					    struct mm_struct *mm,
+					    unsigned long va)
+{
+	native_flush_tlb_others(cpumask, mm, va);
+}
+
 int __cpuinit init_smp_flush(void)
 {
 	int i;
include/asm-x86_64/smp.h +8 -0
index 3f303d2..6b11114 100644
@@ -19,6 +19,14 @@ extern int disable_apic;
 
 #include <asm/pda.h>
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+void native_flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
+						unsigned long va);
+#else
+#define startup_ipi_hook(apicid, rip, rsp) do { } while (0)
+#endif
+
 struct pt_regs;
 
 extern cpumask_t cpu_present_mask;
include/asm-x86_64/tlbflush.h +18 -4
index 888eb4a..1c68cc8 100644
@@ -6,21 +6,30 @@
 #include <asm/processor.h>
 #include <asm/system.h>
 
-static inline void __flush_tlb(void)
+static inline void native_flush_tlb(void)
 {
 	write_cr3(read_cr3());
 }
 
-static inline void __flush_tlb_all(void)
+static inline void native_flush_tlb_all(void)
 {
 	unsigned long cr4 = read_cr4();
 	write_cr4(cr4 & ~X86_CR4_PGE);	/* clear PGE */
 	write_cr4(cr4);			/* write old PGE again and flush TLBs */
 }
 
-#define __flush_tlb_one(addr) \
-	__asm__ __volatile__("invlpg (%0)" :: "r" (addr) : "memory")
+static inline void native_flush_tlb_one(unsigned long addr)
+{
+	asm volatile ("invlpg (%0)" :: "r" (addr) : "memory");
+}
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define __flush_tlb()		native_flush_tlb()
+#define __flush_tlb_all()	native_flush_tlb_all()
+#define __flush_tlb_one(addr)	native_flush_tlb_one(addr)
+#endif	/* CONFIG_PARAVIRT */
 
 /*
  * TLB flushing:
@@ -64,6 +73,11 @@ static inline void flush_tlb_range(struct vm_area_struct *vma,
 		__flush_tlb();
 }
 
+static inline void native_flush_tlb_others(cpumask_t *cpumask,
+					   struct mm_struct *mm, unsigned long va)
+{
+}
+
 #else
 
 #include <asm/smp.h>
Signature · 1 line
1.4.4.2
#4 lore raw

PATCH 3/25 irq_flags / halt routines

In reply to Glauber de Oliveira Costa

This patch turns the irq_flags and halt routines into the native versions.

---
 include/asm-x86_64/irqflags.h |   32 +++++++++++++++++++++++++++-----
 1 files changed, 27 insertions(+), 5 deletions(-)
include/asm-x86_64/irqflags.h +27 -5
index 86e70fe..4ba5241 100644
@@ -16,6 +16,22 @@
  * Interrupt control:
  */
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF);
+}
+#  endif
+
+#else /* PARAVIRT */
+
 static inline unsigned long __raw_local_save_flags(void)
 {
 	unsigned long flags;
@@ -31,9 +47,6 @@ static inline unsigned long __raw_local_save_flags(void)
 	return flags;
 }
 
-#define raw_local_save_flags(flags) \
-		do { (flags) = __raw_local_save_flags(); } while (0)
-
 static inline void raw_local_irq_restore(unsigned long flags)
 {
 	__asm__ __volatile__(
@@ -88,6 +101,10 @@ static inline int raw_irqs_disabled_flags(unsigned long flags)
 
 #endif
 
+#endif /* CONFIG_PARAVIRT */
+
+#define raw_local_save_flags(flags) \
+		do { (flags) = __raw_local_save_flags(); } while (0)
 /*
  * For spinlocks, etc.:
  */
@@ -115,7 +132,7 @@ static inline int raw_irqs_disabled(void)
  * Used in the idle loop; sti takes one instruction cycle
  * to complete:
  */
-static inline void raw_safe_halt(void)
+static inline void native_raw_safe_halt(void)
 {
 	__asm__ __volatile__("sti; hlt" : : : "memory");
 }
@@ -124,11 +141,16 @@ static inline void raw_safe_halt(void)
  * Used when interrupts are already enabled or to
  * shutdown the processor:
  */
-static inline void halt(void)
+static inline void native_halt(void)
 {
 	__asm__ __volatile__("hlt": : :"memory");
 }
 
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt	native_raw_safe_halt
+#define halt		native_halt
+#endif /* ! CONFIG_PARAVIRT */
+
 #else /* __ASSEMBLY__: */
 # ifdef CONFIG_TRACE_IRQFLAGS
 #  define TRACE_IRQS_ON		call trace_hardirqs_on_thunk
Signature · 1 line
1.4.4.2
#6 lore raw

PATCH 4/25 Add debugreg/load_rsp native hooks

In reply to Glauber de Oliveira Costa

This patch adds native hooks for debugreg handling functions, and for the native load_rsp0 function. The later also have its call sites patched.

---
 arch/x86_64/kernel/process.c   |    2 +-
 arch/x86_64/kernel/smpboot.c   |    2 +-
 include/asm-x86_64/processor.h |   71 ++++++++++++++++++++++++++++++++++++----
 3 files changed, 66 insertions(+), 9 deletions(-)
arch/x86_64/kernel/process.c +1 -1
index 2842f50..33046f1 100644
@@ -595,7 +595,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	/*
 	 * Reload esp0, LDT and the page table pointer:
 	 */
-	tss->rsp0 = next->rsp0;
+	load_rsp0(tss, next);
 
 	/* 
 	 * Switch DS and ES.
arch/x86_64/kernel/smpboot.c +1 -1
index 32f5078..be9c7eb 100644
@@ -620,7 +620,7 @@ do_rest:
 	start_rip = setup_trampoline();
 
 	init_rsp = c_idle.idle->thread.rsp;
-	per_cpu(init_tss,cpu).rsp0 = init_rsp;
+ 	load_rsp0(&per_cpu(init_tss,cpu), &c_idle.idle->thread);
 	initial_code = start_secondary;
 	clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
 
include/asm-x86_64/processor.h +64 -7

83 lines not on this page · raw message

Signature · 1 line
1.4.4.2
#8 lore raw

PATCH 5/25 native versions for system.h functions

In reply to Glauber de Oliveira Costa

This patch adds the native hook for the functions in system.h They are the read/write_crX, clts and wbinvd. The later, also gets its call sites patched.

---
 arch/x86_64/kernel/tce.c  |    2 +-
 arch/x86_64/mm/pageattr.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86_64/kernel/tce.c +1 -1
index e3f2569..587f0c2 100644
@@ -42,7 +42,7 @@ static inline void flush_tce(void* tceaddr)
 	if (cpu_has_clflush)
 		asm volatile("clflush (%0)" :: "r" (tceaddr));
 	else
-		asm volatile("wbinvd":::"memory");
+		wbinvd();
 }
 
 void tce_build(struct iommu_table *tbl, unsigned long index,
arch/x86_64/mm/pageattr.c +1 -1
index 7e161c6..b497afd 100644
@@ -76,7 +76,7 @@ static void flush_kernel_map(void *arg)
 	/* When clflush is available always use it because it is
 	   much cheaper than WBINVD. */
 	if (!cpu_has_clflush)
-		asm volatile("wbinvd" ::: "memory");
+		wbinvd();
 	else list_for_each_entry(pg, l, lru) {
 		void *adr = page_address(pg);
 		cache_flush_page(adr);
Signature · 1 line
1.4.4.2
#10 lore raw

PATCH 6/25 add native_apic read and write functions, as well as boot clocks ones

In reply to Glauber de Oliveira Costa

Time for the apic handling functions to get their native counterparts. Also, put the native hook for the boot clocks functions in the apic.h header

---
 arch/x86_64/kernel/apic.c    |    2 +-
 arch/x86_64/kernel/smpboot.c |    8 +++++++-
 include/asm-x86_64/apic.h    |   13 +++++++++++--
 3 files changed, 19 insertions(+), 4 deletions(-)
arch/x86_64/kernel/apic.c +1 -1
index 900ff38..2d233ef 100644
@@ -1193,7 +1193,7 @@ int __init APIC_init_uniprocessor (void)
 		setup_IO_APIC();
 	else
 		nr_ioapics = 0;
-	setup_boot_APIC_clock();
+	setup_boot_clock();
 	check_nmi_watchdog();
 	return 0;
 }
arch/x86_64/kernel/smpboot.c +7 -1
index be9c7eb..5c7eb60 100644
@@ -338,7 +338,7 @@ void __cpuinit start_secondary(void)
 	check_tsc_sync_target();
 
 	Dprintk("cpu %d: setting up apic clock\n", smp_processor_id()); 	
-	setup_secondary_APIC_clock();
+ 	setup_secondary_clock();
 
 	Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
 
@@ -468,6 +468,12 @@ static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int sta
 	num_starts = 2;
 
 	/*
+	 * Paravirt wants a startup IPI hook here to set up the
+	 * target processor state.
+	 */
+	startup_ipi_hook(phys_apicid, (unsigned long) start_rip,
+		         (unsigned long) init_rsp);
+	/*
 	 * Run STARTUP IPI loop.
 	 */
 	Dprintk("#startup loops: %d.\n", num_starts);
include/asm-x86_64/apic.h +11 -2
index 85125ef..de17908 100644
@@ -38,16 +38,25 @@ struct pt_regs;
  * Basic functions accessing APICs.
  */
 
-static __inline void apic_write(unsigned long reg, unsigned int v)
+static __inline void native_apic_write(unsigned long reg, unsigned int v)
 {
 	*((volatile unsigned int *)(APIC_BASE+reg)) = v;
 }
 
-static __inline unsigned int apic_read(unsigned long reg)
+static __inline unsigned int native_apic_read(unsigned long reg)
 {
 	return *((volatile unsigned int *)(APIC_BASE+reg));
 }
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define apic_write(reg, v) native_apic_write(reg, v)
+#define apic_read(reg)  native_apic_read(reg)
+#define setup_boot_clock(void)	setup_boot_APIC_clock(void)
+#define setup_secondary_clock(void) setup_secondary_APIC_clock(void)
+#endif
+
 extern void apic_wait_icr_idle(void);
 extern unsigned int safe_apic_wait_icr_idle(void);
 
Signature · 1 line
1.4.4.2
#12 lore raw

PATCH 7/25 interrupt related native paravirt functions.

In reply to Glauber de Oliveira Costa

The interrupt initialization routine becomes native_init_IRQ and will be overriden later in case paravirt is on.

The interrupt vector is made global, so paravirt guests can reference it in their initializations. However, "interrupt" is such a common name, and could lead to clashes, so it is renamed.

---
 arch/x86_64/kernel/i8259.c |   15 +++++++++++----
 include/asm-x86_64/irq.h   |    2 ++
 2 files changed, 13 insertions(+), 4 deletions(-)
arch/x86_64/kernel/i8259.c +11 -4
index 948cae6..8dda872 100644
@@ -75,8 +75,12 @@ BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd) BUILD_16_IRQS(0xe) BUILD_16_IRQS(0xf)
 	IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
 	IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
 
-/* for the irq vectors */
-static void (*interrupt[NR_VECTORS - FIRST_EXTERNAL_VECTOR])(void) = {
+/*
+ * For the irq vectors. It is global rather than static to allow for
+ * paravirtualized guests to use it in their own interrupt initialization
+ * routines
+ */
+void (*interrupt_vector[NR_VECTORS - FIRST_EXTERNAL_VECTOR])(void) = {
 					  IRQLIST_16(0x2), IRQLIST_16(0x3),
 	IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
 	IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
@@ -484,7 +488,10 @@ static int __init init_timer_sysfs(void)
 
 device_initcall(init_timer_sysfs);
 
-void __init init_IRQ(void)
+/* Overridden in paravirt.c */
+void init_IRQ(void) __attribute__((weak, alias("native_init_IRQ")));
+
+void __init native_init_IRQ(void)
 {
 	int i;
 
@@ -497,7 +504,7 @@ void __init init_IRQ(void)
 	for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
 		int vector = FIRST_EXTERNAL_VECTOR + i;
 		if (vector != IA32_SYSCALL_VECTOR)
-			set_intr_gate(vector, interrupt[i]);
+			set_intr_gate(vector, interrupt_vector[i]);
 	}
 
 #ifdef CONFIG_SMP
include/asm-x86_64/irq.h +2 -0
index 5006c6e..be55299 100644
@@ -46,6 +46,8 @@ static __inline__ int irq_canonicalize(int irq)
 extern void fixup_irqs(cpumask_t map);
 #endif
 
+void native_init_IRQ(void);
+
 #define __ARCH_HAS_DO_SOFTIRQ 1
 
 #endif /* _ASM_IRQ_H */
Signature · 1 line
1.4.4.2
#14 lore raw

PATCH 8/25 use macro for sti/cli in spinlock definitions

In reply to Glauber de Oliveira Costa

This patch switches the cli and sti instructions into macros. In this header, they're just defined to the instructions they refer to. Later on, when paravirt is defined, they will be defined to something with paravirt abilities.

---
 include/asm-x86_64/spinlock.h |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)
include/asm-x86_64/spinlock.h +13 -3
index 88bf981..a81e27a 100644
@@ -5,6 +5,14 @@
 #include <asm/rwlock.h>
 #include <asm/page.h>
 #include <asm/processor.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define CLI_STI_INPUT_ARGS
+#define CLI_STI_CLOBBERS
+#define CLI_STRING "cli"
+#define STI_STRING "sti"
+#endif
 
 /*
  * Your basic SMP spinlocks, allowing only a single CPU anywhere
@@ -48,12 +56,12 @@ static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long fla
 		"jns 5f\n"
 		"testl $0x200, %1\n\t"	/* interrupts were disabled? */
 		"jz 4f\n\t"
-	        "sti\n"
+		STI_STRING "\n\t"
 		"3:\t"
 		"rep;nop\n\t"
 		"cmpl $0, %0\n\t"
 		"jle 3b\n\t"
-		"cli\n\t"
+		CLI_STRING "\n\t"
 		"jmp 1b\n"
 		"4:\t"
 		"rep;nop\n\t"
@@ -61,7 +69,9 @@ static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long fla
 		"jg 1b\n\t"
 		"jmp 4b\n"
 		"5:\n\t"
-		: "+m" (lock->slock) : "r" ((unsigned)flags) : "memory");
+		: "+m" (lock->slock)
+		: "r" ((unsigned)flags), CLI_STI_INPUT_ARGS
+		: "memory", CLI_STI_CLOBBERS);
 }
 #endif
 
Signature · 1 line
1.4.4.2
#16 lore raw

PATCH 9/25 report ring kernel is running without paravirt

In reply to Glauber de Oliveira Costa

When paravirtualization is disabled, the kernel is always running at ring 0. So report it in the appropriate macro

---
 include/asm-x86_64/segment.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/asm-x86_64/segment.h +4 -0
index 04b8ab2..240c1bf 100644
@@ -50,4 +50,8 @@
 #define GDT_SIZE (GDT_ENTRIES * 8)
 #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8) 
 
+#ifndef CONFIG_PARAVIRT
+#define get_kernel_rpl()  0
+#endif
+
 #endif
Signature · 1 line
1.4.4.2
#18 lore raw

PATCH 10/25 export math_state_restore

In reply to Glauber de Oliveira Costa

Export math_state_restore symbol, so it can be used for hypervisors. They are commonly loaded as modules.

---
 arch/x86_64/kernel/traps.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86_64/kernel/traps.c +1 -0
index 0388842..aacbe12 100644
@@ -1081,6 +1081,7 @@ asmlinkage void math_state_restore(void)
 	task_thread_info(me)->status |= TS_USEDFPU;
 	me->fpu_counter++;
 }
+EXPORT_SYMBOL_GPL(math_state_restore);
 
 void __init trap_init(void)
 {
Signature · 1 line
1.4.4.2
#20 lore raw

PATCH 11/25 introduce paravirt_release_pgd()

In reply to Glauber de Oliveira Costa

This patch introduces a new macro/function that informs a paravirt guest when its page table is not more in use, and can be released. In case we're not paravirt, just do nothing.

---
 include/asm-x86_64/pgalloc.h |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
include/asm-x86_64/pgalloc.h +7 -0
index b467be6..dbe1267 100644
@@ -9,6 +9,12 @@
 #define QUICK_PGD 0	/* We preserve special mappings over free */
 #define QUICK_PT 1	/* Other page table pages that are zero on free */
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define paravirt_release_pgd(pgd) do { } while (0)
+#endif
+
 #define pmd_populate_kernel(mm, pmd, pte) \
 		set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte)))
 #define pud_populate(mm, pud, pmd) \
@@ -100,6 +106,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
 static inline void pgd_free(pgd_t *pgd)
 {
 	BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
+	paravirt_release_pgd(pgd);
 	quicklist_free(QUICK_PGD, pgd_dtor, pgd);
 }
 
Signature · 1 line
1.4.4.2
#22 lore raw

PATCH 12/25 native versions for set pagetables

In reply to Glauber de Oliveira Costa

This patch turns the set_p{te,md,ud,gd} functions into their native_ versions. There is no need to patch any caller.

Also, it adds pte_update() and pte_update_defer() calls whenever we modify a page table entry. This last part was coded to match i386 as close as possible.

Pieces of the header are moved to below the #ifdef CONFIG_PARAVIRT site, as they are users of the newly defined set_* macros.

---
 include/asm-x86_64/pgtable.h |  152 ++++++++++++++++++++++++-----------------
 1 files changed, 89 insertions(+), 63 deletions(-)
include/asm-x86_64/pgtable.h +89 -63
index c9d8764..dd572a2 100644
@@ -57,55 +57,77 @@ extern unsigned long empty_zero_page[PAGE_SIZE/sizeof(unsigned long)];
  */
 #define PTRS_PER_PTE	512
 
-#ifndef __ASSEMBLY__
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
 
-#define pte_ERROR(e) \
-	printk("%s:%d: bad pte %p(%016lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
-#define pmd_ERROR(e) \
-	printk("%s:%d: bad pmd %p(%016lx).\n", __FILE__, __LINE__, &(e), pmd_val(e))
-#define pud_ERROR(e) \
-	printk("%s:%d: bad pud %p(%016lx).\n", __FILE__, __LINE__, &(e), pud_val(e))
-#define pgd_ERROR(e) \
-	printk("%s:%d: bad pgd %p(%016lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
+#define set_pte native_set_pte
+#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
+#define set_pmd native_set_pmd
+#define set_pud native_set_pud
+#define set_pgd native_set_pgd
+#define pte_clear(mm,addr,xp)	do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
+#define pmd_clear(xp)	do { set_pmd(xp, __pmd(0)); } while (0)
+#define pud_clear native_pud_clear
+#define pgd_clear native_pgd_clear
+#define pte_update(mm, addr, ptep)              do { } while (0)
+#define pte_update_defer(mm, addr, ptep)        do { } while (0)
 
-#define pgd_none(x)	(!pgd_val(x))
-#define pud_none(x)	(!pud_val(x))
+#endif
+
+#ifndef __ASSEMBLY__
 
-static inline void set_pte(pte_t *dst, pte_t val)
+static inline void native_set_pte(pte_t *dst, pte_t val)
 {
-	pte_val(*dst) = pte_val(val);
+	dst->pte = pte_val(val);
 } 
-#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
 
-static inline void set_pmd(pmd_t *dst, pmd_t val)
+
+static inline void native_set_pmd(pmd_t *dst, pmd_t val)
 {
-        pmd_val(*dst) = pmd_val(val); 
+	dst->pmd = pmd_val(val);
 } 
 
-static inline void set_pud(pud_t *dst, pud_t val)
+static inline void native_set_pud(pud_t *dst, pud_t val)
 {
-	pud_val(*dst) = pud_val(val);
+	dst->pud = pud_val(val);
 }
 
-static inline void pud_clear (pud_t *pud)
+static inline void native_set_pgd(pgd_t *dst, pgd_t val)
 {
-	set_pud(pud, __pud(0));
+	dst->pgd = pgd_val(val);
 }
-
-static inline void set_pgd(pgd_t *dst, pgd_t val)
+static inline void native_pud_clear (pud_t *pud)
 {
-	pgd_val(*dst) = pgd_val(val); 
-} 
+	set_pud(pud, __pud(0));
+}
 
-static inline void pgd_clear (pgd_t * pgd)
+static inline void native_pgd_clear (pgd_t * pgd)
 {
 	set_pgd(pgd, __pgd(0));
 }
 
-#define ptep_get_and_clear(mm,addr,xp)	__pte(xchg(&(xp)->pte, 0))
+#define pte_ERROR(e) \
+	printk("%s:%d: bad pte %p(%016lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
+#define pmd_ERROR(e) \
+	printk("%s:%d: bad pmd %p(%016lx).\n", __FILE__, __LINE__, &(e), pmd_val(e))
+#define pud_ERROR(e) \
+	printk("%s:%d: bad pud %p(%016lx).\n", __FILE__, __LINE__, &(e), pud_val(e))
+#define pgd_ERROR(e) \
+	printk("%s:%d: bad pgd %p(%016lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
+
+#define pgd_none(x)	(!pgd_val(x))
+#define pud_none(x)	(!pud_val(x))
 
 struct mm_struct;
 
+static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	pte_t pte = __pte(xchg(&ptep->pte, 0));
+	pte_update(mm, addr, ptep);
+	return pte;
+}
+
 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
 {
 	pte_t pte;
@@ -245,7 +267,6 @@ static inline unsigned long pmd_bad(pmd_t pmd)
 
 #define pte_none(x)	(!pte_val(x))
 #define pte_present(x)	(pte_val(x) & (_PAGE_PRESENT | _PAGE_PROTNONE))
-#define pte_clear(mm,addr,xp)	do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
 
 #define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT))	/* FIXME: is this
 						   right? */
@@ -254,11 +275,11 @@ static inline unsigned long pmd_bad(pmd_t pmd)
 
 static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
 {
-	pte_t pte;
-	pte_val(pte) = (page_nr << PAGE_SHIFT);
-	pte_val(pte) |= pgprot_val(pgprot);
-	pte_val(pte) &= __supported_pte_mask;
-	return pte;
+	unsigned long pte;
+	pte = (page_nr << PAGE_SHIFT);
+	pte |= pgprot_val(pgprot);
+	pte &= __supported_pte_mask;
+	return __pte(pte);
 }
 
 /*
@@ -282,30 +303,6 @@ static inline pte_t pte_mkwrite(pte_t pte)	{ set_pte(&pte, __pte(pte_val(pte) |
 static inline pte_t pte_mkhuge(pte_t pte)	{ set_pte(&pte, __pte(pte_val(pte) | _PAGE_PSE)); return pte; }
 static inline pte_t pte_clrhuge(pte_t pte)	{ set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_PSE)); return pte; }
 
-struct vm_area_struct;
-
-static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
-{
-	if (!pte_young(*ptep))
-		return 0;
-	return test_and_clear_bit(_PAGE_BIT_ACCESSED, &ptep->pte);
-}
-
-static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
-{
-	clear_bit(_PAGE_BIT_RW, &ptep->pte);
-}
-
-/*
- * Macro to mark a page protection value as "uncacheable".
- */
-#define pgprot_noncached(prot)	(__pgprot(pgprot_val(prot) | _PAGE_PCD | _PAGE_PWT))
-
-static inline int pmd_large(pmd_t pte) { 
-	return (pmd_val(pte) & __LARGE_PTE) == __LARGE_PTE; 
-} 	
-
-
 /*
  * Conversion functions: convert a page and protection to a page entry,
  * and a page entry and page directory to the page they refer to.
@@ -339,7 +336,6 @@ static inline int pmd_large(pmd_t pte) {
 			pmd_index(address))
 #define pmd_none(x)	(!pmd_val(x))
 #define pmd_present(x)	(pmd_val(x) & _PAGE_PRESENT)
-#define pmd_clear(xp)	do { set_pmd(xp, __pmd(0)); } while (0)
 #define pfn_pmd(nr,prot) (__pmd(((nr) << PAGE_SHIFT) | pgprot_val(prot)))
 #define pmd_pfn(x)  ((pmd_val(x) & __PHYSICAL_MASK) >> PAGE_SHIFT)
 
@@ -352,14 +348,43 @@ static inline int pmd_large(pmd_t pte) {
 /* page, protection -> pte */
 #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
 #define mk_pte_huge(entry) (pte_val(entry) |= _PAGE_PRESENT | _PAGE_PSE)
- 
+
+struct vm_area_struct;
+
+#include <linux/mm.h>
+static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
+{
+	int ret = 0;
+	if (!pte_young(*ptep))
+		return 0;
+	ret = test_and_clear_bit(_PAGE_BIT_ACCESSED, &ptep->pte);
+	pte_update(vma->vm_mm, addr, ptep);
+	return ret;
+}
+
+static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	clear_bit(_PAGE_BIT_RW, &ptep->pte);
+	pte_update(mm, addr, ptep);
+}
+
+/*
+ * Macro to mark a page protection value as "uncacheable".
+ */
+#define pgprot_noncached(prot)	(__pgprot(pgprot_val(prot) | _PAGE_PCD | _PAGE_PWT))
+
+static inline int pmd_large(pmd_t pte) {
+	return (pmd_val(pte) & __LARGE_PTE) == __LARGE_PTE;
+}
+
 /* Change flags of a PTE */
-static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
+static inline pte_t pte_modify(pte_t pte_old, pgprot_t newprot)
 { 
-	pte_val(pte) &= _PAGE_CHG_MASK;
-	pte_val(pte) |= pgprot_val(newprot);
-	pte_val(pte) &= __supported_pte_mask;
-       return pte; 
+	unsigned long pte = pte_val(pte_old);
+	pte &= _PAGE_CHG_MASK;
+	pte |= pgprot_val(newprot);
+	pte &= __supported_pte_mask;
+	return __pte(pte);
 }
 
 #define pte_index(address) \
@@ -386,6 +411,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
 	int __changed = !pte_same(*(__ptep), __entry);			  \
 	if (__changed && __dirty) {					  \
 		set_pte(__ptep, __entry);			  	  \
+		pte_update_defer((__vma)->vm_mm, (__address), (__ptep));  \
 		flush_tlb_page(__vma, __address);		  	  \
 	}								  \
 	__changed;							  \
Signature · 1 line
1.4.4.2
#24 lore raw

PATCH 13/25 turn msr.h functions into native versions

In reply to Glauber de Oliveira Costa

This patch turns makes the basic operations in msr.h out of native ones. Those operations are: rdmsr, wrmsr, rdtsc, rdtscp, rdpmc, and cpuid. After they are turned into functions, some call sites need casts, and so we provide them.

There is also a fixup needed in the functions located in the vsyscall area, as they cannot call any of them anymore (otherwise, the call would go through a kernel address, invalid in userspace mapping).

The solution is to call the now-provided native_ versions instead.

---
 arch/x86_64/ia32/syscall32.c  |    2 +-
 arch/x86_64/kernel/setup64.c  |    6 +-
 arch/x86_64/kernel/tsc.c      |   42 ++++++-
 arch/x86_64/kernel/vsyscall.c |    4 +-
 arch/x86_64/vdso/vgetcpu.c    |    4 +-
 include/asm-x86_64/msr.h      |  284 +++++++++++++++++++++++++---------------
 6 files changed, 226 insertions(+), 116 deletions(-)
arch/x86_64/ia32/syscall32.c +1 -1
index 15013ba..dd1b4a3 100644
@@ -79,5 +79,5 @@ void syscall32_cpu_init(void)
 	checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
 	checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
 
-	wrmsrl(MSR_CSTAR, ia32_cstar_target);
+	wrmsrl(MSR_CSTAR, (u64)ia32_cstar_target);
 }
arch/x86_64/kernel/setup64.c +3 -3
index 1200aaa..395cf02 100644
@@ -122,7 +122,7 @@ void pda_init(int cpu)
 	asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0)); 
 	/* Memory clobbers used to order PDA accessed */
 	mb();
-	wrmsrl(MSR_GS_BASE, pda);
+	wrmsrl(MSR_GS_BASE, (u64)pda);
 	mb();
 
 	pda->cpunumber = cpu; 
@@ -161,8 +161,8 @@ void syscall_init(void)
 	 * but only a 32bit target. LSTAR sets the 64bit rip. 	 
 	 */ 
 	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
-	wrmsrl(MSR_LSTAR, system_call); 
-	wrmsrl(MSR_CSTAR, ignore_sysret);
+	wrmsrl(MSR_LSTAR, (u64)system_call);
+	wrmsrl(MSR_CSTAR, (u64)ignore_sysret);
 
 #ifdef CONFIG_IA32_EMULATION   		
 	syscall32_cpu_init ();
arch/x86_64/kernel/tsc.c +41 -1

54 lines not on this page · raw message

arch/x86_64/kernel/vsyscall.c +2 -2
index 06c3494..22fc4c9 100644
@@ -184,7 +184,7 @@ time_t __vsyscall(1) vtime(time_t *t)
 long __vsyscall(2)
 vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 {
-	unsigned int dummy, p;
+	unsigned int p;
 	unsigned long j = 0;
 
 	/* Fast cache - only recompute value once per jiffies and avoid
@@ -199,7 +199,7 @@ vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 		p = tcache->blob[1];
 	} else if (__vgetcpu_mode == VGETCPU_RDTSCP) {
 		/* Load per CPU data from RDTSCP */
-		rdtscp(dummy, dummy, p);
+		native_read_tscp(&p);
 	} else {
 		/* Load per CPU data from GDT */
 		asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
arch/x86_64/vdso/vgetcpu.c +2 -2
index 91f6e85..61d0def 100644
@@ -15,7 +15,7 @@
 
 long __vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 {
-	unsigned int dummy, p;
+	unsigned int p;
 	unsigned long j = 0;
 
 	/* Fast cache - only recompute value once per jiffies and avoid
@@ -30,7 +30,7 @@ long __vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 		p = tcache->blob[1];
 	} else if (*vdso_vgetcpu_mode == VGETCPU_RDTSCP) {
 		/* Load per CPU data from RDTSCP */
-		rdtscp(dummy, dummy, p);
+		native_read_tscp(&p);
 	} else {
 		/* Load per CPU data from GDT */
 		asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
include/asm-x86_64/msr.h +177 -107

329 lines not on this page · raw message

Signature · 1 line
1.4.4.2
#26 lore raw

PATCH 14/25 add native functions for descriptors handling

In reply to Glauber de Oliveira Costa

This patch turns the basic descriptor handling into native_ functions. It is basically write_idt, load_idt, write_gdt, load_gdt, set_ldt, store_tr, load_tls, and the ones for updating a single entry.

In the process of doing that, we change the definition of load_LDT_nolock, and caller sites have to be patched. We also patch call sites that now needs a typecast.

---
 arch/x86_64/kernel/head64.c      |    2 +-
 arch/x86_64/kernel/ldt.c         |    6 +-
 arch/x86_64/kernel/reboot.c      |    3 +-
 arch/x86_64/kernel/setup64.c     |    4 +-
 arch/x86_64/kernel/suspend.c     |   11 ++-
 include/asm-x86_64/desc.h        |  183 +++++++++++++++++++++++++++----------
 include/asm-x86_64/mmu_context.h |    4 +-
 7 files changed, 148 insertions(+), 65 deletions(-)
arch/x86_64/kernel/head64.c +1 -1
index 6c34bdd..a0d05d7 100644
@@ -70,7 +70,7 @@ void __init x86_64_start_kernel(char * real_mode_data)
 
 	for (i = 0; i < IDT_ENTRIES; i++)
 		set_intr_gate(i, early_idt_handler);
-	asm volatile("lidt %0" :: "m" (idt_descr));
+	load_idt(&idt_descr);
 
 	early_printk("Kernel alive\n");
 
arch/x86_64/kernel/ldt.c +2 -4
index bc9ffd5..8e6fcc1 100644
@@ -173,7 +173,7 @@ static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
 {
 	struct task_struct *me = current;
 	struct mm_struct * mm = me->mm;
-	__u32 entry_1, entry_2, *lp;
+	__u32 entry_1, entry_2;
 	int error;
 	struct user_desc ldt_info;
 
@@ -202,7 +202,6 @@ static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
 			goto out_unlock;
 	}
 
-	lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
 
    	/* Allow LDTs to be cleared by the user. */
    	if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
@@ -220,8 +219,7 @@ static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
 
 	/* Install the new entry ...  */
 install:
-	*lp	= entry_1;
-	*(lp+1)	= entry_2;
+	write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1, entry_2);
 	error = 0;
 
 out_unlock:
arch/x86_64/kernel/reboot.c +2 -1
index 368db2b..ebc242c 100644
@@ -11,6 +11,7 @@
 #include <linux/sched.h>
 #include <asm/io.h>
 #include <asm/delay.h>
+#include <asm/desc.h>
 #include <asm/hw_irq.h>
 #include <asm/system.h>
 #include <asm/pgtable.h>
@@ -136,7 +137,7 @@ void machine_emergency_restart(void)
 		}
 
 		case BOOT_TRIPLE: 
-			__asm__ __volatile__("lidt (%0)": :"r" (&no_idt));
+			load_idt((struct desc_ptr *)&no_idt);
 			__asm__ __volatile__("int3");
 
 			reboot_type = BOOT_KBD;
arch/x86_64/kernel/setup64.c +2 -2
index 395cf02..49f7342 100644
@@ -224,8 +224,8 @@ void __cpuinit cpu_init (void)
  		memcpy(cpu_gdt(cpu), cpu_gdt_table, GDT_SIZE);
 
 	cpu_gdt_descr[cpu].size = GDT_SIZE;
-	asm volatile("lgdt %0" :: "m" (cpu_gdt_descr[cpu]));
-	asm volatile("lidt %0" :: "m" (idt_descr));
+	load_gdt(&cpu_gdt_descr[cpu]);
+	load_idt(&idt_descr);
 
 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
 	syscall_init();
arch/x86_64/kernel/suspend.c +6 -5
index 573c0a6..24055b6 100644
@@ -32,9 +32,9 @@ void __save_processor_state(struct saved_context *ctxt)
 	/*
 	 * descriptor tables
 	 */
-	asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
-	asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
-	asm volatile ("str %0"  : "=m" (ctxt->tr));
+	store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
+	store_idt((struct desc_ptr *)&ctxt->idt_limit);
+	store_tr(ctxt->tr);
 
 	/* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
 	/*
@@ -91,8 +91,9 @@ void __restore_processor_state(struct saved_context *ctxt)
 	 * now restore the descriptor tables to their proper values
 	 * ltr is done i fix_processor_context().
 	 */
-	asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
-	asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
+	load_gdt((struct desc_ptr *)&ctxt->gdt_limit);
+	load_idt((struct desc_ptr *)&ctxt->idt_limit);
+
 
 	/*
 	 * segment registers
include/asm-x86_64/desc.h +133 -50

228 lines not on this page · raw message

include/asm-x86_64/mmu_context.h +2 -2
index 0cce83a..c8cdc1e 100644
@@ -43,7 +43,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 		load_cr3(next->pgd);
 
 		if (unlikely(next->context.ldt != prev->context.ldt)) 
-			load_LDT_nolock(&next->context, cpu);
+			load_LDT_nolock(&next->context);
 	}
 #ifdef CONFIG_SMP
 	else {
@@ -56,7 +56,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 			 * to make sure to use no freed page tables.
 			 */
 			load_cr3(next->pgd);
-			load_LDT_nolock(&next->context, cpu);
+			load_LDT_nolock(&next->context);
 		}
 	}
 #endif
Signature · 1 line
1.4.4.2
#28 lore raw

PATCH 15/25 get rid of inline asm for load_cr3

In reply to Glauber de Oliveira Costa

Besides not elegant, it is now even forbidden, since it can break paravirtualized guests. load_cr3 should call write_cr3() instead.

---
 include/asm-x86_64/mmu_context.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
include/asm-x86_64/mmu_context.h +1 -1
index c8cdc1e..9592698 100644
@@ -25,7 +25,7 @@ static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
 
 static inline void load_cr3(pgd_t *pgd)
 {
-	asm volatile("movq %0,%%cr3" :: "r" (__pa(pgd)) : "memory");
+	write_cr3(__pa(pgd));
 }
 
 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, 
Signature · 1 line
1.4.4.2
#30 lore raw

PATCH 16/25 introducing paravirt_activate_mm

In reply to Glauber de Oliveira Costa

This function/macro will allow a paravirt guest to be notified we changed the current task cr3, and act upon it. It's up to them

---
 include/asm-x86_64/mmu_context.h |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)
include/asm-x86_64/mmu_context.h +14 -3
index 9592698..77ce047 100644
@@ -7,7 +7,16 @@
 #include <asm/pda.h>
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
+
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
 #include <asm-generic/mm_hooks.h>
+static inline void paravirt_activate_mm(struct mm_struct *prev,
+					struct mm_struct *next)
+{
+}
+#endif /* CONFIG_PARAVIRT */
 
 /*
  * possibly do the LDT unload here?
@@ -67,8 +76,10 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 	asm volatile("movl %0,%%fs"::"r"(0));  \
 } while(0)
 
-#define activate_mm(prev, next) \
-	switch_mm((prev),(next),NULL)
-
+#define activate_mm(prev, next) 		\
+do {						\
+	paravirt_activate_mm(prev, next);	\
+	switch_mm((prev),(next),NULL);		\
+} while (0)
 
 #endif
Signature · 1 line
1.4.4.2
#32 lore raw

PATCH 17/25 turn page operations into native versions

In reply to Glauber de Oliveira Costa

This patch turns the page operations (set and make a page table) into native_ versions. The operations itself will be later overriden by paravirt.

---
 include/asm-x86_64/page.h |   36 +++++++++++++++++++++++++++++++-----
 1 files changed, 31 insertions(+), 5 deletions(-)
include/asm-x86_64/page.h +31 -5
index 88adf1a..ec8b245 100644
@@ -64,16 +64,42 @@ typedef struct { unsigned long pgprot; } pgprot_t;
 
 extern unsigned long phys_base;
 
-#define pte_val(x)	((x).pte)
-#define pmd_val(x)	((x).pmd)
-#define pud_val(x)	((x).pud)
-#define pgd_val(x)	((x).pgd)
-#define pgprot_val(x)	((x).pgprot)
+static inline unsigned long native_pte_val(pte_t pte)
+{
+	return pte.pte;
+}
+
+static inline unsigned long native_pud_val(pud_t pud)
+{
+	return pud.pud;
+}
+
+
+static inline unsigned long native_pmd_val(pmd_t pmd)
+{
+	return pmd.pmd;
+}
+
+static inline unsigned long native_pgd_val(pgd_t pgd)
+{
+	return pgd.pgd;
+}
+
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define pte_val(x)	native_pte_val(x)
+#define pmd_val(x)	native_pmd_val(x)
+#define pud_val(x)	native_pud_val(x)
+#define pgd_val(x)	native_pgd_val(x)
 
 #define __pte(x) ((pte_t) { (x) } )
 #define __pmd(x) ((pmd_t) { (x) } )
 #define __pud(x) ((pud_t) { (x) } )
 #define __pgd(x) ((pgd_t) { (x) } )
+#endif /* CONFIG_PARAVIRT */
+
+#define pgprot_val(x)	((x).pgprot)
 #define __pgprot(x)	((pgprot_t) { (x) } )
 
 #endif /* !__ASSEMBLY__ */
Signature · 1 line
1.4.4.2
#34 lore raw

PATCH 18/25 turn priviled operations into macros in entry.S

In reply to Glauber de Oliveira Costa

With paravirt on, we cannot issue operations like swapgs, sysretq, iretq, cli, sti. So they have to be changed into macros, that will be later properly replaced for the paravirt case.

The sysretq is a little bit more complicated, and is replaced by a sequence of three instructions. It is basically because if we had already issued an swapgs, we would be with a user stack at this point. So we do all-in-one.

The clobber list follows the idea of the i386 version closely, and represents which registers are safe to modify at the point the function is called.

---
 arch/x86_64/kernel/entry.S |  125 ++++++++++++++++++++++++++++---------------
 1 files changed, 81 insertions(+), 44 deletions(-)
arch/x86_64/kernel/entry.S +81 -44
index 1d232e5..48e953b 100644
@@ -51,8 +51,31 @@
 #include <asm/page.h>
 #include <asm/irqflags.h>
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define ENABLE_INTERRUPTS(x)	sti
+#define DISABLE_INTERRUPTS(x)	cli
+#define INTERRUPT_RETURN	iretq
+#define SWAPGS			swapgs
+#define SYSRETQ						\
+			movq	%gs:pda_oldrsp,%rsp;	\
+			swapgs;				\
+			sysretq;
+#endif
+
 	.code64
 
+/* Currently paravirt can't handle swapgs nicely when we
+ * don't have a stack.  So we either find a way around these
+ * or just fault and emulate if a guest tries to call swapgs
+ * directly.
+ *
+ * Either way, this is a good way to document that we don't
+ * have a reliable stack.
+ */
+#define SWAPGS_NOSTACK		swapgs
+
 #ifndef CONFIG_PREEMPT
 #define retint_kernel retint_restore_args
 #endif	
@@ -216,14 +239,14 @@ ENTRY(system_call)
 	CFI_DEF_CFA	rsp,PDA_STACKOFFSET
 	CFI_REGISTER	rip,rcx
 	/*CFI_REGISTER	rflags,r11*/
-	swapgs
+	SWAPGS_NOSTACK
 	movq	%rsp,%gs:pda_oldrsp 
 	movq	%gs:pda_kernelstack,%rsp
 	/*
 	 * No need to follow this irqs off/on section - it's straight
 	 * and short:
 	 */
-	sti					
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	SAVE_ARGS 8,1
 	movq  %rax,ORIG_RAX-ARGOFFSET(%rsp) 
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
@@ -245,7 +268,7 @@ ret_from_sys_call:
 	/* edi:	flagmask */
 sysret_check:		
 	GET_THREAD_INFO(%rcx)
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	movl threadinfo_flags(%rcx),%edx
 	andl %edi,%edx
@@ -259,9 +282,7 @@ sysret_check:
 	CFI_REGISTER	rip,rcx
 	RESTORE_ARGS 0,-ARG_SKIP,1
 	/*CFI_REGISTER	rflags,r11*/
-	movq	%gs:pda_oldrsp,%rsp
-	swapgs
-	sysretq
+	SYSRETQ
 
 	CFI_RESTORE_STATE
 	/* Handle reschedules */
@@ -270,7 +291,7 @@ sysret_careful:
 	bt $TIF_NEED_RESCHED,%edx
 	jnc sysret_signal
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushq %rdi
 	CFI_ADJUST_CFA_OFFSET 8
 	call schedule
@@ -281,7 +302,7 @@ sysret_careful:
 	/* Handle a signal */ 
 sysret_signal:
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
 	jz    1f
 
@@ -294,7 +315,7 @@ sysret_signal:
 1:	movl $_TIF_NEED_RESCHED,%edi
 	/* Use IRET because user could have changed frame. This
 	   works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp int_with_check
 	
@@ -326,7 +347,7 @@ tracesys:
  */
 	.globl int_ret_from_sys_call
 int_ret_from_sys_call:
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_OFF
 	testl $3,CS-ARGOFFSET(%rsp)
 	je retint_restore_args
@@ -347,20 +368,20 @@ int_careful:
 	bt $TIF_NEED_RESCHED,%edx
 	jnc  int_very_careful
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushq %rdi
 	CFI_ADJUST_CFA_OFFSET 8
 	call schedule
 	popq %rdi
 	CFI_ADJUST_CFA_OFFSET -8
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp int_with_check
 
 	/* handle signals and tracing -- both require a full stack frame */
 int_very_careful:
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	SAVE_REST
 	/* Check for syscall exit trace */	
 	testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP),%edx
@@ -383,7 +404,7 @@ int_signal:
 1:	movl $_TIF_NEED_RESCHED,%edi	
 int_restore_rest:
 	RESTORE_REST
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp int_with_check
 	CFI_ENDPROC
@@ -504,7 +525,7 @@ END(stub_rt_sigreturn)
 	CFI_DEF_CFA_REGISTER	rbp
 	testl $3,CS(%rdi)
 	je 1f
-	swapgs	
+	SWAPGS
 	/* irqcount is used to check if a CPU is already on an interrupt
 	   stack or not. While this is essentially redundant with preempt_count
 	   it is a little cheaper to use a separate counter in the PDA
@@ -525,7 +546,7 @@ ENTRY(common_interrupt)
 	interrupt do_IRQ
 	/* 0(%rsp): oldrsp-ARGOFFSET */
 ret_from_intr:
-	cli	
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	decl %gs:pda_irqcount
 	leaveq
@@ -552,13 +573,13 @@ retint_swapgs:
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_IRETQ
-	swapgs 
+	SWAPGS
 	jmp restore_args
 
 retint_restore_args:				
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
 	RESTORE_ARGS 0,8,0						
 iret_label:	
-	iretq
+#ifdef CONFIG_PARAVIRT
+	INTERRUPT_RETURN
+ENTRY(native_iret)
+#endif
+1:	iretq
 
 	.section __ex_table,"a"
-	.quad iret_label,bad_iret	
+	.quad 1b, bad_iret
 	.previous
 	.section .fixup,"ax"
 	/* force a signal here? this matches i386 behaviour */
@@ -577,24 +602,27 @@ iret_label:
 bad_iret:
 	movq $11,%rdi	/* SIGSEGV */
 	TRACE_IRQS_ON
-	sti
-	jmp do_exit			
-	.previous	
-	
+	ENABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
+	jmp do_exit
+	.previous
+#ifdef CONFIG_PARAVIRT
+ENDPROC(native_iret)
+#endif
+
 	/* edi: workmask, edx: work */
 retint_careful:
 	CFI_RESTORE_STATE
 	bt    $TIF_NEED_RESCHED,%edx
 	jnc   retint_signal
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushq %rdi
 	CFI_ADJUST_CFA_OFFSET	8
 	call  schedule
 	popq %rdi		
 	CFI_ADJUST_CFA_OFFSET	-8
 	GET_THREAD_INFO(%rcx)
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp retint_check
 	
@@ -602,14 +630,14 @@ retint_signal:
 	testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
 	jz    retint_swapgs
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	SAVE_REST
 	movq $-1,ORIG_RAX(%rsp) 			
 	xorl %esi,%esi		# oldset
 	movq %rsp,%rdi		# &pt_regs
 	call do_notify_resume
 	RESTORE_REST
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	movl $_TIF_NEED_RESCHED,%edi
 	GET_THREAD_INFO(%rcx)
@@ -727,7 +755,7 @@ END(spurious_interrupt)
 	rdmsr
 	testl %edx,%edx
 	js    1f
-	swapgs
+	SWAPGS
 	xorl  %ebx,%ebx
 1:
 	.if \ist
@@ -743,7 +771,7 @@ END(spurious_interrupt)
 	.if \ist
 	addq	$EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
 	.endif
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	.if \irqtrace
 	TRACE_IRQS_OFF
 	.endif
@@ -772,10 +800,10 @@ paranoid_swapgs\trace:
 	.if \trace
 	TRACE_IRQS_IRETQ 0
 	.endif
-	swapgs
+	SWAPGS_NOSTACK
 paranoid_restore\trace:
 	RESTORE_ALL 8
-	iretq
+	INTERRUPT_RETURN
 paranoid_userspace\trace:
 	GET_THREAD_INFO(%rcx)
 	movl threadinfo_flags(%rcx),%ebx
@@ -790,11 +818,11 @@ paranoid_userspace\trace:
 	.if \trace
 	TRACE_IRQS_ON
 	.endif
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	xorl %esi,%esi 			/* arg2: oldset */
 	movq %rsp,%rdi 			/* arg1: &pt_regs */
 	call do_notify_resume
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	.if \trace
 	TRACE_IRQS_OFF
 	.endif
@@ -803,9 +831,9 @@ paranoid_schedule\trace:
 	.if \trace
 	TRACE_IRQS_ON
 	.endif
-	sti
+	ENABLE_INTERRUPTS(CLBR_ANY)
 	call schedule
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	.if \trace
 	TRACE_IRQS_OFF
 	.endif
@@ -858,7 +886,7 @@ KPROBE_ENTRY(error_entry)
 	testl $3,CS(%rsp)
 	je  error_kernelspace
 error_swapgs:	
-	swapgs
+	SWAPGS
 error_sti:	
 	movq %rdi,RDI(%rsp) 	
 	CFI_REL_OFFSET	rdi,RDI
@@ -870,7 +898,7 @@ error_sti:
 error_exit:		
 	movl %ebx,%eax		
 	RESTORE_REST
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	GET_THREAD_INFO(%rcx)	
 	testl %eax,%eax
@@ -883,7 +911,7 @@ error_exit:
 	 * The iret might restore flags:
 	 */
 	TRACE_IRQS_IRETQ
-	swapgs 
+	SWAPGS
 	RESTORE_ARGS 0,8,0						
 	jmp iret_label
 	CFI_ENDPROC
@@ -912,12 +940,12 @@ ENTRY(load_gs_index)
 	CFI_STARTPROC
 	pushf
 	CFI_ADJUST_CFA_OFFSET 8
-	cli
-        swapgs
+	DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
+        SWAPGS
 gs_change:     
         movl %edi,%gs   
 2:	mfence		/* workaround */
-	swapgs
+	SWAPGS
         popf
 	CFI_ADJUST_CFA_OFFSET -8
         ret
@@ -931,7 +959,7 @@ ENDPROC(load_gs_index)
         .section .fixup,"ax"
 	/* running with kernelgs */
 bad_gs: 
-	swapgs			/* switch back to user gs */
+	SWAPGS			/* switch back to user gs */
 	xorl %eax,%eax
         movl %eax,%gs
         jmp  2b
@@ -1072,6 +1100,15 @@ KPROBE_ENTRY(int3)
  	CFI_ENDPROC
 KPROBE_END(int3)
 
+#ifdef CONFIG_PARAVIRT
+ENTRY(native_sysret)
+	movq	%gs:pda_oldrsp,%rsp
+	swapgs
+	sysretq
+ENDPROC(native_sysret)
+
+#endif /* CONFIG_PARAVIRT */
+
 ENTRY(overflow)
 	zeroentry do_overflow
 END(overflow)
Signature · 1 line
1.4.4.2
#36 lore raw

PATCH 19/25 time-related functions paravirt provisions

In reply to Glauber de Oliveira Costa

This patch add provisions for time related functions so they can be later replaced by paravirt versions.

it basically encloses {g,s}et_wallclock inside the already existent functions update_persistent_clock and read_persistent_clock, and defines {s,g}et_wallclock to the core of such functions.

The timer interrupt setup also have to be replaced. The job is done by time_init_hook().

---
 arch/x86_64/kernel/time.c |   37 +++++++++++++++++++++++++------------
 1 files changed, 25 insertions(+), 12 deletions(-)
arch/x86_64/kernel/time.c +25 -12
index 6d48a4e..29fcd91 100644
@@ -42,6 +42,7 @@
 #include <asm/sections.h>
 #include <linux/hpet.h>
 #include <asm/apic.h>
+#include <asm/time.h>
 #include <asm/hpet.h>
 #include <asm/mpspec.h>
 #include <asm/nmi.h>
@@ -82,18 +83,12 @@ EXPORT_SYMBOL(profile_pc);
  * sheet for details.
  */
 
-static int set_rtc_mmss(unsigned long nowtime)
+int do_set_rtc_mmss(unsigned long nowtime)
 {
 	int retval = 0;
 	int real_seconds, real_minutes, cmos_minutes;
 	unsigned char control, freq_select;
 
-/*
- * IRQs are disabled when we're called from the timer interrupt,
- * no need for spin_lock_irqsave()
- */
-
-	spin_lock(&rtc_lock);
 
 /*
  * Tell the clock it's being set and stop it.
@@ -143,14 +138,22 @@ static int set_rtc_mmss(unsigned long nowtime)
 	CMOS_WRITE(control, RTC_CONTROL);
 	CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
 
-	spin_unlock(&rtc_lock);
-
 	return retval;
 }
 
 int update_persistent_clock(struct timespec now)
 {
-	return set_rtc_mmss(now.tv_sec);
+	int retval;
+
+/*
+ * IRQs are disabled when we're called from the timer interrupt,
+ * no need for spin_lock_irqsave()
+ */
+	spin_lock(&rtc_lock);
+	retval = set_wallclock(now.tv_sec);
+	spin_unlock(&rtc_lock);
+
+	return retval;
 }
 
 void main_timer_handler(void)
@@ -195,7 +198,7 @@ static irqreturn_t timer_interrupt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-unsigned long read_persistent_clock(void)
+unsigned long do_get_cmos_time(void)
 {
 	unsigned int year, mon, day, hour, min, sec;
 	unsigned long flags;
@@ -246,6 +249,11 @@ unsigned long read_persistent_clock(void)
 	return mktime(year, mon, day, hour, min, sec);
 }
 
+unsigned long read_persistent_clock(void)
+{
+	return get_wallclock();
+}
+
 /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  * processor frequency */
 #define TICK_COUNT 100000000
@@ -365,6 +373,11 @@ static struct irqaction irq0 = {
 	.name		= "timer"
 };
 
+inline void time_init_hook()
+{
+	setup_irq(0, &irq0);
+}
+
 void __init time_init(void)
 {
 	if (nohpet)
@@ -403,7 +416,7 @@ void __init time_init(void)
 		cpu_khz / 1000, cpu_khz % 1000);
 	init_tsc_clocksource();
 
-	setup_irq(0, &irq0);
+	do_time_init();
 }
 
 /*
Signature · 1 line
1.4.4.2
#38 lore raw

PATCH 20/25 replace syscall_init

In reply to Glauber de Oliveira Costa

This patch replaces syscall_init by x86_64_syscall_init. The former will be later replaced by a paravirt replacement in case paravirt is on

---
 arch/x86_64/kernel/setup64.c |    8 +++++++-
 include/asm-x86_64/proto.h   |    3 +++
 2 files changed, 10 insertions(+), 1 deletions(-)
arch/x86_64/kernel/setup64.c +7 -1
index 49f7342..723822c 100644
@@ -153,7 +153,7 @@ __attribute__((section(".bss.page_aligned")));
 extern asmlinkage void ignore_sysret(void);
 
 /* May not be marked __init: used by software suspend */
-void syscall_init(void)
+void x86_64_syscall_init(void)
 {
 	/* 
 	 * LSTAR and STAR live in a bit strange symbiosis.
@@ -172,6 +172,12 @@ void syscall_init(void)
 	wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000); 
 }
 
+/* Overriden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) syscall_init(void)
+{
+	x86_64_syscall_init();
+}
+
 void __cpuinit check_efer(void)
 {
 	unsigned long efer;
include/asm-x86_64/proto.h +3 -0
index 31f20ad..77ed2de 100644
@@ -18,6 +18,9 @@ extern void init_memory_mapping(unsigned long start, unsigned long end);
 
 extern void system_call(void); 
 extern int kernel_syscall(void);
+#ifdef CONFIG_PARAVIRT
+extern void x86_64_syscall_init(void);
+#endif
 extern void syscall_init(void);
 
 extern void ia32_syscall(void);
Signature · 1 line
1.4.4.2
#40 lore raw

PATCH 21/25 export cpu_gdt_descr

In reply to Glauber de Oliveira Costa

With paravirualization, hypervisors needs to handle the gdt, that was right to this point only used at very early inialization code. Hypervisors are commonly modules, so make it an export

---
 arch/x86_64/kernel/x8664_ksyms.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86_64/kernel/x8664_ksyms.c +6 -0
index 77c25b3..8f10698 100644
@@ -60,3 +60,9 @@ EXPORT_SYMBOL(init_level4_pgt);
 EXPORT_SYMBOL(load_gs_index);
 
 EXPORT_SYMBOL(_proxy_pda);
+
+#ifdef CONFIG_PARAVIRT
+extern unsigned long *cpu_gdt_descr;
+/* Virtualized guests may want to use it */
+EXPORT_SYMBOL(cpu_gdt_descr);
+#endif
Signature · 1 line
1.4.4.2
#42 lore raw

PATCH 22/25 turn priviled operation into a macro

In reply to Glauber de Oliveira Costa

under paravirt, read cr2 cannot be issued directly anymore. So wrap it in a macro, defined to the operation itself in case paravirt is off, but to something else if we have paravirt in the game

---
 arch/x86_64/kernel/head.S |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
arch/x86_64/kernel/head.S +9 -1
index e89abcd..1bb6c55 100644
@@ -18,6 +18,12 @@
 #include <asm/page.h>
 #include <asm/msr.h>
 #include <asm/cache.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/asm-offsets.h>
+#include <asm/paravirt.h>
+#else
+#define GET_CR2_INTO_RCX mov %cr2, %rcx
+#endif
 
 /* we are not able to switch in one step to the final KERNEL ADRESS SPACE
  * because we need identity-mapped pages.
@@ -267,7 +273,9 @@ ENTRY(early_idt_handler)
 	xorl %eax,%eax
 	movq 8(%rsp),%rsi	# get rip
 	movq (%rsp),%rdx
-	movq %cr2,%rcx
+	/* When PARAVIRT is on, this operation may clobber rax. It is
+	  something safe to do, because we've just zeroed rax. */
+	GET_CR2_INTO_RCX
 	leaq early_idt_msg(%rip),%rdi
 	call early_printk
 	cmpl $2,early_recursion_flag(%rip)
Signature · 1 line
1.4.4.2
#44 lore raw

PATCH 23/25 paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa

This patch add paravirtualization hooks in the arch initialization process. paravirt_arch_setup() lets the guest issue any specific initialization routine, and skip all the rest if it see fits, which it signals by a proper return value.

In case the initialization continues, we hook at least memory_setup(), so it can handle it in its own way.

The hypervisor can make its own ebda mapping visible by providing its custom ebda_info function.

---
 arch/x86_64/kernel/setup.c |   41 ++++++++++++++++++++++++++++++++++++-----
 include/asm-x86_64/e820.h  |    6 ++++++
 2 files changed, 42 insertions(+), 5 deletions(-)
arch/x86_64/kernel/setup.c +36 -5
index af838f6..8e58a5d 100644
@@ -65,6 +65,12 @@
 #include <asm/sections.h>
 #include <asm/dmi.h>
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define paravirt_arch_setup() 0
+#endif
+
 /*
  * Machine setup..
  */
@@ -201,17 +207,28 @@ static inline void copy_edd(void)
 unsigned __initdata ebda_addr;
 unsigned __initdata ebda_size;
 
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)
 {
 	/*
 	 * there is a real-mode segmented pointer pointing to the 
 	 * 4K EBDA area at 0x40E
 	 */
-	ebda_addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);
-	ebda_addr <<= 4;
+	*addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);
+	*addr <<= 4;
+
+	*size = *(unsigned short *)__va(*addr);
+}
 
-	ebda_size = *(unsigned short *)__va(ebda_addr);
+/* Overriden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) ebda_info(unsigned *addr, unsigned *size)
+{
+	native_ebda_info(addr, size);
+}
 
+static void discover_ebda(void)
+{
+
+	ebda_info(&ebda_addr, &ebda_size);
 	/* Round EBDA up to pages */
 	if (ebda_size == 0)
 		ebda_size = 1;
@@ -221,6 +238,13 @@ static void discover_ebda(void)
 		ebda_size = 64*1024;
 }
 
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
 	saved_video_mode = SAVED_VIDEO_MODE;
 	bootloader_type = LOADER_TYPE;
 
+	/*
+	 * By returning non-zero here, a paravirt impl can choose to
+	 * skip the rest of the setup process
+	 */
+	if (paravirt_arch_setup())
+		return;
+
 #ifdef CONFIG_BLK_DEV_RAM
 	rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
 	rd_prompt = ((RAMDISK_FLAGS & RAMDISK_PROMPT_FLAG) != 0);
 	rd_doload = ((RAMDISK_FLAGS & RAMDISK_LOAD_FLAG) != 0);
 #endif
-	setup_memory_region();
+	memory_setup();
 	copy_edd();
 
 	if (!MOUNT_ROOT_RDONLY)
include/asm-x86_64/e820.h +6 -0
index 3486e70..2ced3ba 100644
@@ -20,7 +20,12 @@
 #define E820_ACPI	3
 #define E820_NVS	4
 
+#define MAP_TYPE_STR	"BIOS-e820"
+
 #ifndef __ASSEMBLY__
+
+void native_ebda_info(unsigned *addr, unsigned *size);
+
 struct e820entry {
 	u64 addr;	/* start of memory segment */
 	u64 size;	/* size of memory segment */
@@ -56,6 +61,7 @@ extern struct e820map e820;
 
 extern unsigned ebda_addr, ebda_size;
 extern unsigned long nodemap_addr, nodemap_size;
+
 #endif/*!__ASSEMBLY__*/
 
 #endif/*__E820_HEADER*/
Signature · 1 line
1.4.4.2
#46 lore raw

PATCH 24/25 provide paravirt patching function

In reply to Glauber de Oliveira Costa

This patch introduces apply_paravirt(), a function that shall be called by i386/alternative.c to apply replacements to paravirt_functions. It is defined to an do-nothing function if paravirt is not enabled.

---
 include/asm-x86_64/alternative.h |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
include/asm-x86_64/alternative.h +5 -3
index ab161e8..e69a141 100644
@@ -143,12 +143,14 @@ static inline void alternatives_smp_switch(int smp) {}
  */
 #define ASM_OUTPUT2(a, b) a, b
 
-struct paravirt_patch;
+struct paravirt_patch_site;
 #ifdef CONFIG_PARAVIRT
-void apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end);
+void apply_paravirt(struct paravirt_patch_site *start,
+				struct paravirt_patch_site *end);
 #else
 static inline void
-apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end)
+apply_paravirt(struct paravirt_patch_site *start,
+				struct paravirt_patch_site *end)
 {}
 #define __parainstructions NULL
 #define __parainstructions_end NULL
Signature · 1 line
1.4.4.2
#48 lore raw

PATCH 25/25 add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa

This is finally, the patch we were all looking for. This patch adds a paravirt.h header with the definition of paravirt_ops struct. Also, it defines a bunch of inline functions that will replace, or hook, the other calls. Every one of those functions adds an entry in the parainstructions section (see vmlinux.lds.S). Those entries can then be used to runtime-patch the paravirt_ops functions.

paravirt.c contains implementations of paravirt functions that are used natively, such as the native_patch. It also fill the paravirt_ops structure with the whole lot of functions that were (re)defined throughout this patch set.

There are also changes in asm-offsets.c. paravirt.h needs it to find out the offsets into the structure of functions such as irq_enable, used in assembly files.

The text in Kconfig is the same as i386 one.

---
 arch/x86_64/Kconfig              |   11 +
 arch/x86_64/kernel/Makefile      |    1 +
 arch/x86_64/kernel/asm-offsets.c |   14 +
 arch/x86_64/kernel/paravirt.c    |  455 +++++++++++++++++++
 arch/x86_64/kernel/vmlinux.lds.S |    6 +
 include/asm-x86_64/paravirt.h    |  901 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 1388 insertions(+), 0 deletions(-)
arch/x86_64/Kconfig +11 -0
index ffa0364..bfea34c 100644
@@ -373,6 +373,17 @@ config NODES_SHIFT
 
 # Dummy CONFIG option to select ACPI_NUMA from drivers/acpi/Kconfig.
 
+config PARAVIRT
+       bool "Paravirtualization support (EXPERIMENTAL)"
+       depends on EXPERIMENTAL
+       help
+         Paravirtualization is a way of running multiple instances of
+         Linux on the same machine, under a hypervisor.  This option
+         changes the kernel so it can modify itself when it is run
+         under a hypervisor, improving performance significantly.
+         However, when run without a hypervisor the kernel is
+         theoretically slower.  If in doubt, say N.
+
 config X86_64_ACPI_NUMA
        bool "ACPI NUMA detection"
        depends on NUMA
arch/x86_64/kernel/Makefile +1 -0
index ff5d8c9..120467f 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_X86_VSMP)		+= vsmp.o
 obj-$(CONFIG_K8_NB)		+= k8.o
 obj-$(CONFIG_AUDIT)		+= audit.o
 
+obj-$(CONFIG_PARAVIRT)		+= paravirt.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_PCI)		+= early-quirks.o
 
arch/x86_64/kernel/asm-offsets.c +14 -0
index 778953b..a8ffc95 100644
@@ -15,6 +15,9 @@
 #include <asm/segment.h>
 #include <asm/thread_info.h>
 #include <asm/ia32.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
 
 #define DEFINE(sym, val) \
         asm volatile("\n->" #sym " %0 " #val : : "i" (val))
@@ -72,6 +75,17 @@ int main(void)
 	       offsetof (struct rt_sigframe32, uc.uc_mcontext));
 	BLANK();
 #endif
+#ifdef CONFIG_PARAVIRT
+#define ENTRY(entry) DEFINE(PARAVIRT_ ## entry, offsetof(struct paravirt_ops, entry))
+	ENTRY(paravirt_enabled);
+	ENTRY(irq_disable);
+	ENTRY(irq_enable);
+	ENTRY(sysret);
+	ENTRY(iret);
+	ENTRY(read_cr2);
+	ENTRY(swapgs);
+	BLANK();
+#endif
 	DEFINE(pbe_address, offsetof(struct pbe, address));
 	DEFINE(pbe_orig_address, offsetof(struct pbe, orig_address));
 	DEFINE(pbe_next, offsetof(struct pbe, next));
arch/x86_64/kernel/paravirt.c new file +455 -0

455 lines not on this page · raw message

arch/x86_64/kernel/vmlinux.lds.S +6 -0
index ba8ea97..c3fce85 100644
@@ -185,6 +185,12 @@ SECTIONS
   .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
 	*(.altinstr_replacement)
   }
+  . = ALIGN(8);
+  .parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
+  __parainstructions = .;
+	*(.parainstructions)
+  __parainstructions_end = .;
+  }
   /* .exit.text is discard at runtime, not link time, to deal with references
      from .altinstructions and .eh_frame */
   .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) { *(.exit.text) }
include/asm-x86_64/paravirt.h new file +901 -0

901 lines not on this page · raw message

Signature · 1 line
1.4.4.2
#5 lore raw

PATCH 3/25 irq_flags / halt routines

In reply to Glauber de Oliveira Costa

This patch turns the irq_flags and halt routines into the native versions.

---
 include/asm-x86_64/irqflags.h |   32 +++++++++++++++++++++++++++-----
 1 files changed, 27 insertions(+), 5 deletions(-)
include/asm-x86_64/irqflags.h +27 -5
index 86e70fe..4ba5241 100644
@@ -16,6 +16,22 @@
  * Interrupt control:
  */
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF);
+}
+#  endif
+
+#else /* PARAVIRT */
+
 static inline unsigned long __raw_local_save_flags(void)
 {
 	unsigned long flags;
@@ -31,9 +47,6 @@ static inline unsigned long __raw_local_save_flags(void)
 	return flags;
 }
 
-#define raw_local_save_flags(flags) \
-		do { (flags) = __raw_local_save_flags(); } while (0)
-
 static inline void raw_local_irq_restore(unsigned long flags)
 {
 	__asm__ __volatile__(
@@ -88,6 +101,10 @@ static inline int raw_irqs_disabled_flags(unsigned long flags)
 
 #endif
 
+#endif /* CONFIG_PARAVIRT */
+
+#define raw_local_save_flags(flags) \
+		do { (flags) = __raw_local_save_flags(); } while (0)
 /*
  * For spinlocks, etc.:
  */
@@ -115,7 +132,7 @@ static inline int raw_irqs_disabled(void)
  * Used in the idle loop; sti takes one instruction cycle
  * to complete:
  */
-static inline void raw_safe_halt(void)
+static inline void native_raw_safe_halt(void)
 {
 	__asm__ __volatile__("sti; hlt" : : : "memory");
 }
@@ -124,11 +141,16 @@ static inline void raw_safe_halt(void)
  * Used when interrupts are already enabled or to
  * shutdown the processor:
  */
-static inline void halt(void)
+static inline void native_halt(void)
 {
 	__asm__ __volatile__("hlt": : :"memory");
 }
 
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt	native_raw_safe_halt
+#define halt		native_halt
+#endif /* ! CONFIG_PARAVIRT */
+
 #else /* __ASSEMBLY__: */
 # ifdef CONFIG_TRACE_IRQFLAGS
 #  define TRACE_IRQS_ON		call trace_hardirqs_on_thunk
Signature · 1 line
1.4.4.2
#7 lore raw

PATCH 4/25 Add debugreg/load_rsp native hooks

This patch adds native hooks for debugreg handling functions, and for the native load_rsp0 function. The later also have its call sites patched.

---
 arch/x86_64/kernel/process.c   |    2 +-
 arch/x86_64/kernel/smpboot.c   |    2 +-
 include/asm-x86_64/processor.h |   71 ++++++++++++++++++++++++++++++++++++----
 3 files changed, 66 insertions(+), 9 deletions(-)
arch/x86_64/kernel/process.c +1 -1
index 2842f50..33046f1 100644
@@ -595,7 +595,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	/*
 	 * Reload esp0, LDT and the page table pointer:
 	 */
-	tss->rsp0 = next->rsp0;
+	load_rsp0(tss, next);
 
 	/* 
 	 * Switch DS and ES.
arch/x86_64/kernel/smpboot.c +1 -1
index 32f5078..be9c7eb 100644
@@ -620,7 +620,7 @@ do_rest:
 	start_rip = setup_trampoline();
 
 	init_rsp = c_idle.idle->thread.rsp;
-	per_cpu(init_tss,cpu).rsp0 = init_rsp;
+ 	load_rsp0(&per_cpu(init_tss,cpu), &c_idle.idle->thread);
 	initial_code = start_secondary;
 	clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
 
include/asm-x86_64/processor.h +64 -7

83 lines not on this page · raw message

Signature · 1 line
1.4.4.2
#3 lore raw

PATCH 2/25 tlb flushing routines

In reply to Glauber de Oliveira Costa

This patch turns the flush_tlb routines into native versions. In case paravirt is not defined, the natives are defined into the actually used ones. flush_tlb_others() goes in smp.c, unless we smp is not in the game

---
 arch/x86_64/kernel/smp.c      |   10 +++++++++-
 include/asm-x86_64/smp.h      |    8 ++++++++
 include/asm-x86_64/tlbflush.h |   22 ++++++++++++++++++----
 3 files changed, 35 insertions(+), 5 deletions(-)
arch/x86_64/kernel/smp.c +9 -1
index 673a300..39f5f6b 100644
@@ -165,7 +165,7 @@ out:
 	cpu_clear(cpu, f->flush_cpumask);
 }
 
-static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
+void native_flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
 						unsigned long va)
 {
 	int sender;
@@ -198,6 +198,14 @@ static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
 	spin_unlock(&f->tlbstate_lock);
 }
 
+/* Overriden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) flush_tlb_others(cpumask_t cpumask,
+					    struct mm_struct *mm,
+					    unsigned long va)
+{
+	native_flush_tlb_others(cpumask, mm, va);
+}
+
 int __cpuinit init_smp_flush(void)
 {
 	int i;
include/asm-x86_64/smp.h +8 -0
index 3f303d2..6b11114 100644
@@ -19,6 +19,14 @@ extern int disable_apic;
 
 #include <asm/pda.h>
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+void native_flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
+						unsigned long va);
+#else
+#define startup_ipi_hook(apicid, rip, rsp) do { } while (0)
+#endif
+
 struct pt_regs;
 
 extern cpumask_t cpu_present_mask;
include/asm-x86_64/tlbflush.h +18 -4
index 888eb4a..1c68cc8 100644
@@ -6,21 +6,30 @@
 #include <asm/processor.h>
 #include <asm/system.h>
 
-static inline void __flush_tlb(void)
+static inline void native_flush_tlb(void)
 {
 	write_cr3(read_cr3());
 }
 
-static inline void __flush_tlb_all(void)
+static inline void native_flush_tlb_all(void)
 {
 	unsigned long cr4 = read_cr4();
 	write_cr4(cr4 & ~X86_CR4_PGE);	/* clear PGE */
 	write_cr4(cr4);			/* write old PGE again and flush TLBs */
 }
 
-#define __flush_tlb_one(addr) \
-	__asm__ __volatile__("invlpg (%0)" :: "r" (addr) : "memory")
+static inline void native_flush_tlb_one(unsigned long addr)
+{
+	asm volatile ("invlpg (%0)" :: "r" (addr) : "memory");
+}
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define __flush_tlb()		native_flush_tlb()
+#define __flush_tlb_all()	native_flush_tlb_all()
+#define __flush_tlb_one(addr)	native_flush_tlb_one(addr)
+#endif	/* CONFIG_PARAVIRT */
 
 /*
  * TLB flushing:
@@ -64,6 +73,11 @@ static inline void flush_tlb_range(struct vm_area_struct *vma,
 		__flush_tlb();
 }
 
+static inline void native_flush_tlb_others(cpumask_t *cpumask,
+					   struct mm_struct *mm, unsigned long va)
+{
+}
+
 #else
 
 #include <asm/smp.h>
Signature · 1 line
1.4.4.2
#9 lore raw

PATCH 5/25 native versions for system.h functions

In reply to Glauber de Oliveira Costa

This patch adds the native hook for the functions in system.h They are the read/write_crX, clts and wbinvd. The later, also gets its call sites patched.

---
 arch/x86_64/kernel/tce.c  |    2 +-
 arch/x86_64/mm/pageattr.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86_64/kernel/tce.c +1 -1
index e3f2569..587f0c2 100644
@@ -42,7 +42,7 @@ static inline void flush_tce(void* tceaddr)
 	if (cpu_has_clflush)
 		asm volatile("clflush (%0)" :: "r" (tceaddr));
 	else
-		asm volatile("wbinvd":::"memory");
+		wbinvd();
 }
 
 void tce_build(struct iommu_table *tbl, unsigned long index,
arch/x86_64/mm/pageattr.c +1 -1
index 7e161c6..b497afd 100644
@@ -76,7 +76,7 @@ static void flush_kernel_map(void *arg)
 	/* When clflush is available always use it because it is
 	   much cheaper than WBINVD. */
 	if (!cpu_has_clflush)
-		asm volatile("wbinvd" ::: "memory");
+		wbinvd();
 	else list_for_each_entry(pg, l, lru) {
 		void *adr = page_address(pg);
 		cache_flush_page(adr);
Signature · 1 line
1.4.4.2
#11 lore raw

PATCH 6/25 add native_apic read and write functions, as well as boot clocks ones

Time for the apic handling functions to get their native counterparts. Also, put the native hook for the boot clocks functions in the apic.h header

---
 arch/x86_64/kernel/apic.c    |    2 +-
 arch/x86_64/kernel/smpboot.c |    8 +++++++-
 include/asm-x86_64/apic.h    |   13 +++++++++++--
 3 files changed, 19 insertions(+), 4 deletions(-)
arch/x86_64/kernel/apic.c +1 -1
index 900ff38..2d233ef 100644
@@ -1193,7 +1193,7 @@ int __init APIC_init_uniprocessor (void)
 		setup_IO_APIC();
 	else
 		nr_ioapics = 0;
-	setup_boot_APIC_clock();
+	setup_boot_clock();
 	check_nmi_watchdog();
 	return 0;
 }
arch/x86_64/kernel/smpboot.c +7 -1
index be9c7eb..5c7eb60 100644
@@ -338,7 +338,7 @@ void __cpuinit start_secondary(void)
 	check_tsc_sync_target();
 
 	Dprintk("cpu %d: setting up apic clock\n", smp_processor_id()); 	
-	setup_secondary_APIC_clock();
+ 	setup_secondary_clock();
 
 	Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
 
@@ -468,6 +468,12 @@ static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int sta
 	num_starts = 2;
 
 	/*
+	 * Paravirt wants a startup IPI hook here to set up the
+	 * target processor state.
+	 */
+	startup_ipi_hook(phys_apicid, (unsigned long) start_rip,
+		         (unsigned long) init_rsp);
+	/*
 	 * Run STARTUP IPI loop.
 	 */
 	Dprintk("#startup loops: %d.\n", num_starts);
include/asm-x86_64/apic.h +11 -2
index 85125ef..de17908 100644
@@ -38,16 +38,25 @@ struct pt_regs;
  * Basic functions accessing APICs.
  */
 
-static __inline void apic_write(unsigned long reg, unsigned int v)
+static __inline void native_apic_write(unsigned long reg, unsigned int v)
 {
 	*((volatile unsigned int *)(APIC_BASE+reg)) = v;
 }
 
-static __inline unsigned int apic_read(unsigned long reg)
+static __inline unsigned int native_apic_read(unsigned long reg)
 {
 	return *((volatile unsigned int *)(APIC_BASE+reg));
 }
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define apic_write(reg, v) native_apic_write(reg, v)
+#define apic_read(reg)  native_apic_read(reg)
+#define setup_boot_clock(void)	setup_boot_APIC_clock(void)
+#define setup_secondary_clock(void) setup_secondary_APIC_clock(void)
+#endif
+
 extern void apic_wait_icr_idle(void);
 extern unsigned int safe_apic_wait_icr_idle(void);
 
Signature · 1 line
1.4.4.2
#15 lore raw

PATCH 8/25 use macro for sti/cli in spinlock definitions

In reply to Glauber de Oliveira Costa

This patch switches the cli and sti instructions into macros. In this header, they're just defined to the instructions they refer to. Later on, when paravirt is defined, they will be defined to something with paravirt abilities.

---
 include/asm-x86_64/spinlock.h |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)
include/asm-x86_64/spinlock.h +13 -3
index 88bf981..a81e27a 100644
@@ -5,6 +5,14 @@
 #include <asm/rwlock.h>
 #include <asm/page.h>
 #include <asm/processor.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define CLI_STI_INPUT_ARGS
+#define CLI_STI_CLOBBERS
+#define CLI_STRING "cli"
+#define STI_STRING "sti"
+#endif
 
 /*
  * Your basic SMP spinlocks, allowing only a single CPU anywhere
@@ -48,12 +56,12 @@ static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long fla
 		"jns 5f\n"
 		"testl $0x200, %1\n\t"	/* interrupts were disabled? */
 		"jz 4f\n\t"
-	        "sti\n"
+		STI_STRING "\n\t"
 		"3:\t"
 		"rep;nop\n\t"
 		"cmpl $0, %0\n\t"
 		"jle 3b\n\t"
-		"cli\n\t"
+		CLI_STRING "\n\t"
 		"jmp 1b\n"
 		"4:\t"
 		"rep;nop\n\t"
@@ -61,7 +69,9 @@ static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long fla
 		"jg 1b\n\t"
 		"jmp 4b\n"
 		"5:\n\t"
-		: "+m" (lock->slock) : "r" ((unsigned)flags) : "memory");
+		: "+m" (lock->slock)
+		: "r" ((unsigned)flags), CLI_STI_INPUT_ARGS
+		: "memory", CLI_STI_CLOBBERS);
 }
 #endif
 
Signature · 1 line
1.4.4.2
#31 lore raw

PATCH 16/25 introducing paravirt_activate_mm

In reply to Glauber de Oliveira Costa

This function/macro will allow a paravirt guest to be notified we changed the current task cr3, and act upon it. It's up to them

---
 include/asm-x86_64/mmu_context.h |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)
include/asm-x86_64/mmu_context.h +14 -3
index 9592698..77ce047 100644
@@ -7,7 +7,16 @@
 #include <asm/pda.h>
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
+
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
 #include <asm-generic/mm_hooks.h>
+static inline void paravirt_activate_mm(struct mm_struct *prev,
+					struct mm_struct *next)
+{
+}
+#endif /* CONFIG_PARAVIRT */
 
 /*
  * possibly do the LDT unload here?
@@ -67,8 +76,10 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 	asm volatile("movl %0,%%fs"::"r"(0));  \
 } while(0)
 
-#define activate_mm(prev, next) \
-	switch_mm((prev),(next),NULL)
-
+#define activate_mm(prev, next) 		\
+do {						\
+	paravirt_activate_mm(prev, next);	\
+	switch_mm((prev),(next),NULL);		\
+} while (0)
 
 #endif
Signature · 1 line
1.4.4.2
#19 lore raw

PATCH 10/25 export math_state_restore

In reply to Glauber de Oliveira Costa

Export math_state_restore symbol, so it can be used for hypervisors. They are commonly loaded as modules.

---
 arch/x86_64/kernel/traps.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86_64/kernel/traps.c +1 -0
index 0388842..aacbe12 100644
@@ -1081,6 +1081,7 @@ asmlinkage void math_state_restore(void)
 	task_thread_info(me)->status |= TS_USEDFPU;
 	me->fpu_counter++;
 }
+EXPORT_SYMBOL_GPL(math_state_restore);
 
 void __init trap_init(void)
 {
Signature · 1 line
1.4.4.2
#37 lore raw

PATCH 19/25 time-related functions paravirt provisions

In reply to Glauber de Oliveira Costa

This patch add provisions for time related functions so they can be later replaced by paravirt versions.

it basically encloses {g,s}et_wallclock inside the already existent functions update_persistent_clock and read_persistent_clock, and defines {s,g}et_wallclock to the core of such functions.

The timer interrupt setup also have to be replaced. The job is done by time_init_hook().

---
 arch/x86_64/kernel/time.c |   37 +++++++++++++++++++++++++------------
 1 files changed, 25 insertions(+), 12 deletions(-)
arch/x86_64/kernel/time.c +25 -12
index 6d48a4e..29fcd91 100644
@@ -42,6 +42,7 @@
 #include <asm/sections.h>
 #include <linux/hpet.h>
 #include <asm/apic.h>
+#include <asm/time.h>
 #include <asm/hpet.h>
 #include <asm/mpspec.h>
 #include <asm/nmi.h>
@@ -82,18 +83,12 @@ EXPORT_SYMBOL(profile_pc);
  * sheet for details.
  */
 
-static int set_rtc_mmss(unsigned long nowtime)
+int do_set_rtc_mmss(unsigned long nowtime)
 {
 	int retval = 0;
 	int real_seconds, real_minutes, cmos_minutes;
 	unsigned char control, freq_select;
 
-/*
- * IRQs are disabled when we're called from the timer interrupt,
- * no need for spin_lock_irqsave()
- */
-
-	spin_lock(&rtc_lock);
 
 /*
  * Tell the clock it's being set and stop it.
@@ -143,14 +138,22 @@ static int set_rtc_mmss(unsigned long nowtime)
 	CMOS_WRITE(control, RTC_CONTROL);
 	CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
 
-	spin_unlock(&rtc_lock);
-
 	return retval;
 }
 
 int update_persistent_clock(struct timespec now)
 {
-	return set_rtc_mmss(now.tv_sec);
+	int retval;
+
+/*
+ * IRQs are disabled when we're called from the timer interrupt,
+ * no need for spin_lock_irqsave()
+ */
+	spin_lock(&rtc_lock);
+	retval = set_wallclock(now.tv_sec);
+	spin_unlock(&rtc_lock);
+
+	return retval;
 }
 
 void main_timer_handler(void)
@@ -195,7 +198,7 @@ static irqreturn_t timer_interrupt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-unsigned long read_persistent_clock(void)
+unsigned long do_get_cmos_time(void)
 {
 	unsigned int year, mon, day, hour, min, sec;
 	unsigned long flags;
@@ -246,6 +249,11 @@ unsigned long read_persistent_clock(void)
 	return mktime(year, mon, day, hour, min, sec);
 }
 
+unsigned long read_persistent_clock(void)
+{
+	return get_wallclock();
+}
+
 /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  * processor frequency */
 #define TICK_COUNT 100000000
@@ -365,6 +373,11 @@ static struct irqaction irq0 = {
 	.name		= "timer"
 };
 
+inline void time_init_hook()
+{
+	setup_irq(0, &irq0);
+}
+
 void __init time_init(void)
 {
 	if (nohpet)
@@ -403,7 +416,7 @@ void __init time_init(void)
 		cpu_khz / 1000, cpu_khz % 1000);
 	init_tsc_clocksource();
 
-	setup_irq(0, &irq0);
+	do_time_init();
 }
 
 /*
Signature · 1 line
1.4.4.2
#39 lore raw

PATCH 20/25 replace syscall_init

This patch replaces syscall_init by x86_64_syscall_init. The former will be later replaced by a paravirt replacement in case paravirt is on

---
 arch/x86_64/kernel/setup64.c |    8 +++++++-
 include/asm-x86_64/proto.h   |    3 +++
 2 files changed, 10 insertions(+), 1 deletions(-)
arch/x86_64/kernel/setup64.c +7 -1
index 49f7342..723822c 100644
@@ -153,7 +153,7 @@ __attribute__((section(".bss.page_aligned")));
 extern asmlinkage void ignore_sysret(void);
 
 /* May not be marked __init: used by software suspend */
-void syscall_init(void)
+void x86_64_syscall_init(void)
 {
 	/* 
 	 * LSTAR and STAR live in a bit strange symbiosis.
@@ -172,6 +172,12 @@ void syscall_init(void)
 	wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE|0x3000); 
 }
 
+/* Overriden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) syscall_init(void)
+{
+	x86_64_syscall_init();
+}
+
 void __cpuinit check_efer(void)
 {
 	unsigned long efer;
include/asm-x86_64/proto.h +3 -0
index 31f20ad..77ed2de 100644
@@ -18,6 +18,9 @@ extern void init_memory_mapping(unsigned long start, unsigned long end);
 
 extern void system_call(void); 
 extern int kernel_syscall(void);
+#ifdef CONFIG_PARAVIRT
+extern void x86_64_syscall_init(void);
+#endif
 extern void syscall_init(void);
 
 extern void ia32_syscall(void);
Signature · 1 line
1.4.4.2
#35 lore raw

PATCH 18/25 turn priviled operations into macros in entry.S

In reply to Glauber de Oliveira Costa

With paravirt on, we cannot issue operations like swapgs, sysretq, iretq, cli, sti. So they have to be changed into macros, that will be later properly replaced for the paravirt case.

The sysretq is a little bit more complicated, and is replaced by a sequence of three instructions. It is basically because if we had already issued an swapgs, we would be with a user stack at this point. So we do all-in-one.

The clobber list follows the idea of the i386 version closely, and represents which registers are safe to modify at the point the function is called.

---
 arch/x86_64/kernel/entry.S |  125 ++++++++++++++++++++++++++++---------------
 1 files changed, 81 insertions(+), 44 deletions(-)
arch/x86_64/kernel/entry.S +81 -44
index 1d232e5..48e953b 100644
@@ -51,8 +51,31 @@
 #include <asm/page.h>
 #include <asm/irqflags.h>
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define ENABLE_INTERRUPTS(x)	sti
+#define DISABLE_INTERRUPTS(x)	cli
+#define INTERRUPT_RETURN	iretq
+#define SWAPGS			swapgs
+#define SYSRETQ						\
+			movq	%gs:pda_oldrsp,%rsp;	\
+			swapgs;				\
+			sysretq;
+#endif
+
 	.code64
 
+/* Currently paravirt can't handle swapgs nicely when we
+ * don't have a stack.  So we either find a way around these
+ * or just fault and emulate if a guest tries to call swapgs
+ * directly.
+ *
+ * Either way, this is a good way to document that we don't
+ * have a reliable stack.
+ */
+#define SWAPGS_NOSTACK		swapgs
+
 #ifndef CONFIG_PREEMPT
 #define retint_kernel retint_restore_args
 #endif	
@@ -216,14 +239,14 @@ ENTRY(system_call)
 	CFI_DEF_CFA	rsp,PDA_STACKOFFSET
 	CFI_REGISTER	rip,rcx
 	/*CFI_REGISTER	rflags,r11*/
-	swapgs
+	SWAPGS_NOSTACK
 	movq	%rsp,%gs:pda_oldrsp 
 	movq	%gs:pda_kernelstack,%rsp
 	/*
 	 * No need to follow this irqs off/on section - it's straight
 	 * and short:
 	 */
-	sti					
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	SAVE_ARGS 8,1
 	movq  %rax,ORIG_RAX-ARGOFFSET(%rsp) 
 	movq  %rcx,RIP-ARGOFFSET(%rsp)
@@ -245,7 +268,7 @@ ret_from_sys_call:
 	/* edi:	flagmask */
 sysret_check:		
 	GET_THREAD_INFO(%rcx)
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	movl threadinfo_flags(%rcx),%edx
 	andl %edi,%edx
@@ -259,9 +282,7 @@ sysret_check:
 	CFI_REGISTER	rip,rcx
 	RESTORE_ARGS 0,-ARG_SKIP,1
 	/*CFI_REGISTER	rflags,r11*/
-	movq	%gs:pda_oldrsp,%rsp
-	swapgs
-	sysretq
+	SYSRETQ
 
 	CFI_RESTORE_STATE
 	/* Handle reschedules */
@@ -270,7 +291,7 @@ sysret_careful:
 	bt $TIF_NEED_RESCHED,%edx
 	jnc sysret_signal
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushq %rdi
 	CFI_ADJUST_CFA_OFFSET 8
 	call schedule
@@ -281,7 +302,7 @@ sysret_careful:
 	/* Handle a signal */ 
 sysret_signal:
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
 	jz    1f
 
@@ -294,7 +315,7 @@ sysret_signal:
 1:	movl $_TIF_NEED_RESCHED,%edi
 	/* Use IRET because user could have changed frame. This
 	   works because ptregscall_common has called FIXUP_TOP_OF_STACK. */
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp int_with_check
 	
@@ -326,7 +347,7 @@ tracesys:
  */
 	.globl int_ret_from_sys_call
 int_ret_from_sys_call:
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_OFF
 	testl $3,CS-ARGOFFSET(%rsp)
 	je retint_restore_args
@@ -347,20 +368,20 @@ int_careful:
 	bt $TIF_NEED_RESCHED,%edx
 	jnc  int_very_careful
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushq %rdi
 	CFI_ADJUST_CFA_OFFSET 8
 	call schedule
 	popq %rdi
 	CFI_ADJUST_CFA_OFFSET -8
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp int_with_check
 
 	/* handle signals and tracing -- both require a full stack frame */
 int_very_careful:
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	SAVE_REST
 	/* Check for syscall exit trace */	
 	testl $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP),%edx
@@ -383,7 +404,7 @@ int_signal:
 1:	movl $_TIF_NEED_RESCHED,%edi	
 int_restore_rest:
 	RESTORE_REST
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp int_with_check
 	CFI_ENDPROC
@@ -504,7 +525,7 @@ END(stub_rt_sigreturn)
 	CFI_DEF_CFA_REGISTER	rbp
 	testl $3,CS(%rdi)
 	je 1f
-	swapgs	
+	SWAPGS
 	/* irqcount is used to check if a CPU is already on an interrupt
 	   stack or not. While this is essentially redundant with preempt_count
 	   it is a little cheaper to use a separate counter in the PDA
@@ -525,7 +546,7 @@ ENTRY(common_interrupt)
 	interrupt do_IRQ
 	/* 0(%rsp): oldrsp-ARGOFFSET */
 ret_from_intr:
-	cli	
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	decl %gs:pda_irqcount
 	leaveq
@@ -552,13 +573,13 @@ retint_swapgs:
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	TRACE_IRQS_IRETQ
-	swapgs 
+	SWAPGS
 	jmp restore_args
 
 retint_restore_args:				
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
 	RESTORE_ARGS 0,8,0						
 iret_label:	
-	iretq
+#ifdef CONFIG_PARAVIRT
+	INTERRUPT_RETURN
+ENTRY(native_iret)
+#endif
+1:	iretq
 
 	.section __ex_table,"a"
-	.quad iret_label,bad_iret	
+	.quad 1b, bad_iret
 	.previous
 	.section .fixup,"ax"
 	/* force a signal here? this matches i386 behaviour */
@@ -577,24 +602,27 @@ iret_label:
 bad_iret:
 	movq $11,%rdi	/* SIGSEGV */
 	TRACE_IRQS_ON
-	sti
-	jmp do_exit			
-	.previous	
-	
+	ENABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
+	jmp do_exit
+	.previous
+#ifdef CONFIG_PARAVIRT
+ENDPROC(native_iret)
+#endif
+
 	/* edi: workmask, edx: work */
 retint_careful:
 	CFI_RESTORE_STATE
 	bt    $TIF_NEED_RESCHED,%edx
 	jnc   retint_signal
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	pushq %rdi
 	CFI_ADJUST_CFA_OFFSET	8
 	call  schedule
 	popq %rdi		
 	CFI_ADJUST_CFA_OFFSET	-8
 	GET_THREAD_INFO(%rcx)
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	jmp retint_check
 	
@@ -602,14 +630,14 @@ retint_signal:
 	testl $(_TIF_SIGPENDING|_TIF_SINGLESTEP|_TIF_MCE_NOTIFY),%edx
 	jz    retint_swapgs
 	TRACE_IRQS_ON
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	SAVE_REST
 	movq $-1,ORIG_RAX(%rsp) 			
 	xorl %esi,%esi		# oldset
 	movq %rsp,%rdi		# &pt_regs
 	call do_notify_resume
 	RESTORE_REST
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	movl $_TIF_NEED_RESCHED,%edi
 	GET_THREAD_INFO(%rcx)
@@ -727,7 +755,7 @@ END(spurious_interrupt)
 	rdmsr
 	testl %edx,%edx
 	js    1f
-	swapgs
+	SWAPGS
 	xorl  %ebx,%ebx
 1:
 	.if \ist
@@ -743,7 +771,7 @@ END(spurious_interrupt)
 	.if \ist
 	addq	$EXCEPTION_STKSZ, per_cpu__init_tss + TSS_ist + (\ist - 1) * 8(%rbp)
 	.endif
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	.if \irqtrace
 	TRACE_IRQS_OFF
 	.endif
@@ -772,10 +800,10 @@ paranoid_swapgs\trace:
 	.if \trace
 	TRACE_IRQS_IRETQ 0
 	.endif
-	swapgs
+	SWAPGS_NOSTACK
 paranoid_restore\trace:
 	RESTORE_ALL 8
-	iretq
+	INTERRUPT_RETURN
 paranoid_userspace\trace:
 	GET_THREAD_INFO(%rcx)
 	movl threadinfo_flags(%rcx),%ebx
@@ -790,11 +818,11 @@ paranoid_userspace\trace:
 	.if \trace
 	TRACE_IRQS_ON
 	.endif
-	sti
+	ENABLE_INTERRUPTS(CLBR_NONE)
 	xorl %esi,%esi 			/* arg2: oldset */
 	movq %rsp,%rdi 			/* arg1: &pt_regs */
 	call do_notify_resume
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	.if \trace
 	TRACE_IRQS_OFF
 	.endif
@@ -803,9 +831,9 @@ paranoid_schedule\trace:
 	.if \trace
 	TRACE_IRQS_ON
 	.endif
-	sti
+	ENABLE_INTERRUPTS(CLBR_ANY)
 	call schedule
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)
 	.if \trace
 	TRACE_IRQS_OFF
 	.endif
@@ -858,7 +886,7 @@ KPROBE_ENTRY(error_entry)
 	testl $3,CS(%rsp)
 	je  error_kernelspace
 error_swapgs:	
-	swapgs
+	SWAPGS
 error_sti:	
 	movq %rdi,RDI(%rsp) 	
 	CFI_REL_OFFSET	rdi,RDI
@@ -870,7 +898,7 @@ error_sti:
 error_exit:		
 	movl %ebx,%eax		
 	RESTORE_REST
-	cli
+	DISABLE_INTERRUPTS(CLBR_NONE)
 	TRACE_IRQS_OFF
 	GET_THREAD_INFO(%rcx)	
 	testl %eax,%eax
@@ -883,7 +911,7 @@ error_exit:
 	 * The iret might restore flags:
 	 */
 	TRACE_IRQS_IRETQ
-	swapgs 
+	SWAPGS
 	RESTORE_ARGS 0,8,0						
 	jmp iret_label
 	CFI_ENDPROC
@@ -912,12 +940,12 @@ ENTRY(load_gs_index)
 	CFI_STARTPROC
 	pushf
 	CFI_ADJUST_CFA_OFFSET 8
-	cli
-        swapgs
+	DISABLE_INTERRUPTS(CLBR_ANY | ~(CLBR_RDI))
+        SWAPGS
 gs_change:     
         movl %edi,%gs   
 2:	mfence		/* workaround */
-	swapgs
+	SWAPGS
         popf
 	CFI_ADJUST_CFA_OFFSET -8
         ret
@@ -931,7 +959,7 @@ ENDPROC(load_gs_index)
         .section .fixup,"ax"
 	/* running with kernelgs */
 bad_gs: 
-	swapgs			/* switch back to user gs */
+	SWAPGS			/* switch back to user gs */
 	xorl %eax,%eax
         movl %eax,%gs
         jmp  2b
@@ -1072,6 +1100,15 @@ KPROBE_ENTRY(int3)
  	CFI_ENDPROC
 KPROBE_END(int3)
 
+#ifdef CONFIG_PARAVIRT
+ENTRY(native_sysret)
+	movq	%gs:pda_oldrsp,%rsp
+	swapgs
+	sysretq
+ENDPROC(native_sysret)
+
+#endif /* CONFIG_PARAVIRT */
+
 ENTRY(overflow)
 	zeroentry do_overflow
 END(overflow)
Signature · 1 line
1.4.4.2
#41 lore raw

PATCH 21/25 export cpu_gdt_descr

In reply to Glauber de Oliveira Costa

With paravirualization, hypervisors needs to handle the gdt, that was right to this point only used at very early inialization code. Hypervisors are commonly modules, so make it an export

---
 arch/x86_64/kernel/x8664_ksyms.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86_64/kernel/x8664_ksyms.c +6 -0
index 77c25b3..8f10698 100644
@@ -60,3 +60,9 @@ EXPORT_SYMBOL(init_level4_pgt);
 EXPORT_SYMBOL(load_gs_index);
 
 EXPORT_SYMBOL(_proxy_pda);
+
+#ifdef CONFIG_PARAVIRT
+extern unsigned long *cpu_gdt_descr;
+/* Virtualized guests may want to use it */
+EXPORT_SYMBOL(cpu_gdt_descr);
+#endif
Signature · 1 line
1.4.4.2
#13 lore raw

PATCH 7/25 interrupt related native paravirt functions.

In reply to Glauber de Oliveira Costa

The interrupt initialization routine becomes native_init_IRQ and will be overriden later in case paravirt is on.

The interrupt vector is made global, so paravirt guests can reference it in their initializations. However, "interrupt" is such a common name, and could lead to clashes, so it is renamed.

---
 arch/x86_64/kernel/i8259.c |   15 +++++++++++----
 include/asm-x86_64/irq.h   |    2 ++
 2 files changed, 13 insertions(+), 4 deletions(-)
arch/x86_64/kernel/i8259.c +11 -4
index 948cae6..8dda872 100644
@@ -75,8 +75,12 @@ BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd) BUILD_16_IRQS(0xe) BUILD_16_IRQS(0xf)
 	IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
 	IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
 
-/* for the irq vectors */
-static void (*interrupt[NR_VECTORS - FIRST_EXTERNAL_VECTOR])(void) = {
+/*
+ * For the irq vectors. It is global rather than static to allow for
+ * paravirtualized guests to use it in their own interrupt initialization
+ * routines
+ */
+void (*interrupt_vector[NR_VECTORS - FIRST_EXTERNAL_VECTOR])(void) = {
 					  IRQLIST_16(0x2), IRQLIST_16(0x3),
 	IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
 	IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
@@ -484,7 +488,10 @@ static int __init init_timer_sysfs(void)
 
 device_initcall(init_timer_sysfs);
 
-void __init init_IRQ(void)
+/* Overridden in paravirt.c */
+void init_IRQ(void) __attribute__((weak, alias("native_init_IRQ")));
+
+void __init native_init_IRQ(void)
 {
 	int i;
 
@@ -497,7 +504,7 @@ void __init init_IRQ(void)
 	for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
 		int vector = FIRST_EXTERNAL_VECTOR + i;
 		if (vector != IA32_SYSCALL_VECTOR)
-			set_intr_gate(vector, interrupt[i]);
+			set_intr_gate(vector, interrupt_vector[i]);
 	}
 
 #ifdef CONFIG_SMP
include/asm-x86_64/irq.h +2 -0
index 5006c6e..be55299 100644
@@ -46,6 +46,8 @@ static __inline__ int irq_canonicalize(int irq)
 extern void fixup_irqs(cpumask_t map);
 #endif
 
+void native_init_IRQ(void);
+
 #define __ARCH_HAS_DO_SOFTIRQ 1
 
 #endif /* _ASM_IRQ_H */
Signature · 1 line
1.4.4.2
#43 lore raw

PATCH 22/25 turn priviled operation into a macro

In reply to Glauber de Oliveira Costa

under paravirt, read cr2 cannot be issued directly anymore. So wrap it in a macro, defined to the operation itself in case paravirt is off, but to something else if we have paravirt in the game

---
 arch/x86_64/kernel/head.S |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
arch/x86_64/kernel/head.S +9 -1
index e89abcd..1bb6c55 100644
@@ -18,6 +18,12 @@
 #include <asm/page.h>
 #include <asm/msr.h>
 #include <asm/cache.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/asm-offsets.h>
+#include <asm/paravirt.h>
+#else
+#define GET_CR2_INTO_RCX mov %cr2, %rcx
+#endif
 
 /* we are not able to switch in one step to the final KERNEL ADRESS SPACE
  * because we need identity-mapped pages.
@@ -267,7 +273,9 @@ ENTRY(early_idt_handler)
 	xorl %eax,%eax
 	movq 8(%rsp),%rsi	# get rip
 	movq (%rsp),%rdx
-	movq %cr2,%rcx
+	/* When PARAVIRT is on, this operation may clobber rax. It is
+	  something safe to do, because we've just zeroed rax. */
+	GET_CR2_INTO_RCX
 	leaq early_idt_msg(%rip),%rdi
 	call early_printk
 	cmpl $2,early_recursion_flag(%rip)
Signature · 1 line
1.4.4.2
#17 lore raw

PATCH 9/25 report ring kernel is running without paravirt

In reply to Glauber de Oliveira Costa

When paravirtualization is disabled, the kernel is always running at ring 0. So report it in the appropriate macro

---
 include/asm-x86_64/segment.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/asm-x86_64/segment.h +4 -0
index 04b8ab2..240c1bf 100644
@@ -50,4 +50,8 @@
 #define GDT_SIZE (GDT_ENTRIES * 8)
 #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8) 
 
+#ifndef CONFIG_PARAVIRT
+#define get_kernel_rpl()  0
+#endif
+
 #endif
Signature · 1 line
1.4.4.2
#21 lore raw

PATCH 11/25 introduce paravirt_release_pgd()

In reply to Glauber de Oliveira Costa

This patch introduces a new macro/function that informs a paravirt guest when its page table is not more in use, and can be released. In case we're not paravirt, just do nothing.

---
 include/asm-x86_64/pgalloc.h |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
include/asm-x86_64/pgalloc.h +7 -0
index b467be6..dbe1267 100644
@@ -9,6 +9,12 @@
 #define QUICK_PGD 0	/* We preserve special mappings over free */
 #define QUICK_PT 1	/* Other page table pages that are zero on free */
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define paravirt_release_pgd(pgd) do { } while (0)
+#endif
+
 #define pmd_populate_kernel(mm, pmd, pte) \
 		set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte)))
 #define pud_populate(mm, pud, pmd) \
@@ -100,6 +106,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
 static inline void pgd_free(pgd_t *pgd)
 {
 	BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
+	paravirt_release_pgd(pgd);
 	quicklist_free(QUICK_PGD, pgd_dtor, pgd);
 }
 
Signature · 1 line
1.4.4.2
#47 lore raw

PATCH 24/25 provide paravirt patching function

In reply to Glauber de Oliveira Costa

This patch introduces apply_paravirt(), a function that shall be called by i386/alternative.c to apply replacements to paravirt_functions. It is defined to an do-nothing function if paravirt is not enabled.

---
 include/asm-x86_64/alternative.h |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
include/asm-x86_64/alternative.h +5 -3
index ab161e8..e69a141 100644
@@ -143,12 +143,14 @@ static inline void alternatives_smp_switch(int smp) {}
  */
 #define ASM_OUTPUT2(a, b) a, b
 
-struct paravirt_patch;
+struct paravirt_patch_site;
 #ifdef CONFIG_PARAVIRT
-void apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end);
+void apply_paravirt(struct paravirt_patch_site *start,
+				struct paravirt_patch_site *end);
 #else
 static inline void
-apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end)
+apply_paravirt(struct paravirt_patch_site *start,
+				struct paravirt_patch_site *end)
 {}
 #define __parainstructions NULL
 #define __parainstructions_end NULL
Signature · 1 line
1.4.4.2
#49 lore raw

PATCH 25/25 add paravirtualization support for x86_64

This is finally, the patch we were all looking for. This patch adds a paravirt.h header with the definition of paravirt_ops struct. Also, it defines a bunch of inline functions that will replace, or hook, the other calls. Every one of those functions adds an entry in the parainstructions section (see vmlinux.lds.S). Those entries can then be used to runtime-patch the paravirt_ops functions.

paravirt.c contains implementations of paravirt functions that are used natively, such as the native_patch. It also fill the paravirt_ops structure with the whole lot of functions that were (re)defined throughout this patch set.

There are also changes in asm-offsets.c. paravirt.h needs it to find out the offsets into the structure of functions such as irq_enable, used in assembly files.

The text in Kconfig is the same as i386 one.

---
 arch/x86_64/Kconfig              |   11 +
 arch/x86_64/kernel/Makefile      |    1 +
 arch/x86_64/kernel/asm-offsets.c |   14 +
 arch/x86_64/kernel/paravirt.c    |  455 +++++++++++++++++++
 arch/x86_64/kernel/vmlinux.lds.S |    6 +
 include/asm-x86_64/paravirt.h    |  901 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 1388 insertions(+), 0 deletions(-)
arch/x86_64/Kconfig +11 -0
index ffa0364..bfea34c 100644
@@ -373,6 +373,17 @@ config NODES_SHIFT
 
 # Dummy CONFIG option to select ACPI_NUMA from drivers/acpi/Kconfig.
 
+config PARAVIRT
+       bool "Paravirtualization support (EXPERIMENTAL)"
+       depends on EXPERIMENTAL
+       help
+         Paravirtualization is a way of running multiple instances of
+         Linux on the same machine, under a hypervisor.  This option
+         changes the kernel so it can modify itself when it is run
+         under a hypervisor, improving performance significantly.
+         However, when run without a hypervisor the kernel is
+         theoretically slower.  If in doubt, say N.
+
 config X86_64_ACPI_NUMA
        bool "ACPI NUMA detection"
        depends on NUMA
arch/x86_64/kernel/Makefile +1 -0
index ff5d8c9..120467f 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_X86_VSMP)		+= vsmp.o
 obj-$(CONFIG_K8_NB)		+= k8.o
 obj-$(CONFIG_AUDIT)		+= audit.o
 
+obj-$(CONFIG_PARAVIRT)		+= paravirt.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_PCI)		+= early-quirks.o
 
arch/x86_64/kernel/asm-offsets.c +14 -0
index 778953b..a8ffc95 100644
@@ -15,6 +15,9 @@
 #include <asm/segment.h>
 #include <asm/thread_info.h>
 #include <asm/ia32.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
 
 #define DEFINE(sym, val) \
         asm volatile("\n->" #sym " %0 " #val : : "i" (val))
@@ -72,6 +75,17 @@ int main(void)
 	       offsetof (struct rt_sigframe32, uc.uc_mcontext));
 	BLANK();
 #endif
+#ifdef CONFIG_PARAVIRT
+#define ENTRY(entry) DEFINE(PARAVIRT_ ## entry, offsetof(struct paravirt_ops, entry))
+	ENTRY(paravirt_enabled);
+	ENTRY(irq_disable);
+	ENTRY(irq_enable);
+	ENTRY(sysret);
+	ENTRY(iret);
+	ENTRY(read_cr2);
+	ENTRY(swapgs);
+	BLANK();
+#endif
 	DEFINE(pbe_address, offsetof(struct pbe, address));
 	DEFINE(pbe_orig_address, offsetof(struct pbe, orig_address));
 	DEFINE(pbe_next, offsetof(struct pbe, next));
arch/x86_64/kernel/paravirt.c new file +455 -0

455 lines not on this page · raw message

arch/x86_64/kernel/vmlinux.lds.S +6 -0
index ba8ea97..c3fce85 100644
@@ -185,6 +185,12 @@ SECTIONS
   .altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
 	*(.altinstr_replacement)
   }
+  . = ALIGN(8);
+  .parainstructions : AT(ADDR(.parainstructions) - LOAD_OFFSET) {
+  __parainstructions = .;
+	*(.parainstructions)
+  __parainstructions_end = .;
+  }
   /* .exit.text is discard at runtime, not link time, to deal with references
      from .altinstructions and .eh_frame */
   .exit.text : AT(ADDR(.exit.text) - LOAD_OFFSET) { *(.exit.text) }
include/asm-x86_64/paravirt.h new file +901 -0

901 lines not on this page · raw message

Signature · 1 line
1.4.4.2
#27 lore raw

PATCH 14/25 add native functions for descriptors handling

In reply to Glauber de Oliveira Costa

This patch turns the basic descriptor handling into native_ functions. It is basically write_idt, load_idt, write_gdt, load_gdt, set_ldt, store_tr, load_tls, and the ones for updating a single entry.

In the process of doing that, we change the definition of load_LDT_nolock, and caller sites have to be patched. We also patch call sites that now needs a typecast.

---
 arch/x86_64/kernel/head64.c      |    2 +-
 arch/x86_64/kernel/ldt.c         |    6 +-
 arch/x86_64/kernel/reboot.c      |    3 +-
 arch/x86_64/kernel/setup64.c     |    4 +-
 arch/x86_64/kernel/suspend.c     |   11 ++-
 include/asm-x86_64/desc.h        |  183 +++++++++++++++++++++++++++----------
 include/asm-x86_64/mmu_context.h |    4 +-
 7 files changed, 148 insertions(+), 65 deletions(-)
arch/x86_64/kernel/head64.c +1 -1
index 6c34bdd..a0d05d7 100644
@@ -70,7 +70,7 @@ void __init x86_64_start_kernel(char * real_mode_data)
 
 	for (i = 0; i < IDT_ENTRIES; i++)
 		set_intr_gate(i, early_idt_handler);
-	asm volatile("lidt %0" :: "m" (idt_descr));
+	load_idt(&idt_descr);
 
 	early_printk("Kernel alive\n");
 
arch/x86_64/kernel/ldt.c +2 -4
index bc9ffd5..8e6fcc1 100644
@@ -173,7 +173,7 @@ static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
 {
 	struct task_struct *me = current;
 	struct mm_struct * mm = me->mm;
-	__u32 entry_1, entry_2, *lp;
+	__u32 entry_1, entry_2;
 	int error;
 	struct user_desc ldt_info;
 
@@ -202,7 +202,6 @@ static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
 			goto out_unlock;
 	}
 
-	lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
 
    	/* Allow LDTs to be cleared by the user. */
    	if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
@@ -220,8 +219,7 @@ static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
 
 	/* Install the new entry ...  */
 install:
-	*lp	= entry_1;
-	*(lp+1)	= entry_2;
+	write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1, entry_2);
 	error = 0;
 
 out_unlock:
arch/x86_64/kernel/reboot.c +2 -1
index 368db2b..ebc242c 100644
@@ -11,6 +11,7 @@
 #include <linux/sched.h>
 #include <asm/io.h>
 #include <asm/delay.h>
+#include <asm/desc.h>
 #include <asm/hw_irq.h>
 #include <asm/system.h>
 #include <asm/pgtable.h>
@@ -136,7 +137,7 @@ void machine_emergency_restart(void)
 		}
 
 		case BOOT_TRIPLE: 
-			__asm__ __volatile__("lidt (%0)": :"r" (&no_idt));
+			load_idt((struct desc_ptr *)&no_idt);
 			__asm__ __volatile__("int3");
 
 			reboot_type = BOOT_KBD;
arch/x86_64/kernel/setup64.c +2 -2
index 395cf02..49f7342 100644
@@ -224,8 +224,8 @@ void __cpuinit cpu_init (void)
  		memcpy(cpu_gdt(cpu), cpu_gdt_table, GDT_SIZE);
 
 	cpu_gdt_descr[cpu].size = GDT_SIZE;
-	asm volatile("lgdt %0" :: "m" (cpu_gdt_descr[cpu]));
-	asm volatile("lidt %0" :: "m" (idt_descr));
+	load_gdt(&cpu_gdt_descr[cpu]);
+	load_idt(&idt_descr);
 
 	memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
 	syscall_init();
arch/x86_64/kernel/suspend.c +6 -5
index 573c0a6..24055b6 100644
@@ -32,9 +32,9 @@ void __save_processor_state(struct saved_context *ctxt)
 	/*
 	 * descriptor tables
 	 */
-	asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
-	asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
-	asm volatile ("str %0"  : "=m" (ctxt->tr));
+	store_gdt((struct desc_ptr *)&ctxt->gdt_limit);
+	store_idt((struct desc_ptr *)&ctxt->idt_limit);
+	store_tr(ctxt->tr);
 
 	/* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
 	/*
@@ -91,8 +91,9 @@ void __restore_processor_state(struct saved_context *ctxt)
 	 * now restore the descriptor tables to their proper values
 	 * ltr is done i fix_processor_context().
 	 */
-	asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
-	asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
+	load_gdt((struct desc_ptr *)&ctxt->gdt_limit);
+	load_idt((struct desc_ptr *)&ctxt->idt_limit);
+
 
 	/*
 	 * segment registers
include/asm-x86_64/desc.h +133 -50

228 lines not on this page · raw message

include/asm-x86_64/mmu_context.h +2 -2
index 0cce83a..c8cdc1e 100644
@@ -43,7 +43,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 		load_cr3(next->pgd);
 
 		if (unlikely(next->context.ldt != prev->context.ldt)) 
-			load_LDT_nolock(&next->context, cpu);
+			load_LDT_nolock(&next->context);
 	}
 #ifdef CONFIG_SMP
 	else {
@@ -56,7 +56,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 			 * to make sure to use no freed page tables.
 			 */
 			load_cr3(next->pgd);
-			load_LDT_nolock(&next->context, cpu);
+			load_LDT_nolock(&next->context);
 		}
 	}
 #endif
Signature · 1 line
1.4.4.2
#23 lore raw

PATCH 12/25 native versions for set pagetables

In reply to Glauber de Oliveira Costa

This patch turns the set_p{te,md,ud,gd} functions into their native_ versions. There is no need to patch any caller.

Also, it adds pte_update() and pte_update_defer() calls whenever we modify a page table entry. This last part was coded to match i386 as close as possible.

Pieces of the header are moved to below the #ifdef CONFIG_PARAVIRT site, as they are users of the newly defined set_* macros.

---
 include/asm-x86_64/pgtable.h |  152 ++++++++++++++++++++++++-----------------
 1 files changed, 89 insertions(+), 63 deletions(-)
include/asm-x86_64/pgtable.h +89 -63
index c9d8764..dd572a2 100644
@@ -57,55 +57,77 @@ extern unsigned long empty_zero_page[PAGE_SIZE/sizeof(unsigned long)];
  */
 #define PTRS_PER_PTE	512
 
-#ifndef __ASSEMBLY__
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
 
-#define pte_ERROR(e) \
-	printk("%s:%d: bad pte %p(%016lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
-#define pmd_ERROR(e) \
-	printk("%s:%d: bad pmd %p(%016lx).\n", __FILE__, __LINE__, &(e), pmd_val(e))
-#define pud_ERROR(e) \
-	printk("%s:%d: bad pud %p(%016lx).\n", __FILE__, __LINE__, &(e), pud_val(e))
-#define pgd_ERROR(e) \
-	printk("%s:%d: bad pgd %p(%016lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
+#define set_pte native_set_pte
+#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
+#define set_pmd native_set_pmd
+#define set_pud native_set_pud
+#define set_pgd native_set_pgd
+#define pte_clear(mm,addr,xp)	do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
+#define pmd_clear(xp)	do { set_pmd(xp, __pmd(0)); } while (0)
+#define pud_clear native_pud_clear
+#define pgd_clear native_pgd_clear
+#define pte_update(mm, addr, ptep)              do { } while (0)
+#define pte_update_defer(mm, addr, ptep)        do { } while (0)
 
-#define pgd_none(x)	(!pgd_val(x))
-#define pud_none(x)	(!pud_val(x))
+#endif
+
+#ifndef __ASSEMBLY__
 
-static inline void set_pte(pte_t *dst, pte_t val)
+static inline void native_set_pte(pte_t *dst, pte_t val)
 {
-	pte_val(*dst) = pte_val(val);
+	dst->pte = pte_val(val);
 } 
-#define set_pte_at(mm,addr,ptep,pteval) set_pte(ptep,pteval)
 
-static inline void set_pmd(pmd_t *dst, pmd_t val)
+
+static inline void native_set_pmd(pmd_t *dst, pmd_t val)
 {
-        pmd_val(*dst) = pmd_val(val); 
+	dst->pmd = pmd_val(val);
 } 
 
-static inline void set_pud(pud_t *dst, pud_t val)
+static inline void native_set_pud(pud_t *dst, pud_t val)
 {
-	pud_val(*dst) = pud_val(val);
+	dst->pud = pud_val(val);
 }
 
-static inline void pud_clear (pud_t *pud)
+static inline void native_set_pgd(pgd_t *dst, pgd_t val)
 {
-	set_pud(pud, __pud(0));
+	dst->pgd = pgd_val(val);
 }
-
-static inline void set_pgd(pgd_t *dst, pgd_t val)
+static inline void native_pud_clear (pud_t *pud)
 {
-	pgd_val(*dst) = pgd_val(val); 
-} 
+	set_pud(pud, __pud(0));
+}
 
-static inline void pgd_clear (pgd_t * pgd)
+static inline void native_pgd_clear (pgd_t * pgd)
 {
 	set_pgd(pgd, __pgd(0));
 }
 
-#define ptep_get_and_clear(mm,addr,xp)	__pte(xchg(&(xp)->pte, 0))
+#define pte_ERROR(e) \
+	printk("%s:%d: bad pte %p(%016lx).\n", __FILE__, __LINE__, &(e), pte_val(e))
+#define pmd_ERROR(e) \
+	printk("%s:%d: bad pmd %p(%016lx).\n", __FILE__, __LINE__, &(e), pmd_val(e))
+#define pud_ERROR(e) \
+	printk("%s:%d: bad pud %p(%016lx).\n", __FILE__, __LINE__, &(e), pud_val(e))
+#define pgd_ERROR(e) \
+	printk("%s:%d: bad pgd %p(%016lx).\n", __FILE__, __LINE__, &(e), pgd_val(e))
+
+#define pgd_none(x)	(!pgd_val(x))
+#define pud_none(x)	(!pud_val(x))
 
 struct mm_struct;
 
+static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	pte_t pte = __pte(xchg(&ptep->pte, 0));
+	pte_update(mm, addr, ptep);
+	return pte;
+}
+
 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, unsigned long addr, pte_t *ptep, int full)
 {
 	pte_t pte;
@@ -245,7 +267,6 @@ static inline unsigned long pmd_bad(pmd_t pmd)
 
 #define pte_none(x)	(!pte_val(x))
 #define pte_present(x)	(pte_val(x) & (_PAGE_PRESENT | _PAGE_PROTNONE))
-#define pte_clear(mm,addr,xp)	do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
 
 #define pages_to_mb(x) ((x) >> (20-PAGE_SHIFT))	/* FIXME: is this
 						   right? */
@@ -254,11 +275,11 @@ static inline unsigned long pmd_bad(pmd_t pmd)
 
 static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
 {
-	pte_t pte;
-	pte_val(pte) = (page_nr << PAGE_SHIFT);
-	pte_val(pte) |= pgprot_val(pgprot);
-	pte_val(pte) &= __supported_pte_mask;
-	return pte;
+	unsigned long pte;
+	pte = (page_nr << PAGE_SHIFT);
+	pte |= pgprot_val(pgprot);
+	pte &= __supported_pte_mask;
+	return __pte(pte);
 }
 
 /*
@@ -282,30 +303,6 @@ static inline pte_t pte_mkwrite(pte_t pte)	{ set_pte(&pte, __pte(pte_val(pte) |
 static inline pte_t pte_mkhuge(pte_t pte)	{ set_pte(&pte, __pte(pte_val(pte) | _PAGE_PSE)); return pte; }
 static inline pte_t pte_clrhuge(pte_t pte)	{ set_pte(&pte, __pte(pte_val(pte) & ~_PAGE_PSE)); return pte; }
 
-struct vm_area_struct;
-
-static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
-{
-	if (!pte_young(*ptep))
-		return 0;
-	return test_and_clear_bit(_PAGE_BIT_ACCESSED, &ptep->pte);
-}
-
-static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
-{
-	clear_bit(_PAGE_BIT_RW, &ptep->pte);
-}
-
-/*
- * Macro to mark a page protection value as "uncacheable".
- */
-#define pgprot_noncached(prot)	(__pgprot(pgprot_val(prot) | _PAGE_PCD | _PAGE_PWT))
-
-static inline int pmd_large(pmd_t pte) { 
-	return (pmd_val(pte) & __LARGE_PTE) == __LARGE_PTE; 
-} 	
-
-
 /*
  * Conversion functions: convert a page and protection to a page entry,
  * and a page entry and page directory to the page they refer to.
@@ -339,7 +336,6 @@ static inline int pmd_large(pmd_t pte) {
 			pmd_index(address))
 #define pmd_none(x)	(!pmd_val(x))
 #define pmd_present(x)	(pmd_val(x) & _PAGE_PRESENT)
-#define pmd_clear(xp)	do { set_pmd(xp, __pmd(0)); } while (0)
 #define pfn_pmd(nr,prot) (__pmd(((nr) << PAGE_SHIFT) | pgprot_val(prot)))
 #define pmd_pfn(x)  ((pmd_val(x) & __PHYSICAL_MASK) >> PAGE_SHIFT)
 
@@ -352,14 +348,43 @@ static inline int pmd_large(pmd_t pte) {
 /* page, protection -> pte */
 #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
 #define mk_pte_huge(entry) (pte_val(entry) |= _PAGE_PRESENT | _PAGE_PSE)
- 
+
+struct vm_area_struct;
+
+#include <linux/mm.h>
+static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
+{
+	int ret = 0;
+	if (!pte_young(*ptep))
+		return 0;
+	ret = test_and_clear_bit(_PAGE_BIT_ACCESSED, &ptep->pte);
+	pte_update(vma->vm_mm, addr, ptep);
+	return ret;
+}
+
+static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
+{
+	clear_bit(_PAGE_BIT_RW, &ptep->pte);
+	pte_update(mm, addr, ptep);
+}
+
+/*
+ * Macro to mark a page protection value as "uncacheable".
+ */
+#define pgprot_noncached(prot)	(__pgprot(pgprot_val(prot) | _PAGE_PCD | _PAGE_PWT))
+
+static inline int pmd_large(pmd_t pte) {
+	return (pmd_val(pte) & __LARGE_PTE) == __LARGE_PTE;
+}
+
 /* Change flags of a PTE */
-static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
+static inline pte_t pte_modify(pte_t pte_old, pgprot_t newprot)
 { 
-	pte_val(pte) &= _PAGE_CHG_MASK;
-	pte_val(pte) |= pgprot_val(newprot);
-	pte_val(pte) &= __supported_pte_mask;
-       return pte; 
+	unsigned long pte = pte_val(pte_old);
+	pte &= _PAGE_CHG_MASK;
+	pte |= pgprot_val(newprot);
+	pte &= __supported_pte_mask;
+	return __pte(pte);
 }
 
 #define pte_index(address) \
@@ -386,6 +411,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
 	int __changed = !pte_same(*(__ptep), __entry);			  \
 	if (__changed && __dirty) {					  \
 		set_pte(__ptep, __entry);			  	  \
+		pte_update_defer((__vma)->vm_mm, (__address), (__ptep));  \
 		flush_tlb_page(__vma, __address);		  	  \
 	}								  \
 	__changed;							  \
Signature · 1 line
1.4.4.2
#29 lore raw

PATCH 15/25 get rid of inline asm for load_cr3

In reply to Glauber de Oliveira Costa

Besides not elegant, it is now even forbidden, since it can break paravirtualized guests. load_cr3 should call write_cr3() instead.

---
 include/asm-x86_64/mmu_context.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
include/asm-x86_64/mmu_context.h +1 -1
index c8cdc1e..9592698 100644
@@ -25,7 +25,7 @@ static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
 
 static inline void load_cr3(pgd_t *pgd)
 {
-	asm volatile("movq %0,%%cr3" :: "r" (__pa(pgd)) : "memory");
+	write_cr3(__pa(pgd));
 }
 
 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, 
Signature · 1 line
1.4.4.2
#25 lore raw

PATCH 13/25 turn msr.h functions into native versions

In reply to Glauber de Oliveira Costa

This patch turns makes the basic operations in msr.h out of native ones. Those operations are: rdmsr, wrmsr, rdtsc, rdtscp, rdpmc, and cpuid. After they are turned into functions, some call sites need casts, and so we provide them.

There is also a fixup needed in the functions located in the vsyscall area, as they cannot call any of them anymore (otherwise, the call would go through a kernel address, invalid in userspace mapping).

The solution is to call the now-provided native_ versions instead.

---
 arch/x86_64/ia32/syscall32.c  |    2 +-
 arch/x86_64/kernel/setup64.c  |    6 +-
 arch/x86_64/kernel/tsc.c      |   42 ++++++-
 arch/x86_64/kernel/vsyscall.c |    4 +-
 arch/x86_64/vdso/vgetcpu.c    |    4 +-
 include/asm-x86_64/msr.h      |  284 +++++++++++++++++++++++++---------------
 6 files changed, 226 insertions(+), 116 deletions(-)
arch/x86_64/ia32/syscall32.c +1 -1
index 15013ba..dd1b4a3 100644
@@ -79,5 +79,5 @@ void syscall32_cpu_init(void)
 	checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
 	checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
 
-	wrmsrl(MSR_CSTAR, ia32_cstar_target);
+	wrmsrl(MSR_CSTAR, (u64)ia32_cstar_target);
 }
arch/x86_64/kernel/setup64.c +3 -3
index 1200aaa..395cf02 100644
@@ -122,7 +122,7 @@ void pda_init(int cpu)
 	asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0)); 
 	/* Memory clobbers used to order PDA accessed */
 	mb();
-	wrmsrl(MSR_GS_BASE, pda);
+	wrmsrl(MSR_GS_BASE, (u64)pda);
 	mb();
 
 	pda->cpunumber = cpu; 
@@ -161,8 +161,8 @@ void syscall_init(void)
 	 * but only a 32bit target. LSTAR sets the 64bit rip. 	 
 	 */ 
 	wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
-	wrmsrl(MSR_LSTAR, system_call); 
-	wrmsrl(MSR_CSTAR, ignore_sysret);
+	wrmsrl(MSR_LSTAR, (u64)system_call);
+	wrmsrl(MSR_CSTAR, (u64)ignore_sysret);
 
 #ifdef CONFIG_IA32_EMULATION   		
 	syscall32_cpu_init ();
arch/x86_64/kernel/tsc.c +41 -1

54 lines not on this page · raw message

arch/x86_64/kernel/vsyscall.c +2 -2
index 06c3494..22fc4c9 100644
@@ -184,7 +184,7 @@ time_t __vsyscall(1) vtime(time_t *t)
 long __vsyscall(2)
 vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 {
-	unsigned int dummy, p;
+	unsigned int p;
 	unsigned long j = 0;
 
 	/* Fast cache - only recompute value once per jiffies and avoid
@@ -199,7 +199,7 @@ vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 		p = tcache->blob[1];
 	} else if (__vgetcpu_mode == VGETCPU_RDTSCP) {
 		/* Load per CPU data from RDTSCP */
-		rdtscp(dummy, dummy, p);
+		native_read_tscp(&p);
 	} else {
 		/* Load per CPU data from GDT */
 		asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
arch/x86_64/vdso/vgetcpu.c +2 -2
index 91f6e85..61d0def 100644
@@ -15,7 +15,7 @@
 
 long __vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 {
-	unsigned int dummy, p;
+	unsigned int p;
 	unsigned long j = 0;
 
 	/* Fast cache - only recompute value once per jiffies and avoid
@@ -30,7 +30,7 @@ long __vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
 		p = tcache->blob[1];
 	} else if (*vdso_vgetcpu_mode == VGETCPU_RDTSCP) {
 		/* Load per CPU data from RDTSCP */
-		rdtscp(dummy, dummy, p);
+		native_read_tscp(&p);
 	} else {
 		/* Load per CPU data from GDT */
 		asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
include/asm-x86_64/msr.h +177 -107

329 lines not on this page · raw message

Signature · 1 line
1.4.4.2
#33 lore raw

PATCH 17/25 turn page operations into native versions

In reply to Glauber de Oliveira Costa

This patch turns the page operations (set and make a page table) into native_ versions. The operations itself will be later overriden by paravirt.

---
 include/asm-x86_64/page.h |   36 +++++++++++++++++++++++++++++++-----
 1 files changed, 31 insertions(+), 5 deletions(-)
include/asm-x86_64/page.h +31 -5
index 88adf1a..ec8b245 100644
@@ -64,16 +64,42 @@ typedef struct { unsigned long pgprot; } pgprot_t;
 
 extern unsigned long phys_base;
 
-#define pte_val(x)	((x).pte)
-#define pmd_val(x)	((x).pmd)
-#define pud_val(x)	((x).pud)
-#define pgd_val(x)	((x).pgd)
-#define pgprot_val(x)	((x).pgprot)
+static inline unsigned long native_pte_val(pte_t pte)
+{
+	return pte.pte;
+}
+
+static inline unsigned long native_pud_val(pud_t pud)
+{
+	return pud.pud;
+}
+
+
+static inline unsigned long native_pmd_val(pmd_t pmd)
+{
+	return pmd.pmd;
+}
+
+static inline unsigned long native_pgd_val(pgd_t pgd)
+{
+	return pgd.pgd;
+}
+
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define pte_val(x)	native_pte_val(x)
+#define pmd_val(x)	native_pmd_val(x)
+#define pud_val(x)	native_pud_val(x)
+#define pgd_val(x)	native_pgd_val(x)
 
 #define __pte(x) ((pte_t) { (x) } )
 #define __pmd(x) ((pmd_t) { (x) } )
 #define __pud(x) ((pud_t) { (x) } )
 #define __pgd(x) ((pgd_t) { (x) } )
+#endif /* CONFIG_PARAVIRT */
+
+#define pgprot_val(x)	((x).pgprot)
 #define __pgprot(x)	((pgprot_t) { (x) } )
 
 #endif /* !__ASSEMBLY__ */
Signature · 1 line
1.4.4.2
#45 lore raw

PATCH 23/25 paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa

This patch add paravirtualization hooks in the arch initialization process. paravirt_arch_setup() lets the guest issue any specific initialization routine, and skip all the rest if it see fits, which it signals by a proper return value.

In case the initialization continues, we hook at least memory_setup(), so it can handle it in its own way.

The hypervisor can make its own ebda mapping visible by providing its custom ebda_info function.

---
 arch/x86_64/kernel/setup.c |   41 ++++++++++++++++++++++++++++++++++++-----
 include/asm-x86_64/e820.h  |    6 ++++++
 2 files changed, 42 insertions(+), 5 deletions(-)
arch/x86_64/kernel/setup.c +36 -5
index af838f6..8e58a5d 100644
@@ -65,6 +65,12 @@
 #include <asm/sections.h>
 #include <asm/dmi.h>
 
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#else
+#define paravirt_arch_setup() 0
+#endif
+
 /*
  * Machine setup..
  */
@@ -201,17 +207,28 @@ static inline void copy_edd(void)
 unsigned __initdata ebda_addr;
 unsigned __initdata ebda_size;
 
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)
 {
 	/*
 	 * there is a real-mode segmented pointer pointing to the 
 	 * 4K EBDA area at 0x40E
 	 */
-	ebda_addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);
-	ebda_addr <<= 4;
+	*addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);
+	*addr <<= 4;
+
+	*size = *(unsigned short *)__va(*addr);
+}
 
-	ebda_size = *(unsigned short *)__va(ebda_addr);
+/* Overriden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) ebda_info(unsigned *addr, unsigned *size)
+{
+	native_ebda_info(addr, size);
+}
 
+static void discover_ebda(void)
+{
+
+	ebda_info(&ebda_addr, &ebda_size);
 	/* Round EBDA up to pages */
 	if (ebda_size == 0)
 		ebda_size = 1;
@@ -221,6 +238,13 @@ static void discover_ebda(void)
 		ebda_size = 64*1024;
 }
 
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
 	saved_video_mode = SAVED_VIDEO_MODE;
 	bootloader_type = LOADER_TYPE;
 
+	/*
+	 * By returning non-zero here, a paravirt impl can choose to
+	 * skip the rest of the setup process
+	 */
+	if (paravirt_arch_setup())
+		return;
+
 #ifdef CONFIG_BLK_DEV_RAM
 	rd_image_start = RAMDISK_FLAGS & RAMDISK_IMAGE_START_MASK;
 	rd_prompt = ((RAMDISK_FLAGS & RAMDISK_PROMPT_FLAG) != 0);
 	rd_doload = ((RAMDISK_FLAGS & RAMDISK_LOAD_FLAG) != 0);
 #endif
-	setup_memory_region();
+	memory_setup();
 	copy_edd();
 
 	if (!MOUNT_ROOT_RDONLY)
include/asm-x86_64/e820.h +6 -0
index 3486e70..2ced3ba 100644
@@ -20,7 +20,12 @@
 #define E820_ACPI	3
 #define E820_NVS	4
 
+#define MAP_TYPE_STR	"BIOS-e820"
+
 #ifndef __ASSEMBLY__
+
+void native_ebda_info(unsigned *addr, unsigned *size);
+
 struct e820entry {
 	u64 addr;	/* start of memory segment */
 	u64 size;	/* size of memory segment */
@@ -56,6 +61,7 @@ extern struct e820map e820;
 
 extern unsigned ebda_addr, ebda_size;
 extern unsigned long nodemap_addr, nodemap_size;
+
 #endif/*!__ASSEMBLY__*/
 
 #endif/*__E820_HEADER*/
Signature · 1 line
1.4.4.2
#158 lore raw

Re: [PATCH 5/25] [PATCH] native versions for system.h functions

In reply to Glauber de Oliveira Costa

Okay, this one is obviously wrong, my fault (it doesn't do what it says it does in the body of the e-mail. Resending...

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
 arch/x86_64/kernel/tce.c    |    2 -
 arch/x86_64/mm/pageattr.c   |    2 -
 include/asm-x86_64/system.h |   55 +++++++++++++++++++++++++++++---------------
 3 files changed, 39 insertions(+), 20 deletions(-)

foo

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/virtualization

#159 lore raw

Re: [PATCH 5/25] [PATCH] native versions for system.h functions

In reply to Glauber de Oliveira Costa

Okay, this one is obviously wrong, my fault (it doesn't do what it says it does in the body of the e-mail. Resending...

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
 arch/x86_64/kernel/tce.c    |    2 -
 arch/x86_64/mm/pageattr.c   |    2 -
 include/asm-x86_64/system.h |   55 +++++++++++++++++++++++++++++---------------
 3 files changed, 39 insertions(+), 20 deletions(-)

foo

#166 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Glauber de Oliveira Costa
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

include/asm-x86_64/irqflags.h static inline void raw_safe_halt(void) · line 152

Quoted 4 lines
include/asm-x86_64/irqflags.h +4 -0
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt	native_raw_safe_halt
+#define halt		native_halt
+#endif /* ! CONFIG_PARAVIRT */

This seems inconsistent

-Andi

#160 lore raw

Re: [PATCH 4/25] [PATCH] Add debugreg/load_rsp native hooks

In reply to Glauber de Oliveira Costa
Quoted 6 lines
include/asm-x86_64/processor.h +0 -0
@@ -264,13 +270,64 @@ struct thread_struct {
 	set_fs(USER_DS);							 \
 } while(0)
 

include/asm-x86_64/processor.h struct thread_struct { · line 275

Quoted 10 lines
include/asm-x86_64/processor.h +3 -7
-#define get_debugreg(var, register)				\
-		__asm__("movq %%db" #register ", %0"		\
-			:"=r" (var))
-#define set_debugreg(value, register)			\
-		__asm__("movq %0,%%db" #register		\
-			: /* no output */			\
-			:"r" (value))
+static inline unsigned long native_get_debugreg(int regno)
+{
+	unsigned long val;

It would be better to have own functions for each debug register I think

-Andi

#150 lore raw

Re: [PATCH 13/25] [PATCH] turn msr.h functions into native versions

In reply to Glauber de Oliveira Costa
Quoted 1 line · Glauber de Oliveira Costa

+static __always_inline long long vget_cycles_sync(void)

Why is there a copy of this function now? That seems wrong

Review

Quoted 1 line
(quoted diff) +1 -0
+		native_read_tscp(&p);

The instruction is called rdtscp not read_tscp. Please follow that

Quoted 1 line

+#define rdtsc(low, high) \

This macro can be probably eliminated, no callers in kernel

-Andi

#112 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Glauber de Oliveira Costa

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ						\
+			movq	%gs:pda_oldrsp,%rsp;	\
+			swapgs;				\
+			sysretq;

When the macro does more than sysret it should have a different name

arch/x86_64/kernel/entry.S tracesys: · line 350

Quoted 5 lines

*/

arch/x86_64/kernel/entry.S +1 -1
 	.globl int_ret_from_sys_call
 int_ret_from_sys_call:
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

ANY? There are certainly some registers alive at this point like rax

Review

Quoted 3 lines

retint_restore_args:

(quoted diff) +1 -1
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

arch/x86_64/kernel/entry.S retint_restore_args: · line 592

Quoted 11 lines
arch/x86_64/kernel/entry.S +3 -1
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
 	RESTORE_ARGS 0,8,0
 iret_label:
-	iretq
+#ifdef CONFIG_PARAVIRT
+	INTERRUPT_RETURN
+ENTRY(native_iret)

ENTRY adds alignment. Why do you need that export anyways?

arch/x86_64/kernel/entry.S retint_restore_args: · line 597

Quoted 6 lines
arch/x86_64/kernel/entry.S +3 -1
+#endif
+1:	iretq
 
 	.section __ex_table,"a"
-	.quad iret_label,bad_iret
+	.quad 1b, bad_iret

iret_label seems more expressive to me than 1

Review retint_restore_args: · line 598

Quoted 1 line
(quoted diff) +1 -0
+	ENABLE_INTERRUPTS(CLBR_NONE)

In many of the CLBR_NONEs there are actually some registers free; but it might be safer to keep it this way. But if some client can get significantly better code with one or two free registers it might be worthwhile to investigate.

Review retint_restore_args: · line 599

Quoted 2 lines
(quoted diff) +1 -1
-	swapgs
+	SWAPGS_NOSTACK

There's still stack here

arch/x86_64/kernel/entry.S paranoid_swapgs\trace: · line 806

Quoted 4 lines
arch/x86_64/kernel/entry.S +1 -1
 paranoid_restore\trace:
 	RESTORE_ALL 8
-	iretq
+	INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

-Andi

#86 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

Quoted 11 lines
arch/x86_64/kernel/setup.c +7 -0
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
 	printk(KERN_INFO "Command line: %s\n", boot_command_line);

arch/x86_64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 263

Quoted 9 lines
arch/x86_64/kernel/setup.c +6 -0
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
 	saved_video_mode = SAVED_VIDEO_MODE;
 	bootloader_type = LOADER_TYPE;
 
+	/*
+	 * By returning non-zero here, a paravirt impl can choose to
+	 * skip the rest of the setup process
+	 */
+	if (paravirt_arch_setup())
+		return;

Sorry, but that's an extremly ugly and clumpsy interface and will lead to extensive code duplication in hypervisors because so much code is disabled.

This needs to be solved in some better way.

-Andi

#50 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa

arch/x86_64/Kconfig config NODES_SHIFT · line 377

Quoted 3 lines
arch/x86_64/Kconfig +2 -0
+config PARAVIRT
+       bool "Paravirtualization support (EXPERIMENTAL)"

This should be hidden and selected by the clients as needed (I already did this change on i386)

Users know nothing about paravirt, they just know about Xen, lguest etc.

Strictly you would at least need a !X86_VSMP dependency, but with the vsmp change i requested that will be unnecessary

Is this really synced with the latest version of the i386 code?

Quoted 19 lines
arch/x86_64/kernel/paravirt.c new file +17 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
 
 
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/efi.h>
+#include <linux/bcd.h>
+#include <linux/start_kernel.h>
+
+#include <asm/bug.h>
+#include <asm/paravirt.h>
+#include <asm/desc.h>
+#include <asm/setup.h>
+#include <asm/irq.h>
+#include <asm/delay.h>
+#include <asm/fixmap.h>
+#include <asm/apic.h>

arch/x86_64/kernel/paravirt.c line 43

Quoted 10 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+#include <asm/tlbflush.h>
+#include <asm/msr.h>
+#include <asm/page.h>
+#include <asm/pgtable.h>
+#include <asm/proto.h>
+#include <asm/e820.h>
+#include <asm/time.h>
+#include <asm/asm-offsets.h>
+#include <asm/smp.h>
+#include <asm/irqflags.h>

Are the includes really all needed?

arch/x86_64/kernel/paravirt.c line 226

Quoted 3 lines
arch/x86_64/kernel/paravirt.c new file +3 -0
+	if (opfunc == NULL)
+		/* If there's no function, patch it with a ud2a (BUG) */
+		ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);

This will actually give corrupted BUGs because you don't supply the full inline BUG header. Perhaps another trap would be better.

Quoted 1 line

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Quoted 1 line

+extern struct paravirt_ops paravirt_ops;

Should be native_paravirt_ops I guess

Quoted 2 lines

+
+ * This generates an indirect call based on the operation type number.

The macros here don't

include/asm-x86_64/paravirt.h line 478

Quoted 3 lines
include/asm-x86_64/paravirt.h new file +3 -0
+static inline unsigned long read_msr(unsigned int msr)
+{
+	int __err;

No need for __ in inlines

include/asm-x86_64/paravirt.h line 607

Quoted 2 lines
include/asm-x86_64/paravirt.h new file +2 -0
+/* The paravirtualized I/O functions */
+static inline void slow_down_io(void) {

I doubt this needs to be inline and it's large

Review

Quoted 1 line
(quoted diff) +1 -0
+	__asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)

No __*__ in new code please

include/asm-x86_64/paravirt.h line 769

Quoted 12 lines
include/asm-x86_64/paravirt.h new file +12 -0
+			     : "=a"(f)
+			     : paravirt_type(save_fl),
+			       paravirt_clobber(CLBR_RAX)
+			     : "memory", "cc");
+	return f;
+}
+
+static inline void raw_local_irq_restore(unsigned long f)
+{
+	__asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)
+			     :
+			     : "D" (f),

Have you investigated if a different input register generates better/smaller code? I would assume rdi to be usually used already for the caller's arguments so it will produce spilling

Similar for the rax return in the other functions.

-Andi

#161 lore raw

Re: [PATCH 4/25] [PATCH] Add debugreg/load_rsp native hooks

In reply to Glauber de Oliveira Costa
Quoted 6 lines
include/asm-x86_64/processor.h +0 -0
@@ -264,13 +270,64 @@ struct thread_struct {
 	set_fs(USER_DS);							 \
 } while(0)
 

include/asm-x86_64/processor.h struct thread_struct { · line 275

Quoted 10 lines
include/asm-x86_64/processor.h +3 -7
-#define get_debugreg(var, register)				\
-		__asm__("movq %%db" #register ", %0"		\
-			:"=r" (var))
-#define set_debugreg(value, register)			\
-		__asm__("movq %0,%%db" #register		\
-			: /* no output */			\
-			:"r" (value))
+static inline unsigned long native_get_debugreg(int regno)
+{
+	unsigned long val;

It would be better to have own functions for each debug register I think

-Andi

#87 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

Quoted 11 lines
arch/x86_64/kernel/setup.c +7 -0
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
 	printk(KERN_INFO "Command line: %s\n", boot_command_line);

arch/x86_64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 263

Quoted 9 lines
arch/x86_64/kernel/setup.c +6 -0
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
 	saved_video_mode = SAVED_VIDEO_MODE;
 	bootloader_type = LOADER_TYPE;
 
+	/*
+	 * By returning non-zero here, a paravirt impl can choose to
+	 * skip the rest of the setup process
+	 */
+	if (paravirt_arch_setup())
+		return;

Sorry, but that's an extremly ugly and clumpsy interface and will lead to extensive code duplication in hypervisors because so much code is disabled.

This needs to be solved in some better way.

-Andi

#167 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Glauber de Oliveira Costa
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+	return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

include/asm-x86_64/irqflags.h static inline void raw_safe_halt(void) · line 152

Quoted 4 lines
include/asm-x86_64/irqflags.h +4 -0
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt	native_raw_safe_halt
+#define halt		native_halt
+#endif /* ! CONFIG_PARAVIRT */

This seems inconsistent

-Andi

#51 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa

arch/x86_64/Kconfig config NODES_SHIFT · line 377

Quoted 3 lines
arch/x86_64/Kconfig +2 -0
+config PARAVIRT
+       bool "Paravirtualization support (EXPERIMENTAL)"

This should be hidden and selected by the clients as needed (I already did this change on i386)

Users know nothing about paravirt, they just know about Xen, lguest etc.

Strictly you would at least need a !X86_VSMP dependency, but with the vsmp change i requested that will be unnecessary

Is this really synced with the latest version of the i386 code?

Quoted 19 lines
arch/x86_64/kernel/paravirt.c new file +17 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
 
 
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/efi.h>
+#include <linux/bcd.h>
+#include <linux/start_kernel.h>
+
+#include <asm/bug.h>
+#include <asm/paravirt.h>
+#include <asm/desc.h>
+#include <asm/setup.h>
+#include <asm/irq.h>
+#include <asm/delay.h>
+#include <asm/fixmap.h>
+#include <asm/apic.h>

arch/x86_64/kernel/paravirt.c line 43

Quoted 10 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+#include <asm/tlbflush.h>
+#include <asm/msr.h>
+#include <asm/page.h>
+#include <asm/pgtable.h>
+#include <asm/proto.h>
+#include <asm/e820.h>
+#include <asm/time.h>
+#include <asm/asm-offsets.h>
+#include <asm/smp.h>
+#include <asm/irqflags.h>

Are the includes really all needed?

arch/x86_64/kernel/paravirt.c line 226

Quoted 3 lines
arch/x86_64/kernel/paravirt.c new file +3 -0
+	if (opfunc == NULL)
+		/* If there's no function, patch it with a ud2a (BUG) */
+		ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);

This will actually give corrupted BUGs because you don't supply the full inline BUG header. Perhaps another trap would be better.

Quoted 1 line

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Quoted 1 line

+extern struct paravirt_ops paravirt_ops;

Should be native_paravirt_ops I guess

Quoted 2 lines

+
+ * This generates an indirect call based on the operation type number.

The macros here don't

include/asm-x86_64/paravirt.h line 478

Quoted 3 lines
include/asm-x86_64/paravirt.h new file +3 -0
+static inline unsigned long read_msr(unsigned int msr)
+{
+	int __err;

No need for __ in inlines

include/asm-x86_64/paravirt.h line 607

Quoted 2 lines
include/asm-x86_64/paravirt.h new file +2 -0
+/* The paravirtualized I/O functions */
+static inline void slow_down_io(void) {

I doubt this needs to be inline and it's large

Review

Quoted 1 line
(quoted diff) +1 -0
+	__asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)

No __*__ in new code please

include/asm-x86_64/paravirt.h line 769

Quoted 12 lines
include/asm-x86_64/paravirt.h new file +12 -0
+			     : "=a"(f)
+			     : paravirt_type(save_fl),
+			       paravirt_clobber(CLBR_RAX)
+			     : "memory", "cc");
+	return f;
+}
+
+static inline void raw_local_irq_restore(unsigned long f)
+{
+	__asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)
+			     :
+			     : "D" (f),

Have you investigated if a different input register generates better/smaller code? I would assume rdi to be usually used already for the caller's arguments so it will produce spilling

Similar for the rax return in the other functions.

-Andi

#151 lore raw

Re: [PATCH 13/25] [PATCH] turn msr.h functions into native versions

In reply to Glauber de Oliveira Costa
Quoted 1 line · Glauber de Oliveira Costa

+static __always_inline long long vget_cycles_sync(void)

Why is there a copy of this function now? That seems wrong

Review

Quoted 1 line
(quoted diff) +1 -0
+		native_read_tscp(&p);

The instruction is called rdtscp not read_tscp. Please follow that

Quoted 1 line

+#define rdtsc(low, high) \

This macro can be probably eliminated, no callers in kernel

-Andi

#113 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Glauber de Oliveira Costa

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ						\
+			movq	%gs:pda_oldrsp,%rsp;	\
+			swapgs;				\
+			sysretq;

When the macro does more than sysret it should have a different name

arch/x86_64/kernel/entry.S tracesys: · line 350

Quoted 5 lines

*/

arch/x86_64/kernel/entry.S +1 -1
 	.globl int_ret_from_sys_call
 int_ret_from_sys_call:
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

ANY? There are certainly some registers alive at this point like rax

Review

Quoted 3 lines

retint_restore_args:

(quoted diff) +1 -1
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

arch/x86_64/kernel/entry.S retint_restore_args: · line 592

Quoted 11 lines
arch/x86_64/kernel/entry.S +3 -1
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
 	RESTORE_ARGS 0,8,0
 iret_label:
-	iretq
+#ifdef CONFIG_PARAVIRT
+	INTERRUPT_RETURN
+ENTRY(native_iret)

ENTRY adds alignment. Why do you need that export anyways?

arch/x86_64/kernel/entry.S retint_restore_args: · line 597

Quoted 6 lines
arch/x86_64/kernel/entry.S +3 -1
+#endif
+1:	iretq
 
 	.section __ex_table,"a"
-	.quad iret_label,bad_iret
+	.quad 1b, bad_iret

iret_label seems more expressive to me than 1

Review retint_restore_args: · line 598

Quoted 1 line
(quoted diff) +1 -0
+	ENABLE_INTERRUPTS(CLBR_NONE)

In many of the CLBR_NONEs there are actually some registers free; but it might be safer to keep it this way. But if some client can get significantly better code with one or two free registers it might be worthwhile to investigate.

Review retint_restore_args: · line 599

Quoted 2 lines
(quoted diff) +1 -1
-	swapgs
+	SWAPGS_NOSTACK

There's still stack here

arch/x86_64/kernel/entry.S paranoid_swapgs\trace: · line 806

Quoted 4 lines
arch/x86_64/kernel/entry.S +1 -1
 paranoid_restore\trace:
 	RESTORE_ALL 8
-	iretq
+	INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

-Andi

#108 lore raw

Re: [PATCH 21/25] [PATCH] export cpu_gdt_descr

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa

With paravirualization, hypervisors needs to handle the gdt, that was right to this point only used at very early inialization code. Hypervisors are commonly modules, so make it an export

the GDT is so deeply internal that this really ought to be a _GPL export..

#109 lore raw

Re: [PATCH 21/25] [PATCH] export cpu_gdt_descr

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa

With paravirualization, hypervisors needs to handle the gdt, that was right to this point only used at very early inialization code. Hypervisors are commonly modules, so make it an export

the GDT is so deeply internal that this really ought to be a _GPL export..

#114 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

Hi Andi,

Thanks for all the comments, it's greatly appreciated.

Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ						\
+			movq	%gs:pda_oldrsp,%rsp;	\
+			swapgs;				\
+			sysretq;

When the macro does more than sysret it should have a different name

Noted. Do you have a better idea? Something like SETSTACK_SWAPGS_SYSRETQ?

Quoted 9 lines

arch/x86_64/kernel/entry.S tracesys: · line 350

Quoted 7 lines

*/

arch/x86_64/kernel/entry.S +1 -1
 	.globl int_ret_from_sys_call
 int_ret_from_sys_call:
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

ANY? There are certainly some registers alive at this point like rax

Glauber will need to address this, this is his code ;-)

Quoted 8 lines

Review

Quoted 4 lines

retint_restore_args:

(quoted diff) +1 -1
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

Quoted 13 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 592

Quoted 11 lines
arch/x86_64/kernel/entry.S +3 -1
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
 	RESTORE_ARGS 0,8,0
 iret_label:
-	iretq
+#ifdef CONFIG_PARAVIRT
+	INTERRUPT_RETURN
+ENTRY(native_iret)

ENTRY adds alignment. Why do you need that export anyways?

The paravirt ops struct points to it.

Quoted 9 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 597

Quoted 7 lines
arch/x86_64/kernel/entry.S +3 -1
+#endif
+1:	iretq
 
 	.section __ex_table,"a"
-	.quad iret_label,bad_iret
+	.quad 1b, bad_iret

iret_label seems more expressive to me than 1

The reason for this change is because of the added:
#ifdef CONFIG_PARAVIRT
INTERRUPT_RETURN
ENTRY(native_iret)
#endif

If we are paravirt ops, we need the iretq in the exception table, not the paravit ops function call. Since that function call may simply call the native_iretq, and if we take a fault at the iretq, it wont be in the exception table.

Quoted 7 lines

Review retint_restore_args: · line 598

Quoted 2 lines
(quoted diff) +1 -0
+	ENABLE_INTERRUPTS(CLBR_NONE)

In many of the CLBR_NONEs there are actually some registers free; but it might be safer to keep it this way. But if some client can get significantly better code with one or two free registers it might be worthwhile to investigate.

Glauber, that comment is for you.

Quoted 5 lines

Review retint_restore_args: · line 599

Quoted 3 lines
(quoted diff) +1 -1
-	swapgs
+	SWAPGS_NOSTACK

There's still stack here

OK, bad name then. How about SWAPGS_UNTRUSTED_STACK?

From earlier in the file where SWAPGS_NOSTACK is declared we have:

/* Currently paravirt can't handle swapgs nicely when we
* don't have a stack. So we either find a way around these
* or just fault and emulate if a guest tries to call swapgs
* directly.
*
* Either way, this is a good way to document that we don't
* have a reliable stack.
*/
#define SWAPGS_NOSTACK swapgs

Quoted 8 lines

arch/x86_64/kernel/entry.S paranoid_swapgs\trace: · line 806

Quoted 5 lines
arch/x86_64/kernel/entry.S +1 -1
 paranoid_restore\trace:
 	RESTORE_ALL 8
-	iretq
+	INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

Probably not, but this part of the code I don't fully understand. But just doing this doesn't break lguest. But perhaps it only did not break it yet ;-)

Thanks,

-- Steve

#115 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

Hi Andi,

Thanks for all the comments, it's greatly appreciated.

Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ						\
+			movq	%gs:pda_oldrsp,%rsp;	\
+			swapgs;				\
+			sysretq;

When the macro does more than sysret it should have a different name

Noted. Do you have a better idea? Something like SETSTACK_SWAPGS_SYSRETQ?

Quoted 9 lines

arch/x86_64/kernel/entry.S tracesys: · line 350

Quoted 7 lines

*/

arch/x86_64/kernel/entry.S +1 -1
 	.globl int_ret_from_sys_call
 int_ret_from_sys_call:
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

ANY? There are certainly some registers alive at this point like rax

Glauber will need to address this, this is his code ;-)

Quoted 8 lines

Review

Quoted 4 lines

retint_restore_args:

(quoted diff) +1 -1
-	cli
+	DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

Quoted 13 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 592

Quoted 11 lines
arch/x86_64/kernel/entry.S +3 -1
 	/*
 	 * The iretq could re-enable interrupts:
 	 */
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
 	RESTORE_ARGS 0,8,0
 iret_label:
-	iretq
+#ifdef CONFIG_PARAVIRT
+	INTERRUPT_RETURN
+ENTRY(native_iret)

ENTRY adds alignment. Why do you need that export anyways?

The paravirt ops struct points to it.

Quoted 9 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 597

Quoted 7 lines
arch/x86_64/kernel/entry.S +3 -1
+#endif
+1:	iretq
 
 	.section __ex_table,"a"
-	.quad iret_label,bad_iret
+	.quad 1b, bad_iret

iret_label seems more expressive to me than 1

The reason for this change is because of the added:
#ifdef CONFIG_PARAVIRT
INTERRUPT_RETURN
ENTRY(native_iret)
#endif

If we are paravirt ops, we need the iretq in the exception table, not the paravit ops function call. Since that function call may simply call the native_iretq, and if we take a fault at the iretq, it wont be in the exception table.

Quoted 7 lines

Review retint_restore_args: · line 598

Quoted 2 lines
(quoted diff) +1 -0
+	ENABLE_INTERRUPTS(CLBR_NONE)

In many of the CLBR_NONEs there are actually some registers free; but it might be safer to keep it this way. But if some client can get significantly better code with one or two free registers it might be worthwhile to investigate.

Glauber, that comment is for you.

Quoted 5 lines

Review retint_restore_args: · line 599

Quoted 3 lines
(quoted diff) +1 -1
-	swapgs
+	SWAPGS_NOSTACK

There's still stack here

OK, bad name then. How about SWAPGS_UNTRUSTED_STACK?

Quoted 1 line

From earlier in the file where SWAPGS_NOSTACK is declared we have:

/* Currently paravirt can't handle swapgs nicely when we
* don't have a stack. So we either find a way around these
* or just fault and emulate if a guest tries to call swapgs
* directly.
*
* Either way, this is a good way to document that we don't
* have a reliable stack.
*/
#define SWAPGS_NOSTACK swapgs

Quoted 8 lines

arch/x86_64/kernel/entry.S paranoid_swapgs\trace: · line 806

Quoted 5 lines
arch/x86_64/kernel/entry.S +1 -1
 paranoid_restore\trace:
 	RESTORE_ALL 8
-	iretq
+	INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

Probably not, but this part of the code I don't fully understand. But just doing this doesn't break lguest. But perhaps it only did not break it yet ;-)

Thanks,

-- Steve

#52 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 4 lines · Andi Kleen

Strictly you would at least need a !X86_VSMP dependency, but with the vsmp change i requested that will be unnecessary

Is this really synced with the latest version of the i386 code?

Glauber started the paravirt ops 64 a second time around, from scratch using the PVOPS of i386 as a base. But since we couldn't just take a the PVOPS patch from i386 and apply it to x86_64, this was mainly done by looking at i386 code and massaging it for x86_64. Somethings may have slipped (and we may have been looking at different versions of PVOPS). It's not easy trying to keep up with a moving target ;-)

-- Steve

#53 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 4 lines · Andi Kleen

Strictly you would at least need a !X86_VSMP dependency, but with the vsmp change i requested that will be unnecessary

Is this really synced with the latest version of the i386 code?

Glauber started the paravirt ops 64 a second time around, from scratch using the PVOPS of i386 as a base. But since we couldn't just take a the PVOPS patch from i386 and apply it to x86_64, this was mainly done by looking at i386 code and massaging it for x86_64. Somethings may have slipped (and we may have been looking at different versions of PVOPS). It's not easy trying to keep up with a moving target ;-)

-- Steve

#116 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

-Andi

#117 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

-Andi

#118 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

Quoted 5 lines · Andi Kleen
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

Who uses that code? NMIs and debug regs? Lguest only has the host handle the NMIs (doesn't pass to guest). And we haven't gotten to debug regs. Who else uses that part of the code?

We can leave the "paranoid check" change out for this series.

-- Steve

#119 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

Who uses that code? NMIs and debug regs? Lguest only has the host handle the NMIs (doesn't pass to guest). And we haven't gotten to debug regs. Who else uses that part of the code?

We can leave the "paranoid check" change out for this series.

-- Steve

#120 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

Quoted 11 lines · Steven Rostedt
Quoted 5 lines · Andi Kleen
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

Who uses that code? NMIs and debug regs? Lguest only has the host handle the NMIs (doesn't pass to guest). And we haven't gotten to debug regs. Who else uses that part of the code?

I'm not sure I understand your question. You're asking who uses entry.S? Answer would be everybody. If you asked something else please reformulate.

-Andi

#121 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

--

Quoted 10 lines · Andi Kleen
Quoted 8 lines · Steven Rostedt
Quoted 5 lines · Andi Kleen
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

Quoted 6 lines
Quoted 3 lines

Who uses that code? NMIs and debug regs? Lguest only has the host handle the NMIs (doesn't pass to guest). And we haven't gotten to debug regs. Who else uses that part of the code?

I'm not sure I understand your question. You're asking who uses entry.S? Answer would be everybody. If you asked something else please reformulate.

When I said "this part of the code I don't fully understand" I was not talking about entry.S. I understand entry.S very well, but the comment was originally on the paranoid_restore code. Which I thought had to deal with NMIs and such that I didn't worry about that I simply did the default.

Quoted 7 lines

Review

Quoted 4 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

The above was what I was replying to.

-- Steve

#122 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

--

Quoted 10 lines · Andi Kleen
Quoted 8 lines · Steven Rostedt
Quoted 5 lines · Andi Kleen
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

Quoted 6 lines
Quoted 3 lines

Who uses that code? NMIs and debug regs? Lguest only has the host handle the NMIs (doesn't pass to guest). And we haven't gotten to debug regs. Who else uses that part of the code?

I'm not sure I understand your question. You're asking who uses entry.S? Answer would be everybody. If you asked something else please reformulate.

When I said "this part of the code I don't fully understand" I was not talking about entry.S. I understand entry.S very well, but the comment was originally on the paranoid_restore code. Which I thought had to deal with NMIs and such that I didn't worry about that I simply did the default.

Quoted 7 lines

Review

Quoted 4 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

The above was what I was replying to.

-- Steve

#123 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

Quoted 7 lines · Steven Rostedt
Quoted 5 lines · Andi Kleen
Quoted 4 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN
Quoted 5 lines
Quoted 2 lines

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

The above was what I was replying to.

If you were talking about the general iretq => INTERRUPT_RETURN, then the answer is "Yes, they are sufficient". The first version of lguest ran the guest kernel in ring 3 (using dual page tables for guest kernel and guest user). The current version I'm pushing runs lguest in ring 1, and the entry.S code worked for both.

-- Steve

#124 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 7 lines · Steven Rostedt
Quoted 5 lines · Andi Kleen
Quoted 4 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN
Quoted 5 lines
Quoted 2 lines

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

The above was what I was replying to.

If you were talking about the general iretq => INTERRUPT_RETURN, then the answer is "Yes, they are sufficient". The first version of lguest ran the guest kernel in ring 3 (using dual page tables for guest kernel and guest user). The current version I'm pushing runs lguest in ring 1, and the entry.S code worked for both.

-- Steve

#137 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 5 lines

When I said "this part of the code I don't fully understand" I was not talking about entry.S. I understand entry.S very well, but the comment was originally on the paranoid_restore code. Which I thought had to deal with NMIs and such that I didn't worry about that I simply did the default.

The paranoid path is used for more than just NMIs; it's also used for MCEs, stack faults, double faults or debug exceptions. Anything that might happen with a invalid stack or unknown GS state or system in other unknown state.

If you can guarantee your hypervisor never injects any of those it could be ignored; but at least losing debug exceptions would be probably not nice.

Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

This was really a general comment not especially applying to the paranoid path.

-Andi

#138 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 5 lines

When I said "this part of the code I don't fully understand" I was not talking about entry.S. I understand entry.S very well, but the comment was originally on the paranoid_restore code. Which I thought had to deal with NMIs and such that I didn't worry about that I simply did the default.

The paranoid path is used for more than just NMIs; it's also used for MCEs, stack faults, double faults or debug exceptions. Anything that might happen with a invalid stack or unknown GS state or system in other unknown state.

If you can guarantee your hypervisor never injects any of those it could be ignored; but at least losing debug exceptions would be probably not nice.

Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

This was really a general comment not especially applying to the paranoid path.

-Andi

#125 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 5 lines

If you were talking about the general iretq => INTERRUPT_RETURN, then the answer is "Yes, they are sufficient". The first version of lguest ran the guest kernel in ring 3 (using dual page tables for guest kernel and guest user). The current version I'm pushing runs lguest in ring 1, and the entry.S code worked for both.

How do you implement system calls then?

-Andi

#126 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 5 lines

If you were talking about the general iretq => INTERRUPT_RETURN, then the answer is "Yes, they are sufficient". The first version of lguest ran the guest kernel in ring 3 (using dual page tables for guest kernel and guest user). The current version I'm pushing runs lguest in ring 1, and the entry.S code worked for both.

How do you implement system calls then?

-Andi

#127 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

--

Quoted 7 lines · Andi Kleen
Quoted 5 lines

If you were talking about the general iretq => INTERRUPT_RETURN, then the answer is "Yes, they are sufficient". The first version of lguest ran the guest kernel in ring 3 (using dual page tables for guest kernel and guest user). The current version I'm pushing runs lguest in ring 1, and the entry.S code worked for both.

How do you implement system calls then?

/me working very hard to get lguest64 ready for public display

Here's a snippet from my version of core.c. I've been thinking of ways to optimize it, but for now it works fine. This was done for both ring 3 and ring 1 lguest versions (this is the host running):

	/*
	 * Update the LSTAR to point to the HV syscall handler.
	 * Also update the fsbase if the guest uses one.
	 */
	wrmsrl(MSR_LSTAR, (unsigned long)HV_OFFSET(&lguest_syscall_trampoline));

[...]

	asm volatile ("pushq %2; pushq %%rsp; pushfq; pushq %3; call *%6;"
		      /* The stack we pushed is off by 8, due to the
previous pushq */
		      "addq $8, %%rsp"
		      : "=D"(foo), "=a"(bar)
		      : "i" (__KERNEL_DS), "i" (__KERNEL_CS), "0"
(vcpu->vcpu),
			"1"(get_idt_table()),
			"r" (sw_guest)
		      : "memory", "cc");

[...]

	/* restore old LSTAR */
	wrmsrl(MSR_LSTAR, vcpu->host_syscall);

Also in the switcher.S (The Hypervisor):

.global lguest_syscall_trampoline
.type lguest_syscall_trampoline, @function
lguest_syscall_trampoline:

	/*
	 * Tricky, we don't have much to choose from here.
	 * The only way to get to our stack is with swapgs.
	 * but we need to save the stack too, so we have to play
	 * very carefully.
	 */
	swapgs
	/* now gs points to our VCPU Guest Data */

	/* first save the stack! */
	movq	%rsp, %gs:LGUEST_GUEST_DATA_regs_rsp

	/*
	 * x86 arch doesn't have an easy way to find out where
	 * gs is located. So we need to read the MSR. But first
	 * we need to save off the rcx, rax and rdx.
	 */
	movq	%rax, %gs:LGUEST_GUEST_DATA_regs_rax
	movq	%rdx, %gs:LGUEST_GUEST_DATA_regs_rdx
	movq	%rcx, %gs:LGUEST_GUEST_DATA_regs_rcx

	/* Need to read manual, does rdmsr clear
	 * the top 32 bits of rax? */
	xor	%rax, %rax

	movl  	$MSR_GS_BASE,%ecx
	rdmsr
	shl	$32, %rdx
	orq	%rax, %rdx

	movq	%rdx, %rsp

	/* see if we need to disable interrupts */
	testq	$(1<<9),  %gs:LGUEST_GUEST_DATA_SFMASK
	jz	1f
	movq	$0, %gs:LGUEST_GUEST_DATA_irq_enabled
	jmp	2f
1:
	/* Still need to clear bit 10 (just in case) */
	/*  (see lguest_iretq)			     */
	movq	$(1<<10), %rax
	not	%rax
	andq	%rax, %gs:LGUEST_GUEST_DATA_irq_enabled
2:
	/* put back the generic regs */
	movq	%gs:LGUEST_GUEST_DATA_regs_rdx, %rdx
	movq	%gs:LGUEST_GUEST_DATA_regs_rcx, %rcx
	movq	%gs:LGUEST_GUEST_DATA_regs_rax, %rax

	/* Is this a hypercall? */
	testq	$1, %gs:LGUEST_GUEST_DATA_is_hc
	jnz	handle_hcall

	/* We have 64 bytes to play with */
	addq	$LGUEST_GUEST_DATA_regs, %rsp

	/* do the swapgs if possible */
	testq	$1, %gs:LGUEST_GUEST_DATA_do_swapgs
	je	1f

	/* We now have a stack to use */
	/* go back to the guest's gs */
	swapgs
	DO_SWAPGS_USE_STACK
	/* and then back to HV gs */
	swapgs
1:
	/*
	 * The stack has 64 bytes of playing room.
	 * Which is enough to do a jump to the guest kernel.
	 * We store the guest LSTAR register in the scatch pad
	 * because we don't care if the guest messes with it.
	 * If it is a bad address, we fault from the guest side
	 * and we kill the guest. No harm done to the host.
	 */
	pushq	$(__KERNEL_DS | 1)
	pushq	%gs:LGUEST_GUEST_DATA_regs_rsp
	pushfq
	/* Make sure we have actual interrupts on */
	orq	$(1<<9), 0(%rsp)
	pushq	$(__KERNEL_CS | 1)
	pushq	%gs:LGUEST_GUEST_DATA_LSTAR

	swapgs
	iretq

NOTE: This is still under development, since I'm going with a new design change to try to stay more in sync with lguest32.

-- Steve

#128 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

--

Quoted 7 lines · Andi Kleen
Quoted 5 lines

If you were talking about the general iretq => INTERRUPT_RETURN, then the answer is "Yes, they are sufficient". The first version of lguest ran the guest kernel in ring 3 (using dual page tables for guest kernel and guest user). The current version I'm pushing runs lguest in ring 1, and the entry.S code worked for both.

How do you implement system calls then?

/me working very hard to get lguest64 ready for public display

Here's a snippet from my version of core.c. I've been thinking of ways to optimize it, but for now it works fine. This was done for both ring 3 and ring 1 lguest versions (this is the host running):

	/*
	 * Update the LSTAR to point to the HV syscall handler.
	 * Also update the fsbase if the guest uses one.
	 */
	wrmsrl(MSR_LSTAR, (unsigned long)HV_OFFSET(&lguest_syscall_trampoline));

[...]

	asm volatile ("pushq %2; pushq %%rsp; pushfq; pushq %3; call *%6;"
		      /* The stack we pushed is off by 8, due to the
previous pushq */
		      "addq $8, %%rsp"
		      : "=D"(foo), "=a"(bar)
		      : "i" (__KERNEL_DS), "i" (__KERNEL_CS), "0"
(vcpu->vcpu),
			"1"(get_idt_table()),
			"r" (sw_guest)
		      : "memory", "cc");

[...]

	/* restore old LSTAR */
	wrmsrl(MSR_LSTAR, vcpu->host_syscall);

Also in the switcher.S (The Hypervisor):

.global lguest_syscall_trampoline
.type lguest_syscall_trampoline, @function
lguest_syscall_trampoline:

	/*
	 * Tricky, we don't have much to choose from here.
	 * The only way to get to our stack is with swapgs.
	 * but we need to save the stack too, so we have to play
	 * very carefully.
	 */
	swapgs
	/* now gs points to our VCPU Guest Data */

	/* first save the stack! */
	movq	%rsp, %gs:LGUEST_GUEST_DATA_regs_rsp

	/*
	 * x86 arch doesn't have an easy way to find out where
	 * gs is located. So we need to read the MSR. But first
	 * we need to save off the rcx, rax and rdx.
	 */
	movq	%rax, %gs:LGUEST_GUEST_DATA_regs_rax
	movq	%rdx, %gs:LGUEST_GUEST_DATA_regs_rdx
	movq	%rcx, %gs:LGUEST_GUEST_DATA_regs_rcx

	/* Need to read manual, does rdmsr clear
	 * the top 32 bits of rax? */
	xor	%rax, %rax

	movl  	$MSR_GS_BASE,%ecx
	rdmsr
	shl	$32, %rdx
	orq	%rax, %rdx

	movq	%rdx, %rsp

	/* see if we need to disable interrupts */
	testq	$(1<<9),  %gs:LGUEST_GUEST_DATA_SFMASK
	jz	1f
	movq	$0, %gs:LGUEST_GUEST_DATA_irq_enabled
	jmp	2f
1:
	/* Still need to clear bit 10 (just in case) */
	/*  (see lguest_iretq)			     */
	movq	$(1<<10), %rax
	not	%rax
	andq	%rax, %gs:LGUEST_GUEST_DATA_irq_enabled
2:
	/* put back the generic regs */
	movq	%gs:LGUEST_GUEST_DATA_regs_rdx, %rdx
	movq	%gs:LGUEST_GUEST_DATA_regs_rcx, %rcx
	movq	%gs:LGUEST_GUEST_DATA_regs_rax, %rax

	/* Is this a hypercall? */
	testq	$1, %gs:LGUEST_GUEST_DATA_is_hc
	jnz	handle_hcall

	/* We have 64 bytes to play with */
	addq	$LGUEST_GUEST_DATA_regs, %rsp

	/* do the swapgs if possible */
	testq	$1, %gs:LGUEST_GUEST_DATA_do_swapgs
	je	1f

	/* We now have a stack to use */
	/* go back to the guest's gs */
	swapgs
	DO_SWAPGS_USE_STACK
	/* and then back to HV gs */
	swapgs
1:
	/*
	 * The stack has 64 bytes of playing room.
	 * Which is enough to do a jump to the guest kernel.
	 * We store the guest LSTAR register in the scatch pad
	 * because we don't care if the guest messes with it.
	 * If it is a bad address, we fault from the guest side
	 * and we kill the guest. No harm done to the host.
	 */
	pushq	$(__KERNEL_DS | 1)
	pushq	%gs:LGUEST_GUEST_DATA_regs_rsp
	pushfq
	/* Make sure we have actual interrupts on */
	orq	$(1<<9), 0(%rsp)
	pushq	$(__KERNEL_CS | 1)
	pushq	%gs:LGUEST_GUEST_DATA_LSTAR

	swapgs
	iretq

NOTE: This is still under development, since I'm going with a new design change to try to stay more in sync with lguest32.

-- Steve

#141 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 11 lines · Steven Rostedt
Quoted 5 lines · Andi Kleen
Quoted 2 lines

Probably not, but this part of the code I don't fully understand.

I would suggest to defer all this until at least one example to test it (except vsmp which is too simple) is around.

Who uses that code? NMIs and debug regs? Lguest only has the host handle the NMIs (doesn't pass to guest). And we haven't gotten to debug regs. Who else uses that part of the code?

I'm not sure I understand your question. You're asking who uses entry.S? Answer would be everybody. If you asked something else please reformulate.

-Andi

#144 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

Thank you for the attention, andi

let's go:

Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ                                              \
+                     movq    %gs:pda_oldrsp,%rsp;    \
+                     swapgs;                         \
+                     sysretq;

When the macro does more than sysret it should have a different name

That's fair. Again, suggestions are welcome. Maybe SYSCALL_RETURN ?

Quoted 7 lines

Review

Quoted 5 lines

*/
.globl int_ret_from_sys_call
int_ret_from_sys_call:

(quoted diff) +1 -1
-     cli
+     DISABLE_INTERRUPTS(CLBR_ANY)

ANY? There are certainly some registers alive at this point like rax

yes, this one is wrong. Thanks for the catch

Quoted 5 lines

Review

Quoted 3 lines

retint_restore_args:

(quoted diff) +1 -1
-     cli
+     DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

I don't think so. They are live here, but restore_args follows, so we can safely clobber anything here. Right?

Quoted 14 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 592

Quoted 12 lines
      /*
       * The iretq could re-enable interrupts:
       */
arch/x86_64/kernel/entry.S +3 -1
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
      RESTORE_ARGS 0,8,0
 iret_label:
-     iretq
+#ifdef CONFIG_PARAVIRT
+     INTERRUPT_RETURN
+ENTRY(native_iret)

ENTRY adds alignment. Why do you need that export anyways?

Just went on the flow. Will change.

Quoted 8 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 597

Quoted 6 lines
arch/x86_64/kernel/entry.S +3 -1
+#endif
+1:   iretq
 
      .section __ex_table,"a"
-     .quad iret_label,bad_iret
+     .quad 1b, bad_iret

iret_label seems more expressive to me than 1

fair.

Quoted 6 lines

Review retint_restore_args: · line 598

Quoted 1 line
(quoted diff) +1 -0
+     ENABLE_INTERRUPTS(CLBR_NONE)

In many of the CLBR_NONEs there are actually some registers free; but it might be safer to keep it this way. But if some client can get significantly better code with one or two free registers it might be worthwhile to investigate.

That's exactly what I had in mind. I'd highly prefer to keep it this way until it is merged, and we are sure all the rest is stable

Quoted 4 lines

Review retint_restore_args: · line 599

Quoted 2 lines
(quoted diff) +1 -1
-     swapgs
+     SWAPGS_NOSTACK

There's still stack here

Yes, but it is not safe to use. I think Roasted addressed it later on.

Quoted 7 lines

arch/x86_64/kernel/entry.S paranoid_swapgs\trace: · line 806

Quoted 4 lines
arch/x86_64/kernel/entry.S +1 -1
 paranoid_restore\trace:
      RESTORE_ALL 8
-     iretq
+     INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

Yes, they are sufficient for lguest.
Does any xen folks have any comment?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#145 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

Thank you for the attention, andi

let's go:

Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ                                              \
+                     movq    %gs:pda_oldrsp,%rsp;    \
+                     swapgs;                         \
+                     sysretq;

When the macro does more than sysret it should have a different name

That's fair. Again, suggestions are welcome. Maybe SYSCALL_RETURN ?

Quoted 7 lines

Review

Quoted 5 lines

*/
.globl int_ret_from_sys_call
int_ret_from_sys_call:

(quoted diff) +1 -1
-     cli
+     DISABLE_INTERRUPTS(CLBR_ANY)

ANY? There are certainly some registers alive at this point like rax

yes, this one is wrong. Thanks for the catch

Quoted 5 lines

Review

Quoted 3 lines

retint_restore_args:

(quoted diff) +1 -1
-     cli
+     DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

I don't think so. They are live here, but restore_args follows, so we can safely clobber anything here. Right?

Quoted 14 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 592

Quoted 12 lines
      /*
       * The iretq could re-enable interrupts:
       */
arch/x86_64/kernel/entry.S +3 -1
@@ -566,10 +587,14 @@ retint_restore_args:
 restore_args:
      RESTORE_ARGS 0,8,0
 iret_label:
-     iretq
+#ifdef CONFIG_PARAVIRT
+     INTERRUPT_RETURN
+ENTRY(native_iret)

ENTRY adds alignment. Why do you need that export anyways?

Just went on the flow. Will change.

Quoted 8 lines

arch/x86_64/kernel/entry.S retint_restore_args: · line 597

Quoted 6 lines
arch/x86_64/kernel/entry.S +3 -1
+#endif
+1:   iretq
 
      .section __ex_table,"a"
-     .quad iret_label,bad_iret
+     .quad 1b, bad_iret

iret_label seems more expressive to me than 1

fair.

Quoted 6 lines

Review retint_restore_args: · line 598

Quoted 1 line
(quoted diff) +1 -0
+     ENABLE_INTERRUPTS(CLBR_NONE)

In many of the CLBR_NONEs there are actually some registers free; but it might be safer to keep it this way. But if some client can get significantly better code with one or two free registers it might be worthwhile to investigate.

That's exactly what I had in mind. I'd highly prefer to keep it this way until it is merged, and we are sure all the rest is stable

Quoted 4 lines

Review retint_restore_args: · line 599

Quoted 2 lines
(quoted diff) +1 -1
-     swapgs
+     SWAPGS_NOSTACK

There's still stack here

Yes, but it is not safe to use. I think Roasted addressed it later on.

Quoted 7 lines

arch/x86_64/kernel/entry.S paranoid_swapgs\trace: · line 806

Quoted 4 lines
arch/x86_64/kernel/entry.S +1 -1
 paranoid_restore\trace:
      RESTORE_ALL 8
-     iretq
+     INTERRUPT_RETURN

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

Yes, they are sufficient for lguest.
Does any xen folks have any comment?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#142 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 3 lines
Quoted 1 line

ENTRY adds alignment. Why do you need that export anyways?

The paravirt ops struct points to it.

But the paravirt_ops probably won't need it as an export. So I guess andi is right.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#143 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 3 lines
Quoted 1 line

ENTRY adds alignment. Why do you need that export anyways?

The paravirt ops struct points to it.

But the paravirt_ops probably won't need it as an export. So I guess andi is right.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#139 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

--

Quoted 6 lines · Andi Kleen
Quoted 5 lines

When I said "this part of the code I don't fully understand" I was not talking about entry.S. I understand entry.S very well, but the comment was originally on the paranoid_restore code. Which I thought had to deal with NMIs and such that I didn't worry about that I simply did the default.

Quoted 7 lines

The paranoid path is used for more than just NMIs; it's also used for MCEs, stack faults, double faults or debug exceptions. Anything that might happen with a invalid stack or unknown GS state or system in other unknown state.

If you can guarantee your hypervisor never injects any of those it could be ignored; but at least losing debug exceptions would be probably not nice.

For now it does ignore them. But that's something to work on for later versions of lguest64. I want lguest out in the public and working for the general case. I don't plan on ignoring the paranoid section forever. But it's more on an anomaly for now. But you are right, I want the debug stuff working. But I also need to walk before I can run ;-)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

This was really a general comment not especially applying to the paranoid path.

Ah, OK, I assumed you were talking about just the previous code. But rereading your post, I see it was more general. Sorry, for the miscommunication.

-- Steve

#140 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

--

Quoted 6 lines · Andi Kleen
Quoted 5 lines

When I said "this part of the code I don't fully understand" I was not talking about entry.S. I understand entry.S very well, but the comment was originally on the paranoid_restore code. Which I thought had to deal with NMIs and such that I didn't worry about that I simply did the default.

Quoted 7 lines

The paranoid path is used for more than just NMIs; it's also used for MCEs, stack faults, double faults or debug exceptions. Anything that might happen with a invalid stack or unknown GS state or system in other unknown state.

If you can guarantee your hypervisor never injects any of those it could be ignored; but at least losing debug exceptions would be probably not nice.

For now it does ignore them. But that's something to work on for later versions of lguest64. I want lguest out in the public and working for the general case. I don't plan on ignoring the paranoid section forever. But it's more on an anomaly for now. But you are right, I want the debug stuff working. But I also need to walk before I can run ;-)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

paranoid_restore\trace:
RESTORE_ALL 8

(quoted diff) +1 -1
-     iretq
+     INTERRUPT_RETURN
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

I suspect Xen will need much more changes anyways because of its ring 3 guest. Are these changes sufficient for lguest?

This was really a general comment not especially applying to the paranoid path.

Ah, OK, I assumed you were talking about just the previous code. But rereading your post, I see it was more general. Sorry, for the miscommunication.

-- Steve

#129 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 13 lines · Steven Rostedt

/me working very hard to get lguest64 ready for public display

Here's a snippet from my version of core.c. I've been thinking of ways to optimize it, but for now it works fine. This was done for both ring 3 and ring 1 lguest versions (this is the host running):

	/*
	 * Update the LSTAR to point to the HV syscall handler.
	 * Also update the fsbase if the guest uses one.
	 */
	wrmsrl(MSR_LSTAR, (unsigned long)HV_OFFSET(&lguest_syscall_trampoline));
Quoted 5 lines

[...]

	asm volatile ("pushq %2; pushq %%rsp; pushfq; pushq %3; call *%6;"
		      /* The stack we pushed is off by 8, due to the
previous pushq */
		      "addq $8, %%rsp"

Weird stack inbalance, i'm surprised that works. %6 must be doing strange things.

Quoted 6 lines
	/* Need to read manual, does rdmsr clear
	 * the top 32 bits of rax? */
	xor	%rax, %rax

	movl  	$MSR_GS_BASE,%ecx
	rdmsr

This seems incredibly slow. Since GS changes are controlled in the kernel why don't you just cache them in the per cpu area? The only case where user can reload it is using segment selector changes, which can be also handled.

Also why do you need the guest gs value anyways?

You could just use your own stack and let the guest switch to its own.

-Andi

#130 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 13 lines · Steven Rostedt

/me working very hard to get lguest64 ready for public display

Here's a snippet from my version of core.c. I've been thinking of ways to optimize it, but for now it works fine. This was done for both ring 3 and ring 1 lguest versions (this is the host running):

	/*
	 * Update the LSTAR to point to the HV syscall handler.
	 * Also update the fsbase if the guest uses one.
	 */
	wrmsrl(MSR_LSTAR, (unsigned long)HV_OFFSET(&lguest_syscall_trampoline));
Quoted 5 lines

[...]

	asm volatile ("pushq %2; pushq %%rsp; pushfq; pushq %3; call *%6;"
		      /* The stack we pushed is off by 8, due to the
previous pushq */
		      "addq $8, %%rsp"

Weird stack inbalance, i'm surprised that works. %6 must be doing strange things.

Quoted 6 lines
	/* Need to read manual, does rdmsr clear
	 * the top 32 bits of rax? */
	xor	%rax, %rax

	movl  	$MSR_GS_BASE,%ecx
	rdmsr

This seems incredibly slow. Since GS changes are controlled in the kernel why don't you just cache them in the per cpu area? The only case where user can reload it is using segment selector changes, which can be also handled.

Also why do you need the guest gs value anyways?

You could just use your own stack and let the guest switch to its own.

-Andi

#88 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Andi Kleen
Quoted 7 lines · Andi Kleen

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

Let's make sure I understand: So you suggest skipping discover altogether in case it is already reserved?

Quoted 15 lines
Quoted 15 lines
arch/x86_64/kernel/setup.c +7 -0
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
      printk(KERN_INFO "Command line: %s\n", boot_command_line);
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
      saved_video_mode = SAVED_VIDEO_MODE;
      bootloader_type = LOADER_TYPE;
 
Quoted 10 lines

arch/x86_64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 263

Quoted 6 lines
arch/x86_64/kernel/setup.c +6 -0
+     /*
+      * By returning non-zero here, a paravirt impl can choose to
+      * skip the rest of the setup process
+      */
+     if (paravirt_arch_setup())
+             return;

Sorry, but that's an extremly ugly and clumpsy interface and will lead to extensive code duplication in hypervisors because so much code is disabled.

We can just wipe out the return value right now. Note that it was a choice, it would only lead to code duplication if the hypervisor wanted it. But yeah, I understand your concern. They may chose to return 1 here just to change some tiny thing in the bottom.

I don't know exactly what other kinds of hooks we could put there. lguest surely didn't need any. Are you okay with just turning it into void by now ?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#89 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Andi Kleen
Quoted 7 lines · Andi Kleen

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

Let's make sure I understand: So you suggest skipping discover altogether in case it is already reserved?

Quoted 15 lines
Quoted 15 lines
arch/x86_64/kernel/setup.c +7 -0
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
      printk(KERN_INFO "Command line: %s\n", boot_command_line);
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
      saved_video_mode = SAVED_VIDEO_MODE;
      bootloader_type = LOADER_TYPE;
 
Quoted 10 lines

arch/x86_64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 263

Quoted 6 lines
arch/x86_64/kernel/setup.c +6 -0
+     /*
+      * By returning non-zero here, a paravirt impl can choose to
+      * skip the rest of the setup process
+      */
+     if (paravirt_arch_setup())
+             return;

Sorry, but that's an extremly ugly and clumpsy interface and will lead to extensive code duplication in hypervisors because so much code is disabled.

We can just wipe out the return value right now. Note that it was a choice, it would only lead to code duplication if the hypervisor wanted it. But yeah, I understand your concern. They may chose to return 1 here just to change some tiny thing in the bottom.

I don't know exactly what other kinds of hooks we could put there. lguest surely didn't need any. Are you okay with just turning it into void by now ?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#168 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Andi Kleen
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

By "client" you mean a user of the paravirt interface?

Quoted 6 lines

include/asm-x86_64/irqflags.h static inline void raw_safe_halt(void) · line 152

Quoted 4 lines
include/asm-x86_64/irqflags.h +4 -0
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt        native_raw_safe_halt
+#define halt         native_halt
+#endif /* ! CONFIG_PARAVIRT */

This seems inconsistent

Sorry andi. Can't see why. Could you elaborate?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#169 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Andi Kleen
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

By "client" you mean a user of the paravirt interface?

Quoted 6 lines

include/asm-x86_64/irqflags.h static inline void raw_safe_halt(void) · line 152

Quoted 4 lines
include/asm-x86_64/irqflags.h +4 -0
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt        native_raw_safe_halt
+#define halt         native_halt
+#endif /* ! CONFIG_PARAVIRT */

This seems inconsistent

Sorry andi. Can't see why. Could you elaborate?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#146 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Glauber de Oliveira Costa
Quoted 10 lines · On Wednesday 08 August 2007 15:58:06 Glauber de Oliveira Costa wrote:
Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ                                              \
+                     movq    %gs:pda_oldrsp,%rsp;    \
+                     swapgs;                         \
+                     sysretq;

When the macro does more than sysret it should have a different name

That's fair. Again, suggestions are welcome. Maybe SYSCALL_RETURN ?

Sounds reasonable.

Quoted 7 lines
Quoted 5 lines

Review

Quoted 3 lines

retint_restore_args:

(quoted diff) +1 -1
-     cli
+     DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

I don't think so. They are live here, but restore_args follows, so we can safely clobber anything here. Right?

The non argument registers cannot be clobbered.

-Andi

#147 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Glauber de Oliveira Costa
Quoted 10 lines · On Wednesday 08 August 2007 15:58:06 Glauber de Oliveira Costa wrote:
Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/entry.S line 64

Quoted 5 lines
arch/x86_64/kernel/entry.S +4 -0
+#define SYSRETQ                                              \
+                     movq    %gs:pda_oldrsp,%rsp;    \
+                     swapgs;                         \
+                     sysretq;

When the macro does more than sysret it should have a different name

That's fair. Again, suggestions are welcome. Maybe SYSCALL_RETURN ?

Sounds reasonable.

Quoted 7 lines
Quoted 5 lines

Review

Quoted 3 lines

retint_restore_args:

(quoted diff) +1 -1
-     cli
+     DISABLE_INTERRUPTS(CLBR_ANY)

Similar.

I don't think so. They are live here, but restore_args follows, so we can safely clobber anything here. Right?

The non argument registers cannot be clobbered.

-Andi

#90 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 10 lines · On Wednesday 08 August 2007 16:08:25 Glauber de Oliveira Costa wrote:
Quoted 7 lines · Andi Kleen

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

Let's make sure I understand: So you suggest skipping discover altogether in case it is already reserved?

Yes

Quoted 3 lines

I don't know exactly what other kinds of hooks we could put there. lguest surely didn't need any. Are you okay with just turning it into void by now ?

Yes.

-Andi

#91 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 10 lines · On Wednesday 08 August 2007 16:08:25 Glauber de Oliveira Costa wrote:
Quoted 7 lines · Andi Kleen

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

Let's make sure I understand: So you suggest skipping discover altogether in case it is already reserved?

Yes

Quoted 3 lines

I don't know exactly what other kinds of hooks we could put there. lguest surely didn't need any. Are you okay with just turning it into void by now ?

Yes.

-Andi

#170 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Glauber de Oliveira Costa
Quoted 19 lines · On Wednesday 08 August 2007 16:10:28 Glauber de Oliveira Costa wrote:
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

Quoted 1 line

By "client" you mean a user of the paravirt interface?

Yes

Quoted 8 lines
Quoted 7 lines

include/asm-x86_64/irqflags.h static inline void raw_safe_halt(void) · line 152

Quoted 5 lines
include/asm-x86_64/irqflags.h +4 -0
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt        native_raw_safe_halt
+#define halt         native_halt
+#endif /* ! CONFIG_PARAVIRT */

This seems inconsistent

Sorry andi. Can't see why. Could you elaborate?

Hmm, must have misread it. It's ok

-Andi

#171 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Glauber de Oliveira Costa
Quoted 19 lines · On Wednesday 08 August 2007 16:10:28 Glauber de Oliveira Costa wrote:
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

Quoted 1 line

By "client" you mean a user of the paravirt interface?

Yes

Quoted 8 lines
Quoted 7 lines

include/asm-x86_64/irqflags.h static inline void raw_safe_halt(void) · line 152

Quoted 5 lines
include/asm-x86_64/irqflags.h +4 -0
+#ifndef CONFIG_PARAVIRT
+#define raw_safe_halt        native_raw_safe_halt
+#define halt         native_halt
+#endif /* ! CONFIG_PARAVIRT */

This seems inconsistent

Sorry andi. Can't see why. Could you elaborate?

Hmm, must have misread it. It's ok

-Andi

#152 lore raw

Re: [PATCH 13/25] [PATCH] turn msr.h functions into native versions

In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 1 line · Glauber de Oliveira Costa

+static __always_inline long long vget_cycles_sync(void)

Why is there a copy of this function now? That seems wrong

Yeah, the other one is in i386 headers, so We probably wan't to leave it there. One option is to move get_cycles_sync to x86_64 headers, and then #ifdef just the offending part.

Quoted 3 lines
Quoted 1 line

+ native_read_tscp(&p);

The instruction is called rdtscp not read_tscp. Please follow that

Although the operation consists in reading tscp. I choose this to be consistent with i386, but I have no special feelings about it. I'm okay with changing it if you prefer.

Quoted 5 lines
Quoted 1 line

+#define rdtsc(low, high) \

This macro can be probably eliminated, no callers in kernel

Fine.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#153 lore raw

Re: [PATCH 13/25] [PATCH] turn msr.h functions into native versions

In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 1 line · Glauber de Oliveira Costa

+static __always_inline long long vget_cycles_sync(void)

Why is there a copy of this function now? That seems wrong

Yeah, the other one is in i386 headers, so We probably wan't to leave it there. One option is to move get_cycles_sync to x86_64 headers, and then #ifdef just the offending part.

Quoted 3 lines
Quoted 1 line

+ native_read_tscp(&p);

The instruction is called rdtscp not read_tscp. Please follow that

Although the operation consists in reading tscp. I choose this to be consistent with i386, but I have no special feelings about it. I'm okay with changing it if you prefer.

Quoted 5 lines
Quoted 1 line

+#define rdtsc(low, high) \

This macro can be probably eliminated, no callers in kernel

Fine.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#172 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Andi Kleen
Quoted 20 lines · Andi Kleen
Quoted 19 lines · On Wednesday 08 August 2007 16:10:28 Glauber de Oliveira Costa wrote:
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

Quoted 3 lines
Quoted 1 line

By "client" you mean a user of the paravirt interface?

Yes

Ohhh, I see. You're talking about just the first piece of code inside the #ifdef CONFIG_PARAVIRT. In this case yes, I agree with you. It can be done.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#173 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Andi Kleen
Quoted 20 lines · Andi Kleen
Quoted 19 lines · On Wednesday 08 August 2007 16:10:28 Glauber de Oliveira Costa wrote:
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

Quoted 3 lines
Quoted 1 line

By "client" you mean a user of the paravirt interface?

Yes

Ohhh, I see. You're talking about just the first piece of code inside the #ifdef CONFIG_PARAVIRT. In this case yes, I agree with you. It can be done.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#154 lore raw

Re: [PATCH 13/25] [PATCH] turn msr.h functions into native versions

In reply to Glauber de Oliveira Costa
Quoted 3 lines

The instruction is called rdtscp not read_tscp. Please follow that

Although the operation consists in reading tscp.

There is no tscp register

-Andi

#155 lore raw

Re: [PATCH 13/25] [PATCH] turn msr.h functions into native versions

In reply to Glauber de Oliveira Costa
Quoted 3 lines

The instruction is called rdtscp not read_tscp. Please follow that

Although the operation consists in reading tscp.

There is no tscp register

-Andi

#110 lore raw

Re: [PATCH 21/25] [PATCH] export cpu_gdt_descr

In reply to Arjan van de Ven
Quoted 9 lines · Arjan van de Ven
Quoted 5 lines · Glauber de Oliveira Costa

With paravirualization, hypervisors needs to handle the gdt, that was right to this point only used at very early inialization code. Hypervisors are commonly modules, so make it an export

the GDT is so deeply internal that this really ought to be a _GPL export..

Yes, Arjan, I agree. Thanks for noticing it.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#111 lore raw

Re: [PATCH 21/25] [PATCH] export cpu_gdt_descr

In reply to Arjan van de Ven
Quoted 9 lines · Arjan van de Ven
Quoted 5 lines · Glauber de Oliveira Costa

With paravirualization, hypervisors needs to handle the gdt, that was right to this point only used at very early inialization code. Hypervisors are commonly modules, so make it an export

the GDT is so deeply internal that this really ought to be a _GPL export..

Yes, Arjan, I agree. Thanks for noticing it.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#54 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 2 lines · Andi Kleen

Is this really synced with the latest version of the i386 code?

Roasted already commented on this. I will check out and change it here.

Quoted 25 lines
Quoted 25 lines
arch/x86_64/kernel/paravirt.c new file +22 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
 
 
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/efi.h>
+#include <linux/bcd.h>
+#include <linux/start_kernel.h>
+
+#include <asm/bug.h>
+#include <asm/paravirt.h>
+#include <asm/desc.h>
+#include <asm/setup.h>
+#include <asm/irq.h>
+#include <asm/delay.h>
+#include <asm/fixmap.h>
+#include <asm/apic.h>
+#include <asm/tlbflush.h>
+#include <asm/msr.h>
+#include <asm/page.h>
+#include <asm/pgtable.h>
+#include <asm/proto.h>
Quoted 8 lines

arch/x86_64/kernel/paravirt.c line 43

Quoted 5 lines
arch/x86_64/kernel/paravirt.c new file +5 -0
+#include <asm/e820.h>
+#include <asm/time.h>
+#include <asm/asm-offsets.h>
+#include <asm/smp.h>
+#include <asm/irqflags.h>

Are the includes really all needed?

delay.h is not needed anymore. Most of them, could be maybe moved to paravirt.c , which is the one that really needs all the native_ things. Yeah, it will be better code this way, will change.

Quoted 7 lines

arch/x86_64/kernel/paravirt.c line 226

Quoted 4 lines
arch/x86_64/kernel/paravirt.c new file +3 -0
+     if (opfunc == NULL)
+             /* If there's no function, patch it with a ud2a (BUG) */
+             ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);

This will actually give corrupted BUGs because you don't supply the full inline BUG header. Perhaps another trap would be better.

You mean this:

Quoted 1 line
Quoted 1 line

+#include <asm/bug.h>

?

Quoted 4 lines
Quoted 2 lines

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Sure.

Quoted 2 lines

Should be native_paravirt_ops I guess

makes sense.

Quoted 11 lines
Quoted 2 lines

+
+ * This generates an indirect call based on the operation type number.

The macros here don't

include/asm-x86_64/paravirt.h line 478

Quoted 3 lines
include/asm-x86_64/paravirt.h new file +3 -0
+static inline unsigned long read_msr(unsigned int msr)
+{
+     int __err;

No need for __ in inlines

Right. Thanks.

Quoted 4 lines

include/asm-x86_64/paravirt.h line 607

Quoted 2 lines
include/asm-x86_64/paravirt.h new file +2 -0
+/* The paravirtualized I/O functions */
+static inline void slow_down_io(void) {

I doubt this needs to be inline and it's large

In a second look, i386 have such a function in io.h because they need slow_down_io in a bunch of I/O instructions. It seems that we do not. Could we just get rid of it, then?

Quoted 3 lines
Quoted 1 line

+ __asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)

No __*__ in new code please

Yup, will fix.

Quoted 17 lines

include/asm-x86_64/paravirt.h line 769

Quoted 12 lines
include/asm-x86_64/paravirt.h new file +12 -0
+                          : "=a"(f)
+                          : paravirt_type(save_fl),
+                            paravirt_clobber(CLBR_RAX)
+                          : "memory", "cc");
+     return f;
+}
+
+static inline void raw_local_irq_restore(unsigned long f)
+{
+     __asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)
+                          :
+                          : "D" (f),

Have you investigated if a different input register generates better/smaller code? I would assume rdi to be usually used already for the caller's arguments so it will produce spilling

Quoted 1 line

Similar for the rax return in the other functions.

I don't think we can do different. These functions can be patched, and if it happens, they will put their return value in rax. So we'd better expect it there.
Same goes for rdi, as they will expect the value to be there as an input.

I don't think it will spill in the normal case, as rdi is already the parameter. So the compiler will just leave it there, untouched.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#55 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 2 lines · Andi Kleen

Is this really synced with the latest version of the i386 code?

Roasted already commented on this. I will check out and change it here.

Quoted 25 lines
Quoted 25 lines
arch/x86_64/kernel/paravirt.c new file +22 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
 
 
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/efi.h>
+#include <linux/bcd.h>
+#include <linux/start_kernel.h>
+
+#include <asm/bug.h>
+#include <asm/paravirt.h>
+#include <asm/desc.h>
+#include <asm/setup.h>
+#include <asm/irq.h>
+#include <asm/delay.h>
+#include <asm/fixmap.h>
+#include <asm/apic.h>
+#include <asm/tlbflush.h>
+#include <asm/msr.h>
+#include <asm/page.h>
+#include <asm/pgtable.h>
+#include <asm/proto.h>
Quoted 8 lines

arch/x86_64/kernel/paravirt.c line 43

Quoted 5 lines
arch/x86_64/kernel/paravirt.c new file +5 -0
+#include <asm/e820.h>
+#include <asm/time.h>
+#include <asm/asm-offsets.h>
+#include <asm/smp.h>
+#include <asm/irqflags.h>

Are the includes really all needed?

delay.h is not needed anymore. Most of them, could be maybe moved to paravirt.c , which is the one that really needs all the native_ things. Yeah, it will be better code this way, will change.

Quoted 7 lines

arch/x86_64/kernel/paravirt.c line 226

Quoted 4 lines
arch/x86_64/kernel/paravirt.c new file +3 -0
+     if (opfunc == NULL)
+             /* If there's no function, patch it with a ud2a (BUG) */
+             ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);

This will actually give corrupted BUGs because you don't supply the full inline BUG header. Perhaps another trap would be better.

You mean this:

Quoted 1 line
Quoted 1 line

+#include <asm/bug.h>

?

Quoted 4 lines
Quoted 2 lines

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Sure.

Quoted 2 lines

Should be native_paravirt_ops I guess

makes sense.

Quoted 11 lines
Quoted 2 lines

+
+ * This generates an indirect call based on the operation type number.

The macros here don't

include/asm-x86_64/paravirt.h line 478

Quoted 3 lines
include/asm-x86_64/paravirt.h new file +3 -0
+static inline unsigned long read_msr(unsigned int msr)
+{
+     int __err;

No need for __ in inlines

Right. Thanks.

Quoted 4 lines

include/asm-x86_64/paravirt.h line 607

Quoted 2 lines
include/asm-x86_64/paravirt.h new file +2 -0
+/* The paravirtualized I/O functions */
+static inline void slow_down_io(void) {

I doubt this needs to be inline and it's large

In a second look, i386 have such a function in io.h because they need slow_down_io in a bunch of I/O instructions. It seems that we do not. Could we just get rid of it, then?

Quoted 3 lines
Quoted 1 line

+ __asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)

No __*__ in new code please

Yup, will fix.

Quoted 17 lines

include/asm-x86_64/paravirt.h line 769

Quoted 12 lines
include/asm-x86_64/paravirt.h new file +12 -0
+                          : "=a"(f)
+                          : paravirt_type(save_fl),
+                            paravirt_clobber(CLBR_RAX)
+                          : "memory", "cc");
+     return f;
+}
+
+static inline void raw_local_irq_restore(unsigned long f)
+{
+     __asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)
+                          :
+                          : "D" (f),

Have you investigated if a different input register generates better/smaller code? I would assume rdi to be usually used already for the caller's arguments so it will produce spilling

Quoted 1 line

Similar for the rax return in the other functions.

I don't think we can do different. These functions can be patched, and if it happens, they will put their return value in rax. So we'd better expect it there.
Same goes for rdi, as they will expect the value to be there as an input.

I don't think it will spill in the normal case, as rdi is already the parameter. So the compiler will just leave it there, untouched.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#176 lore raw
In reply to Glauber de Oliveira Costa
Quoted 3 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence, here

is

Quoted 5 lines

the updated version of paravirt_ops for x86_64, heading to inclusion.

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its mappings (e.g. initilal text/data, direct mapping of physical memory) under your paravirt_ops? As far as I look at the patches, I don't find one.

Jun
---
Intel Open Source Technology Center

#148 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 3 lines
Quoted 1 line

Similar.

I don't think so. They are live here, but restore_args follows, so we can safely clobber anything here. Right?

The non argument registers cannot be clobbered.

But they are not. Yeah, I ommited it in the changelog, (it is in a comment at paravirt.h), should probably include. the CLBR_ defines only accounts for the caller-saved registers.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#177 lore raw
In reply to Glauber de Oliveira Costa
Quoted 3 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence, here

is

Quoted 5 lines

the updated version of paravirt_ops for x86_64, heading to inclusion.

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its mappings (e.g. initilal text/data, direct mapping of physical memory) under your paravirt_ops? As far as I look at the patches, I don't find one.

Jun
---
Intel Open Source Technology Center

#149 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 3 lines
Quoted 1 line

Similar.

I don't think so. They are live here, but restore_args follows, so we can safely clobber anything here. Right?

The non argument registers cannot be clobbered.

But they are not. Yeah, I ommited it in the changelog, (it is in a comment at paravirt.h), should probably include. the CLBR_ defines only accounts for the caller-saved registers.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#178 lore raw
In reply to Nakajima, Jun
Quoted 7 lines · Jun
Quoted 3 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence, here

is

Quoted 2 lines

the updated version of paravirt_ops for x86_64, heading to inclusion.

Quoted 7 lines
Quoted 3 lines

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its mappings (e.g. initilal text/data, direct mapping of physical memory) under your paravirt_ops? As far as I look at the patches, I don't find one.

I don't think how it could be relevant here. lguest kernel does use 2MB pages, and it goes smootly. For 2MB pages, we will update the page tables in the very same way, and in the very places we did before. Just that the operations can now be overwritten.

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#179 lore raw
In reply to Nakajima, Jun
Quoted 7 lines · Jun
Quoted 3 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence, here

is

Quoted 2 lines

the updated version of paravirt_ops for x86_64, heading to inclusion.

Quoted 7 lines
Quoted 3 lines

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its mappings (e.g. initilal text/data, direct mapping of physical memory) under your paravirt_ops? As far as I look at the patches, I don't find one.

I don't think how it could be relevant here. lguest kernel does use 2MB pages, and it goes smootly. For 2MB pages, we will update the page tables in the very same way, and in the very places we did before. Just that the operations can now be overwritten.

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#131 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

--

Quoted 10 lines · Andi Kleen
Quoted 6 lines · Steven Rostedt

[...]

	asm volatile ("pushq %2; pushq %%rsp; pushfq; pushq %3; call *%6;"
		      /* The stack we pushed is off by 8, due to the
previous pushq */
		      "addq $8, %%rsp"

Weird stack inbalance, i'm surprised that works. %6 must be doing strange things.

Heh, no it's very subtle. We do the "push %rsp" after the "push %2". So on return from the call, the stack that was pushed, is really 8 bytes off. Because we didn't save the stack pointer from the entry of the asm, we saved it after doing that "push %2". :-)

We get back to this call via a retq from the HV switcher code. So that rsp that was pushed will be the stack.

Quoted 9 lines
Quoted 7 lines
	/* Need to read manual, does rdmsr clear
	 * the top 32 bits of rax? */
	xor	%rax, %rax

	movl  	$MSR_GS_BASE,%ecx
	rdmsr
Quoted 6 lines

This seems incredibly slow. Since GS changes are controlled in the kernel why don't you just cache them in the per cpu area? The only case where user can reload it is using segment selector changes, which can be also handled.

Also why do you need the guest gs value anyways?

I did a swapgs here, so we have the HV GS value. I don't know of an easy way to read the GS value to put it into the RSP.

Quoted 3 lines

You could just use your own stack and let the guest switch to its own.

Well, it is quite complex, since we can't save anything in the writable section that the guest can't also write to. Remember, we are in ring 1 for the guest kernel, so we have no protection. All the host related stuff is in read only sections when we are using the guest cr3.

What I would like to do is have the GS point to the read only section, and load the RSP from a value there. But first we need to store the old value of RSP. The problem is, where do you store it. Hmm, I guess I can create my own "big" offset from the read only section :)

OK this gives me an idea, I *can* point the kernel GS to the vcpu read only section, and have a way to store the value with the GS offset. The read only section is always mapped before the read write section, so it should be easy to calculate the diff!

Thanks for the comment, you guided me to this great idea!

-- Steve

#132 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Andi Kleen

--

Quoted 10 lines · Andi Kleen
Quoted 6 lines · Steven Rostedt

[...]

	asm volatile ("pushq %2; pushq %%rsp; pushfq; pushq %3; call *%6;"
		      /* The stack we pushed is off by 8, due to the
previous pushq */
		      "addq $8, %%rsp"

Weird stack inbalance, i'm surprised that works. %6 must be doing strange things.

Heh, no it's very subtle. We do the "push %rsp" after the "push %2". So on return from the call, the stack that was pushed, is really 8 bytes off. Because we didn't save the stack pointer from the entry of the asm, we saved it after doing that "push %2". :-)

We get back to this call via a retq from the HV switcher code. So that rsp that was pushed will be the stack.

Quoted 9 lines
Quoted 7 lines
	/* Need to read manual, does rdmsr clear
	 * the top 32 bits of rax? */
	xor	%rax, %rax

	movl  	$MSR_GS_BASE,%ecx
	rdmsr
Quoted 6 lines

This seems incredibly slow. Since GS changes are controlled in the kernel why don't you just cache them in the per cpu area? The only case where user can reload it is using segment selector changes, which can be also handled.

Also why do you need the guest gs value anyways?

I did a swapgs here, so we have the HV GS value. I don't know of an easy way to read the GS value to put it into the RSP.

Quoted 3 lines

You could just use your own stack and let the guest switch to its own.

Well, it is quite complex, since we can't save anything in the writable section that the guest can't also write to. Remember, we are in ring 1 for the guest kernel, so we have no protection. All the host related stuff is in read only sections when we are using the guest cr3.

What I would like to do is have the GS point to the read only section, and load the RSP from a value there. But first we need to store the old value of RSP. The problem is, where do you store it. Hmm, I guess I can create my own "big" offset from the read only section :)

OK this gives me an idea, I *can* point the kernel GS to the vcpu read only section, and have a way to store the value with the GS offset. The read only section is always mapped before the read write section, so it should be easy to calculate the diff!

Thanks for the comment, you guided me to this great idea!

-- Steve

#56 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Andi Kleen
Quoted 1 line

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Sure.

We ended up making it EXPORT_SYMBOL in i386 because every driver wants to save and restore interrupt state.

But questionably-licensed drivers might be less of a concern on x86-64.

Cheers,
Rusty.

#57 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Andi Kleen
Quoted 1 line

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Sure.

We ended up making it EXPORT_SYMBOL in i386 because every driver wants to save and restore interrupt state.

But questionably-licensed drivers might be less of a concern on x86-64.

Cheers,
Rusty.

#58 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

Quoted 9 lines · On Thursday 09 August 2007 01:18:57 Rusty Russell wrote:
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Andi Kleen
Quoted 1 line

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Sure.

We ended up making it EXPORT_SYMBOL in i386 because every driver wants to save and restore interrupt state.

Ah true.

Quoted 1 line

But questionably-licensed drivers might be less of a concern on x86-64.

Nvidia/ATI and other binary modules exist too and users will probably unhappy if they cannot run them anymore.

But at usually irq state changes should be patched in anyways and won't need paravirt I guess?

Hmm, actually thinking about it the module loader probably has no clue that the relocation it linked will be overwritten so it'll check for the export anyways.

So the alternatives would be to add ugly hacks to the module loader or split paravirt_ops in "common" and "low level system" areas or export it as a normal export.

Not sure what's best. Ok using a normal export is easiest and not that big an issue.

-Andi

#59 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Rusty Russell
Quoted 9 lines · On Thursday 09 August 2007 01:18:57 Rusty Russell wrote:
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Andi Kleen
Quoted 1 line

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

Sure.

We ended up making it EXPORT_SYMBOL in i386 because every driver wants to save and restore interrupt state.

Ah true.

Quoted 1 line

But questionably-licensed drivers might be less of a concern on x86-64.

Nvidia/ATI and other binary modules exist too and users will probably unhappy if they cannot run them anymore.

But at usually irq state changes should be patched in anyways and won't need paravirt I guess?

Hmm, actually thinking about it the module loader probably has no clue that the relocation it linked will be overwritten so it'll check for the export anyways.

So the alternatives would be to add ugly hacks to the module loader or split paravirt_ops in "common" and "low level system" areas or export it as a normal export.

Not sure what's best. Ok using a normal export is easiest and not that big an issue.

-Andi

#180 lore raw
In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 4 lines · Jun
Quoted 3 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence,

here is

Quoted 1 line
Quoted 1 line
Quoted 1 line

the updated version of paravirt_ops for x86_64, heading to

inclusion.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its

mappings

Quoted 1 line
Quoted 1 line

(e.g. initilal text/data, direct mapping of physical memory) under

your

Quoted 10 lines
Quoted 1 line

paravirt_ops? As far as I look at the patches, I don't find one.

I don't think how it could be relevant here. lguest kernel does use 2MB pages, and it goes smootly. For 2MB pages, we will update the page tables in the very same way, and in the very places we did before. Just that the operations can now be overwritten.

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

Some MMU-related PV techiniques (including Xen, and direct paging mode for Xen/KVM) need to write-protect page tables, avoiding to use 2MB pages when mapping page tables. Looks like you did not, and that exaplains why the patches are missing the relevant (many) paravirt_ops in include/asm-x86_64/pgalloc.h, for example, compared with the i386 tree.

Jun
---
Intel Open Source Technology Center

#181 lore raw
In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 4 lines · Jun
Quoted 3 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence,

here is

Quoted 1 line
Quoted 1 line
Quoted 1 line

the updated version of paravirt_ops for x86_64, heading to

inclusion.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its

mappings

Quoted 1 line
Quoted 1 line

(e.g. initilal text/data, direct mapping of physical memory) under

your

Quoted 10 lines
Quoted 1 line

paravirt_ops? As far as I look at the patches, I don't find one.

I don't think how it could be relevant here. lguest kernel does use 2MB pages, and it goes smootly. For 2MB pages, we will update the page tables in the very same way, and in the very places we did before. Just that the operations can now be overwritten.

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

Some MMU-related PV techiniques (including Xen, and direct paging mode for Xen/KVM) need to write-protect page tables, avoiding to use 2MB pages when mapping page tables. Looks like you did not, and that exaplains why the patches are missing the relevant (many) paravirt_ops in include/asm-x86_64/pgalloc.h, for example, compared with the i386 tree.

Jun
---
Intel Open Source Technology Center

#182 lore raw
Quoted 10 lines · Jun
Quoted 3 lines

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

Some MMU-related PV techiniques (including Xen, and direct paging mode for Xen/KVM) need to write-protect page tables, avoiding to use 2MB pages when mapping page tables. Looks like you did not, and that exaplains why the patches are missing the relevant (many) paravirt_ops in include/asm-x86_64/pgalloc.h, for example, compared with the i386 tree.

I see.

I'll address this in the next version of the patch.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#183 lore raw
In reply to Nakajima, Jun
Quoted 10 lines · Jun
Quoted 3 lines

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

Some MMU-related PV techiniques (including Xen, and direct paging mode for Xen/KVM) need to write-protect page tables, avoiding to use 2MB pages when mapping page tables. Looks like you did not, and that exaplains why the patches are missing the relevant (many) paravirt_ops in include/asm-x86_64/pgalloc.h, for example, compared with the i386 tree.

I see.

I'll address this in the next version of the patch.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#60 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 2 lines · Andi Kleen

Not sure what's best. Ok using a normal export is easiest and not that big an issue.

Yeah, there's also been talk of breaking up paravirt_ops into multiple structs by area, which would lead naturally into finer-grained export control.

Cheers,
Rusty.

#61 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 2 lines · Andi Kleen

Not sure what's best. Ok using a normal export is easiest and not that big an issue.

Yeah, there's also been talk of breaking up paravirt_ops into multiple structs by area, which would lead naturally into finer-grained export control.

Cheers,
Rusty.

#133 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 5 lines · Steven Rostedt

/*
* x86 arch doesn't have an easy way to find out where
* gs is located. So we need to read the MSR. But first
* we need to save off the rcx, rax and rdx.

Why don't you store it in gs? movq %gs:my_gs_base, %rax?

J

#134 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Steven Rostedt
Quoted 5 lines · Steven Rostedt

/*
* x86 arch doesn't have an easy way to find out where
* gs is located. So we need to read the MSR. But first
* we need to save off the rcx, rax and rdx.

Why don't you store it in gs? movq %gs:my_gs_base, %rax?

J

#184 lore raw
In reply to Glauber de Oliveira Costa
Quoted 21 lines · Glauber de Oliveira Costa
Quoted 10 lines · Jun
Quoted 4 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence, here

is

Quoted 2 lines

the updated version of paravirt_ops for x86_64, heading to inclusion.

Quoted 8 lines
Quoted 4 lines

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its mappings (e.g. initilal text/data, direct mapping of physical memory) under your paravirt_ops? As far as I look at the patches, I don't find one.

Quoted 9 lines

I don't think how it could be relevant here. lguest kernel does use 2MB pages, and it goes smootly. For 2MB pages, we will update the page tables in the very same way, and in the very places we did before. Just that the operations can now be overwritten.

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

At the moment Xen can't support guests with 2M pages. In 32-bit this isn't a huge problem, since the kernel doesn't assume it can map itself with 2M pages. But I think the 64-bit kernel assumes 2M pages are always available for mapping the kernel. I don't know how pervasive this assumption is, but it would be nice to parameterize it in pv-ops.

J

#185 lore raw
In reply to Glauber de Oliveira Costa
Quoted 21 lines · Glauber de Oliveira Costa
Quoted 10 lines · Jun
Quoted 4 lines · Glauber de Oliveira Costa

Hi folks,

After some time away from it, and a big rebase as a consequence, here

is

Quoted 2 lines

the updated version of paravirt_ops for x86_64, heading to inclusion.

Quoted 8 lines
Quoted 4 lines

Your criticism is of course, very welcome.

Have fun

Do you assume that the kernel ougtht to use 2MB pages for its mappings (e.g. initilal text/data, direct mapping of physical memory) under your paravirt_ops? As far as I look at the patches, I don't find one.

Quoted 9 lines

I don't think how it could be relevant here. lguest kernel does use 2MB pages, and it goes smootly. For 2MB pages, we will update the page tables in the very same way, and in the very places we did before. Just that the operations can now be overwritten.

So, unless I'm very wrong, it only makes sense to talk about not supporting large pages in the guest level. But it is not a paravirt_ops problem.

At the moment Xen can't support guests with 2M pages. In 32-bit this isn't a huge problem, since the kernel doesn't assume it can map itself with 2M pages. But I think the 64-bit kernel assumes 2M pages are always available for mapping the kernel. I don't know how pervasive this assumption is, but it would be nice to parameterize it in pv-ops.

J

#62 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Rusty Russell
Quoted 9 lines · Rusty Russell
Quoted 3 lines · Andi Kleen

Not sure what's best. Ok using a normal export is easiest and not that big an issue.

Yeah, there's also been talk of breaking up paravirt_ops into multiple structs by area, which would lead naturally into finer-grained export control.

Yeah, I should dust that patch off again.

J

#63 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Rusty Russell
Quoted 9 lines · Rusty Russell
Quoted 3 lines · Andi Kleen

Not sure what's best. Ok using a normal export is easiest and not that big an issue.

Yeah, there's also been talk of breaking up paravirt_ops into multiple structs by area, which would lead naturally into finer-grained export control.

Yeah, I should dust that patch off again.

J

#162 lore raw

Re: [PATCH 4/25] [PATCH] Add debugreg/load_rsp native hooks

In reply to Andi Kleen
Quoted 4 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/processor.h +0 -0
@@ -264,13 +270,64 @@ struct thread_struct {
 	set_fs(USER_DS);							 \
 } while(0)
 
Quoted 14 lines

include/asm-x86_64/processor.h struct thread_struct { · line 331

Quoted 11 lines
include/asm-x86_64/processor.h +3 -7
-#define get_debugreg(var, register)				\
-		__asm__("movq %%db" #register ", %0"		\
-			:"=r" (var))
-#define set_debugreg(value, register)			\
-		__asm__("movq %0,%%db" #register		\
-			: /* no output */			\
-			:"r" (value))
+static inline unsigned long native_get_debugreg(int regno)
+{
+	unsigned long val;
 

It would be better to have own functions for each debug register I think

? A separate pvop for each? Seems excessive. And surely this should be identical to 32bit either way.

J

#163 lore raw

Re: [PATCH 4/25] [PATCH] Add debugreg/load_rsp native hooks

In reply to Andi Kleen
Quoted 4 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/processor.h +0 -0
@@ -264,13 +270,64 @@ struct thread_struct {
 	set_fs(USER_DS);							 \
 } while(0)
 
Quoted 14 lines

include/asm-x86_64/processor.h struct thread_struct { · line 331

Quoted 11 lines
include/asm-x86_64/processor.h +3 -7
-#define get_debugreg(var, register)				\
-		__asm__("movq %%db" #register ", %0"		\
-			:"=r" (var))
-#define set_debugreg(value, register)			\
-		__asm__("movq %0,%%db" #register		\
-			: /* no output */			\
-			:"r" (value))
+static inline unsigned long native_get_debugreg(int regno)
+{
+	unsigned long val;
 

It would be better to have own functions for each debug register I think

? A separate pvop for each? Seems excessive. And surely this should be identical to 32bit either way.

J

#164 lore raw

Re: [PATCH 4/25] [PATCH] Add debugreg/load_rsp native hooks

In reply to Andi Kleen
Quoted 6 lines · Andi Kleen
Quoted 6 lines
include/asm-x86_64/processor.h +0 -0
@@ -264,13 +270,64 @@ struct thread_struct {
      set_fs(USER_DS);                                                         \
 } while(0)
 
Quoted 13 lines

include/asm-x86_64/processor.h struct thread_struct { · line 275

Quoted 10 lines
include/asm-x86_64/processor.h +3 -7
-#define get_debugreg(var, register)                          \
-             __asm__("movq %%db" #register ", %0"            \
-                     :"=r" (var))
-#define set_debugreg(value, register)                        \
-             __asm__("movq %0,%%db" #register                \
-                     : /* no output */                       \
-                     :"r" (value))
+static inline unsigned long native_get_debugreg(int regno)
+{
+     unsigned long val;

It would be better to have own functions for each debug register I think

Andi, you mean:
a) split the debugreg paravirt_ops in various paravirt_ops.set/get_debugreg{X,Y,Z...}, and then join them together in a set/get_debugreg(a,b) to keep the current interface. OR
b) keep one paravirt_ops for each set/get_debugreg, then split then in various set/get_debugregX(a, b), changing the current interface, OR
c) plit the debugreg paravirt_ops in various paravirt_ops.set/get_debugreg{X,Y,Z...}, and give each its own function set/get_debugregX(a, b), again, changing the current interface, OR
d) None of the above?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#165 lore raw

Re: [PATCH 4/25] [PATCH] Add debugreg/load_rsp native hooks

In reply to Andi Kleen
Quoted 6 lines · Andi Kleen
Quoted 6 lines
include/asm-x86_64/processor.h +0 -0
@@ -264,13 +270,64 @@ struct thread_struct {
      set_fs(USER_DS);                                                         \
 } while(0)
 
Quoted 13 lines

include/asm-x86_64/processor.h struct thread_struct { · line 275

Quoted 10 lines
include/asm-x86_64/processor.h +3 -7
-#define get_debugreg(var, register)                          \
-             __asm__("movq %%db" #register ", %0"            \
-                     :"=r" (var))
-#define set_debugreg(value, register)                        \
-             __asm__("movq %0,%%db" #register                \
-                     : /* no output */                       \
-                     :"r" (value))
+static inline unsigned long native_get_debugreg(int regno)
+{
+     unsigned long val;

It would be better to have own functions for each debug register I think

Andi, you mean:
a) split the debugreg paravirt_ops in various paravirt_ops.set/get_debugreg{X,Y,Z...}, and then join them together in a set/get_debugreg(a,b) to keep the current interface. OR
b) keep one paravirt_ops for each set/get_debugreg, then split then in various set/get_debugregX(a, b), changing the current interface, OR
c) plit the debugreg paravirt_ops in various paravirt_ops.set/get_debugreg{X,Y,Z...}, and give each its own function set/get_debugregX(a, b), again, changing the current interface, OR
d) None of the above?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#156 lore raw

Re: [PATCH 7/25] [PATCH] interrupt related native paravirt functions.

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa

The interrupt initialization routine becomes native_init_IRQ and will be overriden later in case paravirt is on.

The interrupt vector is made global, so paravirt guests can reference it in their initializations.

Why? And if so, wouldn't it be better to add an accessor function instead?

J

#157 lore raw

Re: [PATCH 7/25] [PATCH] interrupt related native paravirt functions.

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa

The interrupt initialization routine becomes native_init_IRQ and will be overriden later in case paravirt is on.

The interrupt vector is made global, so paravirt guests can reference it in their initializations.

Why? And if so, wouldn't it be better to add an accessor function instead?

J

#72 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa
Quoted 27 lines · Glauber de Oliveira Costa
arch/x86_64/kernel/paravirt.c new file +26 -0
+static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
+{
+	const unsigned char *start, *end;
+	unsigned ret;
+
+	switch(type) {
+#define SITE(x)	case PARAVIRT_PATCH(x):	start = start_##x; end = end_##x; goto patch_site
+		SITE(irq_disable);
+		SITE(irq_enable);
+		SITE(restore_fl);
+		SITE(save_fl);
+		SITE(iret);
+		SITE(sysret);
+		SITE(swapgs);
+		SITE(read_cr2);
+		SITE(read_cr3);
+		SITE(write_cr3);
+		SITE(clts);
+		SITE(flush_tlb_single);
+		SITE(wbinvd);
+#undef SITE
+
+	patch_site:
+		ret = paravirt_patch_insns(insns, len, start, end);
+		break;
+

arch/x86_64/kernel/paravirt.c line 130

Quoted 10 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+	case PARAVIRT_PATCH(make_pgd):
+	case PARAVIRT_PATCH(pgd_val):
+	case PARAVIRT_PATCH(make_pte):
+	case PARAVIRT_PATCH(pte_val):
+	case PARAVIRT_PATCH(make_pmd):
+	case PARAVIRT_PATCH(pmd_val):
+	case PARAVIRT_PATCH(make_pud):
+	case PARAVIRT_PATCH(pud_val):
+		/* These functions end up returning what
+		   they're passed in the first argument */

Is this still true with 64-bit? Either way, I don't think its worth having this here. The damage to codegen around all those sites has already happened, and the additional cost of a noop direct call is pretty trivial. I think this is a nanooptimisation which risks more problems than it could possibly be worth.

arch/x86_64/kernel/paravirt.c line 141

Quoted 9 lines
arch/x86_64/kernel/paravirt.c new file +8 -0
+	case PARAVIRT_PATCH(set_pte):
+	case PARAVIRT_PATCH(set_pmd):
+	case PARAVIRT_PATCH(set_pud):
+	case PARAVIRT_PATCH(set_pgd):
+		/* These functions end up storing the second
+		 * argument in the location pointed by the first */
+		ret = paravirt_patch_store_reg(insns, len);
+		break;

Ditto, really. Do this in a later patch if it actually seems to help.

Quoted 16 lines
arch/x86_64/kernel/paravirt.c new file +15 -0
+unsigned paravirt_patch_copy_reg(void *site, unsigned len)
+{
+	unsigned char *mov = site;
+	if (len < 3)
+		return len;
+
+	/* This is mov %rdi, %rax */
+	*mov++ = 0x48;
+	*mov++ = 0x89;
+	*mov   = 0xf8;
+	return 3;
+}
+
+unsigned paravirt_patch_store_reg(void *site, unsigned len)
+{

arch/x86_64/kernel/paravirt.c line 203

Quoted 10 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+	unsigned char *mov = site;
+	if (len < 3)
+		return len;
+
+	/* This is mov %rsi, (%rdi) */
+	*mov++ = 0x48;
+	*mov++ = 0x89;
+	*mov   = 0x37;
+	return 3;
+}

These seem excessively special-purpose. Are their only uses the ones I commented on above.

Quoted 6 lines

+/*
+ * integers must be use with care here. They can break the PARAVIRT_PATCH(x)
+ * macro, that divides the offset in the structure by 8, to get a number
+ * associated with the hook. Dividing by four would be a solution, but it
+ * would limit the future growth of the structure if needed.

Why not just stick them at the end of the structure?

J

#73 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa
Quoted 27 lines · Glauber de Oliveira Costa
arch/x86_64/kernel/paravirt.c new file +26 -0
+static unsigned native_patch(u8 type, u16 clobbers, void *insns, unsigned len)
+{
+	const unsigned char *start, *end;
+	unsigned ret;
+
+	switch(type) {
+#define SITE(x)	case PARAVIRT_PATCH(x):	start = start_##x; end = end_##x; goto patch_site
+		SITE(irq_disable);
+		SITE(irq_enable);
+		SITE(restore_fl);
+		SITE(save_fl);
+		SITE(iret);
+		SITE(sysret);
+		SITE(swapgs);
+		SITE(read_cr2);
+		SITE(read_cr3);
+		SITE(write_cr3);
+		SITE(clts);
+		SITE(flush_tlb_single);
+		SITE(wbinvd);
+#undef SITE
+
+	patch_site:
+		ret = paravirt_patch_insns(insns, len, start, end);
+		break;
+

arch/x86_64/kernel/paravirt.c line 130

Quoted 10 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+	case PARAVIRT_PATCH(make_pgd):
+	case PARAVIRT_PATCH(pgd_val):
+	case PARAVIRT_PATCH(make_pte):
+	case PARAVIRT_PATCH(pte_val):
+	case PARAVIRT_PATCH(make_pmd):
+	case PARAVIRT_PATCH(pmd_val):
+	case PARAVIRT_PATCH(make_pud):
+	case PARAVIRT_PATCH(pud_val):
+		/* These functions end up returning what
+		   they're passed in the first argument */

Is this still true with 64-bit? Either way, I don't think its worth having this here. The damage to codegen around all those sites has already happened, and the additional cost of a noop direct call is pretty trivial. I think this is a nanooptimisation which risks more problems than it could possibly be worth.

arch/x86_64/kernel/paravirt.c line 141

Quoted 9 lines
arch/x86_64/kernel/paravirt.c new file +8 -0
+	case PARAVIRT_PATCH(set_pte):
+	case PARAVIRT_PATCH(set_pmd):
+	case PARAVIRT_PATCH(set_pud):
+	case PARAVIRT_PATCH(set_pgd):
+		/* These functions end up storing the second
+		 * argument in the location pointed by the first */
+		ret = paravirt_patch_store_reg(insns, len);
+		break;

Ditto, really. Do this in a later patch if it actually seems to help.

Quoted 16 lines
arch/x86_64/kernel/paravirt.c new file +15 -0
+unsigned paravirt_patch_copy_reg(void *site, unsigned len)
+{
+	unsigned char *mov = site;
+	if (len < 3)
+		return len;
+
+	/* This is mov %rdi, %rax */
+	*mov++ = 0x48;
+	*mov++ = 0x89;
+	*mov   = 0xf8;
+	return 3;
+}
+
+unsigned paravirt_patch_store_reg(void *site, unsigned len)
+{

arch/x86_64/kernel/paravirt.c line 203

Quoted 10 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+	unsigned char *mov = site;
+	if (len < 3)
+		return len;
+
+	/* This is mov %rsi, (%rdi) */
+	*mov++ = 0x48;
+	*mov++ = 0x89;
+	*mov   = 0x37;
+	return 3;
+}

These seem excessively special-purpose. Are their only uses the ones I commented on above.

Quoted 6 lines

+/*
+ * integers must be use with care here. They can break the PARAVIRT_PATCH(x)
+ * macro, that divides the offset in the structure by 8, to get a number
+ * associated with the hook. Dividing by four would be a solution, but it
+ * would limit the future growth of the structure if needed.

Why not just stick them at the end of the structure?

J

#64 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/paravirt.c line 226

Quoted 4 lines
arch/x86_64/kernel/paravirt.c new file +3 -0
+	if (opfunc == NULL)
+		/* If there's no function, patch it with a ud2a (BUG) */
+		ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);

This will actually give corrupted BUGs because you don't supply the full inline BUG header. Perhaps another trap would be better.

The BUG handler will still report it as a normal illegal instruction. It should never happen; the main thing is that it clearly points out where the problem is (as opposed to jumping to a NULL pointer and getting the unhelpful "oh, eip is zero" symptom).

Quoted 5 lines
Quoted 2 lines

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

No, for the same reason as i386.

Quoted 6 lines
Quoted 2 lines

+extern struct paravirt_ops paravirt_ops;

Should be native_paravirt_ops I guess

No, because its the current set of pv_ops. It starts all native, but it is either completely or partially overwritten by hypervisor-specific ops.

Quoted 6 lines
Quoted 3 lines

+
+ * This generates an indirect call based on the operation type number.

The macros here don't

Yes, PARAVIRT_CALL does: "call *(paravirt_ops+%c[paravirt_typenum]*8);"

Quoted 18 lines

include/asm-x86_64/paravirt.h line 769

Quoted 13 lines
include/asm-x86_64/paravirt.h new file +12 -0
+			     : "=a"(f)
+			     : paravirt_type(save_fl),
+			       paravirt_clobber(CLBR_RAX)
+			     : "memory", "cc");
+	return f;
+}
+
+static inline void raw_local_irq_restore(unsigned long f)
+{
+	__asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)
+			     :
+			     : "D" (f),

Have you investigated if a different input register generates better/smaller code? I would assume rdi to be usually used already for the caller's arguments so it will produce spilling

Quoted 2 lines

Similar for the rax return in the other functions.

This has to match the normal C calling convention though, doesn't it?

J

#65 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen
Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/paravirt.c line 226

Quoted 4 lines
arch/x86_64/kernel/paravirt.c new file +3 -0
+	if (opfunc == NULL)
+		/* If there's no function, patch it with a ud2a (BUG) */
+		ret = paravirt_patch_insns(site, len, start_ud2a, end_ud2a);

This will actually give corrupted BUGs because you don't supply the full inline BUG header. Perhaps another trap would be better.

The BUG handler will still report it as a normal illegal instruction. It should never happen; the main thing is that it clearly points out where the problem is (as opposed to jumping to a NULL pointer and getting the unhelpful "oh, eip is zero" symptom).

Quoted 5 lines
Quoted 2 lines

+EXPORT_SYMBOL(paravirt_ops);

Definitely _GPL at least.

No, for the same reason as i386.

Quoted 6 lines
Quoted 2 lines

+extern struct paravirt_ops paravirt_ops;

Should be native_paravirt_ops I guess

No, because its the current set of pv_ops. It starts all native, but it is either completely or partially overwritten by hypervisor-specific ops.

Quoted 6 lines
Quoted 3 lines

+
+ * This generates an indirect call based on the operation type number.

The macros here don't

Yes, PARAVIRT_CALL does: "call *(paravirt_ops+%c[paravirt_typenum]*8);"

Quoted 18 lines

include/asm-x86_64/paravirt.h line 769

Quoted 13 lines
include/asm-x86_64/paravirt.h new file +12 -0
+			     : "=a"(f)
+			     : paravirt_type(save_fl),
+			       paravirt_clobber(CLBR_RAX)
+			     : "memory", "cc");
+	return f;
+}
+
+static inline void raw_local_irq_restore(unsigned long f)
+{
+	__asm__ __volatile__(paravirt_alt(PARAVIRT_CALL)
+			     :
+			     : "D" (f),

Have you investigated if a different input register generates better/smaller code? I would assume rdi to be usually used already for the caller's arguments so it will produce spilling

Quoted 2 lines

Similar for the rax return in the other functions.

This has to match the normal C calling convention though, doesn't it?

J

#74 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 12 lines

arch/x86_64/kernel/paravirt.c line 130

Quoted 11 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+     case PARAVIRT_PATCH(make_pgd):
+     case PARAVIRT_PATCH(pgd_val):
+     case PARAVIRT_PATCH(make_pte):
+     case PARAVIRT_PATCH(pte_val):
+     case PARAVIRT_PATCH(make_pmd):
+     case PARAVIRT_PATCH(pmd_val):
+     case PARAVIRT_PATCH(make_pud):
+     case PARAVIRT_PATCH(pud_val):
+             /* These functions end up returning what
+                they're passed in the first argument */
Quoted 5 lines

Is this still true with 64-bit? Either way, I don't think its worth having this here. The damage to codegen around all those sites has already happened, and the additional cost of a noop direct call is pretty trivial. I think this is a nanooptimisation which risks more problems than it could possibly be worth.

No it is not. But it is just the comment that is broken. (I forgot to update it). The case here, is that they put in rax what they receive in rdi.

Quoted 11 lines

arch/x86_64/kernel/paravirt.c line 141

Quoted 9 lines
arch/x86_64/kernel/paravirt.c new file +8 -0
+     case PARAVIRT_PATCH(set_pte):
+     case PARAVIRT_PATCH(set_pmd):
+     case PARAVIRT_PATCH(set_pud):
+     case PARAVIRT_PATCH(set_pgd):
+             /* These functions end up storing the second
+              * argument in the location pointed by the first */
+             ret = paravirt_patch_store_reg(insns, len);
+             break;

Ditto, really. Do this in a later patch if it actually seems to help.

Okay, I can remove them both.

Quoted 8 lines
Quoted 6 lines

+/*
+ * integers must be use with care here. They can break the PARAVIRT_PATCH(x)
+ * macro, that divides the offset in the structure by 8, to get a number
+ * associated with the hook. Dividing by four would be a solution, but it
+ * would limit the future growth of the structure if needed.

Why not just stick them at the end of the structure?

Does it really matter?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#75 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 12 lines

arch/x86_64/kernel/paravirt.c line 130

Quoted 11 lines
arch/x86_64/kernel/paravirt.c new file +10 -0
+     case PARAVIRT_PATCH(make_pgd):
+     case PARAVIRT_PATCH(pgd_val):
+     case PARAVIRT_PATCH(make_pte):
+     case PARAVIRT_PATCH(pte_val):
+     case PARAVIRT_PATCH(make_pmd):
+     case PARAVIRT_PATCH(pmd_val):
+     case PARAVIRT_PATCH(make_pud):
+     case PARAVIRT_PATCH(pud_val):
+             /* These functions end up returning what
+                they're passed in the first argument */
Quoted 5 lines

Is this still true with 64-bit? Either way, I don't think its worth having this here. The damage to codegen around all those sites has already happened, and the additional cost of a noop direct call is pretty trivial. I think this is a nanooptimisation which risks more problems than it could possibly be worth.

No it is not. But it is just the comment that is broken. (I forgot to update it). The case here, is that they put in rax what they receive in rdi.

Quoted 11 lines

arch/x86_64/kernel/paravirt.c line 141

Quoted 9 lines
arch/x86_64/kernel/paravirt.c new file +8 -0
+     case PARAVIRT_PATCH(set_pte):
+     case PARAVIRT_PATCH(set_pmd):
+     case PARAVIRT_PATCH(set_pud):
+     case PARAVIRT_PATCH(set_pgd):
+             /* These functions end up storing the second
+              * argument in the location pointed by the first */
+             ret = paravirt_patch_store_reg(insns, len);
+             break;

Ditto, really. Do this in a later patch if it actually seems to help.

Okay, I can remove them both.

Quoted 8 lines
Quoted 6 lines

+/*
+ * integers must be use with care here. They can break the PARAVIRT_PATCH(x)
+ * macro, that divides the offset in the structure by 8, to get a number
+ * associated with the hook. Dividing by four would be a solution, but it
+ * would limit the future growth of the structure if needed.

Why not just stick them at the end of the structure?

Does it really matter?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#76 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

Quoted 7 lines · Glauber de Oliveira Costa
Quoted 7 lines
Quoted 7 lines

+/*
+ * integers must be use with care here. They can break the PARAVIRT_PATCH(x)
+ * macro, that divides the offset in the structure by 8, to get a number
+ * associated with the hook. Dividing by four would be a solution, but it
+ * would limit the future growth of the structure if needed.

Quoted 5 lines
Quoted 2 lines

Why not just stick them at the end of the structure?

Does it really matter?

Well, yes, if alignment is an issue.

J

#77 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa
Quoted 7 lines · Glauber de Oliveira Costa
Quoted 7 lines
Quoted 7 lines

+/*
+ * integers must be use with care here. They can break the PARAVIRT_PATCH(x)
+ * macro, that divides the offset in the structure by 8, to get a number
+ * associated with the hook. Dividing by four would be a solution, but it
+ * would limit the future growth of the structure if needed.

Quoted 5 lines
Quoted 2 lines

Why not just stick them at the end of the structure?

Does it really matter?

Well, yes, if alignment is an issue.

J

#78 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

Quoted 5 lines · Jeremy Fitzhardinge
Quoted 3 lines

Does it really matter?

Well, yes, if alignment is an issue.

Of course, But the question rises from the context that they are both together at the beginning. So they are not making anybody non-aligned. Then the question: Why would putting it in the end be different to putting them _together_, aligned at the beginning ?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#79 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 3 lines

Does it really matter?

Well, yes, if alignment is an issue.

Of course, But the question rises from the context that they are both together at the beginning. So they are not making anybody non-aligned. Then the question: Why would putting it in the end be different to putting them _together_, aligned at the beginning ?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#80 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Jeremy Fitzhardinge
Quoted 3 lines

Does it really matter?

Quoted 7 lines
Quoted 2 lines

Well, yes, if alignment is an issue.

Of course, But the question rises from the context that they are both together at the beginning. So they are not making anybody non-aligned. Then the question: Why would putting it in the end be different to putting them _together_, aligned at the beginning ?

Well, the point is that if you add new ones then alignment may be an issue. Putting them at the end (with a comment explaining why they're there) will make it more robust. Though splitting them into their own sub-structure would probably be better.

Hm. So x86-64 doesn't make 64-bit pointers be 64-bit aligned?

J

#81 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Glauber de Oliveira Costa
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Jeremy Fitzhardinge
Quoted 3 lines

Does it really matter?

Quoted 7 lines
Quoted 2 lines

Well, yes, if alignment is an issue.

Of course, But the question rises from the context that they are both together at the beginning. So they are not making anybody non-aligned. Then the question: Why would putting it in the end be different to putting them _together_, aligned at the beginning ?

Well, the point is that if you add new ones then alignment may be an issue. Putting them at the end (with a comment explaining why they're there) will make it more robust. Though splitting them into their own sub-structure would probably be better.

Hm. So x86-64 doesn't make 64-bit pointers be 64-bit aligned?

J

#174 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Andi Kleen
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

Looking at it more carefully, it turns out that those functions are not eligible for being paravirt clients. They do no privileged operation at all. In fact, all they do is bit manipulation.
That said, the code got a little bit cleaner by moving them down, and so I did.

But later on, you voiced concern about making CONFIG_PARAVIRT depend on !VSMP. (and said it would be okay, because these functions would be paravirt clients: but they won't) Given this updated picture, what's your position about this?

Again, as they don't do anything besides bit manipulation, I don't think they will stop VSMP from working with PARAVIRT.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#175 lore raw

Re: [PATCH 3/25] [PATCH] irq_flags / halt routines

In reply to Andi Kleen
Quoted 17 lines · Andi Kleen
Quoted 4 lines
include/asm-x86_64/irqflags.h +3 -0
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#  ifdef CONFIG_X86_VSMP

include/asm-x86_64/irqflags.h line 31

Quoted 10 lines
include/asm-x86_64/irqflags.h +10 -0
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF) || (flags & X86_EFLAGS_AC);
+}
+#  else
+static inline int raw_irqs_disabled_flags(unsigned long flags)
+{
+     return !(flags & X86_EFLAGS_IF);
+}
+#  endif

You should really turn the vsmp special case into a paravirt client first instead of complicating all this even more.

Looking at it more carefully, it turns out that those functions are not eligible for being paravirt clients. They do no privileged operation at all. In fact, all they do is bit manipulation.
That said, the code got a little bit cleaner by moving them down, and so I did.

But later on, you voiced concern about making CONFIG_PARAVIRT depend on !VSMP. (and said it would be okay, because these functions would be paravirt clients: but they won't) Given this updated picture, what's your position about this?

Again, as they don't do anything besides bit manipulation, I don't think they will stop VSMP from working with PARAVIRT.

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#135 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Jeremy Fitzhardinge

--

Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines · Steven Rostedt

/*
* x86 arch doesn't have an easy way to find out where
* gs is located. So we need to read the MSR. But first
* we need to save off the rcx, rax and rdx.

Why don't you store it in gs? movq %gs:my_gs_base, %rax?

Because it can't be trusted. After the swapgs, we are pointing to the RW section of the HV. But by running the guest kernel in ring 1, we have no protection from the guest writing into that area too. So we can't put anything in that section and expect it to be safe after jumping to guest code. The only trusted values, is jumping in after getting there by an interrupt, where the hardware places the values onto the stack.

But with Andi's comments, I realized I can point the gs pointer to the RO area. And make a constant offset that will point up into the RW area, so we could save the stack and replace it.

-- Steve

#136 lore raw

Re: [PATCH 18/25] [PATCH] turn priviled operations into macros in entry.S

In reply to Jeremy Fitzhardinge

--

Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines · Steven Rostedt

/*
* x86 arch doesn't have an easy way to find out where
* gs is located. So we need to read the MSR. But first
* we need to save off the rcx, rax and rdx.

Why don't you store it in gs? movq %gs:my_gs_base, %rax?

Because it can't be trusted. After the swapgs, we are pointing to the RW section of the HV. But by running the guest kernel in ring 1, we have no protection from the guest writing into that area too. So we can't put anything in that section and expect it to be safe after jumping to guest code. The only trusted values, is jumping in after getting there by an interrupt, where the hardware places the values onto the stack.

But with Andi's comments, I realized I can point the gs pointer to the RO area. And make a constant offset that will point up into the RW area, so we could save the stack and replace it.

-- Steve

#82 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge

--

Quoted 14 lines · Jeremy Fitzhardinge
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Jeremy Fitzhardinge
Quoted 3 lines

Does it really matter?

Quoted 7 lines
Quoted 2 lines

Well, yes, if alignment is an issue.

Of course, But the question rises from the context that they are both together at the beginning. So they are not making anybody non-aligned. Then the question: Why would putting it in the end be different to putting them _together_, aligned at the beginning ?

Quoted 4 lines

Well, the point is that if you add new ones then alignment may be an issue. Putting them at the end (with a comment explaining why they're there) will make it more robust. Though splitting them into their own sub-structure would probably be better.

Glauber,

I was thinking of putting them at the end too, and that would make it all work better. But I didn't mention it because I was in the mindset of "well i386 has that there, we should too" :-(

Quoted 2 lines

Hm. So x86-64 doesn't make 64-bit pointers be 64-bit aligned?

yeah, it usually does. But it's one of those paranoid things, where you want it to still work even if someone later on throws an __attribute__((packed)) in on paravirt ops ;-)

-- Steve

#83 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge

--

Quoted 14 lines · Jeremy Fitzhardinge
Quoted 5 lines · Glauber de Oliveira Costa
Quoted 3 lines · Jeremy Fitzhardinge
Quoted 3 lines

Does it really matter?

Quoted 7 lines
Quoted 2 lines

Well, yes, if alignment is an issue.

Of course, But the question rises from the context that they are both together at the beginning. So they are not making anybody non-aligned. Then the question: Why would putting it in the end be different to putting them _together_, aligned at the beginning ?

Quoted 4 lines

Well, the point is that if you add new ones then alignment may be an issue. Putting them at the end (with a comment explaining why they're there) will make it more robust. Though splitting them into their own sub-structure would probably be better.

Glauber,

I was thinking of putting them at the end too, and that would make it all work better. But I didn't mention it because I was in the mindset of "well i386 has that there, we should too" :-(

Quoted 2 lines

Hm. So x86-64 doesn't make 64-bit pointers be 64-bit aligned?

yeah, it usually does. But it's one of those paranoid things, where you want it to still work even if someone later on throws an __attribute__((packed)) in on paravirt ops ;-)

-- Steve

#186 lore raw
In reply to Jeremy Fitzhardinge

--

Quoted 7 lines · Jeremy Fitzhardinge

At the moment Xen can't support guests with 2M pages. In 32-bit this isn't a huge problem, since the kernel doesn't assume it can map itself with 2M pages. But I think the 64-bit kernel assumes 2M pages are always available for mapping the kernel. I don't know how pervasive this assumption is, but it would be nice to parameterize it in pv-ops.

I got 2M pages working in lguest64, but it was a cause of a lot of pain. By removing support for 2M pages in guests, that might clean up a lot of lguest64 shadow paging as well.

The only advantage I see for 2M pages or a guest, is that it saves the guest from having extra page tables. The host still needs the map the 2M to 4K, so it's not an advantage there. It doesn't speed anything up, so I'd be all for scrapping 2M pages for guests.

-- Steve

#66 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 2 lines

This has to match the normal C calling convention though, doesn't it?

Native cli/sti/save/restore_flags are all only assembly and can be easily (in fact more easily than in C) written as pure assembler functions. Then you can use whatever calling convention you want.

While some paravirt implementations may have more complicated implementations i guess it's still a reasonable requirement to make them simple enough in pure assembler. If not they can use a trampoline, but that's hopefully not needed.

-Andi

#84 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 2 lines

Hm. So x86-64 doesn't make 64-bit pointers be 64-bit aligned?

The ABI does of course, although the penalty of not doing it on current CPUs is only minor.

-Andi

#67 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 2 lines

This has to match the normal C calling convention though, doesn't it?

Native cli/sti/save/restore_flags are all only assembly and can be easily (in fact more easily than in C) written as pure assembler functions. Then you can use whatever calling convention you want.

While some paravirt implementations may have more complicated implementations i guess it's still a reasonable requirement to make them simple enough in pure assembler. If not they can use a trampoline, but that's hopefully not needed.

-Andi

#85 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Jeremy Fitzhardinge
Quoted 2 lines

Hm. So x86-64 doesn't make 64-bit pointers be 64-bit aligned?

The ABI does of course, although the penalty of not doing it on current CPUs is only minor.

-Andi

#68 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen

--

Quoted 6 lines · Andi Kleen
Quoted 2 lines

This has to match the normal C calling convention though, doesn't it?

Native cli/sti/save/restore_flags are all only assembly and can be easily (in fact more easily than in C) written as pure assembler functions. Then you can use whatever calling convention you want.

I agree.
Should we make a paravirt_ops_asm.S file that can implement these native funcions, and so we can get rid of the C functions only doing asm?

Quoted 5 lines

While some paravirt implementations may have more complicated implementations i guess it's still a reasonable requirement to make them simple enough in pure assembler. If not they can use a trampoline, but that's hopefully not needed.

It works for lguest64. I'm sure it should be no problem with other HVs.

-- Steve

#69 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Andi Kleen

--

Quoted 6 lines · Andi Kleen
Quoted 2 lines

This has to match the normal C calling convention though, doesn't it?

Native cli/sti/save/restore_flags are all only assembly and can be easily (in fact more easily than in C) written as pure assembler functions. Then you can use whatever calling convention you want.

I agree.
Should we make a paravirt_ops_asm.S file that can implement these native funcions, and so we can get rid of the C functions only doing asm?

Quoted 5 lines

While some paravirt implementations may have more complicated implementations i guess it's still a reasonable requirement to make them simple enough in pure assembler. If not they can use a trampoline, but that's hopefully not needed.

It works for lguest64. I'm sure it should be no problem with other HVs.

-- Steve

#70 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

Quoted 15 lines · Steven Rostedt

--

Quoted 6 lines · Andi Kleen
Quoted 2 lines

This has to match the normal C calling convention though, doesn't it?

Native cli/sti/save/restore_flags are all only assembly and can be easily (in fact more easily than in C) written as pure assembler functions. Then you can use whatever calling convention you want.

I agree.
Should we make a paravirt_ops_asm.S file that can implement these native funcions, and so we can get rid of the C functions only doing asm?

Quoted 8 lines
Quoted 5 lines

While some paravirt implementations may have more complicated implementations i guess it's still a reasonable requirement to make them simple enough in pure assembler. If not they can use a trampoline, but that's hopefully not needed.

It works for lguest64. I'm sure it should be no problem with other HVs.

Hm, I can't say the idea thrills me. Lets get the thing working first, and then worry about having special per-pvop calling conventions.

J

#71 lore raw

Re: [PATCH 25/25] [PATCH] add paravirtualization support for x86_64

In reply to Steven Rostedt
Quoted 15 lines · Steven Rostedt

--

Quoted 6 lines · Andi Kleen
Quoted 2 lines

This has to match the normal C calling convention though, doesn't it?

Native cli/sti/save/restore_flags are all only assembly and can be easily (in fact more easily than in C) written as pure assembler functions. Then you can use whatever calling convention you want.

I agree.
Should we make a paravirt_ops_asm.S file that can implement these native funcions, and so we can get rid of the C functions only doing asm?

Quoted 8 lines
Quoted 5 lines

While some paravirt implementations may have more complicated implementations i guess it's still a reasonable requirement to make them simple enough in pure assembler. If not they can use a trampoline, but that's hopefully not needed.

It works for lguest64. I'm sure it should be no problem with other HVs.

Hm, I can't say the idea thrills me. Lets get the thing working first, and then worry about having special per-pvop calling conventions.

J

#92 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Andi Kleen
Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

Seems to me that we can just arrange to have the early lguest/xen setup code set the EBDA_ADDR pointer to NULL and make discover_ebda() special case that to zero out ebda_addr/size.

Quoted 26 lines
Quoted 11 lines
arch/x86_64/kernel/setup.c +7 -0
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
 	printk(KERN_INFO "Command line: %s\n", boot_command_line);

arch/x86_64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 264

Quoted 10 lines
arch/x86_64/kernel/setup.c +6 -0
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
 	saved_video_mode = SAVED_VIDEO_MODE;
 	bootloader_type = LOADER_TYPE;
 
+	/*
+	 * By returning non-zero here, a paravirt impl can choose to
+	 * skip the rest of the setup process
+	 */
+	if (paravirt_arch_setup())
+		return;
 

Sorry, but that's an extremly ugly and clumpsy interface and will lead to extensive code duplication in hypervisors because so much code is disabled.

Quoted 2 lines

This needs to be solved in some better way.

Yeah, it seems a bit hamfisted. Looks like it would be better to deal with it by some combination of:

1. implement pv analogues of existing functions
2. try to neutralize functions we don't care about in pv-land
3. refactoring the setup() function to make the pv-friendly and pv-hostile parts clearly distinct

and remember: native isn't a special case; its just another pv driver.

J

#93 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Andi Kleen
Quoted 8 lines · Andi Kleen

arch/x86_64/kernel/setup.c static inline void copy_edd(void) · line 210

Quoted 3 lines
arch/x86_64/kernel/setup.c +1 -1
-static void discover_ebda(void)
+void native_ebda_info(unsigned *addr, unsigned *size)

I guess it would be better to use the resources frame work here. Before checking EBDA check if it is already reserved. Then lguest/Xen can reserve these areas and stop using it.

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

Seems to me that we can just arrange to have the early lguest/xen setup code set the EBDA_ADDR pointer to NULL and make discover_ebda() special case that to zero out ebda_addr/size.

Quoted 26 lines
Quoted 11 lines
arch/x86_64/kernel/setup.c +7 -0
+/* Overridden in paravirt.c if CONFIG_PARAVIRT */
+void __attribute__((weak)) memory_setup(void)
+{
+       return setup_memory_region();
+}
+
+
 void __init setup_arch(char **cmdline_p)
 {
 	printk(KERN_INFO "Command line: %s\n", boot_command_line);

arch/x86_64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 264

Quoted 10 lines
arch/x86_64/kernel/setup.c +6 -0
@@ -231,12 +255,19 @@ void __init setup_arch(char **cmdline_p)
 	saved_video_mode = SAVED_VIDEO_MODE;
 	bootloader_type = LOADER_TYPE;
 
+	/*
+	 * By returning non-zero here, a paravirt impl can choose to
+	 * skip the rest of the setup process
+	 */
+	if (paravirt_arch_setup())
+		return;
 

Sorry, but that's an extremly ugly and clumpsy interface and will lead to extensive code duplication in hypervisors because so much code is disabled.

Quoted 2 lines

This needs to be solved in some better way.

Yeah, it seems a bit hamfisted. Looks like it would be better to deal with it by some combination of:

1. implement pv analogues of existing functions
2. try to neutralize functions we don't care about in pv-land
3. refactoring the setup() function to make the pv-friendly and pv-hostile parts clearly distinct

and remember: native isn't a special case; its just another pv driver.

J

#94 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

Quoted 2 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

#95 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

Quoted 4 lines · Alan Cox
Quoted 2 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#96 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Alan Cox
Quoted 4 lines · Alan Cox
Quoted 2 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

Signature · 5 lines
Glauber de Oliveira Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#97 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

Quoted 8 lines · On Fri, 10 Aug 2007 15:08:35 -0300 "Glauber de Oliveira Costa"…
Quoted 4 lines · Alan Cox
Quoted 2 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

An EBDA is an optional BIOS feature so providing you virtualise the relevant entry in the zero page (to avoid guest applications trying to scan the EBDA for some system tables they may want to use like DMI) as a zero entry you can ignore the EBDA.

That probably the right thing to do since the host EBDA won't match the guest environment and the guest might migrate anyway

#98 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 8 lines · On Fri, 10 Aug 2007 15:08:35 -0300 "Glauber de Oliveira Costa"…
Quoted 4 lines · Alan Cox
Quoted 2 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

An EBDA is an optional BIOS feature so providing you virtualise the relevant entry in the zero page (to avoid guest applications trying to scan the EBDA for some system tables they may want to use like DMI) as a zero entry you can ignore the EBDA.

That probably the right thing to do since the host EBDA won't match the guest environment and the guest might migrate anyway

#99 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 11 lines · Glauber de Oliveira Costa
Quoted 5 lines · Alan Cox
Quoted 3 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

It's better to make discover_ebda() quietly cope with a missing ebda for whatever reason. We could add an explicit interface to paravirt_ops to handle this one little corner, but it isn't very important, not very general and really its just clutter. Its much better to have things cope with being virtualized quietly on their own rather than hit them
all with the pv_ops hammer. pv_ops is really for things where the
hypervisor-specific code really has to get actively involved.

For Xen-domU and lguest, there probably won't be an ebda, but its quite likely that it will be present in some form for vmware, kvm and xen-dom0; in those cases the (presumably virtual) ebda will be put in place as it would be in the native case, eliminating the need for any special handling.

J

#100 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 11 lines · Glauber de Oliveira Costa
Quoted 5 lines · Alan Cox
Quoted 3 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

It's better to make discover_ebda() quietly cope with a missing ebda for whatever reason. We could add an explicit interface to paravirt_ops to handle this one little corner, but it isn't very important, not very general and really its just clutter. Its much better to have things cope with being virtualized quietly on their own rather than hit them
all with the pv_ops hammer. pv_ops is really for things where the
hypervisor-specific code really has to get actively involved.

For Xen-domU and lguest, there probably won't be an ebda, but its quite likely that it will be present in some form for vmware, kvm and xen-dom0; in those cases the (presumably virtual) ebda will be put in place as it would be in the native case, eliminating the need for any special handling.

J

#101 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

Jeremy Fitzhardinge escreveu:

Quoted 12 lines
Quoted 10 lines · Glauber de Oliveira Costa
Quoted 5 lines · Alan Cox
Quoted 3 lines

What's the EBDA actually used for? The only place which seems to use ebda_addr is in the e820 code to avoid that area as RAM.

It belongs to the firmware.

Wouldn't it be better, then, to just skip this step unconditionally if we are running a paravirtualized guest? What do we from doing it?

Quoted 8 lines

It's better to make discover_ebda() quietly cope with a missing ebda for whatever reason. We could add an explicit interface to paravirt_ops to handle this one little corner, but it isn't very important, not very general and really its just clutter. Its much better to have things cope with being virtualized quietly on their own rather than hit them
all with the pv_ops hammer. pv_ops is really for things where the
hypervisor-specific code really has to get actively involved.

I think the idea you gave me earlier of using probe_kernel_address could
work. Xen/lguest/put_yours_here that won't use an ebda would then have to unmap the page, to make sure a read would fault.

#102 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

Quoted 3 lines · Glauber de Oliveira Costa

I think the idea you gave me earlier of using probe_kernel_address could work. Xen/lguest/put_yours_here that won't use an ebda would then have to unmap the page, to make sure a read would fault.

Hm, the memory might be mapped anyway, but we could make sure its all zero. discover_ebda should be able to deal with that OK.

J

#103 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 3 lines · Glauber de Oliveira Costa

I think the idea you gave me earlier of using probe_kernel_address could work. Xen/lguest/put_yours_here that won't use an ebda would then have to unmap the page, to make sure a read would fault.

Hm, the memory might be mapped anyway, but we could make sure its all zero. discover_ebda should be able to deal with that OK.

J

#104 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

Jeremy Fitzhardinge escreveu:

Quoted 9 lines
Quoted 3 lines · Glauber de Oliveira Costa

I think the idea you gave me earlier of using probe_kernel_address could work. Xen/lguest/put_yours_here that won't use an ebda would then have to unmap the page, to make sure a read would fault.

Hm, the memory might be mapped anyway, but we could make sure its all zero. discover_ebda should be able to deal with that OK.

J

Indeed, as the EBDA_ADDR_POINTER is not aligned, this may work even better.

It seems to me safe to assume that if we read zero on that line:

ebda_addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);

We could just do ebda_size = 0 and go home happy, skipping the rest of the process.

Andi, are you okay with it ?

#107 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Jeremy Fitzhardinge

Jeremy Fitzhardinge escreveu:

Quoted 9 lines
Quoted 3 lines · Glauber de Oliveira Costa

I think the idea you gave me earlier of using probe_kernel_address could work. Xen/lguest/put_yours_here that won't use an ebda would then have to unmap the page, to make sure a read would fault.

Hm, the memory might be mapped anyway, but we could make sure its all zero. discover_ebda should be able to deal with that OK.

J

Indeed, as the EBDA_ADDR_POINTER is not aligned, this may work even better.

It seems to me safe to assume that if we read zero on that line:

ebda_addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);

We could just do ebda_size = 0 and go home happy, skipping the rest of the process.

Andi, are you okay with it ?

#105 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 9 lines · Glauber de Oliveira Costa

Indeed, as the EBDA_ADDR_POINTER is not aligned, this may work even better.

It seems to me safe to assume that if we read zero on that line:

ebda_addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);

We could just do ebda_size = 0 and go home happy, skipping the rest of the process.

Sure, but it should use probe_kernel_addr as well, just so that it will be robust against having that page unmapped too.

J

#106 lore raw

Re: [PATCH 23/25] [PATCH] paravirt hooks for arch initialization

In reply to Glauber de Oliveira Costa
Quoted 9 lines · Glauber de Oliveira Costa

Indeed, as the EBDA_ADDR_POINTER is not aligned, this may work even better.

It seems to me safe to assume that if we read zero on that line:

ebda_addr = *(unsigned short *)__va(EBDA_ADDR_POINTER);

We could just do ebda_size = 0 and go home happy, skipping the rest of the process.

Sure, but it should use probe_kernel_addr as well, just so that it will be robust against having that page unmapped too.

J