Use descriptor's functions instead of inline assembly
PATCH 1/1 Use descriptor's functions instead of inline assembly
This patch provides a new set of functions for managing the descriptor tables that can be used instead of putting the raw assembly in .c files.
arch/x86_64/kernel/head64.c +1 -1
@@ -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); - asmvolatile ("lidt %0" ::"m" (idt_descr)); +load_idt ((const struct desc_ptr *)&idt_descr);early_printk ("Kernel alive\n" );
arch/x86_64/kernel/reboot.c +2 -1
@@ -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>
@@ -132,7 +133,7 @@ void machine_emergency_restart(void) }case BOOT_TRIPLE : - __asm__ __volatile__("lidt (%0)" : :"r" (&no_idt)); +load_idt ((const struct desc_ptr *)&no_idt); __asm__ __volatile__("int3" ); reboot_type =BOOT_KBD ;
arch/x86_64/kernel/setup64.c +2 -2
@@ -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 ; - asmvolatile ("lgdt %0" ::"m" (cpu_gdt_descr[cpu])); - asmvolatile ("lidt %0" ::"m" (idt_descr)); +load_gdt ((const struct desc_ptr *)&cpu_gdt_descr[cpu]); +load_idt ((const struct desc_ptr *)&idt_descr);memset (me->thread.tls_array,0 ,GDT_ENTRY_TLS_ENTRIES *8 );syscall_init ();
arch/x86_64/kernel/suspend.c +6 -5
@@ -32,9 +32,9 @@ void __save_processor_state(struct saved_context *ctxt)/* * descriptor tables */ - asmvolatile ("sgdt %0" :"=m" (ctxt->gdt_limit)); - asmvolatile ("sidt %0" :"=m" (ctxt->idt_limit)); - asmvolatile ("str %0" :"=m" (ctxt->tr)); +store_gdt ((struct desc_ptr *)&ctxt->gdt_limit); +store_idt ((struct desc_ptr *)&ctxt->idt_limit); + ctxt->tr =store_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(). */ - asmvolatile ("lgdt %0" ::"m" (ctxt->gdt_limit)); - asmvolatile ("lidt %0" ::"m" (ctxt->idt_limit)); +load_gdt ((const struct desc_ptr *)&ctxt->gdt_limit); +load_idt ((const struct desc_ptr *)&ctxt->idt_limit); +/* * segment registers
include/asm-x86_64/desc.h +27 -0
@@ -20,6 +20,13 @@ extern struct desc_struct cpu_gdt_table[GDT_ENTRIES];#define load_LDT_desc () asm volatile("lldt %w0"::"r" (GDT_ENTRY_LDT*8))#define clear_LDT () asm volatile("lldt %w0"::"r" (0)) +static inline unsigned long store_tr (void ) +{ +unsigned long tr; + asmvolatile ("str %w0" :"=r" (tr)); +return tr; +} +/* * This is the ldt that every process will get unless we need * something other than this.
@@ -31,6 +38,16 @@ extern struct desc_ptr cpu_gdt_descr[];/* the cpu gdt accessor */ #define cpu_gdt (_cpu) ((struct desc_struct *)cpu_gdt_descr[_cpu].address) +static inline void load_gdt (const struct desc_ptr *ptr) +{ + asmvolatile ("lgdt %w0" ::"m" (*ptr)); +} + +static inline void store_gdt (struct desc_ptr *ptr) +{ + asm ("sgdt %w0" :"=m" (*ptr)); +} +static inline void _set_gate (void *adr,unsigned type,unsigned long func,unsigned dpl,unsigned ist) {struct gate_struct s;
@@ -71,6 +88,16 @@ static inline void set_system_gate_ist(int nr, void *func, unsigned ist)_set_gate (&idt_table[nr],GATE_INTERRUPT , (unsigned long ) func,3 , ist); } +static inline void load_idt (const struct desc_ptr *ptr) +{ + asmvolatile ("lidt %w0" ::"m" (*ptr)); +} + +static inline void store_idt (struct desc_ptr *dtr) +{ + asm ("sidt %w0" :"=m" (*dtr)); +} +static inline void set_tssldt_descriptor (void *ptr,unsigned long tss,unsigned type,unsigned size) {
arch/x86_64/kernel/suspend.c void __save_processor_state(struct saved_context *ctxt) · line 37
Quoted 10 lines · Glauber de Oliveira Costa
This patch provides a new set of functions for managing the descriptor tables that can be used instead of putting the raw assembly in .c files.
Signed-off-by Glauber de Oliveira Costa <[email protected]>arch/x86_64/kernel/suspend.c +3
-1- asmvolatile ("str %0" :"=m" (ctxt->tr)); +store_gdt ((struct desc_ptr *)&ctxt->gdt_limit); +store_idt ((struct desc_ptr *)&ctxt->idt_limit); + ctxt->tr =store_tr ();
Would'nt a store_tr(&ctxt->tr) be more homogeneous here?
Regards,
Frederik
Quoted 4 lines · Frederik Deweerdt
Quoted 3 lines · Glauber de Oliveira Costa
This patch provides a new set of functions for managing the descriptor tables that can be used instead of putting the raw assembly in .c files.
Quoted 8 lines
arch/x86_64/kernel/suspend.c void __save_processor_state(struct saved_context *ctxt) · line 37
Quoted 7 lines
Signed-off-by Glauber de Oliveira Costa <[email protected]>arch/x86_64/kernel/suspend.c +3
-1- asmvolatile ("str %0" :"=m" (ctxt->tr)); +store_gdt ((struct desc_ptr *)&ctxt->gdt_limit); +store_idt ((struct desc_ptr *)&ctxt->idt_limit); + ctxt->tr =store_tr ();Would'nt a
store_tr(&ctxt->tr)be more homogeneous here?
Indeed, it would. The main reason I let it this way was to keep it closer to the i386 paravirt code (which is the main reason I'm sending this patches - to pave the way for the x86_64 one), but they seem to be getting an argument for store_tr in current tree. So there's no arguable advantage on doing it my way at all ;-)
Thanks
Quoted 4 lines · Frederik Deweerdt
Quoted 3 lines · Glauber de Oliveira Costa
This patch provides a new set of functions for managing the descriptor tables that can be used instead of putting the raw assembly in .c files.
Quoted 8 lines
arch/x86_64/kernel/suspend.c void __save_processor_state(struct saved_context *ctxt) · line 37
Quoted 7 lines
Signed-off-by Glauber de Oliveira Costa <[email protected]>arch/x86_64/kernel/suspend.c +3
-1- asmvolatile ("str %0" :"=m" (ctxt->tr)); +store_gdt ((struct desc_ptr *)&ctxt->gdt_limit); +store_idt ((struct desc_ptr *)&ctxt->idt_limit); + ctxt->tr =store_tr ();Would'nt a
store_tr(&ctxt->tr)be more homogeneous here?
Not really. The task register is a segment number (a simple integer), while the sidt/sgdt instructions write the cached descriptor to memory.
--
Brian Gerst
arch/x86_64/kernel/head64.c void __init x86_64_start_kernel(char * real_mode_data) · line 73
Quoted 10 lines · Glauber de Oliveira Costa
arch/x86_64/kernel/head64.c +1
-1@@ -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); - asmvolatile ("lidt %0" ::"m" (idt_descr)); +load_idt ((const struct desc_ptr *)&idt_descr);
Cast is not required, &idt_descr is already 'struct desc_ptr *'. (Also elsewhere)
Jan